Skip to content

Commit e8fbd39

Browse files
committed
Templates: better handling of 'X<class Y>' template instantiations. Ticket: cppcheck-opensource#4544
1 parent e03a394 commit e8fbd39

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

lib/templatesimplifier.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,17 +1092,19 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
10921092
else if (indentlevel > 0 && Token::Match(tok3, "> [,>]"))
10931093
--indentlevel;
10941094
templateMatchPattern += tok3->str();
1095-
templateMatchPattern += " ";
1095+
templateMatchPattern += ' ';
10961096
if (indentlevel == 0 && Token::Match(tok3->previous(), "[<,]"))
10971097
typesUsedInTemplateInstantiation.push_back(tok3);
10981098
// add additional type information
1099-
if (tok3->isUnsigned())
1100-
typeForNewNameStr += "unsigned";
1101-
else if (tok3->isSigned())
1102-
typeForNewNameStr += "signed";
1103-
if (tok3->isLong())
1104-
typeForNewNameStr += "long";
1105-
typeForNewNameStr += tok3->str();
1099+
if (tok3->str() != "class") {
1100+
if (tok3->isUnsigned())
1101+
typeForNewNameStr += "unsigned";
1102+
else if (tok3->isSigned())
1103+
typeForNewNameStr += "signed";
1104+
if (tok3->isLong())
1105+
typeForNewNameStr += "long";
1106+
typeForNewNameStr += tok3->str();
1107+
}
11061108
}
11071109
templateMatchPattern += ">";
11081110
const std::string typeForNewName(typeForNewNameStr);

test/testsimplifytokens.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class TestSimplifyTokens : public TestFixture {
128128
TEST_CASE(template34); // #3706 - namespace => hang
129129
TEST_CASE(template35); // #4074 - A<'x'> a;
130130
TEST_CASE(template36); // #4310 - passing unknown template instantiation as template argument
131+
TEST_CASE(template37); // #4544 - A<class B> a;
131132
TEST_CASE(template_unhandled);
132133
TEST_CASE(template_default_parameter);
133134
TEST_CASE(template_default_type);
@@ -2252,6 +2253,15 @@ class TestSimplifyTokens : public TestFixture {
22522253
tok(code));
22532254
}
22542255

2256+
void template37() { // #4544 - A<class B> a;
2257+
const char code[] = "class A { };\n"
2258+
"template<class T> class B {};\n"
2259+
"B<class A> b1;\n"
2260+
"B<A> b2;";
2261+
ASSERT_EQUALS("class A { } ; B<A> b1 ; B<A> b2 ; class B<A> { }",
2262+
tok(code));
2263+
}
2264+
22552265
void template_unhandled() {
22562266
// An unhandled template usage should be simplified..
22572267
ASSERT_EQUALS("x<int> ( ) ;", tok("x<int>();"));

0 commit comments

Comments
 (0)