1 //===- llvm/unittest/IR/AsmWriter.cpp - AsmWriter tests -------------------===//
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
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 "gtest/gtest.h"
21 TEST(AsmWriterTest
, DebugPrintDetachedInstruction
) {
23 // PR24852: Ensure that an instruction can be printed even when it
24 // has metadata attached but no parent.
26 auto Ty
= Type::getInt32Ty(Ctx
);
27 auto Undef
= UndefValue::get(Ty
);
28 std::unique_ptr
<BinaryOperator
> Add(BinaryOperator::CreateAdd(Undef
, Undef
));
30 "", MDNode::get(Ctx
, {ConstantAsMetadata::get(ConstantInt::get(Ty
, 1))}));
32 raw_string_ostream
OS(S
);
34 std::size_t r
= OS
.str().find("<badref> = add i32 undef, undef, !<empty");
35 EXPECT_TRUE(r
!= std::string::npos
);
38 TEST(AsmWriterTest
, DebugPrintDetachedArgument
) {
40 auto Ty
= Type::getInt32Ty(Ctx
);
41 auto Arg
= new Argument(Ty
);
44 raw_string_ostream
OS(S
);
46 EXPECT_EQ(S
, "i32 <badref>");
50 TEST(AsmWriterTest
, DumpDIExpression
) {
53 dwarf::DW_OP_constu
, 4,
57 DIExpression
*Expr
= DIExpression::get(Ctx
, Ops
);
59 raw_string_ostream
OS(S
);
61 EXPECT_EQ("!DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref)",