diff --git a/src/tokenize.cpp b/src/tokenize.cpp index a01dd8940..3256083cc 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -571,13 +571,23 @@ void Tokenizer::simplifyTemplates() type.push_back(tok->str()); } + // bail out if the end of the file was reached if (!tok) break; - // name of template function/class.. - const std::string name(tok->strAt(2)); + // if this is a template function, get the position of the function name + 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.. diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 52f80e1c5..b40baf6b3 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -93,6 +93,7 @@ private: TEST_CASE(template7); TEST_CASE(template8); TEST_CASE(template9); + TEST_CASE(template10); TEST_CASE(namespaces); @@ -826,6 +827,26 @@ private: ASSERT_EQUALS(expected, sizeof_(code)); } + void template10() + { + const char code[] = "template 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)); + }