Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
bool def = false;
bool maxconfigs = false;
bool debug = false;
bool inputAsFilter = false; // set by: --file-filter=+

ImportProject::Type projectType = ImportProject::Type::NONE;
ImportProject project;
Expand Down Expand Up @@ -768,6 +769,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
mLogger.printError("Failed: --file-filter=-");
return Result::Fail;
}
} else if (std::strcmp(filter, "+") == 0) {
inputAsFilter = true;
} else {
mSettings.fileFilters.emplace_back(filter);
}
Expand Down Expand Up @@ -1582,6 +1585,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
//mLogger.printMessage("whole program analysis requires --cppcheck-build-dir to be active with -j.");
}

if (inputAsFilter) {
mSettings.fileFilters.insert(mSettings.fileFilters.end(), mPathNames.cbegin(), mPathNames.cend());
mPathNames.clear();
}

if (!mPathNames.empty() && projectType != ImportProject::Type::NONE) {
mLogger.printError("--project cannot be used in conjunction with source files.");
return Result::Fail;
Expand Down Expand Up @@ -1777,10 +1785,12 @@ void CmdLineParser::printHelp() const
" --exitcode-suppressions=<file>\n"
" Used when certain messages should be displayed but\n"
" should not cause a non-zero exitcode.\n"
" --file-filter=<str> Analyze only those files matching the given filter str\n"
" Can be used multiple times\n"
" --file-filter=<str> Analyze only those files matching the given filter str.\n"
" Can be used multiple times. When str is '-', the file\n"
" filter will be read from standard input. When str is '+',\n"
" given files on CLI will be treated as file filters.\n"
" Example: --file-filter=*bar.cpp analyzes only files\n"
" that end with bar.cpp.\n"
" that end with bar.cpp.\n"
" --file-list=<file> Specify the files to check in a text file. Add one\n"
" filename per line. When file is '-,' the file list will\n"
" be read from standard input.\n"
Expand Down
26 changes: 20 additions & 6 deletions test/cli/more-projects_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,13 @@ def test_clang_tidy(tmpdir):
assert stderr == ''


def test_project_file_filter(tmpdir):
@pytest.mark.parametrize("file_filter", [
['--file-filter=test.cpp'],
['--file-filter=*.cpp'],
['--file-filter=+', 'test.cpp'],
['--file-filter=+', '*.cpp'],
])
def test_project_file_filter(tmpdir, file_filter):
test_file = os.path.join(tmpdir, 'test.cpp')
with open(test_file, 'wt') as f:
pass
Expand All @@ -310,15 +316,19 @@ def test_project_file_filter(tmpdir):
</paths>
</project>""".format(test_file))

args = ['--file-filter=*.cpp', '--project={}'.format(project_file)]
args = file_filter + ['--project={}'.format(project_file)]
out_lines = [
'Checking {} ...'.format(test_file)
]

assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines)


def test_project_file_filter_2(tmpdir):
@pytest.mark.parametrize("file_filter", [
['--file-filter=*.cpp'],
['--file-filter=+', '*.cpp'],
])
def test_project_file_filter_cpp(tmpdir, file_filter):
test_file_1 = os.path.join(tmpdir, 'test.cpp')
with open(test_file_1, 'wt') as f:
pass
Expand All @@ -337,15 +347,19 @@ def test_project_file_filter_2(tmpdir):
</paths>
</project>""".format(test_file_1, test_file_2))

args = ['--file-filter=*.cpp', '--project={}'.format(project_file)]
args = file_filter + ['--project={}'.format(project_file)]
out_lines = [
'Checking {} ...'.format(test_file_1)
]

assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines)


def test_project_file_filter_3(tmpdir):
@pytest.mark.parametrize("file_filter", [
['--file-filter=*.c'],
['--file-filter=+', '*.c'],
])
def test_project_file_filter_c(tmpdir, file_filter):
test_file_1 = os.path.join(tmpdir, 'test.cpp')
with open(test_file_1, 'wt') as f:
pass
Expand All @@ -364,7 +378,7 @@ def test_project_file_filter_3(tmpdir):
</paths>
</project>""".format(test_file_1, test_file_2))

args = ['--file-filter=*.c', '--project={}'.format(project_file)]
args = file_filter + ['--project={}'.format(project_file)]
out_lines = [
'Checking {} ...'.format(test_file_2)
]
Expand Down
14 changes: 14 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(fileFilterFileWithDetailsSimplifiedPath);
TEST_CASE(fileFilterFileWithDetailsCase);
TEST_CASE(fileFilterStdin);
TEST_CASE(fileFilterPlus);
TEST_CASE(fileFilterNoMatch);
TEST_CASE(fileList);
TEST_CASE(fileListNoFile);
Expand Down Expand Up @@ -1233,6 +1234,19 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("file2.cpp", settings->fileFilters[1]);
}

void fileFilterPlus() {
REDIRECT;
ScopedFile file("project.cppcheck",
"<project>\n"
"<paths>\n"
"<dir name=\"src\"/>\n"
"</paths>\n"
"</project>");
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "--file-filter=+", "src/file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
ASSERT_EQUALS("src/file.cpp", settings->fileFilters.front());
}

void fileFilterNoMatch() {
REDIRECT;
RedirectInput input("notexist1.c\nnotexist2.cpp\n");
Expand Down