From 9352e124feebe19def07ee84af516caa1e5829cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 19 Jul 2012 16:42:56 +0200 Subject: [PATCH] Auto variables: Speedup my fix. Use the symbol database instead of Token::findmatch. --- lib/checkautovariables.cpp | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 0c1fabc69..5471c67f2 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -231,9 +231,31 @@ bool CheckAutoVariables::returnTemporary(const Token *tok) const { if (!Token::Match(tok, "return %var% (")) return false; - // TODO: Find all functions that return objects by value - return bool(NULL != Token::findmatch(_tokenizer->tokens(), ("std :: string " + tok->next()->str() + " (").c_str()) && - NULL == Token::findmatch(_tokenizer->tokens(), ("std :: string & " + tok->next()->str() + " (").c_str())); + + const std::string &funcname(tok->next()->str()); + + const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); + + std::list::const_iterator scope; + + bool retref = false; // is there such a function that returns a reference? + bool retvalue = false; // is there such a function that returns a value? + + for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { + if (scope->type == Scope::eFunction) { + if (scope->classDef->str() == funcname) { + const Token *tok2 = scope->classDef; + while (tok2 && Token::Match(tok2->tokAt(-2), "%type% ::")) + tok2 = tok2->tokAt(-2); + if (tok2 && Token::simpleMatch(tok2->tokAt(-3), "std :: string")) + retvalue = true; + else if (tok2 && Token::simpleMatch(tok2->tokAt(-4), "std :: string &")) + retref = true; + } + } + } + + return bool(!retref && retvalue); } //---------------------------------------------------------------------------