From d129f5ed08c4a70865be7465510f4651aa4f40fa Mon Sep 17 00:00:00 2001 From: Erik Pilkington Date: Wed, 5 Sep 2018 19:13:27 +0000 Subject: [PATCH] [Sema] Don't warn about omitting unavailable enum constants in a switch rdar://42717026 Differential revision: https://reviews.llvm.org/D51649 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@341490 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaStmt.cpp | 16 +++++++++++++++- test/Sema/switch-availability.c | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/Sema/switch-availability.c diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 8d620a79d3..61c6b37b4c 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -1164,7 +1164,21 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, SmallVector UnhandledNames; - for (EI = EnumVals.begin(); EI != EIEnd; EI++){ + for (EI = EnumVals.begin(); EI != EIEnd; EI++) { + // Don't warn about omitted unavailable EnumConstantDecls. + switch (EI->second->getAvailability()) { + case AR_Deprecated: + // Omitting a deprecated constant is ok; it should never materialize. + case AR_Unavailable: + continue; + + case AR_NotYetIntroduced: + // Partially available enum constants should be present. Note that we + // suppress -Wunguarded-availability diagnostics for such uses. + case AR_Available: + break; + } + // Drop unneeded case values while (CI != CaseVals.end() && CI->first < EI->first) CI++; diff --git a/test/Sema/switch-availability.c b/test/Sema/switch-availability.c new file mode 100644 index 0000000000..888edddac4 --- /dev/null +++ b/test/Sema/switch-availability.c @@ -0,0 +1,27 @@ +// RUN: %clang_cc1 -verify -Wswitch -triple x86_64-apple-macosx10.12 %s + +enum SwitchOne { + Unavail __attribute__((availability(macos, unavailable))), +}; + +void testSwitchOne(enum SwitchOne so) { + switch (so) {} // no warning +} + +enum SwitchTwo { + Ed __attribute__((availability(macos, deprecated=10.12))), + Vim __attribute__((availability(macos, deprecated=10.13))), + Emacs, +}; + +void testSwitchTwo(enum SwitchTwo st) { + switch (st) {} // expected-warning{{enumeration values 'Vim' and 'Emacs' not handled in switch}} +} + +enum SwitchThree { + New __attribute__((availability(macos, introduced=1000))), +}; + +void testSwitchThree(enum SwitchThree st) { + switch (st) {} // expected-warning{{enumeration value 'New' not handled in switch}} +} -- 2.40.0