diff options
| author | Yaron Keren <yaron.keren@gmail.com> | 2017-04-17 08:51:20 +0000 |
|---|---|---|
| committer | Yaron Keren <yaron.keren@gmail.com> | 2017-04-17 08:51:20 +0000 |
| commit | 27e2ff964fc3e18c27488e15dc74b44ef32d2fe0 (patch) | |
| tree | 7ea962556c927a86fc590391ea20320b75fed0f0 | |
| parent | 11d9c4f691b68f8abcba85345abd4d3067cbee3f (diff) | |
| download | bcm5719-llvm-27e2ff964fc3e18c27488e15dc74b44ef32d2fe0.tar.gz bcm5719-llvm-27e2ff964fc3e18c27488e15dc74b44ef32d2fe0.zip | |
Address http://bugs.llvm.org/pr30994 so that a non-friend can properly replace a friend, and a visible friend can properly replace an invisible friend but not vice verse, and definitions are not replaced. This fixes the two FIXME in SemaTemplate/friend-template.cpp.
The code implements Richard Smith suggestion in comment 3 of the PR.
reviewer: Vassil Vassilev
Differential Revision: https://reviews.llvm.org/D31540
llvm-svn: 300443
| -rw-r--r-- | clang/include/clang/AST/DeclBase.h | 2 | ||||
| -rw-r--r-- | clang/lib/AST/Decl.cpp | 4 | ||||
| -rw-r--r-- | clang/lib/AST/DeclBase.cpp | 15 | ||||
| -rw-r--r-- | clang/test/SemaTemplate/friend-template.cpp | 26 |
4 files changed, 34 insertions, 13 deletions
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h index c88cb6a8fd1..5ca1f1ec6ed 100644 --- a/clang/include/clang/AST/DeclBase.h +++ b/clang/include/clang/AST/DeclBase.h @@ -417,6 +417,8 @@ public: return const_cast<Decl*>(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 index 2b22e5bb50a..6a288cdb21f 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -1536,6 +1536,10 @@ bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const { if (isa<ObjCMethodDecl>(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<ParmVarDecl>(this)) diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp index cda70c5edcd..ae7444579b0 100644 --- a/clang/lib/AST/DeclBase.cpp +++ b/clang/lib/AST/DeclBase.cpp @@ -861,6 +861,21 @@ const FunctionType *Decl::getFunctionType(bool BlocksToo) const { return Ty->getAs<FunctionType>(); } +bool Decl::isThisDeclarationADefinition() const { + if (auto *TD = dyn_cast<TagDecl>(this)) + return TD->isThisDeclarationADefinition(); + if (auto *FD = dyn_cast<FunctionDecl>(this)) + return FD->isThisDeclarationADefinition(); + if (auto *VD = dyn_cast<VarDecl>(this)) + return VD->isThisDeclarationADefinition(); + if (auto *CTD = dyn_cast<ClassTemplateDecl>(this)) + return CTD->isThisDeclarationADefinition(); + if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this)) + return FTD->isThisDeclarationADefinition(); + if (auto *VTD = dyn_cast<VarTemplateDecl>(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 index e9b2b9b8e64..1ed751b4f36 100644 --- 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 @@ namespace test3 { Foo<int> foo; template<typename T, T Value> struct X2a; - - template<typename T, int Size> struct X2b; + template<typename T, int Size> struct X2b; // expected-note {{previous non-type template parameter with type 'int' is here}} template<typename T> class X3 { template<typename U, U Value> friend struct X2a; - - // FIXME: the redeclaration note ends up here because redeclaration - // lookup ends up finding the friend target from X3<int>. - template<typename U, T Value> 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<typename U, T Value> friend struct X2b; // expected-error {{template non-type parameter has a different type 'long' in template redeclaration}} }; X3<int> x3i; // okay @@ -297,14 +292,11 @@ namespace PR12585 { int n = C::D<void*>().f(); struct F { - template<int> struct G; + template<int> struct G; // expected-note {{previous}} }; template<typename T> 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<T> friend struct F::G; // \ - // expected-error {{different type 'char' in template redeclaration}} \ - // expected-note {{previous}} + // expected-error {{different type 'char' in template redeclaration}} }; H<int> h1; // ok H<char> h2; // expected-note {{instantiation}} @@ -329,3 +321,11 @@ namespace rdar12350696 { 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}} +} |

