diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 62410c6b3..87195727e 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -982,6 +982,17 @@ void TemplateSimplifier::expandTemplate( Token::Match(tok3, "%name% <") && fullName == getFullName(scopeInfo, tok3->str()) && TemplateSimplifier::instantiateMatch(tok3, typeParametersInDeclaration.size(), ":: ~| %name% (")) { + // there must be template.. + bool istemplate = false; + for (const Token *prev = tok3; prev && !Token::Match(prev, "[;{}]"); prev = prev->previous()) { + if (prev->str() == "template") { + istemplate = true; + break; + } + } + if (!istemplate) + continue; + const Token *tok4 = tok3->next()->findClosingBracket(); while (tok4 && tok4->str() != "(") tok4 = tok4->next(); diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 708df4962..327e4e6f1 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -99,6 +99,7 @@ private: TEST_CASE(template59); // #8051 - TemplateSimplifier::simplifyTemplateInstantiation failure TEST_CASE(template60); // handling of methods outside template definition TEST_CASE(template61); // daca2, kodi + TEST_CASE(template62); // #8314 - inner template instantiation 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) @@ -547,16 +548,8 @@ private: void template20() { // Ticket #1788 - the destructor implementation is lost - const char code[] = "template class A\n" - "{\n" - "public:\n" - " ~A();\n" - "};\n" - "\n" - "template A::~A()\n" - "{\n" - "}\n" - "\n" + const char code[] = "template class A { public: ~A(); };\n" + "template A::~A() {}\n" "A a;\n"; // The expected result.. @@ -1137,6 +1130,21 @@ private: ASSERT_EQUALS(exp, tok(code)); } + void template62() { // #8314 + const char code[] = "template struct C1 {};\n" + "template void f() { x = y ? C1::allocate(1) : 0; }\n" + "template class C3 {};\n" + "template C3::C3(const C3 &v) { C1 c1; }\n" + "C3 c3;"; + const char exp[] = "template < class T > void f ( ) { x = y ? C1 < int > :: allocate ( 1 ) : 0 ; } " + "template < class T , int S > C3 < T , S > :: C3 ( const C3 < T , S > & v ) { C1 < T * > c1 ; } " + "C3 c3 ; " + "class C3 { } ; " + "C3 :: C3 ( const C3 & v ) { C1 c1 ; } " + "struct C1 { } ;"; + 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"