Skip to content
Open
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
31 changes: 29 additions & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ CmdLineParser::CmdLineParser(CmdLineLogger &logger, Settings &settings, Suppress
, mSuppressions(suppressions)
{}

// The file lister does not walk into an excluded directory, so a pattern that only matches paths in
// there looks unused. Walk the paths again without excluding anything to find out which of the
// remaining patterns match something. This is only done when some patterns look unused.
static std::vector<std::string> unmatchedIgnoredPaths(const PathMatch &matcher, const std::vector<std::string> &pathnames, const std::set<std::string> &extra)
{
std::vector<std::string> unmatched = matcher.unmatched();
if (unmatched.empty())
return unmatched;

PathMatch recorder(std::move(unmatched), Path::getCurrentPath());
recorder.setRecordOnly(true);
std::list<FileWithDetails> files;
for (const std::string &pathname : pathnames)
(void)FileLister::recursiveAddFiles(files, Path::toNativeSeparators(pathname), extra, recorder);
return recorder.unmatched();
}

bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
{
const Result result = parseFromArgs(argc, argv);
Expand Down Expand Up @@ -247,7 +264,7 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
std::list<FileWithDetails> filesResolved;
// Execute recursiveAddFiles() to each given file parameter
// TODO: verbose log which files were ignored?
const PathMatch matcher(ignored, Path::getCurrentPath());
PathMatch matcher(ignored, Path::getCurrentPath());
for (const std::string &pathname : pathnamesRef) {
const std::string err = FileLister::recursiveAddFiles(filesResolved, Path::toNativeSeparators(pathname), mSettings.library.markupExtensions(), matcher, mSettings.debugignore);
if (!err.empty()) {
Expand All @@ -264,6 +281,12 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
return false;
}

const std::vector<std::string> unmatched = unmatchedIgnoredPaths(matcher, pathnamesRef, mSettings.library.markupExtensions());
if (!unmatched.empty()) {
mLogger.printError("unused ignore/exclude path '" + unmatched.front() + "'. To hide warnings in certain files use suppressions instead.");
Comment thread
danmar marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of points about making this a hard error:

  • Only unmatched.front() is reported. If a user has several stale -i paths, they have to fix one and re-run for each. Listing all of them would be easier to use (same for the project branch below).
  • -ifoo.h now always fails, because headers are never passed to match(). The user first sees the existing "filename exclusion does not apply to header files" info message and then this error. It may be worth making the two messages consistent.
  • Builds that share one ignore list across different path arguments will now fail. So will builds that ignore generated directories that don't exist yet, e.g. -ibuild/ on a fresh checkout. Is a hard error (rather than a warning) intended here? If so, the release note should call it out as a breaking change.

return false;
}

std::list<FileWithDetails> files;
if (!mSettings.fileFilters.empty()) {
files = filterFiles(mSettings.fileFilters, filesResolved);
Expand Down Expand Up @@ -1723,12 +1746,16 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
mPathNames = project.guiProject.pathNames;

if (!project.fileSettings.empty()) {
project.ignorePaths(mIgnoredPaths, mSettings.debugignore);
const auto& unmatched = project.ignorePaths(mIgnoredPaths, mSettings.debugignore);
if (project.fileSettings.empty()) {
mLogger.printError("no C or C++ source files found.");
mLogger.printMessage("all paths were ignored"); // TODO: log this differently?
return Result::Fail;
}
if (!unmatched.empty()) {
mLogger.printError("unused ignore/exclude path '" + unmatched.front() + "'. To hide warnings in certain files use suppressions instead.");
return Result::Fail;
}
mFileSettings = project.fileSettings;
}

Expand Down
10 changes: 5 additions & 5 deletions cli/filelister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
// When compiling Unicode targets WinAPI automatically uses *W Unicode versions
// of called functions. Thus, we explicitly call *A versions of the functions.

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)
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)
{
const std::string cleanedPath = Path::toNativeSeparators(path);

Expand Down Expand Up @@ -163,7 +163,7 @@ static std::string addFiles2(std::list<FileWithDetails>&files, const std::string
return "";
}

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)
std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, PathMatch& ignored, bool debug)
{
if (path.empty())
return "no path specified";
Expand Down Expand Up @@ -205,7 +205,7 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
const std::string &path,
const std::set<std::string> &extra,
bool recursive,
const PathMatch& ignored,
PathMatch& ignored,
bool debug)
{
if (ignored.match(path))
Expand Down Expand Up @@ -284,7 +284,7 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
return "";
}

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)
std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, PathMatch& ignored, bool debug)
{
if (path.empty())
return "no path specified";
Expand Down Expand Up @@ -312,7 +312,7 @@ std::string FileLister::addFiles(std::list<FileWithDetails> &files, const std::s

#endif

std::string FileLister::recursiveAddFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored, bool debug)
std::string FileLister::recursiveAddFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, PathMatch& ignored, bool debug)
{
return addFiles(files, path, extra, true, ignored, debug);
}
4 changes: 2 additions & 2 deletions cli/filelister.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class FileLister {
* @param debug log if path was ignored
* @return On success, an empty string is returned. On error, a error message is returned.
*/
static std::string recursiveAddFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored, bool debug = false);
static std::string recursiveAddFiles(std::list<FileWithDetails> &files, const std::string &path, const std::set<std::string> &extra, PathMatch& ignored, bool debug = false);

/**
* @brief (Recursively) add source files to a map.
Expand All @@ -59,7 +59,7 @@ class FileLister {
* @param debug log when a path was ignored
* @return On success, an empty string is returned. On error, a error message is returned.
*/
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);
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);
Comment thread
danmar marked this conversation as resolved.
Resolved
};

/// @}
Expand Down
17 changes: 14 additions & 3 deletions gui/filelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ void FileList::addPathList(const QStringList &paths)
}
}

QStringList FileList::getFileList() const
QStringList FileList::getFileList()
{
mUnmatchedExcludes.clear();
if (mExcludedPaths.empty()) {
QStringList names;
for (const QFileInfo& item : mFileList) {
Expand All @@ -103,6 +104,11 @@ QStringList FileList::getFileList() const
return applyExcludeList();
}

const QStringList& FileList::getUnmatchedExcludes() &
{
return mUnmatchedExcludes;
}

void FileList::addExcludeList(const QStringList &paths)
{
mExcludedPaths = paths;
Expand All @@ -117,9 +123,9 @@ static std::vector<std::string> toStdStringList(const QStringList &stringList)
return ret;
}

QStringList FileList::applyExcludeList() const
QStringList FileList::applyExcludeList()
{
const PathMatch pathMatch(toStdStringList(mExcludedPaths), QDir::currentPath().toStdString());
PathMatch pathMatch(toStdStringList(mExcludedPaths), QDir::currentPath().toStdString());

QStringList paths;
for (const QFileInfo& item : mFileList) {
Expand All @@ -129,5 +135,10 @@ QStringList FileList::applyExcludeList() const
if (!pathMatch.match(canonical.toStdString()))
paths << canonical;
}

for (const std::string& excludePath: pathMatch.unmatched()) {
mUnmatchedExcludes << QString::fromStdString(excludePath);
}

return paths;
}
14 changes: 11 additions & 3 deletions gui/filelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ class FileList {
* @brief Return list of filenames (to check).
* @return list of filenames to check.
*/
QStringList getFileList() const;
QStringList getFileList();

/**
* @brief Return list of exclude paths that did not match any file in the last getFileList() call
* @return list of unmatched excludes
*/
const QStringList& getUnmatchedExcludes() &;

/**
* @brief Add list of paths to exclusion list.
Expand All @@ -88,14 +94,16 @@ class FileList {
* @brief Get filtered list of paths.
* This method takes the list of paths and applies the exclude lists to
* it. And then returns the list of paths that did not match the
* exclude filters.
* exclude filters. The exclude paths that did not match any file
* are stored in mUnmatchedExcludes.
* @return Filtered list of paths.
*/
QStringList applyExcludeList() const;
QStringList applyExcludeList();

private:
QFileInfoList mFileList;
QStringList mExcludedPaths;
QStringList mUnmatchedExcludes;
};

#endif // FILELIST_H
18 changes: 16 additions & 2 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLib, const bo
mUI->mResults->setCheckSettings(checkSettings);
}

void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLib, const bool checkConfig, const QStringList& selectedFiles, const Recheck recheck)
void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLib, const bool checkConfig, const bool checkUnusedExcludes, const QStringList& selectedFiles, const Recheck recheck)
{
if (files.isEmpty())
return;
Expand Down Expand Up @@ -706,6 +706,17 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLib, c
return;
}

const QStringList unmatchedExcludePaths = checkUnusedExcludes ? pathList.getUnmatchedExcludes() : QStringList();
if (!unmatchedExcludePaths.isEmpty()) {
QMessageBox msg(QMessageBox::Warning,
"Cppcheck",
tr("Unused exclude paths:\n%1\nto hide warnings in certain files use suppressions instead").arg(unmatchedExcludePaths.join("\n")),
QMessageBox::Ok,
this);
msg.exec();
return;
}

std::list<FileWithDetails> fdetails = enrichFilesForAnalysis(fileNames, checkSettings);

// TODO: lock UI here?
Expand Down Expand Up @@ -2039,7 +2050,10 @@ void MainWindow::analyzeProject(const ProjectFile *projectFile, const QStringLis
if (paths.isEmpty()) {
paths << mCurrentDirectory;
}
doAnalyzeFiles(paths, checkLib, checkConfig, selectedFiles, recheck);

// the exclude paths can only be validated when the whole project is analyzed
const bool checkUnusedExcludes = (recheck == Recheck::None);
doAnalyzeFiles(paths, checkLib, checkConfig, checkUnusedExcludes, selectedFiles, recheck);
}

void MainWindow::newProjectFile()
Expand Down
3 changes: 2 additions & 1 deletion gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,11 @@ private slots:
* @param files List of files and/or directories to analyze
* @param checkLib Flag to indicate if library should be checked
* @param checkConfig Flag to indicate if the configuration should be checked.
* @param checkUnusedExcludes Flag to indicate if unused exclude paths should be reported.
* @param selectedFiles Files selected for a recheck
* @param recheck How the selected files are rechecked
*/
void doAnalyzeFiles(const QStringList &files, bool checkLib = false, bool checkConfig = false, const QStringList& selectedFiles = QStringList(), Recheck recheck = Recheck::None);
void doAnalyzeFiles(const QStringList &files, bool checkLib = false, bool checkConfig = false, bool checkUnusedExcludes = false, const QStringList& selectedFiles = QStringList(), Recheck recheck = Recheck::None);

/**
* @brief Clear the results that a new analysis replaces
Expand Down
8 changes: 8 additions & 0 deletions gui/manualtest/tests/analysis.feature
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ Feature: Analysis and recheck
| latin1 |
| long_line |
| unclosed_nesting |

@id:t1508ae01 @ticket:15081 @project:unused-exclude
Scenario: Unused exclude paths are reported
When I open the project file {project:unused-exclude}/p.cppcheck
Then a warning message box says that there are unused exclude paths
And the message lists src/missing/
And the message does not list src/skip/, that exclude path is used
And the message says that suppressions should be used to hide warnings in certain files
73 changes: 73 additions & 0 deletions gui/manualtest/tests/analysis.scripts.json
Original file line number Diff line number Diff line change
Expand Up @@ -395,5 +395,78 @@
}
],
"script_for": "3090dfe1cf47"
},
"t1508ae01": {
"script": [
{
"do": "click",
"name": "File",
"role": "menu item",
"label": "Open the File menu to open the project file"
},
{
"do": "click",
"name": "Open Project File...",
"role": "menu item",
"label": "Choose Open Project File"
},
{
"do": "wait",
"name": "Select Project File",
"role": "dialog",
"label": "Wait for the project file dialog"
},
{
"do": "settext",
"name": "File name:",
"role": "text",
"value": "{project:unused-exclude}/p.cppcheck",
"label": "Enter the project file {project:unused-exclude}/p.cppcheck"
},
{
"do": "key",
"keys": [
"Return"
],
"label": "Open the project file"
},
{
"expect": "visible",
"name": "Project: p.cppcheck",
"role": "frame",
"timeout": 10,
"label": "The project p.cppcheck is loaded"
},
{
"expect": "visible",
"name": "Cppcheck",
"role": "alert",
"timeout": 10,
"label": "A warning message box is shown"
},
{
"expect": "visible",
"name": "Unused exclude paths",
"role": "label",
"label": "The message says that there are unused exclude paths"
},
{
"expect": "text",
"contains": "src/missing/",
"label": "The message lists src/missing/"
},
{
"expect": "text",
"not_contains": "src/skip/",
"label": "The message does not list src/skip/, that exclude path is used"
},
{
"expect": "visible",
"name": "to hide warnings in certain files use suppressions instead",
"role": "label",
"label": "The message says that suppressions should be used to hide warnings in certain files"
}
],
"script_for": "8e3fa82eb6c0"
}
}
Loading
Loading