From 8087cfed5dd00f6330cbbec8f56bb58598dfabc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 2 Dec 2018 09:28:05 +0100 Subject: [PATCH] Fixed #8627 (Tokenizer::setVarIdPass2: constructor parameter) --- lib/tokenize.cpp | 7 ++++++- test/testvarid.cpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9e344ae8b..30456ff78 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3093,8 +3093,13 @@ void Tokenizer::setVarIdPass2() if (tok2->strAt(-1) == ")" || tok2->strAt(-2) == ")") setVarIdClassFunction(scopeName2 + classname, tok2, tok2->link(), thisClassVars, structMembers, &mVarId); tok2 = tok2->link(); - } else if (tok2->str() == "(" && tok2->link()->strAt(1) != "(") + } else if (tok2->str() == "(" && tok2->link()->strAt(1) != "(") { tok2 = tok2->link(); + + // Skip initialization list + while (Token::Match(tok2, ") [:,] %name% (")) + tok2 = tok2->linkAt(3); + } } // Found a member variable.. diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 74278f950..3ea1b597e 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -129,6 +129,7 @@ private: TEST_CASE(varid_in_class21); // #7788 TEST_CASE(varid_namespace_1); // #7272 TEST_CASE(varid_namespace_2); // #7000 + TEST_CASE(varid_namespace_3); // #8627 TEST_CASE(varid_initList); TEST_CASE(varid_initListWithBaseTemplate); TEST_CASE(varid_initListWithScope); @@ -1825,6 +1826,41 @@ private: ASSERT(actual.find("X@2 = 0") != std::string::npos); } + std::string getLine(const std::string &code, int lineNumber) { + std::string nr = MathLib::toString(lineNumber); + const std::string::size_type pos1 = code.find('\n' + nr + ": "); + if (pos1 == std::string::npos) + return ""; + const std::string::size_type pos2 = code.find('\n', pos1+1); + if (pos2 == std::string::npos) + return ""; + return code.substr(pos1+1, pos2-pos1-1); + } + + void varid_namespace_3() { // #8627 + const char code[] = "namespace foo {\n" + "struct Bar {\n" + " explicit Bar(int type);\n" + " void f();\n" + " int type;\n" // <- Same varid here ... + "};\n" + "\n" + "Bar::Bar(int type) : type(type) {}\n" + "\n" + "void Bar::f() {\n" + " type = 0;\n" // <- ... and here + "}\n" + "}"; + + const std::string actual = tokenize(code, false, "test.cpp"); + + const std::string line5 = getLine(actual, 5); + const std::string line11 = getLine(actual, 11); + + ASSERT_EQUALS("5: int type@2 ;", getLine(actual,5)); + ASSERT_EQUALS("11: type@2 = 0 ;", getLine(actual,11)); + } + void varid_initList() { const char code1[] = "class A {\n" " A() : x(0) {}\n"