diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index e2ffd3c96..cd9265c00 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -124,54 +124,39 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c } } - const std::vector& pathnames = parser.GetPathNames(); - - if (!pathnames.empty()) { - // Execute recursiveAddFiles() to each given file parameter - std::vector::const_iterator iter; - for (iter = pathnames.begin(); iter != pathnames.end(); ++iter) - FileLister::recursiveAddFiles(_files, Path::toNativeSeparators(*iter), _settings->library.markupExtensions()); + // Output a warning for the user if he tries to exclude headers + bool warn = false; + const std::vector& ignored = parser.GetIgnoredPaths(); + for (std::vector::const_iterator i = ignored.cbegin(); i != ignored.cend(); ++i) { + if (Path::isHeader(*i)) { + warn = true; + break; + } + } + if (warn) { + std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl; + std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl; } - if (!_files.empty()) { - // Remove header files from the list of ignored files. - // Also output a warning for the user. - // TODO: Remove all unknown files? (use FileLister::acceptFile()) - bool warn = false; - std::vector ignored = parser.GetIgnoredPaths(); - for (std::vector::iterator i = ignored.begin(); i != ignored.end();) { - const std::string extension = Path::getFilenameExtension(*i); - if (extension == ".h" || extension == ".hpp") { - i = ignored.erase(i); - warn = true; - } else - ++i; - } - if (warn) { - std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl; - std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl; - } + const std::vector& pathnames = parser.GetPathNames(); #if defined(_WIN32) - // For Windows we want case-insensitive path matching - const bool caseSensitive = false; + // For Windows we want case-insensitive path matching + const bool caseSensitive = false; #else - const bool caseSensitive = true; + const bool caseSensitive = true; #endif - PathMatch matcher(parser.GetIgnoredPaths(), caseSensitive); - for (std::map::iterator i = _files.begin(); i != _files.end();) { - if (matcher.Match(i->first)) - _files.erase(i++); - else - ++i; - } - } else { - std::cout << "cppcheck: error: could not find or open any of the paths given." << std::endl; - return false; + if (!pathnames.empty()) { + // Execute recursiveAddFiles() to each given file parameter + PathMatch matcher(ignored, caseSensitive); + for (std::vector::const_iterator iter = pathnames.begin(); iter != pathnames.end(); ++iter) + FileLister::recursiveAddFiles(_files, Path::toNativeSeparators(*iter), _settings->library.markupExtensions(), matcher); } if (_files.empty()) { - std::cout << "cppcheck: error: no files to check - all paths ignored." << std::endl; + std::cout << "cppcheck: error: could not find or open any of the paths given." << std::endl; + if (!ignored.empty()) + std::cout << "cppcheck: Maybe all paths were ignored?" << std::endl; return false; } return true; diff --git a/cli/filelister.cpp b/cli/filelister.cpp index d76aa7767..43da14936 100644 --- a/cli/filelister.cpp +++ b/cli/filelister.cpp @@ -18,6 +18,7 @@ #include "filelister.h" #include "path.h" +#include "pathmatch.h" #include #include #include @@ -67,7 +68,7 @@ static BOOL MyFileExists(const std::string& path) return result; } -void FileLister::recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra) +void FileLister::recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra, const PathMatch& ignored) { const std::string cleanedPath = Path::toNativeSeparators(path); @@ -123,7 +124,7 @@ void FileLister::recursiveAddFiles(std::map &files, co // File const std::string nativename = Path::fromNativeSeparators(fname); - if (!checkAllFilesInDir || Path::acceptFile(fname, extra)) { + if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.Match(fname)) { // Limitation: file sizes are assumed to fit in a 'size_t' #ifdef _WIN64 files[nativename] = (static_cast(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow; @@ -133,7 +134,8 @@ void FileLister::recursiveAddFiles(std::map &files, co } } else { // Directory - FileLister::recursiveAddFiles(files, fname, extra); + if (!ignored.Match(fname)) + FileLister::recursiveAddFiles(files, fname, extra, ignored); } } while (FindNextFileA(hFind, &ffd) != FALSE); @@ -193,7 +195,8 @@ void FileLister::addFiles2(std::set &seen_paths, std::map &files, const std::string &path, const std::set &extra, - bool recursive + bool recursive, + const PathMatch& ignored ) { std::ostringstream oss; @@ -220,7 +223,7 @@ void FileLister::addFiles2(std::set &seen_paths, if (filename[filename.length()-1] != '/') { // File - if (Path::sameFileName(path,filename) || Path::acceptFile(filename, extra)) { + if ((Path::sameFileName(path,filename) || Path::acceptFile(filename, extra)) && !ignored.Match(filename)) { seen_paths.insert(absolute_path); struct stat sb; @@ -232,25 +235,26 @@ void FileLister::addFiles2(std::set &seen_paths, } } else if (recursive) { // Directory - - seen_paths.insert(absolute_path); - addFiles2(seen_paths, files, filename, extra, recursive); + if (!ignored.Match(filename)) { + seen_paths.insert(absolute_path); + addFiles2(seen_paths, files, filename, extra, recursive, ignored); + } } } globfree(&glob_results); } -void FileLister::recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra) +void FileLister::recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra, const PathMatch& ignored) { std::set seen_paths; - addFiles2(seen_paths, files, path, extra, true); + addFiles2(seen_paths, files, path, extra, true, ignored); } -void FileLister::addFiles(std::map &files, const std::string &path, const std::set &extra, bool recursive) +void FileLister::addFiles(std::map &files, const std::string &path, const std::set &extra, bool recursive, const PathMatch& ignored) { std::set seen_paths; - addFiles2(seen_paths, files, path, extra, recursive); + addFiles2(seen_paths, files, path, extra, recursive, ignored); } bool FileLister::isDirectory(const std::string &path) diff --git a/cli/filelister.h b/cli/filelister.h index 27a477a98..69dd68a1d 100644 --- a/cli/filelister.h +++ b/cli/filelister.h @@ -23,6 +23,8 @@ #include #include +class PathMatch; + /// @addtogroup CLI /// @{ @@ -36,10 +38,11 @@ public: * (*.c;*.cpp;*.cxx;*.c++;*.cc;*.txx) are added. * @param files output map that associates the size of each file with its name * @param path root path + * @param ignored ignored paths */ - static void recursiveAddFiles(std::map &files, const std::string &path) { + static void recursiveAddFiles(std::map &files, const std::string &path, const PathMatch& ignored) { const std::set extra; - recursiveAddFiles(files, path, extra); + recursiveAddFiles(files, path, extra, ignored); } /** @@ -50,8 +53,9 @@ public: * @param files output map that associates the size of each file with its name * @param path root path * @param extra Extra file extensions + * @param ignored ignored paths */ - static void recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra); + static void recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra, const PathMatch& ignored); /** * @brief (Recursively) add source files to a map. @@ -62,8 +66,9 @@ public: * @param path root path * @param extra Extra file extensions * @param recursive Enable recursion + * @param ignored ignored paths */ - static void addFiles(std::map &files, const std::string &path, const std::set &extra, bool recursive); + static void addFiles(std::map &files, const std::string &path, const std::set &extra, bool recursive, const PathMatch& ignored); /** * @brief Is given path a directory? @@ -86,7 +91,8 @@ private: std::map &files, const std::string &path, const std::set &extra, - bool recursive); + bool recursive, + const PathMatch& ignored); #endif }; diff --git a/lib/path.h b/lib/path.h index 4b49ddeae..b5d21d637 100644 --- a/lib/path.h +++ b/lib/path.h @@ -145,7 +145,6 @@ public: */ static bool isCPP(const std::string &extensionInLowerCase); -private: /** * @brief Is filename a header based on file extension * @param path filename to check. path info is optional diff --git a/test/testfilelister.cpp b/test/testfilelister.cpp index 503696560..26712c261 100644 --- a/test/testfilelister.cpp +++ b/test/testfilelister.cpp @@ -19,6 +19,7 @@ #include "testsuite.h" #include "filelister.h" #include "settings.h" +#include "pathmatch.h" #include #ifndef _WIN32 @@ -76,8 +77,9 @@ private: void recursiveAddFiles() const { // Recursively add add files.. std::map files; - std::set extra; - FileLister::recursiveAddFiles(files, ".", extra); + std::vector masks; + PathMatch matcher(masks); + FileLister::recursiveAddFiles(files, ".", matcher); // In case there are leading "./".. for (std::map::iterator i = files.begin(); i != files.end();) { diff --git a/test/testsamples.cpp b/test/testsamples.cpp index 35a2e17ea..b3fca407d 100644 --- a/test/testsamples.cpp +++ b/test/testsamples.cpp @@ -20,6 +20,7 @@ #include "testsuite.h" #include "cppcheckexecutor.h" #include "path.h" +#include "pathmatch.h" #include #include #include @@ -40,10 +41,12 @@ private: REDIRECT; std::map files; + const std::vector masks; + const PathMatch matcher(masks); #ifdef _WIN32 - FileLister::recursiveAddFiles(files, "..\\samples"); + FileLister::recursiveAddFiles(files, "..\\samples", matcher); #else - FileLister::recursiveAddFiles(files, "samples"); + FileLister::recursiveAddFiles(files, "samples", matcher); #endif for (std::map::const_iterator i = files.begin(); i != files.end(); ++i) { CLEAR_REDIRECT_ERROUT;