Skip to content

Commit ce9272a

Browse files
committed
Refactorized file listing code (CLI):
- Apply PathMatch in FileLister::recursiveAddFiles() already to avoid touching directories that are ignored (danmar#5775) - Simplified code to warn about header exclusion; use Path::isHeader() instead of custom header filename detection
1 parent 28fd6ce commit ce9272a

6 files changed

Lines changed: 60 additions & 61 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -124,54 +124,39 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
124124
}
125125
}
126126

127-
const std::vector<std::string>& pathnames = parser.GetPathNames();
128-
129-
if (!pathnames.empty()) {
130-
// Execute recursiveAddFiles() to each given file parameter
131-
std::vector<std::string>::const_iterator iter;
132-
for (iter = pathnames.begin(); iter != pathnames.end(); ++iter)
133-
FileLister::recursiveAddFiles(_files, Path::toNativeSeparators(*iter), _settings->library.markupExtensions());
127+
// Output a warning for the user if he tries to exclude headers
128+
bool warn = false;
129+
const std::vector<std::string>& ignored = parser.GetIgnoredPaths();
130+
for (std::vector<std::string>::const_iterator i = ignored.cbegin(); i != ignored.cend(); ++i) {
131+
if (Path::isHeader(*i)) {
132+
warn = true;
133+
break;
134+
}
135+
}
136+
if (warn) {
137+
std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl;
138+
std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl;
134139
}
135140

136-
if (!_files.empty()) {
137-
// Remove header files from the list of ignored files.
138-
// Also output a warning for the user.
139-
// TODO: Remove all unknown files? (use FileLister::acceptFile())
140-
bool warn = false;
141-
std::vector<std::string> ignored = parser.GetIgnoredPaths();
142-
for (std::vector<std::string>::iterator i = ignored.begin(); i != ignored.end();) {
143-
const std::string extension = Path::getFilenameExtension(*i);
144-
if (extension == ".h" || extension == ".hpp") {
145-
i = ignored.erase(i);
146-
warn = true;
147-
} else
148-
++i;
149-
}
150-
if (warn) {
151-
std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl;
152-
std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl;
153-
}
141+
const std::vector<std::string>& pathnames = parser.GetPathNames();
154142

155143
#if defined(_WIN32)
156-
// For Windows we want case-insensitive path matching
157-
const bool caseSensitive = false;
144+
// For Windows we want case-insensitive path matching
145+
const bool caseSensitive = false;
158146
#else
159-
const bool caseSensitive = true;
147+
const bool caseSensitive = true;
160148
#endif
161-
PathMatch matcher(parser.GetIgnoredPaths(), caseSensitive);
162-
for (std::map<std::string, std::size_t>::iterator i = _files.begin(); i != _files.end();) {
163-
if (matcher.Match(i->first))
164-
_files.erase(i++);
165-
else
166-
++i;
167-
}
168-
} else {
169-
std::cout << "cppcheck: error: could not find or open any of the paths given." << std::endl;
170-
return false;
149+
if (!pathnames.empty()) {
150+
// Execute recursiveAddFiles() to each given file parameter
151+
PathMatch matcher(ignored, caseSensitive);
152+
for (std::vector<std::string>::const_iterator iter = pathnames.begin(); iter != pathnames.end(); ++iter)
153+
FileLister::recursiveAddFiles(_files, Path::toNativeSeparators(*iter), _settings->library.markupExtensions(), matcher);
171154
}
172155

173156
if (_files.empty()) {
174-
std::cout << "cppcheck: error: no files to check - all paths ignored." << std::endl;
157+
std::cout << "cppcheck: error: could not find or open any of the paths given." << std::endl;
158+
if (!ignored.empty())
159+
std::cout << "cppcheck: Maybe all paths were ignored?" << std::endl;
175160
return false;
176161
}
177162
return true;

cli/filelister.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "filelister.h"
2020
#include "path.h"
21+
#include "pathmatch.h"
2122
#include <cstring>
2223
#include <string>
2324
#include <sstream>
@@ -67,7 +68,7 @@ static BOOL MyFileExists(const std::string& path)
6768
return result;
6869
}
6970

70-
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra)
71+
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
7172
{
7273
const std::string cleanedPath = Path::toNativeSeparators(path);
7374

@@ -123,7 +124,7 @@ void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, co
123124
// File
124125
const std::string nativename = Path::fromNativeSeparators(fname);
125126

126-
if (!checkAllFilesInDir || Path::acceptFile(fname, extra)) {
127+
if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.Match(fname)) {
127128
// Limitation: file sizes are assumed to fit in a 'size_t'
128129
#ifdef _WIN64
129130
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
133134
}
134135
} else {
135136
// Directory
136-
FileLister::recursiveAddFiles(files, fname, extra);
137+
if (!ignored.Match(fname))
138+
FileLister::recursiveAddFiles(files, fname, extra, ignored);
137139
}
138140
} while (FindNextFileA(hFind, &ffd) != FALSE);
139141

@@ -193,7 +195,8 @@ void FileLister::addFiles2(std::set<std::string> &seen_paths,
193195
std::map<std::string, std::size_t> &files,
194196
const std::string &path,
195197
const std::set<std::string> &extra,
196-
bool recursive
198+
bool recursive,
199+
const PathMatch& ignored
197200
)
198201
{
199202
std::ostringstream oss;
@@ -220,7 +223,7 @@ void FileLister::addFiles2(std::set<std::string> &seen_paths,
220223
if (filename[filename.length()-1] != '/') {
221224
// File
222225

223-
if (Path::sameFileName(path,filename) || Path::acceptFile(filename, extra)) {
226+
if ((Path::sameFileName(path,filename) || Path::acceptFile(filename, extra)) && !ignored.Match(filename)) {
224227
seen_paths.insert(absolute_path);
225228

226229
struct stat sb;
@@ -232,25 +235,26 @@ void FileLister::addFiles2(std::set<std::string> &seen_paths,
232235
}
233236
} else if (recursive) {
234237
// Directory
235-
236-
seen_paths.insert(absolute_path);
237-
addFiles2(seen_paths, files, filename, extra, recursive);
238+
if (!ignored.Match(filename)) {
239+
seen_paths.insert(absolute_path);
240+
addFiles2(seen_paths, files, filename, extra, recursive, ignored);
241+
}
238242
}
239243
}
240244
globfree(&glob_results);
241245
}
242246

243247

244-
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra)
248+
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
245249
{
246250
std::set<std::string> seen_paths;
247-
addFiles2(seen_paths, files, path, extra, true);
251+
addFiles2(seen_paths, files, path, extra, true, ignored);
248252
}
249253

250-
void FileLister::addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive)
254+
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)
251255
{
252256
std::set<std::string> seen_paths;
253-
addFiles2(seen_paths, files, path, extra, recursive);
257+
addFiles2(seen_paths, files, path, extra, recursive, ignored);
254258
}
255259

256260
bool FileLister::isDirectory(const std::string &path)

cli/filelister.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <set>
2424
#include <map>
2525

26+
class PathMatch;
27+
2628
/// @addtogroup CLI
2729
/// @{
2830

@@ -36,10 +38,11 @@ class FileLister {
3638
* (*.c;*.cpp;*.cxx;*.c++;*.cc;*.txx) are added.
3739
* @param files output map that associates the size of each file with its name
3840
* @param path root path
41+
* @param ignored ignored paths
3942
*/
40-
static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path) {
43+
static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const PathMatch& ignored) {
4144
const std::set<std::string> extra;
42-
recursiveAddFiles(files, path, extra);
45+
recursiveAddFiles(files, path, extra, ignored);
4346
}
4447

4548
/**
@@ -50,8 +53,9 @@ class FileLister {
5053
* @param files output map that associates the size of each file with its name
5154
* @param path root path
5255
* @param extra Extra file extensions
56+
* @param ignored ignored paths
5357
*/
54-
static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra);
58+
static void recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored);
5559

5660
/**
5761
* @brief (Recursively) add source files to a map.
@@ -62,8 +66,9 @@ class FileLister {
6266
* @param path root path
6367
* @param extra Extra file extensions
6468
* @param recursive Enable recursion
69+
* @param ignored ignored paths
6570
*/
66-
static void addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive);
71+
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);
6772

6873
/**
6974
* @brief Is given path a directory?
@@ -86,7 +91,8 @@ class FileLister {
8691
std::map<std::string, std::size_t> &files,
8792
const std::string &path,
8893
const std::set<std::string> &extra,
89-
bool recursive);
94+
bool recursive,
95+
const PathMatch& ignored);
9096
#endif
9197

9298
};

lib/path.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ class CPPCHECKLIB Path {
145145
*/
146146
static bool isCPP(const std::string &extensionInLowerCase);
147147

148-
private:
149148
/**
150149
* @brief Is filename a header based on file extension
151150
* @param path filename to check. path info is optional

test/testfilelister.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "testsuite.h"
2020
#include "filelister.h"
2121
#include "settings.h"
22+
#include "pathmatch.h"
2223
#include <fstream>
2324

2425
#ifndef _WIN32
@@ -76,8 +77,9 @@ class TestFileLister: public TestFixture {
7677
void recursiveAddFiles() const {
7778
// Recursively add add files..
7879
std::map<std::string, std::size_t> files;
79-
std::set<std::string> extra;
80-
FileLister::recursiveAddFiles(files, ".", extra);
80+
std::vector<std::string> masks;
81+
PathMatch matcher(masks);
82+
FileLister::recursiveAddFiles(files, ".", matcher);
8183

8284
// In case there are leading "./"..
8385
for (std::map<std::string, std::size_t>::iterator i = files.begin(); i != files.end();) {

test/testsamples.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "testsuite.h"
2121
#include "cppcheckexecutor.h"
2222
#include "path.h"
23+
#include "pathmatch.h"
2324
#include <fstream>
2425
#include <cstring>
2526
#include <algorithm>
@@ -40,10 +41,12 @@ class TestSamples : public TestFixture {
4041
REDIRECT;
4142

4243
std::map<std::string, std::size_t> files;
44+
const std::vector<std::string> masks;
45+
const PathMatch matcher(masks);
4346
#ifdef _WIN32
44-
FileLister::recursiveAddFiles(files, "..\\samples");
47+
FileLister::recursiveAddFiles(files, "..\\samples", matcher);
4548
#else
46-
FileLister::recursiveAddFiles(files, "samples");
49+
FileLister::recursiveAddFiles(files, "samples", matcher);
4750
#endif
4851
for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i) {
4952
CLEAR_REDIRECT_ERROUT;

0 commit comments

Comments
 (0)