Fixed #6406 (VarId: struct member initialization)

This commit is contained in:
Daniel Marjamäki 2016-05-22 21:18:52 +02:00
parent 9ea49d48d1
commit 710e066a9a
3 changed files with 47 additions and 10 deletions

View File

@ -2460,6 +2460,34 @@ static void setVarIdStructMembers(Token **tok1,
unsigned int *_varId)
{
Token *tok = *tok1;
if (Token::Match(tok, "%name% = { . %name% =")) {
const unsigned int struct_varid = tok->varId();
if (struct_varid == 0)
return;
std::map<std::string, unsigned int>& members = structMembers[struct_varid];
tok = tok->tokAt(3);
while (tok->str() != "}") {
while (Token::Match(tok, "{|[|("))
tok = tok->link()->next();
if (Token::Match(tok->previous(), "[,{] . %name% =")) {
tok = tok->next();
const std::map<std::string, unsigned int>::iterator it = members.find(tok->str());
if (it == members.end()) {
members[tok->str()] = ++(*_varId);
tok->varId(*_varId);
} else {
tok->varId(it->second);
}
}
tok = tok->next();
}
return;
}
while (Token::Match(tok->next(), ". %name% !!(")) {
const unsigned int struct_varid = tok->varId();
tok = tok->tokAt(2);

View File

@ -3007,16 +3007,6 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
// #6406 - false positive for struct with designated initializer
check("struct callbacks {\n"
" void (*something)(void);\n"
"};\n"
"void something(void) {}\n"
"void f() {\n"
" struct callbacks ops = { .something = something };\n"
"}\n");
TODO_ASSERT_EQUALS("", "[test.cpp:6]: (warning) Redundant assignment of 'something' to itself.\n", errout.str());
// #6406 - designated initializer doing bogus self assignment
check("struct callbacks {\n"
" void (*something)(void);\n"

View File

@ -148,6 +148,7 @@ private:
TEST_CASE(varid_inheritedMembers); // #4101
TEST_CASE(varid_header); // #6386
TEST_CASE(varid_rangeBasedFor);
TEST_CASE(varid_structinit); // #6406
TEST_CASE(varidclass1);
TEST_CASE(varidclass2);
@ -2442,6 +2443,24 @@ private:
"}", false, "test.c"));
}
void varid_structinit() { // #6406
ASSERT_EQUALS("\n\n##file 0\n"
"1: void foo ( ) {\n"
"2: struct ABC abc@1 ; abc@1 = { . a@2 = 0 , . b@3 = 1 } ;\n"
"3: }\n",
tokenize("void foo() {\n"
" struct ABC abc = {.a=0,.b=1};\n"
"}"));
ASSERT_EQUALS("\n\n##file 0\n"
"1: void foo ( ) {\n"
"2: struct ABC abc@1 ; abc@1 = { . a@2 = abc@1 . a@2 , . b@3 = abc@1 . b@3 } ;\n"
"3: }\n",
tokenize("void foo() {\n"
" struct ABC abc = {.a=abc.a,.b=abc.b};\n"
"}"));
}
void varidclass1() {
const std::string actual = tokenize(
"class Fred\n"