• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/unittest/IR/AsmWriter.cpp - AsmWriter tests -------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/IRBuilder.h"
10 #include "llvm/IR/Function.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/MDBuilder.h"
13 #include "llvm/IR/Module.h"
14 #include "gtest/gtest.h"
15 
16 using namespace llvm;
17 
18 namespace {
19 
TEST(AsmWriterTest,DebugPrintDetachedInstruction)20 TEST(AsmWriterTest, DebugPrintDetachedInstruction) {
21 
22   // PR24852: Ensure that an instruction can be printed even when it
23   // has metadata attached but no parent.
24   LLVMContext Ctx;
25   auto Ty = Type::getInt32Ty(Ctx);
26   auto Undef = UndefValue::get(Ty);
27   std::unique_ptr<BinaryOperator> Add(BinaryOperator::CreateAdd(Undef, Undef));
28   Add->setMetadata(
29       "", MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(Ty, 1))}));
30   std::string S;
31   raw_string_ostream OS(S);
32   Add->print(OS);
33   std::size_t r = OS.str().find("<badref> = add i32 undef, undef, !<empty");
34   EXPECT_TRUE(r != std::string::npos);
35 }
36 
37 }
38