[cfe-commits] r62135 - in /cfe/trunk: lib/AST/ASTContext.cpp test/CodeGenObjC/encode-test-1.m

Fariborz Jahanian fjahanian at apple.com
Mon Jan 12 17:18:17 PST 2009


Author: fjahanian
Date: Mon Jan 12 19:18:13 2009
New Revision: 62135

URL: http://llvm.org/viewvc/llvm-project?rev=62135&view=rev
Log:
Patch to fix encoding of Enum bitfields in ObjC.


Added:
    cfe/trunk/test/CodeGenObjC/encode-test-1.m
Modified:
    cfe/trunk/lib/AST/ASTContext.cpp

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=62135&r1=62134&r2=62135&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Mon Jan 12 19:18:13 2009
@@ -1775,6 +1775,16 @@
                              true /* outermost type */);
 }
 
+static void EncodeBitField(const ASTContext *Context, std::string& S, 
+                           FieldDecl *FD) {
+  const Expr *E = FD->getBitWidth();
+  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
+  ASTContext *Ctx = const_cast<ASTContext*>(Context);
+  unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
+  S += 'b';
+  S += llvm::utostr(N);
+}
+
 void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
                                             bool ExpandPointedToStructures,
                                             bool ExpandStructures,
@@ -1782,12 +1792,7 @@
                                             bool OutermostType) const {
   if (const BuiltinType *BT = T->getAsBuiltinType()) {
     if (FD && FD->isBitField()) {
-      const Expr *E = FD->getBitWidth();
-      assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
-      ASTContext *Ctx = const_cast<ASTContext*>(this);
-      unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
-      S += 'b';
-      S += llvm::utostr(N);
+      EncodeBitField(this, S, FD);
     }
     else {
       char encoding;
@@ -1949,7 +1954,10 @@
     }
     S += RDecl->isUnion() ? ')' : '}';
   } else if (T->isEnumeralType()) {
-    S += 'i';
+    if (FD && FD->isBitField())
+      EncodeBitField(this, S, FD);
+    else
+      S += 'i';
   } else if (T->isBlockPointerType()) {
     S += '^'; // This type string is the same as general pointers.
   } else if (T->isObjCInterfaceType()) {

Added: cfe/trunk/test/CodeGenObjC/encode-test-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/encode-test-1.m?rev=62135&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenObjC/encode-test-1.m (added)
+++ cfe/trunk/test/CodeGenObjC/encode-test-1.m Mon Jan 12 19:18:13 2009
@@ -0,0 +1,36 @@
+// RUN: clang -triple=i686-apple-darwin9 -fnext-runtime -emit-llvm -o %t %s &&
+// RUN: grep -e "{Base=b2b3b4b5}" %t | count 1 &&
+// RUN: grep -e "{Derived=b2b3b4b5b5b4b3}" %t | count 1
+
+enum Enum { one, two, three, four };
+
+ at interface Base {
+  unsigned a: 2;
+  int b: 3;
+  enum Enum c: 4;
+  unsigned d: 5;
+} 
+ at end
+
+ at interface Derived: Base {
+  signed e: 5;
+  int f: 4;
+  enum Enum g: 3;
+} 
+ at end
+
+ at implementation Base @end
+
+ at implementation Derived @end
+  
+int main(void)
+{
+
+  const char *en = @encode(Base);
+//  printf ("%s\n", en);
+
+  const char *ed = @encode(Derived);
+ // printf ("%s\n", ed);
+
+  return 0;
+}





More information about the cfe-commits mailing list