diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 74e417bb5..1a4100c8c 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -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& 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 << " " << std::endl; - for (unsigned int i = 0; i < files.size(); ++i) - fdump << " " << std::endl; - for (const simplecpp::Token *tok = rawtokens; tok; tok = tok->next) { - fdump << " location.fileIndex << "\" " - << "linenr=\"" << tok->location.line << "\" " - << "column=\"" << tok->location.col << "\" " - << "str=\"" << ErrorLogger::toxml(tok->str()) << "\"" - << "/>" << std::endl; - } - fdump << " " << 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 << "" << 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 << " " << std::endl; + for (unsigned int i = 0; i < files.size(); ++i) + dumpProlog << " " << std::endl; + for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) { + dumpProlog + << " location.fileIndex << "\" " + << "linenr=\"" << tok->location.line << "\" " + << "column=\"" << tok->location.col << "\" " + << "str=\"" << ErrorLogger::toxml(tok->str()) << "\"" + << "/>" << std::endl; + } + dumpProlog << " " << 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); diff --git a/test/cli/test-helloworld.py b/test/cli/test-helloworld.py index 21310de9b..a5ccb8732 100644 --- a/test/cli/test-helloworld.py +++ b/test/cli/test-helloworld.py @@ -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 '' in dump, 'invalid dump data: ...' + dump[-100:] + + + +