Fixed #7211 (False positive: Finding the same expression on both sides of an operator (enumconstant == 0))

This commit is contained in:
Daniel Marjamäki 2016-01-10 11:21:43 +01:00
parent f47e05b6a7
commit b3208fb4b3
4 changed files with 15 additions and 5 deletions

View File

@ -110,7 +110,7 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
if (tok2->str() == "." && tok2->astOperand1() && tok2->astOperand1()->str() == "this")
tok2 = tok2->astOperand2();
}
if (tok1->varId() != tok2->varId() || tok1->str() != tok2->str()) {
if (tok1->varId() != tok2->varId() || tok1->str() != tok2->str() || tok1->originalName() != tok2->originalName()) {
if ((Token::Match(tok1,"<|>") && Token::Match(tok2,"<|>")) ||
(Token::Match(tok1,"<=|>=") && Token::Match(tok2,"<=|>="))) {
return isSameExpression(cpp, macro, tok1->astOperand1(), tok2->astOperand2(), constFunctions) &&
@ -118,8 +118,6 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
}
return false;
}
if (tok1->str() == "." && tok1->originalName() != tok2->originalName())
return false;
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro()))
return false;
if (tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof") {

View File

@ -7675,6 +7675,8 @@ void Tokenizer::simplifyEnum()
if (simplify) {
if (ev->value) {
if (tok2->originalName().empty())
tok2->originalName(tok2->str());
tok2->str(ev->value->str());
while (tok2->strAt(1) == "::")
tok2->deleteNext(2);

View File

@ -3771,7 +3771,7 @@ private:
" enum { Four = 4 };\n"
" if (Four == 4) {}"
"}", nullptr, false, true, false);
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (style) Same expression on both sides of '=='.\n", errout.str());
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" enum { Four = 4 };\n"
@ -3790,7 +3790,7 @@ private:
" enum { FourInEnumTwo = 4 };\n"
" if (FourInEnumOne == FourInEnumTwo) {}\n"
"}", nullptr, false, true, false);
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:4]: (style) Same expression on both sides of '=='.\n", errout.str());
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" enum { FourInEnumOne = 4 };\n"

View File

@ -206,6 +206,7 @@ private:
TEST_CASE(enum43); // lhs in assignment
TEST_CASE(enum44);
TEST_CASE(enumscope1); // ticket #3949
TEST_CASE(enumOriginalName)
TEST_CASE(duplicateDefinition); // ticket #3565
// remove "std::" on some standard functions
@ -3420,6 +3421,15 @@ private:
ASSERT_EQUALS("void foo ( ) { } void bar ( ) { int a ; a = A ; }", checkSimplifyEnum(code));
}
void enumOriginalName() {
Tokenizer tokenizer(&settings0, this);
std::istringstream istr("enum {X,Y}; x=X;");
tokenizer.tokenize(istr, "test.c");
Token *x_token = Token::findsimplematch(tokenizer.list.front(),"x");
ASSERT_EQUALS("0", x_token->strAt(2));
ASSERT_EQUALS("X", x_token->tokAt(2)->originalName());
}
void duplicateDefinition() { // #3565 - wrongly detects duplicate definition
Tokenizer tokenizer(&settings0, this);
std::istringstream istr("x ; return a not_eq x;");