Templates: better handling of 'X<class Y>' template instantiations. Ticket: #4544

This commit is contained in:
Daniel Marjamäki 2013-04-11 18:58:39 +02:00
parent e03a3946d0
commit e8fbd39efd
2 changed files with 20 additions and 8 deletions

View File

@ -1092,10 +1092,11 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
else if (indentlevel > 0 && Token::Match(tok3, "> [,>]")) else if (indentlevel > 0 && Token::Match(tok3, "> [,>]"))
--indentlevel; --indentlevel;
templateMatchPattern += tok3->str(); templateMatchPattern += tok3->str();
templateMatchPattern += " "; templateMatchPattern += ' ';
if (indentlevel == 0 && Token::Match(tok3->previous(), "[<,]")) if (indentlevel == 0 && Token::Match(tok3->previous(), "[<,]"))
typesUsedInTemplateInstantiation.push_back(tok3); typesUsedInTemplateInstantiation.push_back(tok3);
// add additional type information // add additional type information
if (tok3->str() != "class") {
if (tok3->isUnsigned()) if (tok3->isUnsigned())
typeForNewNameStr += "unsigned"; typeForNewNameStr += "unsigned";
else if (tok3->isSigned()) else if (tok3->isSigned())
@ -1104,6 +1105,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
typeForNewNameStr += "long"; typeForNewNameStr += "long";
typeForNewNameStr += tok3->str(); typeForNewNameStr += tok3->str();
} }
}
templateMatchPattern += ">"; templateMatchPattern += ">";
const std::string typeForNewName(typeForNewNameStr); const std::string typeForNewName(typeForNewNameStr);

View File

@ -128,6 +128,7 @@ private:
TEST_CASE(template34); // #3706 - namespace => hang TEST_CASE(template34); // #3706 - namespace => hang
TEST_CASE(template35); // #4074 - A<'x'> a; TEST_CASE(template35); // #4074 - A<'x'> a;
TEST_CASE(template36); // #4310 - passing unknown template instantiation as template argument TEST_CASE(template36); // #4310 - passing unknown template instantiation as template argument
TEST_CASE(template37); // #4544 - A<class B> a;
TEST_CASE(template_unhandled); TEST_CASE(template_unhandled);
TEST_CASE(template_default_parameter); TEST_CASE(template_default_parameter);
TEST_CASE(template_default_type); TEST_CASE(template_default_type);
@ -2252,6 +2253,15 @@ private:
tok(code)); tok(code));
} }
void template37() { // #4544 - A<class B> a;
const char code[] = "class A { };\n"
"template<class T> class B {};\n"
"B<class A> b1;\n"
"B<A> b2;";
ASSERT_EQUALS("class A { } ; B<A> b1 ; B<A> b2 ; class B<A> { }",
tok(code));
}
void template_unhandled() { void template_unhandled() {
// An unhandled template usage should be simplified.. // An unhandled template usage should be simplified..
ASSERT_EQUALS("x<int> ( ) ;", tok("x<int>();")); ASSERT_EQUALS("x<int> ( ) ;", tok("x<int>();"));