From f781f13997b8706899bfe9594dd9b3d526edd778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 7 Feb 2016 13:34:03 +0100 Subject: [PATCH] Fixed #6806 (wrong enum simplification in initialization list) --- lib/tokenize.cpp | 9 ++++----- test/testsimplifytokens.cpp | 7 +++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c7f043bc6..dea89aab2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7748,14 +7748,13 @@ void Tokenizer::simplifyEnum() } } else if (tok2->str() == "{") ++level; - else if (!pattern.empty() && ((tok2->str() == "enum" && Token::simpleMatch(tok2->next(), pattern.c_str())) || Token::simpleMatch(tok2, pattern.c_str()))) { + else if (!pattern.empty() && Token::Match(tok2, ("enum| " + pattern).c_str())) { simplify = true; hasClass = true; } else if (inScope && !exitThisScope && (tok2->str() == enumType->str() || (tok2->str() == "enum" && tok2->next() && tok2->next()->str() == enumType->str()))) { - if (tok2->strAt(-1) == "::") { - // Don't replace this enum if it's preceded by "::" - } else if (tok2->next() && - (tok2->next()->isName() || tok2->next()->str() == "(")) { + if (!Token::Match(tok2->previous(), "%op%|::|:") && + !Token::simpleMatch(tok2->tokAt(-2), ") ,") && + Token::Match(tok2->next(), "%name%|(")) { simplify = true; hasClass = false; } else if (tok2->previous()->str() == "(" && tok2->next()->str() == ")") { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 510a22a0c..fd7b35c06 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -200,6 +200,7 @@ private: TEST_CASE(enum42); // ticket #5182 (template function call in enum value) TEST_CASE(enum43); // lhs in assignment TEST_CASE(enum44); + TEST_CASE(enum45); // ticket #6806 (enum in init list) TEST_CASE(enumscope1); // ticket #3949 TEST_CASE(enumOriginalName) TEST_CASE(duplicateDefinition); // ticket #3565 @@ -3297,6 +3298,12 @@ private: ASSERT_EQUALS("( datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value ) ;", checkSimplifyEnum(code2)); } + void enum45() { // #6806 - enum in init list + const char code[] = "enum a {};\n" + "c::c() : a(0), a(0) {}"; + ASSERT_EQUALS("c :: c ( ) : a ( 0 ) , a ( 0 ) { }", 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; }";