Removed special 'else if' handling. this is redundant since these are simplified.

This commit is contained in:
Daniel Marjamäki 2014-07-02 16:16:19 +02:00
parent 719304a8fc
commit d40b77dce2
6 changed files with 20 additions and 22 deletions

View File

@ -456,7 +456,7 @@ void CheckOther::checkSuspiciousSemicolon()
// Look for "if(); {}", "for(); {}" or "while(); {}" // Look for "if(); {}", "for(); {}" or "while(); {}"
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { for (std::list<Scope>::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 // 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. // and the {..} block follows it without an extra empty line.
if (Token::simpleMatch(i->classStart, "{ ; } {") && if (Token::simpleMatch(i->classStart, "{ ; } {") &&
@ -2467,7 +2467,7 @@ void CheckOther::checkDuplicateBranch()
std::list<Scope>::const_iterator scope; std::list<Scope>::const_iterator scope;
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++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; continue;
// check all the code in the function for if (..) else // check all the code in the function for if (..) else
@ -3310,7 +3310,7 @@ void CheckOther::oppositeInnerCondition()
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::list<Scope>::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { for (std::list<Scope>::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; continue;
if (!Token::simpleMatch(scope->classDef->linkAt(1), ") {")) if (!Token::simpleMatch(scope->classDef->linkAt(1), ") {"))

View File

@ -308,12 +308,10 @@ void CheckStl::stlOutOfBounds()
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) {
const Token* tok = i->classDef; const Token* tok = i->classDef;
// only interested in conditions // 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; continue;
if (i->type == Scope::eElseIf) if (i->type == Scope::eFor)
tok = tok->tokAt(2);
else if (i->type == Scope::eFor)
tok = Token::findsimplematch(tok->tokAt(2), ";"); tok = Token::findsimplematch(tok->tokAt(2), ";");
else else
tok = tok->next(); tok = tok->next();
@ -773,7 +771,7 @@ void CheckStl::if_find()
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { for (std::list<Scope>::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; continue;
const Token* tok = i->classDef->next(); const Token* tok = i->classDef->next();
@ -973,13 +971,10 @@ void CheckStl::redundantCondition()
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { for (std::list<Scope>::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; continue;
const Token* tok = i->classDef->tokAt(2); 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% ) ;")) if (!Token::Match(tok, "%var% . find ( %any% ) != %var% . end|rend|cend|crend ( ) ) { %var% . remove|erase ( %any% ) ;"))
continue; continue;
@ -1467,13 +1462,11 @@ void CheckStl::checkDereferenceInvalidIterator()
// be an iterator that is dereferenced before being checked for validity. // be an iterator that is dereferenced before being checked for validity.
const std::list<Scope>& scopeList = _tokenizer->getSymbolDatabase()->scopeList; const std::list<Scope>& scopeList = _tokenizer->getSymbolDatabase()->scopeList;
for (std::list<Scope>::const_iterator i = scopeList.begin(); i != scopeList.end(); ++i) { for (std::list<Scope>::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* const tok = i->classDef;
const Token* startOfCondition = tok->next(); const Token* startOfCondition = tok->next();
if (i->type == Scope::eElseIf) if (i->type == Scope::eDo)
startOfCondition = startOfCondition->next();
else if (i->type == Scope::eDo)
startOfCondition = startOfCondition->link()->tokAt(2); startOfCondition = startOfCondition->link()->tokAt(2);
const Token* endOfCondition = startOfCondition->link(); const Token* endOfCondition = startOfCondition->link();
if (!endOfCondition) if (!endOfCondition)

View File

@ -748,9 +748,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
scope = &scopeList.back(); scope = &scopeList.back();
} else if (Token::Match(tok, "if|for|while|catch|switch (") && Token::simpleMatch(tok->next()->link(), ") {")) { } else if (Token::Match(tok, "if|for|while|catch|switch (") && Token::simpleMatch(tok->next()->link(), ") {")) {
const Token *tok1 = tok->next()->link()->next(); const Token *tok1 = tok->next()->link()->next();
if (tok->str() == "if" && tok->strAt(-1) == "else") if (tok->str() == "if")
scopeList.push_back(Scope(this, tok->previous(), scope, Scope::eElseIf, tok1));
else if (tok->str() == "if")
scopeList.push_back(Scope(this, tok, scope, Scope::eIf, tok1)); scopeList.push_back(Scope(this, tok, scope, Scope::eIf, tok1));
else if (tok->str() == "for") { else if (tok->str() == "for") {
scopeList.push_back(Scope(this, tok, scope, Scope::eFor, tok1)); 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::eFunction ? "Function" :
type == Scope::eIf ? "If" : type == Scope::eIf ? "If" :
type == Scope::eElse ? "Else" : type == Scope::eElse ? "Else" :
type == Scope::eElseIf ? "ElseIf" :
type == Scope::eFor ? "For" : type == Scope::eFor ? "For" :
type == Scope::eWhile ? "While" : type == Scope::eWhile ? "While" :
type == Scope::eDo ? "Do" : type == Scope::eDo ? "Do" :

View File

@ -651,7 +651,7 @@ public:
const Scope *scope; 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_);
Scope(const SymbolDatabase *check_, const Token *classDef_, const Scope *nestedIn_, ScopeType type_, const Token *start_); Scope(const SymbolDatabase *check_, const Token *classDef_, const Scope *nestedIn_, ScopeType type_, const Token *start_);
@ -685,7 +685,7 @@ public:
} }
bool isLocal() const { bool isLocal() const {
return (type == eIf || type == eElse || type == eElseIf || return (type == eIf || type == eElse ||
type == eFor || type == eWhile || type == eDo || type == eFor || type == eWhile || type == eDo ||
type == eSwitch || type == eUnconditional || type == eSwitch || type == eUnconditional ||
type == eTry || type == eCatch); type == eTry || type == eCatch);

View File

@ -30,6 +30,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <cassert> #include <cassert>
#include <iostream>
#ifdef GDB_HELPERS #ifdef GDB_HELPERS

View File

@ -32,6 +32,7 @@
#include <cassert> #include <cassert>
#include <cctype> #include <cctype>
#include <stack> #include <stack>
#include <iostream>
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -2234,6 +2235,12 @@ void Tokenizer::simplifyTemplates()
if (isC()) if (isC())
return; 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()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
// #2648 - simple fix for sizeof used as template parameter // #2648 - simple fix for sizeof used as template parameter
// TODO: this is a bit hardcoded. make a bit more generic // TODO: this is a bit hardcoded. make a bit more generic