diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 454fc2b7e..ac49b45be 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2954,20 +2954,28 @@ void Tokenizer::setVarId() tok2 = tok2->linkAt(2); // constructor with initializer list - if (Token::Match(tok2, ") : %name% (|{|<")) { + if (Token::Match(tok2, ") : %name%")) { Token *tok3 = tok2; - do { - Token *vartok = tok3->tokAt(2); - std::map::const_iterator varpos = thisClassVars.find(vartok->str()); + while (Token::Match(tok3, "[)}] [,:]")) { + tok3 = tok3->tokAt(2); + while (Token::Match(tok3, "%name% :: %name%")) + tok3 = tok3->tokAt(2); + if (!Token::Match(tok3, "%name% (|{|<")) + break; + + // set varid + std::map::const_iterator varpos = thisClassVars.find(tok3->str()); if (varpos != thisClassVars.end()) - vartok->varId(varpos->second); - if (vartok->strAt(1) == "<") { - tok3 = vartok->next()->findClosingBracket(); + tok3->varId(varpos->second); + + // goto end of var + if (tok3->strAt(1) == "<") { + tok3 = tok3->next()->findClosingBracket(); if (tok3 && tok3->next() && tok3->next()->link()) tok3 = tok3->next()->link(); } else - tok3 = vartok->linkAt(1); - } while (Token::Match(tok3, ")|} [:,] %name% (|{|<")); + tok3 = tok3->linkAt(1); + } if (Token::Match(tok3, ")|} {")) { setVarIdClassFunction(classname, tok2, tok3->next()->link(), thisClassVars, structMembers, &_varId); } diff --git a/test/testvarid.cpp b/test/testvarid.cpp index ef5c979ca..c8122491f 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -123,6 +123,7 @@ private: TEST_CASE(varid_in_class19); TEST_CASE(varid_initList); TEST_CASE(varid_initListWithBaseTemplate); + TEST_CASE(varid_initListWithScope); TEST_CASE(varid_operator); TEST_CASE(varid_throw); TEST_CASE(varid_unknown_macro); // #2638 - unknown macro is not type @@ -1977,6 +1978,19 @@ private: tokenize(code5)); } + void varid_initListWithScope() { + const char code1[] = "class A : public B::C {\n" + " A() : B::C(), x(0) {}\n" + " int x;\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A : public B :: C {\n" + "2: A ( ) : B :: C ( ) , x@1 ( 0 ) { }\n" + "3: int x@1 ;\n" + "4: } ;\n", + tokenize(code1)); + } + void varid_operator() { { const std::string actual = tokenize(