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.get());
// 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.get(), 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
64 changes: 51 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,12 @@ void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std::
fout << getFilesTxt(sourcefiles, fileSettings);
}

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

void AnalyzerInformation::writeIncludes(const std::set<std::string> &files)
{
if (!files.empty() && mOutputStream.is_open()) {
Expand All @@ -90,7 +122,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(mFilesTxt, buildDir, sourcefile, cfg, fsFileId);

tinyxml2::XMLDocument analyzerInfoDoc;
if (loadFile(analyzerInfoDoc, analyzerInfoFile) != tinyxml2::XML_SUCCESS)
Expand Down Expand Up @@ -209,11 +241,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 +251,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(nullptr, 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) {
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 +285,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(mFilesTxt, buildDir, sourcefile, cfg, fsFileId);

{
tinyxml2::XMLDocument analyzerInfoDoc;
Expand Down Expand Up @@ -380,7 +418,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(mFilesTxt, buildDir, sourcefile, cfg, fsFileId);
std::ifstream ifs(analyzerInfoFile);
if (!ifs.is_open())
return;
Expand Down
13 changes: 13 additions & 0 deletions lib/analyzerinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
#include <fstream>
#include <functional>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <tuple>

class ErrorMessage;
struct FileSettings;
Expand Down Expand Up @@ -56,9 +59,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 std::shared_ptr<const FilesTxt> getFilesTxtIndex(const std::list<std::string> &sourcefiles, const std::list<FileSettings> &fileSettings);

/** Close current TU.analyzerinfo file */
void close();
Expand All @@ -72,6 +82,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 null */
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 +108,7 @@ class CPPCHECKLIB AnalyzerInformation {
static std::string skipAnalysis(const tinyxml2::XMLDocument &analyzerInfoDoc, std::size_t hash, std::list<ErrorMessage> &errors);

private:
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.get(), 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.get(), 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.get()));
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.get()));
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.get(), mSettings.buildDir, fileSettings.sfilename(), fileSettings.cfg, fileSettings.file.fsFileId());
std::ofstream fcmd(analyzerInfoFile + ".clang-tidy-cmd");
fcmd << istr.str();
}
Expand Down
4 changes: 3 additions & 1 deletion lib/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ bool Settings::isEnabled(const ValueFlow::Value *value, bool inconclusiveCheck)

void Settings::loadSummaries()
{
Summaries::loadReturn(buildDir, summaryReturn);
std::set<std::string> functions;
Summaries::loadReturn(buildDir, functions);
summaryReturn = std::make_shared<const std::set<std::string>>(std::move(functions));
}

void Settings::setCheckLevel(CheckLevel level)
Expand Down
8 changes: 7 additions & 1 deletion lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <atomic>
#include <cstdint>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <tuple>
Expand Down Expand Up @@ -151,6 +153,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 null. Shared so that copying Settings does not copy it. */
std::shared_ptr<const std::map<std::tuple<std::string, std::string, std::size_t>, std::string>> buildDirFilesTxt;

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

Expand Down Expand Up @@ -590,7 +595,8 @@ class CPPCHECKLIB WARN_UNUSED Settings {
return Settings::mTerminated;
}

std::set<std::string> summaryReturn;
/** functions that return, from the summaries of buildDir, null until loadSummaries(). Shared so that copying Settings does not copy it. */
std::shared_ptr<const std::set<std::string>> summaryReturn;

void loadSummaries();

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.get(), settings.buildDir, Path::simplifyPath(tokenizer.list.getSourceFilePath()), cfg, fsFileId));
if (!filename.empty()) {
std::ofstream fout(filename);
fout << ostr.str();
Expand Down
2 changes: 1 addition & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8537,7 +8537,7 @@ bool Tokenizer::isScopeNoReturn(const Token *endScopeToken, bool *unknown) const
{
std::string unknownFunc;
const bool ret = mSettings.library.isScopeNoReturn(endScopeToken,&unknownFunc);
if (!unknownFunc.empty() && mSettings.summaryReturn.find(unknownFunc) != mSettings.summaryReturn.end()) {
if (!unknownFunc.empty() && mSettings.summaryReturn && mSettings.summaryReturn->count(unknownFunc) != 0) {
return false;
}
if (unknown)
Expand Down
23 changes: 17 additions & 6 deletions test/cli/other_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5196,7 +5196,7 @@ def test_redundant_file_reads(tmpdir, flags, expected):
assert stderr.splitlines()[-1].strip() == f'{expected} total'.encode('utf-8')


def __test_builddir_summary(tmp_path, filename, extra_args):
def __test_builddir_summary(tmp_path, filename, extra_args, use_compdb=False):
with open(tmp_path / filename, 'wt') as f:
f.write('void bar(void) {}\n')
with open(tmp_path / 'test.c', 'wt') as f:
Expand All @@ -5213,10 +5213,17 @@ def __test_builddir_summary(tmp_path, filename, extra_args):
args = [
'-q',
'--template=simple',
'--cppcheck-build-dir={}'.format(build_dir),
filename,
'test.c'
] + extra_args
'--cppcheck-build-dir={}'.format(build_dir)
]
if use_compdb:
compdb_file = tmp_path / 'compile_commands.json'
create_compile_commands(compdb_file, [tmp_path / filename, tmp_path / 'test.c'])
args.append('--project={}'.format(compdb_file))
test_file = tmp_path / 'test.c'
else:
args += [filename, 'test.c']
test_file = 'test.c'
args += extra_args

exitcode, stdout, stderr = cppcheck(args, cwd=tmp_path)
assert exitcode == 0, stdout
Expand All @@ -5238,14 +5245,18 @@ def __test_builddir_summary(tmp_path, filename, extra_args):
exitcode, stdout, stderr = cppcheck(args, cwd=tmp_path)
assert exitcode == 0, stdout
assert stderr.splitlines() == [
'test.c:5:1: error: Memory leak: p [memleak]'
'{}:5:1: error: Memory leak: p [memleak]'.format(test_file)
]


def test_builddir_summary(tmp_path):
__test_builddir_summary(tmp_path, 'bar.c', [])


def test_builddir_summary_compdb(tmp_path):
__test_builddir_summary(tmp_path, 'bar.c', [], use_compdb=True)


def test_builddir_summary_define(tmp_path):
__test_builddir_summary(tmp_path, 'bar.c', ['-DX'])

Expand Down
Loading
Loading