From: Fariborz Jahanian Date: Sun, 22 Sep 2013 00:02:16 +0000 (+0000) Subject: ObjectiveC: ObjC declarations, including forward class X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=44242396a3cf647f4f45058856c7e751df1d9a6e;p=clang ObjectiveC: ObjC declarations, including forward class and protocols can be at global scope only. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@191155 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp index b938bd6f8b..ec82221289 100644 --- a/lib/Sema/SemaDeclObjC.cpp +++ b/lib/Sema/SemaDeclObjC.cpp @@ -809,6 +809,12 @@ Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, unsigned NumElts, AttributeList *attrList) { SmallVector DeclsInGroup; + if (isa(CurContext)) { + Diag(AtProtocolLoc, + diag::err_objc_decls_may_only_appear_in_global_scope); + return BuildDeclaratorGroup(DeclsInGroup, false); + } + for (unsigned i = 0; i != NumElts; ++i) { IdentifierInfo *Ident = IdentList[i].first; ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second, @@ -1927,6 +1933,12 @@ Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, SourceLocation *IdentLocs, unsigned NumElts) { SmallVector DeclsInGroup; + if (isa(CurContext)) { + Diag(AtClassLoc, + diag::err_objc_decls_may_only_appear_in_global_scope); + return BuildDeclaratorGroup(DeclsInGroup, false); + } + for (unsigned i = 0; i != NumElts; ++i) { // Check for another declaration kind with the same name. NamedDecl *PrevDecl diff --git a/test/Parser/missing-end-4.m b/test/Parser/missing-end-4.m index 8a96e64421..058daf91f4 100644 --- a/test/Parser/missing-end-4.m +++ b/test/Parser/missing-end-4.m @@ -32,9 +32,9 @@ @interface I @end @implementation I -@protocol P; // forward declarations of protocols in @implementations is allowed -@class C; // forward declarations of classes in @implementations is allowed -- (C

*) MyMeth {} +@protocol P; // expected-error {{Objective-C declarations may only appear in global scope}} +@class C; // expected-error {{Objective-C declarations may only appear in global scope}} +- (C

*) MyMeth {} // expected-error {{expected a type}} @end @interface I2 {} @@ -47,5 +47,5 @@ @implementation I3 - Meth {} + Cls {} -@protocol P3; +@protocol P3; // expected-error {{Objective-C declarations may only appear in global scope}} @end