llvm-project
108 строк · 3.5 Кб
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-exception
6//
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 it
26// has metadata attached but no parent.
27LLVMContext Ctx;
28auto Ty = Type::getInt32Ty(Ctx);
29auto Poison = PoisonValue::get(Ty);
30std::unique_ptr<BinaryOperator> Add(BinaryOperator::CreateAdd(Poison, Poison));
31Add->setMetadata(
32"", MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(Ty, 1))}));
33std::string S;
34raw_string_ostream OS(S);
35Add->print(OS);
36EXPECT_THAT(OS.str(),
37HasSubstr("<badref> = add i32 poison, poison, !<empty"));
38}
39
40TEST(AsmWriterTest, DebugPrintDetachedArgument) {
41LLVMContext Ctx;
42auto Ty = Type::getInt32Ty(Ctx);
43auto Arg = new Argument(Ty);
44
45std::string S;
46raw_string_ostream OS(S);
47Arg->print(OS);
48EXPECT_EQ(S, "i32 <badref>");
49delete Arg;
50}
51
52TEST(AsmWriterTest, DumpDIExpression) {
53LLVMContext Ctx;
54uint64_t Ops[] = {
55dwarf::DW_OP_constu, 4,
56dwarf::DW_OP_minus,
57dwarf::DW_OP_deref,
58};
59DIExpression *Expr = DIExpression::get(Ctx, Ops);
60std::string S;
61raw_string_ostream OS(S);
62Expr->print(OS);
63EXPECT_EQ("!DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref)",
64OS.str());
65}
66
67TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) {
68LLVMContext Ctx;
69Module M("test module", Ctx);
70SmallVector<Type *, 3> FArgTypes;
71FArgTypes.push_back(Type::getInt64Ty(Ctx));
72FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
73Function *F = Function::Create(FTy, Function::ExternalLinkage, "", &M);
74Argument *Arg0 = F->getArg(0);
75Value *Args[] = {Arg0};
76std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
77// This will make Call's operand null.
78Call->dropAllReferences();
79
80std::string S;
81raw_string_ostream OS(S);
82Call->print(OS);
83EXPECT_THAT(OS.str(), HasSubstr("<cannot get addrspace!>"));
84}
85
86TEST(AsmWriterTest, PrintNullOperandBundle) {
87LLVMContext C;
88Type *Int32Ty = Type::getInt32Ty(C);
89FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
90Value *Callee = Constant::getNullValue(PointerType::getUnqual(C));
91Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
92std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));
93std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));
94OperandBundleDef Bundle("bundle", UndefValue::get(Int32Ty));
95std::unique_ptr<InvokeInst> Invoke(
96InvokeInst::Create(FnTy, Callee, NormalDest.get(), UnwindDest.get(), Args,
97Bundle, "result"));
98// Makes the operand bundle null.
99Invoke->dropAllReferences();
100Invoke->setNormalDest(NormalDest.get());
101Invoke->setUnwindDest(UnwindDest.get());
102
103std::string S;
104raw_string_ostream OS(S);
105Invoke->print(OS);
106EXPECT_THAT(OS.str(), HasSubstr("<null operand bundle!>"));
107}
108}
109