diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 93615f682..e826f560f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2647,8 +2647,8 @@ void Tokenizer::setVarId() scopeStack.push(scopeStackEntryType(isExecutable, _varId)); } } else if (tok->str() == "}") { - // parse anonymous unions as part of the current scope - if (!(Token::simpleMatch(tok, "} ;") && tok->link() && Token::simpleMatch(tok->link()->previous(), "union {")) && + // 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%|>|>> {"))) { // Set variable ids in class declaration.. if (!initlist && !isC() && !scopeStack.top().isExecutable && tok->link()) { diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 0dbeb0013..c303f6a4d 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -1545,6 +1545,29 @@ private: "3: union { float a@1 ; int b@2 ; } ;\n" "4: } ;\n", tokenize(code2)); + + const char code3[] = "void f() {\n" + " union {\n" + " struct {\n" + " char a, b, c, d;\n" + " };\n" + " int abcd;\n" + " };\n" + " g(abcd);\n" + " h(a, b, c, d);\n" + "}"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: void f ( ) {\n" + "2: union {\n" + "3: struct {\n" + "4: char a@1 ; char b@2 ; char c@3 ; char d@4 ;\n" + "5: } ;\n" + "6: int abcd@5 ;\n" + "7: } ;\n" + "8: g ( abcd@5 ) ;\n" + "9: h ( a@1 , b@2 , c@3 , d@4 ) ;\n" + "10: }\n", + tokenize(code3)); } void varid_in_class12() { // #4637 - method