Mehdi Amini | b9b50aa | 2016-01-07 20:14:30 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/AsmWriter.cpp - AsmWriter tests -------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Mehdi Amini | b9b50aa | 2016-01-07 20:14:30 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Reid Kleckner | b95b427 | 2017-08-30 20:40:36 +0000 | [diff] [blame] | 8 | #include "llvm/BinaryFormat/Dwarf.h" |
| 9 | #include "llvm/IR/DebugInfoMetadata.h" |
Mehdi Amini | b9b50aa | 2016-01-07 20:14:30 +0000 | [diff] [blame] | 10 | #include "llvm/IR/Function.h" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 11 | #include "llvm/IR/IRBuilder.h" |
Mehdi Amini | b9b50aa | 2016-01-07 20:14:30 +0000 | [diff] [blame] | 12 | #include "llvm/IR/LLVMContext.h" |
| 13 | #include "llvm/IR/MDBuilder.h" |
| 14 | #include "llvm/IR/Module.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | TEST(AsmWriterTest, DebugPrintDetachedInstruction) { |
| 22 | |
| 23 | // PR24852: Ensure that an instruction can be printed even when it |
| 24 | // has metadata attached but no parent. |
| 25 | LLVMContext Ctx; |
| 26 | auto Ty = Type::getInt32Ty(Ctx); |
| 27 | auto Undef = UndefValue::get(Ty); |
| 28 | std::unique_ptr<BinaryOperator> Add(BinaryOperator::CreateAdd(Undef, Undef)); |
| 29 | Add->setMetadata( |
| 30 | "", MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(Ty, 1))})); |
| 31 | std::string S; |
| 32 | raw_string_ostream OS(S); |
| 33 | Add->print(OS); |
| 34 | std::size_t r = OS.str().find("<badref> = add i32 undef, undef, !<empty"); |
| 35 | EXPECT_TRUE(r != std::string::npos); |
| 36 | } |
| 37 | |
Tim Northover | a009a60 | 2019-08-03 14:28:34 +0000 | [diff] [blame^] | 38 | TEST(AsmWriterTest, DebugPrintDetachedArgument) { |
| 39 | LLVMContext Ctx; |
| 40 | auto Ty = Type::getInt32Ty(Ctx); |
| 41 | auto Arg = new Argument(Ty); |
| 42 | |
| 43 | std::string S; |
| 44 | raw_string_ostream OS(S); |
| 45 | Arg->print(OS); |
| 46 | EXPECT_EQ(S, "i32 <badref>"); |
| 47 | } |
| 48 | |
Reid Kleckner | b95b427 | 2017-08-30 20:40:36 +0000 | [diff] [blame] | 49 | TEST(AsmWriterTest, DumpDIExpression) { |
| 50 | LLVMContext Ctx; |
| 51 | uint64_t Ops[] = { |
| 52 | dwarf::DW_OP_constu, 4, |
| 53 | dwarf::DW_OP_minus, |
| 54 | dwarf::DW_OP_deref, |
| 55 | }; |
| 56 | DIExpression *Expr = DIExpression::get(Ctx, Ops); |
| 57 | std::string S; |
| 58 | raw_string_ostream OS(S); |
| 59 | Expr->print(OS); |
| 60 | EXPECT_EQ("!DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref)", |
| 61 | OS.str()); |
| 62 | } |
| 63 | |
Mehdi Amini | b9b50aa | 2016-01-07 20:14:30 +0000 | [diff] [blame] | 64 | } |