From 3f257d63103a455c4c28d920bc447ecb7e61b7be Mon Sep 17 00:00:00 2001 From: IOBYTE Date: Mon, 25 Feb 2019 15:01:34 -0500 Subject: [PATCH] =?UTF-8?q?template=20simplifier:=20instantiate=20template?= =?UTF-8?q?=20class=20when=20something=20inside=E2=80=A6=20(#1695)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * template simplifier: instantiate template class when something inside class instantiated. * template simplifier: add output to another test that now works --- lib/templatesimplifier.cpp | 4 +- test/testsimplifytemplate.cpp | 73 ++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 21 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index f61a1905f..e4eec3f6a 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -2211,7 +2211,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations( } if (Token::Match(startToken->previous(), ";|{|}|=|const") && - (!specialized && !instantiateMatch(tok2, typeParametersInDeclaration.size(), isfunc ? "(" : isVar ? ";|%op%" : "*|&| %name%"))) + (!specialized && !instantiateMatch(tok2, typeParametersInDeclaration.size(), isfunc ? "(" : isVar ? ";|%op%" : "*|&|::| %name%"))) continue; // New type.. @@ -2274,7 +2274,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations( } if (Token::Match(startToken->previous(), ";|{|}|=|const") && - (!specialized && !instantiateMatch(tok2, typeParametersInDeclaration.size(), isfunc ? "(" : isVar ? ";|%op%" : "*|&| %name%"))) + (!specialized && !instantiateMatch(tok2, typeParametersInDeclaration.size(), isfunc ? "(" : isVar ? ";|%op%" : "*|&|::| %name%"))) return false; // already simplified diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index cc381901c..be559d001 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -309,19 +309,21 @@ private: " return 0;\n" "}\n"; - const char wanted[] = "template < typename T > class ABC { public: } ; " + const char wanted[] = "class ABC ; " "int main ( ) { " "std :: vector < int > v ; " "v . push_back ( 4 ) ; " "return 0 ; " - "}"; + "} " + "class ABC { public: } ;"; - const char current[] = "template < typename T > class ABC { public: } ; " + const char current[] = "class ABC ; " "int main ( ) { " - "ABC < int > :: type v ; " + "ABC :: type v ; " "v . push_back ( 4 ) ; " "return 0 ; " - "}"; + "} " + "class ABC { public: } ;"; TODO_ASSERT_EQUALS(wanted, current, tok(code)); } @@ -1043,13 +1045,25 @@ private: } void template45() { // #5814 - tok("namespace Constants { const int fourtytwo = 42; } " - "template struct TypeMath { " - " static const int mult = sizeof(T) * U; " - "}; " - "template struct FOO { " - " enum { value = TypeMath::something }; " - "};"); + const char code[] = "namespace Constants { const int fourtytwo = 42; } " + "template struct TypeMath { " + " static const int mult = sizeof(T) * U; " + "}; " + "template struct FOO { " + " enum { value = TypeMath::mult }; " + "}; " + "FOO foo;"; + const char expected[] = "namespace Constants { const int fourtytwo = 42 ; } " + "struct TypeMath ; " + "struct FOO ; " + "FOO foo ; " + "struct FOO { " + "enum Anonymous0 { value = TypeMath :: mult } ; " + "} ; " + "struct TypeMath { " + "static const int mult = sizeof ( int ) * Constants :: fourtytwo ; " + "} ;"; + ASSERT_EQUALS(expected, tok(code, false, true)); ASSERT_EQUALS("", errout.str()); } @@ -1160,13 +1174,34 @@ private: } void template53() { // #4335 - tok("template struct Factorial { " - " enum { value = N * Factorial::value }; " - "};" - "template <> struct Factorial<0> { " - " enum { value = 1 }; " - "};" - "const int x = Factorial<4>::value;", /*simplify=*/true, /*debugwarnings=*/true); + const char code[] = "template struct Factorial { " + " enum { value = N * Factorial::value }; " + "};" + "template <> struct Factorial<0> { " + " enum { value = 1 }; " + "};" + "const int x = Factorial<4>::value;"; + const char expected[] = "struct Factorial<4> ; " + "struct Factorial<3> ; " + "struct Factorial<2> ; " + "struct Factorial<1> ; " + "struct Factorial<0> { " + "enum Anonymous1 { value = 1 } ; " + "} ; " + "const int x = Factorial<4> :: value ; " + "struct Factorial<4> { " + "enum Anonymous0 { value = 4 * Factorial<3> :: value } ; " + "} ; " + "struct Factorial<3> { " + "enum Anonymous0 { value = 3 * Factorial<2> :: value } ; " + "} ; " + "struct Factorial<2> { " + "enum Anonymous0 { value = 2 * Factorial<1> :: value } ; " + "} ; " + "struct Factorial<1> { " + "enum Anonymous0 { value = 1 * Factorial<0> :: value } ; " + "} ;"; + ASSERT_EQUALS(expected, tok(code, false, true)); ASSERT_EQUALS("", errout.str()); }