From 61c87d2c0ffd0c984a53ff4a70f5c172653b5aa4 Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Tue, 24 Nov 2026 17:16:15 +0100 Subject: [PATCH] Fix #15269 FP shadowFunction: deduction guide is treated as a function SymbolDatabase::isFunction() accepted a deduction guide like 'template S(T) -> S;' as a function declaration with a trailing return type (taking 'template' as its return type), and also non-template guides in class scope. A local variable named like the class template then shadowed this function. A function with a trailing return type has 'auto' or a qualification before its name, so a declaration without it whose trailing type names the same template is a deduction guide. Co-Authored-By: Claude Opus 5.5 --- lib/symboldatabase.cpp | 15 ++++++++++ test/testother.cpp | 13 +++++++++ test/testsymboldatabase.cpp | 57 +++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index d063588eb63..8c2fb0fc700 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2016,6 +2016,21 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const const Token* tok1 = tok->previous(); const Token* tok2 = tok->linkAt(1)->next(); + // deduction guide 'S ( .. ) -> S < .. > ;': a function with a trailing return type has 'auto' or a + // qualification directly before its name (the tokenizer removed calling conventions and attributes) + if (!Token::Match(tok1, "auto|::") && Token::Match(tok2, ". ::| %name%")) { + const Token* typeTok = tok2->next(); + if (typeTok->str() == "::") + typeTok = typeTok->next(); + while (Token::Match(typeTok, "%name% :: %name%")) + typeTok = typeTok->tokAt(2); + const std::string& trailingType = typeTok->str(); + const bool sameTemplate = (trailingType == tok->str() && typeTok->strAt(1) == "<") || + startsWith(trailingType, tok->str() + " <"); // instantiated 'S < .. >' is one token + if (sameTemplate) + return false; + } + if (!TokenList::isFunctionHead(tok->next(), ";:{")) return false; diff --git a/test/testother.cpp b/test/testother.cpp index 085b39cfffb..7272f2405e8 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -13682,6 +13682,19 @@ class TestOther : public TestFixture { "int i = 1;\n"); ASSERT_EQUALS("", errout_str()); + check("template \n" // #15269 + "struct unexpected {\n" + " explicit unexpected(E e) : err(e) {}\n" + " E err;\n" + "};\n" + "template \n" + "unexpected(E) -> unexpected;\n" + "int f(int x) {\n" + " const int unexpected = x + 1;\n" + " return unexpected;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + { ScopedFile inc("test.h", "int pad1;\nint pad2;\nint pad3;\nint pad4;\nfloat f;\n"); checkP("void g() {\n" diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 587450541b4..35e77db30f8 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -263,6 +263,7 @@ class TestSymbolDatabase : public TestFixture { TEST_CASE(hasSubClassConstructor); TEST_CASE(testConstructors); TEST_CASE(functionDeclarationTemplate); + TEST_CASE(functionDeclarationDeductionGuide); TEST_CASE(functionDeclarations); TEST_CASE(functionDeclarations2); TEST_CASE(constexprFunction); @@ -2541,6 +2542,62 @@ class TestSymbolDatabase : public TestFixture { ASSERT(foo && foo->hasBody()); } + void functionDeclarationDeductionGuide() { // #15269 + { + GET_SYMBOL_DB("template struct S { S(T) {} };\n" + "template S(T) -> S;\n"); + ASSERT(db); + ASSERT_EQUALS(0, db->scopeList.front().functionList.size()); + } + { + GET_SYMBOL_DB("template struct S { explicit S(T*) {} };\n" + "template explicit S(T*) -> S;\n"); + ASSERT(db); + ASSERT_EQUALS(0, db->scopeList.front().functionList.size()); + } + { + GET_SYMBOL_DB("template struct S { S(T) {} };\n" + "explicit S(const char*) -> S;\n"); + ASSERT(db); + ASSERT_EQUALS(0, db->scopeList.front().functionList.size()); + } + { + GET_SYMBOL_DB("struct O {\n" + " template struct S { S(T) {} };\n" + " S(const char*) -> S;\n" + "};\n"); + ASSERT(db); + const Scope* o = db->findScopeByName("O"); + ASSERT(o); + ASSERT_EQUALS(0, o->functionList.size()); + } + { + GET_SYMBOL_DB("template struct S { S(T) {} };\n" + "template S(T*) -> ::S;\n"); + ASSERT(db); + ASSERT_EQUALS(0, db->scopeList.front().functionList.size()); + } + { + GET_SYMBOL_DB("struct O {\n" + " template struct S { S(T) {} };\n" + " template S(T*) -> O::S;\n" + "};\n"); + ASSERT(db); + const Scope* o = db->findScopeByName("O"); + ASSERT(o); + ASSERT_EQUALS(0, o->functionList.size()); + } + { + // a function returning a class template of the same name is no deduction guide + GET_SYMBOL_DB("template struct X {};\n" + "namespace n { auto X(int) -> X; }\n"); + ASSERT(db); + const Scope* n = db->findScopeByName("n"); + ASSERT(n); + ASSERT_EQUALS(1, n->functionList.size()); + } + } + void functionDeclarations() { GET_SYMBOL_DB("void foo();\nvoid foo();\nint foo(int i);\nvoid foo() {}\n");