diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h --- a/clang/include/clang/AST/DeclBase.h +++ b/clang/include/clang/AST/DeclBase.h @@ -417,6 +417,8 @@ return const_cast(this)->getTranslationUnitDecl(); } + bool isThisDeclarationADefinition() const; + bool isInAnonymousNamespace() const; bool isInStdNamespace() const; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -1536,6 +1536,10 @@ if (isa(this)) return false; + if (getFriendObjectKind() > OldD->getFriendObjectKind() && + !isThisDeclarationADefinition()) + return false; + // For parameters, pick the newer one. This is either an error or (in // Objective-C) permitted as an extension. if (isa(this)) diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp --- a/clang/lib/AST/DeclBase.cpp +++ b/clang/lib/AST/DeclBase.cpp @@ -861,6 +861,21 @@ return Ty->getAs(); } +bool Decl::isThisDeclarationADefinition() const { + if (auto *TD = dyn_cast(this)) + return TD->isThisDeclarationADefinition(); + if (auto *FD = dyn_cast(this)) + return FD->isThisDeclarationADefinition(); + if (auto *VD = dyn_cast(this)) + return VD->isThisDeclarationADefinition(); + if (auto *CTD = dyn_cast(this)) + return CTD->isThisDeclarationADefinition(); + if (auto *FTD = dyn_cast(this)) + return FTD->isThisDeclarationADefinition(); + if (auto *VTD = dyn_cast(this)) + return VTD->isThisDeclarationADefinition(); + return false; +} /// Starting at a given context (a Decl or DeclContext), look for a /// code context that is not a closure (a lambda, block, etc.). diff --git a/clang/test/SemaTemplate/friend-template.cpp b/clang/test/SemaTemplate/friend-template.cpp --- a/clang/test/SemaTemplate/friend-template.cpp +++ b/clang/test/SemaTemplate/friend-template.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify %s +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s // PR5057 namespace test0 { namespace std { @@ -68,17 +68,12 @@ Foo foo; template struct X2a; - - template struct X2b; + template struct X2b; // expected-note {{previous non-type template parameter with type 'int' is here}} template class X3 { template friend struct X2a; - - // FIXME: the redeclaration note ends up here because redeclaration - // lookup ends up finding the friend target from X3. - template friend struct X2b; // expected-error {{template non-type parameter has a different type 'long' in template redeclaration}} \ - // expected-note {{previous non-type template parameter with type 'int' is here}} + template friend struct X2b; // expected-error {{template non-type parameter has a different type 'long' in template redeclaration}} }; X3 x3i; // okay @@ -297,14 +292,11 @@ int n = C::D().f(); struct F { - template struct G; + template struct G; // expected-note {{previous}} }; template struct H { - // FIXME: As with cases above, the note here is on an unhelpful declaration, - // and should point to the declaration of G within F. template friend struct F::G; // \ - // expected-error {{different type 'char' in template redeclaration}} \ - // expected-note {{previous}} + // expected-error {{different type 'char' in template redeclaration}} }; H h1; // ok H h2; // expected-note {{instantiation}} @@ -329,3 +321,11 @@ foo(b); // expected-note {{in instantiation}} } } +namespace PR30994 { + void f(); + struct A { + [[deprecated]] friend void f() {} // \ + expected-note {{has been explicitly marked deprecated here}} + }; + void g() { f(); } // expected-warning {{is deprecated}} +}