Refactoring: Copy FileLister::acceptFile to Path::acceptFile. Use Path::getFilenameExtension and Path::acceptFile in Tokenizer. Use Path::acceptFile in CppCheck::processFile instead of hardcoded handling.

This commit is contained in:
Daniel Marjamäki 2012-01-06 17:31:10 +01:00
parent 9a102702cb
commit de4a64332e
6 changed files with 49 additions and 58 deletions

View File

@ -19,38 +19,10 @@
#include <cstring>
#include <string>
#include <cctype>
#include <algorithm>
#include <sstream>
#include "filelister.h"
#include "path.h"
// This wrapper exists because Sun's CC does not allow a static_cast
// from extern "C" int(*)(int) to int(*)(int).
static int tolowerWrapper(int c)
{
return std::tolower(c);
}
bool FileLister::acceptFile(const std::string &filename)
{
std::string extension = Path::getFilenameExtension(filename);
if (extension == "")
return false;
std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
if (extension == ".cpp" ||
extension == ".cxx" ||
extension == ".cc" ||
extension == ".c" ||
extension == ".c++" ||
extension == ".tpp" ||
extension == ".txx") {
return true;
}
return false;
}
#ifdef _WIN32
@ -207,7 +179,7 @@ void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, std::map
// File
// If recursive is not used, accept all files given by user
if (Path::sameFileName(path,ansiFfd) || FileLister::acceptFile(ansiFfd)) {
if (Path::sameFileName(path,ansiFfd) || Path::acceptFile(ansiFfd)) {
const std::string nativename = Path::fromNativeSeparators(fname.str());
filenames.push_back(nativename);
// Limitation: file sizes are assumed to fit in a 'long'
@ -300,7 +272,7 @@ void FileLister::recursiveAddFiles2(std::vector<std::string> &relative,
if (filename[filename.length()-1] != '/') {
// File
if (Path::sameFileName(path,filename) || FileLister::acceptFile(filename)) {
if (Path::sameFileName(path,filename) || Path::acceptFile(filename)) {
relative.push_back(filename);
seen_paths.insert(absolute_path);

View File

@ -43,14 +43,6 @@ public:
std::map<std::string, long> &filesizes,
const std::string &path);
/**
* @brief Check if the file extension indicates that it's a source file.
* Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
* @param filename filename to check
* @return returns true if the file extension indicates it should be checked
*/
static bool acceptFile(const std::string &filename);
/**
* @brief Is given path a directory?
* @return returns true if the path is a directory

View File

@ -138,24 +138,13 @@ bool CppCheck::findError(std::string code, const char FileName[])
return true;
}
// Disable debug warnings?
static bool no_debug_warnings(const std::string &filename) {
const std::string::size_type pos = filename.rfind(".");
if (pos == std::string::npos)
return false;
const std::string ext = filename.substr(pos);
// Only allow debug warnings if file extension is c or cpp so people
// won't be tempted to fix java / c# problems spotted this way.
return bool(ext != ".c" && ext != ".cpp");
}
unsigned int CppCheck::processFile()
{
exitcode = 0;
// disable debug warnings?
if (no_debug_warnings(_filename))
// only show debug warnings for C/C++ source files (don't fix
// debug warnings for java/c#/etc files)
if (!Path::acceptFile(_filename))
_settings.debugwarnings = false;
// TODO: Should this be moved out to its own function so all the files can be

View File

@ -122,3 +122,33 @@ std::string Path::getFilenameExtension(const std::string &path)
const std::string extension = path.substr(dotLocation);
return extension;
}
// This wrapper exists because Sun's CC does not allow a static_cast
// from extern "C" int(*)(int) to int(*)(int).
static int tolowerWrapper(int c)
{
return std::tolower(c);
}
bool Path::acceptFile(const std::string &filename)
{
std::string extension = Path::getFilenameExtension(filename);
if (extension == "")
return false;
std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
if (extension == ".cpp" ||
extension == ".cxx" ||
extension == ".cc" ||
extension == ".c" ||
extension == ".c++" ||
extension == ".tpp" ||
extension == ".txx") {
return true;
}
return false;
}

View File

@ -76,6 +76,15 @@ public:
* @return Filename extension (containing the dot, e.g. ".h").
*/
static std::string getFilenameExtension(const std::string &path);
/**
* @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
* @return returns true if the file extension indicates it should be checked
*/
static bool acceptFile(const std::string &filename);
};
/// @}

View File

@ -22,6 +22,8 @@
#define tokenizeH
//---------------------------------------------------------------------------
#include "path.h"
#include <string>
#include <map>
#include <list>
@ -51,10 +53,7 @@ public:
std::string fileExtension() const {
if (_files.empty())
return std::string("");
const std::string::size_type pos = _files[0].rfind('.');
if (pos != std::string::npos)
return _files[0].substr(pos);
return std::string("");
return Path::getFilenameExtension(_files[0]);
}
/** Is the code JAVA. Used for bailouts */
@ -74,13 +73,13 @@ public:
/** Is the code C. Used for bailouts */
bool isC() const {
std::string ext = fileExtension();
const std::string ext = fileExtension();
return (ext == ".c" || ext == ".C");
}
/** Is the code CPP. Used for bailouts */
bool isCPP() const {
return !isC() && !isJavaOrCSharp();
return !isC() && (_files.size() && Path::acceptFile(_files[0]));
}
/**