Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
mFiles = parser.getFiles();
mFileSettings = parser.getFileSettings();

if (!settings.buildDir.empty()) {
std::list<std::string> fileNames;
for (auto i = mFiles.cbegin(); i != mFiles.cend(); ++i)
fileNames.emplace_back(i->path());
settings.buildDirFilesTxt = AnalyzerInformation::getFilesTxtIndex(fileNames, mFileSettings);
}

const int ret = check_wrapper(settings, supprs);

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

for (const AnalyzerInfoId &id : ids) {
AnalyzerInformation analyzerInfo;
AnalyzerInformation analyzerInfo(settings.buildDirFilesTxt);
// FIXME: this is a horrible hack
// we need to "re-open" the file so we can add the unmatchedSuppression findings.
// we cannot keep it open conditionally because the whole program analysis reads the XML.
Expand Down
2 changes: 1 addition & 1 deletion gui/checkthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void CheckThread::runAddonsAndTools(const Settings& settings, const FileSettings

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

QStringList args2(args);
args2.insert(0,"-E");
Expand Down
2 changes: 2 additions & 0 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLib, const bo
checkSettings.loadSummaries();
std::list<std::string> sourcefiles;
AnalyzerInformation::writeFilesTxt(checkSettings.buildDir, sourcefiles, p.fileSettings);
checkSettings.buildDirFilesTxt = AnalyzerInformation::getFilesTxtIndex(sourcefiles, p.fileSettings);
}

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

mThread->setCheckFiles(true);
Expand Down
70 changes: 57 additions & 13 deletions lib/analyzerinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@

#include "xml.h"

static bool nextFilesTxtInfo(std::istream& filesTxt, AnalyzerInformation::Info& filesTxtInfo)
{
std::string line;
while (std::getline(filesTxt, line)) {
if (filesTxtInfo.parse(line))
return true;
// TODO: report error?
}
return false;
}

static AnalyzerInformation::FilesTxt readFilesTxt(std::istream& filesTxt)
{
AnalyzerInformation::FilesTxt index;
AnalyzerInformation::Info filesTxtInfo;
while (nextFilesTxtInfo(filesTxt, filesTxtInfo))
index.emplace(std::make_tuple(filesTxtInfo.sourceFile, filesTxtInfo.cfg, filesTxtInfo.fsFileId), filesTxtInfo.afile);
return index;
}

static std::string findAfile(const AnalyzerInformation::FilesTxt& index, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId)
{
const auto it = index.find(std::make_tuple(Path::simplifyPath(sourcefile), cfg, fsFileId));
return it == index.end() ? "" : it->second;
}

// Not XMLDocument::LoadFile(): with MSVC it denies writing, and addons inherit the handle (#15207)
static tinyxml2::XMLError loadFile(tinyxml2::XMLDocument &doc, const std::string &filename)
{
Expand Down Expand Up @@ -69,6 +95,18 @@ void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std::
fout << getFilesTxt(sourcefiles, fileSettings);
}

AnalyzerInformation::FilesTxt AnalyzerInformation::getFilesTxtIndex(const std::list<std::string> &sourcefiles, const std::list<FileSettings> &fileSettings)
{
std::istringstream istr(getFilesTxt(sourcefiles, fileSettings));
return readFilesTxt(istr);
}

const AnalyzerInformation::FilesTxt &AnalyzerInformation::filesTxt() const
{
static const FilesTxt notRead;
return mFilesTxt ? *mFilesTxt : notRead;
}

void AnalyzerInformation::writeIncludes(const std::set<std::string> &files)
{
if (!files.empty() && mOutputStream.is_open()) {
Expand All @@ -90,7 +128,7 @@ std::set<std::string> AnalyzerInformation::getIncludes(const std::string &buildD
if (buildDir.empty() || sourcefile.empty())
return files;

const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir, sourcefile, cfg, fsFileId);
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(filesTxt(), buildDir, sourcefile, cfg, fsFileId);

tinyxml2::XMLDocument analyzerInfoDoc;
if (loadFile(analyzerInfoDoc, analyzerInfoFile) != tinyxml2::XML_SUCCESS)
Expand Down Expand Up @@ -209,11 +247,8 @@ std::string AnalyzerInformation::skipAnalysis(const tinyxml2::XMLDocument &analy
std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg, size_t fsFileId)
{
const std::string simplifiedSourcefile = Path::simplifyPath(sourcefile);
std::string line;
while (std::getline(filesTxt,line)) {
AnalyzerInformation::Info filesTxtInfo;
if (!filesTxtInfo.parse(line))
continue; // TODO: report error?
AnalyzerInformation::Info filesTxtInfo;
while (nextFilesTxtInfo(filesTxt, filesTxtInfo)) {
if (filesTxtInfo.sourceFile == simplifiedSourcefile && filesTxtInfo.cfg == cfg && filesTxtInfo.fsFileId == fsFileId)
return filesTxtInfo.afile;
}
Expand All @@ -222,12 +257,21 @@ std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& f

std::string AnalyzerInformation::getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId)
{
std::ifstream fin(Path::join(buildDir, "files.txt"));
if (fin.is_open()) {
const std::string& ret = getAnalyzerInfoFileFromFilesTxt(fin, sourcefile, cfg, fsFileId);
if (!ret.empty())
return Path::join(buildDir, ret);
return getAnalyzerInfoFile(FilesTxt(), buildDir, sourcefile, cfg, fsFileId);
}

std::string AnalyzerInformation::getAnalyzerInfoFile(const FilesTxt &filesTxt, const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t fsFileId)
{
std::string afile;
if (filesTxt.empty()) {
std::ifstream fin(Path::join(buildDir, "files.txt"));
if (fin.is_open())
afile = getAnalyzerInfoFileFromFilesTxt(fin, sourcefile, cfg, fsFileId);
} else {
afile = findAfile(filesTxt, sourcefile, cfg, fsFileId);
}
if (!afile.empty())
return Path::join(buildDir, afile);

const std::string::size_type pos = sourcefile.rfind('/');
std::string filename;
Expand All @@ -247,7 +291,7 @@ bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::st
if (buildDir.empty() || sourcefile.empty())
return true;

const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir,sourcefile,cfg,fsFileId);
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(filesTxt(), buildDir, sourcefile, cfg, fsFileId);

{
tinyxml2::XMLDocument analyzerInfoDoc;
Expand Down Expand Up @@ -380,7 +424,7 @@ void AnalyzerInformation::reopen(const std::string &buildDir, const std::string
if (buildDir.empty() || sourcefile.empty())
return;

const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(buildDir,sourcefile,cfg,fsFileId);
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(filesTxt(), buildDir, sourcefile, cfg, fsFileId);
std::ifstream ifs(analyzerInfoFile);
if (!ifs.is_open())
return;
Expand Down
14 changes: 14 additions & 0 deletions lib/analyzerinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
#include <fstream>
#include <functional>
#include <list>
#include <map>
#include <set>
#include <string>
#include <tuple>

class ErrorMessage;
struct FileSettings;
Expand Down Expand Up @@ -56,9 +58,16 @@ namespace tinyxml2 {
*/
class CPPCHECKLIB AnalyzerInformation {
public:
/** afile of each source file, cfg and fsFileId of files.txt */
using FilesTxt = std::map<std::tuple<std::string, std::string, std::size_t>, std::string>;

AnalyzerInformation() = default;
/** find the analyzer information files in filesTxt instead of reading files.txt */
explicit AnalyzerInformation(const FilesTxt &filesTxt) : mFilesTxt(&filesTxt) {}
~AnalyzerInformation();

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

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

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

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

private:
const FilesTxt &filesTxt() const;

const FilesTxt *mFilesTxt = nullptr;
std::ofstream mOutputStream;
};

Expand Down
10 changes: 5 additions & 5 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ static std::string getDumpFileName(const Settings& settings, const FileWithDetai
extension = "." + std::to_string(settings.pid) + extension;

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

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

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

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

if (!mSettings.buildDir.empty()) {
analyzerInformation.reset(new AnalyzerInformation);
analyzerInformation.reset(new AnalyzerInformation(mSettings.buildDirFilesTxt));
mLogger->setAnalyzerInfo(analyzerInformation.get());
}

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

if (!mSettings.buildDir.empty()) {
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, fileSettings.sfilename(), fileSettings.cfg, fileSettings.file.fsFileId());
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDirFilesTxt, mSettings.buildDir, fileSettings.sfilename(), fileSettings.cfg, fileSettings.file.fsFileId());
std::ofstream fcmd(analyzerInfoFile + ".clang-tidy-cmd");
fcmd << istr.str();
}
Expand Down
4 changes: 4 additions & 0 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <atomic>
#include <cstdint>
#include <list>
#include <map>
#include <set>
#include <string>
#include <tuple>
Expand Down Expand Up @@ -151,6 +152,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
/** @brief --cppcheck-build-dir. Always uses / as path separator. No trailing path separator. */
std::string buildDir;

/** AnalyzerInformation::FilesTxt of buildDir, files.txt is read when it is empty */
std::map<std::tuple<std::string, std::string, std::size_t>, std::string> buildDirFilesTxt;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This map is stored by value in Settings, and CppCheck::check(const FileSettings&) copies the whole Settings for every file (Settings tempSettings = mSettings;, lib/cppcheck.cpp:851). So with --project, every file copies all N entries and the cached run is still quadratic. The cost of reading files.txt has only moved into the copy.

Measured on a compile_commands.json with 10000 trivial files (cached run):

  • PR: 25.9s
  • base: 24.0s
  • 2500 / 5000 files: 3.84s / 8.85s with the PR, 3.56s / 8.31s on base

gdb samples land in the _Rb_tree copy of this map inside Settings::Settings. Only the plain-files case benefits (1.5s vs 9.5s). Holding the index as std::shared_ptr<const AnalyzerInformation::FilesTxt> (or similar) would make the copy cheap.


/** Is the 'configuration checking' wanted? */
bool checkConfiguration{};

Expand Down
2 changes: 1 addition & 1 deletion lib/summaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ std::string Summaries::create(const Tokenizer &tokenizer, const std::string &cfg
}

if (!settings.buildDir.empty()) {
const std::string filename = getSummaryFile(AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, Path::simplifyPath(tokenizer.list.getSourceFilePath()), cfg, fsFileId));
const std::string filename = getSummaryFile(AnalyzerInformation::getAnalyzerInfoFile(settings.buildDirFilesTxt, settings.buildDir, Path::simplifyPath(tokenizer.list.getSourceFilePath()), cfg, fsFileId));
if (!filename.empty()) {
std::ofstream fout(filename);
fout << ostr.str();
Expand Down
38 changes: 38 additions & 0 deletions test/testanalyzerinformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class TestAnalyzerInformation : public TestFixture {
void run() override {
TEST_CASE(getAnalyzerInfoFileFromFilesTxt);
TEST_CASE(getAnalyzerInfoFile);
TEST_CASE(writeFilesTxt);
TEST_CASE(getFilesTxtIndex);
TEST_CASE(duplicateFile);
TEST_CASE(filesTextDuplicateFile);
TEST_CASE(parse);
Expand Down Expand Up @@ -100,6 +102,42 @@ class TestAnalyzerInformation : public TestFixture {
ASSERT_EQUALS("builddir/file1.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile("builddir", "some/path/file1.c", "", 0));
}

static std::list<FileSettings> filesTxtFileSettings() {
std::list<FileSettings> fileSettings;
fileSettings.emplace_back("a.c", Standards::Language::C, 10);
fileSettings.back().cfg = "Debug";
fileSettings.emplace_back("a.c", Standards::Language::C, 10);
fileSettings.back().file.setFsFileId(1);
return fileSettings;
}

void writeFilesTxt() const {
const std::string buildDir = "testanalyzerinfo-filestxt";
const ScopedFile filesTxt("files.txt", "", buildDir);

AnalyzerInformation::writeFilesTxt(buildDir, {"a.c", "src/a.c"}, filesTxtFileSettings());
ASSERT_EQUALS(buildDir + "/a.a1", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "", 0));
ASSERT_EQUALS(buildDir + "/a.a2", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "./src/a.c", "", 0));
ASSERT_EQUALS(buildDir + "/a.a3", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "Debug", 0));
ASSERT_EQUALS(buildDir + "/a.a4", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "", 1));
ASSERT_EQUALS(buildDir + "/b.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "b.c", "", 0));

AnalyzerInformation::writeFilesTxt(buildDir, {"src/a.c", "a.c"}, {});
ASSERT_EQUALS(buildDir + "/a.a2", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "", 0));
ASSERT_EQUALS(buildDir + "/a.a1", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "src/a.c", "", 0));
ASSERT_EQUALS(buildDir + "/a.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile(buildDir, "a.c", "Debug", 0));
}

void getFilesTxtIndex() const {
// there is no files.txt in builddir
const AnalyzerInformation::FilesTxt filesTxt = AnalyzerInformation::getFilesTxtIndex({"a.c", "src/a.c"}, filesTxtFileSettings());
ASSERT_EQUALS("builddir/a.a1", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "a.c", "", 0));
ASSERT_EQUALS("builddir/a.a2", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "./src/a.c", "", 0));
ASSERT_EQUALS("builddir/a.a3", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "a.c", "Debug", 0));
ASSERT_EQUALS("builddir/a.a4", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "a.c", "", 1));
ASSERT_EQUALS("builddir/b.c.analyzerinfo", AnalyzerInformation::getAnalyzerInfoFile(filesTxt, "builddir", "b.c", "", 0));
}

void filesTextDuplicateFile() const {
std::list<FileSettings> fileSettings;
fileSettings.emplace_back("a.c", Standards::Language::C, 10);
Expand Down
Loading