From 290563b9640505d140684587e5c21e887d510495 Mon Sep 17 00:00:00 2001 From: IOBYTE Date: Mon, 15 Oct 2018 13:35:26 -0400 Subject: [PATCH] Fix specialized template regression. (#1425) * Fix specialized template regression. Only check for instantiation of template being processed rather than count of all instantiations. * Add 2 more tests. --- lib/templatesimplifier.cpp | 6 +++- test/testsimplifytemplate.cpp | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 68371b549..15778fcfe 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1704,8 +1704,12 @@ bool TemplateSimplifier::simplifyTemplateInstantiations( } // process uninstantiated templates + const std::list::iterator it = std::find_if(mTemplateInstantiations.begin(), + mTemplateInstantiations.end(), + FindName(templateDeclaration.name)); + // TODO: remove the specialized check and handle all uninstantiated templates someday. - if (mTemplateInstantiations.empty() && specialized) { + if (it == mTemplateInstantiations.end() && specialized) { simplifyCalculations(); Token * tok2 = const_cast(tok->tokAt(namepos)); diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index f3c29e042..cef7fa7f0 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -130,6 +130,7 @@ private: TEST_CASE(expandSpecialized1); TEST_CASE(expandSpecialized2); TEST_CASE(expandSpecialized3); // #8671 + TEST_CASE(expandSpecialized4); TEST_CASE(templateAlias1); TEST_CASE(templateAlias2); @@ -1818,6 +1819,58 @@ private: ASSERT_EQUALS(expected, tok(code)); } + void expandSpecialized4() { + { + const char code[] = "template<> class C { };\n" + "map m;"; + const char expected[] = "class C { } ; " + "map < int > m ;"; + ASSERT_EQUALS(expected, tok(code)); + } + { + const char code[] = "template<> class C { };\n" + "map m;\n" + "C c;"; + const char expected[] = "class C { } ; " + "map < int > m ; " + "C c ;"; + ASSERT_EQUALS(expected, tok(code)); + } + { + const char code[] = "template class C { };\n" + "template<> class C { };\n" + "map m;\n"; + const char expected[] = "template < typename T > class C { } ; " + "class C { } ; " + "map < int > m ;"; + ASSERT_EQUALS(expected, tok(code)); + } + { + const char code[] = "template class C { };\n" + "template<> class C { };\n" + "map m;\n" + "C i;"; + const char expected[] = "class C { } ; " + "map < int > m ; " + "C i ; " + "class C { } ;"; + ASSERT_EQUALS(expected, tok(code)); + } + { + const char code[] = "template class C { };\n" + "template<> class C { };\n" + "map m;\n" + "C i;\n" + "C c;"; + const char expected[] = "class C { } ; " + "map < int > m ; " + "C i ; " + "C c ; " + "class C { } ;"; + ASSERT_EQUALS(expected, tok(code)); + } + } + void templateAlias1() { const char code[] = "template struct Foo {};\n" "template using Bar = Foo;\n"