diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a6724ca8c..9091f4205 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 8063b0182..e5ac551f0 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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() diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 96e6dbda5..fa85cba44 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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()