Fixed #4329 (False duplicateBranch when branches use conditionally defined macros)

This commit is contained in:
Daniel Marjamäki 2013-02-18 17:18:33 +01:00
parent e6915e7a78
commit 1c584208b4
2 changed files with 25 additions and 0 deletions

View File

@ -2740,6 +2740,18 @@ void CheckOther::checkDuplicateBranch()
// check all the code in the function for if (..) else // check all the code in the function for if (..) else
if (Token::simpleMatch(scope->classEnd, "} else {")) { if (Token::simpleMatch(scope->classEnd, "} else {")) {
// Make sure there are no macros (different macros might be expanded
// to the same code)
bool macro = false;
for (const Token *tok = scope->classStart; tok != scope->classEnd->linkAt(2); tok = tok->next()) {
if (tok->isExpandedMacro()) {
macro = true;
break;
}
}
if (macro)
continue;
// save if branch code // save if branch code
std::string branch1 = scope->classStart->next()->stringifyList(scope->classEnd); std::string branch1 = scope->classStart->next()->stringifyList(scope->classEnd);

View File

@ -166,6 +166,7 @@ private:
TEST_CASE(duplicateIf1); // ticket 3689 TEST_CASE(duplicateIf1); // ticket 3689
TEST_CASE(duplicateBranch); TEST_CASE(duplicateBranch);
TEST_CASE(duplicateBranch1); // tests extracted by http://www.viva64.com/en/b/0149/ ( Comparison between PVS-Studio and cppcheck ): Errors detected in Quake 3: Arena by PVS-Studio: Fragement 2 TEST_CASE(duplicateBranch1); // tests extracted by http://www.viva64.com/en/b/0149/ ( Comparison between PVS-Studio and cppcheck ): Errors detected in Quake 3: Arena by PVS-Studio: Fragement 2
TEST_CASE(duplicateBranch2); // empty macro
TEST_CASE(duplicateExpression1); TEST_CASE(duplicateExpression1);
TEST_CASE(duplicateExpression2); // ticket #2730 TEST_CASE(duplicateExpression2); // ticket #2730
TEST_CASE(duplicateExpression3); // ticket #3317 TEST_CASE(duplicateExpression3); // ticket #3317
@ -5115,6 +5116,18 @@ private:
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) Found duplicate branches for 'if' and 'else'.\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) Found duplicate branches for 'if' and 'else'.\n", errout.str());
} }
void duplicateBranch2() {
Preprocessor::macroChar = '$';
check("void f(int x) {\n" // #4329
" if (x)\n"
" $;\n"
" else\n"
" $;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void duplicateExpression1() { void duplicateExpression1() {
check("void foo() {\n" check("void foo() {\n"
" if (a == a) { }\n" " if (a == a) { }\n"