Skip to content

Commit f8cf64a

Browse files
committed
Fixed cppcheck-opensource#4710 (Template parser bug: const in constructor changes template names)
1 parent 4e003c3 commit f8cf64a

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

lib/templatesimplifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ std::list<Token *> TemplateSimplifier::getTemplateInstantiations(Token *tokens)
473473
if (!tok)
474474
break;
475475
} else if (Token::Match(tok->previous(), "[({};=] %var% <") ||
476+
Token::Match(tok->previous(), "%type% %var% <") ||
476477
Token::Match(tok->tokAt(-2), "[,:] private|protected|public %var% <")) {
477478

478479
// Add inner template instantiations first => go to the ">"

test/testsimplifytokens.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class TestSimplifyTokens : public TestFixture {
133133
TEST_CASE(template38); // #4832 - crash on C++11 right angle brackets
134134
TEST_CASE(template39); // #4742 - freeze
135135
TEST_CASE(template40); // #5055 - template specialization outside struct
136+
TEST_CASE(template41); // #4710 - const in instantiation not handled perfectly
136137
TEST_CASE(template_unhandled);
137138
TEST_CASE(template_default_parameter);
138139
TEST_CASE(template_default_type);
@@ -2338,6 +2339,16 @@ class TestSimplifyTokens : public TestFixture {
23382339
ASSERT_EQUALS("struct A { template < typename T > struct X { T t ; } ; } ;", tok(code));
23392340
}
23402341

2342+
void template41() { // #4710 - const in template instantiation not handled perfectly
2343+
const char code1[] = "template<class T> struct X { };\n"
2344+
"void f(const X<int> x) { }";
2345+
ASSERT_EQUALS("void f ( const X<int> x ) { } struct X<int> { }", tok(code1));
2346+
2347+
const char code2[] = "template<class T> T f(T t) { return t; }\n"
2348+
"int x() { return f<int>(123); }";
2349+
ASSERT_EQUALS("int x ( ) { return f<int> ( 123 ) ; } int f<int> ( int t ) { return t ; }", tok(code2));
2350+
}
2351+
23412352
void template_default_parameter() {
23422353
{
23432354
const char code[] = "template <class T, int n=3>\n"

test/testsymboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ class TestSymbolDatabase: public TestFixture {
12791279
"public:\n"
12801280
" int f() { return C< ::D,int>::f(); }\n"
12811281
"};");
1282-
ASSERT_EQUALS("", errout.str());
1282+
ASSERT_EQUALS("[test.cpp:1]: (debug) simplifyTemplates: bailing out\n", errout.str());
12831283
}
12841284

12851285
void symboldatabase8() {

0 commit comments

Comments
 (0)