Skip to content

Commit 1d131a1

Browse files
autoantwortclaude
andcommitted
Fix #15269 FP shadowFunction: deduction guide is treated as a function
SymbolDatabase::isFunction() accepted a deduction guide like 'template <class T> S(T) -> S<T>;' 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 <noreply@anthropic.com>
1 parent 4ccfcc6 commit 1d131a1

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,6 +2016,16 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const
20162016
const Token* tok1 = tok->previous();
20172017
const Token* tok2 = tok->linkAt(1)->next();
20182018

2019+
// deduction guide 'S ( .. ) -> S < .. > ;': a function with a trailing return type has 'auto' or a
2020+
// qualification directly before its name (the tokenizer removed calling conventions and attributes)
2021+
if (!Token::Match(tok1, "auto|::") && Token::Match(tok2, ". %name%")) {
2022+
const std::string& trailingType = tok2->strAt(1);
2023+
const bool sameTemplate = (trailingType == tok->str() && tok2->strAt(2) == "<") ||
2024+
startsWith(trailingType, tok->str() + " <"); // instantiated 'S < .. >' is one token
2025+
if (sameTemplate)
2026+
return false;
2027+
}
2028+
20192029
if (!TokenList::isFunctionHead(tok->next(), ";:{"))
20202030
return false;
20212031

‎test/testother.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13682,6 +13682,19 @@ class TestOther : public TestFixture {
1368213682
"int i = 1;\n");
1368313683
ASSERT_EQUALS("", errout_str());
1368413684

13685+
check("template <class E>\n" // #15269
13686+
"struct unexpected {\n"
13687+
" explicit unexpected(E e) : err(e) {}\n"
13688+
" E err;\n"
13689+
"};\n"
13690+
"template <class E>\n"
13691+
"unexpected(E) -> unexpected<E>;\n"
13692+
"int f(int x) {\n"
13693+
" const int unexpected = x + 1;\n"
13694+
" return unexpected;\n"
13695+
"}\n");
13696+
ASSERT_EQUALS("", errout_str());
13697+
1368513698
{
1368613699
ScopedFile inc("test.h", "int pad1;\nint pad2;\nint pad3;\nint pad4;\nfloat f;\n");
1368713700
checkP("void g() {\n"

‎test/testsymboldatabase.cpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ class TestSymbolDatabase : public TestFixture {
263263
TEST_CASE(hasSubClassConstructor);
264264
TEST_CASE(testConstructors);
265265
TEST_CASE(functionDeclarationTemplate);
266+
TEST_CASE(functionDeclarationDeductionGuide);
266267
TEST_CASE(functionDeclarations);
267268
TEST_CASE(functionDeclarations2);
268269
TEST_CASE(constexprFunction);
@@ -2541,6 +2542,46 @@ class TestSymbolDatabase : public TestFixture {
25412542
ASSERT(foo && foo->hasBody());
25422543
}
25432544

2545+
void functionDeclarationDeductionGuide() { // #15269
2546+
{
2547+
GET_SYMBOL_DB("template <class T> struct S { S(T) {} };\n"
2548+
"template <class T> S(T) -> S<T>;\n");
2549+
ASSERT(db);
2550+
ASSERT_EQUALS(0, db->scopeList.front().functionList.size());
2551+
}
2552+
{
2553+
GET_SYMBOL_DB("template <class T> struct S { explicit S(T*) {} };\n"
2554+
"template <class T> explicit S(T*) -> S<T>;\n");
2555+
ASSERT(db);
2556+
ASSERT_EQUALS(0, db->scopeList.front().functionList.size());
2557+
}
2558+
{
2559+
GET_SYMBOL_DB("template <class T> struct S { S(T) {} };\n"
2560+
"explicit S(const char*) -> S<int>;\n");
2561+
ASSERT(db);
2562+
ASSERT_EQUALS(0, db->scopeList.front().functionList.size());
2563+
}
2564+
{
2565+
GET_SYMBOL_DB("struct O {\n"
2566+
" template <class T> struct S { S(T) {} };\n"
2567+
" S(const char*) -> S<int>;\n"
2568+
"};\n");
2569+
ASSERT(db);
2570+
const Scope* o = db->findScopeByName("O");
2571+
ASSERT(o);
2572+
ASSERT_EQUALS(0, o->functionList.size());
2573+
}
2574+
{
2575+
// a function returning a class template of the same name is no deduction guide
2576+
GET_SYMBOL_DB("template <class T> struct X {};\n"
2577+
"namespace n { auto X(int) -> X<int>; }\n");
2578+
ASSERT(db);
2579+
const Scope* n = db->findScopeByName("n");
2580+
ASSERT(n);
2581+
ASSERT_EQUALS(1, n->functionList.size());
2582+
}
2583+
}
2584+
25442585
void functionDeclarations() {
25452586
GET_SYMBOL_DB("void foo();\nvoid foo();\nint foo(int i);\nvoid foo() {}\n");
25462587

0 commit comments

Comments
 (0)