Fix invalid cast.
authorRafael Espindola <rafael.espindola@gmail.com>
Mon, 23 Feb 2015 21:51:06 +0000 (21:51 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Mon, 23 Feb 2015 21:51:06 +0000 (21:51 +0000)
Fixes PR22525.

Patch by Ben Longbons with testcase by me.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230271 91177308-0d34-0410-b5e6-96231b3b80d8

lib/IR/Core.cpp
unittests/IR/ConstantsTest.cpp

index 6303e4548bfb5d43580f58e1a2b7eef0868ac6f2..f007616a1c35ad2062154d51a303cdd5a902e5ca 100644 (file)
@@ -1630,7 +1630,7 @@ LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
   auto *PTy = cast<PointerType>(unwrap(Ty));
   return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
                                   GlobalValue::ExternalLinkage, Name,
-                                  unwrap<GlobalObject>(Aliasee), unwrap(M)));
+                                  unwrap<Constant>(Aliasee), unwrap(M)));
 }
 
 /*--.. Operations on functions .............................................--*/
index 5414b25ca3adfe8b9c290e6702e76f655ac6cd9b..5d271e220ef9bfc6b260a515924321f2d56b9c81 100644 (file)
@@ -7,12 +7,14 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/AsmParser/Parser.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
+#include "llvm/Support/SourceMgr.h"
 #include "gtest/gtest.h"
 
 namespace llvm {
@@ -347,5 +349,19 @@ TEST(ConstantsTest, GEPReplaceWithConstant) {
   ASSERT_EQ(GEP->getOperand(0), Alias);
 }
 
+TEST(ConstantsTest, AliasCAPI) {
+  LLVMContext Context;
+  SMDiagnostic Error;
+  std::unique_ptr<Module> M =
+      parseAssemblyString("@g = global i32 42", Error, Context);
+  GlobalVariable *G = M->getGlobalVariable("g");
+  Type *I16Ty = Type::getInt16Ty(Context);
+  Type *I16PTy = PointerType::get(I16Ty, 0);
+  Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy);
+  LLVMValueRef AliasRef =
+      LLVMAddAlias(wrap(M.get()), wrap(I16PTy), wrap(Aliasee), "a");
+  ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
+}
+
 }  // end anonymous namespace
 }  // end namespace llvm