Skip to content

Commit 9352e12

Browse files
committed
Auto variables: Speedup my fix. Use the symbol database instead of Token::findmatch.
1 parent 4312d31 commit 9352e12

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

‎lib/checkautovariables.cpp‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,31 @@ bool CheckAutoVariables::returnTemporary(const Token *tok) const
231231
{
232232
if (!Token::Match(tok, "return %var% ("))
233233
return false;
234-
// TODO: Find all functions that return objects by value
235-
return bool(NULL != Token::findmatch(_tokenizer->tokens(), ("std :: string " + tok->next()->str() + " (").c_str()) &&
236-
NULL == Token::findmatch(_tokenizer->tokens(), ("std :: string & " + tok->next()->str() + " (").c_str()));
234+
235+
const std::string &funcname(tok->next()->str());
236+
237+
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
238+
239+
std::list<Scope>::const_iterator scope;
240+
241+
bool retref = false; // is there such a function that returns a reference?
242+
bool retvalue = false; // is there such a function that returns a value?
243+
244+
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
245+
if (scope->type == Scope::eFunction) {
246+
if (scope->classDef->str() == funcname) {
247+
const Token *tok2 = scope->classDef;
248+
while (tok2 && Token::Match(tok2->tokAt(-2), "%type% ::"))
249+
tok2 = tok2->tokAt(-2);
250+
if (tok2 && Token::simpleMatch(tok2->tokAt(-3), "std :: string"))
251+
retvalue = true;
252+
else if (tok2 && Token::simpleMatch(tok2->tokAt(-4), "std :: string &"))
253+
retref = true;
254+
}
255+
}
256+
}
257+
258+
return bool(!retref && retvalue);
237259
}
238260

239261
//---------------------------------------------------------------------------

0 commit comments

Comments
 (0)