[cfe-commits] r46260 - in /cfe/trunk: AST/ASTContext.cpp CodeGen/CGExprScalar.cpp Driver/RewriteTest.cpp include/clang/AST/ASTContext.h test/Sema/objc-encoding-bug-1.m

Fariborz Jahanian fjahanian at apple.com
Tue Jan 22 14:44:46 PST 2008


Author: fjahanian
Date: Tue Jan 22 16:44:46 2008
New Revision: 46260

URL: http://llvm.org/viewvc/llvm-project?rev=46260&view=rev
Log:
Problem with ObjC's type-encoding of nested structs causing infinit recursion.

Added:
    cfe/trunk/test/Sema/objc-encoding-bug-1.m
Modified:
    cfe/trunk/AST/ASTContext.cpp
    cfe/trunk/CodeGen/CGExprScalar.cpp
    cfe/trunk/Driver/RewriteTest.cpp
    cfe/trunk/include/clang/AST/ASTContext.h

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

==============================================================================
--- cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/trunk/AST/ASTContext.cpp Tue Jan 22 16:44:46 2008
@@ -984,7 +984,7 @@
   // Encode type qualifer, 'in', 'inout', etc. for the return type.
   getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
   // Encode result type.
-  getObjCEncodingForType(Decl->getResultType(), S);
+  getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
   // Compute size of all parameters.
   // Start with computing size of a pointer in number of bytes.
   // FIXME: There might(should) be a better way of doing this computation!
@@ -1012,13 +1012,14 @@
     // 'in', 'inout', etc.
     getObjCEncodingForTypeQualifier(
       Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
-    getObjCEncodingForType(PType, S);
+    getObjCEncodingForType(PType, S, EncodingRecordTypes);
     S += llvm::utostr(ParmOffset);
     ParmOffset += getObjCEncodingTypeSize(PType);
   }
 }
 
-void ASTContext::getObjCEncodingForType(QualType T, std::string& S) const
+void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
+       llvm::SmallVector<const RecordType *, 8> &ERType) const
 {
   // FIXME: This currently doesn't encode:
   // @ An object (whether statically typed or typed id)
@@ -1086,7 +1087,7 @@
   }
   else if (T->isObjCQualifiedIdType()) {
     // Treat id<P...> same as 'id' for encoding purposes.
-    return getObjCEncodingForType(getObjCIdType(), S);
+    return getObjCEncodingForType(getObjCIdType(), S, ERType);
     
   }
   else if (const PointerType *PT = T->getAsPointerType()) {
@@ -1112,7 +1113,7 @@
     }
     
     S += '^';
-    getObjCEncodingForType(PT->getPointeeType(), S);
+    getObjCEncodingForType(PT->getPointeeType(), S, ERType);
   } else if (const ArrayType *AT = T->getAsArrayType()) {
     S += '[';
     
@@ -1121,7 +1122,7 @@
     else
       assert(0 && "Unhandled array type!");
     
-    getObjCEncodingForType(AT->getElementType(), S);
+    getObjCEncodingForType(AT->getElementType(), S, ERType);
     S += ']';
   } else if (T->getAsFunctionType()) {
     S += '?';
@@ -1129,10 +1130,21 @@
     RecordDecl *RDecl= RTy->getDecl();
     S += '{';
     S += RDecl->getName();
-    S += '=';
-    for (int i = 0; i < RDecl->getNumMembers(); i++) {
-      FieldDecl *field = RDecl->getMember(i);
-      getObjCEncodingForType(field->getType(), S);
+    bool found = false;
+    for (unsigned i = 0, e = ERType.size(); i != e; ++i)
+      if (ERType[i] == RTy) {
+        found = true;
+        break;
+      }
+    if (!found) {
+      ERType.push_back(RTy);
+      S += '=';
+      for (int i = 0; i < RDecl->getNumMembers(); i++) {
+        FieldDecl *field = RDecl->getMember(i);
+        getObjCEncodingForType(field->getType(), S, ERType);
+      }
+      assert(ERType.back() == RTy && "Record Type stack mismatch.");
+      ERType.pop_back();
     }
     S += '}';
   } else if (T->isEnumeralType()) {

Modified: cfe/trunk/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExprScalar.cpp?rev=46260&r1=46259&r2=46260&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/CodeGen/CGExprScalar.cpp Tue Jan 22 16:44:46 2008
@@ -1005,8 +1005,9 @@
 
 Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
   std::string str;
-  
-  CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str);
+  llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; 
+  CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str,
+                                          EncodingRecordTypes);
   
   llvm::Constant *C = llvm::ConstantArray::get(str);
   C = new llvm::GlobalVariable(C->getType(), true, 

Modified: cfe/trunk/Driver/RewriteTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteTest.cpp?rev=46260&r1=46259&r2=46260&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteTest.cpp (original)
+++ cfe/trunk/Driver/RewriteTest.cpp Tue Jan 22 16:44:46 2008
@@ -42,6 +42,7 @@
     llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames;
     llvm::SmallVector<Stmt *, 32> Stmts;
     llvm::SmallVector<int, 8> ObjCBcLabelNo;
+    llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
     
     FunctionDecl *MsgSendFunctionDecl;
     FunctionDecl *MsgSendSuperFunctionDecl;
@@ -1226,7 +1227,8 @@
   // Create a new string expression.
   QualType StrType = Context->getPointerType(Context->CharTy);
   std::string StrEncoding;
-  Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
+  Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding, 
+                                  EncodingRecordTypes);
   Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
                                         StrEncoding.length(), false, StrType, 
                                         SourceLocation(), SourceLocation());
@@ -2516,7 +2518,8 @@
     Result += (*IVI)->getName();
     Result += "\", \"";
     std::string StrEncoding;
-    Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding);
+    Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding,
+                                    EncodingRecordTypes);
     Result += StrEncoding;
     Result += "\", ";
     SynthesizeIvarOffsetComputation(IDecl, *IVI, Result);
@@ -2526,7 +2529,8 @@
       Result += (*IVI)->getName();
       Result += "\", \"";
       std::string StrEncoding;
-      Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding);
+      Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding,
+                                      EncodingRecordTypes);
       Result += StrEncoding;
       Result += "\", ";
       SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result);

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

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Tue Jan 22 16:44:46 2008
@@ -46,6 +46,8 @@
   /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts.
   ///  This is lazily created.  This is intentionally not serialized.
   llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts;
+  
+  llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
     
   /// BuiltinVaListType - built-in va list type.
   /// This is initially null and set by Sema::LazilyCreateBuiltin when
@@ -204,7 +206,8 @@
   }
 
   // Return the ObjC type encoding for a given type.
-  void getObjCEncodingForType(QualType t, std::string &S) const;
+  void getObjCEncodingForType(QualType t, std::string &S, 
+			      llvm::SmallVector<const RecordType *, 8> &RT) const;
   
   // Put the string version of type qualifiers into S.
   void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 

Added: cfe/trunk/test/Sema/objc-encoding-bug-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/objc-encoding-bug-1.m?rev=46260&view=auto

==============================================================================
--- cfe/trunk/test/Sema/objc-encoding-bug-1.m (added)
+++ cfe/trunk/test/Sema/objc-encoding-bug-1.m Tue Jan 22 16:44:46 2008
@@ -0,0 +1,19 @@
+// RUN: clang -rewrite-test %s | clang
+
+typedef struct NSMethodFrameArgInfo {
+    struct NSMethodFrameArgInfo *subInfo;
+    struct NSMethodFrameArgInfo *an;
+} NSMethodFrameArgInfo;
+
+ at interface NSMethodSignature 
+- (NSMethodFrameArgInfo *)_argInfo;
+ at end
+
+ at implementation NSMethodSignature
+
+- (NSMethodFrameArgInfo *)_argInfo{
+    return 0;
+}
+
+ at end
+





More information about the cfe-commits mailing list