Reenable followVar (#1363)

Reenable followVar
This commit is contained in:
Paul Fultz II 2018-09-04 22:53:33 -05:00 committed by Daniel Marjamäki
parent c6d43506b6
commit 08f301a0de
3 changed files with 16 additions and 23 deletions

View File

@ -133,7 +133,6 @@ const Token * astIsVariableComparison(const Token *tok, const std::string &comp,
return ret; return ret;
} }
/*
static const Token * getVariableInitExpression(const Variable * var) static const Token * getVariableInitExpression(const Variable * var)
{ {
if (!var || !var->declEndToken()) if (!var || !var->declEndToken())
@ -166,7 +165,7 @@ static const Token * followVariableExpression(const Token * tok, bool cpp)
if (Token::Match(tok->astTop(), "%assign%") || Token::Match(tok->next(), "%assign%")) if (Token::Match(tok->astTop(), "%assign%") || Token::Match(tok->next(), "%assign%"))
return tok; return tok;
// Skip references // Skip references
if (Token::Match(tok->astTop(), "& %var%;") && Token::Match(tok->astTop()->astOperand1(), "%type%")) if (Token::Match(tok->astTop(), "& %var% ;") && Token::Match(tok->astTop()->astOperand1(), "%type%"))
return tok; return tok;
const Variable * var = tok->variable(); const Variable * var = tok->variable();
const Token * varTok = getVariableInitExpression(var); const Token * varTok = getVariableInitExpression(var);
@ -231,7 +230,6 @@ static void followVariableExpressionError(const Token *tok1, const Token *tok2,
return; return;
errors->push_back(item); errors->push_back(item);
} }
*/
bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2, const Library& library, bool pure, ErrorPath* errors) bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2, const Library& library, bool pure, ErrorPath* errors)
{ {
@ -252,10 +250,6 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
if (Token::simpleMatch(tok2, "!") && Token::simpleMatch(tok2->astOperand1(), "!") && !Token::simpleMatch(tok2->astParent(), "=")) { if (Token::simpleMatch(tok2, "!") && Token::simpleMatch(tok2->astOperand1(), "!") && !Token::simpleMatch(tok2->astParent(), "=")) {
return isSameExpression(cpp, macro, tok1, tok2->astOperand1()->astOperand1(), library, pure, errors); return isSameExpression(cpp, macro, tok1, tok2->astOperand1()->astOperand1(), library, pure, errors);
} }
// Follow variables if possible
// DM: followVariableExpression is disabled for now because it cause problems.
// See #8709, #8713, #8717 and #8722
/*
if (tok1->str() != tok2->str() && (Token::Match(tok1, "%var%") || Token::Match(tok2, "%var%"))) { if (tok1->str() != tok2->str() && (Token::Match(tok1, "%var%") || Token::Match(tok2, "%var%"))) {
const Token * varTok1 = followVariableExpression(tok1, cpp); const Token * varTok1 = followVariableExpression(tok1, cpp);
if (varTok1->str() == tok2->str()) { if (varTok1->str() == tok2->str()) {
@ -273,7 +267,6 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
return isSameExpression(cpp, macro, varTok1, varTok2, library, pure, errors); return isSameExpression(cpp, macro, varTok1, varTok2, library, pure, errors);
} }
} }
*/
if (tok1->varId() != tok2->varId() || tok1->str() != tok2->str() || tok1->originalName() != tok2->originalName()) { if (tok1->varId() != tok2->varId() || tok1->str() != tok2->str() || tok1->originalName() != tok2->originalName()) {
if ((Token::Match(tok1,"<|>") && Token::Match(tok2,"<|>")) || if ((Token::Match(tok1,"<|>") && Token::Match(tok2,"<|>")) ||
(Token::Match(tok1,"<=|>=") && Token::Match(tok2,"<=|>="))) { (Token::Match(tok1,"<=|>=") && Token::Match(tok2,"<=|>="))) {

View File

@ -1245,7 +1245,7 @@ private:
" if (a > x && a < y)\n" " if (a > x && a < y)\n"
" return;\n" " return;\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:8]: (warning) Logical conjunction always evaluates to false: a > x && a < y.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:8]: (warning) Logical conjunction always evaluates to false: a > x && a < y.\n", errout.str());
check("struct A {\n" check("struct A {\n"
" void f();\n" " void f();\n"
@ -1275,7 +1275,7 @@ private:
" if (a > x && a < y)\n" " if (a > x && a < y)\n"
" return;\n" " return;\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:5]: (warning) Logical conjunction always evaluates to false: a > x && a < y.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (warning) Logical conjunction always evaluates to false: a > x && a < y.\n", errout.str());
} }
void secondAlwaysTrueFalseWhenFirstTrueError() { void secondAlwaysTrueFalseWhenFirstTrueError() {
@ -1600,7 +1600,7 @@ private:
" if(!b) {}\n" " if(!b) {}\n"
" }\n" " }\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str());
check("void foo(unsigned u) {\n" check("void foo(unsigned u) {\n"
" if (u != 0) {\n" " if (u != 0) {\n"

View File

@ -3916,13 +3916,13 @@ private:
" const int i = sizeof(int);\n" " const int i = sizeof(int);\n"
" if ( i != sizeof (int)){}\n" " if ( i != sizeof (int)){}\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'i != sizeof(int)' is always false because 'i' and 'sizeof(int)' represent the same value.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'i != sizeof(int)' is always false because 'i' and 'sizeof(int)' represent the same value.\n", errout.str());
check("void f() {\n" check("void f() {\n"
" const int i = sizeof(int);\n" " const int i = sizeof(int);\n"
" if ( sizeof (int) != i){}\n" " if ( sizeof (int) != i){}\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'sizeof(int) != i' is always false because 'sizeof(int)' and 'i' represent the same value.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'sizeof(int) != i' is always false because 'sizeof(int)' and 'i' represent the same value.\n", errout.str());
check("void f(int a = 1) { if ( a != 1){}}\n"); check("void f(int a = 1) { if ( a != 1){}}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
@ -3931,21 +3931,21 @@ private:
" int a = 1;\n" " int a = 1;\n"
" if ( a != 1){} \n" " if ( a != 1){} \n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", errout.str());
check("void f() {\n" check("void f() {\n"
" int a = 1;\n" " int a = 1;\n"
" int b = 1;\n" " int b = 1;\n"
" if ( a != b){} \n" " if ( a != b){} \n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != b' is always false because 'a' and 'b' represent the same value.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != b' is always false because 'a' and 'b' represent the same value.\n", errout.str());
check("void f() {\n" check("void f() {\n"
" int a = 1;\n" " int a = 1;\n"
" int b = a;\n" " int b = a;\n"
" if ( a != b){} \n" " if ( a != b){} \n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != b' is always false because 'a' and 'b' represent the same value.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != b' is always false because 'a' and 'b' represent the same value.\n", errout.str());
check("void use(int);\n" check("void use(int);\n"
"void f() {\n" "void f() {\n"
@ -3954,7 +3954,7 @@ private:
" use(b);\n" " use(b);\n"
" if ( a != 1){} \n" " if ( a != 1){} \n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (style) The expression 'a != 1' is always false.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (style) The expression 'a != 1' is always false.\n", errout.str());
check("void use(int);\n" check("void use(int);\n"
"void f() {\n" "void f() {\n"
@ -3978,7 +3978,7 @@ private:
" void f() {\n" " void f() {\n"
" if ( a != 1){} \n" " if ( a != 1){} \n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", errout.str());
check("int a = 1;\n" check("int a = 1;\n"
" void f() {\n" " void f() {\n"
@ -3990,7 +3990,7 @@ private:
" static const int a = 1;\n" " static const int a = 1;\n"
" if ( a != 1){} \n" " if ( a != 1){} \n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", errout.str());
check("void f() {\n" check("void f() {\n"
" static int a = 1;\n" " static int a = 1;\n"
@ -4003,7 +4003,7 @@ private:
" if ( a != 1){\n" " if ( a != 1){\n"
" a++;\n" " a++;\n"
" }}\n"); " }}\n");
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", errout.str());
check("void f(int b) {\n" check("void f(int b) {\n"
" int a = 1;\n" " int a = 1;\n"
@ -4086,7 +4086,7 @@ private:
" int a = 1;\n" " int a = 1;\n"
" while ( a != 1){}\n" " while ( a != 1){}\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", errout.str());
check("void f() { int a = 1; while ( a != 1){ a++; }}\n"); check("void f() { int a = 1; while ( a != 1){ a++; }}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
@ -4102,7 +4102,7 @@ private:
" if( i != 0 ) {}\n" " if( i != 0 ) {}\n"
" }\n" " }\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'i != 0' is always false.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'i != 0' is always false.\n", errout.str());
check("void f() {\n" check("void f() {\n"
" for(int i = 0; i < 10;) {\n" " for(int i = 0; i < 10;) {\n"
@ -4143,7 +4143,7 @@ private:
" b++;\n" " b++;\n"
" }\n" " }\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != 1' is always false.\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != 1' is always false.\n", errout.str());
} }
void duplicateExpressionTernary() { // #6391 void duplicateExpressionTernary() { // #6391