Skip to content

Commit 77739b6

Browse files
committed
add a -Wdeprecated-switch-case flag
1 parent 9523bbd commit 77739b6

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ def DeprecatedCopyWithDtor : DiagGroup<"deprecated-copy-with-dtor", [DeprecatedC
233233
def DeprecatedLiteralOperator : DiagGroup<"deprecated-literal-operator">;
234234
// For compatibility with GCC.
235235
def : DiagGroup<"deprecated-copy-dtor", [DeprecatedCopyWithDtor]>;
236-
def DeprecatedDeclarations : DiagGroup<"deprecated-declarations">;
236+
def DeprecatedSwitchCase : DiagGroup<"deprecated-switch-case">;
237+
def DeprecatedDeclarations : DiagGroup<"deprecated-declarations", [DeprecatedSwitchCase]>;
237238
def DeprecatedRedundantConstexprStaticDef : DiagGroup<"deprecated-redundant-constexpr-static-def">;
238239
def UnavailableDeclarations : DiagGroup<"unavailable-declarations">;
239240
def UnguardedAvailabilityNew : DiagGroup<"unguarded-availability-new">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6009,6 +6009,8 @@ def note_not_found_by_two_phase_lookup : Note<"%0 should be declared prior to th
60096009
def err_undeclared_use : Error<"use of undeclared %0">;
60106010
def warn_deprecated : Warning<"%0 is deprecated">,
60116011
InGroup<DeprecatedDeclarations>;
6012+
def warn_deprecated_switch_case : Warning<"%0 is deprecated">,
6013+
InGroup<DeprecatedSwitchCase>;
60126014
def note_from_diagnose_if : Note<"from 'diagnose_if' attribute on %0:">;
60136015
def warn_property_method_deprecated :
60146016
Warning<"property access is using %0 method which is deprecated">,

clang/lib/Sema/SemaAvailability.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,13 @@ static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
549549
return;
550550
}
551551
case AR_Deprecated:
552-
// Don't diagnose deprecated values in case expressions: they still need to
553-
// be handled for the switch to be considered covered.
554-
if (S.currentEvaluationContext().IsCaseExpr)
555-
return;
552+
if (ObjCPropertyAccess)
553+
diag = diag::warn_property_method_deprecated;
554+
else if (S.currentEvaluationContext().IsCaseExpr)
555+
diag = diag::warn_deprecated_switch_case;
556+
else
557+
diag = diag::warn_deprecated;
556558

557-
diag = !ObjCPropertyAccess ? diag::warn_deprecated
558-
: diag::warn_property_method_deprecated;
559559
diag_message = diag::warn_deprecated_message;
560560
diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
561561
property_note_select = /* deprecated */ 0;

clang/test/Sema/switch-availability.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -verify -Wswitch -Wreturn-type -triple x86_64-apple-macosx10.12 %s
2+
// RUN: %clang_cc1 -verify -Wswitch -Wreturn-type -Wno-deprecated-switch-case -DNO_DEPRECATED_CASE -triple x86_64-apple-macosx10.12 %s
23

34
enum SwitchOne {
45
Unavail __attribute__((availability(macos, unavailable))),
@@ -29,6 +30,9 @@ void testSwitchThree(enum SwitchThree st) {
2930
enum SwitchFour {
3031
Red,
3132
Green,
33+
#ifndef NO_DEPRECATED_CASE
34+
// expected-note@+2{{'Blue' has been explicitly marked deprecated here}}
35+
#endif
3236
Blue [[deprecated]]
3337
};
3438

@@ -43,6 +47,9 @@ int testSwitchFourCovered(enum SwitchFour e) {
4347
switch (e) {
4448
case Red: return 1;
4549
case Green: return 2;
46-
case Blue: return 3; // no warning
47-
}
50+
#ifndef NO_DEPRECATED_CASE
51+
// expected-warning@+2{{'Blue' is deprecated}}
52+
#endif
53+
case Blue: return 3;
54+
} // no warning
4855
}

clang/test/SemaObjC/unguarded-availability.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,21 +343,21 @@ @interface BaseClass (CategoryWithNewProtocolRequirement) <NewProtocol>
343343
@end
344344

345345
typedef enum {
346-
AK_Dodo __attribute__((availability(macos, deprecated=10.3))), // expected-note 1 {{marked deprecated here}}
346+
AK_Dodo __attribute__((availability(macos, deprecated=10.3))), // expected-note 3 {{marked deprecated here}}
347347
AK_Cat __attribute__((availability(macos, introduced=10.4))),
348348
AK_CyborgCat __attribute__((availability(macos, introduced=10.12))), // expected-note {{'AK_CyborgCat' has been marked as being introduced in macOS 10.12 here, but the deployment target is macOS 10.9}}
349349
} Animals;
350350

351351
void switchAnimals(Animals a) {
352352
switch (a) {
353-
case AK_Dodo: break; // no warn
353+
case AK_Dodo: break; // expected-warning{{'AK_Dodo' is deprecated}}
354354
case AK_Cat: break;
355355
case AK_Cat|AK_CyborgCat: break; // expected-warning{{case value not in enum}}
356356
case AK_CyborgCat: break; // no warn
357357
}
358358

359359
switch (a) {
360-
case AK_Dodo...AK_CyborgCat: // no warn
360+
case AK_Dodo...AK_CyborgCat: // expected-warning {{'AK_Dodo' is depr}}
361361
break;
362362
}
363363

0 commit comments

Comments
 (0)