Skip to content

Commit 5e52f82

Browse files
committed
Fix #15081 (usability: try to report misuse of exclude options)
1 parent fdc1942 commit 5e52f82

19 files changed

Lines changed: 320 additions & 84 deletions

‎cli/cmdlineparser.cpp‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
247247
std::list<FileWithDetails> filesResolved;
248248
// Execute recursiveAddFiles() to each given file parameter
249249
// TODO: verbose log which files were ignored?
250-
const PathMatch matcher(ignored, Path::getCurrentPath());
250+
PathMatch matcher(ignored, Path::getCurrentPath());
251251
for (const std::string &pathname : pathnamesRef) {
252252
const std::string err = FileLister::recursiveAddFiles(filesResolved, Path::toNativeSeparators(pathname), mSettings.library.markupExtensions(), matcher, mSettings.debugignore);
253253
if (!err.empty()) {
@@ -264,6 +264,12 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
264264
return false;
265265
}
266266

267+
const auto& unmatched = matcher.unmatched();
268+
if (!unmatched.empty()) {
269+
mLogger.printError("unused ignore/exclude path '" + unmatched.front() + "'. To hide warnings in certain files use suppressions instead.");
270+
return false;
271+
}
272+
267273
std::list<FileWithDetails> files;
268274
if (!mSettings.fileFilters.empty()) {
269275
files = filterFiles(mSettings.fileFilters, filesResolved);
@@ -1723,12 +1729,16 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
17231729
mPathNames = project.guiProject.pathNames;
17241730

17251731
if (!project.fileSettings.empty()) {
1726-
project.ignorePaths(mIgnoredPaths, mSettings.debugignore);
1732+
const auto& unmatched = project.ignorePaths(mIgnoredPaths, mSettings.debugignore);
17271733
if (project.fileSettings.empty()) {
17281734
mLogger.printError("no C or C++ source files found.");
17291735
mLogger.printMessage("all paths were ignored"); // TODO: log this differently?
17301736
return Result::Fail;
17311737
}
1738+
if (!unmatched.empty()) {
1739+
mLogger.printError("unused ignore/exclude path '" + unmatched.front() + "'. To hide warnings in certain files use suppressions instead.");
1740+
return Result::Fail;
1741+
}
17321742
mFileSettings = project.fileSettings;
17331743
}
17341744

‎cli/filelister.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
// When compiling Unicode targets WinAPI automatically uses *W Unicode versions
4747
// of called functions. Thus, we explicitly call *A versions of the functions.
4848

49-
static std::string addFiles2(std::list<FileWithDetails>&files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored, bool debug = false)
49+
static std::string addFiles2(std::list<FileWithDetails>&files, const std::string &path, const std::set<std::string> &extra, bool recursive, PathMatch& ignored, bool debug = false)
5050
{
5151
const std::string cleanedPath = Path::toNativeSeparators(path);
5252

@@ -163,7 +163,7 @@ static std::string addFiles2(std::list<FileWithDetails>&files, const std::string
163163
return "";
164164
}
165165

166-
std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored, bool debug)
166+
std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, PathMatch& ignored, bool debug)
167167
{
168168
if (path.empty())
169169
return "no path specified";
@@ -205,7 +205,7 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
205205
const std::string &path,
206206
const std::set<std::string> &extra,
207207
bool recursive,
208-
const PathMatch& ignored,
208+
PathMatch& ignored,
209209
bool debug)
210210
{
211211
if (ignored.match(path))
@@ -284,7 +284,7 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
284284
return "";
285285
}
286286

287-
std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored, bool debug)
287+
std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, PathMatch& ignored, bool debug)
288288
{
289289
if (path.empty())
290290
return "no path specified";
@@ -312,7 +312,7 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s
312312

313313
#endif
314314

315-
std::string FileLister::recursiveAddFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored, bool debug)
315+
std::string FileLister::recursiveAddFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, PathMatch& ignored, bool debug)
316316
{
317317
return addFiles(files, path, extra, true, ignored, debug);
318318
}

‎cli/filelister.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class FileLister {
4444
* @param debug log if path was ignored
4545
* @return On success, an empty string is returned. On error, a error message is returned.
4646
*/
47-
static std::string recursiveAddFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored, bool debug = false);
47+
static std::string recursiveAddFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, PathMatch& ignored, bool debug = false);
4848

4949
/**
5050
* @brief (Recursively) add source files to a map.
@@ -59,7 +59,7 @@ class FileLister {
5959
* @param debug log when a path was ignored
6060
* @return On success, an empty string is returned. On error, a error message is returned.
6161
*/
62-
static std::string addFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored, bool debug = false);
62+
static std::string addFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, PathMatch& ignored, bool debug = false);
6363
};
6464

6565
/// @}

‎gui/filelist.cpp‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ void FileList::addPathList(const QStringList &paths)
9090
}
9191
}
9292

93-
QStringList FileList::getFileList() const
93+
QStringList FileList::getFileList()
9494
{
95+
mUnmatchedExcludes.clear();
9596
if (mExcludedPaths.empty()) {
9697
QStringList names;
9798
for (const QFileInfo& item : mFileList) {
@@ -103,6 +104,11 @@ QStringList FileList::getFileList() const
103104
return applyExcludeList();
104105
}
105106

107+
const QStringList& FileList::getUnmatchedExcludes() &
108+
{
109+
return mUnmatchedExcludes;
110+
}
111+
106112
void FileList::addExcludeList(const QStringList &paths)
107113
{
108114
mExcludedPaths = paths;
@@ -117,9 +123,9 @@ static std::vector<std::string> toStdStringList(const QStringList &stringList)
117123
return ret;
118124
}
119125

120-
QStringList FileList::applyExcludeList() const
126+
QStringList FileList::applyExcludeList()
121127
{
122-
const PathMatch pathMatch(toStdStringList(mExcludedPaths), QDir::currentPath().toStdString());
128+
PathMatch pathMatch(toStdStringList(mExcludedPaths), QDir::currentPath().toStdString());
123129

124130
QStringList paths;
125131
for (const QFileInfo& item : mFileList) {
@@ -129,5 +135,10 @@ QStringList FileList::applyExcludeList() const
129135
if (!pathMatch.match(canonical.toStdString()))
130136
paths << canonical;
131137
}
138+
139+
for (const std::string& excludePath: pathMatch.unmatched()) {
140+
mUnmatchedExcludes << QString::fromStdString(excludePath);
141+
}
142+
132143
return paths;
133144
}

‎gui/filelist.h‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ class FileList {
6262
* @brief Return list of filenames (to check).
6363
* @return list of filenames to check.
6464
*/
65-
QStringList getFileList() const;
65+
QStringList getFileList();
66+
67+
/**
68+
* @brief Return list of exclude paths that did not match any file in the last getFileList() call
69+
* @return list of unmatched excludes
70+
*/
71+
const QStringList& getUnmatchedExcludes() &;
6672

6773
/**
6874
* @brief Add list of paths to exclusion list.
@@ -88,14 +94,16 @@ class FileList {
8894
* @brief Get filtered list of paths.
8995
* This method takes the list of paths and applies the exclude lists to
9096
* it. And then returns the list of paths that did not match the
91-
* exclude filters.
97+
* exclude filters. The exclude paths that did not match any file
98+
* are stored in mUnmatchedExcludes.
9299
* @return Filtered list of paths.
93100
*/
94-
QStringList applyExcludeList() const;
101+
QStringList applyExcludeList();
95102

96103
private:
97104
QFileInfoList mFileList;
98105
QStringList mExcludedPaths;
106+
QStringList mUnmatchedExcludes;
99107
};
100108

101109
#endif // FILELIST_H

‎gui/mainwindow.cpp‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLib, const bo
671671
mUI->mResults->setCheckSettings(checkSettings);
672672
}
673673

674-
void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLib, const bool checkConfig, const QStringList& selectedFiles, const Recheck recheck)
674+
void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLib, const bool checkConfig, const bool checkUnusedExcludes, const QStringList& selectedFiles, const Recheck recheck)
675675
{
676676
if (files.isEmpty())
677677
return;
@@ -706,6 +706,17 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLib, c
706706
return;
707707
}
708708

709+
const QStringList unmatchedExcludePaths = checkUnusedExcludes ? pathList.getUnmatchedExcludes() : QStringList();
710+
if (!unmatchedExcludePaths.isEmpty()) {
711+
QMessageBox msg(QMessageBox::Warning,
712+
"Cppcheck",
713+
tr("Unused exclude paths:\n%1\nto hide warnings in certain files use suppressions instead").arg(unmatchedExcludePaths.join("\n")),
714+
QMessageBox::Ok,
715+
this);
716+
msg.exec();
717+
return;
718+
}
719+
709720
std::list<FileWithDetails> fdetails = enrichFilesForAnalysis(fileNames, checkSettings);
710721

711722
// TODO: lock UI here?
@@ -2039,7 +2050,10 @@ void MainWindow::analyzeProject(const ProjectFile *projectFile, const QStringLis
20392050
if (paths.isEmpty()) {
20402051
paths << mCurrentDirectory;
20412052
}
2042-
doAnalyzeFiles(paths, checkLib, checkConfig, selectedFiles, recheck);
2053+
2054+
// the exclude paths can only be validated when the whole project is analyzed
2055+
const bool checkUnusedExcludes = recheckFiles.isEmpty();
2056+
doAnalyzeFiles(paths, checkLib, checkConfig, checkUnusedExcludes, selectedFiles, recheck);
20432057
}
20442058

20452059
void MainWindow::newProjectFile()

‎gui/mainwindow.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,11 @@ private slots:
333333
* @param files List of files and/or directories to analyze
334334
* @param checkLib Flag to indicate if library should be checked
335335
* @param checkConfig Flag to indicate if the configuration should be checked.
336+
* @param checkUnusedExcludes Flag to indicate if unused exclude paths should be reported.
336337
* @param selectedFiles Files selected for a recheck
337338
* @param recheck How the selected files are rechecked
338339
*/
339-
void doAnalyzeFiles(const QStringList &files, bool checkLib = false, bool checkConfig = false, const QStringList& selectedFiles = QStringList(), Recheck recheck = Recheck::None);
340+
void doAnalyzeFiles(const QStringList &files, bool checkLib = false, bool checkConfig = false, bool checkUnusedExcludes = false, const QStringList& selectedFiles = QStringList(), Recheck recheck = Recheck::None);
340341

341342
/**
342343
* @brief Clear the results that a new analysis replaces

‎gui/test/filelist/testfilelist.cpp‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,41 @@ void TestFileList::filterFiles5() const
184184
QVERIFY(!files.contains(base + "/dir1/dir11/foo11.cpp"));
185185
}
186186

187+
void TestFileList::unmatchedExcludes1() const
188+
{
189+
FileList list;
190+
QStringList filters;
191+
filters << "foo1.cpp" << "foo3.cc";
192+
list.addExcludeList(filters);
193+
list.addDirectory(QString(SRCDIR) + "/../data/files");
194+
QVERIFY(!list.getFileList().isEmpty());
195+
QVERIFY(list.getUnmatchedExcludes().isEmpty());
196+
}
197+
198+
void TestFileList::unmatchedExcludes2() const
199+
{
200+
FileList list;
201+
QStringList filters;
202+
filters << "foo1.cpp" << "bar.cpp" << "dir3/";
203+
list.addExcludeList(filters);
204+
list.addDirectory(QString(SRCDIR) + "/../data/files", true);
205+
// unmatched excludes does not affect the file list
206+
QCOMPARE(list.getFileList().size(), 9);
207+
const QStringList unmatched = list.getUnmatchedExcludes();
208+
QCOMPARE(unmatched.size(), 2);
209+
QCOMPARE(unmatched[0], QString("bar.cpp"));
210+
QCOMPARE(unmatched[1], QString("dir3/"));
211+
}
212+
213+
void TestFileList::unmatchedExcludes3() const
214+
{
215+
FileList list;
216+
QStringList filters;
217+
filters << "dir1/";
218+
list.addExcludeList(filters);
219+
list.addDirectory(QString(SRCDIR) + "/../data/files", true);
220+
QVERIFY(!list.getFileList().isEmpty());
221+
QVERIFY(list.getUnmatchedExcludes().isEmpty());
222+
}
223+
187224
QTEST_MAIN(TestFileList)

‎gui/test/filelist/testfilelist.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ private slots:
3333
void filterFiles3() const;
3434
void filterFiles4() const;
3535
void filterFiles5() const;
36+
void unmatchedExcludes1() const;
37+
void unmatchedExcludes2() const;
38+
void unmatchedExcludes3() const;
3639
};

‎lib/importproject.cpp‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ void ImportProject::parseArgs(FileSettings &fs, const std::vector<std::string> &
236236
fsSetDefines(fs, std::move(defs));
237237
}
238238

239-
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths, bool debug)
239+
std::vector<std::string> ImportProject::ignorePaths(const std::vector<std::string> &ipaths, bool debug)
240240
{
241241
PathMatch matcher(ipaths, Path::getCurrentPath());
242242
for (auto it = fileSettings.cbegin(); it != fileSettings.cend();) {
@@ -248,6 +248,7 @@ void ImportProject::ignorePaths(const std::vector<std::string> &ipaths, bool deb
248248
else
249249
++it;
250250
}
251+
return matcher.unmatched();
251252
}
252253

253254
void ImportProject::ignoreOtherConfigs(const std::string &cfg)
@@ -1787,27 +1788,24 @@ ImportProject::Type ImportProject::import(const std::string &filename, Settings
17871788
if (!mPath.empty() && !endsWith(mPath,'/'))
17881789
mPath += '/';
17891790

1790-
const std::vector<std::string> fileFilters =
1791-
settings ? settings->fileFilters : std::vector<std::string>();
1792-
17931791
if (endsWith(filename, ".json")) {
17941792
if (processCompileCommands(fin)) {
17951793
setRelativePaths(filename);
17961794
return ImportProject::Type::COMPILE_DB;
17971795
}
17981796
} else if (endsWith(filename, ".sln")) {
1799-
if (importSln(fin, filename, fileFilters)) {
1797+
if (importSln(fin, filename)) {
18001798
setRelativePaths(filename);
18011799
return ImportProject::Type::VS_SLN;
18021800
}
18031801
} else if (endsWith(filename, ".slnx")) {
1804-
if (importSlnx(filename, fileFilters)) {
1802+
if (importSlnx(filename)) {
18051803
setRelativePaths(filename);
18061804
return ImportProject::Type::VS_SLNX;
18071805
}
18081806
} else if (endsWith(filename, ".vcxproj")) {
18091807
PropertiesMap mVariables;
1810-
if (importVcxproj(toAbsolute(filename), mVariables, fileFilters)) {
1808+
if (importVcxproj(toAbsolute(filename), mVariables)) {
18111809
setRelativePaths(filename);
18121810
return ImportProject::Type::VS_VCXPROJ;
18131811
}
@@ -1935,7 +1933,7 @@ void ImportProject::setSolution(const std::string &filename, PropertiesMap &prop
19351933
properties["SolutionName"] = fileStem(properties["SolutionFileName"]);
19361934
}
19371935

1938-
bool ImportProject::importSln(std::istream &istr, const std::string &filename, const std::vector<std::string> &fileFilters)
1936+
bool ImportProject::importSln(std::istream &istr, const std::string &filename)
19391937
{
19401938
std::string line;
19411939

@@ -2028,7 +2026,7 @@ bool ImportProject::importSln(std::istream &istr, const std::string &filename, c
20282026

20292027
for (const std::string &vcxproj : vcxprojs) {
20302028
PropertiesMap mVariables = solutionVariables;
2031-
if (!importVcxproj(vcxproj, mVariables, fileFilters)) {
2029+
if (!importVcxproj(vcxproj, mVariables)) {
20322030
errors.emplace_back("failed to load '" + vcxproj + "' from Visual Studio solution");
20332031
return false;
20342032
}
@@ -2037,7 +2035,7 @@ bool ImportProject::importSln(std::istream &istr, const std::string &filename, c
20372035
return true;
20382036
}
20392037

2040-
bool ImportProject::importSlnx(const std::string& filename, const std::vector<std::string>& fileFilters)
2038+
bool ImportProject::importSlnx(const std::string& filename)
20412039
{
20422040
debugs.clear();
20432041

@@ -2082,7 +2080,7 @@ bool ImportProject::importSlnx(const std::string& filename, const std::vector<st
20822080
vcxproj = Path::fromNativeSeparators(std::move(vcxproj));
20832081

20842082
PropertiesMap mVariables = solutionVariables;
2085-
if (!importVcxproj(vcxproj, mVariables, fileFilters)) {
2083+
if (!importVcxproj(vcxproj, mVariables)) {
20862084
errors.emplace_back("failed to load '" + vcxproj + "' from Visual Studio solution");
20872085
return false;
20882086
}
@@ -4621,8 +4619,7 @@ ImportProject::ImportResult ImportProject::processImport(const std::string &file
46214619
}
46224620

46234621
bool ImportProject::importVcxproj(const std::string &filename,
4624-
PropertiesMap &properties,
4625-
const std::vector<std::string> &fileFilters)
4622+
PropertiesMap &properties)
46264623
{
46274624
tinyxml2::XMLDocument doc;
46284625
const tinyxml2::XMLError error = doc.LoadFile(filename.c_str());
@@ -4946,11 +4943,7 @@ bool ImportProject::importVcxproj(const std::string &filename,
49464943
// we can only set it globally but in this context it needs to be treated per file
49474944

49484945
// Project files
4949-
PathMatch filtermatcher(fileFilters, Path::getCurrentPath());
49504946
for (const ItemGroupClCompile &compile : compileList) {
4951-
if (!fileFilters.empty() && !filtermatcher.match(compile.filename))
4952-
continue;
4953-
49544947
const std::string &excl = compile.get("ExcludedFromBuild");
49554948
if (!excl.empty() && caseInsensitiveStringCompare(excl, "true") == 0)
49564949
continue;

0 commit comments

Comments
 (0)