[cfe-commits] r41328 - in /cfe/trunk: Sema/SemaStmt.cpp clang.xcodeproj/project.pbxproj include/clang/Basic/DiagnosticKinds.def test/Sema/switch.c

Chris Lattner sabre at nondot.org
Thu Aug 23 10:48:14 PDT 2007


Author: lattner
Date: Thu Aug 23 12:48:14 2007
New Revision: 41328

URL: http://llvm.org/viewvc/llvm-project?rev=41328&view=rev
Log:
detect and diagnose empty case ranges:

switch.c:16:8: warning: empty case range specified
  case 100 ... 99: ;  // expected-warning {{empty case range}}
       ^~~~~~~~~~


Modified:
    cfe/trunk/Sema/SemaStmt.cpp
    cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/test/Sema/switch.c

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

==============================================================================
--- cfe/trunk/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/Sema/SemaStmt.cpp Thu Aug 23 12:48:14 2007
@@ -298,7 +298,7 @@
     
     // Scan the ranges, computing the high values and removing empty ranges.
     std::vector<llvm::APSInt> HiVals;
-    for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) {
+    for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
       CaseStmt *CR = CaseRanges[i].second;
       llvm::APSInt HiVal(32);
       CR->getRHS()->isIntegerConstantExpr(HiVal, Context);
@@ -308,9 +308,15 @@
                                          CR->getRHS()->getLocStart(),
                                          diag::warn_case_value_overflow);
       
-      // FIXME: if the low value is bigger than the high value, the case is
-      // empty: emit "empty range specified" warning and drop it.
-
+      // If the low value is bigger than the high value, the case is empty.
+      if (CaseRanges[i].first > HiVal) {
+        Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
+             SourceRange(CR->getLHS()->getLocStart(),
+                         CR->getRHS()->getLocEnd()));
+        CaseRanges.erase(CaseRanges.begin()+i);
+        --i, --e;
+        continue;
+      }
       HiVals.push_back(HiVal);
     }
 

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=41328&r1=41327&r2=41328&view=diff

==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Thu Aug 23 12:48:14 2007
@@ -206,7 +206,7 @@
 		1ABC36930C7A4BDC006DB0AB /* CGBuiltin.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CGBuiltin.cpp; path = CodeGen/CGBuiltin.cpp; sourceTree = "<group>"; };
 		84D9A8870C1A57E100AC7ABC /* AttributeList.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = AttributeList.cpp; path = Parse/AttributeList.cpp; sourceTree = "<group>"; };
 		84D9A88B0C1A581300AC7ABC /* AttributeList.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = AttributeList.h; path = clang/Parse/AttributeList.h; sourceTree = "<group>"; };
-		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
+		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
 		DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; };
 		DE06756B0C051CFE00EBBFD8 /* ParseExprCXX.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ParseExprCXX.cpp; path = Parse/ParseExprCXX.cpp; sourceTree = "<group>"; };
 		DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; };

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=41328&r1=41327&r2=41328&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Thu Aug 23 12:48:14 2007
@@ -719,6 +719,8 @@
      "duplicate case value '%0'")
 DIAG(err_duplicate_case_prev, ERROR,
      "previous case value occurrence defined here")
+DIAG(warn_case_empty_range, WARNING,
+     "empty case range specified")
 DIAG(err_typecheck_return_incompatible, ERROR,
      "incompatible type returning '%1', expected '%0'")
 DIAG(ext_typecheck_return_pointer_int, WARNING,

Modified: cfe/trunk/test/Sema/switch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/switch.c?rev=41328&r1=41327&r2=41328&view=diff

==============================================================================
--- cfe/trunk/test/Sema/switch.c (original)
+++ cfe/trunk/test/Sema/switch.c Thu Aug 23 12:48:14 2007
@@ -12,6 +12,8 @@
   case 5000000000LL:  // expected-warning {{overflow}}
   case 42:            // expected-error {{duplicate case value}}
    ;
+
+  case 100 ... 99: ;  // expected-warning {{empty case range}}
   }
 }
 





More information about the cfe-commits mailing list