From: Alex Lorenz Date: Wed, 26 Apr 2017 14:20:02 +0000 (+0000) Subject: -Wunguarded-availability should support if (@available) checks in top-level X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7ca19008749f426183ab9013a06e6eda89560e96;p=clang -Wunguarded-availability should support if (@available) checks in top-level blocks and lambdas Prior to this commit Clang emitted the old "partial availability" warning for expressions that referred to declarations that were not yet introduced in blocks and lambdas that were not in a function/method. This commit ensures that top-level blocks and lambdas use the new unguarded availability checks. rdar://31835952 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@301409 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 027b3fe0e7..17c6975ca5 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -7220,6 +7220,8 @@ void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) { Body = FD->getBody(); } else if (auto *MD = dyn_cast(D)) Body = MD->getBody(); + else if (auto *BD = dyn_cast(D)) + Body = BD->getBody(); assert(Body && "Need a body here!"); diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 5a56f70937..00480f821f 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -171,9 +171,14 @@ DiagnoseAvailabilityOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc, if (AvailabilityResult Result = S.ShouldDiagnoseAvailabilityOfDecl(D, &Message)) { - if (Result == AR_NotYetIntroduced && S.getCurFunctionOrMethodDecl()) { - S.getEnclosingFunction()->HasPotentialAvailabilityViolations = true; - return; + if (Result == AR_NotYetIntroduced) { + if (S.getCurFunctionOrMethodDecl()) { + S.getEnclosingFunction()->HasPotentialAvailabilityViolations = true; + return; + } else if (S.getCurBlock() || S.getCurLambda()) { + S.getCurFunction()->HasPotentialAvailabilityViolations = true; + return; + } } const ObjCPropertyDecl *ObjCPDecl = nullptr; @@ -12498,6 +12503,9 @@ ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, BSI->TheDecl->setBody(cast(Body)); + if (Body && getCurFunction()->HasPotentialAvailabilityViolations) + DiagnoseUnguardedAvailabilityViolations(BSI->TheDecl); + // Try to apply the named return value optimization. We have to check again // if we can do this, though, because blocks keep return statements around // to deduce an implicit return type. diff --git a/test/SemaObjC/unguarded-availability.m b/test/SemaObjC/unguarded-availability.m index 4369a0e329..ae921f4a27 100644 --- a/test/SemaObjC/unguarded-availability.m +++ b/test/SemaObjC/unguarded-availability.m @@ -1,5 +1,5 @@ // RUN: %clang_cc1 -triple x86_64-apple-macosx-10.9 -Wunguarded-availability -fblocks -fsyntax-only -verify %s -// RUN: %clang_cc1 -xobjective-c++ -DOBJCPP -triple x86_64-apple-macosx-10.9 -Wunguarded-availability -fblocks -fsyntax-only -verify %s +// RUN: %clang_cc1 -xobjective-c++ -std=c++11 -DOBJCPP -triple x86_64-apple-macosx-10.9 -Wunguarded-availability -fblocks -fsyntax-only -verify %s #define AVAILABLE_10_0 __attribute__((availability(macos, introduced = 10.0))) #define AVAILABLE_10_11 __attribute__((availability(macos, introduced = 10.11))) @@ -8,9 +8,9 @@ int func_10_11() AVAILABLE_10_11; // expected-note 4 {{'func_10_11' has been explicitly marked partial here}} #ifdef OBJCPP -// expected-note@+2 {{marked partial here}} +// expected-note@+2 2 {{marked partial here}} #endif -int func_10_12() AVAILABLE_10_12; // expected-note 5 {{'func_10_12' has been explicitly marked partial here}} +int func_10_12() AVAILABLE_10_12; // expected-note 6 {{'func_10_12' has been explicitly marked partial here}} int func_10_0() AVAILABLE_10_0; @@ -129,6 +129,12 @@ void test_params(int_10_12 x); // expected-warning {{'int_10_12' is partial: int void test_params2(int_10_12 x) AVAILABLE_10_12; // no warn +void (^topLevelBlockDecl)() = ^ { + func_10_12(); // expected-warning{{'func_10_12' is only available on macOS 10.12 or newer}} expected-note{{@available}} + if (@available(macos 10.12, *)) + func_10_12(); +}; + #ifdef OBJCPP int f(char) AVAILABLE_10_12; @@ -176,4 +182,10 @@ int instantiate_availability() { with_availability_attr(); // expected-warning{{'with_availability_attr' is only available on macOS 10.11 or newer}} expected-warning{{'int_10_12' is only available on macOS 10.12 or newer}} expected-note 2 {{enclose}} } +auto topLevelLambda = [] () { + func_10_12(); // expected-warning{{'func_10_12' is only available on macOS 10.12 or newer}} expected-note{{@available}} + if (@available(macos 10.12, *)) + func_10_12(); +}; + #endif