From 5fef7cc050b50722bc3d95c162eb45b74d6dd484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 7 Feb 2016 15:15:20 +0100 Subject: [PATCH] Fixed #4973 (wrong enum simplification of shadow struct variable) --- lib/tokenize.cpp | 8 ++++---- test/testsimplifytokens.cpp | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8d3692c40..8a8d06a07 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7549,10 +7549,11 @@ void Tokenizer::simplifyEnum() bool inScope = !enumClass; // enum class objects are always in a different scope std::stack > shadowId; // duplicate ids in inner scope - bool simplify = false; - const EnumValue *ev = nullptr; for (Token *tok2 = tok1->next(); tok2; tok2 = tok2->next()) { + bool simplify = false; + const EnumValue *ev = nullptr; + if (tok2->str() == "}") { --level; if (level < 0) @@ -7640,6 +7641,7 @@ void Tokenizer::simplifyEnum() } } else if (inScope && // enum is in scope (shadowId.empty() || shadowId.top().find(tok2->str()) == shadowId.top().end()) && // no shadow enum/var/etc of enum + !Token::Match(tok2->previous(), "} %name% ;") && enumValues.find(tok2->str()) != enumValues.end()) { // tok2 is a enum id with a known value ev = &(enumValues.find(tok2->str())->second); if (!duplicateDefinition(&tok2)) { @@ -7697,8 +7699,6 @@ void Tokenizer::simplifyEnum() tok2 = tok2->next(); } } - - simplify = false; } } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 80f83cf59..8e14fc0b9 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -202,6 +202,7 @@ private: TEST_CASE(enum44); TEST_CASE(enum45); // ticket #6806 (enum in init list) TEST_CASE(enum46); // ticket #4625 (shadow declaration) + TEST_CASE(enum47); // ticket #4973 (wrong simplification in shadow struct variable declaration) TEST_CASE(enumscope1); // ticket #3949 TEST_CASE(enumOriginalName) TEST_CASE(duplicateDefinition); // ticket #3565 @@ -3311,6 +3312,12 @@ private: ASSERT_EQUALS("class c { } ;", checkSimplifyEnum(code)); } + void enum47() { // #4973 - wrong simplification in shadow struct variable declaration + const char code[] = "enum e {foo};\n" + "union { struct { } foo; };"; + ASSERT_EQUALS("union { struct Anonymous0 { } ; struct Anonymous0 foo ; } ;", checkSimplifyEnum(code)); + } + void enumscope1() { // #3949 - don't simplify enum from one function in another function const char code[] = "void foo() { enum { A = 0, B = 1 }; }\n" "void bar() { int a = A; }";