IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
IDecl->setLocEnd(EndProtoLoc);
}
+
+ CheckObjCDeclScope(IDecl);
return IDecl;
}
ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
ObjCAliasDecls[AliasName] = AliasDecl;
- TUScope->AddDecl(AliasDecl);
+
+ if (!CheckObjCDeclScope(AliasDecl))
+ TUScope->AddDecl(AliasDecl);
+
return AliasDecl;
}
PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
PDecl->setLocEnd(EndProtoLoc);
}
+
+ CheckObjCDeclScope(PDecl);
return PDecl;
}
Protocols.push_back(PDecl);
}
- return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
- &Protocols[0], Protocols.size());
+
+ ObjCForwardProtocolDecl *PDecl =
+ ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
+ &Protocols[0], Protocols.size());
+
+ CheckObjCDeclScope(PDecl);
+ return PDecl;
}
Sema::DeclTy *Sema::
CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
CDecl->setLocEnd(EndProtoLoc);
}
+
+ CheckObjCDeclScope(CDecl);
return CDecl;
}
/// TODO: Check that CatName, category name, is not used in another
// implementation.
ObjCCategoryImpls.push_back(CDecl);
+
+ CheckObjCDeclScope(CDecl);
return CDecl;
}
ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
IDecl, SDecl);
+ if (CheckObjCDeclScope(IMPDecl))
+ return IMPDecl;
+
// Check that there is no duplicate implementation of this class.
if (ObjCImplementations[ClassName])
// FIXME: Don't leak everything!
Interfaces.push_back(IDecl);
}
- return ObjCClassDecl::Create(Context, AtClassLoc,
- &Interfaces[0], Interfaces.size());
+ ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
+ &Interfaces[0],
+ Interfaces.size());
+
+ CheckObjCDeclScope(CDecl);
+ return CDecl;
}
return PIDecl;
}
+
+bool Sema::CheckObjCDeclScope(Decl *D)
+{
+ if (isa<TranslationUnitDecl>(CurContext))
+ return false;
+
+ Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
+ D->setInvalidDecl();
+
+ return true;
+}
--- /dev/null
+// RUN: clang -fsyntax-only -verify %s
+
+namespace C {
+
+@protocol P; //expected-error{{Objective-C declarations may only appear in global scope}}
+
+@class Bar; //expected-error{{Objective-C declarations may only appear in global scope}}
+
+@compatibility_alias Foo Bar; //expected-error{{Objective-C declarations may only appear in global scope}}
+
+@interface A //expected-error{{Objective-C declarations may only appear in global scope}}
+@end
+
+@implementation A //expected-error{{Objective-C declarations may only appear in global scope}}
+@end
+
+@protocol P //expected-error{{Objective-C declarations may only appear in global scope}}
+@end
+
+@interface A(C) //expected-error{{Objective-C declarations may only appear in global scope}}
+@end
+
+@implementation A(C) //expected-error{{Objective-C declarations may only appear in global scope}}
+@end
+
+}
+