From 371838b9cb582df93e8120cd3404b39919609e5d Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Sat, 6 Aug 2011 12:29:51 +0300 Subject: [PATCH] CLI: Warn if user tries to exclude header files. Filename exclusion (with -i) works only for the source files. Print a warning if user tries to exclude header file. The warning instructs user to use --suppress for ignoring warnings from the header files. --- cli/cppcheckexecutor.cpp | 21 +++++++++++++++++++++ cli/filelister.cpp | 6 ++---- lib/path.cpp | 10 ++++++++++ lib/path.h | 7 +++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 25b486229..e659b7ec6 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -99,6 +99,27 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c if (!filenames.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 warned = false; + std::vector ignored = parser.GetIgnoredPaths(); + std::vector::iterator iterIgnored = ignored.begin(); + for (int i = (int)ignored.size() - 1; i >= 0; i--) + { + const std::string extension = Path::getFilenameExtension(ignored[i]); + if (extension == ".h" || extension == ".hpp") + { + ignored.erase(iterIgnored + i); + if (!warned) + { + 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; + warned = true; // Warn only once + } + } + } + PathMatch matcher(parser.GetIgnoredPaths()); std::vector::iterator iterBegin = filenames.begin(); for (int i = (int)filenames.size() - 1; i >= 0; i--) diff --git a/cli/filelister.cpp b/cli/filelister.cpp index c8e65f1e1..4f71232f9 100644 --- a/cli/filelister.cpp +++ b/cli/filelister.cpp @@ -33,11 +33,9 @@ static int tolowerWrapper(int c) bool FileLister::acceptFile(const std::string &filename) { - std::string::size_type dotLocation = filename.find_last_of('.'); - if (dotLocation == std::string::npos) + std::string extension = Path::getFilenameExtension(filename); + if (extension == "") return false; - - std::string extension = filename.substr(dotLocation); std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper); if (extension == ".cpp" || diff --git a/lib/path.cpp b/lib/path.cpp index 67caa64ae..ffea4e17b 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -127,3 +127,13 @@ std::string Path::removeQuotationMarks(const std::string &path) editPath.erase(std::remove(editPath.begin(), editPath.end(), '\"'), editPath.end()); return editPath; } + +std::string Path::getFilenameExtension(const std::string &path) +{ + const std::string::size_type dotLocation = path.find_last_of('.'); + if (dotLocation == std::string::npos) + return ""; + + const std::string extension = path.substr(dotLocation); + return extension; +} diff --git a/lib/path.h b/lib/path.h index 1c37a142a..1ee7b261c 100644 --- a/lib/path.h +++ b/lib/path.h @@ -70,6 +70,13 @@ public: * @return Cleaned path without quotation marks. */ static std::string removeQuotationMarks(const std::string &path); + + /** + * @brief Get an extension of the filename. + * @param path Path containing filename. + * @return Filename extension (containing the dot, e.g. ".h"). + */ + static std::string getFilenameExtension(const std::string &path); }; /// @}