From 993502eafd8f108d266c21726b2f628fe50628e6 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 23 Feb 2015 21:51:06 +0000 Subject: [PATCH] Fix invalid cast. Fixes PR22525. Patch by Ben Longbons with testcase by me. llvm-svn: 230271 --- llvm/lib/IR/Core.cpp | 2 +- llvm/unittests/IR/ConstantsTest.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 6303e45..f007616 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -1630,7 +1630,7 @@ LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, auto *PTy = cast(unwrap(Ty)); return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), GlobalValue::ExternalLinkage, Name, - unwrap(Aliasee), unwrap(M))); + unwrap(Aliasee), unwrap(M))); } /*--.. Operations on functions .............................................--*/ diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp index 5414b25..5d271e2 100644 --- a/llvm/unittests/IR/ConstantsTest.cpp +++ b/llvm/unittests/IR/ConstantsTest.cpp @@ -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 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(AliasRef)->getAliasee(), Aliasee); +} + } // end anonymous namespace } // end namespace llvm -- 2.7.4