Skip to content

Commit

Permalink
Function with unparsed body is a definition
Browse files Browse the repository at this point in the history
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<typename T> struct Somewhat {
      void internal() const {}
      friend void operator+(int const &, Somewhat<T> const &) {}
    };
void operator+(int const &, Somewhat<char> 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<char>` 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.

llvm-svn: 305903
  • Loading branch information
spavloff committed Jun 21, 2017
1 parent f81d46f commit 25dbe1a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/AST/Decl.h
Expand Up @@ -1874,7 +1874,7 @@ class FunctionDecl : public DeclaratorDecl, public DeclContext,
///
bool isThisDeclarationADefinition() const {
return IsDeleted || IsDefaulted || Body || IsLateTemplateParsed ||
hasDefiningAttr();
WillHaveBody || hasDefiningAttr();
}

/// doesThisDeclarationHaveABody - Returns whether this specific
Expand Down
6 changes: 0 additions & 6 deletions clang/lib/Sema/SemaCUDA.cpp
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Sema/SemaDecl.cpp
Expand Up @@ -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() &&
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaDeclCXX.cpp
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Expand Up @@ -1782,6 +1782,9 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Previous.clear();
}

if (isFriend)
Function->setObjectOfFriendDecl();

SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous,
isExplicitSpecialization);

Expand Down
37 changes: 37 additions & 0 deletions clang/test/SemaCXX/friend2.cpp
Expand Up @@ -170,3 +170,40 @@ struct Test {
template class Test<int>;

}

namespace pr14785 {
template<typename T>
struct Somewhat {
void internal() const { }
friend void operator+(int const &, Somewhat<T> const &) {} // expected-error{{redefinition of 'operator+'}}
};

void operator+(int const &, Somewhat<char> const &x) { // expected-note {{previous definition is here}}
x.internal(); // expected-note{{in instantiation of template class 'pr14785::Somewhat<char>' requested here}}
}
}

namespace D30375 {
template <typename K> struct B {
template <typename A> bool insert(A &);
};

template <typename K>
template <typename A> bool B<K>::insert(A &x) { return x < x; }

template <typename K> class D {
B<K> t;

public:
K x;
bool insert() { return t.insert(x); }
template <typename K1> friend bool operator<(const D<K1> &, const D<K1> &);
};

template <typename K> bool operator<(const D<K> &, const D<K> &);

void func() {
D<D<int>> cache;
cache.insert();
}
}

0 comments on commit 25dbe1a

Please sign in to comment.