Fixed #7272 (Tokenizer:setVarId: handle namespaces better)

This commit is contained in:
Daniel Marjamäki 2016-01-19 14:32:27 +01:00
parent 516f043a43
commit a434e0fb1a
2 changed files with 23 additions and 1 deletions

View File

@ -2679,8 +2679,11 @@ void Tokenizer::setVarId()
// parse anonymous unions/structs as part of the current scope
if (!(Token::simpleMatch(tok, "} ;") && tok->link() && Token::Match(tok->link()->previous(), "union|struct {")) &&
!(initlist && Token::Match(tok, "} ,|{") && Token::Match(tok->link()->previous(), "%name%|>|>> {"))) {
bool isNamespace = false;
for (const Token *tok1 = tok->link()->previous(); tok1 && tok1->isName(); tok1 = tok1->previous())
isNamespace |= (tok1->str() == "namespace");
// Set variable ids in class declaration..
if (!initlist && !isC() && !scopeStack.top().isExecutable && tok->link()) {
if (!initlist && !isC() && !scopeStack.top().isExecutable && tok->link() && !isNamespace) {
setVarIdClassDeclaration(tok->link(),
variableId,
scopeStack.top().startVarid,

View File

@ -122,6 +122,7 @@ private:
TEST_CASE(varid_in_class18); // #7127
TEST_CASE(varid_in_class19);
TEST_CASE(varid_in_class20); // #7267
TEST_CASE(varid_namespace); // #7272
TEST_CASE(varid_initList);
TEST_CASE(varid_initListWithBaseTemplate);
TEST_CASE(varid_initListWithScope);
@ -1827,6 +1828,24 @@ private:
"8: template < class C > cacheEntry < C > :: cacheEntry ( ) : m_key@1 ( ) { }\n", tokenize(code, false, "test.cpp"));
}
void varid_namespace() { // #7272
const char code[] = "namespace Blah {\n"
" struct foo { int x;};\n"
" struct bar {\n"
" int x;\n"
" union { char y; };\n"
" };\n"
"}";
ASSERT_EQUALS("\n\n##file 0\n"
"1: namespace Blah {\n"
"2: struct foo { int x@1 ; } ;\n"
"3: struct bar {\n"
"4: int x@2 ;\n"
"5: union { char y@3 ; } ;\n"
"6: } ;\n"
"7: }\n", tokenize(code, false, "test.cpp"));
}
void varid_initList() {
const char code1[] = "class A {\n"
" A() : x(0) {}\n"