From 0033836b3c4bb578ef999813de4f5f1a24862d18 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Mon, 11 May 2009 22:55:49 +0000 Subject: [PATCH] Friend declarations are only valid inside class definitions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71489 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 3 +++ lib/Sema/Sema.h | 3 +++ lib/Sema/SemaDeclCXX.cpp | 9 +++++++++ test/SemaCXX/friend.cpp | 6 ++++++ 4 files changed, 21 insertions(+) create mode 100644 test/SemaCXX/friend.cpp diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 577392cbd6..aaaad13b12 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -243,6 +243,9 @@ def err_static_assert_expression_is_not_constant : Error< "static_assert expression is not an integral constant expression">; def err_static_assert_failed : Error<"static_assert failed \"%0\"">; +def err_friend_decl_outside_class : Error< + "'friend' used outside of class">; + def err_abstract_type_in_decl : Error< "%select{return|parameter|variable|field}1 type %0 is an abstract class">; def err_allocation_of_abstract_type : Error< diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 7a25fa9668..21aca40c7d 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -1731,6 +1731,9 @@ public: ExprArg AssertExpr, ExprArg AssertMessageExpr); + virtual bool ActOnFriendDecl(Scope *S, SourceLocation FriendLoc, + DeclPtrTy Dcl); + QualType CheckConstructorDeclarator(Declarator &D, QualType R, FunctionDecl::StorageClass& SC); void CheckConstructor(CXXConstructorDecl *Constructor); diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 7e91f5beac..726080b7ff 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -2643,6 +2643,15 @@ Sema::DeclPtrTy Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, return DeclPtrTy::make(Decl); } +bool Sema::ActOnFriendDecl(Scope *S, SourceLocation FriendLoc, DeclPtrTy Dcl) { + if (!(S->getFlags() & Scope::ClassScope)) { + Diag(FriendLoc, diag::err_friend_decl_outside_class); + return true; + } + + return false; +} + void Sema::SetDeclDeleted(DeclPtrTy dcl, SourceLocation DelLoc) { Decl *Dcl = dcl.getAs(); FunctionDecl *Fn = dyn_cast(Dcl); diff --git a/test/SemaCXX/friend.cpp b/test/SemaCXX/friend.cpp new file mode 100644 index 0000000000..76e84e5fbe --- /dev/null +++ b/test/SemaCXX/friend.cpp @@ -0,0 +1,6 @@ +// RUN: clang-cc -fsyntax-only -verify %s + +friend class A; // expected-error {{'friend' used outside of class}} +void f() { friend class A; } // expected-error {{'friend' used outside of class}} +class C { friend class A; }; +class D { void f() { friend class A; } }; // expected-error {{'friend' used outside of class}} -- 2.40.0