From 3b4abb67921126aefbf67d15bd6cdb7aa7cf47ff Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 7 Apr 2010 17:57:12 +0000 Subject: Improve handling of friend types in several ways: - When instantiating a friend type template, perform semantic analysis on the resulting type. - Downgrade the errors concerning friend type declarations that do not refer to classes to ExtWarns in C++98/03. C++0x allows practically any type to be befriended, and ignores the friend declaration if the type is not a class. llvm-svn: 100635 --- .../CXX/class.access/class.friend/p2-cxx03.cpp | 13 ++++++++++ .../CXX/class.access/class.friend/p3-cxx0x.cpp | 29 ++++++++++++++++++++++ clang/test/CXX/class/class.friend/p2.cpp | 4 +-- .../dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp | 2 +- clang/test/CXX/temp/temp.decls/temp.friend/p3.cpp | 3 +-- clang/test/Parser/cxx-friend.cpp | 6 ++--- 6 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 clang/test/CXX/class.access/class.friend/p2-cxx03.cpp create mode 100644 clang/test/CXX/class.access/class.friend/p3-cxx0x.cpp (limited to 'clang/test') diff --git a/clang/test/CXX/class.access/class.friend/p2-cxx03.cpp b/clang/test/CXX/class.access/class.friend/p2-cxx03.cpp new file mode 100644 index 00000000000..0391c4b989d --- /dev/null +++ b/clang/test/CXX/class.access/class.friend/p2-cxx03.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +template +class X0 { + friend T; // expected-warning{{non-class type 'T' cannot be a friend}} +}; + +class X1 { }; +enum E1 { }; +X0 x0a; +X0 x0b; +X0 x0c; +X0 x0d; + diff --git a/clang/test/CXX/class.access/class.friend/p3-cxx0x.cpp b/clang/test/CXX/class.access/class.friend/p3-cxx0x.cpp new file mode 100644 index 00000000000..4f55e5339ab --- /dev/null +++ b/clang/test/CXX/class.access/class.friend/p3-cxx0x.cpp @@ -0,0 +1,29 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++0x -verify %s +template +class X0 { + friend T; +}; + +class Y1 { }; +enum E1 { }; +X0 x0a; +X0 x0b; +X0 x0c; +X0 x0d; + +template +class X1 { + friend typename T::type; // expected-error{{no type named 'type' in 'Y1'}} +}; + +struct Y2 { + struct type { }; +}; + +struct Y3 { + typedef int type; +}; + +X1 x1a; +X1 x1b; +X1 x1c; // expected-note{{in instantiation of template class 'X1' requested here}} diff --git a/clang/test/CXX/class/class.friend/p2.cpp b/clang/test/CXX/class/class.friend/p2.cpp index 9fe2b17e504..eb5036f8128 100644 --- a/clang/test/CXX/class/class.friend/p2.cpp +++ b/clang/test/CXX/class/class.friend/p2.cpp @@ -4,7 +4,7 @@ struct B0; class A { friend class B {}; // expected-error {{cannot define a type in a friend declaration}} - friend int; // expected-error {{friends can only be classes or functions}} - friend B0; // expected-error {{must specify 'struct' to befriend}} + friend int; // expected-warning {{non-class type 'int' cannot be a friend}} + friend B0; // expected-warning {{must specify 'struct' to befriend}} friend class C; // okay }; diff --git a/clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp b/clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp index c5303117732..b04e869a486 100644 --- a/clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp +++ b/clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp @@ -13,7 +13,7 @@ class A1 { friend union A; // expected-error {{use of 'A' with tag type that does not match previous declaration}} friend enum A; // expected-error {{ISO C++ forbids forward references to 'enum' types}} \ - // expected-error {{enum types cannot be friends}} + // expected-warning {{cannot be a friend}} }; template struct B { // expected-note {{previous use is here}} diff --git a/clang/test/CXX/temp/temp.decls/temp.friend/p3.cpp b/clang/test/CXX/temp/temp.decls/temp.friend/p3.cpp index 17d8c85df00..d116e016f1d 100644 --- a/clang/test/CXX/temp/temp.decls/temp.friend/p3.cpp +++ b/clang/test/CXX/temp/temp.decls/temp.friend/p3.cpp @@ -8,6 +8,5 @@ class B { template friend class A; template friend class Undeclared; - // FIXME: Diagnostic below could be (and was) better. - template friend typename A::Member; // expected-error {{classes or functions}} + template friend typename A::Member; // expected-warning {{non-class type 'typename A::Member' cannot be a friend}} }; diff --git a/clang/test/Parser/cxx-friend.cpp b/clang/test/Parser/cxx-friend.cpp index 2fe30cd3e42..59350b56636 100644 --- a/clang/test/Parser/cxx-friend.cpp +++ b/clang/test/Parser/cxx-friend.cpp @@ -21,9 +21,9 @@ class B { // 'A' here should refer to the declaration above. friend class A; - friend C; // expected-error {{must specify 'class' to befriend}} - friend U; // expected-error {{must specify 'union' to befriend}} - friend int; // expected-error {{friends can only be classes or functions}} + friend C; // expected-warning {{must specify 'class' to befriend}} + friend U; // expected-warning {{must specify 'union' to befriend}} + friend int; // expected-warning {{non-class type 'int' cannot be a friend}} friend void myfunc(); -- cgit v1.2.3