diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0b2b2d122..576da850c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2931,15 +2931,15 @@ void Tokenizer::setVarId() } // constructor with initializer list - if (Token::Match(tok2, ") : %name% (")) { + if (Token::Match(tok2, ") : %name% (|{")) { Token *tok3 = tok2; do { Token *vartok = tok3->tokAt(2); if (varlist[classname].find(vartok->str()) != varlist[classname].end()) vartok->varId(varlist[classname][vartok->str()]); tok3 = tok3->linkAt(3); - } while (Token::Match(tok3, ") [:,] %name% (")); - if (Token::simpleMatch(tok3, ") {")) { + } while (Token::Match(tok3, ")|} [:,] %name% (|{")); + if (Token::Match(tok3, ")|} {")) { setVarIdClassFunction(classname, tok2, tok3->next()->link(), varlist[classname], &structMembers, &_varId); } } diff --git a/test/testclass.cpp b/test/testclass.cpp index 88989709e..f289cf6bb 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -6007,6 +6007,22 @@ private: "};"); ASSERT_EQUALS("[test.cpp:3]: (error) Member variable 's' is initialized by itself.\n", errout.str()); + checkSelfInitialization("class Fred {\n" + " int x;\n" + " Fred(int x);\n" + "};\n" + "Fred::Fred(int x) : x(x) { }\n" + ); + ASSERT_EQUALS("", errout.str()); + + checkSelfInitialization("class Fred {\n" + " int x;\n" + " Fred(int x);\n" + "};\n" + "Fred::Fred(int x) : x{x} { }\n" + ); + ASSERT_EQUALS("", errout.str()); + checkSelfInitialization("class Fred {\n" " std::string s;\n" " Fred(const std::string& s) : s(s) {\n" @@ -6014,6 +6030,13 @@ private: "};"); ASSERT_EQUALS("", errout.str()); + checkSelfInitialization("class Fred {\n" + " std::string s;\n" + " Fred(const std::string& s) : s{s} {\n" + " }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + checkSelfInitialization("struct Foo : Bar {\n" " int i;\n" " Foo(int i)\n" diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 97be75bc2..c473b2c24 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -156,6 +156,7 @@ private: TEST_CASE(varidclass15); // initializer list TEST_CASE(varidclass16); // #4577 TEST_CASE(varidclass17); // #6073 + TEST_CASE(varidclass18); TEST_CASE(varid_classnameshaddowsvariablename); // #3990 TEST_CASE(varidnamespace1); @@ -2541,6 +2542,23 @@ private: ASSERT_EQUALS(expected, tokenize(code)); } + void varidclass18() { + const char code[] = "class A {\n" + " int a;\n" + " int b;\n" + " A();\n" + "};\n" + "A::A() : a{0} { b = 1; }"; + const char expected[] = "\n\n##file 0\n" + "1: class A {\n" + "2: int a@1 ;\n" + "3: int b@2 ;\n" + "4: A ( ) ;\n" + "5: } ;\n" + "6: A :: A ( ) : a@1 { 0 } { b@2 = 1 ; }\n"; + ASSERT_EQUALS(expected, tokenize(code)); + } + void varid_classnameshaddowsvariablename() { const char code[] = "class Data;\n" "void strange_declarated(const Data& Data);\n"