Properly consider the range of enum for range comparisons in C mode

In some cases, clang applies the C++ rules for computing the range of a
value when said value is an enum.

Instead, apply C semantics when in C mode.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183084 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp
index da24667..d1ba64e 100644
--- a/lib/Sema/SemaChecking.cpp
+++ b/lib/Sema/SemaChecking.cpp
@@ -4020,20 +4020,23 @@
     if (const ComplexType *CT = dyn_cast<ComplexType>(T))
       T = CT->getElementType().getTypePtr();
 
-    // For enum types, use the known bit width of the enumerators.
     if (const EnumType *ET = dyn_cast<EnumType>(T)) {
-      EnumDecl *Enum = ET->getDecl();
-      if (!Enum->isCompleteDefinition())
-        return IntRange(C.getIntWidth(QualType(T, 0)), false);
+      if (C.getLangOpts().CPlusPlus) {
+        // For enum types, use the known bit width of the enumerators.
+        EnumDecl *Enum = ET->getDecl();
+        if (!Enum->isCompleteDefinition())
+          return IntRange(C.getIntWidth(QualType(T, 0)), false);
 
-      unsigned NumPositive = Enum->getNumPositiveBits();
-      unsigned NumNegative = Enum->getNumNegativeBits();
+        unsigned NumPositive = Enum->getNumPositiveBits();
+        unsigned NumNegative = Enum->getNumNegativeBits();
 
-      if (NumNegative == 0)
-        return IntRange(NumPositive, true/*NonNegative*/);
-      else
-        return IntRange(std::max(NumPositive + 1, NumNegative),
-                        false/*NonNegative*/);
+        if (NumNegative == 0)
+          return IntRange(NumPositive, true/*NonNegative*/);
+        else
+          return IntRange(std::max(NumPositive + 1, NumNegative),
+              false/*NonNegative*/);
+      } else
+        T = C.getCanonicalType(ET->getDecl()->getIntegerType().getTypePtr());
     }
 
     const BuiltinType *BT = cast<BuiltinType>(T);
diff --git a/test/Sema/outof-range-constant-compare.c b/test/Sema/outof-range-constant-compare.c
index 4b1637c..5aa7bc8 100644
--- a/test/Sema/outof-range-constant-compare.c
+++ b/test/Sema/outof-range-constant-compare.c
@@ -147,3 +147,11 @@
 
     return 1;
 }
+
+typedef enum {
+    alpha=0, bravo, charlie, delta, echo
+} named_t;
+
+static int bar(named_t foo) {
+    return foo > 42;
+}