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

* Fixed so that the tokenizer handle variable declarations better when the variable is assigned the return value of a template function
 * Fixed so that the simplifyTemplates detect that a template function is used when its return value is taken
This commit is contained in:
Daniel Marjamäki 2009-06-14 14:57:47 +02:00
parent 7bbdc8382a
commit af7c63155e
4 changed files with 35 additions and 5 deletions

View File

@ -551,7 +551,7 @@ void Tokenizer::simplifyTemplates()
if (!tok) if (!tok)
break; break;
} }
else if (Token::Match(tok->previous(), "[{};] %var% <")) else if (Token::Match(tok->previous(), "[{};=] %var% <"))
{ {
used.push_back(tok); used.push_back(tok);
} }
@ -2199,14 +2199,14 @@ bool Tokenizer::simplifyVarDecl()
int parlevel = 0; int parlevel = 0;
while (tok2) while (tok2)
{ {
if (strchr("{(", tok2->str()[0])) if (strchr("{(<", tok2->str()[0]))
{ {
++parlevel; ++parlevel;
} }
else if (strchr("})", tok2->str()[0])) else if (strchr("})>", tok2->str()[0]))
{ {
if (parlevel < 0) if (parlevel <= 0)
break; break;
--parlevel; --parlevel;
} }

View File

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

View File

@ -143,6 +143,7 @@ private:
TEST_CASE(vardecl1); TEST_CASE(vardecl1);
TEST_CASE(vardecl2); TEST_CASE(vardecl2);
TEST_CASE(vardecl3);
TEST_CASE(volatile_variables); TEST_CASE(volatile_variables);
TEST_CASE(syntax_error); TEST_CASE(syntax_error);
@ -2035,6 +2036,13 @@ private:
ASSERT_EQUALS("void foo ( a , b ) unsigned int a ; unsigned int b ; { }", actual); ASSERT_EQUALS("void foo ( a , b ) unsigned int a ; unsigned int b ; { }", actual);
} }
void vardecl3()
{
const char code[] = "void f() { char * p = foo<10,char>(); }";
const std::string actual(tokenizeAndStringify(code));
ASSERT_EQUALS("void f ( ) { char * p ; p = foo < 10 , char > ( ) ; }", actual);
}
void volatile_variables() void volatile_variables()
{ {
const char code[] = "volatile int a=0;\n" const char code[] = "volatile int a=0;\n"