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

View File

@ -18,6 +18,7 @@
#include "filelister.h"
#include "path.h"
#include "pathmatch.h"
#include <cstring>
#include <string>
#include <sstream>
@ -67,7 +68,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 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);
@ -123,7 +124,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, extra)) {
if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.Match(fname)) {
// 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;
@ -133,7 +134,8 @@ void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, co
}
} else {
// Directory
FileLister::recursiveAddFiles(files, fname, extra);
if (!ignored.Match(fname))
FileLister::recursiveAddFiles(files, fname, extra, ignored);
}
} 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,
const std::string &path,
const std::set<std::string> &extra,
bool recursive
bool recursive,
const PathMatch& ignored
)
{
std::ostringstream oss;
@ -220,7 +223,7 @@ void FileLister::addFiles2(std::set<std::string> &seen_paths,
if (filename[filename.length()-1] != '/') {
// 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);
struct stat sb;
@ -232,25 +235,26 @@ void FileLister::addFiles2(std::set<std::string> &seen_paths,
}
} else if (recursive) {
// Directory
seen_paths.insert(absolute_path);
addFiles2(seen_paths, files, filename, extra, recursive);
if (!ignored.Match(filename)) {
seen_paths.insert(absolute_path);
addFiles2(seen_paths, files, filename, extra, recursive, ignored);
}
}
}
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;
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;
addFiles2(seen_paths, files, path, extra, recursive);
addFiles2(seen_paths, files, path, extra, recursive, ignored);
}
bool FileLister::isDirectory(const std::string &path)

View File

@ -23,6 +23,8 @@
#include <set>
#include <map>
class PathMatch;
/// @addtogroup CLI
/// @{
@ -36,10 +38,11 @@ public:
* (*.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 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;
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 path root path
* @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.
@ -62,8 +66,9 @@ public:
* @param path root path
* @param extra Extra file extensions
* @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?
@ -86,7 +91,8 @@ private:
std::map<std::string, std::size_t> &files,
const std::string &path,
const std::set<std::string> &extra,
bool recursive);
bool recursive,
const PathMatch& ignored);
#endif
};

View File

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

View File

@ -19,6 +19,7 @@
#include "testsuite.h"
#include "filelister.h"
#include "settings.h"
#include "pathmatch.h"
#include <fstream>
#ifndef _WIN32
@ -76,8 +77,9 @@ private:
void recursiveAddFiles() const {
// Recursively add add files..
std::map<std::string, std::size_t> files;
std::set<std::string> extra;
FileLister::recursiveAddFiles(files, ".", extra);
std::vector<std::string> masks;
PathMatch matcher(masks);
FileLister::recursiveAddFiles(files, ".", matcher);
// In case there are leading "./"..
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 "cppcheckexecutor.h"
#include "path.h"
#include "pathmatch.h"
#include <fstream>
#include <cstring>
#include <algorithm>
@ -40,10 +41,12 @@ private:
REDIRECT;
std::map<std::string, std::size_t> files;
const std::vector<std::string> masks;
const PathMatch matcher(masks);
#ifdef _WIN32
FileLister::recursiveAddFiles(files, "..\\samples");
FileLister::recursiveAddFiles(files, "..\\samples", matcher);
#else
FileLister::recursiveAddFiles(files, "samples");
FileLister::recursiveAddFiles(files, "samples", matcher);
#endif
for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i) {
CLEAR_REDIRECT_ERROUT;