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

David Majnemer david.majnemer at gmail.com
Sun Jun 2 01:11:23 PDT 2013


Author: majnemer
Date: Sun Jun  2 03:11:22 2013
New Revision: 183084

URL: http://llvm.org/viewvc/llvm-project?rev=183084&view=rev
Log:
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.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/outof-range-constant-compare.c

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=183084&r1=183083&r2=183084&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Sun Jun  2 03:11:22 2013
@@ -4020,20 +4020,23 @@ struct IntRange {
     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);

Modified: cfe/trunk/test/Sema/outof-range-constant-compare.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/outof-range-constant-compare.c?rev=183084&r1=183083&r2=183084&view=diff
==============================================================================
--- cfe/trunk/test/Sema/outof-range-constant-compare.c (original)
+++ cfe/trunk/test/Sema/outof-range-constant-compare.c Sun Jun  2 03:11:22 2013
@@ -147,3 +147,11 @@ int main()
 
     return 1;
 }
+
+typedef enum {
+    alpha=0, bravo, charlie, delta, echo
+} named_t;
+
+static int bar(named_t foo) {
+    return foo > 42;
+}





More information about the cfe-commits mailing list