From e3c4a27ca0c760c9fe590d29d4dfb3e810ec23b6 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 21 Dec 2024 22:30:45 +0100 Subject: [PATCH] Fix #13444 FP uninitMemberVar caused by template specialization instantiated in wrong scope --- lib/templatesimplifier.cpp | 7 +++++++ test/testsimplifytemplate.cpp | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index f3bdb05cc14..be6a842dc99 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -2061,7 +2061,14 @@ void TemplateSimplifier::expandTemplate( const std::string lastName = (templateInstantiation.name().find(' ') != std::string::npos) ? templateInstantiation.name().substr(templateInstantiation.name().rfind(' ')+1) : templateInstantiation.name(); std::stack templates; + int scopeCount = 0; for (; tok3; tok3 = tok3->next()) { + if (tok3->str() == "{") + ++scopeCount; + else if (tok3->str() == "}") + --scopeCount; + if (scopeCount < 0) + break; if (tok3->isName() && !Token::Match(tok3, "class|typename|struct") && !tok3->isStandardType()) { // search for this token in the type vector unsigned int itype = 0; diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index e3f8e9d83c8..02744722af4 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -239,6 +239,7 @@ class TestSimplifyTemplate : public TestFixture { TEST_CASE(template_namespace_9); TEST_CASE(template_namespace_10); TEST_CASE(template_namespace_11); // #7145 + TEST_CASE(template_namespace_12); TEST_CASE(template_pointer_type); TEST_CASE(template_array_type); @@ -5264,6 +5265,27 @@ class TestSimplifyTemplate : public TestFixture { "} int MyNamespace :: TestClass :: TemplatedMethod ( int t ) { return t ; }", tok(code)); } + void template_namespace_12() { + const char code[] = "struct S {};\n" // #13444 + "namespace N {\n" + " template<>\n" + " struct hash {};\n" + "}\n" + "struct T {\n" + " T(int i) : hash(i) {}\n" + " int hash;\n" + "};\n"; + ASSERT_EQUALS("struct S { } ; " + "namespace N { " + "struct hash { } ; " + "} " + "struct T { " + "T ( int i ) : hash ( i ) { } " + "int hash ; " + "} ;", + tok(code)); + } + void template_pointer_type() { const char code[] = "template void foo(const T x) {}\n" "void bar() { foo(0); }";