Skip to content

Commit cf6606e

Browse files
committed
Don't warn about unreachable code in @available
rdar://137999979
1 parent 7135e91 commit cf6606e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

clang/lib/Analysis/ReachableCode.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,13 @@ static bool shouldTreatSuccessorsAsReachable(const CFGBlock *B,
308308
if (const auto *IS = dyn_cast<IfStmt>(Term);
309309
IS != nullptr && IS->isConstexpr())
310310
return true;
311+
312+
// Do not treat successors of `if (@available(domain:))` as unreachable.
313+
if (const auto *IS = dyn_cast<IfStmt>(Term))
314+
if (const auto *AC = dyn_cast<ObjCAvailabilityCheckExpr>(
315+
IS->getCond()->IgnoreParenImpCasts());
316+
AC && AC->hasDomainName())
317+
return true;
311318
}
312319

313320
const Stmt *Cond = B->getTerminatorCondition(/* stripParens */ false);

clang/test/Sema/feature-availability.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang_cc1 -triple arm64-apple-macosx15 -fblocks -ffeature-availability=feature1:1 -ffeature-availability=feature2:0 -ffeature-availability=feature3:on -fsyntax-only -verify %s
2-
// RUN: %clang_cc1 -triple arm64-apple-macosx15 -fblocks -fsyntax-only -verify -DUSE_DOMAIN %s
1+
// RUN: %clang_cc1 -triple arm64-apple-macosx15 -fblocks -ffeature-availability=feature1:1 -ffeature-availability=feature2:0 -ffeature-availability=feature3:on -fsyntax-only -Wunreachable-code -verify %s
2+
// RUN: %clang_cc1 -triple arm64-apple-macosx15 -fblocks -fsyntax-only -Wunreachable-code -verify -DUSE_DOMAIN %s
33

44
#include <feature-availability.h>
55

@@ -33,6 +33,14 @@ __attribute__((availability(domain:feature4, AVAIL))) int g4;
3333
__attribute__((availability(domain:feature4, UNAVAIL))) int g5;
3434
#endif
3535

36+
void test_unreachable_code(void) {
37+
if (__builtin_available(domain:feature1)) {
38+
} else {
39+
// Warning -Wunreachable-code isn't emitted.
40+
(void)2;
41+
}
42+
}
43+
3644
__attribute__((availability(domain:2, AVAIL))) void func5(void); // expected-error {{expected a domain name}}
3745
__attribute__((availability(domain, AVAIL))) void func4(void); // expected-error {{expected ':'}}
3846
__attribute__((availability(domain:, AVAIL))) void func0(void); // expected-error {{expected a domain name}}

0 commit comments

Comments
 (0)