From 60de3e75af560973a6dabc4ed7b3e61a4e3cf62d Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Mon, 1 Oct 2012 15:38:31 +0200 Subject: [PATCH] CheckOther::checkComparisonOfBoolWithBool,checkComparisonOfFuncReturningBool: use symbolDatabase to check only tokens in executable code. --- lib/checkother.cpp | 102 ++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 47 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index dccb503eb..bdde0cfef 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2231,40 +2231,44 @@ void CheckOther::checkComparisonOfFuncReturningBool() const SymbolDatabase * const symbolDatabase = _tokenizer->getSymbolDatabase(); - for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (tok->previous() && tok->type() == Token::eComparisonOp && tok->str() != "==" && tok->str() != "!=") { - const Token *first_token; - bool first_token_func_of_type_bool = false; - if (Token::simpleMatch(tok->previous(), ")")) { - first_token = tok->previous()->link()->previous(); - } else { - first_token = tok->previous(); - } - if (Token::Match(first_token, "%var% (") && !Token::Match(first_token->previous(), "::|.")) { - const Function* func = symbolDatabase->findFunctionByName(first_token->str(), first_token->scope()); - if (func && func->tokenDef && func->tokenDef->strAt(-1) == "bool") { - first_token_func_of_type_bool = true; + for (std::list::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { + if (!scope->isExecutable()) + continue; + for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { + if (tok->previous() && tok->type() == Token::eComparisonOp && tok->str() != "==" && tok->str() != "!=") { + const Token *first_token; + bool first_token_func_of_type_bool = false; + if (Token::simpleMatch(tok->previous(), ")")) { + first_token = tok->previous()->link()->previous(); + } else { + first_token = tok->previous(); } - } - - Token *second_token = tok->next(); - bool second_token_func_of_type_bool = false; - while (second_token->str()=="!") { - second_token = second_token->next(); - } - if (Token::Match(second_token, "%var% (") && !Token::Match(second_token->previous(), "::|.")) { - const Function* func = symbolDatabase->findFunctionByName(second_token->str(), second_token->scope()); - if (func && func->tokenDef && func->tokenDef->strAt(-1) == "bool") { - second_token_func_of_type_bool = true; + if (Token::Match(first_token, "%var% (") && !Token::Match(first_token->previous(), "::|.")) { + const Function* func = symbolDatabase->findFunctionByName(first_token->str(), first_token->scope()); + if (func && func->tokenDef && func->tokenDef->strAt(-1) == "bool") { + first_token_func_of_type_bool = true; + } } - } - if ((first_token_func_of_type_bool == true) && (second_token_func_of_type_bool == true)) { - comparisonOfTwoFuncsReturningBoolError(first_token->next(), first_token->str(), second_token->str()); - } else if (first_token_func_of_type_bool == true) { - comparisonOfFuncReturningBoolError(first_token->next(), first_token->str()); - } else if (second_token_func_of_type_bool == true) { - comparisonOfFuncReturningBoolError(second_token->previous(), second_token->str()); + Token *second_token = tok->next(); + bool second_token_func_of_type_bool = false; + while (second_token->str()=="!") { + second_token = second_token->next(); + } + if (Token::Match(second_token, "%var% (") && !Token::Match(second_token->previous(), "::|.")) { + const Function* func = symbolDatabase->findFunctionByName(second_token->str(), second_token->scope()); + if (func && func->tokenDef && func->tokenDef->strAt(-1) == "bool") { + second_token_func_of_type_bool = true; + } + } + + if ((first_token_func_of_type_bool == true) && (second_token_func_of_type_bool == true)) { + comparisonOfTwoFuncsReturningBoolError(first_token->next(), first_token->str(), second_token->str()); + } else if (first_token_func_of_type_bool == true) { + comparisonOfFuncReturningBoolError(first_token->next(), first_token->str()); + } else if (second_token_func_of_type_bool == true) { + comparisonOfFuncReturningBoolError(second_token->previous(), second_token->str()); + } } } } @@ -2307,25 +2311,29 @@ void CheckOther::checkComparisonOfBoolWithBool() const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase(); - for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (tok->previous() && tok->type() == Token::eComparisonOp && tok->str() != "==" && tok->str() != "!=") { - bool first_token_bool = false; - bool second_token_bool = false; + for (std::list::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { + if (!scope->isExecutable()) + continue; + for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { + if (tok->previous() && tok->type() == Token::eComparisonOp && tok->str() != "==" && tok->str() != "!=") { + bool first_token_bool = false; + bool second_token_bool = false; - const Token *first_token = tok->previous(); - if (first_token->varId()) { - if (isBool(symbolDatabase->getVariableFromVarId(first_token->varId()))) { - first_token_bool = true; + const Token *first_token = tok->previous(); + if (first_token->varId()) { + if (isBool(symbolDatabase->getVariableFromVarId(first_token->varId()))) { + first_token_bool = true; + } } - } - const Token *second_token = tok->next(); - if (second_token->varId()) { - if (isBool(symbolDatabase->getVariableFromVarId(second_token->varId()))) { - second_token_bool = true; + const Token *second_token = tok->next(); + if (second_token->varId()) { + if (isBool(symbolDatabase->getVariableFromVarId(second_token->varId()))) { + second_token_bool = true; + } + } + if ((first_token_bool == true) && (second_token_bool == true)) { + comparisonOfBoolWithBoolError(first_token->next(), first_token->str()); } - } - if ((first_token_bool == true) && (second_token_bool == true)) { - comparisonOfBoolWithBoolError(first_token->next(), first_token->str()); } } }