Partial fix #1464 (takes too long (days) to process some files)

Fix template functions that return "&T".
http://sourceforge.net/apps/trac/cppcheck/ticket/1464
This commit is contained in:
Reijo Tomperi 2010-03-03 23:02:32 +02:00
parent e207da18a2
commit d881fd7a31
2 changed files with 24 additions and 4 deletions

View File

@ -1621,7 +1621,6 @@ void Tokenizer::simplifyTemplates()
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
{
Token *tok = *iter1;
std::vector<std::string> type;
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next())
{
@ -1635,11 +1634,11 @@ void Tokenizer::simplifyTemplates()
// if this is a template function, get the position of the function name
unsigned int pos = 0;
if (Token::Match(tok, "> %type% *| %var% ("))
if (Token::Match(tok, "> %type% *|&| %var% ("))
pos = 2;
else if (Token::Match(tok, "> %type% %type% *| %var% ("))
else if (Token::Match(tok, "> %type% %type% *|&| %var% ("))
pos = 3;
if (pos > 0 && tok->tokAt(pos)->str() == "*")
if (pos > 0 && (tok->tokAt(pos)->str() == "*" || tok->tokAt(pos)->str() == "&"))
++pos;
if (_settings && _settings->_debug)

View File

@ -96,6 +96,7 @@ private:
TEST_CASE(template16);
TEST_CASE(template17);
TEST_CASE(template18);
TEST_CASE(template19);
TEST_CASE(template_default_parameter);
TEST_CASE(template_default_type);
TEST_CASE(template_typename);
@ -1508,6 +1509,26 @@ private:
ASSERT_EQUALS(expected, sizeof_(code));
}
void template19()
{
const char code[] = "template <typename T> T & foo()\n"
"{ static T temp; return temp; }\n"
"\n"
"void f ( )\n"
"{\n"
" char p = foo<char>();\n"
"}\n";
// The expected result..
const std::string expected("; "
"void f ( ) "
"{"
" char p ; p = foo<char> ( ) ; "
"} "
"char & foo<char> ( ) { static char temp ; return temp ; }");
ASSERT_EQUALS(expected, sizeof_(code));
}
void template_default_parameter()
{
{