[Sema] Display an objc_subclassing_restricted error for Objective-C
implementation declarations when appropriate.

rdar://28753587
(cherry picked from commit eb81c6fb87dd0fd5661209e2cf2aef5d77ca56b1)
diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp
index 0a5279a..7f6d29f 100644
--- a/lib/Sema/SemaDeclObjC.cpp
+++ b/lib/Sema/SemaDeclObjC.cpp
@@ -3859,6 +3859,18 @@
         Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
       }
 
+      if (const ObjCInterfaceDecl *Super = IDecl->getSuperClass()) {
+        // An interface can subclass another interface with a
+        // objc_subclassing_restricted attribute when it has that attribute as
+        // well (because of interfaces imported from Swift). Therefore we have
+        // to check if we can subclass in the implementation as well.
+        if (IDecl->hasAttr<ObjCSubclassingRestrictedAttr>() &&
+            Super->hasAttr<ObjCSubclassingRestrictedAttr>()) {
+          Diag(IC->getLocation(), diag::err_restricted_superclass_mismatch);
+          Diag(Super->getLocation(), diag::note_class_declared);
+        }
+      }
+
       if (LangOpts.ObjCRuntime.isNonFragile()) {
         while (IDecl->getSuperClass()) {
           DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
diff --git a/test/SemaObjC/subclassing-restricted-attr.m b/test/SemaObjC/subclassing-restricted-attr.m
index 2e77df8..5edd342 100644
--- a/test/SemaObjC/subclassing-restricted-attr.m
+++ b/test/SemaObjC/subclassing-restricted-attr.m
@@ -21,3 +21,16 @@
 __attribute__((objc_subclassing_restricted))
 @interface Sub2Class : PlainRoot // okay
 @end
+
+// rdar://28753587
+__attribute__((objc_subclassing_restricted))
+@interface SuperImplClass // expected-note {{class is declared here}}
+@end
+@implementation SuperImplClass
+@end
+
+__attribute__((objc_subclassing_restricted))
+@interface SubImplClass : SuperImplClass
+@end
+@implementation SubImplClass // expected-error {{cannot subclass a class with objc_subclassing_restricted attribute}}
+@end