blob: 55c2a70e21f5876f6446b015a4192a7adee6b549 [file] [log] [blame]
Mehdi Aminib9b50aa2016-01-07 20:14:30 +00001//===- 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//===----------------------------------------------------------------------===//
Mehdi Aminib9b50aa2016-01-07 20:14:30 +00009#include "llvm/IR/Function.h"
Chandler Carruth9a67b072017-06-06 11:06:56 +000010#include "llvm/IR/IRBuilder.h"
Mehdi Aminib9b50aa2016-01-07 20:14:30 +000011#include "llvm/IR/LLVMContext.h"
12#include "llvm/IR/MDBuilder.h"
13#include "llvm/IR/Module.h"
14#include "gtest/gtest.h"
15
16using namespace llvm;
17
18namespace {
19
20TEST(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}