From d40b77dce24014cd17f1b6fef9c11f4017a8db0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 2 Jul 2014 16:16:19 +0200 Subject: [PATCH] Removed special 'else if' handling. this is redundant since these are simplified. --- lib/checkother.cpp | 6 +++--- lib/checkstl.cpp | 19 ++++++------------- lib/symboldatabase.cpp | 5 +---- lib/symboldatabase.h | 4 ++-- lib/templatesimplifier.cpp | 1 + lib/tokenize.cpp | 7 +++++++ 6 files changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9ede3825f..55a764f27 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -456,7 +456,7 @@ void CheckOther::checkSuspiciousSemicolon() // Look for "if(); {}", "for(); {}" or "while(); {}" for (std::list::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { - if (i->type == Scope::eIf || i->type == Scope::eElse || i->type == Scope::eElseIf || i->type == Scope::eWhile || i->type == Scope::eFor) { + if (i->type == Scope::eIf || i->type == Scope::eElse || i->type == Scope::eWhile || i->type == Scope::eFor) { // Ensure the semicolon is at the same line number as the if/for/while statement // and the {..} block follows it without an extra empty line. if (Token::simpleMatch(i->classStart, "{ ; } {") && @@ -2467,7 +2467,7 @@ void CheckOther::checkDuplicateBranch() std::list::const_iterator scope; for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { - if (scope->type != Scope::eIf && scope->type != Scope::eElseIf) + if (scope->type != Scope::eIf) continue; // check all the code in the function for if (..) else @@ -3310,7 +3310,7 @@ void CheckOther::oppositeInnerCondition() const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); for (std::list::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { - if (scope->type != Scope::eIf && scope->type != Scope::eElseIf) + if (scope->type != Scope::eIf) continue; if (!Token::simpleMatch(scope->classDef->linkAt(1), ") {")) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 9baf8e127..dd43d1125 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -308,12 +308,10 @@ void CheckStl::stlOutOfBounds() for (std::list::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { const Token* tok = i->classDef; // only interested in conditions - if ((i->type != Scope::eFor && i->type != Scope::eWhile && i->type != Scope::eIf && i->type != Scope::eElseIf) || !tok) + if ((i->type != Scope::eFor && i->type != Scope::eWhile && i->type != Scope::eIf) || !tok) continue; - if (i->type == Scope::eElseIf) - tok = tok->tokAt(2); - else if (i->type == Scope::eFor) + if (i->type == Scope::eFor) tok = Token::findsimplematch(tok->tokAt(2), ";"); else tok = tok->next(); @@ -773,7 +771,7 @@ void CheckStl::if_find() const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); for (std::list::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { - if ((i->type != Scope::eIf && i->type != Scope::eElseIf && i->type != Scope::eWhile) || !i->classDef) + if ((i->type != Scope::eIf && i->type != Scope::eWhile) || !i->classDef) continue; const Token* tok = i->classDef->next(); @@ -973,13 +971,10 @@ void CheckStl::redundantCondition() const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); for (std::list::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { - if (i->type != Scope::eIf && i->type != Scope::eElseIf) + if (i->type != Scope::eIf) continue; const Token* tok = i->classDef->tokAt(2); - if (i->type == Scope::eElseIf) - tok = tok->next(); - if (!Token::Match(tok, "%var% . find ( %any% ) != %var% . end|rend|cend|crend ( ) ) { %var% . remove|erase ( %any% ) ;")) continue; @@ -1467,13 +1462,11 @@ void CheckStl::checkDereferenceInvalidIterator() // be an iterator that is dereferenced before being checked for validity. const std::list& scopeList = _tokenizer->getSymbolDatabase()->scopeList; for (std::list::const_iterator i = scopeList.begin(); i != scopeList.end(); ++i) { - if (i->type == Scope::eIf || i->type == Scope::eElseIf || i->type == Scope::eDo || i->type == Scope::eWhile || i->type == Scope::eFor) { + if (i->type == Scope::eIf || i->type == Scope::eDo || i->type == Scope::eWhile || i->type == Scope::eFor) { const Token* const tok = i->classDef; const Token* startOfCondition = tok->next(); - if (i->type == Scope::eElseIf) - startOfCondition = startOfCondition->next(); - else if (i->type == Scope::eDo) + if (i->type == Scope::eDo) startOfCondition = startOfCondition->link()->tokAt(2); const Token* endOfCondition = startOfCondition->link(); if (!endOfCondition) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index ffed97035..29ca9f697 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -748,9 +748,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti scope = &scopeList.back(); } else if (Token::Match(tok, "if|for|while|catch|switch (") && Token::simpleMatch(tok->next()->link(), ") {")) { const Token *tok1 = tok->next()->link()->next(); - if (tok->str() == "if" && tok->strAt(-1) == "else") - scopeList.push_back(Scope(this, tok->previous(), scope, Scope::eElseIf, tok1)); - else if (tok->str() == "if") + if (tok->str() == "if") scopeList.push_back(Scope(this, tok, scope, Scope::eIf, tok1)); else if (tok->str() == "for") { scopeList.push_back(Scope(this, tok, scope, Scope::eFor, tok1)); @@ -1784,7 +1782,6 @@ static std::ostream & operator << (std::ostream & s, Scope::ScopeType type) type == Scope::eFunction ? "Function" : type == Scope::eIf ? "If" : type == Scope::eElse ? "Else" : - type == Scope::eElseIf ? "ElseIf" : type == Scope::eFor ? "For" : type == Scope::eWhile ? "While" : type == Scope::eDo ? "Do" : diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index b256d5548..bc78839a1 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -651,7 +651,7 @@ public: const Scope *scope; }; - enum ScopeType { eGlobal, eClass, eStruct, eUnion, eNamespace, eFunction, eIf, eElse, eElseIf, eFor, eWhile, eDo, eSwitch, eUnconditional, eTry, eCatch }; + enum ScopeType { eGlobal, eClass, eStruct, eUnion, eNamespace, eFunction, eIf, eElse, eFor, eWhile, eDo, eSwitch, eUnconditional, eTry, eCatch }; Scope(const SymbolDatabase *check_, const Token *classDef_, const Scope *nestedIn_); Scope(const SymbolDatabase *check_, const Token *classDef_, const Scope *nestedIn_, ScopeType type_, const Token *start_); @@ -685,7 +685,7 @@ public: } bool isLocal() const { - return (type == eIf || type == eElse || type == eElseIf || + return (type == eIf || type == eElse || type == eFor || type == eWhile || type == eDo || type == eSwitch || type == eUnconditional || type == eTry || type == eCatch); diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index e936e33a7..0f71f99fa 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #ifdef GDB_HELPERS diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 7631c32b0..bfa882c2b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -32,6 +32,7 @@ #include #include #include +#include //--------------------------------------------------------------------------- @@ -2234,6 +2235,12 @@ void Tokenizer::simplifyTemplates() if (isC()) return; + for (const Token *tok = list.front(); tok; tok = tok->next()) { + if (tok->str()[0] == '=' && tok->str().length() > 2) { + std::cout << "BAD TOKEN" << std::endl; + } + } + for (Token *tok = list.front(); tok; tok = tok->next()) { // #2648 - simple fix for sizeof used as template parameter // TODO: this is a bit hardcoded. make a bit more generic