From a9952d9da62b1c105a950364fb19ab5b188b244d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Tue, 26 Sep 2023 14:22:52 +0200 Subject: [PATCH] CppCheck: avoid expensive `std::ostringstream` usage in `checkFile()` (#5481) Scanning `common` from `xrdp` project with `DISABLE_VALUEFLOW=1` and `-D__GNUC__ --addon=misra`: Clang 16 `627,748,337` -> `541,852,912` --- lib/cppcheck.cpp | 50 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index da85ebd68..7d198f582 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -787,27 +787,46 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string mPlistFile << ErrorLogger::plistHeader(version(), files); } - std::ostringstream dumpProlog; + std::string 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 += " \n"; + for (unsigned int i = 0; i < files.size(); ++i) { + dumpProlog += " \n"; } - dumpProlog << " " << std::endl; + for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) { + dumpProlog += " location.fileIndex); + dumpProlog += "\" "; + + dumpProlog += "linenr=\""; + dumpProlog += std::to_string(tok->location.line); + dumpProlog += "\" "; + + dumpProlog +="column=\""; + dumpProlog += std::to_string(tok->location.col); + dumpProlog += "\" "; + + dumpProlog += "str=\""; + dumpProlog += ErrorLogger::toxml(tok->str()); + dumpProlog += "\""; + + dumpProlog += "/>\n"; + } + dumpProlog += " \n"; } // Parse comments and then remove them preprocessor.inlineSuppressions(tokens1, mSettings.nomsg); if (mSettings.dump || !mSettings.addons.empty()) { - mSettings.nomsg.dump(dumpProlog); + std::ostringstream oss; + mSettings.nomsg.dump(oss); + dumpProlog += oss.str(); } tokens1.removeComments(); preprocessor.removeComments(); @@ -841,8 +860,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string std::string dumpFile; createDumpFile(mSettings, filename, fdump, dumpFile); if (fdump.is_open()) { - fdump << dumpProlog.str(); - dumpProlog.str(""); + fdump << dumpProlog; if (!mSettings.dump) filesDeleter.addFile(dumpFile); }