[cfe-commits] r79071 - in /cfe/trunk: lib/AST/ASTContext.cpp lib/CodeGen/CodeGenFunction.cpp lib/CodeGen/CodeGenFunction.h test/CodeGen/2009-08-14-vararray-crash.c

Eli Friedman eli.friedman at gmail.com
Fri Aug 14 19:50:32 PDT 2009


Author: efriedma
Date: Fri Aug 14 21:50:32 2009
New Revision: 79071

URL: http://llvm.org/viewvc/llvm-project?rev=79071&view=rev
Log:
Fix for PR4721: adjust CodeGen and ASTContext so that we have a 
consistent model for handling size expressions for VLAs.

The model is essentially as follows: VLA types own their associated
expression.  In some cases, we need to create multiple VLA types to 
represent a given VLA (for canonical types, or qualifiers on array types,
or type merging).  If we need to create multiple types based off of 
the same VLA declaration, we use the new refcounting functionality so they can 
all own the expression. The VLASizeMap in CodeGenFunction then uses the size
expression to identify the group of VLA types based off of the same original
declaration.

I'm not particularly attached to the VLA types owning the expression, 
but we're stuck with at least until someone comes up with a way 
to walk the VLA expressions for a declaration.

I did the parallel fix in ASTContext for DependentSizedArrayType, but I 
haven't really looked closely at it, so there might still be issues 
there.

I'll clean up the code duplication in ASTContext in a followup commit.


Added:
    cfe/trunk/test/CodeGen/2009-08-14-vararray-crash.c
Modified:
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

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

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Fri Aug 14 21:50:32 2009
@@ -2105,14 +2105,16 @@
   if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
     return CanQualType::CreateUnsafe(
              getDependentSizedArrayType(NewEltTy,
-                                        DSAT->getSizeExpr(),
+                                        DSAT->getSizeExpr() ?
+                                          DSAT->getSizeExpr()->Retain() : 0,
                                         DSAT->getSizeModifier(),
                                         DSAT->getIndexTypeQualifier(),
                                         DSAT->getBracketsRange()));
 
   VariableArrayType *VAT = cast<VariableArrayType>(AT);
   return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
-                                                        VAT->getSizeExpr(),
+                                                        VAT->getSizeExpr() ?
+                                              VAT->getSizeExpr()->Retain() : 0,
                                                         VAT->getSizeModifier(),
                                                   VAT->getIndexTypeQualifier(),
                                                      VAT->getBracketsRange()));
@@ -2304,14 +2306,16 @@
         = dyn_cast<DependentSizedArrayType>(ATy))
     return cast<ArrayType>(
                      getDependentSizedArrayType(NewEltTy, 
-                                                DSAT->getSizeExpr(),
+                                                DSAT->getSizeExpr() ?
+                                              DSAT->getSizeExpr()->Retain() : 0,
                                                 DSAT->getSizeModifier(),
                                                 DSAT->getIndexTypeQualifier(),
                                                 DSAT->getBracketsRange()));
   
   const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
   return cast<ArrayType>(getVariableArrayType(NewEltTy,
-                                              VAT->getSizeExpr(),
+                                              VAT->getSizeExpr() ?
+                                               VAT->getSizeExpr()->Retain() : 0,
                                               VAT->getSizeModifier(),
                                               VAT->getIndexTypeQualifier(),
                                               VAT->getBracketsRange()));

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=79071&r1=79070&r2=79071&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Fri Aug 14 21:50:32 2009
@@ -477,7 +477,7 @@
 }
 
 llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
-  llvm::Value *&SizeEntry = VLASizeMap[VAT];
+  llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
   
   assert(SizeEntry && "Did not emit size for type");
   return SizeEntry;
@@ -490,7 +490,7 @@
   EnsureInsertPoint();
   
   if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
-    llvm::Value *&SizeEntry = VLASizeMap[VAT];
+    llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
     
     if (!SizeEntry) {
       const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=79071&r1=79070&r2=79071&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Fri Aug 14 21:50:32 2009
@@ -224,9 +224,12 @@
   llvm::BasicBlock *InvokeDest;
 
   // VLASizeMap - This keeps track of the associated size for each VLA type.
+  // We track this by the size expression rather than the type itself because
+  // in certain situations, like a const qualifier applied to an VLA typedef,
+  // multiple VLA types can share the same size expression.
   // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
   // enter/leave scopes.
-  llvm::DenseMap<const VariableArrayType*, llvm::Value*> VLASizeMap;
+  llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
 
   /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
   /// calling llvm.stacksave for multiple VLAs in the same scope.

Added: cfe/trunk/test/CodeGen/2009-08-14-vararray-crash.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/2009-08-14-vararray-crash.c?rev=79071&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/2009-08-14-vararray-crash.c (added)
+++ cfe/trunk/test/CodeGen/2009-08-14-vararray-crash.c Fri Aug 14 21:50:32 2009
@@ -0,0 +1,11 @@
+// RUN: clang-cc -emit-llvm < %s
+
+void sum1(int rb) {
+  typedef unsigned char imgrow[rb];
+  typedef imgrow img[rb];
+
+  const img *br;
+  int y;
+
+  (*br)[y];
+}





More information about the cfe-commits mailing list