Skip to content

Commit ac1f6f7

Browse files
committed
fixed #13939 - look for platform file relative to project file (first) with CLI
1 parent 8605900 commit ac1f6f7

3 files changed

Lines changed: 59 additions & 37 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
451451
ImportProject project;
452452
std::string vsConfig;
453453

454+
std::string platform;
455+
char defaultSign = '\0';
456+
457+
std::vector<std::string> lookupPaths{argv[0]};
458+
454459
bool executorAuto = true;
455460

456461
for (int i = 1; i < argc; i++) {
@@ -847,10 +852,10 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
847852
mSettings.force = true;
848853

849854
else if (std::strcmp(argv[i], "--fsigned-char") == 0)
850-
mSettings.platform.defaultSign = 's';
855+
defaultSign = 's';
851856

852857
else if (std::strcmp(argv[i], "--funsigned-char") == 0)
853-
mSettings.platform.defaultSign = 'u';
858+
defaultSign = 'u';
854859

855860
// Ignored paths
856861
else if (std::strncmp(argv[i], "-i", 2) == 0) {
@@ -1098,26 +1103,12 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10981103

10991104
// Specify platform
11001105
else if (std::strncmp(argv[i], "--platform=", 11) == 0) {
1101-
const std::string platform(11+argv[i]);
1102-
1103-
std::string errstr;
1104-
const std::vector<std::string> paths = {argv[0]};
1105-
if (!mSettings.platform.set(platform, errstr, paths, mSettings.debuglookup || mSettings.debuglookupPlatform)) {
1106-
mLogger.printError(errstr);
1106+
std::string p = 11 + argv[i];
1107+
if (p.empty()) {
1108+
mLogger.printError("empty platform specified.");
11071109
return Result::Fail;
11081110
}
1109-
1110-
// TODO: remove
1111-
// these are loaded via external files and thus have Settings::PlatformFile set instead.
1112-
// override the type so they behave like the regular platforms.
1113-
if (platform == "unix32-unsigned") {
1114-
mSettings.platform.type = Platform::Type::Unix32;
1115-
mLogger.printMessage("The platform 'unix32-unsigned' has been deprecated and will be removed in Cppcheck 2.19. Please use '--platform=unix32 --funsigned-char' instead");
1116-
}
1117-
else if (platform == "unix64-unsigned") {
1118-
mSettings.platform.type = Platform::Type::Unix64;
1119-
mLogger.printMessage("The platform 'unix64-unsigned' has been deprecated and will be removed in Cppcheck 2.19. Please use '--platform=unix64 --funsigned-char' instead");
1120-
}
1111+
platform = std::move(p);
11211112
}
11221113

11231114
// Write results in results.plist
@@ -1210,17 +1201,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
12101201
const auto& excludedPaths = project.guiProject.excludedPaths;
12111202
std::copy(excludedPaths.cbegin(), excludedPaths.cend(), std::back_inserter(mIgnoredPaths));
12121203

1213-
std::string platform(project.guiProject.platform);
1204+
if (!project.guiProject.platform.empty())
1205+
platform = project.guiProject.platform;
12141206

1215-
// keep existing platform from command-line intact
1216-
if (!platform.empty()) {
1217-
std::string errstr;
1218-
const std::vector<std::string> paths = {projectFile, argv[0]};
1219-
if (!mSettings.platform.set(platform, errstr, paths, mSettings.debuglookup || mSettings.debuglookupPlatform)) {
1220-
mLogger.printError(errstr);
1221-
return Result::Fail;
1222-
}
1223-
}
1207+
// look for external files relative to project first
1208+
lookupPaths.insert(lookupPaths.cbegin(), projectFile);
12241209

12251210
const auto& projectFileGui = project.guiProject.projectFile;
12261211
if (!projectFileGui.empty()) {
@@ -1653,6 +1638,30 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
16531638
}
16541639
}
16551640

1641+
if (!platform.empty())
1642+
{
1643+
std::string errstr;
1644+
if (!mSettings.platform.set(platform, errstr, lookupPaths, mSettings.debuglookup || mSettings.debuglookupPlatform)) {
1645+
mLogger.printError(errstr);
1646+
return Result::Fail;
1647+
}
1648+
1649+
// TODO: remove
1650+
// these are loaded via external files and thus have Settings::PlatformFile set instead.
1651+
// override the type so they behave like the regular platforms.
1652+
if (platform == "unix32-unsigned") {
1653+
mSettings.platform.type = Platform::Type::Unix32;
1654+
mLogger.printMessage("The platform 'unix32-unsigned' has been deprecated and will be removed in Cppcheck 2.19. Please use '--platform=unix32 --funsigned-char' instead");
1655+
}
1656+
else if (platform == "unix64-unsigned") {
1657+
mSettings.platform.type = Platform::Type::Unix64;
1658+
mLogger.printMessage("The platform 'unix64-unsigned' has been deprecated and will be removed in Cppcheck 2.19. Please use '--platform=unix64 --funsigned-char' instead");
1659+
}
1660+
}
1661+
1662+
if (defaultSign != '\0')
1663+
mSettings.platform.defaultSign = defaultSign;
1664+
16561665
if (!mSettings.buildDir.empty() && !Path::isDirectory(mSettings.buildDir)) {
16571666
mLogger.printError("Directory '" + mSettings.buildDir + "' specified by --cppcheck-build-dir argument has to be existent.");
16581667
return Result::Fail;

test/cli/lookup_test.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_lib_lookup_notfound_project(tmpdir): # #13938
134134
]
135135

136136

137-
def test_lib_lookup_notfound_compdb(tmpdir): # #13938
137+
def test_lib_lookup_notfound_compdb(tmpdir):
138138
compdb_file, _ = __create_compdb(tmpdir)
139139

140140
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=library', '--library=none', '--project={}'.format(compdb_file)])
@@ -144,7 +144,6 @@ def test_lib_lookup_notfound_compdb(tmpdir): # #13938
144144
assert exitcode == 1, stdout
145145
lines = __remove_std_lookup_log(stdout.splitlines(), exepath)
146146
assert lines == [
147-
# TODO: needs to look relative to the project first
148147
# TODO: specify which folder is actually used for lookup here
149148
"looking for library 'none.cfg'",
150149
"looking for library '{}/none.cfg'".format(exepath),
@@ -409,6 +408,7 @@ def test_platform_lookup_notfound(tmpdir):
409408

410409
def test_platform_lookup_notfound_project(tmpdir): # #13939
411410
project_file, _ = __create_gui_project(tmpdir)
411+
project_path = os.path.dirname(project_file)
412412

413413
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=none', '--project={}'.format(project_file)])
414414
exepath = os.path.dirname(exe)
@@ -419,8 +419,15 @@ def test_platform_lookup_notfound_project(tmpdir): # #13939
419419
assert exitcode == 1, stdout
420420
lines = stdout.splitlines()
421421
assert lines == [
422-
# TODO: needs to look relative to project file first
422+
# TODO: the CWD lookups are duplicated
423+
# TODO: needs to do the relative project lookup first
424+
"looking for platform 'none' relative to '{}'".format(project_file),
425+
"try to load platform file 'none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none.xml",
426+
"try to load platform file 'platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none.xml",
427+
"try to load platform file '{}/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/none.xml".format(project_path, project_path),
428+
"try to load platform file '{}/platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platforms/none.xml".format(project_path, project_path),
423429
"looking for platform 'none' relative to '{}'".format(exepath_bin),
430+
# TODO: should we really check CWD before relative to executable? should we check CWD at all?
424431
"try to load platform file 'none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none.xml",
425432
"try to load platform file 'platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none.xml",
426433
"try to load platform file '{}/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/none.xml".format(exepath, exepath),
@@ -429,7 +436,7 @@ def test_platform_lookup_notfound_project(tmpdir): # #13939
429436
]
430437

431438

432-
def test_platform_lookup_notfound_compdb(tmpdir): # #13939
439+
def test_platform_lookup_notfound_compdb(tmpdir):
433440
compdb_file, _ = __create_compdb(tmpdir)
434441

435442
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=none', '--project={}'.format(compdb_file)])
@@ -441,7 +448,6 @@ def test_platform_lookup_notfound_compdb(tmpdir): # #13939
441448
assert exitcode == 1, stdout
442449
lines = stdout.splitlines()
443450
assert lines == [
444-
# TODO: needs to look relative to project file first
445451
"looking for platform 'none' relative to '{}'".format(exepath_bin),
446452
"try to load platform file 'none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none.xml",
447453
"try to load platform file 'platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none.xml",
@@ -686,7 +692,7 @@ def test_addon_lookup_notfound_project(tmpdir): # #13940 / #13941
686692
]
687693

688694

689-
def test_addon_lookup_notfound_compdb(tmpdir): # #13940
695+
def test_addon_lookup_notfound_compdb(tmpdir):
690696
compdb_file, _ = __create_compdb(tmpdir)
691697

692698
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none', '--project={}'.format(compdb_file)])
@@ -695,7 +701,6 @@ def test_addon_lookup_notfound_compdb(tmpdir): # #13940
695701
assert exitcode == 1, stdout
696702
lines = stdout.splitlines()
697703
assert lines == [
698-
# TODO: needs to look relative to the project file first
699704
"looking for addon 'none.py'",
700705
"looking for addon '{}none.py'".format(exepath_sep),
701706
"looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators

test/testcmdlineparser.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class TestCmdlineParser : public TestFixture {
271271
TEST_CASE(platformUnspecified);
272272
TEST_CASE(platformPlatformFile);
273273
TEST_CASE(platformUnknown);
274+
TEST_CASE(platformEmpty);
274275
TEST_CASE(plistEmpty);
275276
TEST_CASE(plistDoesNotExist);
276277
TEST_CASE(suppressionsOld);
@@ -1727,6 +1728,13 @@ class TestCmdlineParser : public TestFixture {
17271728
ASSERT_EQUALS("cppcheck: error: unrecognized platform: 'win128'.\n", logger->str());
17281729
}
17291730

1731+
void platformEmpty() {
1732+
REDIRECT;
1733+
const char * const argv[] = {"cppcheck", "--platform=", "file.cpp"};
1734+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parseFromArgs(argv));
1735+
ASSERT_EQUALS("cppcheck: error: empty platform specified.\n", logger->str());
1736+
}
1737+
17301738
void plistEmpty() {
17311739
REDIRECT;
17321740
const char * const argv[] = {"cppcheck", "--plist-output=", "file.cpp"};

0 commit comments

Comments
 (0)