Skip to content

Commit e904ba1

Browse files
committed
avoid expensive std::ostream usage in Tokenizer::dump() / added TODOs
1 parent e76d400 commit e904ba1

File tree

1 file changed

+160
-70
lines changed

1 file changed

+160
-70
lines changed

lib/tokenize.cpp

Lines changed: 160 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5741,128 +5741,218 @@ void Tokenizer::dump(std::ostream &out) const
57415741
// The idea is not that this will be readable for humans. It's a
57425742
// data dump that 3rd party tools could load and get useful info from.
57435743

5744+
std::string outs;
5745+
57445746
std::set<const Library::Container*> containers;
57455747

57465748
// tokens..
5747-
out << " <tokenlist>" << std::endl;
5749+
outs += " <tokenlist>";
5750+
outs += '\n';
57485751
for (const Token *tok = list.front(); tok; tok = tok->next()) {
5749-
out << " <token id=\"" << tok << "\" file=\"" << ErrorLogger::toxml(list.file(tok)) << "\" linenr=\"" << tok->linenr() << "\" column=\"" << tok->column() << "\"";
5750-
out << " str=\"" << ErrorLogger::toxml(tok->str()) << '\"';
5751-
out << " scope=\"" << tok->scope() << '\"';
5752+
outs += " <token id=\"";
5753+
outs += ptr_to_string(tok);
5754+
outs += "\" file=\"";
5755+
outs += ErrorLogger::toxml(list.file(tok)); // TODO: hot spot
5756+
outs += "\" linenr=\"";
5757+
outs += std::to_string(tok->linenr());
5758+
outs += "\" column=\"";
5759+
outs += std::to_string(tok->column());
5760+
outs += "\"";
5761+
5762+
outs += " str=\"";
5763+
outs += ErrorLogger::toxml(tok->str()); // TODO: hot spot
5764+
outs += '\"';
5765+
5766+
outs += " scope=\"";
5767+
outs += ptr_to_string(tok->scope());
5768+
outs += '\"';
57525769
if (tok->isName()) {
5753-
out << " type=\"name\"";
5770+
outs += " type=\"name\"";
57545771
if (tok->isUnsigned())
5755-
out << " isUnsigned=\"true\"";
5772+
outs += " isUnsigned=\"true\"";
57565773
else if (tok->isSigned())
5757-
out << " isSigned=\"true\"";
5774+
outs += " isSigned=\"true\"";
57585775
} else if (tok->isNumber()) {
5759-
out << " type=\"number\"";
5776+
outs += " type=\"number\"";
57605777
if (MathLib::isInt(tok->str()))
5761-
out << " isInt=\"true\"";
5778+
outs += " isInt=\"true\"";
57625779
if (MathLib::isFloat(tok->str()))
5763-
out << " isFloat=\"true\"";
5764-
} else if (tok->tokType() == Token::eString)
5765-
out << " type=\"string\" strlen=\"" << Token::getStrLength(tok) << '\"';
5780+
outs += " isFloat=\"true\"";
5781+
} else if (tok->tokType() == Token::eString) {
5782+
outs += " type=\"string\" strlen=\"";
5783+
outs += std::to_string(Token::getStrLength(tok));
5784+
outs += '\"';
5785+
}
57665786
else if (tok->tokType() == Token::eChar)
5767-
out << " type=\"char\"";
5787+
outs += " type=\"char\"";
57685788
else if (tok->isBoolean())
5769-
out << " type=\"boolean\"";
5789+
outs += " type=\"boolean\"";
57705790
else if (tok->isOp()) {
5771-
out << " type=\"op\"";
5791+
outs += " type=\"op\"";
57725792
if (tok->isArithmeticalOp())
5773-
out << " isArithmeticalOp=\"true\"";
5793+
outs += " isArithmeticalOp=\"true\"";
57745794
else if (tok->isAssignmentOp())
5775-
out << " isAssignmentOp=\"true\"";
5795+
outs += " isAssignmentOp=\"true\"";
57765796
else if (tok->isComparisonOp())
5777-
out << " isComparisonOp=\"true\"";
5797+
outs += " isComparisonOp=\"true\"";
57785798
else if (tok->tokType() == Token::eLogicalOp)
5779-
out << " isLogicalOp=\"true\"";
5799+
outs += " isLogicalOp=\"true\"";
57805800
}
57815801
if (tok->isCast())
5782-
out << " isCast=\"true\"";
5802+
outs += " isCast=\"true\"";
57835803
if (tok->isExternC())
5784-
out << " externLang=\"C\"";
5804+
outs += " externLang=\"C\"";
57855805
if (tok->isExpandedMacro())
5786-
out << " isExpandedMacro=\"true\"";
5806+
outs += " isExpandedMacro=\"true\"";
57875807
if (tok->isRemovedVoidParameter())
5788-
out << " isRemovedVoidParameter=\"true\"";
5808+
outs += " isRemovedVoidParameter=\"true\"";
57895809
if (tok->isSplittedVarDeclComma())
5790-
out << " isSplittedVarDeclComma=\"true\"";
5810+
outs += " isSplittedVarDeclComma=\"true\"";
57915811
if (tok->isSplittedVarDeclEq())
5792-
out << " isSplittedVarDeclEq=\"true\"";
5812+
outs += " isSplittedVarDeclEq=\"true\"";
57935813
if (tok->isImplicitInt())
5794-
out << " isImplicitInt=\"true\"";
5814+
outs += " isImplicitInt=\"true\"";
57955815
if (tok->isComplex())
5796-
out << " isComplex=\"true\"";
5816+
outs += " isComplex=\"true\"";
57975817
if (tok->isRestrict())
5798-
out << " isRestrict=\"true\"";
5799-
if (tok->link())
5800-
out << " link=\"" << tok->link() << '\"';
5801-
if (tok->varId() > 0)
5802-
out << " varId=\"" << MathLib::toString(tok->varId()) << '\"';
5803-
if (tok->exprId() > 0)
5804-
out << " exprId=\"" << MathLib::toString(tok->exprId()) << '\"';
5805-
if (tok->variable())
5806-
out << " variable=\"" << tok->variable() << '\"';
5807-
if (tok->function())
5808-
out << " function=\"" << tok->function() << '\"';
5809-
if (!tok->values().empty())
5810-
out << " values=\"" << &tok->values() << '\"';
5811-
if (tok->type())
5812-
out << " type-scope=\"" << tok->type()->classScope << '\"';
5813-
if (tok->astParent())
5814-
out << " astParent=\"" << tok->astParent() << '\"';
5815-
if (tok->astOperand1())
5816-
out << " astOperand1=\"" << tok->astOperand1() << '\"';
5817-
if (tok->astOperand2())
5818-
out << " astOperand2=\"" << tok->astOperand2() << '\"';
5819-
if (!tok->originalName().empty())
5820-
out << " originalName=\"" << tok->originalName() << '\"';
5818+
outs += " isRestrict=\"true\"";
5819+
if (tok->link()) {
5820+
outs += " link=\"";
5821+
outs += ptr_to_string(tok->link());
5822+
outs += '\"';
5823+
}
5824+
if (tok->varId() > 0) {
5825+
outs += " varId=\"";
5826+
outs += MathLib::toString(tok->varId());
5827+
outs += '\"';
5828+
}
5829+
if (tok->exprId() > 0) {
5830+
outs += " exprId=\"";
5831+
outs += MathLib::toString(tok->exprId());
5832+
outs += '\"';
5833+
}
5834+
if (tok->variable()) {
5835+
outs += " variable=\"";
5836+
outs += ptr_to_string(tok->variable());
5837+
outs += '\"';
5838+
}
5839+
if (tok->function()) {
5840+
outs += " function=\"";
5841+
outs += ptr_to_string(tok->function());
5842+
outs += '\"';
5843+
}
5844+
if (!tok->values().empty()) {
5845+
outs += " values=\"";
5846+
outs += ptr_to_string(&tok->values());
5847+
outs += '\"';
5848+
}
5849+
if (tok->type()) {
5850+
outs += " type-scope=\"";
5851+
outs += ptr_to_string(tok->type()->classScope);
5852+
outs += '\"';
5853+
}
5854+
if (tok->astParent()) {
5855+
outs += " astParent=\"";
5856+
outs += ptr_to_string(tok->astParent());
5857+
outs += '\"';
5858+
}
5859+
if (tok->astOperand1()) {
5860+
outs += " astOperand1=\"";
5861+
outs += ptr_to_string(tok->astOperand1());
5862+
outs += '\"';
5863+
}
5864+
if (tok->astOperand2()) {
5865+
outs += " astOperand2=\"";
5866+
outs += ptr_to_string(tok->astOperand2());
5867+
outs += '\"';
5868+
}
5869+
if (!tok->originalName().empty()) {
5870+
outs += " originalName=\"";
5871+
outs += tok->originalName();
5872+
outs += '\"';
5873+
}
58215874
if (tok->valueType()) {
58225875
const std::string vt = tok->valueType()->dump();
5823-
if (!vt.empty())
5824-
out << ' ' << vt;
5876+
if (!vt.empty()) {
5877+
outs += ' ';
5878+
outs += vt;
5879+
}
58255880
containers.insert(tok->valueType()->container);
58265881
}
58275882
if (!tok->varId() && tok->scope()->isExecutable() && Token::Match(tok, "%name% (")) {
58285883
if (mSettings->library.isnoreturn(tok))
5829-
out << " noreturn=\"true\"";
5884+
outs += " noreturn=\"true\"";
58305885
}
58315886

5832-
out << "/>" << std::endl;
5887+
outs += "/>";
5888+
outs += '\n';
58335889
}
5834-
out << " </tokenlist>" << std::endl;
5890+
outs += " </tokenlist>";
5891+
outs += '\n';
5892+
5893+
out << outs;
5894+
outs.clear();
58355895

5896+
// TODO: hot spot
58365897
mSymbolDatabase->printXml(out);
58375898

58385899
containers.erase(nullptr);
58395900
if (!containers.empty()) {
5840-
out << " <containers>\n";
5901+
outs += " <containers>";
5902+
outs += '\n';
58415903
for (const Library::Container* c: containers) {
5842-
out << " <container id=\"" << c << "\" array-like-index-op=\""
5843-
<< (c->arrayLike_indexOp ? "true" : "false") << "\" "
5844-
<< "std-string-like=\"" << (c->stdStringLike ? "true" : "false") << "\"/>\n";
5904+
outs += " <container id=\"";
5905+
outs += ptr_to_string(c);
5906+
outs += "\" array-like-index-op=\"";
5907+
outs += (c->arrayLike_indexOp ? "true" : "false");
5908+
outs += "\" ";
5909+
outs += "std-string-like=\"";
5910+
outs +=(c->stdStringLike ? "true" : "false");
5911+
outs += "\"/>";
5912+
outs += '\n';
58455913
}
5846-
out << " </containers>\n";
5914+
outs += " </containers>";
5915+
outs += '\n';
58475916
}
58485917

58495918
if (list.front())
58505919
list.front()->printValueFlow(true, out);
58515920

58525921
if (!mTypedefInfo.empty()) {
5853-
out << " <typedef-info>" << std::endl;
5922+
outs += " <typedef-info>";
5923+
outs += '\n';
58545924
for (const TypedefInfo &typedefInfo: mTypedefInfo) {
5855-
out << " <info"
5856-
<< " name=\"" << typedefInfo.name << "\""
5857-
<< " file=\"" << typedefInfo.filename << "\""
5858-
<< " line=\"" << typedefInfo.lineNumber << "\""
5859-
<< " column=\"" << typedefInfo.column << "\""
5860-
<< " used=\"" << (typedefInfo.used?1:0) << "\""
5861-
<< "/>" << std::endl;
5925+
outs += " <info";
5926+
5927+
outs += " name=\"";
5928+
outs += typedefInfo.name;
5929+
outs += "\"";
5930+
5931+
outs += " file=\"";
5932+
outs += typedefInfo.filename;
5933+
outs += "\"";
5934+
5935+
outs += " line=\"";
5936+
outs += std::to_string(typedefInfo.lineNumber);
5937+
outs += "\"";
5938+
5939+
outs += " column=\"";
5940+
outs += std::to_string(typedefInfo.column);
5941+
outs += "\"";
5942+
5943+
outs += " used=\"";
5944+
outs += std::to_string(typedefInfo.used?1:0);
5945+
outs += "\"";
5946+
5947+
outs += "/>";
5948+
outs += '\n';
58625949
}
5863-
out << " </typedef-info>" << std::endl;
5950+
outs += " </typedef-info>";
5951+
outs += '\n';
58645952
}
5865-
out << mTemplateSimplifier->dump();
5953+
outs += mTemplateSimplifier->dump();
5954+
5955+
out << outs;
58665956
}
58675957

58685958
void Tokenizer::simplifyHeadersAndUnusedTemplates()

0 commit comments

Comments
 (0)