cppcheck build dir: do not produce invalid dump file if code is unchanged and checking is skipped.

This commit is contained in:
Daniel Marjamäki 2022-06-12 11:06:15 +02:00
parent d4fb5652c0
commit 8255e32540
2 changed files with 42 additions and 23 deletions

View File

@ -243,8 +243,6 @@ static std::string getCtuInfoFileName(const std::string &dumpFile)
static void createDumpFile(const Settings& settings,
const std::string& filename,
const std::vector<std::string>& files,
const simplecpp::Token* rawtokens,
std::ofstream& fdump,
std::string& dumpFile)
{
@ -271,20 +269,6 @@ static void createDumpFile(const Settings& settings,
<< " long_long_bit=\"" << settings.long_long_bit << '\"'
<< " pointer_bit=\"" << (settings.sizeof_pointer * settings.char_bit) << '\"'
<< "/>\n";
if (rawtokens) {
fdump << " <rawtokens>" << std::endl;
for (unsigned int i = 0; i < files.size(); ++i)
fdump << " <file index=\"" << i << "\" name=\"" << ErrorLogger::toxml(files[i]) << "\"/>" << std::endl;
for (const simplecpp::Token *tok = rawtokens; tok; tok = tok->next) {
fdump << " <tok "
<< "fileIndex=\"" << tok->location.fileIndex << "\" "
<< "linenr=\"" << tok->location.line << "\" "
<< "column=\"" << tok->location.col << "\" "
<< "str=\"" << ErrorLogger::toxml(tok->str()) << "\""
<< "/>" << std::endl;
}
fdump << " </rawtokens>" << std::endl;
}
}
static std::string executeAddon(const AddonInfo &addonInfo,
@ -520,7 +504,7 @@ unsigned int CppCheck::check(const std::string &path)
// create dumpfile
std::ofstream fdump;
std::string dumpFile;
createDumpFile(mSettings, path, tokenizer.list.getFiles(), nullptr, fdump, dumpFile);
createDumpFile(mSettings, path, fdump, dumpFile);
if (fdump.is_open()) {
fdump << "<dump cfg=\"\">" << std::endl;
for (const ErrorMessage& errmsg: compilerWarnings)
@ -686,15 +670,27 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
plistFile << ErrorLogger::plistHeader(version(), files);
}
// write dump file xml prolog
std::ofstream fdump;
std::string dumpFile;
createDumpFile(mSettings, filename, files, tokens1.cfront(), fdump, dumpFile);
std::ostringstream dumpProlog;
if (mSettings.dump || !mSettings.addons.empty()) {
dumpProlog << " <rawtokens>" << std::endl;
for (unsigned int i = 0; i < files.size(); ++i)
dumpProlog << " <file index=\"" << i << "\" name=\"" << ErrorLogger::toxml(files[i]) << "\"/>" << std::endl;
for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) {
dumpProlog
<< " <tok "
<< "fileIndex=\"" << tok->location.fileIndex << "\" "
<< "linenr=\"" << tok->location.line << "\" "
<< "column=\"" << tok->location.col << "\" "
<< "str=\"" << ErrorLogger::toxml(tok->str()) << "\""
<< "/>" << std::endl;
}
dumpProlog << " </rawtokens>" << std::endl;
}
// Parse comments and then remove them
preprocessor.inlineSuppressions(tokens1);
if ((mSettings.dump || !mSettings.addons.empty()) && fdump.is_open()) {
mSettings.nomsg.dump(fdump);
if (mSettings.dump || !mSettings.addons.empty()) {
mSettings.nomsg.dump(dumpProlog);
}
tokens1.removeComments();
preprocessor.removeComments();
@ -723,6 +719,15 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
}
}
// write dump file xml prolog
std::ofstream fdump;
std::string dumpFile;
createDumpFile(mSettings, filename, fdump, dumpFile);
if (fdump.is_open()) {
fdump << dumpProlog.str();
dumpProlog.str("");
}
// Get directives
preprocessor.setDirectives(tokens1);
preprocessor.simplifyPragmaAsm(&tokens1);

View File

@ -3,6 +3,7 @@
import os
import re
import tempfile
from testutils import create_gui_project_file, cppcheck
# Run Cppcheck from project path
@ -178,3 +179,16 @@ def test_exclude():
assert stdout == 'cppcheck: error: no C or C++ source files found.\n'
def test_build_dir_dump_output():
with tempfile.TemporaryDirectory() as tempdir:
args = f'--cppcheck-build-dir={tempdir} --addon=misra helloworld'
cppcheck(args.split())
cppcheck(args.split())
with open(f'{tempdir}/main.a1.dump', 'rt') as f:
dump = f.read()
assert '</dump>' in dump, 'invalid dump data: ...' + dump[-100:]