From 98bf11235258496b3b8166d369eb91ba4b19f827 Mon Sep 17 00:00:00 2001 From: IOBYTE Date: Thu, 28 Feb 2019 02:30:04 -0500 Subject: [PATCH] template simplifier: fix recursive variable templates (#1711) --- lib/templatesimplifier.cpp | 4 ++- test/testsimplifytemplate.cpp | 65 ++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 8ad583207..cd11b9c6f 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1406,8 +1406,10 @@ void TemplateSimplifier::expandTemplate( dst->insertToken(newName, "", true); if (start->strAt(1) == "<") start = start->next()->findClosingBracket(); - } else + } else { dst->insertToken(start->str(), "", true); + mTemplateInstantiations.emplace_back(dst->previous(), templateDeclaration.scope); + } } else { // check if type is a template if (start->strAt(1) == "<") { diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 6122ec2d1..1d3c9456a 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -2094,33 +2094,44 @@ private: } void template96() { // #7854 - const char code[] = "template\n" - " constexpr long fib = fib + fib;\n" - "template<>\n" - " constexpr long fib<0> = 0;\n" - "template<>\n" - " constexpr long fib<1> = 1;\n" - "long f0 = fib<0>;\n" - "long f1 = fib<1>;\n" - "long f2 = fib<2>;\n" - "long f3 = fib<3>;"; - const char act[] = "const long fib<2> = fib < 1 > + fib < 0 > ; " - "const long fib<3> = fib < 2 > + fib < 1 > ; " - "const long fib<0> = 0 ; " - "const long fib<1> = 1 ; " - "long f0 ; f0 = fib<0> ; " - "long f1 ; f1 = fib<1> ; " - "long f2 ; f2 = fib<2> ; " - "long f3 ; f3 = fib<3> ;"; - const char exp[] = "const long fib<2> = fib<1> + fib<0> ; " - "const long fib<3> = fib<2> + fib<1> ; " - "const long fib<0> = 0 ; " - "const long fib<1> = 1 ; " - "long f0 ; f0 = fib<0> ; " - "long f1 ; f1 = fib<1> ; " - "long f2 ; f2 = fib<2> ; " - "long f3 ; f3 = fib<3> ;"; - TODO_ASSERT_EQUALS(exp, act, tok(code)); + { + const char code[] = "template\n" + " constexpr long fib = fib + fib;\n" + "template<>\n" + " constexpr long fib<0> = 0;\n" + "template<>\n" + " constexpr long fib<1> = 1;\n" + "long f0 = fib<0>;\n" + "long f1 = fib<1>;\n" + "long f2 = fib<2>;\n" + "long f3 = fib<3>;"; + const char exp[] = "const long fib<2> = fib<1> + fib<0> ; " + "const long fib<3> = fib<2> + fib<1> ; " + "const long fib<0> = 0 ; " + "const long fib<1> = 1 ; " + "long f0 ; f0 = fib<0> ; " + "long f1 ; f1 = fib<1> ; " + "long f2 ; f2 = fib<2> ; " + "long f3 ; f3 = fib<3> ;"; + ASSERT_EQUALS(exp, tok(code)); + } + { + const char code[] = "template\n" + " constexpr long fib = fib + fib;\n" + "template<>\n" + " constexpr long fib<0> = 0;\n" + "template<>\n" + " constexpr long fib<1> = 1;\n" + "long f5 = fib<5>;\n"; + const char exp[] = "const long fib<5> = fib<4> + fib<3> ; " + "const long fib<4> = fib<3> + fib<2> ; " + "const long fib<3> = fib<2> + fib<1> ; " + "const long fib<2> = fib<1> + fib<0> ; " + "const long fib<0> = 0 ; " + "const long fib<1> = 1 ; " + "long f5 ; f5 = fib<5> ;"; + ASSERT_EQUALS(exp, tok(code)); + } } void template97() {