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
14 changes: 12 additions & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,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 +264,12 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
return false;
}

const auto& unmatched = matcher.unmatched();
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.
return false;
}

std::list<FileWithDetails> files;
if (!mSettings.fileFilters.empty()) {
files = filterFiles(mSettings.fileFilters, filesResolved);
Expand Down Expand Up @@ -1723,12 +1729,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 @@ -201,7 +201,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 @@ -280,7 +280,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 All @@ -304,7 +304,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.
};

/// @}
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
17 changes: 15 additions & 2 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,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)
void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLib, const bool checkConfig, const bool checkUnusedExcludes)
{
if (files.isEmpty())
return;
Expand Down Expand Up @@ -683,6 +683,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 @@ -1981,7 +1992,9 @@ void MainWindow::analyzeProject(const ProjectFile *projectFile, const QStringLis
if (paths.isEmpty()) {
paths << mCurrentDirectory;
}
doAnalyzeFiles(paths, checkLib, checkConfig);
// the exclude paths can only be validated when the whole project is analyzed
const bool checkUnusedExcludes = recheckFiles.isEmpty();
doAnalyzeFiles(paths, checkLib, checkConfig, checkUnusedExcludes);
}

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 @@ -317,8 +317,9 @@ 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.
*/
void doAnalyzeFiles(const QStringList &files, bool checkLib = false, bool checkConfig = false);
void doAnalyzeFiles(const QStringList &files, bool checkLib = false, bool checkConfig = false, bool checkUnusedExcludes = false);

/**
* @brief Get our default cppcheck settings and read project file.
Expand Down
37 changes: 37 additions & 0 deletions gui/test/filelist/testfilelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,41 @@ void TestFileList::filterFiles5() const
QVERIFY(!files.contains(base + "/dir1/dir11/foo11.cpp"));
}

void TestFileList::unmatchedExcludes1() const
{
FileList list;
QStringList filters;
filters << "foo1.cpp" << "foo3.cc";
list.addExcludeList(filters);
list.addDirectory(QString(SRCDIR) + "/../data/files");
QVERIFY(!list.getFileList().isEmpty());
QVERIFY(list.getUnmatchedExcludes().isEmpty());
}

void TestFileList::unmatchedExcludes2() const
{
FileList list;
QStringList filters;
filters << "foo1.cpp" << "bar.cpp" << "dir3/";
list.addExcludeList(filters);
list.addDirectory(QString(SRCDIR) + "/../data/files", true);
// unmatched excludes does not affect the file list
QCOMPARE(list.getFileList().size(), 9);
const QStringList unmatched = list.getUnmatchedExcludes();
QCOMPARE(unmatched.size(), 2);
QCOMPARE(unmatched[0], QString("bar.cpp"));
QCOMPARE(unmatched[1], QString("dir3/"));
}

void TestFileList::unmatchedExcludes3() const
{
FileList list;
QStringList filters;
filters << "dir1/";
list.addExcludeList(filters);
list.addDirectory(QString(SRCDIR) + "/../data/files", true);
QVERIFY(!list.getFileList().isEmpty());
QVERIFY(list.getUnmatchedExcludes().isEmpty());
}

QTEST_MAIN(TestFileList)
3 changes: 3 additions & 0 deletions gui/test/filelist/testfilelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ private slots:
void filterFiles3() const;
void filterFiles4() const;
void filterFiles5() const;
void unmatchedExcludes1() const;
void unmatchedExcludes2() const;
void unmatchedExcludes3() const;
};
Loading
Loading