Add -Wswitch-enum-redundant-default.

This warning acts as the complement to the main -Wswitch-enum warning (which
warns whenever a switch over enum without a default doesn't cover all values of
 the enum) & has been an an-doc coding convention in LLVM and Clang in my
experience. The purpose is to ensure there's never a "dead" default in a
switch-over-enum because this would hide future -Wswitch-enum errors.

The name warning has a separate flag name so it can be disabled but it's grouped
under -Wswitch-enum & is on-by-default because of this.

The existing violations of this rule in test cases have had the warning disabled
& I've added a specific test for the new behavior (many negative cases already
exist in the same test file - and none regressed - so I didn't add more).

Reviewed by Ted Kremenek ( http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20120116/051690.html )

llvm-svn: 148640
This commit is contained in:
David Blaikie 2012-01-21 18:12:07 +00:00
parent fb6ecdf27c
commit 645ae0ce10
7 changed files with 31 additions and 12 deletions

View File

@ -178,7 +178,8 @@ def InvalidOffsetof : DiagGroup<"invalid-offsetof">;
def : DiagGroup<"strict-prototypes">;
def StrictSelector : DiagGroup<"strict-selector-match">;
def MethodDuplicate : DiagGroup<"duplicate-method-match">;
def SwitchEnum : DiagGroup<"switch-enum">;
def SwitchEnumRedundantDefault : DiagGroup<"switch-enum-redundant-default">;
def SwitchEnum : DiagGroup<"switch-enum", [SwitchEnumRedundantDefault]>;
def Switch : DiagGroup<"switch", [SwitchEnum]>;
def Trigraphs : DiagGroup<"trigraphs">;

View File

@ -4946,19 +4946,22 @@ def warn_case_empty_range : Warning<"empty case range specified">;
def warn_missing_case_for_condition :
Warning<"no case matching constant switch condition '%0'">;
def warn_missing_case1 : Warning<"enumeration value %0 not handled in switch">,
InGroup<DiagGroup<"switch-enum"> >;
InGroup<SwitchEnum>;
def warn_missing_case2 : Warning<
"enumeration values %0 and %1 not handled in switch">,
InGroup<DiagGroup<"switch-enum"> >;
InGroup<SwitchEnum>;
def warn_missing_case3 : Warning<
"enumeration values %0, %1, and %2 not handled in switch">,
InGroup<DiagGroup<"switch-enum"> >;
InGroup<SwitchEnum>;
def warn_missing_cases : Warning<
"%0 enumeration values not handled in switch: %1, %2, %3...">,
InGroup<DiagGroup<"switch-enum"> >;
InGroup<SwitchEnum>;
def warn_unreachable_default : Warning<
"default is unreachable as all enumeration values are accounted for">,
InGroup<SwitchEnumRedundantDefault>;
def warn_not_in_enum : Warning<"case value not in enumerated type %0">,
InGroup<DiagGroup<"switch-enum"> >;
InGroup<SwitchEnum>;
def err_typecheck_statement_requires_scalar : Error<
"statement requires expression of scalar type (%0 invalid)">;
def err_typecheck_statement_requires_integer : Error<

View File

@ -924,11 +924,17 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
if (RI == CaseRanges.end() || EI->first < RI->first) {
hasCasesNotInSwitch = true;
if (!TheDefaultStmt)
UnhandledNames.push_back(EI->second->getDeclName());
}
}
if (TheDefaultStmt) {
if (UnhandledNames.size() == 0)
Diag(TheDefaultStmt->getDefaultLoc(), diag::warn_unreachable_default);
else
UnhandledNames.clear();
}
// Produce a nice diagnostic if multiple values aren't handled.
switch (UnhandledNames.size()) {
case 0: break;

View File

@ -288,3 +288,12 @@ void test17(int x) {
case 0: return;
}
}
int test18() {
enum { A, B } a;
switch (a) {
case A: return 0;
case B: return 1;
default: return 2; // expected-warning {{default is unreachable as all enumeration values are accounted for}}
}
}

View File

@ -1,4 +1,4 @@
// RUN: %clang %s -fsyntax-only -Xclang -verify -fblocks -Wunreachable-code -Wno-unused-value
// RUN: %clang %s -fsyntax-only -Xclang -verify -fblocks -Wunreachable-code -Wno-unused-value -Wno-switch-enum-redundant-default
int halt() __attribute__((noreturn));
int live();

View File

@ -1,4 +1,4 @@
// RUN: %clang_cc1 -verify %s
// RUN: %clang_cc1 -verify -Wno-switch-enum-redundant-default %s
enum E {
one,

View File

@ -1,5 +1,5 @@
// RUN: %clang_cc1 %s -fsyntax-only -verify -Wreturn-type -Wmissing-noreturn -Wno-unreachable-code
// RUN: %clang_cc1 %s -fsyntax-only -std=c++11 -verify -Wreturn-type -Wmissing-noreturn -Wno-unreachable-code
// RUN: %clang_cc1 %s -fsyntax-only -verify -Wreturn-type -Wmissing-noreturn -Wno-unreachable-code -Wno-switch-enum-redundant-default
// RUN: %clang_cc1 %s -fsyntax-only -std=c++11 -verify -Wreturn-type -Wmissing-noreturn -Wno-unreachable-code -Wno-switch-enum-redundant-default
// A destructor may be marked noreturn and should still influence the CFG.
void pr6884_abort() __attribute__((noreturn));