Skip to content

Commit be8012e

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

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

cli/cmdlineparser.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
382382
bool def = false;
383383
bool maxconfigs = false;
384384
bool debug = false;
385+
bool inputAsFilter = false; // set by: --file-filter=+
385386

386387
ImportProject::Type projectType = ImportProject::Type::NONE;
387388
ImportProject project;
@@ -768,6 +769,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
768769
mLogger.printError("Failed: --file-filter=-");
769770
return Result::Fail;
770771
}
772+
} else if (std::strcmp(filter, "+") == 0) {
773+
inputAsFilter = true;
771774
} else {
772775
mSettings.fileFilters.emplace_back(filter);
773776
}
@@ -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 (inputAsFilter) {
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;
@@ -1777,10 +1785,12 @@ void CmdLineParser::printHelp() const
17771785
" --exitcode-suppressions=<file>\n"
17781786
" Used when certain messages should be displayed but\n"
17791787
" should not cause a non-zero exitcode.\n"
1780-
" --file-filter=<str> Analyze only those files matching the given filter str\n"
1781-
" Can be used multiple times\n"
1788+
" --file-filter=<str> Analyze only those files matching the given filter str.\n"
1789+
" Can be used multiple times. When str is '-', the file\n"
1790+
" filter will be read from standard input. When str is '+',\n"
1791+
" all path arguments are treated as file filters.\n"
17821792
" Example: --file-filter=*bar.cpp analyzes only files\n"
1783-
" that end with bar.cpp.\n"
1793+
" that end with bar.cpp.\n"
17841794
" --file-list=<file> Specify the files to check in a text file. Add one\n"
17851795
" filename per line. When file is '-,' the file list will\n"
17861796
" be read from standard input.\n"

test/testcmdlineparser.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class TestCmdlineParser : public TestFixture {
208208
TEST_CASE(fileFilterFileWithDetailsSimplifiedPath);
209209
TEST_CASE(fileFilterFileWithDetailsCase);
210210
TEST_CASE(fileFilterStdin);
211+
TEST_CASE(fileFilterPlus);
211212
TEST_CASE(fileFilterNoMatch);
212213
TEST_CASE(fileList);
213214
TEST_CASE(fileListNoFile);
@@ -1233,6 +1234,19 @@ class TestCmdlineParser : public TestFixture {
12331234
ASSERT_EQUALS("file2.cpp", settings->fileFilters[1]);
12341235
}
12351236

1237+
void fileFilterPlus() {
1238+
REDIRECT;
1239+
ScopedFile file("project.cppcheck",
1240+
"<project>\n"
1241+
"<paths>\n"
1242+
"<dir name=\"src\"/>\n"
1243+
"</paths>\n"
1244+
"</project>");
1245+
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "--file-filter=+", "src/file.cpp"};
1246+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
1247+
ASSERT_EQUALS("src/file.cpp", settings->fileFilters.front());
1248+
}
1249+
12361250
void fileFilterNoMatch() {
12371251
REDIRECT;
12381252
RedirectInput input("notexist1.c\nnotexist2.cpp\n");

0 commit comments

Comments
 (0)