Fixed #2039 (unions not handled properly, false positive about initialization)

This commit is contained in:
Daniel Marjamäki 2010-09-09 20:15:00 +02:00
parent 929a54e1b0
commit 0afd19c59b
3 changed files with 49 additions and 6 deletions

View File

@ -8114,6 +8114,40 @@ void Tokenizer::simplifyStructDecl()
// unnamed anonymous struct/union so remove it
else if (tok->next()->str() == ";")
{
if (tok1->str() == "union")
{
// Try to create references in the union..
Token *tok2 = tok1->tokAt(2);
while (tok2)
{
if (Token::Match(tok2, "%type% %var% ;"))
tok2 = tok2->tokAt(3);
else
break;
}
if (!Token::simpleMatch(tok2, "} ;"))
continue;
Token *vartok = 0;
tok2 = tok1->tokAt(2);
while (Token::Match(tok2, "%type% %var% ;"))
{
if (!vartok)
{
vartok = tok2->next();
tok2 = tok2->tokAt(3);
}
else
{
tok2->insertToken("&");
tok2 = tok2->tokAt(2);
tok2->insertToken(vartok->str());
tok2->next()->varId(vartok->varId());
tok2->insertToken("=");
tok2 = tok2->tokAt(4);
}
}
}
tok1->deleteThis();
if (tok1->next() == tok)
{

View File

@ -3438,6 +3438,15 @@ private:
// ticket #1976
const char code1[] = "class Fred { public: union { int a ; int b ; } ; } ;";
ASSERT_EQUALS(code1, tokenizeAndStringify(code1));
// ticket #2039
const char code2[] = "void f() {\n"
" union {\n"
" int x;\n"
" long y;\n"
" };\n"
"}";
ASSERT_EQUALS("void f ( ) {\n\nint x ;\nlong & y = x ;\n\n}", tokenizeAndStringify(code2));
}
void vardec_static()

View File

@ -2211,17 +2211,17 @@ private:
" func();\n"
" } while(a--);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: x\n"
"[test.cpp:4]: (style) Unused variable: z\n", errout.str());
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: x\n"
"[test.cpp:4]: (style) Unused variable: z\n", errout.str());
}
void localvarStruct4()
{
/* This must not SIGSEGV: */
// FIXME!!
//functionVariableUsage("void foo()\n"
// "{\n"
// " struct { \n");
functionVariableUsage("void foo()\n"
"{\n"
" struct { \n");
}
void localvarOp()