From d84d360afcfef4461d106b89b24fad7ea710e4d0 Mon Sep 17 00:00:00 2001 From: Baris Demiray Date: Fri, 19 Oct 2012 06:18:13 +0200 Subject: [PATCH] Fixed #4291 (Variable ID is not set when variable is accessed through 'this') --- lib/checkother.cpp | 2 +- lib/tokenize.cpp | 2 +- test/testother.cpp | 10 ++++++++++ test/testtokenize.cpp | 22 ++++++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 94cd1679b..501e5b309 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1034,7 +1034,7 @@ void CheckOther::checkSelfAssignment() const char selfAssignmentPattern[] = "%var% = %var% ;|=|)"; const Token *tok = Token::findmatch(_tokenizer->tokens(), selfAssignmentPattern); while (tok) { - if (Token::Match(tok->previous(), "[;{}]") && + if (Token::Match(tok->previous(), "[;{}.]") && tok->varId() && tok->varId() == tok->tokAt(2)->varId() && isTypeWithoutSideEffects(_tokenizer, symbolDatabase->getVariableFromVarId(tok->varId()))) { bool err = true; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e94fbc09d..c243b138d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2624,7 +2624,7 @@ static void setVarIdClassFunction(Token * const startToken, unsigned int *_varId) { for (Token *tok2 = startToken; tok2 && tok2 != endToken; tok2 = tok2->next()) { - if (tok2->varId() == 0 && tok2->previous()->str() != ".") { + if (tok2->varId() == 0) { const std::map::const_iterator it = varlist.find(tok2->str()); if (it != varlist.end()) { tok2->varId(it->second); diff --git a/test/testother.cpp b/test/testother.cpp index 757162d1b..513c4ff8a 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2791,6 +2791,16 @@ private: " i = i;\n" "}"); ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'i' to itself.\n", errout.str()); + + // #4291 - id for variables accessed through 'this' + check("class Foo {\n" + " int var;\n" + " void func();\n" + "};\n" + "void Foo::func() {\n" + " this->var = var;\n" + "}"); + ASSERT_EQUALS("[test.cpp:6]: (warning) Redundant assignment of 'var' to itself.\n", errout.str()); } void trac1132() { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index e32424ebc..d1b25a7ed 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -250,6 +250,7 @@ private: TEST_CASE(varid_in_class6); // #3755 TEST_CASE(varid_in_class7); // set variable id for struct members TEST_CASE(varid_in_class8); // unknown macro in class + TEST_CASE(varid_in_class9); // #4291 - id for variables accessed through 'this' TEST_CASE(varid_initList); TEST_CASE(varid_operator); TEST_CASE(varid_throw); @@ -3889,6 +3890,27 @@ private: tokenizeDebugListing(code)); } + void varid_in_class9() { // #4291 - id for variables accessed through 'this' + const char code[] = "class A {\n" + " int var;\n" + "public:\n" + " void setVar();\n" + "};\n" + "void A::setVar() {\n" + " this->var = var;\n" + "}"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: int var@1 ;\n" + "3: public:\n" + "4: void setVar ( ) ;\n" + "5: } ;\n" + "6: void A :: setVar ( ) {\n" + "7: this . var@1 = var@1 ;\n" + "8: }\n", + tokenizeDebugListing(code)); + } + void varid_initList() { const char code[] = "class A {\n" " A() : x(0) {}\n"