diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index cc8ae10e3..cdf7c38c3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2639,6 +2639,17 @@ static void setVarIdClassFunction(Token * const startToken, } } +static bool isInitList(const Token *tok) +{ + if (!Token::Match(tok, ") : %var% (")) + return false; + + tok = tok->linkAt(3); + while (Token::Match(tok, ") , %var% (")) + tok = tok->linkAt(3); + + return Token::Match(tok, ") {"); +} void Tokenizer::setVarId() { @@ -2666,13 +2677,16 @@ void Tokenizer::setVarId() executableScope.push(false); std::stack scopestartvarid; // varid when scope starts scopestartvarid.push(0); + bool initlist = false; for (Token *tok = list.front(); tok; tok = tok->next()) { // scope info to handle shadow variables.. - if (tok->str() == "(" && - (Token::simpleMatch(tok->link(), ") {") || Token::Match(tok->link(), ") %type% {"))) { + if (!initlist && tok->str() == "(" && + (Token::simpleMatch(tok->link(), ") {") || Token::Match(tok->link(), ") %type% {") || isInitList(tok->link()))) { scopeInfo.push(variableId); + initlist = Token::simpleMatch(tok->link(), ") :"); } else if (tok->str() == "{") { + initlist = false; // parse anonymous unions as part of the current scope if (!(Token::simpleMatch(tok->previous(), "union") && Token::simpleMatch(tok->link(), "} ;"))) { scopestartvarid.push(_varId); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index a2e75d4b9..50df41166 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4086,6 +4086,17 @@ private: "4: } ;\n" "5: A :: A ( int x@3 ) : x@2 ( x@3 ) { }\n", tokenizeDebugListing(code3)); + + const char code4[] = "struct A {\n" + " int x;\n" + " A(int x) : x(x) {}\n" + "};\n"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: struct A {\n" + "2: int x@1 ;\n" + "3: A ( int x@2 ) : x@1 ( x@2 ) { }\n" + "4: } ;\n", + tokenizeDebugListing(code4)); } void varid_operator() {