Skip to content

Commit fe65619

Browse files
committed
review fix
1 parent 94d7c78 commit fe65619

5 files changed

Lines changed: 95 additions & 15 deletions

File tree

‎cli/cmdlineparser.cpp‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ CmdLineParser::CmdLineParser(CmdLineLogger &logger, Settings &settings, Suppress
148148
, mSuppressions(suppressions)
149149
{}
150150

151+
// The file lister does not walk into an excluded directory, so a pattern that only matches paths in
152+
// there looks unused. Walk the paths again without excluding anything to find out which of the
153+
// remaining patterns match something. This is only done when some patterns look unused.
154+
static std::vector<std::string> unmatchedIgnoredPaths(const PathMatch &matcher, const std::vector<std::string> &pathnames, const std::set<std::string> &extra)
155+
{
156+
std::vector<std::string> unmatched = matcher.unmatched();
157+
if (unmatched.empty())
158+
return unmatched;
159+
160+
PathMatch recorder(std::move(unmatched), Path::getCurrentPath());
161+
recorder.setRecordOnly(true);
162+
std::list<FileWithDetails> files;
163+
for (const std::string &pathname : pathnames)
164+
(void)FileLister::recursiveAddFiles(files, Path::toNativeSeparators(pathname), extra, recorder);
165+
return recorder.unmatched();
166+
}
167+
151168
bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
152169
{
153170
const Result result = parseFromArgs(argc, argv);
@@ -264,7 +281,7 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
264281
return false;
265282
}
266283

267-
const auto& unmatched = matcher.unmatched();
284+
const std::vector<std::string> unmatched = unmatchedIgnoredPaths(matcher, pathnamesRef, mSettings.library.markupExtensions());
268285
if (!unmatched.empty()) {
269286
mLogger.printError("unused ignore/exclude path '" + unmatched.front() + "'. To hide warnings in certain files use suppressions instead.");
270287
return false;

‎lib/pathmatch.cpp‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool PathMatch::match(const std::string &path, Filemode mode)
4242
ret = true;
4343
}
4444
}
45-
return ret;
45+
return ret && !mRecordOnly;
4646
}
4747

4848
bool PathMatch::match(const std::string &pattern, const std::string &path, const std::string &basepath, Filemode mode, Syntax syntax)
@@ -160,14 +160,7 @@ bool PathMatch::match(const std::string &pattern, const std::string &path, const
160160
std::vector<std::string> PathMatch::unmatched() const {
161161
std::vector<std::string> ret;
162162
std::copy_if(mPatterns.cbegin(), mPatterns.cend(), std::back_inserter(ret), [this](const std::string& s) {
163-
if (mMatchedPatterns.count(s) != 0)
164-
return false;
165-
// paths inside a matched directory are not traversed, so a pattern that is
166-
// covered by a matched pattern is considered used
167-
const Filemode mode = !s.empty() && PathIterator::issep(s.back(), mSyntax) ? Filemode::directory : Filemode::regular;
168-
return std::none_of(mMatchedPatterns.cbegin(), mMatchedPatterns.cend(), [&](const std::string& matched) {
169-
return match(matched, s, mBasepath, mode, mSyntax);
170-
});
163+
return mMatchedPatterns.count(s) == 0;
171164
});
172165
return ret;
173166
}

‎lib/pathmatch.h‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,23 @@ class CPPCHECKLIB PathMatch {
173173
/**
174174
* @brief Get the patterns that have not matched any path.
175175
*
176-
* A pattern that has not matched itself is still considered used if it is covered by
177-
* a pattern that has matched, since paths inside an ignored directory are not traversed.
178-
*
179176
* @return The patterns that have not matched any path passed to match().
180177
*/
181178
std::vector<std::string> unmatched() const;
182179

180+
/**
181+
* @brief Only record the patterns that match, match() returns false.
182+
*
183+
* Use it to walk the paths in excluded directories: nothing is excluded and every
184+
* path is matched against the patterns.
185+
*
186+
* @param recordOnly true: match() records the matching patterns and returns false.
187+
*/
188+
void setRecordOnly(bool recordOnly)
189+
{
190+
mRecordOnly = recordOnly;
191+
}
192+
183193
protected:
184194
class PathIterator;
185195

@@ -191,6 +201,8 @@ class CPPCHECKLIB PathMatch {
191201
std::string mBasepath;
192202
/* The syntax to use */
193203
Syntax mSyntax;
204+
/* match() only records the matching patterns */
205+
bool mRecordOnly{};
194206
};
195207

196208
/**

‎test/testcmdlineparser.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ class TestCmdlineParser : public TestFixture {
522522
TEST_CASE(ignoreUnused1);
523523
TEST_CASE(ignoreUnused2);
524524
TEST_CASE(ignoreUsed);
525+
TEST_CASE(ignoreUsedInExcludedDir1);
526+
TEST_CASE(ignoreUsedInExcludedDir2);
527+
TEST_CASE(ignoreUsedInExcludedDir3);
528+
TEST_CASE(ignoreUnusedExcludedDir);
525529
TEST_CASE(ignoreUnusedProject);
526530
TEST_CASE(ignoreUsedProject);
527531

@@ -3682,6 +3686,47 @@ class TestCmdlineParser : public TestFixture {
36823686
ASSERT_EQUALS("file2.cpp", parser->getFiles().cbegin()->path());
36833687
}
36843688

3689+
// the files in an excluded directory are not listed, a pattern that matches them is still used
3690+
void ignoreUsedInExcludedDir1() {
3691+
REDIRECT;
3692+
ScopedFile file1("a.cpp", "", "iued1");
3693+
ScopedFile file2("b.cpp", "", "iued1/sub");
3694+
const char * const argv[] = {"cppcheck", "-iiued1/sub/", "-ib.cpp", "iued1"};
3695+
ASSERT(fillSettingsFromArgs(argv));
3696+
ASSERT_EQUALS("", logger->str());
3697+
ASSERT_EQUALS(1, parser->getFiles().size());
3698+
ASSERT_EQUALS("iued1/a.cpp", parser->getFiles().cbegin()->path());
3699+
}
3700+
3701+
void ignoreUsedInExcludedDir2() {
3702+
REDIRECT;
3703+
ScopedFile file1("a.cpp", "", "iued2");
3704+
ScopedFile file2("b.cpp", "", "iued2/sub");
3705+
const char * const argv[] = {"cppcheck", "-iiued2/sub/", "-isub/b.cpp", "iued2"};
3706+
ASSERT(fillSettingsFromArgs(argv));
3707+
ASSERT_EQUALS("", logger->str());
3708+
ASSERT_EQUALS(1, parser->getFiles().size());
3709+
}
3710+
3711+
void ignoreUsedInExcludedDir3() {
3712+
REDIRECT;
3713+
ScopedFile file1("a.cpp", "", "iued3");
3714+
ScopedFile file2("b.pb.cc", "", "iued3/third_party");
3715+
const char * const argv[] = {"cppcheck", "-iiued3/third_party/", "-i*.pb.cc", "iued3"};
3716+
ASSERT(fillSettingsFromArgs(argv));
3717+
ASSERT_EQUALS("", logger->str());
3718+
ASSERT_EQUALS(1, parser->getFiles().size());
3719+
}
3720+
3721+
void ignoreUnusedExcludedDir() {
3722+
REDIRECT;
3723+
ScopedFile file1("a.cpp", "", "iued4");
3724+
ScopedFile file2("b.cpp", "", "iued4/sub");
3725+
const char * const argv[] = {"cppcheck", "-iiued4/sub/", "-ic.cpp", "iued4"};
3726+
ASSERT(!fillSettingsFromArgs(argv));
3727+
ASSERT_EQUALS("cppcheck: error: unused ignore/exclude path 'c.cpp'. To hide warnings in certain files use suppressions instead.\n", logger->str());
3728+
}
3729+
36853730
void ignoreUnusedProject() {
36863731
REDIRECT;
36873732
ScopedFile file("compile_commands.json",

‎test/testpathmatch.cpp‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class TestPathMatch : public TestFixture {
9191
TEST_CASE(unmatched4);
9292
TEST_CASE(unmatched5);
9393
TEST_CASE(unmatched6);
94+
TEST_CASE(recordOnly);
9495
}
9596

9697
// Test empty PathMatch
@@ -362,11 +363,23 @@ class TestPathMatch : public TestFixture {
362363
}
363364

364365
void unmatched6() const {
365-
// a pattern inside a matched directory is considered used
366+
// only the patterns that matched a path are used, the text of the patterns is not compared
366367
PathMatch match({"src/", "src/foo.cpp", "src/lib/", "lib/foo.cpp"});
367368
ASSERT(match.match("src/", ifdir));
369+
ASSERT_EQUALS(3, match.unmatched().size());
370+
ASSERT_EQUALS("src/foo.cpp", match.unmatched()[0]);
371+
ASSERT_EQUALS("src/lib/", match.unmatched()[1]);
372+
ASSERT_EQUALS("lib/foo.cpp", match.unmatched()[2]);
373+
}
374+
375+
void recordOnly() const {
376+
// the matching patterns are recorded, but nothing is matched
377+
PathMatch match({"src/", "*.pb.cc", "foo.cpp"});
378+
match.setRecordOnly(true);
379+
ASSERT(!match.match("src/", ifdir));
380+
ASSERT(!match.match("src/a.pb.cc"));
368381
ASSERT_EQUALS(1, match.unmatched().size());
369-
ASSERT_EQUALS("lib/foo.cpp", match.unmatched()[0]);
382+
ASSERT_EQUALS("foo.cpp", match.unmatched()[0]);
370383
}
371384
};
372385

0 commit comments

Comments
 (0)