Add new attribute 'objc_suppress_protocol' to suppress protocol conformance for a class.
The idea is to allow a class to stipulate that its methods (and those
of its parents) cannot be used for protocol conformance in a subclass.
A subclass is then explicitly required to re-implement those methods
of they are present in the class marked with this attribute.
Currently the attribute can only be applied to an @interface, and
not a category or class extension. This is by design. Unlike
protocol conformance, where a category can add explicit conformance
of a protocol to class, this anti-conformance really needs to be
observed uniformly by all clients of the class. That's because
the absence of the attribute implies more permissive checking of
protocol conformance.
This unfortunately required changing method lookup in ObjCInterfaceDecl
to take an optional protocol parameter. This should not slow down
method lookup in most cases, and is just used for protocol conformance.
llvm-svn: 195323
diff --git a/clang/lib/AST/DeclObjC.cpp b/clang/lib/AST/DeclObjC.cpp
index b2b5b70..d759c07 100644
--- a/clang/lib/AST/DeclObjC.cpp
+++ b/clang/lib/AST/DeclObjC.cpp
@@ -457,9 +457,11 @@
/// When argument category "C" is specified, any implicit method found
/// in this category is ignored.
ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
- bool isInstance,
- bool shallowCategoryLookup,
- const ObjCCategoryDecl *C) const {
+ bool isInstance,
+ bool shallowCategoryLookup,
+ const ObjCCategoryDecl *C,
+ const ObjCProtocolDecl *P) const
+{
// FIXME: Should make sure no callers ever do this.
if (!hasDefinition())
return 0;
@@ -470,7 +472,23 @@
if (data().ExternallyCompleted)
LoadExternalDefinition();
- while (ClassDecl != NULL) {
+ while (ClassDecl) {
+ // If we are looking for a method that is part of protocol conformance,
+ // check if the class has been marked to suppress conformance
+ // of that protocol.
+ if (P && ClassDecl->hasAttrs()) {
+ const AttrVec &V = ClassDecl->getAttrs();
+ const IdentifierInfo *PI = P->getIdentifier();
+ for (AttrVec::const_iterator I = V.begin(), E = V.end(); I != E; ++I) {
+ if (const ObjCSuppressProtocolAttr *A =
+ dyn_cast<ObjCSuppressProtocolAttr>(*I)){
+ if (A->getProtocol() == PI) {
+ return 0;
+ }
+ }
+ }
+ }
+
if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
return MethodDecl;