From ba3757dfd383e875fc610863337a36b83d9217fb Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Thu, 10 Jul 2014 11:23:14 +0400 Subject: [PATCH] Bring variables decalaration to where they are really needed. --- lib/checkleakautovar.cpp | 7 +++---- lib/checkother.cpp | 6 +++--- lib/symboldatabase.cpp | 4 ++-- lib/tokenize.cpp | 11 ++++++----- lib/tokenlist.cpp | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index a27685761..e13be4539 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -389,15 +389,14 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, void CheckLeakAutoVar::functionCall(const Token *tok, VarInfo *varInfo, const int dealloc) { - std::map &alloctype = varInfo->alloctype; - std::map &possibleUsage = varInfo->possibleUsage; - // Ignore function call? const bool ignore = bool(_settings->library.leakignore.find(tok->str()) != _settings->library.leakignore.end()); - if (ignore) return; + std::map &alloctype = varInfo->alloctype; + std::map &possibleUsage = varInfo->possibleUsage; + for (const Token *arg = tok->tokAt(2); arg; arg = arg->nextArgument()) { if ((Token::Match(arg, "%var% [-,)]") && arg->varId() > 0) || (Token::Match(arg, "& %var%") && arg->next()->varId() > 0)) { diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 55a764f27..224f544cc 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2759,11 +2759,11 @@ void CheckOther::checkDuplicateExpression() for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) { if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|*|<<|>>")) { - bool assignment = tok->str() == "="; if (Token::Match(tok, "==|!=|-") && astIsFloat(tok->astOperand1(), true)) continue; if (isSameExpression(tok->astOperand1(), tok->astOperand2(), _settings->library.functionpure)) { if (isWithoutSideEffects(_tokenizer, tok->astOperand1())) { + const bool assignment = tok->str() == "="; if (assignment) selfAssignmentError(tok, tok->astOperand1()->expressionString()); else @@ -2949,13 +2949,13 @@ void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalse() const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { if (tok->isName() && Token::Match(tok, "isgreater|isless|islessgreater|isgreaterequal|islessequal ( %var% , %var% )")) { - const std::string& functionName = tok->str(); // store function name - const std::string& varNameLeft = tok->strAt(2); // get the left variable name const unsigned int varidLeft = tok->tokAt(2)->varId();// get the left varid const unsigned int varidRight = tok->tokAt(4)->varId();// get the right varid // compare varids: if they are not zero but equal // --> the comparison function is calles with the same variables if (varidLeft != 0 && varidLeft == varidRight) { + const std::string& functionName = tok->str(); // store function name + const std::string& varNameLeft = tok->strAt(2); // get the left variable name if (functionName == "isgreater" || functionName == "isless" || functionName == "islessgreater") { // e.g.: isgreater(x,x) --> (x)>(x) --> false checkComparisonFunctionIsAlwaysTrueOrFalseError(tok,functionName,varNameLeft,false); diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 29ca9f697..55ea54370 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2344,9 +2344,7 @@ AccessControl Scope::defaultAccess() const // Get variable list.. void Scope::getVariableList() { - AccessControl varaccess = defaultAccess(); const Token *start; - unsigned int level = 1; if (classStart) start = classStart->next(); @@ -2359,6 +2357,8 @@ void Scope::getVariableList() else return; + AccessControl varaccess = defaultAccess(); + unsigned int level = 1; for (const Token *tok = start; tok; tok = tok->next()) { // end of scope? if (tok->str() == "}") { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 87d1f5a87..1a6f910fa 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5273,13 +5273,13 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_ //pattern: "%type% *| ... *| const| %var% ,|=" if (Token::Match(tok2, "%type%") || (tok2 && tok2->previous() && tok2->previous()->str() == ">")) { - bool ispointer = false; Token *varName = tok2; if (!tok2->previous() || tok2->previous()->str() != ">") varName = varName->next(); else --typelen; //skip all the pointer part + bool ispointer = false; while (varName && varName->str() == "*") { ispointer = true; varName = varName->next(); @@ -6337,13 +6337,13 @@ bool Tokenizer::simplifyKnownVariables() } // struct name.. + if (Token::Match(tok2, "%varid% = &| %varid%", tok2->varId())) + continue; + const std::string structname = Token::Match(tok2->tokAt(-3), "[;{}] %var% .") ? std::string(tok2->strAt(-2) + " .") : std::string(""); - if (Token::Match(tok2, "%varid% = &| %varid%", tok2->varId())) - continue; - const Token * const valueToken = tok2->tokAt(2); std::string value; @@ -6396,13 +6396,14 @@ bool Tokenizer::simplifyKnownVariables() else if (Token::Match(tok2, "strcpy|sprintf ( %var% , %str% ) ;")) { const unsigned int varid(tok2->tokAt(2)->varId()); - std::string::size_type n = std::string::npos; if (varid == 0) continue; + const std::string structname; const Token * const valueToken = tok2->tokAt(4); std::string value(valueToken->str()); if (tok2->str() == "sprintf") { + std::string::size_type n = std::string::npos; while ((n = value.find("%%",n+1)) != std::string::npos) { value.replace(n,2,"%"); } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index fafb3a901..fda1cb348 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -579,13 +579,13 @@ static void compilePrecedence2(Token *&tok, AST_state& state) bool opPrevTopSquare = !state.op.empty() && state.op.top() && state.op.top()->str() == "["; std::size_t oldOpSize = state.op.size(); compileExpression(tok, state); - bool operandInside = oldOpSize < state.op.size(); tok = tok2; if ((tok->previous() && tok->previous()->isName() && (tok->strAt(-1) != "return" && (!state.cpp || !Token::Match(tok->previous(), "throw|delete")))) || (tok->strAt(-1) == "]" && (!state.cpp || !Token::Match(tok->linkAt(-1)->previous(), "new|delete"))) || (tok->strAt(-1) == ">" && tok->linkAt(-1)) || (tok->strAt(-1) == ")" && !iscast(tok->linkAt(-1))) // Don't treat brackets to clarify precedence as function calls || (tok->strAt(-1) == "}" && opPrevTopSquare)) { + const bool operandInside = oldOpSize < state.op.size(); if (operandInside) compileBinOp(tok, state, nullptr); else