Skip to content

CppCheck: avoid expensive std::ostringstream usage in checkFile() #5481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2023
Merged
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
48 changes: 33 additions & 15 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << " <rawtokens>" << std::endl;
for (unsigned int i = 0; i < files.size(); ++i)
dumpProlog << " <file index=\"" << i << "\" name=\"" << ErrorLogger::toxml(files[i]) << "\"/>" << std::endl;
dumpProlog += " <rawtokens>\n";
for (unsigned int i = 0; i < files.size(); ++i) {
dumpProlog += " <file index=\"";
dumpProlog += std::to_string(i);
dumpProlog += "\" name=\"";
dumpProlog += ErrorLogger::toxml(files[i]);
dumpProlog += "\"/>\n";
}
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 += " <tok ";

dumpProlog += "fileIndex=\"";
dumpProlog += std::to_string(tok->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 << " </rawtokens>" << std::endl;
dumpProlog += " </rawtokens>\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();
Expand Down Expand Up @@ -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);
}
Expand Down