diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 59cb790b8..8ba563677 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7996,10 +7996,21 @@ void Tokenizer::simplifyStructDecl() { unsigned int count = 0; + // Skip simplification of unions in class definition + std::list skip; + skip.push_back(false); + for (Token *tok = _tokens; tok; tok = tok->next()) { Token *restart; + if (tok->str() == "{") + skip.push_back(!Token::Match(tok->previous(), "const|)")); + else if (tok->str() == "}" && !skip.empty()) + skip.pop_back(); + else if (!skip.empty() && skip.back() && tok->str() == "union") + continue; + // check for named struct/union if (Token::Match(tok, "struct|union %type% :|{")) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index f24b80ea0..3980d2c9a 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -213,6 +213,7 @@ private: TEST_CASE(vardecl12); TEST_CASE(vardecl_stl); TEST_CASE(vardecl_template); + TEST_CASE(vardecl_union); TEST_CASE(volatile_variables); TEST_CASE(syntax_error); TEST_CASE(syntax_error_templates); @@ -3536,6 +3537,13 @@ private: ASSERT_EQUALS(res1, tokenizeAndStringify(code1)); } + void vardecl_union() + { + // ticket #1976 + const char code1[] = "class Fred { public: union { int a ; int b ; } ; } ;"; + ASSERT_EQUALS(code1, tokenizeAndStringify(code1)); + } + void vardec_static() { {