From b3208fb4b3d0c16327277712b328d19324c14690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 10 Jan 2016 11:21:43 +0100 Subject: [PATCH] Fixed #7211 (False positive: Finding the same expression on both sides of an operator (enumconstant == 0)) --- lib/astutils.cpp | 4 +--- lib/tokenize.cpp | 2 ++ test/testother.cpp | 4 ++-- test/testsimplifytokens.cpp | 10 ++++++++++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index b7f3229dc..5222d3bd7 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -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") { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ac49b45be..d6cfeb7bb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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); diff --git a/test/testother.cpp b/test/testother.cpp index 1c190d113..cd7106469 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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" diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 4a263d3c6..5bdffd24e 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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;");