From 8c57e2bb1440194f1acbd816e3a50101281670dc Mon Sep 17 00:00:00 2001 From: IOBYTE Date: Sun, 28 Feb 2021 15:52:14 -0500 Subject: [PATCH] fix template simplifier overloaded specialized instantiations (#3154) --- lib/templatesimplifier.cpp | 24 ++++++++++++------ test/testsimplifytemplate.cpp | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 4d5f57b3f..f6c865f58 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -2858,7 +2858,10 @@ bool TemplateSimplifier::matchSpecialization( if (!endToken) continue; while (declToken != endToken) { - if (declToken->str() != instToken->str()) { + if (declToken->str() != instToken->str() || + declToken->isSigned() != instToken->isSigned() || + declToken->isUnsigned() != instToken->isUnsigned() || + declToken->isLong() != instToken->isLong()) { int nr = 0; while (nr < templateParameters.size() && templateParameters[nr]->str() != declToken->str()) ++nr; @@ -2915,14 +2918,15 @@ std::string TemplateSimplifier::getNewName( } // add additional type information if (!constconst && !Token::Match(tok3, "class|struct|enum")) { - if (tok3->isUnsigned()) - typeForNewName += "unsigned"; - else if (tok3->isSigned()) - typeForNewName += "signed"; - if (tok3->isLong()) - typeForNewName += "long"; if (!typeForNewName.empty()) typeForNewName += ' '; + if (tok3->isUnsigned()) + typeForNewName += "unsigned "; + else if (tok3->isSigned()) + typeForNewName += "signed "; + if (tok3->isLong()) { + typeForNewName += "long "; + } typeForNewName += tok3->str(); } } @@ -3467,6 +3471,12 @@ void TemplateSimplifier::printOut(const TokenAndName &tokenAndName, const std::s const Token *start = tokenAndName.token()->next(); std::cout << indent << "type: "; while (start && start != end) { + if (start->isUnsigned()) + std::cout << "unsigned"; + else if (start->isSigned()) + std::cout << "signed"; + if (start->isLong()) + std::cout << "long"; std::cout << start->str(); start = start->next(); } diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 5fd7deaba..ecd4d813c 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -210,6 +210,7 @@ private: TEST_CASE(template165); // #10032 syntax error TEST_CASE(template166); // #10081 hang TEST_CASE(template167); + TEST_CASE(template168); TEST_CASE(template_specialization_1); // #7868 - template specialization template struct S> {..}; TEST_CASE(template_specialization_2); // #7868 - template specialization template struct S> {..}; TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template) @@ -4218,6 +4219,51 @@ private: ASSERT_EQUALS(exp, tok(code)); } + void template168() { + const char code[] = "template < typename T, typename U > struct type { };\n" + "template < > struct type < bool, bool > {};\n" + "template < > struct type < unsigned char, unsigned char > {};\n" + "template < > struct type < char, char > {};\n" + "template < > struct type < signed char, signed char > {};\n" + "template < > struct type < unsigned short, unsigned short > {};\n" + "template < > struct type < short, short > {};\n" + "template < > struct type < unsigned int, unsigned int > {};\n" + "template < > struct type < int, int > {};\n" + "template < > struct type < unsigned long long, unsigned long long > {};\n" + "template < > struct type < long long, long long > {};\n" + "template < > struct type < double, double > {};\n" + "template < > struct type < float, float > {};\n" + "template < > struct type < long double, long double > {};"; + const char exp[] = "struct type ; " + "struct type ; " + "struct type ; " + "struct type ; " + "struct type ; " + "struct type ; " + "struct type ; " + "struct type ; " + "struct type ; " + "struct type ; " + "struct type ; " + "struct type ; " + "struct type ; " + "template < typename T , typename U > struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ; " + "struct type { } ;"; + ASSERT_EQUALS(exp, tok(code)); + } + void template_specialization_1() { // #7868 - template specialization template struct S> {..}; const char code[] = "template struct C {};\n" "template struct S {a};\n"