[cfe-commits] r41572 - in /cfe/trunk: AST/Type.cpp include/clang/AST/Type.h test/CodeGen/enum.c

Chris Lattner sabre at nondot.org
Wed Aug 29 10:48:46 PDT 2007


Author: lattner
Date: Wed Aug 29 12:48:46 2007
New Revision: 41572

URL: http://llvm.org/viewvc/llvm-project?rev=41572&view=rev
Log:
Teach Type::is[un]SignedIntegerType about enum decls.  This allows the code generator
to emit signed comparisons when needed for enum decl references.  This implements
test/CodeGen/enum.c.  I think enums should be good now.

Added:
    cfe/trunk/test/CodeGen/enum.c
Modified:
    cfe/trunk/AST/Type.cpp
    cfe/trunk/include/clang/AST/Type.h

Modified: cfe/trunk/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Type.cpp?rev=41572&r1=41571&r2=41572&view=diff

==============================================================================
--- cfe/trunk/AST/Type.cpp (original)
+++ cfe/trunk/AST/Type.cpp Wed Aug 29 12:48:46 2007
@@ -391,21 +391,39 @@
   return false;
 }
 
+/// isSignedIntegerType - Return true if this is an integer type that is
+/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
+/// an enum decl which has a signed representation, or a vector of signed
+/// integer element type.
 bool Type::isSignedIntegerType() const {
   if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
     return BT->getKind() >= BuiltinType::Char_S &&
            BT->getKind() <= BuiltinType::LongLong;
   }
+  
+  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
+    if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
+      return ED->getIntegerType()->isSignedIntegerType();
+  
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
     return VT->getElementType()->isSignedIntegerType();
   return false;
 }
 
+/// isUnsignedIntegerType - Return true if this is an integer type that is
+/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
+/// decl which has an unsigned representation, or a vector of unsigned integer
+/// element type.
 bool Type::isUnsignedIntegerType() const {
   if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
     return BT->getKind() >= BuiltinType::Bool &&
            BT->getKind() <= BuiltinType::ULongLong;
   }
+
+  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
+    if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
+      return ED->getIntegerType()->isUnsignedIntegerType();
+
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
     return VT->getElementType()->isUnsignedIntegerType();
   return false;

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=41572&r1=41571&r2=41572&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Wed Aug 29 12:48:46 2007
@@ -279,13 +279,17 @@
   bool isPromotableIntegerType() const; // C99 6.3.1.1p2
 
   /// isSignedIntegerType - Return true if this is an integer type that is
-  /// signed, according to C99 6.2.5p4.
+  /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
+  /// an enum decl which has a signed representation, or a vector of signed
+  /// integer element type.
   bool isSignedIntegerType() const;
 
   /// isUnsignedIntegerType - Return true if this is an integer type that is
-  /// unsigned, according to C99 6.2.5p6. Note that this returns true for _Bool.
+  /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
+  /// decl which has an unsigned representation, or a vector of unsigned integer
+  /// element type.
   bool isUnsignedIntegerType() const;
-  
+
   /// isConstantSizeType - Return true if this is not a variable sized type,
   /// according to the rules of C99 6.7.5p3.  If Loc is non-null, it is set to
   /// the location of the subexpression that makes it a vla type.  It is not

Added: cfe/trunk/test/CodeGen/enum.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/enum.c?rev=41572&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/enum.c (added)
+++ cfe/trunk/test/CodeGen/enum.c Wed Aug 29 12:48:46 2007
@@ -0,0 +1,18 @@
+// RUN: clang %s -emit-llvm | llvm-as | opt -std-compile-opts | llvm-dis | grep 'ret i32 6'
+
+static enum { foo, bar = 1U } z;
+
+int main (void)
+{
+  int r = 0;
+
+  if (bar - 2 < 0)
+    r += 4;
+  if (foo - 1 < 0)
+    r += 2;
+  if (z - 1 < 0)
+    r++;
+
+  return r;
+}
+





More information about the cfe-commits mailing list