[cfe-commits] r132171 - in /cfe/trunk: include/clang/AST/ASTContext.h lib/AST/ASTContext.cpp lib/CodeGen/CGObjCMac.cpp lib/Sema/SemaTemplate.cpp test/CodeGenObjC/forward-decl-param.m tools/libclang/CXType.cpp

Douglas Gregor dgregor at apple.com
Thu May 26 18:19:52 PDT 2011


Author: dgregor
Date: Thu May 26 20:19:52 2011
New Revision: 132171

URL: http://llvm.org/viewvc/llvm-project?rev=132171&view=rev
Log:
Objective-C doesn't consider the use of incomplete types as method
parameter types to be ill-formed. However, it relies on the
completeness of method parameter types when producing metadata, e.g.,
for a protocol, leading IR generating to crash in such cases.

Since there's no real way to tighten down the semantics of Objective-C
here without breaking existing code, do something safe but lame:
suppress the generation of metadata when this happens.

Fixes <rdar://problem/9123036>.

Added:
    cfe/trunk/test/CodeGenObjC/forward-decl-param.m
Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/CodeGen/CGObjCMac.cpp
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/tools/libclang/CXType.cpp

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=132171&r1=132170&r2=132171&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Thu May 26 20:19:52 2011
@@ -914,12 +914,18 @@
                                        std::string &S) const;
 
   /// getObjCEncodingForFunctionDecl - Returns the encoded type for this
-  //function.  This is in the same format as Objective-C method encodings.  
-  void getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S);
+  /// function.  This is in the same format as Objective-C method encodings.  
+  ///
+  /// \returns true if an error occurred (e.g., because one of the parameter
+  /// types is incomplete), false otherwise.
+  bool getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S);
 
   /// getObjCEncodingForMethodDecl - Return the encoded type for this method
   /// declaration.
-  void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S)
+  ///
+  /// \returns true if an error occurred (e.g., because one of the parameter
+  /// types is incomplete), false otherwise.
+  bool getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S)
     const;
 
   /// getObjCEncodingForBlock - Return the encoded type for this block

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=132171&r1=132170&r2=132171&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Thu May 26 20:19:52 2011
@@ -3815,6 +3815,9 @@
 /// getObjCEncodingTypeSize returns size of type for objective-c encoding
 /// purpose.
 CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
+  if (!type->isIncompleteArrayType() && type->isIncompleteType())
+    return CharUnits::Zero();
+  
   CharUnits sz = getTypeSizeInChars(type);
 
   // Make all integer and enum types at least as large as an int
@@ -3882,7 +3885,7 @@
   return S;
 }
 
-void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
+bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
                                                 std::string& S) {
   // Encode result type.
   getObjCEncodingForType(Decl->getResultType(), S);
@@ -3892,8 +3895,11 @@
        E = Decl->param_end(); PI != E; ++PI) {
     QualType PType = (*PI)->getType();
     CharUnits sz = getObjCEncodingTypeSize(PType);
+    if (sz.isZero())
+      return true;
+    
     assert (sz.isPositive() && 
-        "getObjCEncodingForMethodDecl - Incomplete param type");
+        "getObjCEncodingForFunctionDecl - Incomplete param type");
     ParmOffset += sz;
   }
   S += charUnitsToString(ParmOffset);
@@ -3916,11 +3922,13 @@
     S += charUnitsToString(ParmOffset);
     ParmOffset += getObjCEncodingTypeSize(PType);
   }
+  
+  return false;
 }
 
 /// getObjCEncodingForMethodDecl - Return the encoded type for this method
 /// declaration.
-void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
+bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
                                               std::string& S) const {
   // FIXME: This is not very efficient.
   // Encode type qualifer, 'in', 'inout', etc. for the return type.
@@ -3939,6 +3947,9 @@
        E = Decl->sel_param_end(); PI != E; ++PI) {
     QualType PType = (*PI)->getType();
     CharUnits sz = getObjCEncodingTypeSize(PType);
+    if (sz.isZero())
+      return true;
+    
     assert (sz.isPositive() && 
         "getObjCEncodingForMethodDecl - Incomplete param type");
     ParmOffset += sz;
@@ -3968,6 +3979,8 @@
     S += charUnitsToString(ParmOffset);
     ParmOffset += getObjCEncodingTypeSize(PType);
   }
+  
+  return false;
 }
 
 /// getObjCEncodingForPropertyDecl - Return the encoded type for this

Modified: cfe/trunk/lib/CodeGen/CGObjCMac.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGObjCMac.cpp?rev=132171&r1=132170&r2=132171&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGObjCMac.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGObjCMac.cpp Thu May 26 20:19:52 2011
@@ -1686,6 +1686,7 @@
 llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
   if (DefinedProtocols.count(PD->getIdentifier()))
     return GetOrEmitProtocol(PD);
+  
   return GetOrEmitProtocolRef(PD);
 }
 
@@ -1719,6 +1720,9 @@
          i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
     ObjCMethodDecl *MD = *i;
     llvm::Constant *C = GetMethodDescriptionConstant(MD);
+    if (!C)
+      return GetOrEmitProtocolRef(PD);
+    
     if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
       OptInstanceMethods.push_back(C);
     } else {
@@ -1730,6 +1734,9 @@
          i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
     ObjCMethodDecl *MD = *i;
     llvm::Constant *C = GetMethodDescriptionConstant(MD);
+    if (!C)
+      return GetOrEmitProtocolRef(PD);
+
     if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
       OptClassMethods.push_back(C);
     } else {
@@ -1973,6 +1980,9 @@
     llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
                                    ObjCTypes.SelectorPtrTy);
   Desc[1] = GetMethodVarType(MD);
+  if (!Desc[1])
+    return 0;
+  
   return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
                                    Desc);
 }
@@ -3935,8 +3945,10 @@
 
 llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
   std::string TypeStr;
-  CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
-                                                TypeStr);
+  if (CGM.getContext().getObjCEncodingForMethodDecl(
+                                                const_cast<ObjCMethodDecl*>(D),
+                                                    TypeStr))
+    return 0;
 
   llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
 
@@ -5359,6 +5371,9 @@
          i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
     ObjCMethodDecl *MD = *i;
     llvm::Constant *C = GetMethodDescriptionConstant(MD);
+    if (!C)
+      return GetOrEmitProtocolRef(PD);
+    
     if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
       OptInstanceMethods.push_back(C);
     } else {
@@ -5370,6 +5385,9 @@
          i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
     ObjCMethodDecl *MD = *i;
     llvm::Constant *C = GetMethodDescriptionConstant(MD);
+    if (!C)
+      return GetOrEmitProtocolRef(PD);
+
     if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
       OptClassMethods.push_back(C);
     } else {
@@ -5509,6 +5527,9 @@
     llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
                                    ObjCTypes.SelectorPtrTy);
   Desc[1] = GetMethodVarType(MD);
+  if (!Desc[1])
+    return 0;
+  
   // Protocol methods have no implementation. So, this entry is always NULL.
   Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
   return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);

Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=132171&r1=132170&r2=132171&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Thu May 26 20:19:52 2011
@@ -2867,6 +2867,16 @@
     if ((*Param)->isTemplateParameterPack())
       break;
 
+    // If our template is a template template parameter that hasn't acquired
+    // its proper context yet (e.g., because we're using the template template
+    // parameter in the signature of a function template, before we've built
+    // the function template itself), don't attempt substitution of default
+    // template arguments at this point: we don't have enough context to
+    // do it properly.
+    if (isTemplateTemplateParameter && 
+        Template->getDeclContext()->isTranslationUnit())
+      break;
+    
     // We have a default template argument that we will use.
     TemplateArgumentLoc Arg;
 

Added: cfe/trunk/test/CodeGenObjC/forward-decl-param.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/forward-decl-param.m?rev=132171&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenObjC/forward-decl-param.m (added)
+++ cfe/trunk/test/CodeGenObjC/forward-decl-param.m Thu May 26 20:19:52 2011
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - 
+
+// <rdar://problem/9123036> crash due to forward-declared struct in
+// protocol method parameter.
+
+ at protocol P
+- (void) A:(struct z) z;
+ at end
+ at interface I < P >
+ at end
+ at implementation I
+ at end
+
+ at interface I2
+- (void) A:(struct z2) z2;
+ at end
+ at implementation I2
+ at end
+

Modified: cfe/trunk/tools/libclang/CXType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXType.cpp?rev=132171&r1=132170&r2=132171&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXType.cpp (original)
+++ cfe/trunk/tools/libclang/CXType.cpp Thu May 26 20:19:52 2011
@@ -379,9 +379,10 @@
   ASTContext &Ctx = AU->getASTContext();
   std::string encoding;
 
-  if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) 
-    Ctx.getObjCEncodingForMethodDecl(OMD, encoding);
-  else if (ObjCPropertyDecl *OPD = dyn_cast<ObjCPropertyDecl>(D)) 
+  if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))  {
+    if (Ctx.getObjCEncodingForMethodDecl(OMD, encoding))
+      return cxstring::createCXString("?");
+  } else if (ObjCPropertyDecl *OPD = dyn_cast<ObjCPropertyDecl>(D)) 
     Ctx.getObjCEncodingForPropertyDecl(OPD, NULL, encoding);
   else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
     Ctx.getObjCEncodingForFunctionDecl(FD, encoding);





More information about the cfe-commits mailing list