Skip to content

Commit 20a6ff0

Browse files
committed
Fix #14116 (cmdline: Improve IDE integration with --project-settings option)
1 parent a674741 commit 20a6ff0

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

cli/cmdlineparser.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
385385

386386
ImportProject::Type projectType = ImportProject::Type::NONE;
387387
ImportProject project;
388+
bool projectSettings = false;
388389
std::string vsConfig;
389390

390391
std::string platform;
@@ -1132,16 +1133,18 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11321133
}
11331134
}
11341135

1135-
// --project
1136-
else if (std::strncmp(argv[i], "--project=", 10) == 0) {
1136+
// --project, --project-settings
1137+
else if (std::strncmp(argv[i], "--project=", 10) == 0 || std::strncmp(argv[i], "--project-settings=", 19) == 0) {
11371138
if (projectType != ImportProject::Type::NONE)
11381139
{
11391140
mLogger.printError("multiple --project options are not supported.");
11401141
return Result::Fail;
11411142
}
11421143

1144+
projectSettings = (std::strncmp(argv[i], "--project-settings=", 19) == 0);
1145+
11431146
mSettings.checkAllConfigurations = false; // Can be overridden with --max-configs or --force
1144-
std::string projectFile = argv[i]+10;
1147+
std::string projectFile = std::strchr(argv[i], '=') + 1;
11451148
projectType = project.import(projectFile, &mSettings, &mSuppressions, isCppcheckPremium());
11461149
if (projectType == ImportProject::Type::CPPCHECK_GUI) {
11471150
for (const std::string &lib : project.guiProject.libraries)
@@ -1582,6 +1585,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
15821585
//mLogger.printMessage("whole program analysis requires --cppcheck-build-dir to be active with -j.");
15831586
}
15841587

1588+
if (projectSettings) {
1589+
mSettings.fileFilters.insert(mSettings.fileFilters.end(), mPathNames.cbegin(), mPathNames.cend());
1590+
mPathNames.clear();
1591+
}
1592+
15851593
if (!mPathNames.empty() && projectType != ImportProject::Type::NONE) {
15861594
mLogger.printError("--project cannot be used in conjunction with source files.");
15871595
return Result::Fail;
@@ -1918,6 +1926,16 @@ void CmdLineParser::printHelp() const
19181926
" or Visual Studio Project (*.vcxproj) you can limit\n"
19191927
" the configuration cppcheck should check.\n"
19201928
" For example: '--project-configuration=Release|Win32'\n"
1929+
" --project-settings=<file>\n"
1930+
" IDE option: Import settings from project file. Preprocessor\n"
1931+
" settings, suppressions, coding standards, etc. are imported.\n"
1932+
" Unlike --project the files to analyse is not imported with this\n"
1933+
" option.\n"
1934+
" These two commands are effectively the same:\n"
1935+
" cppcheck --project=foo --file-filter=src/test.c\n"
1936+
" cppcheck --project-settings=foo src/test.c\n"
1937+
" Both commands will load the project foo and then analyse only the\n"
1938+
" file src/test.c\n"
19211939
" -q, --quiet Do not show progress reports.\n"
19221940
" Note that this option is not mutually exclusive with --verbose.\n"
19231941
" -rp=<paths>, --relative-paths=<paths>\n"

test/testcmdlineparser.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ class TestCmdlineParser : public TestFixture {
376376
TEST_CASE(projectEmpty);
377377
TEST_CASE(projectMissing);
378378
TEST_CASE(projectNoPaths);
379+
TEST_CASE(projectSettingsBeforeSource);
380+
TEST_CASE(projectSettingsAfterSource);
379381
TEST_CASE(addon);
380382
TEST_CASE(addonMissing);
381383
#ifdef HAVE_RULES
@@ -2502,6 +2504,32 @@ class TestCmdlineParser : public TestFixture {
25022504
ASSERT_EQUALS("cppcheck: error: no C or C++ source files found.\n", logger->str());
25032505
}
25042506

2507+
void projectSettingsBeforeSource() {
2508+
REDIRECT;
2509+
ScopedFile file("project.cppcheck",
2510+
"<project>\n"
2511+
"<paths>\n"
2512+
"<dir name=\"src\"/>\n"
2513+
"</paths>\n"
2514+
"</project>");
2515+
const char * const argv[] = {"cppcheck", "--project-settings=project.cppcheck", "src/file.cpp"};
2516+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
2517+
ASSERT_EQUALS("src/file.cpp", settings->fileFilters.front());
2518+
}
2519+
2520+
void projectSettingsAfterSource() {
2521+
REDIRECT;
2522+
ScopedFile file("project.cppcheck",
2523+
"<project>\n"
2524+
"<paths>\n"
2525+
"<dir name=\"src\"/>\n"
2526+
"</paths>\n"
2527+
"</project>");
2528+
const char * const argv[] = {"cppcheck", "src/file.cpp", "--project-settings=project.cppcheck"};
2529+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
2530+
ASSERT_EQUALS("src/file.cpp", settings->fileFilters.front());
2531+
}
2532+
25052533
void addon() {
25062534
REDIRECT;
25072535
const char * const argv[] = {"cppcheck", "--addon=misra", "file.cpp"};

0 commit comments

Comments
 (0)