]> granicus.if.org Git - clang/commitdiff
Warn about uses of `@available` that can't suppress the
authorAlex Lorenz <arphaman@gmail.com>
Wed, 24 May 2017 15:15:29 +0000 (15:15 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Wed, 24 May 2017 15:15:29 +0000 (15:15 +0000)
-Wunguarded-availability warnings

rdar://32306520

Differential Revision: https://reviews.llvm.org/D33450

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@303761 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclAttr.cpp
lib/Sema/SemaExpr.cpp
test/Parser/objc-available.m
test/SemaObjC/unguarded-availability.m

index 7644a0826416720bc1fa8656cdf195848b1b92f0..56dd52db4c9292386cc7d9254452809d85ce1da4 100644 (file)
@@ -2886,6 +2886,10 @@ def warn_partial_message : Warning<"%0 is partial: %1">,
 def warn_partial_fwdclass_message : Warning<
     "%0 may be partial because the receiver type is unknown">,
     InGroup<UnguardedAvailability>, DefaultIgnore;
+def warn_at_available_unchecked_use : Warning<
+  "%select{@available|__builtin_available}0 does not guard availability here; "
+  "use if (%select{@available|__builtin_available}0) instead">,
+  InGroup<DiagGroup<"unsupported-availability-guard">>;
 
 // Thread Safety Attributes
 def warn_invalid_capability_name : Warning<
index d484dee1d762cb73863c92e4a5ddc6cd0955b7bf..8c4b8c358455572930b0f153468dc130bac8be85 100644 (file)
@@ -7284,6 +7284,12 @@ public:
     return true;
   }
 
+  bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
+    SemaRef.Diag(E->getLocStart(), diag::warn_at_available_unchecked_use)
+        << (!SemaRef.getLangOpts().ObjC1);
+    return true;
+  }
+
   bool VisitTypeLoc(TypeLoc Ty);
 };
 
index b83f42dd190db6bc3ed8b714689fe9f911061660..d2443d2f03e125b30184f5c143f3abe4bdc7b7b8 100644 (file)
@@ -15762,6 +15762,13 @@ ExprResult Sema::ActOnObjCAvailabilityCheckExpr(
   if (Spec != AvailSpecs.end())
     Version = Spec->getVersion();
 
+  // The use of `@available` in the enclosing function should be analyzed to
+  // warn when it's used inappropriately (i.e. not if(@available)).
+  if (getCurFunctionOrMethodDecl())
+    getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
+  else if (getCurBlock() || getCurLambda())
+    getCurFunction()->HasPotentialAvailabilityViolations = true;
+
   return new (Context)
       ObjCAvailabilityCheckExpr(Version, AtLoc, RParen, Context.BoolTy);
 }
index 49bc53930655705d0a68cd5a0177f896a6007fd0..01d59f8f525395a2d6e15f03fd1a79cfc18bc1b8 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -Wunguarded-availability -triple x86_64-apple-macosx10.10.0 -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wunguarded-availability -Wno-unsupported-availability-guard -triple x86_64-apple-macosx10.10.0 -verify %s
 
 void f() {
 
index 82823204ba61b9c1752a169783e7009ab4e109ca..5febee038e3cd968625207b94a44afd59d04da25 100644 (file)
@@ -10,7 +10,7 @@ int func_10_11() AVAILABLE_10_11; // expected-note 4 {{'func_10_11' has been exp
 #ifdef OBJCPP
 // expected-note@+2 6 {{marked partial here}}
 #endif
-int func_10_12() AVAILABLE_10_12; // expected-note 6 {{'func_10_12' has been explicitly marked partial here}}
+int func_10_12() AVAILABLE_10_12; // expected-note 7 {{'func_10_12' has been explicitly marked partial here}}
 
 int func_10_0() AVAILABLE_10_0;
 
@@ -155,6 +155,16 @@ void test_at(Subscriptable *x) {
   id y = x[42]; // expected-warning{{'objectAtIndexedSubscript:' is only available on macOS 10.12 or newer}} expected-note{{@available}}
 }
 
+void uncheckAtAvailable() {
+  if (@available(macOS 10.12, *) || 0) // expected-warning {{@available does not guard availability here; use if (@available) instead}}
+    func_10_12(); // expected-warning {{'func_10_12' is only available on macOS 10.12 or newer}}
+  // expected-note@-1 {{enclose 'func_10_12' in an @available check to silence this warning}}
+}
+
+void justAtAvailable() {
+  int availability = @available(macOS 10.12, *); // expected-warning {{@available does not guard availability here; use if (@available) instead}}
+}
+
 #ifdef OBJCPP
 
 int f(char) AVAILABLE_10_12;