Skip to content

Commit dd627a2

Browse files
authored
moved ImportProject out of Settings and only store fileSettings (cppcheck-opensource#5603)
`ImportProject` is not needed outside of the command-line parsing so we do not need it inside the `Settings` at all. We only use the `fileSettings` in the executors.
1 parent 1065438 commit dd627a2

12 files changed

Lines changed: 54 additions & 48 deletions

cli/cmdlineparser.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
122122
bool def = false;
123123
bool maxconfigs = false;
124124

125+
ImportProject project;
126+
125127
mSettings.exename = Path::getCurrentExecutablePath(argv[0]);
126128

127129
for (int i = 1; i < argc; i++) {
@@ -666,24 +668,24 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
666668

667669
// --project
668670
else if (std::strncmp(argv[i], "--project=", 10) == 0) {
669-
if (mSettings.project.projectType != ImportProject::Type::NONE)
671+
if (project.projectType != ImportProject::Type::NONE)
670672
{
671673
mLogger.printError("multiple --project options are not supported.");
672674
return false;
673675
}
674676

675677
mSettings.checkAllConfigurations = false; // Can be overridden with --max-configs or --force
676678
std::string projectFile = argv[i]+10;
677-
ImportProject::Type projType = mSettings.project.import(projectFile, &mSettings);
678-
mSettings.project.projectType = projType;
679+
ImportProject::Type projType = project.import(projectFile, &mSettings);
680+
project.projectType = projType;
679681
if (projType == ImportProject::Type::CPPCHECK_GUI) {
680-
for (const std::string &lib : mSettings.project.guiProject.libraries)
682+
for (const std::string &lib : project.guiProject.libraries)
681683
mSettings.libraries.emplace_back(lib);
682684

683-
const auto& excludedPaths = mSettings.project.guiProject.excludedPaths;
685+
const auto& excludedPaths = project.guiProject.excludedPaths;
684686
std::copy(excludedPaths.cbegin(), excludedPaths.cend(), std::back_inserter(mIgnoredPaths));
685687

686-
std::string platform(mSettings.project.guiProject.platform);
688+
std::string platform(project.guiProject.platform);
687689

688690
// keep existing platform from command-line intact
689691
if (!platform.empty()) {
@@ -700,14 +702,16 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
700702
}
701703
}
702704

703-
if (!mSettings.project.guiProject.projectFile.empty()) {
704-
projectFile = mSettings.project.guiProject.projectFile;
705-
projType = mSettings.project.import(mSettings.project.guiProject.projectFile, &mSettings);
705+
const auto& projectFileGui = project.guiProject.projectFile;
706+
if (!projectFileGui.empty()) {
707+
// read underlying project
708+
projectFile = projectFileGui;
709+
projType = project.import(projectFileGui, &mSettings);
706710
}
707711
}
708712
if (projType == ImportProject::Type::VS_SLN || projType == ImportProject::Type::VS_VCXPROJ) {
709-
if (mSettings.project.guiProject.analyzeAllVsConfigs == "false")
710-
mSettings.project.selectOneVsConfig(mSettings.platform.type);
713+
if (project.guiProject.analyzeAllVsConfigs == "false")
714+
project.selectOneVsConfig(mSettings.platform.type);
711715
if (!CppCheckExecutor::tryLoadLibrary(mSettings.library, argv[0], "windows.cfg")) {
712716
// This shouldn't happen normally.
713717
mLogger.printError("failed to load 'windows.cfg'. Your Cppcheck installation is broken. Please re-install.");
@@ -731,8 +735,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
731735
// --project-configuration
732736
else if (std::strncmp(argv[i], "--project-configuration=", 24) == 0) {
733737
mVSConfig = argv[i] + 24;
734-
if (!mVSConfig.empty() && (mSettings.project.projectType == ImportProject::Type::VS_SLN || mSettings.project.projectType == ImportProject::Type::VS_VCXPROJ))
735-
mSettings.project.ignoreOtherConfigs(mVSConfig);
738+
if (!mVSConfig.empty() && (project.projectType == ImportProject::Type::VS_SLN || project.projectType == ImportProject::Type::VS_VCXPROJ))
739+
project.ignoreOtherConfigs(mVSConfig);
736740
}
737741

738742
// Only print something when there are errors
@@ -1028,7 +1032,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
10281032
substituteTemplateFormatStatic(mSettings.templateFormat);
10291033
substituteTemplateLocationStatic(mSettings.templateLocation);
10301034

1031-
mSettings.project.ignorePaths(mIgnoredPaths);
1035+
project.ignorePaths(mIgnoredPaths);
10321036

10331037
if (mSettings.force || maxconfigs)
10341038
mSettings.checkAllConfigurations = true;
@@ -1053,19 +1057,22 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
10531057
return true;
10541058
}
10551059

1056-
if (!mPathNames.empty() && mSettings.project.projectType != ImportProject::Type::NONE) {
1060+
if (!mPathNames.empty() && project.projectType != ImportProject::Type::NONE) {
10571061
mLogger.printError("--project cannot be used in conjunction with source files.");
10581062
return false;
10591063
}
10601064

10611065
// Print error only if we have "real" command and expect files
1062-
if (!mExitAfterPrint && mPathNames.empty() && mSettings.project.guiProject.pathNames.empty() && mSettings.project.fileSettings.empty()) {
1066+
if (!mExitAfterPrint && mPathNames.empty() && project.guiProject.pathNames.empty() && project.fileSettings.empty()) {
10631067
mLogger.printError("no C or C++ source files found.");
10641068
return false;
10651069
}
10661070

1067-
if (!mSettings.project.guiProject.pathNames.empty())
1068-
mPathNames = mSettings.project.guiProject.pathNames;
1071+
if (!project.guiProject.pathNames.empty())
1072+
mPathNames = project.guiProject.pathNames;
1073+
1074+
if (!project.fileSettings.empty())
1075+
mSettings.fileSettings = project.fileSettings;
10691076

10701077
// Use paths _pathnames if no base paths for relative path output are given
10711078
if (mSettings.basePaths.empty() && mSettings.relativePaths)

cli/cppcheckexecutor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,16 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
194194
#else
195195
const bool caseSensitive = true;
196196
#endif
197-
if (!settings.project.fileSettings.empty() && !settings.fileFilters.empty()) {
197+
if (!settings.fileSettings.empty() && !settings.fileFilters.empty()) {
198198
// filter only for the selected filenames from all project files
199199
std::list<ImportProject::FileSettings> newList;
200200

201-
const std::list<ImportProject::FileSettings>& fileSettings = settings.project.fileSettings;
201+
const std::list<ImportProject::FileSettings>& fileSettings = settings.fileSettings;
202202
std::copy_if(fileSettings.cbegin(), fileSettings.cend(), std::back_inserter(newList), [&](const ImportProject::FileSettings& fs) {
203203
return matchglobs(settings.fileFilters, fs.filename);
204204
});
205205
if (!newList.empty())
206-
settings.project.fileSettings = newList;
206+
settings.fileSettings = newList;
207207
else {
208208
logger.printError("could not find any files matching the filter.");
209209
return false;
@@ -220,13 +220,13 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
220220
}
221221
}
222222

223-
if (mFiles.empty() && settings.project.fileSettings.empty()) {
223+
if (mFiles.empty() && settings.fileSettings.empty()) {
224224
logger.printError("could not find or open any of the paths given.");
225225
if (!ignored.empty())
226226
logger.printMessage("Maybe all paths were ignored?");
227227
return false;
228228
}
229-
if (!settings.fileFilters.empty() && settings.project.fileSettings.empty()) {
229+
if (!settings.fileFilters.empty() && settings.fileSettings.empty()) {
230230
std::map<std::string, std::size_t> newMap;
231231
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i)
232232
if (matchglobs(settings.fileFilters, i->first)) {
@@ -319,7 +319,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
319319
std::list<std::string> fileNames;
320320
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i)
321321
fileNames.emplace_back(i->first);
322-
AnalyzerInformation::writeFilesTxt(settings.buildDir, fileNames, settings.userDefines, settings.project.fileSettings);
322+
AnalyzerInformation::writeFilesTxt(settings.buildDir, fileNames, settings.userDefines, settings.fileSettings);
323323
}
324324

325325
if (!settings.checkersReportFilename.empty())

cli/processexecutor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ unsigned int ProcessExecutor::check()
237237
std::map<int, std::string> pipeFile;
238238
std::size_t processedsize = 0;
239239
std::map<std::string, std::size_t>::const_iterator iFile = mFiles.cbegin();
240-
std::list<ImportProject::FileSettings>::const_iterator iFileSettings = mSettings.project.fileSettings.cbegin();
240+
std::list<ImportProject::FileSettings>::const_iterator iFileSettings = mSettings.fileSettings.cbegin();
241241
for (;;) {
242242
// Start a new child
243243
const size_t nchildren = childFile.size();
244-
if ((iFile != mFiles.cend() || iFileSettings != mSettings.project.fileSettings.cend()) && nchildren < mSettings.jobs && checkLoadAverage(nchildren)) {
244+
if ((iFile != mFiles.cend() || iFileSettings != mSettings.fileSettings.cend()) && nchildren < mSettings.jobs && checkLoadAverage(nchildren)) {
245245
int pipes[2];
246246
if (pipe(pipes) == -1) {
247247
std::cerr << "#### ThreadExecutor::check, pipe() failed: "<< std::strerror(errno) << std::endl;
@@ -275,7 +275,7 @@ unsigned int ProcessExecutor::check()
275275
fileChecker.settings() = mSettings;
276276
unsigned int resultOfCheck = 0;
277277

278-
if (iFileSettings != mSettings.project.fileSettings.end()) {
278+
if (iFileSettings != mSettings.fileSettings.end()) {
279279
resultOfCheck = fileChecker.check(*iFileSettings);
280280
if (fileChecker.settings().clangTidy)
281281
fileChecker.analyseClangTidy(*iFileSettings);
@@ -291,7 +291,7 @@ unsigned int ProcessExecutor::check()
291291

292292
close(pipes[1]);
293293
rpipes.push_back(pipes[0]);
294-
if (iFileSettings != mSettings.project.fileSettings.end()) {
294+
if (iFileSettings != mSettings.fileSettings.end()) {
295295
childFile[pid] = iFileSettings->filename + ' ' + iFileSettings->cfg;
296296
pipeFile[pipes[0]] = iFileSettings->filename + ' ' + iFileSettings->cfg;
297297
++iFileSettings;
@@ -334,7 +334,7 @@ unsigned int ProcessExecutor::check()
334334
fileCount++;
335335
processedsize += size;
336336
if (!mSettings.quiet)
337-
Executor::reportStatus(fileCount, mFiles.size() + mSettings.project.fileSettings.size(), processedsize, totalfilesize);
337+
Executor::reportStatus(fileCount, mFiles.size() + mSettings.fileSettings.size(), processedsize, totalfilesize);
338338

339339
close(*rp);
340340
rp = rpipes.erase(rp);
@@ -370,7 +370,7 @@ unsigned int ProcessExecutor::check()
370370
}
371371
}
372372
}
373-
if (iFile == mFiles.end() && iFileSettings == mSettings.project.fileSettings.end() && rpipes.empty() && childFile.empty()) {
373+
if (iFile == mFiles.end() && iFileSettings == mSettings.fileSettings.end() && rpipes.empty() && childFile.empty()) {
374374
// All done
375375
break;
376376
}

cli/singleexecutor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ unsigned int SingleExecutor::check()
5151
unsigned int c = 0;
5252
// TODO: processes either mSettings.project.fileSettings or mFiles - process/thread implementations process both
5353
// TODO: thread/process implementations process fileSettings first
54-
if (mSettings.project.fileSettings.empty()) {
54+
if (mSettings.fileSettings.empty()) {
5555
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i) {
5656
if (!mSettings.library.markupFile(i->first)
5757
|| !mSettings.library.processMarkupAfterCode(i->first)) {
@@ -66,13 +66,13 @@ unsigned int SingleExecutor::check()
6666
} else {
6767
// filesettings
6868
// check all files of the project
69-
for (const ImportProject::FileSettings &fs : mSettings.project.fileSettings) {
69+
for (const ImportProject::FileSettings &fs : mSettings.fileSettings) {
7070
if (!mSettings.library.markupFile(fs.filename)
7171
|| !mSettings.library.processMarkupAfterCode(fs.filename)) {
7272
result += mCppcheck.check(fs);
7373
++c;
7474
if (!mSettings.quiet)
75-
reportStatus(c, mSettings.project.fileSettings.size(), c, mSettings.project.fileSettings.size());
75+
reportStatus(c, mSettings.fileSettings.size(), c, mSettings.fileSettings.size());
7676
if (mSettings.clangTidy)
7777
mCppcheck.analyseClangTidy(fs);
7878
}
@@ -82,7 +82,7 @@ unsigned int SingleExecutor::check()
8282
// second loop to parse all markup files which may not work until all
8383
// c/cpp files have been parsed and checked
8484
// TODO: get rid of duplicated code
85-
if (mSettings.project.fileSettings.empty()) {
85+
if (mSettings.fileSettings.empty()) {
8686
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i) {
8787
if (mSettings.library.markupFile(i->first)
8888
&& mSettings.library.processMarkupAfterCode(i->first)) {
@@ -96,13 +96,13 @@ unsigned int SingleExecutor::check()
9696
}
9797
}
9898
else {
99-
for (const ImportProject::FileSettings &fs : mSettings.project.fileSettings) {
99+
for (const ImportProject::FileSettings &fs : mSettings.fileSettings) {
100100
if (mSettings.library.markupFile(fs.filename)
101101
&& mSettings.library.processMarkupAfterCode(fs.filename)) {
102102
result += mCppcheck.check(fs);
103103
++c;
104104
if (!mSettings.quiet)
105-
reportStatus(c, mSettings.project.fileSettings.size(), c, mSettings.project.fileSettings.size());
105+
reportStatus(c, mSettings.fileSettings.size(), c, mSettings.fileSettings.size());
106106
if (mSettings.clangTidy)
107107
mCppcheck.analyseClangTidy(fs);
108108
}

cli/threadexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ unsigned int ThreadExecutor::check()
180180
std::vector<std::future<unsigned int>> threadFutures;
181181
threadFutures.reserve(mSettings.jobs);
182182

183-
ThreadData data(*this, mErrorLogger, mSettings, mFiles, mSettings.project.fileSettings, mExecuteCommand);
183+
ThreadData data(*this, mErrorLogger, mSettings, mFiles, mSettings.fileSettings, mExecuteCommand);
184184

185185
for (unsigned int i = 0; i < mSettings.jobs; ++i) {
186186
try {

gui/mainwindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar
600600
std::transform(fileNames.cbegin(), fileNames.cend(), std::back_inserter(sourcefiles), [](const QString& s) {
601601
return s.toStdString();
602602
});
603-
AnalyzerInformation::writeFilesTxt(checkSettings.buildDir, sourcefiles, checkSettings.userDefines, checkSettings.project.fileSettings);
603+
AnalyzerInformation::writeFilesTxt(checkSettings.buildDir, sourcefiles, checkSettings.userDefines, checkSettings.fileSettings);
604604
}
605605

606606
mThread->setCheckFiles(true);

lib/cppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ void CppCheck::removeCtuInfoFiles(const std::map<std::string, std::size_t> &file
17971797
const std::string &ctuInfoFileName = getCtuInfoFileName(dumpFileName);
17981798
std::remove(ctuInfoFileName.c_str());
17991799
}
1800-
for (const auto& fs: mSettings.project.fileSettings) {
1800+
for (const auto& fs: mSettings.fileSettings) {
18011801
const std::string &dumpFileName = getDumpFileName(mSettings, fs.filename);
18021802
const std::string &ctuInfoFileName = getCtuInfoFileName(dumpFileName);
18031803
std::remove(ctuInfoFileName.c_str());

lib/settings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class CPPCHECKLIB WARN_UNUSED Settings {
205205
/** @brief List of --file-filter for analyzing special files */
206206
std::vector<std::string> fileFilters;
207207

208+
std::list<ImportProject::FileSettings> fileSettings;
209+
208210
/** @brief Force checking the files with "too many" configurations (--force). */
209211
bool force{};
210212

@@ -267,8 +269,6 @@ class CPPCHECKLIB WARN_UNUSED Settings {
267269
/** @brief Using -E for debugging purposes */
268270
bool preprocessOnly{};
269271

270-
ImportProject project;
271-
272272
/** @brief Is --quiet given? */
273273
bool quiet{};
274274

test/testcmdlineparser.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,6 @@ class TestCmdlineParser : public TestFixture {
20152015
"</project>");
20162016
const char * const argv[] = {"cppcheck", "--project=project.cppcheck"};
20172017
ASSERT(parser->parseFromArgs(2, argv));
2018-
ASSERT_EQUALS(static_cast<int>(ImportProject::Type::CPPCHECK_GUI), static_cast<int>(settings->project.projectType));
20192018
ASSERT_EQUALS(1, parser->getPathNames().size());
20202019
auto it = parser->getPathNames().cbegin();
20212020
ASSERT_EQUALS("dir", *it);

test/testprocessexecutor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class TestProcessExecutorBase : public TestFixture {
8282
if (useFS) {
8383
ImportProject::FileSettings fs;
8484
fs.filename = std::move(f_s);
85-
s.project.fileSettings.emplace_back(std::move(fs));
85+
s.fileSettings.emplace_back(std::move(fs));
8686
}
8787
}
8888
}
@@ -93,7 +93,7 @@ class TestProcessExecutorBase : public TestFixture {
9393
if (useFS) {
9494
ImportProject::FileSettings fs;
9595
fs.filename = f;
96-
s.project.fileSettings.emplace_back(std::move(fs));
96+
s.fileSettings.emplace_back(std::move(fs));
9797
}
9898
}
9999
}

0 commit comments

Comments
 (0)