blob: 01bcb308b7d59298ab626d47e770738f7723e190 [file] [log] [blame]
Duncan P. N. Exon Smith71db6422015-02-02 18:20:15 +00001//===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
Nick Lewycky49f89192009-04-04 07:22:01 +00002//
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
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +000012#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000013#include "llvm/IR/DebugInfoMetadata.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000014#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
16#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
19#include "llvm/IR/Type.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000020#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000021#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000022using namespace llvm;
23
24namespace {
25
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000026TEST(ContextAndReplaceableUsesTest, FromContext) {
27 LLVMContext Context;
28 ContextAndReplaceableUses CRU(Context);
29 EXPECT_EQ(&Context, &CRU.getContext());
30 EXPECT_FALSE(CRU.hasReplaceableUses());
31 EXPECT_FALSE(CRU.getReplaceableUses());
32}
33
34TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
35 LLVMContext Context;
36 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
37 EXPECT_EQ(&Context, &CRU.getContext());
38 EXPECT_TRUE(CRU.hasReplaceableUses());
39 EXPECT_TRUE(CRU.getReplaceableUses());
40}
41
42TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
43 LLVMContext Context;
44 ContextAndReplaceableUses CRU(Context);
45 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
46 EXPECT_EQ(&Context, &CRU.getContext());
47 EXPECT_TRUE(CRU.hasReplaceableUses());
48 EXPECT_TRUE(CRU.getReplaceableUses());
49}
50
51TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
52 LLVMContext Context;
53 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
54 auto *Ptr = ReplaceableUses.get();
55 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
56 ReplaceableUses = CRU.takeReplaceableUses();
57 EXPECT_EQ(&Context, &CRU.getContext());
58 EXPECT_FALSE(CRU.hasReplaceableUses());
59 EXPECT_FALSE(CRU.getReplaceableUses());
60 EXPECT_EQ(Ptr, ReplaceableUses.get());
61}
62
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000063class MetadataTest : public testing::Test {
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000064public:
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000065 MetadataTest() : M("test", Context), Counter(0) {}
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000066
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000067protected:
68 LLVMContext Context;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000069 Module M;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000070 int Counter;
71
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000072 MDNode *getNode() { return MDNode::get(Context, None); }
73 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
74 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
75 Metadata *MDs[] = {MD1, MD2};
76 return MDNode::get(Context, MDs);
77 }
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000078
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +000079 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000080 MDSubroutineType *getSubroutineType() {
81 return MDSubroutineType::getDistinct(Context, 0, getNode(nullptr));
82 }
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000083 MDSubprogram *getSubprogram() {
84 return MDSubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
85 nullptr, false, false, 0, nullptr, 0, 0, 0,
86 0);
87 }
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +000088 MDScopeRef getSubprogramRef() { return getSubprogram()->getRef(); }
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000089 MDFile *getFile() {
90 return MDFile::getDistinct(Context, "file.c", "/path/to/dir");
91 }
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +000092 MDTypeRef getBasicType(StringRef Name) {
93 return MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name)
94 ->getRef();
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000095 }
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +000096 MDTypeRef getDerivedType() {
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000097 return MDDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
98 nullptr, 0, nullptr,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +000099 getBasicType("basictype"), 1, 2, 0, 0)
100 ->getRef();
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000101 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000102 Constant *getConstant() {
103 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
104 }
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000105 ConstantAsMetadata *getConstantAsMetadata() {
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000106 return ConstantAsMetadata::get(getConstant());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000107 }
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000108 MDTypeRef getCompositeType() {
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000109 return MDCompositeType::getDistinct(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000110 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr,
111 nullptr, 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "")
112 ->getRef();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000113 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000114 Function *getFunction(StringRef Name) {
115 return cast<Function>(M.getOrInsertFunction(
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000116 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
117 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000118};
119typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +0000120
Nick Lewycky49f89192009-04-04 07:22:01 +0000121// Test that construction of MDString with different value produces different
122// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000123TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000124 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +0000125 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000126 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +0000127 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000128 EXPECT_NE(s1, s2);
129}
130
131// Test that creation of MDStrings with the same string contents produces the
132// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000133TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000134 char x[4] = { 'a', 'b', 'c', 'X' };
135 char y[4] = { 'a', 'b', 'c', 'Y' };
136
Owen Anderson23587322009-07-31 21:38:10 +0000137 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
138 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000139 EXPECT_EQ(s1, s2);
140}
141
142// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000143TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000144 char *str = new char[13];
145 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +0000146 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +0000147 strncpy(str, "aaaaaaaaaaaaa", 13);
148 delete[] str;
149
Chris Lattnerbe354a62009-08-23 04:47:35 +0000150 std::string Str;
151 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000152 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000153 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000154}
155
156// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000157TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000158 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000159 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000160 std::string Str;
161 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000162 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000163 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000164}
165
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000166typedef MetadataTest MDNodeTest;
167
Nick Lewycky49f89192009-04-04 07:22:01 +0000168// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000169TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000170 char x[3] = { 'a', 'b', 'c' };
171 char y[3] = { '1', '2', '3' };
172
Owen Anderson23587322009-07-31 21:38:10 +0000173 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
174 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000175 ConstantAsMetadata *CI = ConstantAsMetadata::get(
176 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000177
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000178 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000179 V.push_back(s1);
180 V.push_back(CI);
181 V.push_back(s2);
182
Jay Foad5514afe2011-04-21 19:59:31 +0000183 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000184 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000185 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000186 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000187 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000188 MDNode *n4 = MDNode::getIfExists(Context, V);
189 MDNode *n5 = MDNode::getIfExists(Context, c1);
190 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000191 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000192 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000193 EXPECT_EQ(n4, n1);
194 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000195 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000196
Chris Lattner9b493022009-12-31 01:22:29 +0000197 EXPECT_EQ(3u, n1->getNumOperands());
198 EXPECT_EQ(s1, n1->getOperand(0));
199 EXPECT_EQ(CI, n1->getOperand(1));
200 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000201
Chris Lattner9b493022009-12-31 01:22:29 +0000202 EXPECT_EQ(1u, n2->getNumOperands());
203 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000204}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000205
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000206TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000207 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
208 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000209
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000210 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000211 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000212 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000213
214 EXPECT_EQ(n, wvh);
215
216 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000217}
Devang Patel0924b332009-07-30 00:03:41 +0000218
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000219TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000220 // !0 = !{!0}
221 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000222 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000223 auto Temp = MDNode::getTemporary(Context, None);
224 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000225 MDNode *Self = MDNode::get(Context, Args);
226 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000227 ASSERT_EQ(Self, Self->getOperand(0));
228
229 // Self-references should be distinct, so MDNode::get() should grab a
230 // uniqued node that references Self, not Self.
231 Args[0] = Self;
232 MDNode *Ref1 = MDNode::get(Context, Args);
233 MDNode *Ref2 = MDNode::get(Context, Args);
234 EXPECT_NE(Self, Ref1);
235 EXPECT_EQ(Ref1, Ref2);
236 }
237
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000238 // !0 = !{!0, !{}}
239 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000240 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000241 auto Temp = MDNode::getTemporary(Context, None);
242 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000243 MDNode *Self = MDNode::get(Context, Args);
244 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000245 ASSERT_EQ(Self, Self->getOperand(0));
246
247 // Self-references should be distinct, so MDNode::get() should grab a
248 // uniqued node that references Self, not Self itself.
249 Args[0] = Self;
250 MDNode *Ref1 = MDNode::get(Context, Args);
251 MDNode *Ref2 = MDNode::get(Context, Args);
252 EXPECT_NE(Self, Ref1);
253 EXPECT_EQ(Ref1, Ref2);
254 }
255}
256
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000257TEST_F(MDNodeTest, Print) {
258 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
259 MDString *S = MDString::get(Context, "foo");
260 MDNode *N0 = getNode();
261 MDNode *N1 = getNode(N0);
262 MDNode *N2 = getNode(N0, N1);
263
264 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
265 MDNode *N = MDNode::get(Context, Args);
266
267 std::string Expected;
268 {
269 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000270 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000271 C->printAsOperand(OS);
272 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000273 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000274 OS << ", null";
275 MDNode *Nodes[] = {N0, N1, N2};
276 for (auto *Node : Nodes)
277 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000278 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000279 }
280
281 std::string Actual;
282 {
283 raw_string_ostream OS(Actual);
284 N->print(OS);
285 }
286
287 EXPECT_EQ(Expected, Actual);
288}
289
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000290#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
291 do { \
292 std::string Actual_; \
293 raw_string_ostream OS(Actual_); \
294 PRINT; \
295 OS.flush(); \
296 std::string Expected_(EXPECTED); \
297 EXPECT_EQ(Expected_, Actual_); \
298 } while (false)
299
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +0000300TEST_F(MDNodeTest, PrintTemporary) {
301 MDNode *Arg = getNode();
302 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
303 MDNode *N = getNode(Temp.get());
304 Module M("test", Context);
305 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
306 NMD->addOperand(N);
307
308 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
309 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
310 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
311
312 // Cleanup.
313 Temp->replaceAllUsesWith(Arg);
314}
315
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000316TEST_F(MDNodeTest, PrintFromModule) {
317 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
318 MDString *S = MDString::get(Context, "foo");
319 MDNode *N0 = getNode();
320 MDNode *N1 = getNode(N0);
321 MDNode *N2 = getNode(N0, N1);
322
323 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
324 MDNode *N = MDNode::get(Context, Args);
325 Module M("test", Context);
326 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
327 NMD->addOperand(N);
328
329 std::string Expected;
330 {
331 raw_string_ostream OS(Expected);
332 OS << "!0 = !{";
333 C->printAsOperand(OS);
334 OS << ", ";
335 S->printAsOperand(OS);
336 OS << ", null, !1, !2, !3}";
337 }
338
339 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
340}
341
342TEST_F(MDNodeTest, PrintFromFunction) {
343 Module M("test", Context);
344 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
345 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
346 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
347 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
348 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
349 auto *R0 = ReturnInst::Create(Context, BB0);
350 auto *R1 = ReturnInst::Create(Context, BB1);
351 auto *N0 = MDNode::getDistinct(Context, None);
352 auto *N1 = MDNode::getDistinct(Context, None);
353 R0->setMetadata("md", N0);
354 R1->setMetadata("md", N1);
355
356 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
357 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
358}
359
360TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
361 Module M("test", Context);
362
363 auto *Intrinsic =
364 Function::Create(FunctionType::get(Type::getVoidTy(Context),
365 Type::getMetadataTy(Context), false),
366 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
367
368 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
369 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
370 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
371 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
372 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
373 auto *N0 = MDNode::getDistinct(Context, None);
374 auto *N1 = MDNode::getDistinct(Context, None);
375 auto *MAV0 = MetadataAsValue::get(Context, N0);
376 auto *MAV1 = MetadataAsValue::get(Context, N1);
377 CallInst::Create(Intrinsic, MAV0, "", BB0);
378 CallInst::Create(Intrinsic, MAV1, "", BB1);
379
380 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
381 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
382 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
383 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
384 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
385 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
386}
387#undef EXPECT_PRINTER_EQ
388
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000389TEST_F(MDNodeTest, NullOperand) {
390 // metadata !{}
391 MDNode *Empty = MDNode::get(Context, None);
392
393 // metadata !{metadata !{}}
394 Metadata *Ops[] = {Empty};
395 MDNode *N = MDNode::get(Context, Ops);
396 ASSERT_EQ(Empty, N->getOperand(0));
397
398 // metadata !{metadata !{}} => metadata !{null}
399 N->replaceOperandWith(0, nullptr);
400 ASSERT_EQ(nullptr, N->getOperand(0));
401
402 // metadata !{null}
403 Ops[0] = nullptr;
404 MDNode *NullOp = MDNode::get(Context, Ops);
405 ASSERT_EQ(nullptr, NullOp->getOperand(0));
406 EXPECT_EQ(N, NullOp);
407}
408
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000409TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
410 // !{}
411 MDNode *Empty = MDNode::get(Context, None);
412 ASSERT_TRUE(Empty->isResolved());
413 EXPECT_FALSE(Empty->isDistinct());
414
415 // !{!{}}
416 Metadata *Wrapped1Ops[] = {Empty};
417 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
418 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
419 ASSERT_TRUE(Wrapped1->isResolved());
420 EXPECT_FALSE(Wrapped1->isDistinct());
421
422 // !{!{!{}}}
423 Metadata *Wrapped2Ops[] = {Wrapped1};
424 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
425 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
426 ASSERT_TRUE(Wrapped2->isResolved());
427 EXPECT_FALSE(Wrapped2->isDistinct());
428
429 // !{!{!{}}} => !{!{}}
430 Wrapped2->replaceOperandWith(0, Empty);
431 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
432 EXPECT_TRUE(Wrapped2->isDistinct());
433 EXPECT_FALSE(Wrapped1->isDistinct());
434}
435
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000436TEST_F(MDNodeTest, getDistinct) {
437 // !{}
438 MDNode *Empty = MDNode::get(Context, None);
439 ASSERT_TRUE(Empty->isResolved());
440 ASSERT_FALSE(Empty->isDistinct());
441 ASSERT_EQ(Empty, MDNode::get(Context, None));
442
443 // distinct !{}
444 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
445 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
446 EXPECT_TRUE(Distinct1->isResolved());
447 EXPECT_TRUE(Distinct2->isDistinct());
448 EXPECT_NE(Empty, Distinct1);
449 EXPECT_NE(Empty, Distinct2);
450 EXPECT_NE(Distinct1, Distinct2);
451
452 // !{}
453 ASSERT_EQ(Empty, MDNode::get(Context, None));
454}
455
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000456TEST_F(MDNodeTest, isUniqued) {
457 MDNode *U = MDTuple::get(Context, None);
458 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000459 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000460 EXPECT_TRUE(U->isUniqued());
461 EXPECT_FALSE(D->isUniqued());
462 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000463}
464
465TEST_F(MDNodeTest, isDistinct) {
466 MDNode *U = MDTuple::get(Context, None);
467 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000468 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000469 EXPECT_FALSE(U->isDistinct());
470 EXPECT_TRUE(D->isDistinct());
471 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000472}
473
474TEST_F(MDNodeTest, isTemporary) {
475 MDNode *U = MDTuple::get(Context, None);
476 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000477 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000478 EXPECT_FALSE(U->isTemporary());
479 EXPECT_FALSE(D->isTemporary());
480 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000481}
482
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000483TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
484 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000485 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000486 ASSERT_FALSE(Temp->isResolved());
487
488 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000489 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000490 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
491 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000492 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000493
494 // temporary !{} => !{}
495 MDNode *Empty = MDNode::get(Context, None);
496 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000497 EXPECT_EQ(Empty, Distinct->getOperand(0));
498}
499
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000500TEST_F(MDNodeTest, handleChangedOperandRecursion) {
501 // !0 = !{}
502 MDNode *N0 = MDNode::get(Context, None);
503
504 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000505 auto Temp3 = MDTuple::getTemporary(Context, None);
506 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000507 MDNode *N1 = MDNode::get(Context, Ops1);
508
509 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000510 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000511 MDNode *N2 = MDNode::get(Context, Ops2);
512
513 // !3 = !{!2}
514 Metadata *Ops3[] = {N2};
515 MDNode *N3 = MDNode::get(Context, Ops3);
516 Temp3->replaceAllUsesWith(N3);
517
518 // !4 = !{!1}
519 Metadata *Ops4[] = {N1};
520 MDNode *N4 = MDNode::get(Context, Ops4);
521
522 // Confirm that the cycle prevented RAUW from getting dropped.
523 EXPECT_TRUE(N0->isResolved());
524 EXPECT_FALSE(N1->isResolved());
525 EXPECT_FALSE(N2->isResolved());
526 EXPECT_FALSE(N3->isResolved());
527 EXPECT_FALSE(N4->isResolved());
528
529 // Create a couple of distinct nodes to observe what's going on.
530 //
531 // !5 = distinct !{!2}
532 // !6 = distinct !{!3}
533 Metadata *Ops5[] = {N2};
534 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
535 Metadata *Ops6[] = {N3};
536 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
537
538 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
539 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
540 // references !3, this can cause a re-entry of handleChangedOperand() when !3
541 // is not ready for it.
542 //
543 // !2->replaceOperandWith(1, nullptr)
544 // !2: !{!3, !0} => !{!3, null}
545 // !2->replaceAllUsesWith(!1)
546 // !3: !{!2] => !{!1}
547 // !3->replaceAllUsesWith(!4)
548 N2->replaceOperandWith(1, nullptr);
549
550 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
551 // under us. Just check that the other nodes are sane.
552 //
553 // !1 = !{!4, null}
554 // !4 = !{!1}
555 // !5 = distinct !{!1}
556 // !6 = distinct !{!4}
557 EXPECT_EQ(N4, N1->getOperand(0));
558 EXPECT_EQ(N1, N4->getOperand(0));
559 EXPECT_EQ(N1, N5->getOperand(0));
560 EXPECT_EQ(N4, N6->getOperand(0));
561}
562
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000563TEST_F(MDNodeTest, replaceResolvedOperand) {
564 // Check code for replacing one resolved operand with another. If doing this
565 // directly (via replaceOperandWith()) becomes illegal, change the operand to
566 // a global value that gets RAUW'ed.
567 //
568 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000569 auto Temp = MDTuple::getTemporary(Context, None);
570 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000571
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000572 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000573 MDNode *N = MDTuple::get(Context, Ops);
574 EXPECT_EQ(nullptr, N->getOperand(0));
575 ASSERT_FALSE(N->isResolved());
576
577 // Check code for replacing resolved nodes.
578 N->replaceOperandWith(0, Empty);
579 EXPECT_EQ(Empty, N->getOperand(0));
580
581 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000582 N->replaceOperandWith(0, Temp.get());
583 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000584
585 // Remove the references to Temp; required for teardown.
586 Temp->replaceAllUsesWith(nullptr);
587}
588
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000589TEST_F(MDNodeTest, replaceWithUniqued) {
590 auto *Empty = MDTuple::get(Context, None);
591 MDTuple *FirstUniqued;
592 {
593 Metadata *Ops[] = {Empty};
594 auto Temp = MDTuple::getTemporary(Context, Ops);
595 EXPECT_TRUE(Temp->isTemporary());
596
597 // Don't expect a collision.
598 auto *Current = Temp.get();
599 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
600 EXPECT_TRUE(FirstUniqued->isUniqued());
601 EXPECT_TRUE(FirstUniqued->isResolved());
602 EXPECT_EQ(Current, FirstUniqued);
603 }
604 {
605 Metadata *Ops[] = {Empty};
606 auto Temp = MDTuple::getTemporary(Context, Ops);
607 EXPECT_TRUE(Temp->isTemporary());
608
609 // Should collide with Uniqued above this time.
610 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
611 EXPECT_TRUE(Uniqued->isUniqued());
612 EXPECT_TRUE(Uniqued->isResolved());
613 EXPECT_EQ(FirstUniqued, Uniqued);
614 }
615 {
616 auto Unresolved = MDTuple::getTemporary(Context, None);
617 Metadata *Ops[] = {Unresolved.get()};
618 auto Temp = MDTuple::getTemporary(Context, Ops);
619 EXPECT_TRUE(Temp->isTemporary());
620
621 // Shouldn't be resolved.
622 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
623 EXPECT_TRUE(Uniqued->isUniqued());
624 EXPECT_FALSE(Uniqued->isResolved());
625
626 // Should be a different node.
627 EXPECT_NE(FirstUniqued, Uniqued);
628
629 // Should resolve when we update its node (note: be careful to avoid a
630 // collision with any other nodes above).
631 Uniqued->replaceOperandWith(0, nullptr);
632 EXPECT_TRUE(Uniqued->isResolved());
633 }
634}
635
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000636TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000637 // temp !{}
638 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
639 EXPECT_FALSE(Op->isResolved());
640
641 // temp !{temp !{}}
642 Metadata *Ops[] = {Op};
643 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
644 EXPECT_FALSE(N->isResolved());
645
646 // temp !{temp !{}} => !{temp !{}}
647 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
648 EXPECT_FALSE(N->isResolved());
649
650 // !{temp !{}} => !{!{}}
651 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
652 EXPECT_TRUE(Op->isResolved());
653 EXPECT_TRUE(N->isResolved());
654}
655
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000656TEST_F(MDNodeTest, replaceWithUniquedChangingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000657 // i1* @GV
658 Type *Ty = Type::getInt1PtrTy(Context);
659 std::unique_ptr<GlobalVariable> GV(
660 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
661 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
662
663 // temp !{i1* @GV}
664 Metadata *Ops[] = {Op};
665 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
666
667 // temp !{i1* @GV} => !{i1* @GV}
668 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
669 ASSERT_TRUE(N->isUniqued());
670
671 // !{i1* @GV} => !{null}
672 GV.reset();
673 ASSERT_TRUE(N->isUniqued());
674 Metadata *NullOps[] = {nullptr};
675 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
676}
677
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000678TEST_F(MDNodeTest, replaceWithDistinct) {
679 {
680 auto *Empty = MDTuple::get(Context, None);
681 Metadata *Ops[] = {Empty};
682 auto Temp = MDTuple::getTemporary(Context, Ops);
683 EXPECT_TRUE(Temp->isTemporary());
684
685 // Don't expect a collision.
686 auto *Current = Temp.get();
687 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
688 EXPECT_TRUE(Distinct->isDistinct());
689 EXPECT_TRUE(Distinct->isResolved());
690 EXPECT_EQ(Current, Distinct);
691 }
692 {
693 auto Unresolved = MDTuple::getTemporary(Context, None);
694 Metadata *Ops[] = {Unresolved.get()};
695 auto Temp = MDTuple::getTemporary(Context, Ops);
696 EXPECT_TRUE(Temp->isTemporary());
697
698 // Don't expect a collision.
699 auto *Current = Temp.get();
700 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
701 EXPECT_TRUE(Distinct->isDistinct());
702 EXPECT_TRUE(Distinct->isResolved());
703 EXPECT_EQ(Current, Distinct);
704
705 // Cleanup; required for teardown.
706 Unresolved->replaceAllUsesWith(nullptr);
707 }
708}
709
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000710TEST_F(MDNodeTest, replaceWithPermanent) {
711 Metadata *Ops[] = {nullptr};
712 auto Temp = MDTuple::getTemporary(Context, Ops);
713 auto *T = Temp.get();
714
715 // U is a normal, uniqued node that references T.
716 auto *U = MDTuple::get(Context, T);
717 EXPECT_TRUE(U->isUniqued());
718
719 // Make Temp self-referencing.
720 Temp->replaceOperandWith(0, T);
721
722 // Try to uniquify Temp. This should, despite the name in the API, give a
723 // 'distinct' node, since self-references aren't allowed to be uniqued.
724 //
725 // Since it's distinct, N should have the same address as when it was a
726 // temporary (i.e., be equal to T not U).
727 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
728 EXPECT_EQ(N, T);
729 EXPECT_TRUE(N->isDistinct());
730
731 // U should be the canonical unique node with N as the argument.
732 EXPECT_EQ(U, MDTuple::get(Context, N));
733 EXPECT_TRUE(U->isUniqued());
734
735 // This temporary should collide with U when replaced, but it should still be
736 // uniqued.
737 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
738 EXPECT_TRUE(U->isUniqued());
739
740 // This temporary should become a new uniqued node.
741 auto Temp2 = MDTuple::getTemporary(Context, U);
742 auto *V = Temp2.get();
743 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
744 EXPECT_TRUE(V->isUniqued());
745 EXPECT_EQ(U, V->getOperand(0));
746}
747
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000748TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
749 TrackingMDRef Ref;
750 EXPECT_EQ(nullptr, Ref.get());
751 {
752 auto Temp = MDTuple::getTemporary(Context, None);
753 Ref.reset(Temp.get());
754 EXPECT_EQ(Temp.get(), Ref.get());
755 }
756 EXPECT_EQ(nullptr, Ref.get());
757}
758
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000759typedef MetadataTest MDLocationTest;
760
761TEST_F(MDLocationTest, Overflow) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000762 MDSubprogram *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000763 {
764 MDLocation *L = MDLocation::get(Context, 2, 7, N);
765 EXPECT_EQ(2u, L->getLine());
766 EXPECT_EQ(7u, L->getColumn());
767 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000768 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000769 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000770 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
771 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000772 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000773 }
774 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000775 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
776 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000777 EXPECT_EQ(0u, L->getColumn());
778 }
779 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000780 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
781 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000782 EXPECT_EQ(0u, L->getColumn());
783 }
784}
785
786TEST_F(MDLocationTest, getDistinct) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000787 MDNode *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000788 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
789 EXPECT_TRUE(L0->isDistinct());
790 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
791 EXPECT_FALSE(L1->isDistinct());
792 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
793}
794
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000795TEST_F(MDLocationTest, getTemporary) {
796 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000797 auto L = MDLocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000798 EXPECT_TRUE(L->isTemporary());
799 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000800}
801
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000802typedef MetadataTest GenericDebugNodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000803
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000804TEST_F(GenericDebugNodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000805 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000806 auto *Empty = MDNode::get(Context, None);
807 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000808 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000809 EXPECT_EQ(15u, N->getTag());
810 EXPECT_EQ(2u, N->getNumOperands());
811 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000812 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000813 EXPECT_EQ(1u, N->getNumDwarfOperands());
814 EXPECT_EQ(Empty, N->getDwarfOperand(0));
815 EXPECT_EQ(Empty, N->getOperand(1));
816 ASSERT_TRUE(N->isUniqued());
817
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000818 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000819
820 N->replaceOperandWith(1, nullptr);
821 EXPECT_EQ(15u, N->getTag());
822 EXPECT_EQ(Header, N->getHeader());
823 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
824 ASSERT_TRUE(N->isUniqued());
825
826 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000827 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000828
829 N->replaceDwarfOperandWith(0, Empty);
830 EXPECT_EQ(15u, N->getTag());
831 EXPECT_EQ(Header, N->getHeader());
832 EXPECT_EQ(Empty, N->getDwarfOperand(0));
833 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000834 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000835
836 TempGenericDebugNode Temp = N->clone();
837 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000838}
839
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000840TEST_F(GenericDebugNodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000841 // Canonicalize !"" to null.
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000842 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
843 EXPECT_EQ(StringRef(), N->getHeader());
844 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000845}
846
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000847typedef MetadataTest MDSubrangeTest;
848
849TEST_F(MDSubrangeTest, get) {
850 auto *N = MDSubrange::get(Context, 5, 7);
851 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
852 EXPECT_EQ(5, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000853 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000854 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
855 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000856
857 TempMDSubrange Temp = N->clone();
858 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000859}
860
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000861TEST_F(MDSubrangeTest, getEmptyArray) {
862 auto *N = MDSubrange::get(Context, -1, 0);
863 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
864 EXPECT_EQ(-1, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000865 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000866 EXPECT_EQ(N, MDSubrange::get(Context, -1, 0));
867}
868
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000869typedef MetadataTest MDEnumeratorTest;
870
871TEST_F(MDEnumeratorTest, get) {
872 auto *N = MDEnumerator::get(Context, 7, "name");
873 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
874 EXPECT_EQ(7, N->getValue());
875 EXPECT_EQ("name", N->getName());
876 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
877
878 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
879 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000880
881 TempMDEnumerator Temp = N->clone();
882 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000883}
884
885typedef MetadataTest MDBasicTypeTest;
886
887TEST_F(MDBasicTypeTest, get) {
888 auto *N =
889 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
890 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
891 EXPECT_EQ("special", N->getName());
892 EXPECT_EQ(33u, N->getSizeInBits());
893 EXPECT_EQ(26u, N->getAlignInBits());
894 EXPECT_EQ(7u, N->getEncoding());
895 EXPECT_EQ(0u, N->getLine());
896 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
897 26, 7));
898
899 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
900 "special", 33, 26, 7));
901 EXPECT_NE(N,
902 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
903 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
904 26, 7));
905 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
906 25, 7));
907 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
908 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000909
910 TempMDBasicType Temp = N->clone();
911 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000912}
913
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000914TEST_F(MDBasicTypeTest, getWithLargeValues) {
915 auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
916 UINT64_MAX, UINT64_MAX - 1, 7);
917 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
918 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
919}
920
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000921TEST_F(MDBasicTypeTest, getUnspecified) {
922 auto *N =
923 MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
924 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
925 EXPECT_EQ("unspecified", N->getName());
926 EXPECT_EQ(0u, N->getSizeInBits());
927 EXPECT_EQ(0u, N->getAlignInBits());
928 EXPECT_EQ(0u, N->getEncoding());
929 EXPECT_EQ(0u, N->getLine());
930}
931
932typedef MetadataTest MDTypeTest;
933
934TEST_F(MDTypeTest, clone) {
935 // Check that MDType has a specialized clone that returns TempMDType.
936 MDType *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
937 dwarf::DW_ATE_signed);
938
939 TempMDType Temp = N->clone();
940 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
941}
942
943TEST_F(MDTypeTest, setFlags) {
944 // void (void)
945 Metadata *TypesOps[] = {nullptr};
946 Metadata *Types = MDTuple::get(Context, TypesOps);
947
948 MDType *D = MDSubroutineType::getDistinct(Context, 0u, Types);
949 EXPECT_EQ(0u, D->getFlags());
950 D->setFlags(DIDescriptor::FlagRValueReference);
951 EXPECT_EQ(DIDescriptor::FlagRValueReference, D->getFlags());
952 D->setFlags(0u);
953 EXPECT_EQ(0u, D->getFlags());
954
955 TempMDType T = MDSubroutineType::getTemporary(Context, 0u, Types);
956 EXPECT_EQ(0u, T->getFlags());
957 T->setFlags(DIDescriptor::FlagRValueReference);
958 EXPECT_EQ(DIDescriptor::FlagRValueReference, T->getFlags());
959 T->setFlags(0u);
960 EXPECT_EQ(0u, T->getFlags());
961}
962
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000963typedef MetadataTest MDDerivedTypeTest;
964
965TEST_F(MDDerivedTypeTest, get) {
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000966 MDFile *File = getFile();
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000967 MDScopeRef Scope = getSubprogramRef();
968 MDTypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000969 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000970
971 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
972 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
973 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
974 EXPECT_EQ("something", N->getName());
975 EXPECT_EQ(File, N->getFile());
976 EXPECT_EQ(1u, N->getLine());
977 EXPECT_EQ(Scope, N->getScope());
978 EXPECT_EQ(BaseType, N->getBaseType());
979 EXPECT_EQ(2u, N->getSizeInBits());
980 EXPECT_EQ(3u, N->getAlignInBits());
981 EXPECT_EQ(4u, N->getOffsetInBits());
982 EXPECT_EQ(5u, N->getFlags());
983 EXPECT_EQ(ExtraData, N->getExtraData());
984 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
985 "something", File, 1, Scope, BaseType, 2, 3,
986 4, 5, ExtraData));
987
988 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
989 "something", File, 1, Scope, BaseType, 2, 3,
990 4, 5, ExtraData));
991 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
992 File, 1, Scope, BaseType, 2, 3, 4, 5,
993 ExtraData));
994 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000995 "something", getFile(), 1, Scope, BaseType, 2,
996 3, 4, 5, ExtraData));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000997 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
998 "something", File, 2, Scope, BaseType, 2, 3,
999 4, 5, ExtraData));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001000 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001001 "something", File, 1, getSubprogramRef(),
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001002 BaseType, 2, 3, 4, 5, ExtraData));
1003 EXPECT_NE(N, MDDerivedType::get(
1004 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1005 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001006 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1007 "something", File, 1, Scope, BaseType, 3, 3,
1008 4, 5, ExtraData));
1009 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1010 "something", File, 1, Scope, BaseType, 2, 2,
1011 4, 5, ExtraData));
1012 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1013 "something", File, 1, Scope, BaseType, 2, 3,
1014 5, 5, ExtraData));
1015 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1016 "something", File, 1, Scope, BaseType, 2, 3,
1017 4, 4, ExtraData));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001018 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1019 "something", File, 1, Scope, BaseType, 2, 3,
1020 4, 5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001021
1022 TempMDDerivedType Temp = N->clone();
1023 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001024}
1025
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001026TEST_F(MDDerivedTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001027 MDFile *File = getFile();
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001028 MDScopeRef Scope = getSubprogramRef();
1029 MDTypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001030 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001031
1032 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
1033 File, 1, Scope, BaseType, UINT64_MAX,
1034 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1035 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1036 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1037 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1038}
1039
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001040typedef MetadataTest MDCompositeTypeTest;
1041
1042TEST_F(MDCompositeTypeTest, get) {
1043 unsigned Tag = dwarf::DW_TAG_structure_type;
1044 StringRef Name = "some name";
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001045 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001046 unsigned Line = 1;
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001047 MDScopeRef Scope = getSubprogramRef();
1048 MDTypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001049 uint64_t SizeInBits = 2;
1050 uint64_t AlignInBits = 3;
1051 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001052 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001053 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001054 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001055 MDTypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001056 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001057 StringRef Identifier = "some id";
1058
1059 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1060 BaseType, SizeInBits, AlignInBits,
1061 OffsetInBits, Flags, Elements, RuntimeLang,
1062 VTableHolder, TemplateParams, Identifier);
1063 EXPECT_EQ(Tag, N->getTag());
1064 EXPECT_EQ(Name, N->getName());
1065 EXPECT_EQ(File, N->getFile());
1066 EXPECT_EQ(Line, N->getLine());
1067 EXPECT_EQ(Scope, N->getScope());
1068 EXPECT_EQ(BaseType, N->getBaseType());
1069 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1070 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1071 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1072 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001073 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001074 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1075 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001076 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001077 EXPECT_EQ(Identifier, N->getIdentifier());
1078
1079 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1080 BaseType, SizeInBits, AlignInBits,
1081 OffsetInBits, Flags, Elements, RuntimeLang,
1082 VTableHolder, TemplateParams, Identifier));
1083
1084 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
1085 BaseType, SizeInBits, AlignInBits,
1086 OffsetInBits, Flags, Elements, RuntimeLang,
1087 VTableHolder, TemplateParams, Identifier));
1088 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
1089 BaseType, SizeInBits, AlignInBits,
1090 OffsetInBits, Flags, Elements, RuntimeLang,
1091 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001092 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001093 BaseType, SizeInBits, AlignInBits,
1094 OffsetInBits, Flags, Elements, RuntimeLang,
1095 VTableHolder, TemplateParams, Identifier));
1096 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
1097 BaseType, SizeInBits, AlignInBits,
1098 OffsetInBits, Flags, Elements, RuntimeLang,
1099 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001100 EXPECT_NE(N, MDCompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001101 Context, Tag, Name, File, Line, getSubprogramRef(), BaseType,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001102 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1103 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001104 EXPECT_NE(N, MDCompositeType::get(
1105 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1106 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1107 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001108 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1109 BaseType, SizeInBits + 1, AlignInBits,
1110 OffsetInBits, Flags, Elements, RuntimeLang,
1111 VTableHolder, TemplateParams, Identifier));
1112 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1113 BaseType, SizeInBits, AlignInBits + 1,
1114 OffsetInBits, Flags, Elements, RuntimeLang,
1115 VTableHolder, TemplateParams, Identifier));
1116 EXPECT_NE(N, MDCompositeType::get(
1117 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1118 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1119 VTableHolder, TemplateParams, Identifier));
1120 EXPECT_NE(N, MDCompositeType::get(
1121 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1122 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1123 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001124 EXPECT_NE(N, MDCompositeType::get(
1125 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1126 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1127 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001128 EXPECT_NE(N, MDCompositeType::get(
1129 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1130 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1131 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001132 EXPECT_NE(N, MDCompositeType::get(
1133 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1134 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1135 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001136 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1137 BaseType, SizeInBits, AlignInBits,
1138 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001139 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001140 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1141 BaseType, SizeInBits, AlignInBits,
1142 OffsetInBits, Flags, Elements, RuntimeLang,
1143 VTableHolder, TemplateParams, "other"));
1144
1145 // Be sure that missing identifiers get null pointers.
1146 EXPECT_FALSE(MDCompositeType::get(
1147 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1148 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1149 VTableHolder, TemplateParams, "")->getRawIdentifier());
1150 EXPECT_FALSE(MDCompositeType::get(
1151 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1152 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1153 VTableHolder, TemplateParams)->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001154
1155 TempMDCompositeType Temp = N->clone();
1156 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001157}
1158
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001159TEST_F(MDCompositeTypeTest, getWithLargeValues) {
1160 unsigned Tag = dwarf::DW_TAG_structure_type;
1161 StringRef Name = "some name";
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001162 MDFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001163 unsigned Line = 1;
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001164 MDScopeRef Scope = getSubprogramRef();
1165 MDTypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001166 uint64_t SizeInBits = UINT64_MAX;
1167 uint64_t AlignInBits = UINT64_MAX - 1;
1168 uint64_t OffsetInBits = UINT64_MAX - 2;
1169 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001170 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001171 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001172 MDTypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001173 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001174 StringRef Identifier = "some id";
1175
1176 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1177 BaseType, SizeInBits, AlignInBits,
1178 OffsetInBits, Flags, Elements, RuntimeLang,
1179 VTableHolder, TemplateParams, Identifier);
1180 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1181 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1182 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1183}
1184
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001185TEST_F(MDCompositeTypeTest, replaceOperands) {
1186 unsigned Tag = dwarf::DW_TAG_structure_type;
1187 StringRef Name = "some name";
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001188 MDFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001189 unsigned Line = 1;
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001190 MDScopeRef Scope = getSubprogramRef();
1191 MDTypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001192 uint64_t SizeInBits = 2;
1193 uint64_t AlignInBits = 3;
1194 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001195 unsigned Flags = 5;
1196 unsigned RuntimeLang = 6;
1197 StringRef Identifier = "some id";
1198
1199 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1200 BaseType, SizeInBits, AlignInBits,
1201 OffsetInBits, Flags, nullptr, RuntimeLang,
1202 nullptr, nullptr, Identifier);
1203
1204 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001205 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001206 N->replaceElements(Elements);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001207 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001208 N->replaceElements(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001209 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001210
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001211 MDTypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001212 EXPECT_EQ(nullptr, N->getVTableHolder());
1213 N->replaceVTableHolder(VTableHolder);
1214 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1215 N->replaceVTableHolder(nullptr);
1216 EXPECT_EQ(nullptr, N->getVTableHolder());
1217
1218 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001219 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001220 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001221 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001222 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001223 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001224}
1225
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001226typedef MetadataTest MDSubroutineTypeTest;
1227
1228TEST_F(MDSubroutineTypeTest, get) {
1229 unsigned Flags = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001230 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001231
1232 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
1233 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1234 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001235 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001236 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
1237
1238 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001239 EXPECT_NE(N, MDSubroutineType::get(Context, Flags, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001240
1241 TempMDSubroutineType Temp = N->clone();
1242 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001243
1244 // Test always-empty operands.
1245 EXPECT_EQ(nullptr, N->getScope());
1246 EXPECT_EQ(nullptr, N->getFile());
1247 EXPECT_EQ("", N->getName());
1248 EXPECT_EQ(nullptr, N->getBaseType());
1249 EXPECT_EQ(nullptr, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001250 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001251 EXPECT_EQ("", N->getIdentifier());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001252}
1253
1254typedef MetadataTest MDFileTest;
1255
1256TEST_F(MDFileTest, get) {
1257 StringRef Filename = "file";
1258 StringRef Directory = "dir";
1259 auto *N = MDFile::get(Context, Filename, Directory);
1260
1261 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1262 EXPECT_EQ(Filename, N->getFilename());
1263 EXPECT_EQ(Directory, N->getDirectory());
1264 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
1265
1266 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1267 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001268
1269 TempMDFile Temp = N->clone();
1270 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001271}
1272
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001273TEST_F(MDFileTest, ScopeGetFile) {
1274 // Ensure that MDScope::getFile() returns itself.
1275 MDScope *N = MDFile::get(Context, "file", "dir");
1276 EXPECT_EQ(N, N->getFile());
1277}
1278
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001279typedef MetadataTest MDCompileUnitTest;
1280
1281TEST_F(MDCompileUnitTest, get) {
1282 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001283 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001284 StringRef Producer = "some producer";
1285 bool IsOptimized = false;
1286 StringRef Flags = "flag after flag";
1287 unsigned RuntimeVersion = 2;
1288 StringRef SplitDebugFilename = "another/file";
1289 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001290 MDTuple *EnumTypes = getTuple();
1291 MDTuple *RetainedTypes = getTuple();
1292 MDTuple *Subprograms = getTuple();
1293 MDTuple *GlobalVariables = getTuple();
1294 MDTuple *ImportedEntities = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001295 auto *N = MDCompileUnit::get(
1296 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1297 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1298 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1299
1300 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1301 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1302 EXPECT_EQ(File, N->getFile());
1303 EXPECT_EQ(Producer, N->getProducer());
1304 EXPECT_EQ(IsOptimized, N->isOptimized());
1305 EXPECT_EQ(Flags, N->getFlags());
1306 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1307 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1308 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001309 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1310 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
1311 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1312 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1313 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001314 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1315 IsOptimized, Flags, RuntimeVersion,
1316 SplitDebugFilename, EmissionKind, EnumTypes,
1317 RetainedTypes, Subprograms, GlobalVariables,
1318 ImportedEntities));
1319
1320 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1321 IsOptimized, Flags, RuntimeVersion,
1322 SplitDebugFilename, EmissionKind, EnumTypes,
1323 RetainedTypes, Subprograms, GlobalVariables,
1324 ImportedEntities));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001325 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, getFile(), Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001326 IsOptimized, Flags, RuntimeVersion,
1327 SplitDebugFilename, EmissionKind, EnumTypes,
1328 RetainedTypes, Subprograms, GlobalVariables,
1329 ImportedEntities));
1330 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1331 IsOptimized, Flags, RuntimeVersion,
1332 SplitDebugFilename, EmissionKind, EnumTypes,
1333 RetainedTypes, Subprograms, GlobalVariables,
1334 ImportedEntities));
1335 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1336 !IsOptimized, Flags, RuntimeVersion,
1337 SplitDebugFilename, EmissionKind, EnumTypes,
1338 RetainedTypes, Subprograms, GlobalVariables,
1339 ImportedEntities));
1340 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1341 IsOptimized, "other", RuntimeVersion,
1342 SplitDebugFilename, EmissionKind, EnumTypes,
1343 RetainedTypes, Subprograms, GlobalVariables,
1344 ImportedEntities));
1345 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1346 IsOptimized, Flags, RuntimeVersion + 1,
1347 SplitDebugFilename, EmissionKind, EnumTypes,
1348 RetainedTypes, Subprograms, GlobalVariables,
1349 ImportedEntities));
1350 EXPECT_NE(N,
1351 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1352 IsOptimized, Flags, RuntimeVersion, "other",
1353 EmissionKind, EnumTypes, RetainedTypes,
1354 Subprograms, GlobalVariables, ImportedEntities));
1355 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1356 IsOptimized, Flags, RuntimeVersion,
1357 SplitDebugFilename, EmissionKind + 1,
1358 EnumTypes, RetainedTypes, Subprograms,
1359 GlobalVariables, ImportedEntities));
1360 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1361 IsOptimized, Flags, RuntimeVersion,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001362 SplitDebugFilename, EmissionKind, getTuple(),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001363 RetainedTypes, Subprograms, GlobalVariables,
1364 ImportedEntities));
1365 EXPECT_NE(N, MDCompileUnit::get(
1366 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1367 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001368 getTuple(), Subprograms, GlobalVariables, ImportedEntities));
1369 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1370 IsOptimized, Flags, RuntimeVersion,
1371 SplitDebugFilename, EmissionKind, EnumTypes,
1372 RetainedTypes, getTuple(), GlobalVariables,
1373 ImportedEntities));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001374 EXPECT_NE(N, MDCompileUnit::get(
1375 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1376 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001377 RetainedTypes, Subprograms, getTuple(), ImportedEntities));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001378 EXPECT_NE(N, MDCompileUnit::get(
1379 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1380 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001381 RetainedTypes, Subprograms, GlobalVariables, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001382
1383 TempMDCompileUnit Temp = N->clone();
1384 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001385}
1386
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001387TEST_F(MDCompileUnitTest, replaceArrays) {
1388 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001389 MDFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001390 StringRef Producer = "some producer";
1391 bool IsOptimized = false;
1392 StringRef Flags = "flag after flag";
1393 unsigned RuntimeVersion = 2;
1394 StringRef SplitDebugFilename = "another/file";
1395 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001396 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1397 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1398 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001399 auto *N = MDCompileUnit::get(
1400 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1401 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1402 RetainedTypes, nullptr, nullptr, ImportedEntities);
1403
1404 auto *Subprograms = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001405 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001406 N->replaceSubprograms(Subprograms);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001407 EXPECT_EQ(Subprograms, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001408 N->replaceSubprograms(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001409 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001410
1411 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001412 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001413 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001414 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001415 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001416 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001417}
1418
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001419typedef MetadataTest MDSubprogramTest;
1420
1421TEST_F(MDSubprogramTest, get) {
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001422 MDScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001423 StringRef Name = "name";
1424 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001425 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001426 unsigned Line = 2;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001427 MDSubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001428 bool IsLocalToUnit = false;
1429 bool IsDefinition = true;
1430 unsigned ScopeLine = 3;
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001431 MDTypeRef ContainingType = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001432 unsigned Virtuality = 4;
1433 unsigned VirtualIndex = 5;
1434 unsigned Flags = 6;
1435 bool IsOptimized = false;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001436 llvm::Function *Function = getFunction("foo");
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001437 MDTuple *TemplateParams = getTuple();
1438 MDSubprogram *Declaration = getSubprogram();
1439 MDTuple *Variables = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001440
1441 auto *N = MDSubprogram::get(
1442 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1443 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1444 IsOptimized, Function, TemplateParams, Declaration, Variables);
1445
1446 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1447 EXPECT_EQ(Scope, N->getScope());
1448 EXPECT_EQ(Name, N->getName());
1449 EXPECT_EQ(LinkageName, N->getLinkageName());
1450 EXPECT_EQ(File, N->getFile());
1451 EXPECT_EQ(Line, N->getLine());
1452 EXPECT_EQ(Type, N->getType());
1453 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1454 EXPECT_EQ(IsDefinition, N->isDefinition());
1455 EXPECT_EQ(ScopeLine, N->getScopeLine());
1456 EXPECT_EQ(ContainingType, N->getContainingType());
1457 EXPECT_EQ(Virtuality, N->getVirtuality());
1458 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1459 EXPECT_EQ(Flags, N->getFlags());
1460 EXPECT_EQ(IsOptimized, N->isOptimized());
1461 EXPECT_EQ(Function, N->getFunction());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001462 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001463 EXPECT_EQ(Declaration, N->getDeclaration());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001464 EXPECT_EQ(Variables, N->getVariables().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001465 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1466 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1467 ContainingType, Virtuality, VirtualIndex,
1468 Flags, IsOptimized, Function, TemplateParams,
1469 Declaration, Variables));
1470
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001471 EXPECT_NE(N, MDSubprogram::get(Context, getCompositeType(), Name, LinkageName,
1472 File, Line, Type, IsLocalToUnit, IsDefinition,
1473 ScopeLine, ContainingType, Virtuality,
1474 VirtualIndex, Flags, IsOptimized, Function,
1475 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001476 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1477 Line, Type, IsLocalToUnit, IsDefinition,
1478 ScopeLine, ContainingType, Virtuality,
1479 VirtualIndex, Flags, IsOptimized, Function,
1480 TemplateParams, Declaration, Variables));
1481 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1482 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1483 ContainingType, Virtuality, VirtualIndex,
1484 Flags, IsOptimized, Function, TemplateParams,
1485 Declaration, Variables));
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001486 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, getFile(),
1487 Line, Type, IsLocalToUnit, IsDefinition,
1488 ScopeLine, ContainingType, Virtuality,
1489 VirtualIndex, Flags, IsOptimized, Function,
1490 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001491 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1492 Line + 1, Type, IsLocalToUnit, IsDefinition,
1493 ScopeLine, ContainingType, Virtuality,
1494 VirtualIndex, Flags, IsOptimized, Function,
1495 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001496 EXPECT_NE(N, MDSubprogram::get(
1497 Context, Scope, Name, LinkageName, File, Line,
1498 getSubroutineType(), IsLocalToUnit, IsDefinition, ScopeLine,
1499 ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1500 Function, TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001501 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1502 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1503 ContainingType, Virtuality, VirtualIndex,
1504 Flags, IsOptimized, Function, TemplateParams,
1505 Declaration, Variables));
1506 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1507 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1508 ContainingType, Virtuality, VirtualIndex,
1509 Flags, IsOptimized, Function, TemplateParams,
1510 Declaration, Variables));
1511 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1512 Type, IsLocalToUnit, IsDefinition,
1513 ScopeLine + 1, ContainingType, Virtuality,
1514 VirtualIndex, Flags, IsOptimized, Function,
1515 TemplateParams, Declaration, Variables));
1516 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1517 Type, IsLocalToUnit, IsDefinition, ScopeLine,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001518 getCompositeType(), Virtuality, VirtualIndex,
1519 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001520 Declaration, Variables));
1521 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1522 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1523 ContainingType, Virtuality + 1, VirtualIndex,
1524 Flags, IsOptimized, Function, TemplateParams,
1525 Declaration, Variables));
1526 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1527 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1528 ContainingType, Virtuality, VirtualIndex + 1,
1529 Flags, IsOptimized, Function, TemplateParams,
1530 Declaration, Variables));
1531 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1532 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1533 ContainingType, Virtuality, VirtualIndex,
1534 ~Flags, IsOptimized, Function, TemplateParams,
1535 Declaration, Variables));
1536 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1537 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1538 ContainingType, Virtuality, VirtualIndex,
1539 Flags, !IsOptimized, Function, TemplateParams,
1540 Declaration, Variables));
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001541 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1542 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1543 ContainingType, Virtuality, VirtualIndex,
1544 Flags, IsOptimized, getFunction("bar"),
1545 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001546 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1547 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1548 ContainingType, Virtuality, VirtualIndex,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001549 Flags, IsOptimized, Function, getTuple(),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001550 Declaration, Variables));
1551 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1552 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1553 ContainingType, Virtuality, VirtualIndex,
1554 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001555 getSubprogram(), Variables));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001556 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1557 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1558 ContainingType, Virtuality, VirtualIndex,
1559 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001560 Declaration, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001561
1562 TempMDSubprogram Temp = N->clone();
1563 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001564}
1565
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001566TEST_F(MDSubprogramTest, replaceFunction) {
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001567 MDScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001568 StringRef Name = "name";
1569 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001570 MDFile *File = getFile();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001571 unsigned Line = 2;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001572 MDSubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001573 bool IsLocalToUnit = false;
1574 bool IsDefinition = true;
1575 unsigned ScopeLine = 3;
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001576 MDTypeRef ContainingType = getCompositeType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001577 unsigned Virtuality = 4;
1578 unsigned VirtualIndex = 5;
1579 unsigned Flags = 6;
1580 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001581 MDTuple *TemplateParams = getTuple();
1582 MDSubprogram *Declaration = getSubprogram();
1583 MDTuple *Variables = getTuple();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001584
1585 auto *N = MDSubprogram::get(
1586 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1587 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1588 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1589
1590 EXPECT_EQ(nullptr, N->getFunction());
1591
1592 std::unique_ptr<Function> F(
1593 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1594 GlobalValue::ExternalLinkage));
1595 N->replaceFunction(F.get());
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001596 EXPECT_EQ(F.get(), N->getFunction());
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001597
1598 N->replaceFunction(nullptr);
1599 EXPECT_EQ(nullptr, N->getFunction());
1600}
1601
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001602typedef MetadataTest MDLexicalBlockTest;
1603
1604TEST_F(MDLexicalBlockTest, get) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001605 MDLocalScope *Scope = getSubprogram();
1606 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001607 unsigned Line = 5;
1608 unsigned Column = 8;
1609
1610 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1611
1612 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1613 EXPECT_EQ(Scope, N->getScope());
1614 EXPECT_EQ(File, N->getFile());
1615 EXPECT_EQ(Line, N->getLine());
1616 EXPECT_EQ(Column, N->getColumn());
1617 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1618
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001619 EXPECT_NE(N,
1620 MDLexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1621 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, getFile(), Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001622 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1623 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001624
1625 TempMDLexicalBlock Temp = N->clone();
1626 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001627}
1628
1629typedef MetadataTest MDLexicalBlockFileTest;
1630
1631TEST_F(MDLexicalBlockFileTest, get) {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001632 MDLocalScope *Scope = getSubprogram();
1633 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001634 unsigned Discriminator = 5;
1635
1636 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1637
1638 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1639 EXPECT_EQ(Scope, N->getScope());
1640 EXPECT_EQ(File, N->getFile());
1641 EXPECT_EQ(Discriminator, N->getDiscriminator());
1642 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1643
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001644 EXPECT_NE(N, MDLexicalBlockFile::get(Context, getSubprogram(), File,
1645 Discriminator));
1646 EXPECT_NE(N,
1647 MDLexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001648 EXPECT_NE(N,
1649 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001650
1651 TempMDLexicalBlockFile Temp = N->clone();
1652 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001653}
1654
1655typedef MetadataTest MDNamespaceTest;
1656
1657TEST_F(MDNamespaceTest, get) {
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001658 MDScope *Scope = getFile();
1659 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001660 StringRef Name = "namespace";
1661 unsigned Line = 5;
1662
1663 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1664
1665 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1666 EXPECT_EQ(Scope, N->getScope());
1667 EXPECT_EQ(File, N->getFile());
1668 EXPECT_EQ(Name, N->getName());
1669 EXPECT_EQ(Line, N->getLine());
1670 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1671
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001672 EXPECT_NE(N, MDNamespace::get(Context, getFile(), File, Name, Line));
1673 EXPECT_NE(N, MDNamespace::get(Context, Scope, getFile(), Name, Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001674 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1675 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001676
1677 TempMDNamespace Temp = N->clone();
1678 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001679}
1680
1681typedef MetadataTest MDTemplateTypeParameterTest;
1682
1683TEST_F(MDTemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001684 StringRef Name = "template";
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001685 MDTypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001686
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001687 auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001688
1689 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001690 EXPECT_EQ(Name, N->getName());
1691 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001692 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001693
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001694 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001695 EXPECT_NE(N,
1696 MDTemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001697
1698 TempMDTemplateTypeParameter Temp = N->clone();
1699 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001700}
1701
1702typedef MetadataTest MDTemplateValueParameterTest;
1703
1704TEST_F(MDTemplateValueParameterTest, get) {
1705 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001706 StringRef Name = "template";
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001707 MDTypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001708 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001709
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001710 auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001711 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001712 EXPECT_EQ(Name, N->getName());
1713 EXPECT_EQ(Type, N->getType());
1714 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001715 EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001716
1717 EXPECT_NE(N, MDTemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001718 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1719 Type, Value));
1720 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001721 Value));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001722 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name,
1723 getBasicType("other"), Value));
1724 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type,
1725 getConstantAsMetadata()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001726
1727 TempMDTemplateValueParameter Temp = N->clone();
1728 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001729}
1730
1731typedef MetadataTest MDGlobalVariableTest;
1732
1733TEST_F(MDGlobalVariableTest, get) {
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001734 MDScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001735 StringRef Name = "name";
1736 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001737 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001738 unsigned Line = 5;
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001739 MDTypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001740 bool IsLocalToUnit = false;
1741 bool IsDefinition = true;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001742 Constant *Variable = getConstant();
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001743 MDDerivedType *StaticDataMemberDeclaration =
1744 cast<MDDerivedType>(getDerivedType());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001745
1746 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1747 Type, IsLocalToUnit, IsDefinition, Variable,
1748 StaticDataMemberDeclaration);
1749 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1750 EXPECT_EQ(Scope, N->getScope());
1751 EXPECT_EQ(Name, N->getName());
1752 EXPECT_EQ(LinkageName, N->getLinkageName());
1753 EXPECT_EQ(File, N->getFile());
1754 EXPECT_EQ(Line, N->getLine());
1755 EXPECT_EQ(Type, N->getType());
1756 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1757 EXPECT_EQ(IsDefinition, N->isDefinition());
1758 EXPECT_EQ(Variable, N->getVariable());
1759 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1760 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1761 Line, Type, IsLocalToUnit, IsDefinition,
1762 Variable, StaticDataMemberDeclaration));
1763
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001764 EXPECT_NE(N,
1765 MDGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
1766 File, Line, Type, IsLocalToUnit, IsDefinition,
1767 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001768 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1769 Line, Type, IsLocalToUnit, IsDefinition,
1770 Variable, StaticDataMemberDeclaration));
1771 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1772 Type, IsLocalToUnit, IsDefinition,
1773 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001774 EXPECT_NE(N,
1775 MDGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
1776 Line, Type, IsLocalToUnit, IsDefinition,
1777 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001778 EXPECT_NE(N,
1779 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1780 Line + 1, Type, IsLocalToUnit, IsDefinition,
1781 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001782 EXPECT_NE(N,
1783 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1784 getDerivedType(), IsLocalToUnit, IsDefinition,
1785 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001786 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1787 Line, Type, !IsLocalToUnit, IsDefinition,
1788 Variable, StaticDataMemberDeclaration));
1789 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1790 Line, Type, IsLocalToUnit, !IsDefinition,
1791 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001792 EXPECT_NE(N,
1793 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1794 Type, IsLocalToUnit, IsDefinition,
1795 getConstant(), StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001796 EXPECT_NE(N,
1797 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1798 Type, IsLocalToUnit, IsDefinition, Variable,
1799 cast<MDDerivedType>(getDerivedType())));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001800
1801 TempMDGlobalVariable Temp = N->clone();
1802 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001803}
1804
1805typedef MetadataTest MDLocalVariableTest;
1806
1807TEST_F(MDLocalVariableTest, get) {
1808 unsigned Tag = dwarf::DW_TAG_arg_variable;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001809 MDLocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001810 StringRef Name = "name";
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001811 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001812 unsigned Line = 5;
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001813 MDTypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001814 unsigned Arg = 6;
1815 unsigned Flags = 7;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001816 MDLocation *InlinedAt =
1817 MDLocation::getDistinct(Context, 10, 20, getSubprogram());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001818
1819 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1820 Arg, Flags, InlinedAt);
1821 EXPECT_EQ(Tag, N->getTag());
1822 EXPECT_EQ(Scope, N->getScope());
1823 EXPECT_EQ(Name, N->getName());
1824 EXPECT_EQ(File, N->getFile());
1825 EXPECT_EQ(Line, N->getLine());
1826 EXPECT_EQ(Type, N->getType());
1827 EXPECT_EQ(Arg, N->getArg());
1828 EXPECT_EQ(Flags, N->getFlags());
1829 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1830 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1831 Arg, Flags, InlinedAt));
1832
1833 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1834 Name, File, Line, Type, Arg, Flags,
1835 InlinedAt));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001836 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, getSubprogram(), Name, File,
1837 Line, Type, Arg, Flags, InlinedAt));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001838 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1839 Type, Arg, Flags, InlinedAt));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001840 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, getFile(), Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001841 Type, Arg, Flags, InlinedAt));
1842 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1843 Type, Arg, Flags, InlinedAt));
1844 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001845 getDerivedType(), Arg, Flags, InlinedAt));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001846 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1847 Arg + 1, Flags, InlinedAt));
1848 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1849 Arg, ~Flags, InlinedAt));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001850 EXPECT_NE(N, MDLocalVariable::get(
1851 Context, Tag, Scope, Name, File, Line, Type, Arg, Flags,
1852 MDLocation::getDistinct(Context, 10, 20, getSubprogram())));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001853
1854 TempMDLocalVariable Temp = N->clone();
1855 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001856
1857 auto *Inlined = N->withoutInline();
1858 EXPECT_NE(N, Inlined);
1859 EXPECT_EQ(N->getTag(), Inlined->getTag());
1860 EXPECT_EQ(N->getScope(), Inlined->getScope());
1861 EXPECT_EQ(N->getName(), Inlined->getName());
1862 EXPECT_EQ(N->getFile(), Inlined->getFile());
1863 EXPECT_EQ(N->getLine(), Inlined->getLine());
1864 EXPECT_EQ(N->getType(), Inlined->getType());
1865 EXPECT_EQ(N->getArg(), Inlined->getArg());
1866 EXPECT_EQ(N->getFlags(), Inlined->getFlags());
1867 EXPECT_EQ(nullptr, Inlined->getInlinedAt());
1868 EXPECT_EQ(N, Inlined->withInline(cast<MDLocation>(InlinedAt)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001869}
1870
1871typedef MetadataTest MDExpressionTest;
1872
1873TEST_F(MDExpressionTest, get) {
1874 uint64_t Elements[] = {2, 6, 9, 78, 0};
1875 auto *N = MDExpression::get(Context, Elements);
1876 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1877 EXPECT_EQ(N, MDExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001878
1879 EXPECT_EQ(5u, N->getNumElements());
1880 EXPECT_EQ(2u, N->getElement(0));
1881 EXPECT_EQ(6u, N->getElement(1));
1882 EXPECT_EQ(9u, N->getElement(2));
1883 EXPECT_EQ(78u, N->getElement(3));
1884 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001885
1886 TempMDExpression Temp = N->clone();
1887 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001888}
1889
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001890TEST_F(MDExpressionTest, isValid) {
1891#define EXPECT_VALID(...) \
1892 do { \
1893 uint64_t Elements[] = {__VA_ARGS__}; \
1894 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1895 } while (false)
1896#define EXPECT_INVALID(...) \
1897 do { \
1898 uint64_t Elements[] = {__VA_ARGS__}; \
1899 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1900 } while (false)
1901
1902 // Empty expression should be valid.
1903 EXPECT_TRUE(MDExpression::get(Context, None));
1904
1905 // Valid constructions.
1906 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1907 EXPECT_VALID(dwarf::DW_OP_deref);
1908 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1909 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1910 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1911 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1912 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1913
1914 // Invalid constructions.
1915 EXPECT_INVALID(~0u);
1916 EXPECT_INVALID(dwarf::DW_OP_plus);
1917 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1918 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1919 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1920 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1921
1922#undef EXPECT_VALID
1923#undef EXPECT_INVALID
1924}
1925
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001926typedef MetadataTest MDObjCPropertyTest;
1927
1928TEST_F(MDObjCPropertyTest, get) {
1929 StringRef Name = "name";
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001930 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001931 unsigned Line = 5;
1932 StringRef GetterName = "getter";
1933 StringRef SetterName = "setter";
1934 unsigned Attributes = 7;
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001935 MDType *Type = cast<MDBasicType>(getBasicType("basic"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001936
1937 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1938 SetterName, Attributes, Type);
1939
1940 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1941 EXPECT_EQ(Name, N->getName());
1942 EXPECT_EQ(File, N->getFile());
1943 EXPECT_EQ(Line, N->getLine());
1944 EXPECT_EQ(GetterName, N->getGetterName());
1945 EXPECT_EQ(SetterName, N->getSetterName());
1946 EXPECT_EQ(Attributes, N->getAttributes());
1947 EXPECT_EQ(Type, N->getType());
1948 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1949 SetterName, Attributes, Type));
1950
1951 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1952 SetterName, Attributes, Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001953 EXPECT_NE(N, MDObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001954 SetterName, Attributes, Type));
1955 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1956 SetterName, Attributes, Type));
1957 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1958 SetterName, Attributes, Type));
1959 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1960 "other", Attributes, Type));
1961 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1962 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001963 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1964 SetterName, Attributes,
1965 cast<MDBasicType>(getBasicType("other"))));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001966
1967 TempMDObjCProperty Temp = N->clone();
1968 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001969}
1970
1971typedef MetadataTest MDImportedEntityTest;
1972
1973TEST_F(MDImportedEntityTest, get) {
1974 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001975 MDScope *Scope = getSubprogram();
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001976 DebugNodeRef Entity = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001977 unsigned Line = 5;
1978 StringRef Name = "name";
1979
1980 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1981
1982 EXPECT_EQ(Tag, N->getTag());
1983 EXPECT_EQ(Scope, N->getScope());
1984 EXPECT_EQ(Entity, N->getEntity());
1985 EXPECT_EQ(Line, N->getLine());
1986 EXPECT_EQ(Name, N->getName());
1987 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1988
1989 EXPECT_NE(N,
1990 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1991 Scope, Entity, Line, Name));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001992 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, getSubprogram(), Entity,
1993 Line, Name));
1994 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, getCompositeType(),
1995 Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001996 EXPECT_NE(N,
1997 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1998 EXPECT_NE(N,
1999 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002000
2001 TempMDImportedEntity Temp = N->clone();
2002 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002003}
2004
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002005typedef MetadataTest MetadataAsValueTest;
2006
2007TEST_F(MetadataAsValueTest, MDNode) {
2008 MDNode *N = MDNode::get(Context, None);
2009 auto *V = MetadataAsValue::get(Context, N);
2010 EXPECT_TRUE(V->getType()->isMetadataTy());
2011 EXPECT_EQ(N, V->getMetadata());
2012
2013 auto *V2 = MetadataAsValue::get(Context, N);
2014 EXPECT_EQ(V, V2);
2015}
2016
2017TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2018 MDNode *N = MDNode::get(Context, None);
2019 Metadata *Ops[] = {N};
2020 MDNode *N2 = MDNode::get(Context, Ops);
2021 auto *V = MetadataAsValue::get(Context, N2);
2022 EXPECT_TRUE(V->getType()->isMetadataTy());
2023 EXPECT_EQ(N2, V->getMetadata());
2024
2025 auto *V2 = MetadataAsValue::get(Context, N2);
2026 EXPECT_EQ(V, V2);
2027
2028 auto *V3 = MetadataAsValue::get(Context, N);
2029 EXPECT_TRUE(V3->getType()->isMetadataTy());
2030 EXPECT_NE(V, V3);
2031 EXPECT_EQ(N, V3->getMetadata());
2032}
2033
2034TEST_F(MetadataAsValueTest, MDNodeConstant) {
2035 auto *C = ConstantInt::getTrue(Context);
2036 auto *MD = ConstantAsMetadata::get(C);
2037 Metadata *Ops[] = {MD};
2038 auto *N = MDNode::get(Context, Ops);
2039
2040 auto *V = MetadataAsValue::get(Context, MD);
2041 EXPECT_TRUE(V->getType()->isMetadataTy());
2042 EXPECT_EQ(MD, V->getMetadata());
2043
2044 auto *V2 = MetadataAsValue::get(Context, N);
2045 EXPECT_EQ(MD, V2->getMetadata());
2046 EXPECT_EQ(V, V2);
2047}
2048
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002049typedef MetadataTest ValueAsMetadataTest;
2050
2051TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2052 Type *Ty = Type::getInt1PtrTy(Context);
2053 std::unique_ptr<GlobalVariable> GV0(
2054 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2055 auto *MD = ValueAsMetadata::get(GV0.get());
2056 EXPECT_TRUE(MD->getValue() == GV0.get());
2057 ASSERT_TRUE(GV0->use_empty());
2058
2059 std::unique_ptr<GlobalVariable> GV1(
2060 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2061 GV0->replaceAllUsesWith(GV1.get());
2062 EXPECT_TRUE(MD->getValue() == GV1.get());
2063}
2064
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002065TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2066 // Create a constant.
2067 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2068 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2069
2070 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002071 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002072
2073 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002074 Metadata *Ops1[] = {CI, CI, Temp.get()};
2075 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002076
2077 auto *N1 = MDTuple::get(Context, Ops1);
2078 auto *N2 = MDTuple::get(Context, Ops2);
2079 ASSERT_NE(N1, N2);
2080
2081 // Tell metadata that the constant is getting deleted.
2082 //
2083 // After this, N1 will be invalid, so don't touch it.
2084 ValueAsMetadata::handleDeletion(CI->getValue());
2085 EXPECT_EQ(nullptr, N2->getOperand(0));
2086 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002087 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002088
2089 // Clean up Temp for teardown.
2090 Temp->replaceAllUsesWith(nullptr);
2091}
2092
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002093typedef MetadataTest TrackingMDRefTest;
2094
2095TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2096 Type *Ty = Type::getInt1PtrTy(Context);
2097 std::unique_ptr<GlobalVariable> GV0(
2098 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2099 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2100 EXPECT_TRUE(MD->getValue() == GV0.get());
2101 ASSERT_TRUE(GV0->use_empty());
2102
2103 std::unique_ptr<GlobalVariable> GV1(
2104 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2105 GV0->replaceAllUsesWith(GV1.get());
2106 EXPECT_TRUE(MD->getValue() == GV1.get());
2107
2108 // Reset it, so we don't inadvertently test deletion.
2109 MD.reset();
2110}
2111
2112TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2113 Type *Ty = Type::getInt1PtrTy(Context);
2114 std::unique_ptr<GlobalVariable> GV(
2115 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2116 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2117 EXPECT_TRUE(MD->getValue() == GV.get());
2118 ASSERT_TRUE(GV->use_empty());
2119
2120 GV.reset();
2121 EXPECT_TRUE(!MD);
2122}
2123
Devang Patel0924b332009-07-30 00:03:41 +00002124TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002125 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002126 ConstantAsMetadata *C =
2127 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2128 ConstantAsMetadata *C2 =
2129 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002130
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002131 Metadata *const V = C;
2132 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002133 MDNode *n = MDNode::get(Context, V);
2134 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002135
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002136 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002137 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002138 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2139 NMD->addOperand(n);
2140 NMD->addOperand(n2);
2141
Chris Lattnerbe354a62009-08-23 04:47:35 +00002142 std::string Str;
2143 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002144 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002145 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002146 oss.str().c_str());
2147}
Nick Lewycky49f89192009-04-04 07:22:01 +00002148}