From e446a28b3df620367945945a496d8e1dd2d61ed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 31 Oct 2013 19:09:01 +0100 Subject: [PATCH] Refactoring FileLister and Path so they don't depend on Library --- cli/cppcheckexecutor.cpp | 8 ++++---- cli/filelister.cpp | 17 ++++++++--------- cli/filelister.h | 18 ++++++++++++++++-- lib/checkunusedfunctions.cpp | 4 ++-- lib/cppcheck.cpp | 2 +- lib/library.cpp | 2 +- lib/library.h | 13 +++++++------ lib/path.cpp | 5 ++--- lib/path.h | 17 +++++++++++++++-- test/testfilelister.cpp | 4 ++-- 10 files changed, 58 insertions(+), 32 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index b24ca64c7..0187d7026 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -94,7 +94,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c // 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); + FileLister::recursiveAddFiles(_files, Path::toNativeSeparators(*iter), _settings->library.markupExtensions()); } if (!_files.empty()) { @@ -176,7 +176,7 @@ int CppCheckExecutor::check(int argc, const char* const argv[]) std::size_t processedsize = 0; unsigned int c = 0; for (std::map::const_iterator i = _files.begin(); i != _files.end(); ++i) { - if (!_settings->library.acceptFile(i->first)) { + if (!_settings->library.markupFile(i->first)) { returnValue += cppCheck.check(i->first); processedsize += i->second; if (!settings._errorsOnly) @@ -185,10 +185,10 @@ int CppCheckExecutor::check(int argc, const char* const argv[]) } } - // second loop to catch all library files which may not work until all + // second loop to parse all markup files which may not work until all // c/cpp files have been parsed and checked for (std::map::const_iterator i = _files.begin(); i != _files.end(); ++i) { - if (_settings->library.acceptFile(i->first)) { + if (_settings->library.markupFile(i->first)) { returnValue += cppCheck.check(i->first); processedsize += i->second; if (!settings._errorsOnly) diff --git a/cli/filelister.cpp b/cli/filelister.cpp index 495cc8c68..ffb9793b6 100644 --- a/cli/filelister.cpp +++ b/cli/filelister.cpp @@ -21,7 +21,6 @@ #include #include #include -#include "library.h" #ifdef _WIN32 @@ -68,7 +67,7 @@ static BOOL MyFileExists(const std::string& path) return result; } -void FileLister::recursiveAddFiles(std::map &files, const std::string &path, const Library * library) +void FileLister::recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra) { const std::string cleanedPath = Path::toNativeSeparators(path); @@ -124,7 +123,7 @@ void FileLister::recursiveAddFiles(std::map &files, co // File const std::string nativename = Path::fromNativeSeparators(fname); - if (!checkAllFilesInDir || Path::acceptFile(fname, library)) { + if (!checkAllFilesInDir || Path::acceptFile(fname, extra)) { // Limitation: file sizes are assumed to fit in a 'size_t' #ifdef _WIN64 files[nativename] = (static_cast(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow; @@ -134,7 +133,7 @@ void FileLister::recursiveAddFiles(std::map &files, co } } else { // Directory - FileLister::recursiveAddFiles(files, fname, library); + FileLister::recursiveAddFiles(files, fname, extra); } } while (FindNextFileA(hFind, &ffd) != FALSE); @@ -189,7 +188,7 @@ std::string FileLister::getAbsolutePath(const std::string& path) void FileLister::recursiveAddFiles2(std::set &seen_paths, std::map &files, const std::string &path, - const Library * library) + const std::set &extra) { std::ostringstream oss; oss << path; @@ -215,7 +214,7 @@ void FileLister::recursiveAddFiles2(std::set &seen_paths, if (filename[filename.length()-1] != '/') { // File - if (Path::sameFileName(path,filename) || Path::acceptFile(filename, library)) { + if (Path::sameFileName(path,filename) || Path::acceptFile(filename, extra)) { seen_paths.insert(absolute_path); struct stat sb; @@ -229,17 +228,17 @@ void FileLister::recursiveAddFiles2(std::set &seen_paths, // Directory seen_paths.insert(absolute_path); - recursiveAddFiles2(seen_paths, files, filename, library); + recursiveAddFiles2(seen_paths, files, filename, extra); } } globfree(&glob_results); } -void FileLister::recursiveAddFiles(std::map &files, const std::string &path, const Library * library) +void FileLister::recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra) { std::set seen_paths; - recursiveAddFiles2(seen_paths, files, path, library); + recursiveAddFiles2(seen_paths, files, path, extra); } bool FileLister::isDirectory(const std::string &path) diff --git a/cli/filelister.h b/cli/filelister.h index 1e3847930..1fd953b5e 100644 --- a/cli/filelister.h +++ b/cli/filelister.h @@ -37,7 +37,21 @@ public: * @param files output map that associates the size of each file with its name * @param path root path */ - static void recursiveAddFiles(std::map &files, const std::string &path, const class Library * library); + static void recursiveAddFiles(std::map &files, const std::string &path) { + const std::set extra; + recursiveAddFiles(files, path, extra); + } + + /** + * @brief Recursively add source files to a map. + * Add source files from given directory and all subdirectries to the + * given map. Only files with accepted extensions + * (*.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 extra Extra file extensions + */ + static void recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra); /** * @brief Is given path a directory? @@ -57,7 +71,7 @@ public: static void recursiveAddFiles2(std::set &seen_paths, std::map &files, const std::string &path, - const class Library * library); + const std::set &extra); #endif }; diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 9c93a6ea1..9c1102c74 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -116,7 +116,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi } } - if (!settings->library.acceptFile(FileName) // only check c/c++ + if (!settings->library.markupFile(FileName) // only check source files && settings->library.isexporter(tok->str()) && tok->next() != 0) { const Token * qPropToken = tok; qPropToken = qPropToken->next(); @@ -139,7 +139,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi } } - if (settings->library.acceptFile(FileName) + if (settings->library.markupFile(FileName) && settings->library.isimporter(FileName, tok->str()) && tok->next()) { const Token * qPropToken = tok; qPropToken = qPropToken->next(); diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index b3abcc077..d0d46ad3f 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -131,7 +131,7 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin exitcode = 0; // only show debug warnings for accepted C/C++ source files - if (!Path::acceptFile(filename, &_settings.library)) + if (!Path::acceptFile(filename)) _settings.debugwarnings = false; if (_settings.terminated()) diff --git a/lib/library.cpp b/lib/library.cpp index c20ddfffb..8e9468101 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -131,7 +131,7 @@ bool Library::load(const tinyxml2::XMLDocument &doc) else if (strcmp(node->Name(),"files")==0) { for (const tinyxml2::XMLElement *functionnode = node->FirstChildElement(); functionnode; functionnode = functionnode->NextSiblingElement()) { if (strcmp(functionnode->Name(), "file") == 0) { - _fileextensions.push_back(functionnode->Attribute("ext")); + _markupExtensions.insert(functionnode->Attribute("ext")); const char * report = functionnode->Attribute("reporterrors"); if (report) _reporterrors[functionnode->Attribute("ext")] = strcmp(report, "true")==0; diff --git a/lib/library.h b/lib/library.h index 94a860d44..d9c7c1fd5 100644 --- a/lib/library.h +++ b/lib/library.h @@ -125,11 +125,12 @@ public: return arg && arg->strz; } - bool acceptFile(const std::string &path) const { - const std::string extension = Path::getFilenameExtensionInLowerCase(path); - const std::list::const_iterator it = - std::find(_fileextensions.begin(), _fileextensions.end(), extension); - return it != _fileextensions.end(); + bool markupFile(const std::string &path) const { + return _markupExtensions.find(Path::Path::getFilenameExtensionInLowerCase(path)) != _markupExtensions.end(); + } + + const std::set &markupExtensions() const { + return _markupExtensions; } bool reportErrors(const std::string &path) const { @@ -340,7 +341,7 @@ private: std::map _noreturn; // is function noreturn? std::map _ignorefunction; // ignore functions/macros from a library (gtk, qt etc) std::map _reporterrors; - std::list _fileextensions; // accepted file extensions + std::set _markupExtensions; // file extensions of markup files std::map > _keywords; // keywords for code in the library std::map _executableblocks; // keywords for blocks of executable code std::map _exporters; // keywords that export variables/functions to libraries (meta-code/macros) diff --git a/lib/path.cpp b/lib/path.cpp index c8a3decff..4c281f8be 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -22,7 +22,6 @@ #include #include #include "path.h" -#include "library.h" /** Is the filesystem case insensitive? */ static bool caseInsensitiveFilesystem() @@ -207,9 +206,9 @@ bool Path::isCPP(const std::string &path) return (getFilenameExtension(path) == ".C"); } -bool Path::acceptFile(const std::string &path, const class Library *library) +bool Path::acceptFile(const std::string &path, const std::set &extra) { - return !Path::isHeader(path) && (Path::isCPP(path) || Path::isC(path) || (library ? library->acceptFile(path) : false)); + return !Path::isHeader(path) && (Path::isCPP(path) || Path::isC(path) || extra.find(getFilenameExtension(path)) != extra.end()); } bool Path::isHeader(const std::string &path) diff --git a/lib/path.h b/lib/path.h index e35127fda..87b0c7ae8 100644 --- a/lib/path.h +++ b/lib/path.h @@ -21,9 +21,10 @@ #define pathH //--------------------------------------------------------------------------- +#include "config.h" +#include #include #include -#include "config.h" /// @addtogroup Core /// @{ @@ -109,7 +110,19 @@ public: * @param filename filename to check. path info is optional * @return returns true if the file extension indicates it should be checked */ - static bool acceptFile(const std::string &filename, const class Library *library = 0); + static bool acceptFile(const std::string &filename) { + const std::set extra; + return acceptFile(filename, extra); + } + + /** + * @brief Check if the file extension indicates that it's a C/C++ source file. + * Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx + * @param filename filename to check. path info is optional + * @param extra extra file extensions + * @return returns true if the file extension indicates it should be checked + */ + static bool acceptFile(const std::string &filename, const std::set &extra); /** * @brief Identify language based on file extension. diff --git a/test/testfilelister.cpp b/test/testfilelister.cpp index 9b1665433..d91da3b29 100644 --- a/test/testfilelister.cpp +++ b/test/testfilelister.cpp @@ -76,8 +76,8 @@ private: void recursiveAddFiles() const { // Recursively add add files.. std::map files; - Settings settings; // TODO(struscott): Pull in settings - FileLister::recursiveAddFiles(files, ".", &settings.library); + std::set extra; + FileLister::recursiveAddFiles(files, ".", extra); // In case there are leading "./".. for (std::map::iterator i = files.begin(); i != files.end();) {