From ee05fe7d7706ef5f11ca46b30be7fab456b57741 Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Sun, 9 Apr 2017 16:01:07 +0200 Subject: [PATCH] Ticket #7942: Don't generate invalid syntax for out of line template class constructors during instantiation. --- lib/templatesimplifier.cpp | 8 ++++++-- test/testconstructors.cpp | 11 +++++++++++ test/testsimplifytemplate.cpp | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 52719bd4c..39e7303a2 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -818,8 +818,12 @@ void TemplateSimplifier::expandTemplate( // replace name.. if (Token::Match(tok3, (name + " !!<").c_str())) { - tokenlist.addtoken(newName, tok3->linenr(), tok3->fileIndex()); - continue; + if (Token::Match(tok3->tokAt(-2), "> :: %name% ( )")) { + ; // Ticket #7942: Replacing for out-of-line constructors generates invalid syntax + } else { + tokenlist.addtoken(newName, tok3->linenr(), tok3->fileIndex()); + continue; + } } // copy diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index fd258cea3..46dc12f8e 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -183,6 +183,7 @@ private: TEST_CASE(uninitConstVar); TEST_CASE(constructors_crash1); // ticket #5641 TEST_CASE(classWithOperatorInName);// ticket #2827 + TEST_CASE(templateConstructor); // ticket #7942 } @@ -3348,6 +3349,16 @@ private: "};"); ASSERT_EQUALS("", errout.str()); } + + void templateConstructor() { // ticket #7942 + check("template struct Containter {\n" + " Containter();\n" + " T* mElements;\n" + "};\n" + "template Containter::Containter() : mElements(nullptr) {}\n" + "Containter intContainer;"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestConstructors) diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 4637e7d10..007cf2d6c 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -184,7 +184,7 @@ private: const char expected[] = "template < class T > Fred < T > :: Fred ( ) { } " // <- TODO: this should be removed "Fred < float > fred ; " "class Fred < float > { } ; " - "Fred < float > :: Fred < float > ( ) { }"; + "Fred < float > :: Fred ( ) { }"; ASSERT_EQUALS(expected, tok(code)); }