Fixed ticket #387 (Templates: template functions that return a pointer are not simplified correctly)

This commit is contained in:
Daniel Marjamäki 2009-06-14 11:28:25 +02:00
parent 2307395d6e
commit be52e573f3
2 changed files with 34 additions and 3 deletions

View File

@ -571,13 +571,23 @@ void Tokenizer::simplifyTemplates()
type.push_back(tok->str()); type.push_back(tok->str());
} }
// bail out if the end of the file was reached
if (!tok) if (!tok)
break; break;
// name of template function/class.. // if this is a template function, get the position of the function name
const std::string name(tok->strAt(2)); unsigned int pos = 0;
if (Token::Match(tok, "> %type% *| %var% ("))
pos = 2;
else if (Token::Match(tok, "> %type% %type% *| %var% ("))
pos = 3;
if (pos > 0 && tok->tokAt(pos)->str() == "*")
++pos;
const bool isfunc(tok->strAt(3)[0] == '('); // name of template function/class..
const std::string name(tok->strAt(pos > 0 ? pos : 2));
const bool isfunc(pos > 0);
// locate template usage.. // locate template usage..

View File

@ -93,6 +93,7 @@ private:
TEST_CASE(template7); TEST_CASE(template7);
TEST_CASE(template8); TEST_CASE(template8);
TEST_CASE(template9); TEST_CASE(template9);
TEST_CASE(template10);
TEST_CASE(namespaces); TEST_CASE(namespaces);
@ -826,6 +827,26 @@ private:
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
} }
void template10()
{
const char code[] = "template <int ui, typename T> T * foo()\n"
"{ return new T[ui]; }\n"
"\n"
"void f ( )\n"
"{\n"
" foo<3,int>();\n"
"}\n";
// The expected result..
const std::string expected(" template < int ui , typename T > T * foo ( )"
" { return new T [ ui ] ; }"
" void f ( )"
" {"
" foo<3,int> ( ) ;"
" }"
" int * foo<3,int> ( ) { return new int [ 3 ] ; }");
ASSERT_EQUALS(expected, sizeof_(code));
}