From 31f8ff723b3a611013bc80b632bc26b0bc9858c2 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Sun, 27 Mar 2011 18:37:31 -0400 Subject: [PATCH] skip nested anonymous unions when searching for variables in symbol database --- lib/symboldatabase.cpp | 5 +++++ test/testconstructors.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 1f9c788a1..449a84660 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1447,6 +1447,11 @@ void Scope::getVariableList() else break; } + else if (Token::Match(tok, "union {") && Token::Match(tok->next()->link(), "} %var% ;")) + { + tok = tok->next()->link()->next()->next(); + continue; + } // Borland C++: Skip all variables in the __published section. // These are automatically initialized. diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index cd40067bc..4c85ed0be 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -73,6 +73,7 @@ private: TEST_CASE(initvar_2constructors); // BUG 2270353 TEST_CASE(initvar_constvar); TEST_CASE(initvar_staticvar); + TEST_CASE(initvar_union); TEST_CASE(initvar_private_constructor); // BUG 2354171 - private constructor TEST_CASE(initvar_copy_constructor); // ticket #1611 @@ -795,6 +796,38 @@ private: } + void initvar_union() + { + check("class Fred\n" + "{\n" + " union\n" + " {\n" + " int a;\n" + " char b[4];\n" + " } U;\n" + "public:\n" + " Fred()\n" + " {\n" + " U.a = 0;\n" + " }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + + check("class Fred\n" + "{\n" + " union\n" + " {\n" + " int a;\n" + " char b[4];\n" + " } U;\n" + "public:\n" + " Fred()\n" + " {\n" + " }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:9]: (warning) Member variable 'Fred::U' is not initialised in the constructor.\n", errout.str()); + } + void initvar_private_constructor() {