From: Serge Pavlov Date: Wed, 21 Jun 2017 12:46:57 +0000 (+0000) Subject: Function with unparsed body is a definition X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cabb95e0814719bf953eeeda569e6abafa5fbfad;p=clang Function with unparsed body is a definition While a function body is being parsed, the function declaration is not considered as a definition because it does not have a body yet. In some cases it leads to incorrect interpretation, the case is presented in https://bugs.llvm.org/show_bug.cgi?id=14785: ``` template struct Somewhat { void internal() const {} friend void operator+(int const &, Somewhat const &) {} }; void operator+(int const &, Somewhat const &x) { x.internal(); } ``` When statement `x.internal()` in the body of global `operator+` is parsed, the type of `x` must be completed, so the instantiation of `Somewhat` is started. It instantiates the declaration of `operator+` defined inline, and makes a check for redefinition. The check does not detect another definition because the declaration of `operator+` is still not defining as does not have a body yet. To solves this problem the function `isThisDeclarationADefinition` considers a function declaration as a definition if it has flag `WillHaveBody` set. This change fixes PR14785. Differential Revision: https://reviews.llvm.org/D30375 This is a recommit of 305379, reverted in 305381, with small changes. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@305903 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index b15c9d3f1e..30552be9b3 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -1874,7 +1874,7 @@ public: /// bool isThisDeclarationADefinition() const { return IsDeleted || IsDefaulted || Body || IsLateTemplateParsed || - hasDefiningAttr(); + WillHaveBody || hasDefiningAttr(); } /// doesThisDeclarationHaveABody - Returns whether this specific diff --git a/lib/Sema/SemaCUDA.cpp b/lib/Sema/SemaCUDA.cpp index b938ac387c..cac5f68227 100644 --- a/lib/Sema/SemaCUDA.cpp +++ b/lib/Sema/SemaCUDA.cpp @@ -629,12 +629,6 @@ static bool IsKnownEmitted(Sema &S, FunctionDecl *FD) { // emitted, because (say) the definition could include "inline". FunctionDecl *Def = FD->getDefinition(); - // We may currently be parsing the body of FD, in which case - // FD->getDefinition() will be null, but we still want to treat FD as though - // it's a definition. - if (!Def && FD->willHaveBody()) - Def = FD; - if (Def && !isDiscardableGVALinkage(S.getASTContext().GetGVALinkageForFunction(Def))) return true; diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index d6164b3ff5..2f78d06a82 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -12232,6 +12232,7 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, if (FD) { FD->setBody(Body); + FD->setWillHaveBody(false); if (getLangOpts().CPlusPlus14) { if (!FD->isInvalidDecl() && Body && !FD->isDependentContext() && diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 844299bb87..0b46e15bb0 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -13878,6 +13878,9 @@ void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { return; } + // Deleted function does not have a body. + Fn->setWillHaveBody(false); + if (const FunctionDecl *Prev = Fn->getPreviousDecl()) { // Don't consider the implicit declaration we generate for explicit // specializations. FIXME: Do not generate these implicit declarations. diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index c58122cd27..479760222d 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -1782,6 +1782,9 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, Previous.clear(); } + if (isFriend) + Function->setObjectOfFriendDecl(); + SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous, isExplicitSpecialization); diff --git a/test/SemaCXX/friend2.cpp b/test/SemaCXX/friend2.cpp index 347af0d61b..d1d4b628ba 100644 --- a/test/SemaCXX/friend2.cpp +++ b/test/SemaCXX/friend2.cpp @@ -170,3 +170,40 @@ struct Test { template class Test; } + +namespace pr14785 { +template +struct Somewhat { + void internal() const { } + friend void operator+(int const &, Somewhat const &) {} // expected-error{{redefinition of 'operator+'}} +}; + +void operator+(int const &, Somewhat const &x) { // expected-note {{previous definition is here}} + x.internal(); // expected-note{{in instantiation of template class 'pr14785::Somewhat' requested here}} +} +} + +namespace D30375 { +template struct B { + template bool insert(A &); +}; + +template +template bool B::insert(A &x) { return x < x; } + +template class D { + B t; + +public: + K x; + bool insert() { return t.insert(x); } + template friend bool operator<(const D &, const D &); +}; + +template bool operator<(const D &, const D &); + +void func() { + D> cache; + cache.insert(); +} +}