Fixed #497 (Templates are not expanded correctly)

This commit is contained in:
Daniel Marjamäki 2009-07-22 13:29:42 +02:00
parent 3243933c90
commit 1d514e1afe
2 changed files with 58 additions and 3 deletions

View File

@ -1942,7 +1942,25 @@ bool Tokenizer::simplifyQuestionMark()
tok->previous()->str() == "0")
{
// Use code after semicolon, remove code before it.
const Token *end = Token::findmatch(tok, ":");
const Token *end = 0;
unsigned int parlevel = 0;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (tok2->str() == "(")
++parlevel;
else if (tok2->str() == ")")
{
if (parlevel == 0)
break;
--parlevel;
}
else if (parlevel == 0 && tok2->str() == ":")
{
end = tok2;
break;
}
}
if (!end || !end->next())
continue;

View File

@ -96,6 +96,7 @@ private:
TEST_CASE(template9);
TEST_CASE(template10);
TEST_CASE(template11);
TEST_CASE(template12);
TEST_CASE(namespaces);
@ -889,6 +890,30 @@ private:
ASSERT_EQUALS(expected, sizeof_(code));
}
void template12()
{
const char code[] = "template <int x, int y, int z>\n"
"class A : public B<x, y, (x - y) ? ((y < z) ? 1 : -1) : 0>\n"
"{ };\n"
"\n"
"void f()\n"
"{\n"
" A<12,12,11> a;\n"
"}\n";
// The expected result..
const std::string expected(" template < int x , int y , int z >"
" class A : public B < x , y , ( x - y ) ? ( ( y < z ) ? 1 : - 1 ) : 0 >"
" { } ;"
" void f ( )"
" {"
" A<12,12,11> a ;"
" }"
" class A<12,12,11> : public B < 12 , 12 , 0 >"
" { }");
ASSERT_EQUALS(expected, sizeof_(code));
}
@ -1052,10 +1077,22 @@ private:
}
void conditionOperator()
{
{
const char code[] = "; x = a ? b : c;";
ASSERT_EQUALS("; if ( a ) { x = b ; } else { x = c ; }", tok(code));
}
{
const char code[] = "(0?(false?1:2):3)";
ASSERT_EQUALS("( 3 )", tok(code));
}
{
const char code[] = "(1?(false?1:2):3)";
TODO_ASSERT_EQUALS("( 2 )", tok(code));
}
}
};
REGISTER_TEST(TestSimplifyTokens)