diff --git a/src/token.cpp b/src/token.cpp index ec9e9df33..1902080bc 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -59,7 +59,7 @@ void Token::str(const std::string &s) void Token::str(const char s[]) { - str(std::string(s)); + str(std::string(s)); } void Token::concatStr(std::string const& b) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 3256083cc..a844f5510 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -551,7 +551,7 @@ void Tokenizer::simplifyTemplates() if (!tok) break; } - else if (Token::Match(tok->previous(), "[{};] %var% <")) + else if (Token::Match(tok->previous(), "[{};=] %var% <")) { used.push_back(tok); } @@ -2199,14 +2199,14 @@ bool Tokenizer::simplifyVarDecl() int parlevel = 0; while (tok2) { - if (strchr("{(", tok2->str()[0])) + if (strchr("{(<", tok2->str()[0])) { ++parlevel; } - else if (strchr("})", tok2->str()[0])) + else if (strchr("})>", tok2->str()[0])) { - if (parlevel < 0) + if (parlevel <= 0) break; --parlevel; } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index b40baf6b3..c715604b1 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -94,6 +94,7 @@ private: TEST_CASE(template8); TEST_CASE(template9); TEST_CASE(template10); + TEST_CASE(template11); TEST_CASE(namespaces); @@ -848,6 +849,27 @@ private: ASSERT_EQUALS(expected, sizeof_(code)); } + void template11() + { + const char code[] = "template 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)); + } + diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 49d2f869f..16711bd8d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -143,6 +143,7 @@ private: TEST_CASE(vardecl1); TEST_CASE(vardecl2); + TEST_CASE(vardecl3); TEST_CASE(volatile_variables); TEST_CASE(syntax_error); @@ -2035,6 +2036,13 @@ private: 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() { const char code[] = "volatile int a=0;\n"