Refactoring FileLister and Path so they don't depend on Library

This commit is contained in:
Daniel Marjamäki 2013-10-31 19:09:01 +01:00
parent c529de6e07
commit e446a28b3d
10 changed files with 58 additions and 32 deletions

View File

@ -94,7 +94,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
// Execute recursiveAddFiles() to each given file parameter
std::vector<std::string>::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<std::string, std::size_t>::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<std::string, std::size_t>::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)

View File

@ -21,7 +21,6 @@
#include <cstring>
#include <string>
#include <sstream>
#include "library.h"
#ifdef _WIN32
@ -68,7 +67,7 @@ static BOOL MyFileExists(const std::string& path)
return result;
}
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const Library * library)
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra)
{
const std::string cleanedPath = Path::toNativeSeparators(path);
@ -124,7 +123,7 @@ void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &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<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow;
@ -134,7 +133,7 @@ void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &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<std::string> &seen_paths,
std::map<std::string, std::size_t> &files,
const std::string &path,
const Library * library)
const std::set<std::string> &extra)
{
std::ostringstream oss;
oss << path;
@ -215,7 +214,7 @@ void FileLister::recursiveAddFiles2(std::set<std::string> &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<std::string> &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<std::string, std::size_t> &files, const std::string &path, const Library * library)
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra)
{
std::set<std::string> seen_paths;
recursiveAddFiles2(seen_paths, files, path, library);
recursiveAddFiles2(seen_paths, files, path, extra);
}
bool FileLister::isDirectory(const std::string &path)

View File

@ -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<std::string, std::size_t> &files, const std::string &path, const class Library * library);
static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path) {
const std::set<std::string> 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<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra);
/**
* @brief Is given path a directory?
@ -57,7 +71,7 @@ public:
static void recursiveAddFiles2(std::set<std::string> &seen_paths,
std::map<std::string, std::size_t> &files,
const std::string &path,
const class Library * library);
const std::set<std::string> &extra);
#endif
};

View File

@ -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();

View File

@ -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())

View File

@ -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;

View File

@ -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<std::string>::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<std::string> &markupExtensions() const {
return _markupExtensions;
}
bool reportErrors(const std::string &path) const {
@ -340,7 +341,7 @@ private:
std::map<std::string, bool> _noreturn; // is function noreturn?
std::map<std::string, bool> _ignorefunction; // ignore functions/macros from a library (gtk, qt etc)
std::map<std::string, bool> _reporterrors;
std::list<std::string> _fileextensions; // accepted file extensions
std::set<std::string> _markupExtensions; // file extensions of markup files
std::map<std::string, std::list<std::string> > _keywords; // keywords for code in the library
std::map<std::string, CodeBlock> _executableblocks; // keywords for blocks of executable code
std::map<std::string, ExportedFunctions> _exporters; // keywords that export variables/functions to libraries (meta-code/macros)

View File

@ -22,7 +22,6 @@
#include <cstring>
#include <cctype>
#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<std::string> &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)

View File

@ -21,9 +21,10 @@
#define pathH
//---------------------------------------------------------------------------
#include "config.h"
#include <set>
#include <string>
#include <vector>
#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<std::string> 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<std::string> &extra);
/**
* @brief Identify language based on file extension.

View File

@ -76,8 +76,8 @@ private:
void recursiveAddFiles() const {
// Recursively add add files..
std::map<std::string, std::size_t> files;
Settings settings; // TODO(struscott): Pull in settings
FileLister::recursiveAddFiles(files, ".", &settings.library);
std::set<std::string> extra;
FileLister::recursiveAddFiles(files, ".", extra);
// In case there are leading "./"..
for (std::map<std::string, std::size_t>::iterator i = files.begin(); i != files.end();) {