Skip to content

Commit

Permalink
Refactoring: Copy FileLister::acceptFile to Path::acceptFile. Use Pat…
Browse files Browse the repository at this point in the history
…h::getFilenameExtension and Path::acceptFile in Tokenizer. Use Path::acceptFile in CppCheck::processFile instead of hardcoded handling.
  • Loading branch information
danmar committed Jan 6, 2012
1 parent 9a10270 commit de4a643
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 58 deletions.
32 changes: 2 additions & 30 deletions cli/filelister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 0 additions & 8 deletions cli/filelister.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ class FileLister {
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
Expand Down
17 changes: 3 additions & 14 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions lib/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

9 changes: 9 additions & 0 deletions lib/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ class Path {
* @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);

};

/// @}
Expand Down
11 changes: 5 additions & 6 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#define tokenizeH
//---------------------------------------------------------------------------

#include "path.h"

#include <string>
#include <map>
#include <list>
Expand Down Expand Up @@ -51,10 +53,7 @@ class Tokenizer {
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 */
Expand All @@ -74,13 +73,13 @@ class Tokenizer {

/** 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]));
}

/**
Expand Down

0 comments on commit de4a643

Please sign in to comment.