Fix uniform init in out-of-class ctor-initer

This commit is contained in:
Antti Tuppurainen 2015-07-14 18:09:07 +02:00 committed by Daniel Marjamäki
parent 405a1fba11
commit a75872a15e
3 changed files with 44 additions and 3 deletions

View File

@ -2931,15 +2931,15 @@ void Tokenizer::setVarId()
} }
// constructor with initializer list // constructor with initializer list
if (Token::Match(tok2, ") : %name% (")) { if (Token::Match(tok2, ") : %name% (|{")) {
Token *tok3 = tok2; Token *tok3 = tok2;
do { do {
Token *vartok = tok3->tokAt(2); Token *vartok = tok3->tokAt(2);
if (varlist[classname].find(vartok->str()) != varlist[classname].end()) if (varlist[classname].find(vartok->str()) != varlist[classname].end())
vartok->varId(varlist[classname][vartok->str()]); vartok->varId(varlist[classname][vartok->str()]);
tok3 = tok3->linkAt(3); tok3 = tok3->linkAt(3);
} while (Token::Match(tok3, ") [:,] %name% (")); } while (Token::Match(tok3, ")|} [:,] %name% (|{"));
if (Token::simpleMatch(tok3, ") {")) { if (Token::Match(tok3, ")|} {")) {
setVarIdClassFunction(classname, tok2, tok3->next()->link(), varlist[classname], &structMembers, &_varId); setVarIdClassFunction(classname, tok2, tok3->next()->link(), varlist[classname], &structMembers, &_varId);
} }
} }

View File

@ -6007,6 +6007,22 @@ private:
"};"); "};");
ASSERT_EQUALS("[test.cpp:3]: (error) Member variable 's' is initialized by itself.\n", errout.str()); 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" checkSelfInitialization("class Fred {\n"
" std::string s;\n" " std::string s;\n"
" Fred(const std::string& s) : s(s) {\n" " Fred(const std::string& s) : s(s) {\n"
@ -6014,6 +6030,13 @@ private:
"};"); "};");
ASSERT_EQUALS("", errout.str()); 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" checkSelfInitialization("struct Foo : Bar {\n"
" int i;\n" " int i;\n"
" Foo(int i)\n" " Foo(int i)\n"

View File

@ -156,6 +156,7 @@ private:
TEST_CASE(varidclass15); // initializer list TEST_CASE(varidclass15); // initializer list
TEST_CASE(varidclass16); // #4577 TEST_CASE(varidclass16); // #4577
TEST_CASE(varidclass17); // #6073 TEST_CASE(varidclass17); // #6073
TEST_CASE(varidclass18);
TEST_CASE(varid_classnameshaddowsvariablename); // #3990 TEST_CASE(varid_classnameshaddowsvariablename); // #3990
TEST_CASE(varidnamespace1); TEST_CASE(varidnamespace1);
@ -2541,6 +2542,23 @@ private:
ASSERT_EQUALS(expected, tokenize(code)); 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() { void varid_classnameshaddowsvariablename() {
const char code[] = "class Data;\n" const char code[] = "class Data;\n"
"void strange_declarated(const Data& Data);\n" "void strange_declarated(const Data& Data);\n"