[cfe-commits] r84346 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaStmt.cpp test/SemaCXX/switch.cpp

Edward O'Callaghan eocallaghan at auroraux.org
Sat Oct 17 12:33:14 PDT 2009


Author: evocallaghan
Date: Sat Oct 17 14:32:54 2009
New Revision: 84346

URL: http://llvm.org/viewvc/llvm-project?rev=84346&view=rev
Log:
Fix for PR5190, Credit to Zhanyong Wan.

Added:
    cfe/trunk/test/SemaCXX/switch.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaStmt.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=84346&r1=84345&r2=84346&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sat Oct 17 14:32:54 2009
@@ -2166,6 +2166,8 @@
 def err_default_not_in_switch : Error<
   "'default' statement not in switch statement">;
 def err_case_not_in_switch : Error<"'case' statement not in switch statement">;
+def warn_bool_switch_condition : Warning<
+  "switch condition is a bool">;
 def warn_case_value_overflow : Warning<
   "overflow converting case value to switch condition type (%0 to %1)">;
 def err_duplicate_case : Error<"duplicate case value '%0'">;

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=84346&r1=84345&r2=84346&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Sat Oct 17 14:32:54 2009
@@ -407,11 +407,20 @@
   QualType CondTypeBeforePromotion =
       GetTypeBeforeIntegralPromotion(CondExpr);
 
-  if (!CondExpr->isTypeDependent() &&
-      !CondType->isIntegerType()) { // C99 6.8.4.2p1
-    Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
-      << CondType << CondExpr->getSourceRange();
-    return StmtError();
+  if (!CondExpr->isTypeDependent()) {
+    if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
+      Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer)
+          << CondType << CondExpr->getSourceRange();
+      return StmtError();
+    }
+
+    if (CondTypeBeforePromotion->isBooleanType()) {
+      // switch(bool_expr) {...} is often a programmer error, e.g.
+      //   switch(n && mask) { ... }  // Doh - should be "n & mask".
+      // One can always use an if statement instead of switch(bool_expr).
+      Diag(SwitchLoc, diag::warn_bool_switch_condition)
+          << CondExpr->getSourceRange();
+    }
   }
 
   // Get the bitwidth of the switched-on value before promotions.  We must

Added: cfe/trunk/test/SemaCXX/switch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/switch.cpp?rev=84346&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/switch.cpp (added)
+++ cfe/trunk/test/SemaCXX/switch.cpp Sat Oct 17 14:32:54 2009
@@ -0,0 +1,15 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+void test() {
+  bool x = true;
+  switch (x) { // expected-warning {{bool}}
+    case 0:
+      break;
+  }
+
+  int n = 3;
+  switch (n && 1) { // expected-warning {{bool}}
+    case 1:
+      break;
+  }
+}





More information about the cfe-commits mailing list