Skip to content

Commit fcb41e2

Browse files
authored
small cleanup of handling of ignored paths (#5757)
1 parent 6112795 commit fcb41e2

4 files changed

Lines changed: 76 additions & 9 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
227227
std::copy_if(fileSettings.cbegin(), fileSettings.cend(), std::back_inserter(mFileSettings), [&](const FileSettings &fs) {
228228
return mSettings.library.markupFile(fs.filename) && mSettings.library.processMarkupAfterCode(fs.filename);
229229
});
230+
231+
if (mFileSettings.empty()) {
232+
mLogger.printError("could not find or open any of the paths given.");
233+
return false;
234+
}
230235
}
231236

232237
if (!pathnamesRef.empty()) {
@@ -239,6 +244,7 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
239244
const bool caseSensitive = true;
240245
#endif
241246
// Execute recursiveAddFiles() to each given file parameter
247+
// TODO: verbose log which files were ignored?
242248
const PathMatch matcher(ignored, caseSensitive);
243249
for (const std::string &pathname : pathnamesRef) {
244250
const std::string err = FileLister::recursiveAddFiles(filesResolved, Path::toNativeSeparators(pathname), mSettings.library.markupExtensions(), matcher);
@@ -248,6 +254,14 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
248254
}
249255
}
250256

257+
if (filesResolved.empty()) {
258+
mLogger.printError("could not find or open any of the paths given.");
259+
// TODO: PathMatch should provide the information if files were ignored
260+
if (!ignored.empty())
261+
mLogger.printMessage("Maybe all paths were ignored?");
262+
return false;
263+
}
264+
251265
// de-duplicate files
252266
{
253267
auto it = filesResolved.begin();
@@ -283,13 +297,11 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
283297
std::copy_if(files.cbegin(), files.cend(), std::inserter(mFiles, mFiles.end()), [&](const decltype(files)::value_type& entry) {
284298
return mSettings.library.markupFile(entry.first) && mSettings.library.processMarkupAfterCode(entry.first);
285299
});
286-
}
287300

288-
if (mFiles.empty() && mFileSettings.empty()) {
289-
mLogger.printError("could not find or open any of the paths given.");
290-
if (!ignored.empty())
291-
mLogger.printMessage("Maybe all paths were ignored?");
292-
return false;
301+
if (mFiles.empty()) {
302+
mLogger.printError("could not find or open any of the paths given.");
303+
return false;
304+
}
293305
}
294306

295307
return true;

test/cli/test-more-projects.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,19 @@ def test_project_file_filter_3(tmpdir):
371371

372372

373373
def test_project_file_filter_no_match(tmpdir):
374+
test_file = os.path.join(tmpdir, 'test.cpp')
375+
with open(test_file, 'wt') as f:
376+
pass
377+
374378
project_file = os.path.join(tmpdir, 'test.cppcheck')
375379
with open(project_file, 'wt') as f:
376380
f.write(
377381
"""<?xml version="1.0" encoding="UTF-8"?>
378382
<project>
379383
<paths>
380-
<dir name="test.cpp"/>
384+
<dir name="{}"/>
381385
</paths>
382-
</project>""")
386+
</project>""".format(test_file))
383387

384388
args = ['--file-filter=*.c', '--project={}'.format(project_file)]
385389
out_lines = [
@@ -504,3 +508,27 @@ def test_project_file_duplicate_2(tmpdir):
504508
'3/3 files checked 0% done'
505509
]
506510
assert stderr == ''
511+
512+
513+
def test_project_file_ignore(tmpdir):
514+
test_file = os.path.join(tmpdir, 'test.cpp')
515+
with open(test_file, 'wt') as f:
516+
pass
517+
518+
project_file = os.path.join(tmpdir, 'test.cppcheck')
519+
with open(project_file, 'wt') as f:
520+
f.write(
521+
"""<?xml version="1.0" encoding="UTF-8"?>
522+
<project>
523+
<paths>
524+
<dir name="{}"/>
525+
</paths>
526+
</project>""".format(test_file))
527+
528+
args = ['-itest.cpp', '--project={}'.format(project_file)]
529+
out_lines = [
530+
'cppcheck: error: could not find or open any of the paths given.',
531+
'cppcheck: Maybe all paths were ignored?'
532+
]
533+
534+
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)

test/cli/test-other.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,11 @@ def test_file_filter_3(tmpdir):
598598

599599

600600
def test_file_filter_no_match(tmpdir):
601-
args = ['--file-filter=*.c', 'test.cpp']
601+
test_file = os.path.join(tmpdir, 'test.cpp')
602+
with open(test_file, 'wt'):
603+
pass
604+
605+
args = ['--file-filter=*.c', test_file]
602606
out_lines = [
603607
'cppcheck: error: could not find any files matching the filter.'
604608
]
@@ -825,3 +829,17 @@ def test_file_duplicate_2(tmpdir):
825829
'3/3 files checked 0% done'
826830
]
827831
assert stderr == ''
832+
833+
834+
def test_file_ignore(tmpdir):
835+
test_file = os.path.join(tmpdir, 'test.cpp')
836+
with open(test_file, 'wt'):
837+
pass
838+
839+
args = ['-itest.cpp', test_file]
840+
out_lines = [
841+
'cppcheck: error: could not find or open any of the paths given.',
842+
'cppcheck: Maybe all paths were ignored?'
843+
]
844+
845+
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)

test/testcmdlineparser.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ class TestCmdlineParser : public TestFixture {
350350
TEST_CASE(ignorepaths4);
351351
TEST_CASE(ignorefilepaths1);
352352
TEST_CASE(ignorefilepaths2);
353+
TEST_CASE(ignorefilepaths3);
353354

354355
TEST_CASE(checkconfig);
355356
TEST_CASE(unknownParam);
@@ -2228,6 +2229,14 @@ class TestCmdlineParser : public TestFixture {
22282229
ASSERT_EQUALS("src/foo.cpp", parser->getIgnoredPaths()[0]);
22292230
}
22302231

2232+
void ignorefilepaths3() {
2233+
REDIRECT;
2234+
const char * const argv[] = {"cppcheck", "-i", "foo.cpp", "file.cpp"};
2235+
ASSERT_EQUALS(CmdLineParser::Result::Success, parser->parseFromArgs(4, argv));
2236+
ASSERT_EQUALS(1, parser->getIgnoredPaths().size());
2237+
ASSERT_EQUALS("foo.cpp", parser->getIgnoredPaths()[0]);
2238+
}
2239+
22312240
void checkconfig() {
22322241
REDIRECT;
22332242
const char * const argv[] = {"cppcheck", "--check-config", "file.cpp"};

0 commit comments

Comments
 (0)