Fixed #4547 (Improve check: Duplicate conditions in 'if' and related 'else { if'.)

Change tokenizer: "else if" -->> "else { if"
This commit is contained in:
Frank Zingsheim 2013-02-04 21:12:12 +01:00
parent 446f326225
commit b531195e08
4 changed files with 31 additions and 16 deletions

View File

@ -2620,14 +2620,14 @@ void CheckOther::checkDuplicateIf()
// save the expression and its location
expressionMap.insert(std::make_pair(expression, tok));
// find the next else if (...) statement
// find the next else { if (...) statement
const Token *tok1 = scope->classEnd;
// check all the else if (...) statements
while (Token::simpleMatch(tok1, "} else if (") &&
Token::simpleMatch(tok1->linkAt(3), ") {")) {
// check all the else { if (...) statements
while (Token::simpleMatch(tok1, "} else { if (") &&
Token::simpleMatch(tok1->linkAt(4), ") {")) {
// get the expression from the token stream
expression = tok1->tokAt(4)->stringifyList(tok1->linkAt(3));
expression = tok1->tokAt(5)->stringifyList(tok1->linkAt(4));
// try to look up the expression to check for duplicates
std::map<std::string, const Token *>::iterator it = expressionMap.find(expression);
@ -2635,7 +2635,7 @@ void CheckOther::checkDuplicateIf()
// found a duplicate
if (it != expressionMap.end()) {
// check for expressions that have side effects and ignore them
if (!expressionHasSideEffects(tok1->tokAt(4), tok1->linkAt(3)->previous()))
if (!expressionHasSideEffects(tok1->tokAt(5), tok1->linkAt(4)->previous()))
duplicateIfError(it->second, tok1->next());
}
@ -2643,8 +2643,8 @@ void CheckOther::checkDuplicateIf()
else
expressionMap.insert(std::make_pair(expression, tok1->next()));
// find the next else if (...) statement
tok1 = tok1->linkAt(3)->next()->link();
// find the next else { if (...) statement
tok1 = tok1->linkAt(4)->next()->link();
}
}
}

View File

@ -4090,14 +4090,8 @@ Token *Tokenizer::simplifyAddBracesToCommand(Token *tok)
} else if (tok->str()=="if") {
tokEnd=simplifyAddBracesPair(tok,true);
Token * tokEndNext=tokEnd->next();
if (tokEndNext && tokEndNext->str()=="else") {
Token * tokEndNextNext=tokEndNext->next();
if (tokEndNextNext && tokEndNextNext->str()=="if") {
// do not change "else if ..." to "else { if ... }"
tokEnd=simplifyAddBracesToCommand(tokEndNextNext);
} else
tokEnd=simplifyAddBracesPair(tokEndNext,false);
}
if (tokEndNext && tokEndNext->str()=="else")
tokEnd=simplifyAddBracesPair(tokEndNext,false);
}
return tokEnd;

View File

@ -4913,6 +4913,12 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (style) Duplicate conditions in 'if' and related 'else if'.\n", errout.str());
check("void f(int a, int &b) {\n"
" if (a) { b = 1; }\n"
" else { if (a) { b = 2; } }\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (style) Duplicate conditions in 'if' and related 'else if'.\n", errout.str());
check("void f(int a, int &b) {\n"
" if (a == 1) { b = 1; }\n"
" else if (a == 2) { b = 2; }\n"

View File

@ -108,6 +108,7 @@ private:
TEST_CASE(ifAddBraces17); // '} else' should be in the same line
TEST_CASE(ifAddBraces18); // #3424 - if if { } else else
TEST_CASE(ifAddBraces19); // #3928 - if for if else
TEST_CASE(ifAddBraces20);
TEST_CASE(whileAddBraces);
TEST_CASE(doWhileAddBraces);
@ -1217,6 +1218,20 @@ private:
"}", tokenizeAndStringify(code, true));
}
void ifAddBraces20() {
// if else if
const char code[] = "void f()\n"
"{\n"
" if (a);\n"
" else if (b);\n"
"}\n";
ASSERT_EQUALS("void f ( )\n"
"{\n"
"if ( a ) { ; }\n"
"else { if ( b ) { ; } }\n"
"}", tokenizeAndStringify(code, true));
}
void whileAddBraces() {
const char code[] = ";while(a);";