From a434e0fb1a996cbce6cdf89c33bd8338dde21561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 19 Jan 2016 14:32:27 +0100 Subject: [PATCH] Fixed #7272 (Tokenizer:setVarId: handle namespaces better) --- lib/tokenize.cpp | 5 ++++- test/testvarid.cpp | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 177914241..9112e6b28 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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, diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 1cc5800ee..015833354 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -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"