Fixed #4291 (Variable ID is not set when variable is accessed through 'this')

This commit is contained in:
Baris Demiray 2012-10-19 06:18:13 +02:00 committed by Daniel Marjamäki
parent be719062b9
commit d84d360afc
4 changed files with 34 additions and 2 deletions

View File

@ -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;

View File

@ -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<std::string,unsigned int>::const_iterator it = varlist.find(tok2->str());
if (it != varlist.end()) {
tok2->varId(it->second);

View File

@ -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() {

View File

@ -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"