fixed #12145 - provided order of source files is not preserved (#5625)

This commit is contained in:
Oliver Stöneberg 2023-11-07 21:21:24 +01:00 committed by GitHub
parent 8c63c8ced1
commit bc174c502a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 211 additions and 103 deletions

View File

@ -209,8 +209,7 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
}
if (!pathnames.empty()) {
// TODO: this should be a vector or list so the order is kept
std::map<std::string, std::size_t> files;
std::list<std::pair<std::string, std::size_t>> files;
// TODO: this needs to be inlined into PathMatch as it depends on the underlying filesystem
#if defined(_WIN32)
// For Windows we want case-insensitive path matching
@ -286,7 +285,7 @@ int CppCheckExecutor::check_wrapper(CppCheck& cppcheck)
return check_internal(cppcheck);
}
bool CppCheckExecutor::reportSuppressions(const Settings &settings, bool unusedFunctionCheckEnabled, const std::map<std::string, std::size_t> &files, ErrorLogger& errorLogger) {
bool CppCheckExecutor::reportSuppressions(const Settings &settings, bool unusedFunctionCheckEnabled, const std::list<std::pair<std::string, std::size_t>> &files, ErrorLogger& errorLogger) {
const auto& suppressions = settings.nomsg.getSuppressions();
if (std::any_of(suppressions.begin(), suppressions.end(), [](const Suppressions::Suppression& s) {
return s.errorId == "unmatchedSuppression" && s.fileName.empty() && s.lineNumber == Suppressions::Suppression::NO_LINE;
@ -295,7 +294,7 @@ bool CppCheckExecutor::reportSuppressions(const Settings &settings, bool unusedF
bool err = false;
if (settings.useSingleJob()) {
for (std::map<std::string, std::size_t>::const_iterator i = files.cbegin(); i != files.cend(); ++i) {
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = files.cbegin(); i != files.cend(); ++i) {
err |= Suppressions::reportUnmatchedSuppressions(
settings.nomsg.getUnmatchedLocalSuppressions(i->first, unusedFunctionCheckEnabled), errorLogger);
}
@ -326,7 +325,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
settings.loadSummaries();
std::list<std::string> fileNames;
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i)
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i)
fileNames.emplace_back(i->first);
AnalyzerInformation::writeFilesTxt(settings.buildDir, fileNames, settings.userDefines, mFileSettings);
}

View File

@ -28,9 +28,9 @@
#include <ctime>
#include <iosfwd>
#include <list>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
class CppCheck;
@ -129,7 +129,7 @@ private:
*/
bool parseFromArgs(Settings &settings, int argc, const char* const argv[]);
static bool reportSuppressions(const Settings &settings, bool unusedFunctionCheckEnabled, const std::map<std::string, std::size_t> &files, ErrorLogger& errorLogger);
static bool reportSuppressions(const Settings &settings, bool unusedFunctionCheckEnabled, const std::list<std::pair<std::string, std::size_t>> &files, ErrorLogger& errorLogger);
/**
* Wrapper around check_internal
@ -183,7 +183,7 @@ private:
/**
* Filename associated with size of file
*/
std::map<std::string, std::size_t> mFiles;
std::list<std::pair<std::string, std::size_t>> mFiles;
std::list<FileSettings> mFileSettings;

View File

@ -30,7 +30,7 @@
struct FileSettings;
Executor::Executor(const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger)
Executor::Executor(const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger)
: mFiles(files), mFileSettings(fileSettings), mSettings(settings), mSuppressions(suppressions), mErrorLogger(errorLogger)
{
// the two inputs may only be used exclusively

View File

@ -21,9 +21,9 @@
#include <cstddef>
#include <list>
#include <map>
#include <mutex>
#include <string>
#include <utility>
class Settings;
class ErrorLogger;
@ -40,7 +40,7 @@ struct FileSettings;
*/
class Executor {
public:
Executor(const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger);
Executor(const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger);
virtual ~Executor() = default;
Executor(const Executor &) = delete;
@ -66,7 +66,7 @@ protected:
*/
bool hasToLog(const ErrorMessage &msg);
const std::map<std::string, std::size_t> &mFiles;
const std::list<std::pair<std::string, std::size_t>> &mFiles;
const std::list<FileSettings>& mFileSettings;
const Settings &mSettings;
Suppressions &mSuppressions;

View File

@ -24,6 +24,7 @@
#include <cstddef>
#include <cstring>
#include <iterator>
#include <memory>
#ifdef _WIN32
@ -39,12 +40,12 @@
// When compiling Unicode targets WinAPI automatically uses *W Unicode versions
// of called functions. Thus, we explicitly call *A versions of the functions.
std::string FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
std::string FileLister::recursiveAddFiles(std::list<std::pair<std::string, std::size_t>>&files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
{
return addFiles(files, path, extra, true, ignored);
}
std::string 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::string FileLister::addFiles(std::list<std::pair<std::string, std::size_t>>&files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
{
if (path.empty())
return "no path specified";
@ -112,18 +113,27 @@ std::string FileLister::addFiles(std::map<std::string, std::size_t> &files, cons
// 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;
files.emplace_back(nativename, (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow);
#else
files[nativename] = ffd.nFileSizeLow;
files.emplace_back(nativename, ffd.nFileSizeLow);
#endif
}
} else {
// Directory
if (recursive) {
if (!ignored.match(fname)) {
std::string err = FileLister::recursiveAddFiles(files, fname, extra, ignored);
std::list<std::pair<std::string, std::size_t>> filesSorted;
std::string err = FileLister::recursiveAddFiles(filesSorted, fname, extra, ignored);
if (!err.empty())
return err;
// files inside directories need to be sorted as the filesystem doesn't provide a stable order
filesSorted.sort([](const decltype(filesSorted)::value_type& a, const decltype(filesSorted)::value_type& b) {
return a.first < b.first;
});
files.insert(files.end(), std::make_move_iterator(filesSorted.begin()), std::make_move_iterator(filesSorted.end()));
}
}
}
@ -155,7 +165,7 @@ std::string FileLister::addFiles(std::map<std::string, std::size_t> &files, cons
#include <sys/stat.h>
#include <cerrno>
static std::string addFiles2(std::map<std::string, std::size_t> &files,
static std::string addFiles2(std::list<std::pair<std::string, std::size_t>> &files,
const std::string &path,
const std::set<std::string> &extra,
bool recursive,
@ -178,6 +188,8 @@ static std::string addFiles2(std::map<std::string, std::size_t> &files,
std::string new_path = path;
new_path += '/';
std::list<std::pair<std::string, std::size_t>> filesSorted;
while (const dirent* dir_result = readdir(dir)) {
if ((std::strcmp(dir_result->d_name, ".") == 0) ||
(std::strcmp(dir_result->d_name, "..") == 0))
@ -193,15 +205,16 @@ static std::string addFiles2(std::map<std::string, std::size_t> &files,
#endif
if (path_is_directory) {
if (recursive && !ignored.match(new_path)) {
std::string err = addFiles2(files, new_path, extra, recursive, ignored);
std::string err = addFiles2(filesSorted, new_path, extra, recursive, ignored);
if (!err.empty()) {
return err;
}
}
} else {
if (Path::acceptFile(new_path, extra) && !ignored.match(new_path)) {
if (stat(new_path.c_str(), &file_stat) != -1)
files[new_path] = file_stat.st_size;
if (stat(new_path.c_str(), &file_stat) != -1) {
filesSorted.emplace_back(new_path, file_stat.st_size);
}
else {
const int err = errno;
return "could not stat file '" + new_path + "' (errno: " + std::to_string(err) + ")";
@ -209,18 +222,25 @@ static std::string addFiles2(std::map<std::string, std::size_t> &files,
}
}
}
// files inside directories need to be sorted as the filesystem doesn't provide a stable order
filesSorted.sort([](const decltype(filesSorted)::value_type& a, const decltype(filesSorted)::value_type& b) {
return a.first < b.first;
});
files.insert(files.end(), std::make_move_iterator(filesSorted.begin()), std::make_move_iterator(filesSorted.end()));
} else
files[path] = file_stat.st_size;
files.emplace_back(path, file_stat.st_size);
}
return "";
}
std::string FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
std::string FileLister::recursiveAddFiles(std::list<std::pair<std::string, std::size_t>> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored)
{
return addFiles(files, path, extra, true, ignored);
}
std::string 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::string FileLister::addFiles(std::list<std::pair<std::string, std::size_t>> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored)
{
if (path.empty())
return "no path specified";

View File

@ -20,9 +20,10 @@
#define filelisterH
#include <cstddef>
#include <map>
#include <list>
#include <set>
#include <string>
#include <utility>
class PathMatch;
@ -37,12 +38,12 @@ public:
* Add source files from given directory and all subdirectries to the
* given map. Only files with accepted extensions
* (*.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 list that associates the size of each file with its name
* @param path root path
* @param ignored ignored paths
* @return On success, an empty string is returned. On error, a error message is returned.
*/
static std::string recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const PathMatch& ignored) {
static std::string recursiveAddFiles(std::list<std::pair<std::string, std::size_t>> &files, const std::string &path, const PathMatch& ignored) {
const std::set<std::string> extra;
return recursiveAddFiles(files, path, extra, ignored);
}
@ -52,27 +53,27 @@ public:
* Add source files from given directory and all subdirectries to the
* given map. Only files with accepted extensions
* (*.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 list that associates the size of each file with its name
* @param path root path
* @param extra Extra file extensions
* @param ignored ignored paths
* @return On success, an empty string is returned. On error, a error message is returned.
*/
static std::string recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored);
static std::string recursiveAddFiles(std::list<std::pair<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.
* Add source files from given directory and all subdirectries to the
* given map. Only files with accepted extensions
* (*.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 list that associates the size of each file with its name
* @param path root path
* @param extra Extra file extensions
* @param recursive Enable recursion
* @param ignored ignored paths
* @return On success, an empty string is returned. On error, a error message is returned.
*/
static std::string addFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored);
static std::string addFiles(std::list<std::pair<std::string, std::size_t>> &files, const std::string &path, const std::set<std::string> &extra, bool recursive, const PathMatch& ignored);
};
/// @}

View File

@ -38,6 +38,7 @@
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <sstream> // IWYU pragma: keep
#include <sys/select.h>
#include <sys/wait.h>
@ -60,7 +61,7 @@ enum class Color;
using std::memset;
ProcessExecutor::ProcessExecutor(const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger, CppCheck::ExecuteCmdFn executeCommand)
ProcessExecutor::ProcessExecutor(const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger, CppCheck::ExecuteCmdFn executeCommand)
: Executor(files, fileSettings, settings, suppressions, errorLogger)
, mExecuteCommand(std::move(executeCommand))
{
@ -236,7 +237,7 @@ unsigned int ProcessExecutor::check()
std::map<pid_t, std::string> childFile;
std::map<int, std::string> pipeFile;
std::size_t processedsize = 0;
std::map<std::string, std::size_t>::const_iterator iFile = mFiles.cbegin();
std::list<std::pair<std::string, std::size_t>>::const_iterator iFile = mFiles.cbegin();
std::list<FileSettings>::const_iterator iFileSettings = mFileSettings.cbegin();
for (;;) {
// Start a new child
@ -325,7 +326,9 @@ unsigned int ProcessExecutor::check()
std::size_t size = 0;
if (p != pipeFile.end()) {
pipeFile.erase(p);
const std::map<std::string, std::size_t>::const_iterator fs = mFiles.find(name);
const auto fs = std::find_if(mFiles.cbegin(), mFiles.cend(), [&name](const std::pair<std::string, std::size_t>& entry) {
return entry.first == name;
});
if (fs != mFiles.end()) {
size = fs->second;
}

View File

@ -24,8 +24,8 @@
#include <cstddef>
#include <list>
#include <map>
#include <string>
#include <utility>
class Settings;
class ErrorLogger;
@ -41,7 +41,7 @@ struct FileSettings;
*/
class ProcessExecutor : public Executor {
public:
ProcessExecutor(const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger, CppCheck::ExecuteCmdFn executeCommand);
ProcessExecutor(const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger, CppCheck::ExecuteCmdFn executeCommand);
ProcessExecutor(const ProcessExecutor &) = delete;
void operator=(const ProcessExecutor &) = delete;

View File

@ -31,7 +31,7 @@
class ErrorLogger;
SingleExecutor::SingleExecutor(CppCheck &cppcheck, const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger)
SingleExecutor::SingleExecutor(CppCheck &cppcheck, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger)
: Executor(files, fileSettings, settings, suppressions, errorLogger)
, mCppcheck(cppcheck)
{
@ -50,7 +50,7 @@ unsigned int SingleExecutor::check()
std::size_t processedsize = 0;
unsigned int c = 0;
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i) {
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i) {
if (!mSettings.library.markupFile(i->first) || !mSettings.library.processMarkupAfterCode(i->first)) {
result += mCppcheck.check(i->first);
processedsize += i->second;
@ -77,7 +77,7 @@ unsigned int SingleExecutor::check()
// second loop to parse all markup files which may not work until all
// c/cpp files have been parsed and checked
// TODO: get rid of duplicated code
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i) {
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i) {
if (mSettings.library.markupFile(i->first) && mSettings.library.processMarkupAfterCode(i->first)) {
result += mCppcheck.check(i->first);
processedsize += i->second;

View File

@ -23,8 +23,8 @@
#include <cstddef>
#include <list>
#include <map>
#include <string>
#include <utility>
class ErrorLogger;
class Settings;
@ -35,7 +35,7 @@ struct FileSettings;
class SingleExecutor : public Executor
{
public:
SingleExecutor(CppCheck &cppcheck, const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger);
SingleExecutor(CppCheck &cppcheck, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger);
SingleExecutor(const SingleExecutor &) = delete;
void operator=(const SingleExecutor &) = delete;

View File

@ -39,7 +39,7 @@
enum class Color;
ThreadExecutor::ThreadExecutor(const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger, CppCheck::ExecuteCmdFn executeCommand)
ThreadExecutor::ThreadExecutor(const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger, CppCheck::ExecuteCmdFn executeCommand)
: Executor(files, fileSettings, settings, suppressions, errorLogger)
, mExecuteCommand(std::move(executeCommand))
{
@ -81,7 +81,7 @@ private:
class ThreadData
{
public:
ThreadData(ThreadExecutor &threadExecutor, ErrorLogger &errorLogger, const Settings &settings, const std::map<std::string, std::size_t> &files, const std::list<FileSettings> &fileSettings, CppCheck::ExecuteCmdFn executeCommand)
ThreadData(ThreadExecutor &threadExecutor, ErrorLogger &errorLogger, const Settings &settings, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings> &fileSettings, CppCheck::ExecuteCmdFn executeCommand)
: mFiles(files), mFileSettings(fileSettings), mSettings(settings), mExecuteCommand(std::move(executeCommand)), logForwarder(threadExecutor, errorLogger)
{
mItNextFile = mFiles.begin();
@ -140,8 +140,8 @@ public:
}
private:
const std::map<std::string, std::size_t> &mFiles;
std::map<std::string, std::size_t>::const_iterator mItNextFile;
const std::list<std::pair<std::string, std::size_t>> &mFiles;
std::list<std::pair<std::string, std::size_t>>::const_iterator mItNextFile;
const std::list<FileSettings> &mFileSettings;
std::list<FileSettings>::const_iterator mItNextFileSettings;

View File

@ -24,8 +24,8 @@
#include <cstddef>
#include <list>
#include <map>
#include <string>
#include <utility>
class Settings;
class ErrorLogger;
@ -43,7 +43,7 @@ class ThreadExecutor : public Executor {
friend class SyncLogForwarder;
public:
ThreadExecutor(const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger, CppCheck::ExecuteCmdFn executeCommand);
ThreadExecutor(const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger, CppCheck::ExecuteCmdFn executeCommand);
ThreadExecutor(const ThreadExecutor &) = delete;
void operator=(const ThreadExecutor &) = delete;

View File

@ -34,10 +34,10 @@
#include <cstddef>
#include <iterator>
#include <list>
#include <map>
#include <ostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <QByteArray>
@ -111,9 +111,10 @@ void CheckThread::run()
if (!mFiles.isEmpty() || mAnalyseWholeProgram) {
mAnalyseWholeProgram = false;
qDebug() << "Whole program analysis";
std::map<std::string,std::size_t> files2;
for (const QString& file : mFiles)
files2[file.toStdString()] = 0;
std::list<std::pair<std::string, std::size_t>> files2;
std::transform(mFiles.cbegin(), mFiles.cend(), std::back_inserter(files2), [&](const QString& file) {
return std::pair<std::string, std::size_t>{file.toStdString(), 0};
});
mCppcheck.analyseWholeProgram(mCppcheck.settings().buildDir, files2, {});
mFiles.clear();
emit done();

View File

@ -39,6 +39,7 @@
#include <cctype>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <utility>
#include <numeric>

View File

@ -1451,7 +1451,7 @@ void CppCheck::executeAddons(const std::vector<std::string>& files, const std::s
}
}
void CppCheck::executeAddonsWholeProgram(const std::map<std::string, std::size_t> &files)
void CppCheck::executeAddonsWholeProgram(const std::list<std::pair<std::string, std::size_t>> &files)
{
if (mSettings.addons.empty())
return;
@ -1722,7 +1722,7 @@ bool CppCheck::analyseWholeProgram()
return errors && (mExitCode > 0);
}
void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings)
void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings)
{
executeAddonsWholeProgram(files); // TODO: pass FileSettings
if (buildDir.empty()) {
@ -1790,7 +1790,7 @@ bool CppCheck::isUnusedFunctionCheckEnabled() const
return (mSettings.useSingleJob() && mSettings.checks.isEnabled(Checks::unusedFunction));
}
void CppCheck::removeCtuInfoFiles(const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings)
void CppCheck::removeCtuInfoFiles(const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings)
{
if (mSettings.buildDir.empty()) {
for (const auto& f: files) {

View File

@ -137,14 +137,14 @@ public:
void analyseClangTidy(const FileSettings &fileSettings);
/** analyse whole program use .analyzeinfo files */
void analyseWholeProgram(const std::string &buildDir, const std::map<std::string, std::size_t> &files, const std::list<FileSettings>& fileSettings);
void analyseWholeProgram(const std::string &buildDir, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings);
/** Check if the user wants to check for unused functions
* and if it's possible at all */
bool isUnusedFunctionCheckEnabled() const;
/** Remove *.ctu-info files */
void removeCtuInfoFiles(const std::map<std::string, std::size_t>& files, const std::list<FileSettings>& fileSettings); // cppcheck-suppress functionConst // has side effects
void removeCtuInfoFiles(const std::list<std::pair<std::string, std::size_t>>& files, const std::list<FileSettings>& fileSettings); // cppcheck-suppress functionConst // has side effects
static void resetTimerResults();
static void printTimerResults(SHOWTIME_MODES mode);
@ -188,7 +188,7 @@ private:
/**
* Execute addons
*/
void executeAddonsWholeProgram(const std::map<std::string, std::size_t> &files);
void executeAddonsWholeProgram(const std::list<std::pair<std::string, std::size_t>> &files);
#ifdef HAVE_RULES
/**

View File

@ -387,3 +387,48 @@ def test_project_file_filter_no_match(tmpdir):
]
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
def test_project_file_order(tmpdir):
test_file_a = os.path.join(tmpdir, 'a.c')
with open(test_file_a, 'wt'):
pass
test_file_b = os.path.join(tmpdir, 'b.c')
with open(test_file_b, 'wt'):
pass
test_file_c = os.path.join(tmpdir, 'c.c')
with open(test_file_c, 'wt'):
pass
test_file_d = os.path.join(tmpdir, 'd.c')
with open(test_file_d, 'wt'):
pass
project_file = os.path.join(tmpdir, 'test.cppcheck')
with open(project_file, 'wt') as f:
f.write(
"""<?xml version="1.0" encoding="UTF-8"?>
<project>
<paths>
<dir name="{}"/>
<dir name="{}"/>
<dir name="{}"/>
<dir name="{}"/>
</paths>
</project>""".format(test_file_c, test_file_d, test_file_b, test_file_a))
args = ['--project={}'.format(project_file)]
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
lines = stdout.splitlines()
assert lines == [
'Checking {} ...'.format(test_file_c),
'1/4 files checked 0% done',
'Checking {} ...'.format(test_file_d),
'2/4 files checked 0% done',
'Checking {} ...'.format(test_file_b),
'3/4 files checked 0% done',
'Checking {} ...'.format(test_file_a),
'4/4 files checked 0% done'
]
assert stderr == ''

View File

@ -632,3 +632,35 @@ def test_file_filter_no_match(tmpdir):
]
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
def test_file_order(tmpdir):
test_file_a = os.path.join(tmpdir, 'a.c')
with open(test_file_a, 'wt'):
pass
test_file_b = os.path.join(tmpdir, 'b.c')
with open(test_file_b, 'wt'):
pass
test_file_c = os.path.join(tmpdir, 'c.c')
with open(test_file_c, 'wt'):
pass
test_file_d = os.path.join(tmpdir, 'd.c')
with open(test_file_d, 'wt'):
pass
args = [test_file_c, test_file_d, test_file_b, test_file_a]
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
lines = stdout.splitlines()
assert lines == [
'Checking {} ...'.format(test_file_c),
'1/4 files checked 0% done',
'Checking {} ...'.format(test_file_d),
'2/4 files checked 0% done',
'Checking {} ...'.format(test_file_b),
'3/4 files checked 0% done',
'Checking {} ...'.format(test_file_a),
'4/4 files checked 0% done'
]
assert stderr == ''

View File

@ -75,7 +75,7 @@ ScopedFile::~ScopedFile() {
// TODO: remove all files
// TODO: simplify the function call
// hack to be able to delete *.plist output files
std::map<std::string, std::size_t> files;
std::list<std::pair<std::string, std::size_t>> files;
const std::string res = FileLister::addFiles(files, mPath, {".plist"}, false, PathMatch({}));
if (!res.empty()) {
std::cout << "ScopedFile(" << mPath + ") - generating file list failed (" << res << ")" << std::endl;

View File

@ -21,8 +21,9 @@
#include "pathmatch.h"
#include "fixture.h"
#include <algorithm>
#include <cstddef>
#include <map>
#include <list>
#include <stdexcept>
#include <string>
#include <vector>
@ -57,7 +58,7 @@ private:
const std::string adddir = findBaseDir() + ".";
// Recursively add add files..
std::map<std::string, std::size_t> files;
std::list<std::pair<std::string, std::size_t>> files;
std::vector<std::string> masks;
PathMatch matcher(masks);
std::string err = FileLister::recursiveAddFiles(files, adddir, matcher);
@ -73,19 +74,25 @@ private:
const std::string dirprefix = adddir + "/";
#endif
const auto find_file = [&](const std::string& name) {
return std::find_if(files.cbegin(), files.cend(), [&name](const std::pair<std::string, std::size_t>& entry) {
return entry.first == name;
});
};
// Make sure source files are added..
ASSERT(files.find(dirprefix + "cli/main.cpp") != files.end());
ASSERT(files.find(dirprefix + "lib/token.cpp") != files.end());
ASSERT(files.find(dirprefix + "lib/tokenize.cpp") != files.end());
ASSERT(files.find(dirprefix + "gui/main.cpp") != files.end());
ASSERT(files.find(dirprefix + "test/testfilelister.cpp") != files.end());
ASSERT(find_file(dirprefix + "cli/main.cpp") != files.end());
ASSERT(find_file(dirprefix + "lib/token.cpp") != files.end());
ASSERT(find_file(dirprefix + "lib/tokenize.cpp") != files.end());
ASSERT(find_file(dirprefix + "gui/main.cpp") != files.end());
ASSERT(find_file(dirprefix + "test/testfilelister.cpp") != files.end());
// Make sure headers are not added..
ASSERT(files.find(dirprefix + "lib/tokenize.h") == files.end());
ASSERT(find_file(dirprefix + "lib/tokenize.h") == files.end());
}
void recursiveAddFilesEmptyPath() const {
std::map<std::string, std::size_t> files;
std::list<std::pair<std::string, std::size_t>> files;
const std::string err = FileLister::recursiveAddFiles(files, "", PathMatch({}));
ASSERT_EQUALS("no path specified", err);
}
@ -93,7 +100,7 @@ private:
void excludeFile1() const {
const std::string basedir = findBaseDir();
std::map<std::string, std::size_t> files;
std::list<std::pair<std::string, std::size_t>> files;
std::vector<std::string> ignored{"lib/token.cpp"};
PathMatch matcher(ignored);
std::string err = FileLister::recursiveAddFiles(files, basedir + "lib/token.cpp", matcher);
@ -104,7 +111,7 @@ private:
void excludeFile2() const {
const std::string basedir = findBaseDir();
std::map<std::string, std::size_t> files;
std::list<std::pair<std::string, std::size_t>> files;
std::vector<std::string> ignored;
PathMatch matcher(ignored);
std::string err = FileLister::recursiveAddFiles(files, basedir + "lib/token.cpp", matcher);

View File

@ -74,11 +74,11 @@ private:
std::list<FileSettings> fileSettings;
std::map<std::string, std::size_t> filemap;
std::list<std::pair<std::string, std::size_t>> filelist;
if (opt.filesList.empty()) {
for (int i = 1; i <= files; ++i) {
std::string f_s = fprefix() + "_" + std::to_string(i) + ".cpp";
filemap[f_s] = data.size();
filelist.emplace_back(f_s, data.size());
if (useFS) {
FileSettings fs;
fs.filename = std::move(f_s);
@ -89,7 +89,7 @@ private:
else {
for (const auto& f : opt.filesList)
{
filemap[f] = data.size();
filelist.emplace_back(f, data.size());
if (useFS) {
FileSettings fs;
fs.filename = f;
@ -117,15 +117,15 @@ private:
};
std::vector<std::unique_ptr<ScopedFile>> scopedfiles;
scopedfiles.reserve(filemap.size());
for (std::map<std::string, std::size_t>::const_iterator i = filemap.cbegin(); i != filemap.cend(); ++i)
scopedfiles.reserve(filelist.size());
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = filelist.cbegin(); i != filelist.cend(); ++i)
scopedfiles.emplace_back(new ScopedFile(i->first, data));
// clear files list so only fileSettings are used
if (useFS)
filemap.clear();
filelist.clear();
ProcessExecutor executor(filemap, fileSettings, s, s.nomsg, *this, executeFn);
ProcessExecutor executor(filelist, fileSettings, s, s.nomsg, *this, executeFn);
ASSERT_EQUALS(result, executor.check());
ASSERT_EQUALS(opt.executeCommandCalled, executeCommandCalled);
ASSERT_EQUALS(opt.exe, exe);

View File

@ -79,11 +79,11 @@ private:
std::list<FileSettings> fileSettings;
std::map<std::string, std::size_t> filemap;
std::list<std::pair<std::string, std::size_t>> filelist;
if (opt.filesList.empty()) {
for (int i = 1; i <= files; ++i) {
std::string f_s = fprefix() + "_" + zpad3(i) + ".cpp";
filemap[f_s] = data.size();
filelist.emplace_back(f_s, data.size());
if (useFS) {
FileSettings fs;
fs.filename = std::move(f_s);
@ -94,7 +94,7 @@ private:
else {
for (const auto& f : opt.filesList)
{
filemap[f] = data.size();
filelist.emplace_back(f, data.size());
if (useFS) {
FileSettings fs;
fs.filename = f;
@ -123,15 +123,15 @@ private:
cppcheck.settings() = s;
std::vector<std::unique_ptr<ScopedFile>> scopedfiles;
scopedfiles.reserve(filemap.size());
for (std::map<std::string, std::size_t>::const_iterator i = filemap.cbegin(); i != filemap.cend(); ++i)
scopedfiles.reserve(filelist.size());
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = filelist.cbegin(); i != filelist.cend(); ++i)
scopedfiles.emplace_back(new ScopedFile(i->first, data));
// clear files list so only fileSettings are used
if (useFS)
filemap.clear();
filelist.clear();
SingleExecutor executor(cppcheck, filemap, fileSettings, s, s.nomsg, *this);
SingleExecutor executor(cppcheck, filelist, fileSettings, s, s.nomsg, *this);
ASSERT_EQUALS(result, executor.check());
ASSERT_EQUALS(opt.executeCommandCalled, executeCommandCalled);
ASSERT_EQUALS(opt.exe, exe);

View File

@ -193,9 +193,9 @@ private:
errout.str("");
output.str("");
std::map<std::string, std::size_t> files;
std::list<std::pair<std::string, std::size_t>> files;
for (std::map<std::string, std::string>::const_iterator i = f.cbegin(); i != f.cend(); ++i) {
files[i->first] = i->second.size();
files.emplace_back(i->first, i->second.size());
}
CppCheck cppCheck(*this, true, nullptr);
@ -227,8 +227,8 @@ private:
errout.str("");
output.str("");
std::map<std::string, std::size_t> files;
files["test.cpp"] = strlen(code);
std::list<std::pair<std::string, std::size_t>> files;
files.emplace_back("test.cpp", strlen(code));
Settings settings;
settings.jobs = 2;
@ -242,7 +242,7 @@ private:
ThreadExecutor executor(files, fileSettings, settings, settings.nomsg, *this, CppCheckExecutor::executeCommand);
std::vector<std::unique_ptr<ScopedFile>> scopedfiles;
scopedfiles.reserve(files.size());
for (std::map<std::string, std::size_t>::const_iterator i = files.cbegin(); i != files.cend(); ++i)
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = files.cbegin(); i != files.cend(); ++i)
scopedfiles.emplace_back(new ScopedFile(i->first, code));
const unsigned int exitCode = executor.check();
@ -257,8 +257,8 @@ private:
errout.str("");
output.str("");
std::map<std::string, std::size_t> files;
files["test.cpp"] = strlen(code);
std::list<std::pair<std::string, std::size_t>> files;
files.emplace_back("test.cpp", strlen(code));
Settings settings;
settings.jobs = 2;
@ -272,7 +272,7 @@ private:
ProcessExecutor executor(files, fileSettings, settings, settings.nomsg, *this, CppCheckExecutor::executeCommand);
std::vector<std::unique_ptr<ScopedFile>> scopedfiles;
scopedfiles.reserve(files.size());
for (std::map<std::string, std::size_t>::const_iterator i = files.cbegin(); i != files.cend(); ++i)
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = files.cbegin(); i != files.cend(); ++i)
scopedfiles.emplace_back(new ScopedFile(i->first, code));
const unsigned int exitCode = executor.check();

View File

@ -74,11 +74,11 @@ private:
std::list<FileSettings> fileSettings;
std::map<std::string, std::size_t> filemap;
std::list<std::pair<std::string, std::size_t>> filelist;
if (opt.filesList.empty()) {
for (int i = 1; i <= files; ++i) {
std::string f_s = fprefix() + "_" + std::to_string(i) + ".cpp";
filemap[f_s] = data.size();
filelist.emplace_back(f_s, data.size());
if (useFS) {
FileSettings fs;
fs.filename = std::move(f_s);
@ -89,7 +89,7 @@ private:
else {
for (const auto& f : opt.filesList)
{
filemap[f] = data.size();
filelist.emplace_back(f, data.size());
if (useFS) {
FileSettings fs;
fs.filename = f;
@ -118,15 +118,15 @@ private:
};
std::vector<std::unique_ptr<ScopedFile>> scopedfiles;
scopedfiles.reserve(filemap.size());
for (std::map<std::string, std::size_t>::const_iterator i = filemap.cbegin(); i != filemap.cend(); ++i)
scopedfiles.reserve(filelist.size());
for (std::list<std::pair<std::string, std::size_t>>::const_iterator i = filelist.cbegin(); i != filelist.cend(); ++i)
scopedfiles.emplace_back(new ScopedFile(i->first, data));
// clear files list so only fileSettings are used
if (useFS)
filemap.clear();
filelist.clear();
ThreadExecutor executor(filemap, fileSettings, s, s.nomsg, *this, executeFn);
ThreadExecutor executor(filelist, fileSettings, s, s.nomsg, *this, executeFn);
ASSERT_EQUALS(result, executor.check());
ASSERT_EQUALS(opt.executeCommandCalled, executeCommandCalled);
ASSERT_EQUALS(opt.exe, exe);

View File

@ -24,6 +24,7 @@
#include <fstream> // IWYU pragma: keep
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
@ -139,16 +140,16 @@ static void compilefiles(std::ostream &fout, const std::vector<std::string> &fil
static std::string getCppFiles(std::vector<std::string> &files, const std::string &path, bool recursive)
{
std::map<std::string,size_t> filemap;
std::list<std::pair<std::string, std::size_t>> filelist;
const std::set<std::string> extra;
const std::vector<std::string> masks;
const PathMatch matcher(masks);
std::string err = FileLister::addFiles(filemap, path, extra, recursive, matcher);
std::string err = FileLister::addFiles(filelist, path, extra, recursive, matcher);
if (!err.empty())
return err;
// add *.cpp files to the "files" vector..
for (const std::pair<const std::string&, size_t> file : filemap) {
for (const std::pair<const std::string&, size_t> file : filelist) {
if (endsWith(file.first, ".cpp"))
files.push_back(file.first);
}

View File

@ -70,7 +70,6 @@
<OutputFile>$(OutDir)dmake.exe</OutputFile>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<TargetMachine>MachineX86</TargetMachine>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -93,7 +92,6 @@
<SubSystem>Console</SubSystem>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<TargetMachine>MachineX86</TargetMachine>
</Link>
</ItemDefinitionGroup>
<ItemGroup>