templates: Quick fix for the problem with default value for a template argument

This commit is contained in:
Daniel Marjamäki 2009-09-03 21:46:07 +02:00
parent 401f8aaa96
commit 2719724a97
2 changed files with 53 additions and 4 deletions

View File

@ -564,6 +564,55 @@ void Tokenizer::simplifyTemplates()
if (used.empty())
return;
// Template arguments with default values
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
{
Token *eq = 0;
std::string pattern;
for (Token *tok = *iter1; tok; tok = tok->next())
{
if (tok->str() == ">")
break;
if (tok->str() == ",")
{
if (pattern.empty())
pattern = " < ";
else
pattern += "%any% , ";
}
if (tok->str() == "=")
{
if (Token::Match(tok, "= %any% > class %var% {"))
{
pattern = tok->strAt(4) + pattern + "%any% >";
eq = tok;
}
break;
}
}
if (!eq || pattern.empty())
continue;
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
{
if (Token::Match(*iter2, pattern.c_str()))
{
Token *tok = *iter2;
while (tok->next()->str() != ">")
tok = tok->next();
tok->insertToken(eq->strAt(1));
tok->insertToken(",");
}
}
eq->deleteThis();
eq->deleteThis();
}
// expand templates
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
{

View File

@ -929,7 +929,7 @@ private:
"}\n";
// The expected result..
const std::string expected(" template < class T , int n = 3 >"
const std::string expected(" template < class T , int n >"
" class A"
" { T ar [ n ] ; } ;"
" void f ( )"
@ -938,10 +938,10 @@ private:
" A<int,3> a2 ;"
" }"
" class A<int,2>"
" { int ar[2]; }"
" { int ar [ 2 ] ; }"
" class A<int,3>"
" { int ar[3]; }");
TODO_ASSERT_EQUALS(expected, sizeof_(code));
" { int ar [ 3 ] ; }");
ASSERT_EQUALS(expected, sizeof_(code));
}