]> granicus.if.org Git - clang/commitdiff
Fix a crash reported by Seo Sanghyeon.
authorChris Lattner <sabre@nondot.org>
Thu, 3 Jan 2008 06:36:51 +0000 (06:36 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 3 Jan 2008 06:36:51 +0000 (06:36 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45530 91177308-0d34-0410-b5e6-96231b3b80d8

CodeGen/CGExprScalar.cpp
CodeGen/CodeGenModule.h
clang.xcodeproj/project.pbxproj
test/CodeGen/exprs.c

index 0fc1d0f8e79ab3feb86231215df04e63058d7fd2..639b907e027183a78d17a174baa9fbd57be255d6 100644 (file)
@@ -713,10 +713,30 @@ Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
     return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
   
   // FIXME: What about a pointer to a VLA?
-  if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int
-    return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr");
-  // int + pointer
-  return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr");
+  Value *Ptr, *Idx;
+  Expr *IdxExp;
+  if (isa<llvm::PointerType>(Ops.LHS->getType())) {  // pointer + int
+    Ptr = Ops.LHS;
+    Idx = Ops.RHS;
+    IdxExp = Ops.E->getRHS();
+  } else {                                           // int + pointer
+    Ptr = Ops.RHS;
+    Idx = Ops.LHS;
+    IdxExp = Ops.E->getLHS();
+  }
+
+  unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
+  if (Width < CGF.LLVMPointerWidth) {
+    // Zero or sign extend the pointer value based on whether the index is
+    // signed or not.
+    const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
+    if (IdxExp->getType().getCanonicalType()->isSignedIntegerType())
+      Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
+    else
+      Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
+  }
+  
+  return Builder.CreateGEP(Ptr, Idx, "add.ptr");
 }
 
 Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
index acfb3349e6ddc2030bdf85960c42ffc46c274511..6653e508813e5ac5297e5b3e672439ce058133de 100644 (file)
@@ -67,6 +67,7 @@ public:
   llvm::Module &getModule() const { return TheModule; }
   CodeGenTypes &getTypes() { return Types; }
   Diagnostic &getDiags() const { return Diags; }
+  const llvm::TargetData &getTargetData() const { return TheTargetData; }
   
   llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D,
                                         bool isDefinition);
index 85781e5bde15a118f6e8d8b57500b0ed6469ef24..6a4013badebc95147d09f82eb526a8db5541cada 100644 (file)
                08FB7793FE84155DC02AAC07 /* Project object */ = {
                        isa = PBXProject;
                        buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
+                       compatibilityVersion = "Xcode 2.4";
                        hasScannedForEncodings = 1;
                        mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
                        projectDirPath = "";
index c57817d7f4eae5a858c92a99919e282cb81531a4..502cb83c131c10580c103793e4f7d9817c1ebb19 100644 (file)
@@ -6,3 +6,11 @@ int zxcv(void);
 int x=sizeof(zxcv);
 int y=__alignof__(zxcv);
 
+
+void *test(int *i) {
+ short a = 1;
+ i += a;
+ i + a;
+ a + i;
+}
+