From 3cf67014ad19723321d99cea84d8a2118e05b90d Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 14 Aug 2015 11:07:42 +0200 Subject: [PATCH] Fixed parsing of template in enum --- lib/tokenize.cpp | 17 +++++------------ test/testsimplifytokens.cpp | 11 +++++++++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 557537990..5e95dee49 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7585,18 +7585,11 @@ void Tokenizer::simplifyEnum() if (Token::Match(enumValueEnd, "(|[")) { enumValueEnd = enumValueEnd->link(); continue; - } else if (isCPP() && Token::Match(enumValueEnd, "%type% <") && TemplateSimplifier::templateParameters(enumValueEnd->next()) > 1U) { - Token *endtoken = enumValueEnd->next(); - do { - endtoken = endtoken->next(); - if (Token::Match(endtoken, "*|,|::|typename")) - endtoken = endtoken->next(); - if (endtoken->str() == "<" && TemplateSimplifier::templateParameters(endtoken)) - endtoken = endtoken->findClosingBracket(); - } while (Token::Match(endtoken, "%name%|%num% *| [,>]") || Token::Match(endtoken, "%name%|%num% ::|< %any%")); - if (endtoken->str() == ">") { + } else if (isCPP() && Token::Match(enumValueEnd, "%type% <") && TemplateSimplifier::templateParameters(enumValueEnd->next()) >= 1U) { + Token *endtoken = enumValueEnd->next()->findClosingBracket(); + if (endtoken) { enumValueEnd = endtoken; - if (Token::simpleMatch(endtoken, "> ( )")) + if (Token::Match(endtoken, ">|>> ( )")) enumValueEnd = enumValueEnd->next(); } else { syntaxError(enumValueEnd); @@ -8796,7 +8789,7 @@ void Tokenizer::simplifyConst() tok->swapWithNext(); } else if (Token::Match(tok, "%type% const") && (!tok->previous() || Token::Match(tok->previous(), "[;{}(,]")) && - tok->str().find(":") == std::string::npos && + tok->str().find(':') == std::string::npos && tok->str() != "operator") { tok->swapWithNext(); } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index ba4ef9753..a37b7bd70 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -194,6 +194,7 @@ private: TEST_CASE(enum41); // ticket #5212 (valgrind errors during enum simplification) TEST_CASE(enum42); // ticket #5182 (template function call in enum value) TEST_CASE(enum43); // lhs in assignment + TEST_CASE(enum44); TEST_CASE(enumscope1); // ticket #3949 TEST_CASE(duplicateDefinition); // ticket #3565 TEST_CASE(invalid_enum); // #5600 @@ -3409,6 +3410,16 @@ private: ASSERT_EQUALS("A = 1 ;", checkSimplifyEnum(code)); } + void enum44() { + const char code1[] = "enum format_t { YYYYMMDD = datemask_traits< datemask<'Y', 'Y', 'Y', 'Y', '/', 'M', 'M', '/', 'D', 'D'> >::value, };\n" + "YYYYMMDD;"; + ASSERT_EQUALS("( datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value ) ;", checkSimplifyEnum(code1)); + + const char code2[] = "enum format_t { YYYYMMDD = datemask_traits< datemask<'Y', 'Y', 'Y', 'Y', '/', 'M', 'M', '/', 'D', 'D'>>::value, };\n" + "YYYYMMDD;"; + ASSERT_EQUALS("( datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value ) ;", checkSimplifyEnum(code2)); + } + 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; }";