[cfe-commits] r113450 - in /cfe/trunk: lib/Sema/AnalysisBasedWarnings.cpp test/Sema/return.c

Ted Kremenek kremenek at apple.com
Wed Sep 8 17:06:07 PDT 2010


Author: kremenek
Date: Wed Sep  8 19:06:07 2010
New Revision: 113450

URL: http://llvm.org/viewvc/llvm-project?rev=113450&view=rev
Log:
Enhance -Wreturn-type to not warn when control-flow is most likely limited by a switch statement explicitly covering
all the cases for an enum value.

Modified:
    cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
    cfe/trunk/test/Sema/return.c

Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=113450&r1=113449&r2=113450&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original)
+++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Wed Sep  8 19:06:07 2010
@@ -108,11 +108,15 @@
   bool HasFakeEdge = false;
   bool HasPlainEdge = false;
   bool HasAbnormalEdge = false;
-  for (CFGBlock::pred_iterator I=cfg->getExit().pred_begin(),
-       E = cfg->getExit().pred_end();
-       I != E;
-       ++I) {
-    CFGBlock& B = **I;
+
+  // Ignore default cases that aren't likely to be reachable because all
+  // enums in a switch(X) have explicit case statements.
+  CFGBlock::FilterOptions FO;
+  FO.IgnoreDefaultsWithCoveredEnums = 1;
+
+  for (CFGBlock::filtered_pred_iterator
+	 I = cfg->getExit().filtered_pred_start_end(FO); I.hasMore(); ++I) {
+    const CFGBlock& B = **I;
     if (!live[B.getBlockID()])
       continue;
     if (B.size() == 0) {

Modified: cfe/trunk/test/Sema/return.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/return.c?rev=113450&r1=113449&r2=113450&view=diff
==============================================================================
--- cfe/trunk/test/Sema/return.c (original)
+++ cfe/trunk/test/Sema/return.c Wed Sep  8 19:06:07 2010
@@ -242,3 +242,16 @@
 // Test warnings on ignored qualifiers on return types.
 const int ignored_c_quals(); // expected-warning{{'const' type qualifier on return type has no effect}}
 const volatile int ignored_cv_quals(); // expected-warning{{'const volatile' type qualifiers on return type have no effect}}
+
+// Test that for switch(enum) that if the switch statement covers all the cases
+// that we don't consider that for -Wreturn-type.
+enum Cases { C1, C2, C3, C4 };
+int test_enum_cases(enum Cases C) {
+  switch (C) {
+  case C1: return 1;
+  case C2: return 2;
+  case C4: return 3;
+  case C3: return 4;
+  }
+} // no-warning
+





More information about the cfe-commits mailing list