template simplifier: class or typename can't be a name (#1875)

* template simplifier: class or typename can't be a name

* struct can't be a name
This commit is contained in:
IOBYTE 2019-06-08 01:27:53 -04:00 committed by Daniel Marjamäki
parent 0947a36c4f
commit 7a87786cbc
2 changed files with 22 additions and 3 deletions

View File

@ -1763,7 +1763,7 @@ void TemplateSimplifier::expandTemplate(
}
} else if (copy) {
bool added = false;
if (tok5->isName()) {
if (tok5->isName() && !Token::Match(tok5, "class|typename|struct")) {
// search for this token in the type vector
unsigned int itype = 0;
while (itype < typeParametersInDeclaration.size() && typeParametersInDeclaration[itype]->str() != tok5->str())
@ -1838,9 +1838,8 @@ void TemplateSimplifier::expandTemplate(
// FIXME use full name matching somehow
const std::string lastName = (templateInstantiation.name.find(' ') != std::string::npos) ? templateInstantiation.name.substr(templateInstantiation.name.rfind(' ')+1) : templateInstantiation.name;
for (; tok3; tok3 = tok3->next()) {
if (tok3->isName()) {
if (tok3->isName() && !Token::Match(tok3, "class|typename|struct")) {
// search for this token in the type vector
unsigned int itype = 0;
while (itype < typeParametersInDeclaration.size() && typeParametersInDeclaration[itype]->str() != tok3->str())

View File

@ -150,6 +150,7 @@ private:
TEST_CASE(template110);
TEST_CASE(template111); // crash
TEST_CASE(template112); // #9146 syntax error
TEST_CASE(template113);
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@ -2642,6 +2643,25 @@ private:
ASSERT_EQUALS(exp, tok(code));
}
void template113() {
{
const char code[] = "template <class> class A { void f(); };\n"
"A<int> a;";
const char exp[] = "class A<int> ; "
"A<int> a ; "
"class A<int> { void f ( ) ; } ;";
ASSERT_EQUALS(exp, tok(code));
}
{
const char code[] = "template <struct> struct A { void f(); };\n"
"A<int> a;";
const char exp[] = "struct A<int> ; "
"A<int> a ; "
"struct A<int> { void f ( ) ; } ;";
ASSERT_EQUALS(exp, tok(code));
}
}
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
const char code[] = "template <typename T> struct C {};\n"
"template <typename T> struct S {a};\n"