Ticket #5625: Simplify constant ternary operator in template parameters.

This commit is contained in:
Simon Martin 2014-10-04 20:49:57 +02:00
parent b48bf1dbad
commit 9ddf857dc7
3 changed files with 18 additions and 3 deletions

View File

@ -246,8 +246,8 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
continue;
}
// Skip '='
if (tok && tok->str() == "=")
// Skip '=', '?', ':'
if (tok && Token::Match(tok, "=|?|:"))
tok = tok->next();
if (!tok)
return 0;

View File

@ -4862,7 +4862,7 @@ bool Tokenizer::simplifyConstTernaryOp()
else if (endTok->str() == "?")
++ternaryOplevel;
else if (Token::Match(endTok, ")|}|]|;|,|:")) {
else if (Token::Match(endTok, ")|}|]|;|,|:|>")) {
if (endTok->str() == ":" && ternaryOplevel)
--ternaryOplevel;
else {

View File

@ -254,6 +254,7 @@ private:
TEST_CASE(simplify_constants3);
TEST_CASE(simplify_constants4);
TEST_CASE(simplify_constants5);
TEST_CASE(simplify_constants6); // Ticket #5625: Ternary operator as template parameter
TEST_CASE(simplify_null);
TEST_CASE(simplifyMulAndParens); // Ticket #2784 + #3184
@ -3591,6 +3592,20 @@ private:
ASSERT_EQUALS("int buffer [ 10 ] ;\n\n\nx = 10 ;\ny = 10 ;", tokenizeAndStringify(code,true));
}
void simplify_constants6() { // Ticket #5625
const char code[] = "template < class T > struct foo ;\n"
"void bar ( ) {\n"
"foo < 1 ? 0 ? 1 : 6 : 2 > x ;\n"
"foo < 1 ? 0 : 2 > y ;\n"
"}";
const char exp [] = "template < class T > struct foo ;\n"
"void bar ( ) {\n"
"foo < 6 > x ;\n"
"foo < 0 > y ;\n"
"}";
ASSERT_EQUALS(exp, tokenizeAndStringify(code, true));
}
void simplify_null() {
{
const char code[] =