107 lines · cpp
1//===- llvm/unittest/IR/AsmWriter.cpp - AsmWriter tests -------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8#include "llvm/BinaryFormat/Dwarf.h"9#include "llvm/IR/DebugInfoMetadata.h"10#include "llvm/IR/Function.h"11#include "llvm/IR/IRBuilder.h"12#include "llvm/IR/LLVMContext.h"13#include "llvm/IR/MDBuilder.h"14#include "llvm/IR/Module.h"15#include "gmock/gmock.h"16#include "gtest/gtest.h"17 18using namespace llvm;19using ::testing::HasSubstr;20 21namespace {22 23TEST(AsmWriterTest, DebugPrintDetachedInstruction) {24 25 // PR24852: Ensure that an instruction can be printed even when it26 // has metadata attached but no parent.27 LLVMContext Ctx;28 auto Ty = Type::getInt32Ty(Ctx);29 auto Poison = PoisonValue::get(Ty);30 std::unique_ptr<BinaryOperator> Add(BinaryOperator::CreateAdd(Poison, Poison));31 Add->setMetadata(32 "", MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(Ty, 1))}));33 std::string S;34 raw_string_ostream OS(S);35 Add->print(OS);36 EXPECT_THAT(S, HasSubstr("<badref> = add i32 poison, poison, !<empty"));37}38 39TEST(AsmWriterTest, DebugPrintDetachedArgument) {40 LLVMContext Ctx;41 auto Ty = Type::getInt32Ty(Ctx);42 auto Arg = new Argument(Ty);43 44 std::string S;45 raw_string_ostream OS(S);46 Arg->print(OS);47 EXPECT_EQ(S, "i32 <badref>");48 delete Arg;49}50 51TEST(AsmWriterTest, DumpDIExpression) {52 LLVMContext Ctx;53 uint64_t Ops[] = {54 dwarf::DW_OP_constu, 4,55 dwarf::DW_OP_minus,56 dwarf::DW_OP_deref,57 };58 DIExpression *Expr = DIExpression::get(Ctx, Ops);59 std::string S;60 raw_string_ostream OS(S);61 Expr->print(OS);62 EXPECT_EQ("!DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref)", S);63}64 65TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) {66 LLVMContext Ctx;67 Module M("test module", Ctx);68 SmallVector<Type *, 3> FArgTypes;69 FArgTypes.push_back(Type::getInt64Ty(Ctx));70 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);71 Function *F = Function::Create(FTy, Function::ExternalLinkage, "", &M);72 Argument *Arg0 = F->getArg(0);73 Value *Args[] = {Arg0};74 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));75 // This will make Call's operand null.76 Call->dropAllReferences();77 78 std::string S;79 raw_string_ostream OS(S);80 Call->print(OS);81 EXPECT_THAT(S, HasSubstr("<cannot get addrspace!>"));82}83 84TEST(AsmWriterTest, PrintNullOperandBundle) {85 LLVMContext C;86 Type *Int32Ty = Type::getInt32Ty(C);87 FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);88 Value *Callee = Constant::getNullValue(PointerType::getUnqual(C));89 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};90 std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));91 std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));92 OperandBundleDef Bundle("bundle", UndefValue::get(Int32Ty));93 std::unique_ptr<InvokeInst> Invoke(94 InvokeInst::Create(FnTy, Callee, NormalDest.get(), UnwindDest.get(), Args,95 Bundle, "result"));96 // Makes the operand bundle null.97 Invoke->dropAllReferences();98 Invoke->setNormalDest(NormalDest.get());99 Invoke->setUnwindDest(UnwindDest.get());100 101 std::string S;102 raw_string_ostream OS(S);103 Invoke->print(OS);104 EXPECT_THAT(S, HasSubstr("<null operand bundle!>"));105}106}107