Skip to content

Commit 752e9d0

Browse files
committed
Tokenizer::varId: Fixed wrong varid for shadow types with same names as class members
1 parent 1be30bf commit 752e9d0

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

lib/tokenize.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,19 +2330,25 @@ static void setVarIdClassDeclaration(Token * const startToken,
23302330

23312331
// Update the variable ids..
23322332
// Parse each function..
2333-
static void setVarIdClassFunction(Token * const startToken,
2333+
static void setVarIdClassFunction(const std::string &classname,
2334+
Token * const startToken,
23342335
const Token * const endToken,
23352336
const std::map<std::string, unsigned int> &varlist,
23362337
std::map<unsigned int, std::map<std::string,unsigned int> > *structMembers,
23372338
unsigned int *_varId)
23382339
{
23392340
for (Token *tok2 = startToken; tok2 && tok2 != endToken; tok2 = tok2->next()) {
2340-
if (tok2->varId() == 0 && (tok2->previous()->str() != "." || tok2->strAt(-2) == "this")) {
2341-
const std::map<std::string,unsigned int>::const_iterator it = varlist.find(tok2->str());
2342-
if (it != varlist.end()) {
2343-
tok2->varId(it->second);
2344-
setVarIdStructMembers(&tok2, structMembers, _varId);
2345-
}
2341+
if (tok2->varId() != 0 || !tok2->isName())
2342+
continue;
2343+
if (Token::Match(tok2->tokAt(-2), ("!!"+classname+" :: ").c_str()))
2344+
continue;
2345+
if (Token::Match(tok2->tokAt(-2), "!!this . "))
2346+
continue;
2347+
2348+
const std::map<std::string,unsigned int>::const_iterator it = varlist.find(tok2->str());
2349+
if (it != varlist.end()) {
2350+
tok2->varId(it->second);
2351+
setVarIdStructMembers(&tok2, structMembers, _varId);
23462352
}
23472353
}
23482354
}
@@ -2594,7 +2600,7 @@ void Tokenizer::setVarId()
25942600
if (Token::Match(tok2, ") const|volatile| {")) {
25952601
while (tok2->str() != "{")
25962602
tok2 = tok2->next();
2597-
setVarIdClassFunction(tok2, tok2->link(), varlist, &structMembers, &_varId);
2603+
setVarIdClassFunction(classname, tok2, tok2->link(), varlist, &structMembers, &_varId);
25982604
}
25992605

26002606
// constructor with initializer list
@@ -2607,7 +2613,7 @@ void Tokenizer::setVarId()
26072613
tok3 = tok3->linkAt(3);
26082614
}
26092615
if (Token::simpleMatch(tok3, ") {")) {
2610-
setVarIdClassFunction(tok2, tok3->next()->link(), varlist, &structMembers, &_varId);
2616+
setVarIdClassFunction(classname, tok2, tok3->next()->link(), varlist, &structMembers, &_varId);
26112617
}
26122618
}
26132619
}

test/testtokenize.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ class TestTokenizer : public TestFixture {
282282
TEST_CASE(varid_in_class11); // #4277 - anonymous union
283283
TEST_CASE(varid_in_class12); // #4637 - method
284284
TEST_CASE(varid_in_class13); // #4637 - method
285+
TEST_CASE(varid_in_class14);
285286
TEST_CASE(varid_initList);
286287
TEST_CASE(varid_operator);
287288
TEST_CASE(varid_throw);
@@ -4451,6 +4452,24 @@ class TestTokenizer : public TestFixture {
44514452
tokenizeDebugListing(code2, false, "test.cpp"));
44524453
}
44534454

4455+
void varid_in_class14() {
4456+
const char code[] = "class Tokenizer { TokenList list; };\n"
4457+
"\n"
4458+
"void Tokenizer::f() {\n"
4459+
" std::list<int> x;\n" // <- not member variable
4460+
" list.do_something();\n" // <- member variable
4461+
" Tokenizer::list.do_something();\n" // <- redundant scope info
4462+
"}\n";
4463+
ASSERT_EQUALS("\n\n##file 0\n"
4464+
"1: class Tokenizer { TokenList list@1 ; } ;\n"
4465+
"2:\n"
4466+
"3: void Tokenizer :: f ( ) {\n"
4467+
"4: std :: list < int > x@2 ;\n"
4468+
"5: list@1 . do_something ( ) ;\n"
4469+
"6: Tokenizer :: list@1 . do_something ( ) ;\n"
4470+
"7: }\n", tokenizeDebugListing(code, false, "test.cpp"));
4471+
}
4472+
44544473
void varid_initList() {
44554474
const char code1[] = "class A {\n"
44564475
" A() : x(0) {}\n"

0 commit comments

Comments
 (0)