]> granicus.if.org Git - clang/commitdiff
Add -Wswitch-enum-redundant-default.
authorDavid Blaikie <dblaikie@gmail.com>
Sat, 21 Jan 2012 18:12:07 +0000 (18:12 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Sat, 21 Jan 2012 18:12:07 +0000 (18:12 +0000)
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 )

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148640 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaStmt.cpp
test/Sema/switch.c
test/Sema/warn-unreachable.c
test/SemaCXX/gnu-case-ranges.cpp
test/SemaCXX/return-noreturn.cpp

index 9c0358041331043f2e4df9abd24109ca37d2d80e..ca0ce99c2c2dde035576592a1060c7e1e004658a 100644 (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">;
 
index f685c9f2b759d1ab1b693bc58ceb7c6aed0cce15..3ffd580b83b9406002d81f2ba8bf20b95079e87d 100644 (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<
index a53e397243bfdcaf614e3dc25debfcc8f6749986..0be3e43b5a7a7b4ccd4f64d03872ffc1f96fdabd 100644 (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());
+          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;
index ef2e4c5d9b7c23ee100aed550d6a519188e0bf0d..63ffed18e3120e279b1efe88b6860c5bb74d85b2 100644 (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}}
+  }
+}
index 3ad53c707bae8ee31e2a26832bd88fb3cc8f5bca..46a680f0ef864f0e782fae45b847f35eb2c23524 100644 (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();
index c1c18a89481cbe66ed5cbf0d475561a338e7d725..94100d2e83e7d598fe286e52207e9a959452cb8f 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify %s
+// RUN: %clang_cc1 -verify -Wno-switch-enum-redundant-default %s
 
 enum E {
     one,
index e06ba403efec308e726bc7013810fa9add5235a9..4b1f82dc6a3c5a2883b04a2c09d09711fb64fe38 100644 (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));