From 433fb267073e40aa52b2ed01e028d62889a83320 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 10 Dec 2007 19:50:32 +0000 Subject: [PATCH] add support for implicit cast from array to pointer that is not the element type. llvm-svn: 44809 --- clang/CodeGen/CodeGenModule.cpp | 9 ++++++++- clang/test/CodeGen/globalinit.c | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/clang/CodeGen/CodeGenModule.cpp b/clang/CodeGen/CodeGenModule.cpp index 2ce7f4e83718..a5f7df18d41a 100644 --- a/clang/CodeGen/CodeGenModule.cpp +++ b/clang/CodeGen/CodeGenModule.cpp @@ -404,7 +404,14 @@ static llvm::Constant *GenerateConstantExpr(const Expr *Expression, llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); llvm::Constant *Ops[] = {Idx0, Idx0}; - return llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); + C = llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); + + // The resultant pointer type can be implicitly casted to other pointer + // types as well, for example void*. + const llvm::Type *DestPTy = Types.ConvertType(type); + assert(isa(DestPTy) && + "Only expect implicit cast to pointer"); + return llvm::ConstantExpr::getBitCast(C, DestPTy); } // If this is an implicit cast of a string literal to an array type, this diff --git a/clang/test/CodeGen/globalinit.c b/clang/test/CodeGen/globalinit.c index 55298bc07eef..f47fd5c45775 100644 --- a/clang/test/CodeGen/globalinit.c +++ b/clang/test/CodeGen/globalinit.c @@ -10,7 +10,7 @@ void bar() { x[0] = 1; } extern int y[]; -//void *g = y; +void *g = y; int latin_ptr2len (char *p); int (*mb_ptr2len) (char *p) = latin_ptr2len;