Skip to content

Commit 47179ab

Browse files
committed
Fix #15293 (Slow cached run with --cppcheck-build-dir on projects with many files)
1 parent 6a4fedd commit 47179ab

9 files changed

Lines changed: 121 additions & 16 deletions

‎cli/cppcheckexecutor.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
277277
mFiles = parser.getFiles();
278278
mFileSettings = parser.getFileSettings();
279279

280+
if (!settings.buildDir.empty()) {
281+
std::list<std::string> fileNames;
282+
for (auto i = mFiles.cbegin(); i != mFiles.cend(); ++i)
283+
fileNames.emplace_back(i->path());
284+
settings.buildDirFilesTxt = AnalyzerInformation::getFilesTxtIndex(fileNames, mFileSettings);
285+
}
286+
280287
const int ret = check_wrapper(settings, supprs);
281288

282289
return ret;
@@ -382,7 +389,7 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con
382389
// TODO: what if sourcefile is empty?
383390

384391
for (const AnalyzerInfoId &id : ids) {
385-
AnalyzerInformation analyzerInfo;
392+
AnalyzerInformation analyzerInfo(settings.buildDirFilesTxt);
386393
// FIXME: this is a horrible hack
387394
// we need to "re-open" the file so we can add the unmatchedSuppression findings.
388395
// we cannot keep it open conditionally because the whole program analysis reads the XML.

‎gui/checkthread.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void CheckThread::runAddonsAndTools(const Settings& settings, const FileSettings
253253

254254
const std::string &buildDir = settings.buildDir;
255255
if (!buildDir.empty()) {
256-
analyzerInfoFile = QString::fromStdString(AnalyzerInformation::getAnalyzerInfoFile(buildDir, fileSettings->sfilename(), fileSettings->cfg, fileSettings->file.fsFileId()));
256+
analyzerInfoFile = QString::fromStdString(AnalyzerInformation::getAnalyzerInfoFile(settings.buildDirFilesTxt, buildDir, fileSettings->sfilename(), fileSettings->cfg, fileSettings->file.fsFileId()));
257257

258258
QStringList args2(args);
259259
args2.insert(0,"-E");

‎gui/mainwindow.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLib, const bo
653653
checkSettings.loadSummaries();
654654
std::list<std::string> sourcefiles;
655655
AnalyzerInformation::writeFilesTxt(checkSettings.buildDir, sourcefiles, p.fileSettings);
656+
checkSettings.buildDirFilesTxt = AnalyzerInformation::getFilesTxtIndex(sourcefiles, p.fileSettings);
656657
}
657658

658659
//mThread->SetanalyzeProject(true);
@@ -731,6 +732,7 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLib, c
731732
checkSettings.loadSummaries();
732733
const auto& sourcefiles = toStdStringList<std::list<std::string>>(fileNames);
733734
AnalyzerInformation::writeFilesTxt(checkSettings.buildDir, sourcefiles, {});
735+
checkSettings.buildDirFilesTxt = AnalyzerInformation::getFilesTxtIndex(sourcefiles, {});
734736
}
735737

736738
mThread->setCheckFiles(true);

‎lib/analyzerinfo.cpp‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@
3333

3434
#include "xml.h"
3535

36+
static AnalyzerInformation::FilesTxt readFilesTxt(std::istream& filesTxt)
37+
{
38+
AnalyzerInformation::FilesTxt index;
39+
std::string line;
40+
while (std::getline(filesTxt, line)) {
41+
AnalyzerInformation::Info filesTxtInfo;
42+
if (!filesTxtInfo.parse(line))
43+
continue; // TODO: report error?
44+
index.emplace(std::make_tuple(filesTxtInfo.sourceFile, filesTxtInfo.cfg, filesTxtInfo.fsFileId), filesTxtInfo.afile);
45+
}
46+
return index;
47+
}
48+
49+
static std::string findAfile(const AnalyzerInformation::FilesTxt& index, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId)
50+
{
51+
const auto it = index.find(std::make_tuple(Path::simplifyPath(sourcefile), cfg, fsFileId));
52+
return it == index.end() ? "" : it->second;
53+
}
54+
3655
// Not XMLDocument::LoadFile(): with MSVC it denies writing, and addons inherit the handle (#15207)
3756
static tinyxml2::XMLError loadFile(tinyxml2::XMLDocument &doc, const std::string &filename)
3857
{
@@ -69,6 +88,18 @@ void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std::
6988
fout << getFilesTxt(sourcefiles, fileSettings);
7089
}
7190

91+
AnalyzerInformation::FilesTxt AnalyzerInformation::getFilesTxtIndex(const std::list<std::string> &sourcefiles, const std::list<FileSettings> &fileSettings)
92+
{
93+
std::istringstream istr(getFilesTxt(sourcefiles, fileSettings));
94+
return readFilesTxt(istr);
95+
}
96+
97+
const AnalyzerInformation::FilesTxt &AnalyzerInformation::filesTxt() const
98+
{
99+
static const FilesTxt notRead;
100+
return mFilesTxt ? *mFilesTxt : notRead;
101+
}
102+
72103
void AnalyzerInformation::writeIncludes(const std::set<std::string> &files)
73104
{
74105
if (!files.empty() && mOutputStream.is_open()) {
@@ -90,7 +121,7 @@ std::set<std::string> AnalyzerInformation::getIncludes(const std::string &buildD
90121
if (buildDir.empty() || sourcefile.empty())
91122
return files;
92123

93-
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir, sourcefile, cfg, fsFileId);
124+
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(filesTxt(), buildDir, sourcefile, cfg, fsFileId);
94125

95126
tinyxml2::XMLDocument analyzerInfoDoc;
96127
if (loadFile(analyzerInfoDoc, analyzerInfoFile) != tinyxml2::XML_SUCCESS)
@@ -222,12 +253,21 @@ std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& f
222253

223254
std::string AnalyzerInformation::getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId)
224255
{
225-
std::ifstream fin(Path::join(buildDir, "files.txt"));
226-
if (fin.is_open()) {
227-
const std::string& ret = getAnalyzerInfoFileFromFilesTxt(fin, sourcefile, cfg, fsFileId);
228-
if (!ret.empty())
229-
return Path::join(buildDir, ret);
256+
return getAnalyzerInfoFile(FilesTxt(), buildDir, sourcefile, cfg, fsFileId);
257+
}
258+
259+
std::string AnalyzerInformation::getAnalyzerInfoFile(const FilesTxt &filesTxt, const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId)
260+
{
261+
std::string afile;
262+
if (filesTxt.empty()) {
263+
std::ifstream fin(Path::join(buildDir, "files.txt"));
264+
if (fin.is_open())
265+
afile = getAnalyzerInfoFileFromFilesTxt(fin, sourcefile, cfg, fsFileId);
266+
} else {
267+
afile = findAfile(filesTxt, sourcefile, cfg, fsFileId);
230268
}
269+
if (!afile.empty())
270+
return Path::join(buildDir, afile);
231271

232272
const std::string::size_type pos = sourcefile.rfind('/');
233273
std::string filename;
@@ -247,7 +287,7 @@ bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::st
247287
if (buildDir.empty() || sourcefile.empty())
248288
return true;
249289

250-
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir,sourcefile,cfg,fsFileId);
290+
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(filesTxt(), buildDir, sourcefile, cfg, fsFileId);
251291

252292
{
253293
tinyxml2::XMLDocument analyzerInfoDoc;
@@ -380,7 +420,7 @@ void AnalyzerInformation::reopen(const std::string &buildDir, const std::string
380420
if (buildDir.empty() || sourcefile.empty())
381421
return;
382422

383-
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir,sourcefile,cfg,fsFileId);
423+
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(filesTxt(), buildDir, sourcefile, cfg, fsFileId);
384424
std::ifstream ifs(analyzerInfoFile);
385425
if (!ifs.is_open())
386426
return;

‎lib/analyzerinfo.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
#include <fstream>
2828
#include <functional>
2929
#include <list>
30+
#include <map>
3031
#include <set>
3132
#include <string>
33+
#include <tuple>
3234

3335
class ErrorMessage;
3436
struct FileSettings;
@@ -56,9 +58,16 @@ namespace tinyxml2 {
5658
*/
5759
class CPPCHECKLIB AnalyzerInformation {
5860
public:
61+
/** afile of each source file, cfg and fsFileId of files.txt */
62+
using FilesTxt = std::map<std::tuple<std::string, std::string, std::size_t>, std::string>;
63+
64+
AnalyzerInformation() = default;
65+
/** find the analyzer information files in filesTxt instead of reading files.txt */
66+
explicit AnalyzerInformation(const FilesTxt &filesTxt) : mFilesTxt(&filesTxt) {}
5967
~AnalyzerInformation();
6068

6169
static void writeFilesTxt(const std::string &buildDir, const std::list<std::string> &sourcefiles, const std::list<FileSettings> &fileSettings);
70+
static FilesTxt getFilesTxtIndex(const std::list<std::string> &sourcefiles, const std::list<FileSettings> &fileSettings);
6271

6372
/** Close current TU.analyzerinfo file */
6473
void close();
@@ -72,6 +81,8 @@ class CPPCHECKLIB AnalyzerInformation {
7281
std::set<std::string> getIncludes(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId) const;
7382
void writeHash(std::size_t hash);
7483
static std::string getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId);
84+
/** files.txt is read when filesTxt is empty */
85+
static std::string getAnalyzerInfoFile(const FilesTxt &filesTxt, const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId);
7586

7687
void reopen(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId);
7788

@@ -96,6 +107,9 @@ class CPPCHECKLIB AnalyzerInformation {
96107
static std::string skipAnalysis(const tinyxml2::XMLDocument &analyzerInfoDoc, std::size_t hash, std::list<ErrorMessage> &errors);
97108

98109
private:
110+
const FilesTxt &filesTxt() const;
111+
112+
const FilesTxt *mFilesTxt = nullptr;
99113
std::ofstream mOutputStream;
100114
};
101115

‎lib/cppcheck.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ static std::string getDumpFileName(const Settings& settings, const FileWithDetai
366366
extension = "." + std::to_string(settings.pid) + extension;
367367

368368
if (!settings.dump && !settings.buildDir.empty())
369-
return AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, file.spath(), cfg, file.fsFileId()) + extension;
369+
return AnalyzerInformation::getAnalyzerInfoFile(settings.buildDirFilesTxt, settings.buildDir, file.spath(), cfg, file.fsFileId()) + extension;
370370
return file.spath() + extension;
371371
}
372372

@@ -710,7 +710,7 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
710710
// TODO: get language from FileWithDetails object
711711
std::string clangStderr;
712712
if (!mSettings.buildDir.empty())
713-
clangStderr = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, file.spath(), "", file.fsFileId()) + ".clang-stderr";
713+
clangStderr = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDirFilesTxt, mSettings.buildDir, file.spath(), "", file.fsFileId()) + ".clang-stderr";
714714

715715
std::string exe = mSettings.clangExecutable;
716716
#ifdef _WIN32
@@ -996,7 +996,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
996996
// TODO: if an exception occurs in this block it will continue in an unexpected code path
997997
if (!mSettings.buildDir.empty())
998998
{
999-
analyzerInformation.reset(new AnalyzerInformation);
999+
analyzerInformation.reset(new AnalyzerInformation(mSettings.buildDirFilesTxt));
10001000
mLogger->setAnalyzerInfo(analyzerInformation.get());
10011001
}
10021002

@@ -1092,7 +1092,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
10921092
preprocessor.setPlatformInfo();
10931093

10941094
if (!mSettings.buildDir.empty()) {
1095-
analyzerInformation.reset(new AnalyzerInformation);
1095+
analyzerInformation.reset(new AnalyzerInformation(mSettings.buildDirFilesTxt));
10961096
mLogger->setAnalyzerInfo(analyzerInformation.get());
10971097
}
10981098

@@ -1844,7 +1844,7 @@ void CppCheck::analyseClangTidy(const FileSettings &fileSettings)
18441844
std::string line;
18451845

18461846
if (!mSettings.buildDir.empty()) {
1847-
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, fileSettings.sfilename(), fileSettings.cfg, fileSettings.file.fsFileId());
1847+
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDirFilesTxt, mSettings.buildDir, fileSettings.sfilename(), fileSettings.cfg, fileSettings.file.fsFileId());
18481848
std::ofstream fcmd(analyzerInfoFile + ".clang-tidy-cmd");
18491849
fcmd << istr.str();
18501850
}

‎lib/settings.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <atomic>
3232
#include <cstdint>
3333
#include <list>
34+
#include <map>
3435
#include <set>
3536
#include <string>
3637
#include <tuple>
@@ -151,6 +152,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
151152
/** @brief --cppcheck-build-dir. Always uses / as path separator. No trailing path separator. */
152153
std::string buildDir;
153154

155+
/** AnalyzerInformation::FilesTxt of buildDir, files.txt is read when it is empty */
156+
std::map<std::tuple<std::string, std::string, std::size_t>, std::string> buildDirFilesTxt;
157+
154158
/** Is the 'configuration checking' wanted? */
155159
bool checkConfiguration{};
156160

‎lib/summaries.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ std::string Summaries::create(const Tokenizer &tokenizer, const std::string &cfg
9191
}
9292

9393
if (!settings.buildDir.empty()) {
94-
const std::string filename = getSummaryFile(AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, Path::simplifyPath(tokenizer.list.getSourceFilePath()), cfg, fsFileId));
94+
const std::string filename = getSummaryFile(AnalyzerInformation::getAnalyzerInfoFile(settings.buildDirFilesTxt, settings.buildDir, Path::simplifyPath(tokenizer.list.getSourceFilePath()), cfg, fsFileId));
9595
if (!filename.empty()) {
9696
std::ofstream fout(filename);
9797
fout << ostr.str();

‎test/testanalyzerinformation.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class TestAnalyzerInformation : public TestFixture {
4545
void run() override {
4646
TEST_CASE(getAnalyzerInfoFileFromFilesTxt);
4747
TEST_CASE(getAnalyzerInfoFile);
48+
TEST_CASE(writeFilesTxt);
49+
TEST_CASE(getFilesTxtIndex);
4850
TEST_CASE(duplicateFile);
4951
TEST_CASE(filesTextDuplicateFile);
5052
TEST_CASE(parse);
@@ -100,6 +102,42 @@ class TestAnalyzerInformation : public TestFixture {
100102
ASSERT_EQUALS("builddir/file1.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile("builddir", "some/path/file1.c", "", 0));
101103
}
102104

105+
static std::list<FileSettings> filesTxtFileSettings() {
106+
std::list<FileSettings> fileSettings;
107+
fileSettings.emplace_back("a.c", Standards::Language::C, 10);
108+
fileSettings.back().cfg = "Debug";
109+
fileSettings.emplace_back("a.c", Standards::Language::C, 10);
110+
fileSettings.back().file.setFsFileId(1);
111+
return fileSettings;
112+
}
113+
114+
void writeFilesTxt() const {
115+
const std::string buildDir = "testanalyzerinfo-filestxt";
116+
const ScopedFile filesTxt("files.txt", "", buildDir);
117+
118+
AnalyzerInformation::writeFilesTxt(buildDir, {"a.c", "src/a.c"}, filesTxtFileSettings());
119+
ASSERT_EQUALS(buildDir + "/a.a1", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "", 0));
120+
ASSERT_EQUALS(buildDir + "/a.a2", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "./src/a.c", "", 0));
121+
ASSERT_EQUALS(buildDir + "/a.a3", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "Debug", 0));
122+
ASSERT_EQUALS(buildDir + "/a.a4", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "", 1));
123+
ASSERT_EQUALS(buildDir + "/b.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "b.c", "", 0));
124+
125+
AnalyzerInformation::writeFilesTxt(buildDir, {"src/a.c", "a.c"}, {});
126+
ASSERT_EQUALS(buildDir + "/a.a2", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "", 0));
127+
ASSERT_EQUALS(buildDir + "/a.a1", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "src/a.c", "", 0));
128+
ASSERT_EQUALS(buildDir + "/a.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "Debug", 0));
129+
}
130+
131+
void getFilesTxtIndex() const {
132+
// there is no files.txt in builddir
133+
const AnalyzerInformation::FilesTxt filesTxt = AnalyzerInformation::getFilesTxtIndex({"a.c", "src/a.c"}, filesTxtFileSettings());
134+
ASSERT_EQUALS("builddir/a.a1", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "a.c", "", 0));
135+
ASSERT_EQUALS("builddir/a.a2", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "./src/a.c", "", 0));
136+
ASSERT_EQUALS("builddir/a.a3", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "a.c", "Debug", 0));
137+
ASSERT_EQUALS("builddir/a.a4", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "a.c", "", 1));
138+
ASSERT_EQUALS("builddir/b.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "b.c", "", 0));
139+
}
140+
103141
void filesTextDuplicateFile() const {
104142
std::list<FileSettings> fileSettings;
105143
fileSettings.emplace_back("a.c", Standards::Language::C, 10);

0 commit comments

Comments
 (0)