Optimized Tokenizer::simplifyFunctionReturn(): Replaced nested loops by two consecutive loops; execute second one only if necessary.

This commit is contained in:
PKEuS 2018-05-14 20:51:09 +02:00
parent 20b2574da8
commit e8bc955023
1 changed files with 18 additions and 13 deletions

View File

@ -5540,26 +5540,31 @@ void Tokenizer::simplifyFunctionPointers()
bool Tokenizer::simplifyFunctionReturn() bool Tokenizer::simplifyFunctionReturn()
{ {
bool ret = false; std::map<std::string, const Token*> functions;
for (const Token *tok = tokens(); tok; tok = tok->next()) { for (const Token *tok = tokens(); tok; tok = tok->next()) {
if (tok->str() == "{") if (tok->str() == "{")
tok = tok->link(); tok = tok->link();
else if (Token::Match(tok, "%name% ( ) { return %bool%|%char%|%num%|%str% ; }") && tok->strAt(-1) != "::") { else if (Token::Match(tok, "%name% ( ) { return %bool%|%char%|%num%|%str% ; }") && tok->strAt(-1) != "::") {
const Token* const any = tok->tokAt(5); const Token* const any = tok->tokAt(5);
functions[tok->str()] = any;
tok = any;
}
}
const std::string pattern("(|[|=|return|%op% " + tok->str() + " ( ) ;|]|)|%cop%"); if (functions.empty())
for (Token *tok2 = list.front(); tok2; tok2 = tok2->next()) { return false;
if (Token::Match(tok2, pattern.c_str())) {
if (tok->str() != tok2->strAt(1)) bool ret = false;
// Ticket #7916: tok is for instance "foo < bar >", a single token for an instantiation, for (Token *tok = list.front(); tok; tok = tok->next()) {
// and tok2->strAt(1) is "foo"; bail out (TODO: we can probably handle this pattern) if (Token::Match(tok, "(|[|=|return|%op% %name% ( ) ;|]|)|%cop%")) {
continue; tok = tok->next();
tok2 = tok2->next(); auto it = functions.find(tok->str());
tok2->str(any->str()); if (it != functions.cend()) {
tok2->deleteNext(2); tok->str(it->second->str());
ret = true; tok->deleteNext(2);
} ret = true;
} }
} }
} }