From 2106e3030487a65a06c3d0dad39dcefa60a53d9f Mon Sep 17 00:00:00 2001 From: IOBYTE Date: Mon, 7 Sep 2020 11:45:19 -0400 Subject: [PATCH] fix template namespace bug (#2780) Co-authored-by: Robert Reif --- lib/templatesimplifier.cpp | 4 +--- test/testsimplifytemplate.cpp | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 8d0bf33b8..0dab6b681 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -843,9 +843,7 @@ void TemplateSimplifier::getTemplateInstantiations() std::string qualification; Token * qualificationTok = tok; while (Token::Match(tok, "%name% :: %name%")) { - // ignore redundant namespaces - if (scopeName.find(tok->str()) == std::string::npos) - qualification += (qualification.empty() ? "" : " :: ") + tok->str(); + qualification += (qualification.empty() ? "" : " :: ") + tok->str(); tok = tok->tokAt(2); } diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 0dbdb3a7a..b42a4ddfa 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -2090,24 +2090,66 @@ private: " template T foo(T t) { return t; }\n" " template<> char foo(char a) { return a; }\n" " template<> int foo(int a) { return a; }\n" + " template short NS2::foo(short);\n" + " template long NS1::NS2::foo(long);\n" " }\n" " template float NS2::foo(float);\n" + " template bool NS1::NS2::foo(bool);\n" "}\n" "template double NS1::NS2::foo(double);"; const char exp[] = "namespace NS1 { " "namespace NS2 { " "int foo ( int a ) ; " "char foo ( char a ) ; " + "short foo ( short t ) ; " + "long foo ( long t ) ; " "float foo ( float t ) ; " + "bool foo ( bool t ) ; " "double foo ( double t ) ; " "char foo ( char a ) { return a ; } " "int foo ( int a ) { return a ; } " "} " "} " + "short NS1 :: NS2 :: foo ( short t ) { return t ; } " + "long NS1 :: NS2 :: foo ( long t ) { return t ; } " "float NS1 :: NS2 :: foo ( float t ) { return t ; } " + "bool NS1 :: NS2 :: foo ( bool t ) { return t ; } " "double NS1 :: NS2 :: foo ( double t ) { return t ; }"; ASSERT_EQUALS(exp, tok(code)); } + { + const char code[] = "namespace NS1 {\n" + " namespace NS {\n" + " template T foo(T t) { return t; }\n" + " template<> char foo(char a) { return a; }\n" + " template<> int foo(int a) { return a; }\n" + " template short NS::foo(short);\n" + " template long NS1::NS::foo(long);\n" + " }\n" + " template float NS::foo(float);\n" + " template bool NS1::NS::foo(bool);\n" + "}\n" + "template double NS1::NS::foo(double);"; + const char exp[] = "namespace NS1 { " + "namespace NS { " + "int foo ( int a ) ; " + "char foo ( char a ) ; " + "short foo ( short t ) ; " + "long foo ( long t ) ; " + "float foo ( float t ) ; " + "bool foo ( bool t ) ; " + "double foo ( double t ) ; " + "char foo ( char a ) { return a ; } " + "int foo ( int a ) { return a ; } " + "} " + "} " + "short NS1 :: NS :: foo ( short t ) { return t ; } " + "long NS1 :: NS :: foo ( long t ) { return t ; } " + "float NS1 :: NS :: foo ( float t ) { return t ; } " + "bool NS1 :: NS :: foo ( bool t ) { return t ; } " + "double NS1 :: NS :: foo ( double t ) { return t ; }"; + ASSERT_EQUALS(exp, tok(code)); + } } void template92() {