Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 13 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13682,6 +13682,19 @@ class TestOther : public TestFixture {
"int i = 1;\n");
ASSERT_EQUALS("", errout_str());

check("template <class E>\n" // #15269
"struct unexpected {\n"
" explicit unexpected(E e) : err(e) {}\n"
" E err;\n"
"};\n"
"template <class E>\n"
"unexpected(E) -> unexpected<E>;\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"
Expand Down
57 changes: 57 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -2541,6 +2542,62 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(foo && foo->hasBody());
}

void functionDeclarationDeductionGuide() { // #15269
{
GET_SYMBOL_DB("template <class T> struct S { S(T) {} };\n"
"template <class T> S(T) -> S<T>;\n");
ASSERT(db);
ASSERT_EQUALS(0, db->scopeList.front().functionList.size());
}
{
GET_SYMBOL_DB("template <class T> struct S { explicit S(T*) {} };\n"
"template <class T> explicit S(T*) -> S<T>;\n");
ASSERT(db);
ASSERT_EQUALS(0, db->scopeList.front().functionList.size());
}
{
GET_SYMBOL_DB("template <class T> struct S { S(T) {} };\n"
"explicit S(const char*) -> S<int>;\n");
ASSERT(db);
ASSERT_EQUALS(0, db->scopeList.front().functionList.size());
}
{
GET_SYMBOL_DB("struct O {\n"
" template <class T> struct S { S(T) {} };\n"
" S(const char*) -> S<int>;\n"
"};\n");
ASSERT(db);
const Scope* o = db->findScopeByName("O");
ASSERT(o);
ASSERT_EQUALS(0, o->functionList.size());
}
{
GET_SYMBOL_DB("template <class T> struct S { S(T) {} };\n"
"template <class T> S(T*) -> ::S<T>;\n");
ASSERT(db);
ASSERT_EQUALS(0, db->scopeList.front().functionList.size());
}
{
GET_SYMBOL_DB("struct O {\n"
" template <class T> struct S { S(T) {} };\n"
" template <class T> S(T*) -> O::S<T>;\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 <class T> struct X {};\n"
"namespace n { auto X(int) -> X<int>; }\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");

Expand Down
Loading