r331421 - [ObjC] Supress the 'implementing unavailable method' warning when

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Wed May 2 18:12:06 PDT 2018


Author: arphaman
Date: Wed May  2 18:12:06 2018
New Revision: 331421

URL: http://llvm.org/viewvc/llvm-project?rev=331421&view=rev
Log:
[ObjC] Supress the 'implementing unavailable method' warning when
the method declaration is unavailable for an app extension platform

Rationale:
Classes are often shared between an app extension code and
non-app extension code. There's no way to remove the implementation
using preprocessor when building the app extension, so we should not warn here.

rdar://38150617

Added:
    cfe/trunk/test/SemaObjC/avoid-unavailable-implementation-warning-in-app-extension.m
Modified:
    cfe/trunk/include/clang/AST/DeclBase.h
    cfe/trunk/lib/AST/DeclBase.cpp
    cfe/trunk/lib/Sema/SemaDeclObjC.cpp

Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=331421&r1=331420&r2=331421&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Wed May  2 18:12:06 2018
@@ -644,9 +644,14 @@ public:
   ///
   /// \param EnclosingVersion The version to compare with. If empty, assume the
   /// deployment target version.
+  ///
+  /// \param RealizedPlatform If non-NULL and the availability result is found
+  /// in an available attribute it will set to the platform which is written in
+  /// the available attribute.
   AvailabilityResult
   getAvailability(std::string *Message = nullptr,
-                  VersionTuple EnclosingVersion = VersionTuple()) const;
+                  VersionTuple EnclosingVersion = VersionTuple(),
+                  StringRef *RealizedPlatform = nullptr) const;
 
   /// \brief Retrieve the version of the target platform in which this
   /// declaration was introduced.

Modified: cfe/trunk/lib/AST/DeclBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=331421&r1=331420&r2=331421&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp (original)
+++ cfe/trunk/lib/AST/DeclBase.cpp Wed May  2 18:12:06 2018
@@ -590,9 +590,11 @@ static AvailabilityResult CheckAvailabil
 }
 
 AvailabilityResult Decl::getAvailability(std::string *Message,
-                                         VersionTuple EnclosingVersion) const {
+                                         VersionTuple EnclosingVersion,
+                                         StringRef *RealizedPlatform) const {
   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
-    return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion);
+    return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion,
+                                                    RealizedPlatform);
 
   AvailabilityResult Result = AR_Available;
   std::string ResultMessage;
@@ -619,8 +621,11 @@ AvailabilityResult Decl::getAvailability
       AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
                                                 Message, EnclosingVersion);
 
-      if (AR == AR_Unavailable)
+      if (AR == AR_Unavailable) {
+        if (RealizedPlatform)
+          *RealizedPlatform = Availability->getPlatform()->getName();
         return AR_Unavailable;
+      }
 
       if (AR > Result) {
         Result = AR;

Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=331421&r1=331420&r2=331421&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Wed May  2 18:12:06 2018
@@ -266,12 +266,20 @@ static void DiagnoseObjCImplementedDepre
   if (!ND)
     return;
   bool IsCategory = false;
-  AvailabilityResult Availability = ND->getAvailability();
+  StringRef RealizedPlatform;
+  AvailabilityResult Availability = ND->getAvailability(
+      /*Message=*/nullptr, /*EnclosingVersion=*/VersionTuple(),
+      &RealizedPlatform);
   if (Availability != AR_Deprecated) {
     if (isa<ObjCMethodDecl>(ND)) {
       if (Availability != AR_Unavailable)
         return;
-      // Warn about implementing unavailable methods.
+      if (RealizedPlatform.empty())
+        RealizedPlatform = S.Context.getTargetInfo().getPlatformName();
+      // Warn about implementing unavailable methods, unless the unavailable
+      // is for an app extension.
+      if (RealizedPlatform.endswith("_app_extension"))
+        return;
       S.Diag(ImplLoc, diag::warn_unavailable_def);
       S.Diag(ND->getLocation(), diag::note_method_declared_at)
           << ND->getDeclName();

Added: cfe/trunk/test/SemaObjC/avoid-unavailable-implementation-warning-in-app-extension.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/avoid-unavailable-implementation-warning-in-app-extension.m?rev=331421&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/avoid-unavailable-implementation-warning-in-app-extension.m (added)
+++ cfe/trunk/test/SemaObjC/avoid-unavailable-implementation-warning-in-app-extension.m Wed May  2 18:12:06 2018
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -triple arm64-apple-ios11 -fapplication-extension -Wdeprecated-implementations -verify -Wno-objc-root-class %s
+// RUN: %clang_cc1 -triple arm64-apple-tvos11 -fapplication-extension -Wdeprecated-implementations -verify -Wno-objc-root-class %s
+// Declarations marked as 'unavailable' in an app extension should not generate a
+// warning on implementation.
+
+ at interface Parent
+- (void)ok __attribute__((availability(ios_app_extension,unavailable,message="not available")));
+- (void)reallyUnavail __attribute__((availability(ios,unavailable))); // expected-note {{method 'reallyUnavail' declared here}}
+- (void)reallyUnavail2 __attribute__((unavailable)); // expected-note {{method 'reallyUnavail2' declared here}}
+ at end
+
+ at interface Child : Parent
+ at end
+
+ at implementation Child
+
+- (void)ok { // no warning.
+}
+- (void)reallyUnavail { // expected-warning {{implementing unavailable method}}
+}
+- (void)reallyUnavail2 { // expected-warning {{implementing unavailable method}}
+}
+
+ at end




More information about the cfe-commits mailing list