Skip to content

Commit b965cf5

Browse files
committed
Fixed danmar#7444 (Tokenizer::varId: Wrong varid when there is anonumous union in class)
1 parent bf8471e commit b965cf5

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

lib/tokenize.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,46 +2640,48 @@ void Tokenizer::setVarId()
26402640
functionDeclEndStack.push(newFunctionDeclEnd);
26412641
scopeInfo.push(variableId);
26422642
}
2643-
} else if (tok->str() == "{") {
2643+
} else if (Token::Match(tok, "{|}")) {
2644+
const Token * const startToken = (tok->str() == "{") ? tok : tok->link();
2645+
26442646
// parse anonymous unions as part of the current scope
2645-
if (!(initlist && Token::Match(tok->previous(), "%name%|>|>>") && Token::Match(tok->link(), "} ,|{"))) {
2646-
bool isExecutable;
2647-
if (tok->strAt(-1) == ")" || Token::Match(tok->tokAt(-2), ") %type%") ||
2648-
(initlist && tok->strAt(-1) == "}")) {
2649-
isExecutable = true;
2650-
} else {
2651-
isExecutable = ((scopeStack.top().isExecutable || initlist || tok->strAt(-1) == "else") &&
2652-
!isClassStructUnionEnumStart(tok));
2653-
scopeInfo.push(variableId);
2654-
}
2655-
initlist = false;
2656-
scopeStack.push(scopeStackEntryType(isExecutable, _varId));
2657-
}
2658-
} else if (tok->str() == "}") {
2659-
// parse anonymous unions/structs as part of the current scope
2660-
if (!(Token::simpleMatch(tok, "} ;") && tok->link() && Token::Match(tok->link()->previous(), "union|struct|enum {")) &&
2661-
!(initlist && Token::Match(tok, "} ,|{") && Token::Match(tok->link()->previous(), "%name%|>|>> {"))) {
2662-
bool isNamespace = false;
2663-
for (const Token *tok1 = tok->link()->previous(); tok1 && tok1->isName(); tok1 = tok1->previous())
2664-
isNamespace |= (tok1->str() == "namespace");
2665-
// Set variable ids in class declaration..
2666-
if (!initlist && !isC() && !scopeStack.top().isExecutable && tok->link() && !isNamespace) {
2667-
setVarIdClassDeclaration(tok->link(),
2668-
variableId,
2669-
scopeStack.top().startVarid,
2670-
structMembers);
2671-
}
2672-
2673-
if (scopeInfo.empty()) {
2674-
variableId.clear();
2675-
} else {
2676-
variableId.swap(scopeInfo.top());
2677-
scopeInfo.pop();
2678-
}
2647+
if (!Token::Match(startToken->previous(), "union|struct|enum {") &&
2648+
!(initlist && Token::Match(startToken->previous(), "%name%|>|>>") && Token::Match(startToken->link(), "} ,|{"))) {
2649+
2650+
if (tok->str() == "{") {
2651+
bool isExecutable;
2652+
if (tok->strAt(-1) == ")" || Token::Match(tok->tokAt(-2), ") %type%") ||
2653+
(initlist && tok->strAt(-1) == "}")) {
2654+
isExecutable = true;
2655+
} else {
2656+
isExecutable = ((scopeStack.top().isExecutable || initlist || tok->strAt(-1) == "else") &&
2657+
!isClassStructUnionEnumStart(tok));
2658+
scopeInfo.push(variableId);
2659+
}
2660+
initlist = false;
2661+
scopeStack.push(scopeStackEntryType(isExecutable, _varId));
2662+
} else { /* if (tok->str() == "}") */
2663+
bool isNamespace = false;
2664+
for (const Token *tok1 = tok->link()->previous(); tok1 && tok1->isName(); tok1 = tok1->previous())
2665+
isNamespace |= (tok1->str() == "namespace");
2666+
// Set variable ids in class declaration..
2667+
if (!initlist && !isC() && !scopeStack.top().isExecutable && tok->link() && !isNamespace) {
2668+
setVarIdClassDeclaration(tok->link(),
2669+
variableId,
2670+
scopeStack.top().startVarid,
2671+
structMembers);
2672+
}
26792673

2680-
scopeStack.pop();
2681-
if (scopeStack.empty()) { // should be impossible
2682-
scopeStack.push(scopeStackEntryType());
2674+
if (scopeInfo.empty()) {
2675+
variableId.clear();
2676+
} else {
2677+
variableId.swap(scopeInfo.top());
2678+
scopeInfo.pop();
2679+
}
2680+
2681+
scopeStack.pop();
2682+
if (scopeStack.empty()) { // should be impossible
2683+
scopeStack.push(scopeStackEntryType());
2684+
}
26832685
}
26842686
}
26852687
}

test/testvarid.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,18 @@ class TestVarID : public TestFixture {
16281628
"9: h ( a@1 , b@2 , c@3 , d@4 ) ;\n"
16291629
"10: }\n",
16301630
tokenize(code3));
1631+
1632+
// #7444
1633+
const char code4[] = "class Foo {\n"
1634+
" void f(float a) { this->a = a; }\n"
1635+
" union { float a; int b; };\n"
1636+
"};";
1637+
ASSERT_EQUALS("\n\n##file 0\n"
1638+
"1: class Foo {\n"
1639+
"2: void f ( float a@1 ) { this . a@2 = a@1 ; }\n"
1640+
"3: union { float a@2 ; int b@3 ; } ;\n"
1641+
"4: } ;\n",
1642+
tokenize(code4));
16311643
}
16321644

16331645
void varid_in_class12() { // #4637 - method

0 commit comments

Comments
 (0)