From ea85dd305a69275c43cb007f9e014cabebc52644 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 28 Aug 2012 12:44:40 +0200 Subject: [PATCH] Fixed #4086: Set varId in initialization list. --- lib/tokenize.cpp | 35 ++++++++++++++++++++--------------- test/testtokenize.cpp | 14 ++++++++++++++ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c0c5bf866..29cf6d099 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2585,27 +2585,32 @@ static void setVarIdClassDeclaration(Token * const startToken, // replace varids.. unsigned int indentlevel = 0; + bool initList = false; for (Token *tok = startToken->next(); tok != endToken; tok = tok->next()) { - if (tok->str() == "{") + if (tok->str() == "{") { + initList = false; ++indentlevel; - else if (tok->str() == "}") + } else if (tok->str() == "}") --indentlevel; - else if (indentlevel > 0 && tok->isName() && tok->varId() <= scopeStartVarId) { - if (Token::Match(tok->previous(), "::|.")) - continue; - if (tok->next()->str() == "::") { - if (tok->str() == className) - tok = tok->tokAt(2); - else + else if (tok->isName() && tok->varId() <= scopeStartVarId) { + if (indentlevel > 0 || (initList && indentlevel == 0 && (tok->strAt(-1) == "," || tok->strAt(-1) == ":"))) { + if (Token::Match(tok->previous(), "::|.")) continue; - } + if (tok->next()->str() == "::") { + if (tok->str() == className) + tok = tok->tokAt(2); + else + continue; + } - const std::map::const_iterator it = variableId.find(tok->str()); - if (it != variableId.end()) { - tok->varId(it->second); - setVarIdStructMembers(&tok, structMembers, _varId); + const std::map::const_iterator it = variableId.find(tok->str()); + if (it != variableId.end()) { + tok->varId(it->second); + setVarIdStructMembers(&tok, structMembers, _varId); + } } - } + } else if (indentlevel == 0 && tok->str() == ":") + initList = true; } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index cd1eae898..97b864a56 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -243,6 +243,7 @@ private: TEST_CASE(varid_in_class6); // #3755 TEST_CASE(varid_in_class7); // set variable id for struct members TEST_CASE(varid_in_class8); // unknown macro in class + TEST_CASE(varid_initList); TEST_CASE(varid_operator); TEST_CASE(varid_throw); TEST_CASE(varid_unknown_macro); // #2638 - unknown macro is not type @@ -3777,6 +3778,19 @@ private: tokenizeDebugListing(code)); } + void varid_initList() { + const char code[] = "class A {\n" + " A() : x(0) {}\n" + " int x;\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: A ( ) : x@1 ( 0 ) { }\n" + "3: int x@1 ;\n" + "4: } ;\n", + tokenizeDebugListing(code)); + } + void varid_operator() { { const std::string actual = tokenizeDebugListing(