Refactorized file listing code (CLI):

- Apply PathMatch in FileLister::recursiveAddFiles() already to avoid touching directories that are ignored (#5775)
- Simplified code to warn about header exclusion; use Path::isHeader() instead of custom header filename detection
This commit is contained in:
PKEuS 2015-07-23 14:01:33 +02:00
parent 28fd6ce2f0
commit ce9272a4ed
6 changed files with 60 additions and 61 deletions

View File

@ -124,54 +124,39 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
} }
} }
const std::vector<std::string>& pathnames = parser.GetPathNames(); // Output a warning for the user if he tries to exclude headers
bool warn = false;
if (!pathnames.empty()) { const std::vector<std::string>& ignored = parser.GetIgnoredPaths();
// Execute recursiveAddFiles() to each given file parameter for (std::vector<std::string>::const_iterator i = ignored.cbegin(); i != ignored.cend(); ++i) {
std::vector<std::string>::const_iterator iter; if (Path::isHeader(*i)) {
for (iter = pathnames.begin(); iter != pathnames.end(); ++iter) warn = true;
FileLister::recursiveAddFiles(_files, Path::toNativeSeparators(*iter), _settings->library.markupExtensions()); break;
}
}
if (warn) {
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;
} }
if (!_files.empty()) { const std::vector<std::string>& pathnames = parser.GetPathNames();
// 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 warn = false;
std::vector<std::string> ignored = parser.GetIgnoredPaths();
for (std::vector<std::string>::iterator i = ignored.begin(); i != ignored.end();) {
const std::string extension = Path::getFilenameExtension(*i);
if (extension == ".h" || extension == ".hpp") {
i = ignored.erase(i);
warn = true;
} else
++i;
}
if (warn) {
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;
}
#if defined(_WIN32) #if defined(_WIN32)
// For Windows we want case-insensitive path matching // For Windows we want case-insensitive path matching
const bool caseSensitive = false; const bool caseSensitive = false;
#else #else
const bool caseSensitive = true; const bool caseSensitive = true;
#endif #endif
PathMatch matcher(parser.GetIgnoredPaths(), caseSensitive); if (!pathnames.empty()) {
for (std::map<std::string, std::size_t>::iterator i = _files.begin(); i != _files.end();) { // Execute recursiveAddFiles() to each given file parameter
if (matcher.Match(i->first)) PathMatch matcher(ignored, caseSensitive);
_files.erase(i++); for (std::vector<std::string>::const_iterator iter = pathnames.begin(); iter != pathnames.end(); ++iter)
else FileLister::recursiveAddFiles(_files, Path::toNativeSeparators(*iter), _settings->library.markupExtensions(), matcher);
++i;
}
} else {
std::cout << "cppcheck: error: could not find or open any of the paths given." << std::endl;
return false;
} }
if (_files.empty()) { if (_files.empty()) {
std::cout << "cppcheck: error: no files to check - all paths ignored." << std::endl; std::cout << "cppcheck: error: could not find or open any of the paths given." << std::endl;
if (!ignored.empty())
std::cout << "cppcheck: Maybe all paths were ignored?" << std::endl;
return false; return false;
} }
return true; return true;

View File

@ -18,6 +18,7 @@
#include "filelister.h" #include "filelister.h"
#include "path.h" #include "path.h"
#include "pathmatch.h"
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <sstream> #include <sstream>
@ -67,7 +68,7 @@ static BOOL MyFileExists(const std::string& path)
return result; return result;
} }
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra) void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
{ {
const std::string cleanedPath = Path::toNativeSeparators(path); const std::string cleanedPath = Path::toNativeSeparators(path);
@ -123,7 +124,7 @@ void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, co
// File // File
const std::string nativename = Path::fromNativeSeparators(fname); const std::string nativename = Path::fromNativeSeparators(fname);
if (!checkAllFilesInDir || Path::acceptFile(fname, extra)) { if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.Match(fname)) {
// Limitation: file sizes are assumed to fit in a 'size_t' // Limitation: file sizes are assumed to fit in a 'size_t'
#ifdef _WIN64 #ifdef _WIN64
files[nativename] = (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow; files[nativename] = (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow;
@ -133,7 +134,8 @@ void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, co
} }
} else { } else {
// Directory // Directory
FileLister::recursiveAddFiles(files, fname, extra); if (!ignored.Match(fname))
FileLister::recursiveAddFiles(files, fname, extra, ignored);
} }
} while (FindNextFileA(hFind, &ffd) != FALSE); } while (FindNextFileA(hFind, &ffd) != FALSE);
@ -193,7 +195,8 @@ void FileLister::addFiles2(std::set<std::string> &seen_paths,
std::map<std::string, std::size_t> &files, std::map<std::string, std::size_t> &files,
const std::string &path, const std::string &path,
const std::set<std::string> &extra, const std::set<std::string> &extra,
bool recursive bool recursive,
const PathMatch& ignored
) )
{ {
std::ostringstream oss; std::ostringstream oss;
@ -220,7 +223,7 @@ void FileLister::addFiles2(std::set<std::string> &seen_paths,
if (filename[filename.length()-1] != '/') { if (filename[filename.length()-1] != '/') {
// File // File
if (Path::sameFileName(path,filename) || Path::acceptFile(filename, extra)) { if ((Path::sameFileName(path,filename) || Path::acceptFile(filename, extra)) && !ignored.Match(filename)) {
seen_paths.insert(absolute_path); seen_paths.insert(absolute_path);
struct stat sb; struct stat sb;
@ -232,25 +235,26 @@ void FileLister::addFiles2(std::set<std::string> &seen_paths,
} }
} else if (recursive) { } else if (recursive) {
// Directory // Directory
if (!ignored.Match(filename)) {
seen_paths.insert(absolute_path); seen_paths.insert(absolute_path);
addFiles2(seen_paths, files, filename, extra, recursive); addFiles2(seen_paths, files, filename, extra, recursive, ignored);
}
} }
} }
globfree(&glob_results); globfree(&glob_results);
} }
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra) void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
{ {
std::set<std::string> seen_paths; std::set<std::string> seen_paths;
addFiles2(seen_paths, files, path, extra, true); addFiles2(seen_paths, files, path, extra, true, ignored);
} }
void FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive) void FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
{ {
std::set<std::string> seen_paths; std::set<std::string> seen_paths;
addFiles2(seen_paths, files, path, extra, recursive); addFiles2(seen_paths, files, path, extra, recursive, ignored);
} }
bool FileLister::isDirectory(const std::string &path) bool FileLister::isDirectory(const std::string &path)

View File

@ -23,6 +23,8 @@
#include <set> #include <set>
#include <map> #include <map>
class PathMatch;
/// @addtogroup CLI /// @addtogroup CLI
/// @{ /// @{
@ -36,10 +38,11 @@ public:
* (*.c;*.cpp;*.cxx;*.c++;*.cc;*.txx) are added. * (*.c;*.cpp;*.cxx;*.c++;*.cc;*.txx) are added.
* @param files output map that associates the size of each file with its name * @param files output map that associates the size of each file with its name
* @param path root path * @param path root path
* @param ignored ignored paths
*/ */
static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path) { static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const PathMatch& ignored) {
const std::set<std::string> extra; const std::set<std::string> extra;
recursiveAddFiles(files, path, extra); recursiveAddFiles(files, path, extra, ignored);
} }
/** /**
@ -50,8 +53,9 @@ public:
* @param files output map that associates the size of each file with its name * @param files output map that associates the size of each file with its name
* @param path root path * @param path root path
* @param extra Extra file extensions * @param extra Extra file extensions
* @param ignored ignored paths
*/ */
static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra); static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored);
/** /**
* @brief (Recursively) add source files to a map. * @brief (Recursively) add source files to a map.
@ -62,8 +66,9 @@ public:
* @param path root path * @param path root path
* @param extra Extra file extensions * @param extra Extra file extensions
* @param recursive Enable recursion * @param recursive Enable recursion
* @param ignored ignored paths
*/ */
static void addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive); static void addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored);
/** /**
* @brief Is given path a directory? * @brief Is given path a directory?
@ -86,7 +91,8 @@ private:
std::map<std::string, std::size_t> &files, std::map<std::string, std::size_t> &files,
const std::string &path, const std::string &path,
const std::set<std::string> &extra, const std::set<std::string> &extra,
bool recursive); bool recursive,
const PathMatch& ignored);
#endif #endif
}; };

View File

@ -145,7 +145,6 @@ public:
*/ */
static bool isCPP(const std::string &extensionInLowerCase); static bool isCPP(const std::string &extensionInLowerCase);
private:
/** /**
* @brief Is filename a header based on file extension * @brief Is filename a header based on file extension
* @param path filename to check. path info is optional * @param path filename to check. path info is optional

View File

@ -19,6 +19,7 @@
#include "testsuite.h" #include "testsuite.h"
#include "filelister.h" #include "filelister.h"
#include "settings.h" #include "settings.h"
#include "pathmatch.h"
#include <fstream> #include <fstream>
#ifndef _WIN32 #ifndef _WIN32
@ -76,8 +77,9 @@ private:
void recursiveAddFiles() const { void recursiveAddFiles() const {
// Recursively add add files.. // Recursively add add files..
std::map<std::string, std::size_t> files; std::map<std::string, std::size_t> files;
std::set<std::string> extra; std::vector<std::string> masks;
FileLister::recursiveAddFiles(files, ".", extra); PathMatch matcher(masks);
FileLister::recursiveAddFiles(files, ".", matcher);
// In case there are leading "./".. // In case there are leading "./"..
for (std::map<std::string, std::size_t>::iterator i = files.begin(); i != files.end();) { for (std::map<std::string, std::size_t>::iterator i = files.begin(); i != files.end();) {

View File

@ -20,6 +20,7 @@
#include "testsuite.h" #include "testsuite.h"
#include "cppcheckexecutor.h" #include "cppcheckexecutor.h"
#include "path.h" #include "path.h"
#include "pathmatch.h"
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
#include <algorithm> #include <algorithm>
@ -40,10 +41,12 @@ private:
REDIRECT; REDIRECT;
std::map<std::string, std::size_t> files; std::map<std::string, std::size_t> files;
const std::vector<std::string> masks;
const PathMatch matcher(masks);
#ifdef _WIN32 #ifdef _WIN32
FileLister::recursiveAddFiles(files, "..\\samples"); FileLister::recursiveAddFiles(files, "..\\samples", matcher);
#else #else
FileLister::recursiveAddFiles(files, "samples"); FileLister::recursiveAddFiles(files, "samples", matcher);
#endif #endif
for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i) { for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i) {
CLEAR_REDIRECT_ERROUT; CLEAR_REDIRECT_ERROUT;