IR: Make Metadata::print() reliable and useful
[oota-llvm.git] / unittests / IR / MetadataTest.cpp
1 //===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/DebugInfo.h"
13 #include "llvm/IR/DebugInfoMetadata.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Metadata.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/Type.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "gtest/gtest.h"
22 using namespace llvm;
23
24 namespace {
25
26 TEST(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
34 TEST(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
42 TEST(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
51 TEST(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
63 class MetadataTest : public testing::Test {
64 protected:
65   LLVMContext Context;
66   MDNode *getNode() { return MDNode::get(Context, None); }
67   MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
68   MDNode *getNode(Metadata *MD1, Metadata *MD2) {
69     Metadata *MDs[] = {MD1, MD2};
70     return MDNode::get(Context, MDs);
71   }
72 };
73 typedef MetadataTest MDStringTest;
74
75 // Test that construction of MDString with different value produces different
76 // MDString objects, even with the same string pointer and nulls in the string.
77 TEST_F(MDStringTest, CreateDifferent) {
78   char x[3] = { 'f', 0, 'A' };
79   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
80   x[2] = 'B';
81   MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
82   EXPECT_NE(s1, s2);
83 }
84
85 // Test that creation of MDStrings with the same string contents produces the
86 // same MDString object, even with different pointers.
87 TEST_F(MDStringTest, CreateSame) {
88   char x[4] = { 'a', 'b', 'c', 'X' };
89   char y[4] = { 'a', 'b', 'c', 'Y' };
90
91   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
92   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
93   EXPECT_EQ(s1, s2);
94 }
95
96 // Test that MDString prints out the string we fed it.
97 TEST_F(MDStringTest, PrintingSimple) {
98   char *str = new char[13];
99   strncpy(str, "testing 1 2 3", 13);
100   MDString *s = MDString::get(Context, StringRef(str, 13));
101   strncpy(str, "aaaaaaaaaaaaa", 13);
102   delete[] str;
103
104   std::string Str;
105   raw_string_ostream oss(Str);
106   s->print(oss);
107   EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
108 }
109
110 // Test printing of MDString with non-printable characters.
111 TEST_F(MDStringTest, PrintingComplex) {
112   char str[5] = {0, '\n', '"', '\\', (char)-1};
113   MDString *s = MDString::get(Context, StringRef(str+0, 5));
114   std::string Str;
115   raw_string_ostream oss(Str);
116   s->print(oss);
117   EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
118 }
119
120 typedef MetadataTest MDNodeTest;
121
122 // Test the two constructors, and containing other Constants.
123 TEST_F(MDNodeTest, Simple) {
124   char x[3] = { 'a', 'b', 'c' };
125   char y[3] = { '1', '2', '3' };
126
127   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
128   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
129   ConstantAsMetadata *CI = ConstantAsMetadata::get(
130       ConstantInt::get(getGlobalContext(), APInt(8, 0)));
131
132   std::vector<Metadata *> V;
133   V.push_back(s1);
134   V.push_back(CI);
135   V.push_back(s2);
136
137   MDNode *n1 = MDNode::get(Context, V);
138   Metadata *const c1 = n1;
139   MDNode *n2 = MDNode::get(Context, c1);
140   Metadata *const c2 = n2;
141   MDNode *n3 = MDNode::get(Context, V);
142   MDNode *n4 = MDNode::getIfExists(Context, V);
143   MDNode *n5 = MDNode::getIfExists(Context, c1);
144   MDNode *n6 = MDNode::getIfExists(Context, c2);
145   EXPECT_NE(n1, n2);
146   EXPECT_EQ(n1, n3);
147   EXPECT_EQ(n4, n1);
148   EXPECT_EQ(n5, n2);
149   EXPECT_EQ(n6, (Metadata *)nullptr);
150
151   EXPECT_EQ(3u, n1->getNumOperands());
152   EXPECT_EQ(s1, n1->getOperand(0));
153   EXPECT_EQ(CI, n1->getOperand(1));
154   EXPECT_EQ(s2, n1->getOperand(2));
155
156   EXPECT_EQ(1u, n2->getNumOperands());
157   EXPECT_EQ(n1, n2->getOperand(0));
158 }
159
160 TEST_F(MDNodeTest, Delete) {
161   Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
162   Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
163
164   Metadata *const V = LocalAsMetadata::get(I);
165   MDNode *n = MDNode::get(Context, V);
166   TrackingMDRef wvh(n);
167
168   EXPECT_EQ(n, wvh);
169
170   delete I;
171 }
172
173 TEST_F(MDNodeTest, SelfReference) {
174   // !0 = !{!0}
175   // !1 = !{!0}
176   {
177     auto Temp = MDNode::getTemporary(Context, None);
178     Metadata *Args[] = {Temp.get()};
179     MDNode *Self = MDNode::get(Context, Args);
180     Self->replaceOperandWith(0, Self);
181     ASSERT_EQ(Self, Self->getOperand(0));
182
183     // Self-references should be distinct, so MDNode::get() should grab a
184     // uniqued node that references Self, not Self.
185     Args[0] = Self;
186     MDNode *Ref1 = MDNode::get(Context, Args);
187     MDNode *Ref2 = MDNode::get(Context, Args);
188     EXPECT_NE(Self, Ref1);
189     EXPECT_EQ(Ref1, Ref2);
190   }
191
192   // !0 = !{!0, !{}}
193   // !1 = !{!0, !{}}
194   {
195     auto Temp = MDNode::getTemporary(Context, None);
196     Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
197     MDNode *Self = MDNode::get(Context, Args);
198     Self->replaceOperandWith(0, Self);
199     ASSERT_EQ(Self, Self->getOperand(0));
200
201     // Self-references should be distinct, so MDNode::get() should grab a
202     // uniqued node that references Self, not Self itself.
203     Args[0] = Self;
204     MDNode *Ref1 = MDNode::get(Context, Args);
205     MDNode *Ref2 = MDNode::get(Context, Args);
206     EXPECT_NE(Self, Ref1);
207     EXPECT_EQ(Ref1, Ref2);
208   }
209 }
210
211 TEST_F(MDNodeTest, Print) {
212   Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
213   MDString *S = MDString::get(Context, "foo");
214   MDNode *N0 = getNode();
215   MDNode *N1 = getNode(N0);
216   MDNode *N2 = getNode(N0, N1);
217
218   Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
219   MDNode *N = MDNode::get(Context, Args);
220
221   std::string Expected;
222   {
223     raw_string_ostream OS(Expected);
224     OS << "<" << (void *)N << "> = !{";
225     C->printAsOperand(OS);
226     OS << ", ";
227     S->printAsOperand(OS);
228     OS << ", null";
229     MDNode *Nodes[] = {N0, N1, N2};
230     for (auto *Node : Nodes)
231       OS << ", <" << (void *)Node << ">";
232     OS << "}";
233   }
234
235   std::string Actual;
236   {
237     raw_string_ostream OS(Actual);
238     N->print(OS);
239   }
240
241   EXPECT_EQ(Expected, Actual);
242 }
243
244 #define EXPECT_PRINTER_EQ(EXPECTED, PRINT)                                     \
245   do {                                                                         \
246     std::string Actual_;                                                       \
247     raw_string_ostream OS(Actual_);                                            \
248     PRINT;                                                                     \
249     OS.flush();                                                                \
250     std::string Expected_(EXPECTED);                                           \
251     EXPECT_EQ(Expected_, Actual_);                                             \
252   } while (false)
253
254 TEST_F(MDNodeTest, PrintFromModule) {
255   Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
256   MDString *S = MDString::get(Context, "foo");
257   MDNode *N0 = getNode();
258   MDNode *N1 = getNode(N0);
259   MDNode *N2 = getNode(N0, N1);
260
261   Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
262   MDNode *N = MDNode::get(Context, Args);
263   Module M("test", Context);
264   NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
265   NMD->addOperand(N);
266
267   std::string Expected;
268   {
269     raw_string_ostream OS(Expected);
270     OS << "!0 = !{";
271     C->printAsOperand(OS);
272     OS << ", ";
273     S->printAsOperand(OS);
274     OS << ", null, !1, !2, !3}";
275   }
276
277   EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
278 }
279
280 TEST_F(MDNodeTest, PrintFromFunction) {
281   Module M("test", Context);
282   auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
283   auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
284   auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
285   auto *BB0 = BasicBlock::Create(Context, "entry", F0);
286   auto *BB1 = BasicBlock::Create(Context, "entry", F1);
287   auto *R0 = ReturnInst::Create(Context, BB0);
288   auto *R1 = ReturnInst::Create(Context, BB1);
289   auto *N0 = MDNode::getDistinct(Context, None);
290   auto *N1 = MDNode::getDistinct(Context, None);
291   R0->setMetadata("md", N0);
292   R1->setMetadata("md", N1);
293
294   EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
295   EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
296 }
297
298 TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
299   Module M("test", Context);
300
301   auto *Intrinsic =
302       Function::Create(FunctionType::get(Type::getVoidTy(Context),
303                                          Type::getMetadataTy(Context), false),
304                        GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
305
306   auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
307   auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
308   auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
309   auto *BB0 = BasicBlock::Create(Context, "entry", F0);
310   auto *BB1 = BasicBlock::Create(Context, "entry", F1);
311   auto *N0 = MDNode::getDistinct(Context, None);
312   auto *N1 = MDNode::getDistinct(Context, None);
313   auto *MAV0 = MetadataAsValue::get(Context, N0);
314   auto *MAV1 = MetadataAsValue::get(Context, N1);
315   CallInst::Create(Intrinsic, MAV0, "", BB0);
316   CallInst::Create(Intrinsic, MAV1, "", BB1);
317
318   EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
319   EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
320   EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
321   EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
322   EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
323   EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
324 }
325 #undef EXPECT_PRINTER_EQ
326
327 TEST_F(MDNodeTest, NullOperand) {
328   // metadata !{}
329   MDNode *Empty = MDNode::get(Context, None);
330
331   // metadata !{metadata !{}}
332   Metadata *Ops[] = {Empty};
333   MDNode *N = MDNode::get(Context, Ops);
334   ASSERT_EQ(Empty, N->getOperand(0));
335
336   // metadata !{metadata !{}} => metadata !{null}
337   N->replaceOperandWith(0, nullptr);
338   ASSERT_EQ(nullptr, N->getOperand(0));
339
340   // metadata !{null}
341   Ops[0] = nullptr;
342   MDNode *NullOp = MDNode::get(Context, Ops);
343   ASSERT_EQ(nullptr, NullOp->getOperand(0));
344   EXPECT_EQ(N, NullOp);
345 }
346
347 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
348   // !{}
349   MDNode *Empty = MDNode::get(Context, None);
350   ASSERT_TRUE(Empty->isResolved());
351   EXPECT_FALSE(Empty->isDistinct());
352
353   // !{!{}}
354   Metadata *Wrapped1Ops[] = {Empty};
355   MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
356   ASSERT_EQ(Empty, Wrapped1->getOperand(0));
357   ASSERT_TRUE(Wrapped1->isResolved());
358   EXPECT_FALSE(Wrapped1->isDistinct());
359
360   // !{!{!{}}}
361   Metadata *Wrapped2Ops[] = {Wrapped1};
362   MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
363   ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
364   ASSERT_TRUE(Wrapped2->isResolved());
365   EXPECT_FALSE(Wrapped2->isDistinct());
366
367   // !{!{!{}}} => !{!{}}
368   Wrapped2->replaceOperandWith(0, Empty);
369   ASSERT_EQ(Empty, Wrapped2->getOperand(0));
370   EXPECT_TRUE(Wrapped2->isDistinct());
371   EXPECT_FALSE(Wrapped1->isDistinct());
372 }
373
374 TEST_F(MDNodeTest, getDistinct) {
375   // !{}
376   MDNode *Empty = MDNode::get(Context, None);
377   ASSERT_TRUE(Empty->isResolved());
378   ASSERT_FALSE(Empty->isDistinct());
379   ASSERT_EQ(Empty, MDNode::get(Context, None));
380
381   // distinct !{}
382   MDNode *Distinct1 = MDNode::getDistinct(Context, None);
383   MDNode *Distinct2 = MDNode::getDistinct(Context, None);
384   EXPECT_TRUE(Distinct1->isResolved());
385   EXPECT_TRUE(Distinct2->isDistinct());
386   EXPECT_NE(Empty, Distinct1);
387   EXPECT_NE(Empty, Distinct2);
388   EXPECT_NE(Distinct1, Distinct2);
389
390   // !{}
391   ASSERT_EQ(Empty, MDNode::get(Context, None));
392 }
393
394 TEST_F(MDNodeTest, isUniqued) {
395   MDNode *U = MDTuple::get(Context, None);
396   MDNode *D = MDTuple::getDistinct(Context, None);
397   auto T = MDTuple::getTemporary(Context, None);
398   EXPECT_TRUE(U->isUniqued());
399   EXPECT_FALSE(D->isUniqued());
400   EXPECT_FALSE(T->isUniqued());
401 }
402
403 TEST_F(MDNodeTest, isDistinct) {
404   MDNode *U = MDTuple::get(Context, None);
405   MDNode *D = MDTuple::getDistinct(Context, None);
406   auto T = MDTuple::getTemporary(Context, None);
407   EXPECT_FALSE(U->isDistinct());
408   EXPECT_TRUE(D->isDistinct());
409   EXPECT_FALSE(T->isDistinct());
410 }
411
412 TEST_F(MDNodeTest, isTemporary) {
413   MDNode *U = MDTuple::get(Context, None);
414   MDNode *D = MDTuple::getDistinct(Context, None);
415   auto T = MDTuple::getTemporary(Context, None);
416   EXPECT_FALSE(U->isTemporary());
417   EXPECT_FALSE(D->isTemporary());
418   EXPECT_TRUE(T->isTemporary());
419 }
420
421 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
422   // temporary !{}
423   auto Temp = MDTuple::getTemporary(Context, None);
424   ASSERT_FALSE(Temp->isResolved());
425
426   // distinct !{temporary !{}}
427   Metadata *Ops[] = {Temp.get()};
428   MDNode *Distinct = MDNode::getDistinct(Context, Ops);
429   EXPECT_TRUE(Distinct->isResolved());
430   EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
431
432   // temporary !{} => !{}
433   MDNode *Empty = MDNode::get(Context, None);
434   Temp->replaceAllUsesWith(Empty);
435   EXPECT_EQ(Empty, Distinct->getOperand(0));
436 }
437
438 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
439   // !0 = !{}
440   MDNode *N0 = MDNode::get(Context, None);
441
442   // !1 = !{!3, null}
443   auto Temp3 = MDTuple::getTemporary(Context, None);
444   Metadata *Ops1[] = {Temp3.get(), nullptr};
445   MDNode *N1 = MDNode::get(Context, Ops1);
446
447   // !2 = !{!3, !0}
448   Metadata *Ops2[] = {Temp3.get(), N0};
449   MDNode *N2 = MDNode::get(Context, Ops2);
450
451   // !3 = !{!2}
452   Metadata *Ops3[] = {N2};
453   MDNode *N3 = MDNode::get(Context, Ops3);
454   Temp3->replaceAllUsesWith(N3);
455
456   // !4 = !{!1}
457   Metadata *Ops4[] = {N1};
458   MDNode *N4 = MDNode::get(Context, Ops4);
459
460   // Confirm that the cycle prevented RAUW from getting dropped.
461   EXPECT_TRUE(N0->isResolved());
462   EXPECT_FALSE(N1->isResolved());
463   EXPECT_FALSE(N2->isResolved());
464   EXPECT_FALSE(N3->isResolved());
465   EXPECT_FALSE(N4->isResolved());
466
467   // Create a couple of distinct nodes to observe what's going on.
468   //
469   // !5 = distinct !{!2}
470   // !6 = distinct !{!3}
471   Metadata *Ops5[] = {N2};
472   MDNode *N5 = MDNode::getDistinct(Context, Ops5);
473   Metadata *Ops6[] = {N3};
474   MDNode *N6 = MDNode::getDistinct(Context, Ops6);
475
476   // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
477   // This will ripple up, with !3 colliding with !4, and RAUWing.  Since !2
478   // references !3, this can cause a re-entry of handleChangedOperand() when !3
479   // is not ready for it.
480   //
481   // !2->replaceOperandWith(1, nullptr)
482   // !2: !{!3, !0} => !{!3, null}
483   // !2->replaceAllUsesWith(!1)
484   // !3: !{!2] => !{!1}
485   // !3->replaceAllUsesWith(!4)
486   N2->replaceOperandWith(1, nullptr);
487
488   // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
489   // under us.  Just check that the other nodes are sane.
490   //
491   // !1 = !{!4, null}
492   // !4 = !{!1}
493   // !5 = distinct !{!1}
494   // !6 = distinct !{!4}
495   EXPECT_EQ(N4, N1->getOperand(0));
496   EXPECT_EQ(N1, N4->getOperand(0));
497   EXPECT_EQ(N1, N5->getOperand(0));
498   EXPECT_EQ(N4, N6->getOperand(0));
499 }
500
501 TEST_F(MDNodeTest, replaceResolvedOperand) {
502   // Check code for replacing one resolved operand with another.  If doing this
503   // directly (via replaceOperandWith()) becomes illegal, change the operand to
504   // a global value that gets RAUW'ed.
505   //
506   // Use a temporary node to keep N from being resolved.
507   auto Temp = MDTuple::getTemporary(Context, None);
508   Metadata *Ops[] = {nullptr, Temp.get()};
509
510   MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
511   MDNode *N = MDTuple::get(Context, Ops);
512   EXPECT_EQ(nullptr, N->getOperand(0));
513   ASSERT_FALSE(N->isResolved());
514
515   // Check code for replacing resolved nodes.
516   N->replaceOperandWith(0, Empty);
517   EXPECT_EQ(Empty, N->getOperand(0));
518
519   // Check code for adding another unresolved operand.
520   N->replaceOperandWith(0, Temp.get());
521   EXPECT_EQ(Temp.get(), N->getOperand(0));
522
523   // Remove the references to Temp; required for teardown.
524   Temp->replaceAllUsesWith(nullptr);
525 }
526
527 TEST_F(MDNodeTest, replaceWithUniqued) {
528   auto *Empty = MDTuple::get(Context, None);
529   MDTuple *FirstUniqued;
530   {
531     Metadata *Ops[] = {Empty};
532     auto Temp = MDTuple::getTemporary(Context, Ops);
533     EXPECT_TRUE(Temp->isTemporary());
534
535     // Don't expect a collision.
536     auto *Current = Temp.get();
537     FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
538     EXPECT_TRUE(FirstUniqued->isUniqued());
539     EXPECT_TRUE(FirstUniqued->isResolved());
540     EXPECT_EQ(Current, FirstUniqued);
541   }
542   {
543     Metadata *Ops[] = {Empty};
544     auto Temp = MDTuple::getTemporary(Context, Ops);
545     EXPECT_TRUE(Temp->isTemporary());
546
547     // Should collide with Uniqued above this time.
548     auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
549     EXPECT_TRUE(Uniqued->isUniqued());
550     EXPECT_TRUE(Uniqued->isResolved());
551     EXPECT_EQ(FirstUniqued, Uniqued);
552   }
553   {
554     auto Unresolved = MDTuple::getTemporary(Context, None);
555     Metadata *Ops[] = {Unresolved.get()};
556     auto Temp = MDTuple::getTemporary(Context, Ops);
557     EXPECT_TRUE(Temp->isTemporary());
558
559     // Shouldn't be resolved.
560     auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
561     EXPECT_TRUE(Uniqued->isUniqued());
562     EXPECT_FALSE(Uniqued->isResolved());
563
564     // Should be a different node.
565     EXPECT_NE(FirstUniqued, Uniqued);
566
567     // Should resolve when we update its node (note: be careful to avoid a
568     // collision with any other nodes above).
569     Uniqued->replaceOperandWith(0, nullptr);
570     EXPECT_TRUE(Uniqued->isResolved());
571   }
572 }
573
574 TEST_F(MDNodeTest, replaceWithDistinct) {
575   {
576     auto *Empty = MDTuple::get(Context, None);
577     Metadata *Ops[] = {Empty};
578     auto Temp = MDTuple::getTemporary(Context, Ops);
579     EXPECT_TRUE(Temp->isTemporary());
580
581     // Don't expect a collision.
582     auto *Current = Temp.get();
583     auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
584     EXPECT_TRUE(Distinct->isDistinct());
585     EXPECT_TRUE(Distinct->isResolved());
586     EXPECT_EQ(Current, Distinct);
587   }
588   {
589     auto Unresolved = MDTuple::getTemporary(Context, None);
590     Metadata *Ops[] = {Unresolved.get()};
591     auto Temp = MDTuple::getTemporary(Context, Ops);
592     EXPECT_TRUE(Temp->isTemporary());
593
594     // Don't expect a collision.
595     auto *Current = Temp.get();
596     auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
597     EXPECT_TRUE(Distinct->isDistinct());
598     EXPECT_TRUE(Distinct->isResolved());
599     EXPECT_EQ(Current, Distinct);
600
601     // Cleanup; required for teardown.
602     Unresolved->replaceAllUsesWith(nullptr);
603   }
604 }
605
606 TEST_F(MDNodeTest, replaceWithPermanent) {
607   Metadata *Ops[] = {nullptr};
608   auto Temp = MDTuple::getTemporary(Context, Ops);
609   auto *T = Temp.get();
610
611   // U is a normal, uniqued node that references T.
612   auto *U = MDTuple::get(Context, T);
613   EXPECT_TRUE(U->isUniqued());
614
615   // Make Temp self-referencing.
616   Temp->replaceOperandWith(0, T);
617
618   // Try to uniquify Temp.  This should, despite the name in the API, give a
619   // 'distinct' node, since self-references aren't allowed to be uniqued.
620   //
621   // Since it's distinct, N should have the same address as when it was a
622   // temporary (i.e., be equal to T not U).
623   auto *N = MDNode::replaceWithPermanent(std::move(Temp));
624   EXPECT_EQ(N, T);
625   EXPECT_TRUE(N->isDistinct());
626
627   // U should be the canonical unique node with N as the argument.
628   EXPECT_EQ(U, MDTuple::get(Context, N));
629   EXPECT_TRUE(U->isUniqued());
630
631   // This temporary should collide with U when replaced, but it should still be
632   // uniqued.
633   EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
634   EXPECT_TRUE(U->isUniqued());
635
636   // This temporary should become a new uniqued node.
637   auto Temp2 = MDTuple::getTemporary(Context, U);
638   auto *V = Temp2.get();
639   EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
640   EXPECT_TRUE(V->isUniqued());
641   EXPECT_EQ(U, V->getOperand(0));
642 }
643
644 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
645   TrackingMDRef Ref;
646   EXPECT_EQ(nullptr, Ref.get());
647   {
648     auto Temp = MDTuple::getTemporary(Context, None);
649     Ref.reset(Temp.get());
650     EXPECT_EQ(Temp.get(), Ref.get());
651   }
652   EXPECT_EQ(nullptr, Ref.get());
653 }
654
655 typedef MetadataTest MDLocationTest;
656
657 TEST_F(MDLocationTest, Overflow) {
658   MDNode *N = MDNode::get(Context, None);
659   {
660     MDLocation *L = MDLocation::get(Context, 2, 7, N);
661     EXPECT_EQ(2u, L->getLine());
662     EXPECT_EQ(7u, L->getColumn());
663   }
664   unsigned U16 = 1u << 16;
665   {
666     MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
667     EXPECT_EQ(UINT32_MAX, L->getLine());
668     EXPECT_EQ(U16 - 1, L->getColumn());
669   }
670   {
671     MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
672     EXPECT_EQ(UINT32_MAX, L->getLine());
673     EXPECT_EQ(0u, L->getColumn());
674   }
675   {
676     MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
677     EXPECT_EQ(UINT32_MAX, L->getLine());
678     EXPECT_EQ(0u, L->getColumn());
679   }
680 }
681
682 TEST_F(MDLocationTest, getDistinct) {
683   MDNode *N = MDNode::get(Context, None);
684   MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
685   EXPECT_TRUE(L0->isDistinct());
686   MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
687   EXPECT_FALSE(L1->isDistinct());
688   EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
689 }
690
691 TEST_F(MDLocationTest, getTemporary) {
692   MDNode *N = MDNode::get(Context, None);
693   auto L = MDLocation::getTemporary(Context, 2, 7, N);
694   EXPECT_TRUE(L->isTemporary());
695   EXPECT_FALSE(L->isResolved());
696 }
697
698 typedef MetadataTest GenericDebugNodeTest;
699
700 TEST_F(GenericDebugNodeTest, get) {
701   StringRef Header = "header";
702   auto *Empty = MDNode::get(Context, None);
703   Metadata *Ops1[] = {Empty};
704   auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
705   EXPECT_EQ(15u, N->getTag());
706   EXPECT_EQ(2u, N->getNumOperands());
707   EXPECT_EQ(Header, N->getHeader());
708   EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
709   EXPECT_EQ(1u, N->getNumDwarfOperands());
710   EXPECT_EQ(Empty, N->getDwarfOperand(0));
711   EXPECT_EQ(Empty, N->getOperand(1));
712   ASSERT_TRUE(N->isUniqued());
713
714   EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
715
716   N->replaceOperandWith(1, nullptr);
717   EXPECT_EQ(15u, N->getTag());
718   EXPECT_EQ(Header, N->getHeader());
719   EXPECT_EQ(nullptr, N->getDwarfOperand(0));
720   ASSERT_TRUE(N->isUniqued());
721
722   Metadata *Ops2[] = {nullptr};
723   EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
724
725   N->replaceDwarfOperandWith(0, Empty);
726   EXPECT_EQ(15u, N->getTag());
727   EXPECT_EQ(Header, N->getHeader());
728   EXPECT_EQ(Empty, N->getDwarfOperand(0));
729   ASSERT_TRUE(N->isUniqued());
730   EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
731
732   TempGenericDebugNode Temp = N->clone();
733   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
734 }
735
736 TEST_F(GenericDebugNodeTest, getEmptyHeader) {
737   // Canonicalize !"" to null.
738   auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
739   EXPECT_EQ(StringRef(), N->getHeader());
740   EXPECT_EQ(nullptr, N->getOperand(0));
741 }
742
743 typedef MetadataTest MDSubrangeTest;
744
745 TEST_F(MDSubrangeTest, get) {
746   auto *N = MDSubrange::get(Context, 5, 7);
747   EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
748   EXPECT_EQ(5, N->getCount());
749   EXPECT_EQ(7, N->getLo());
750   EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
751   EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
752
753   TempMDSubrange Temp = N->clone();
754   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
755 }
756
757 TEST_F(MDSubrangeTest, getEmptyArray) {
758   auto *N = MDSubrange::get(Context, -1, 0);
759   EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
760   EXPECT_EQ(-1, N->getCount());
761   EXPECT_EQ(0, N->getLo());
762   EXPECT_EQ(N, MDSubrange::get(Context, -1, 0));
763 }
764
765 typedef MetadataTest MDEnumeratorTest;
766
767 TEST_F(MDEnumeratorTest, get) {
768   auto *N = MDEnumerator::get(Context, 7, "name");
769   EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
770   EXPECT_EQ(7, N->getValue());
771   EXPECT_EQ("name", N->getName());
772   EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
773
774   EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
775   EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
776
777   TempMDEnumerator Temp = N->clone();
778   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
779 }
780
781 typedef MetadataTest MDBasicTypeTest;
782
783 TEST_F(MDBasicTypeTest, get) {
784   auto *N =
785       MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
786   EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
787   EXPECT_EQ("special", N->getName());
788   EXPECT_EQ(33u, N->getSizeInBits());
789   EXPECT_EQ(26u, N->getAlignInBits());
790   EXPECT_EQ(7u, N->getEncoding());
791   EXPECT_EQ(0u, N->getLine());
792   EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
793                                 26, 7));
794
795   EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
796                                 "special", 33, 26, 7));
797   EXPECT_NE(N,
798             MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
799   EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
800                                 26, 7));
801   EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
802                                 25, 7));
803   EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
804                                 26, 6));
805
806   TempMDBasicType Temp = N->clone();
807   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
808 }
809
810 TEST_F(MDBasicTypeTest, getWithLargeValues) {
811   auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
812                              UINT64_MAX, UINT64_MAX - 1, 7);
813   EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
814   EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
815 }
816
817 TEST_F(MDBasicTypeTest, getUnspecified) {
818   auto *N =
819       MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
820   EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
821   EXPECT_EQ("unspecified", N->getName());
822   EXPECT_EQ(0u, N->getSizeInBits());
823   EXPECT_EQ(0u, N->getAlignInBits());
824   EXPECT_EQ(0u, N->getEncoding());
825   EXPECT_EQ(0u, N->getLine());
826 }
827
828 typedef MetadataTest MDTypeTest;
829
830 TEST_F(MDTypeTest, clone) {
831   // Check that MDType has a specialized clone that returns TempMDType.
832   MDType *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
833                                dwarf::DW_ATE_signed);
834
835   TempMDType Temp = N->clone();
836   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
837 }
838
839 TEST_F(MDTypeTest, setFlags) {
840   // void (void)
841   Metadata *TypesOps[] = {nullptr};
842   Metadata *Types = MDTuple::get(Context, TypesOps);
843
844   MDType *D = MDSubroutineType::getDistinct(Context, 0u, Types);
845   EXPECT_EQ(0u, D->getFlags());
846   D->setFlags(DIDescriptor::FlagRValueReference);
847   EXPECT_EQ(DIDescriptor::FlagRValueReference, D->getFlags());
848   D->setFlags(0u);
849   EXPECT_EQ(0u, D->getFlags());
850
851   TempMDType T = MDSubroutineType::getTemporary(Context, 0u, Types);
852   EXPECT_EQ(0u, T->getFlags());
853   T->setFlags(DIDescriptor::FlagRValueReference);
854   EXPECT_EQ(DIDescriptor::FlagRValueReference, T->getFlags());
855   T->setFlags(0u);
856   EXPECT_EQ(0u, T->getFlags());
857 }
858
859 typedef MetadataTest MDDerivedTypeTest;
860
861 TEST_F(MDDerivedTypeTest, get) {
862   Metadata *File = MDTuple::getDistinct(Context, None);
863   Metadata *Scope = MDTuple::getDistinct(Context, None);
864   Metadata *BaseType = MDTuple::getDistinct(Context, None);
865   Metadata *ExtraData = MDTuple::getDistinct(Context, None);
866
867   auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
868                                File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
869   EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
870   EXPECT_EQ("something", N->getName());
871   EXPECT_EQ(File, N->getFile());
872   EXPECT_EQ(1u, N->getLine());
873   EXPECT_EQ(Scope, N->getScope());
874   EXPECT_EQ(BaseType, N->getBaseType());
875   EXPECT_EQ(2u, N->getSizeInBits());
876   EXPECT_EQ(3u, N->getAlignInBits());
877   EXPECT_EQ(4u, N->getOffsetInBits());
878   EXPECT_EQ(5u, N->getFlags());
879   EXPECT_EQ(ExtraData, N->getExtraData());
880   EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
881                                   "something", File, 1, Scope, BaseType, 2, 3,
882                                   4, 5, ExtraData));
883
884   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
885                                   "something", File, 1, Scope, BaseType, 2, 3,
886                                   4, 5, ExtraData));
887   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
888                                   File, 1, Scope, BaseType, 2, 3, 4, 5,
889                                   ExtraData));
890   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
891                                   "something", Scope, 1, Scope, BaseType, 2, 3,
892                                   4, 5, ExtraData));
893   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
894                                   "something", File, 2, Scope, BaseType, 2, 3,
895                                   4, 5, ExtraData));
896   EXPECT_NE(N,
897             MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
898                                File, 1, File, BaseType, 2, 3, 4, 5, ExtraData));
899   EXPECT_NE(N,
900             MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
901                                File, 1, Scope, File, 2, 3, 4, 5, ExtraData));
902   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
903                                   "something", File, 1, Scope, BaseType, 3, 3,
904                                   4, 5, ExtraData));
905   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
906                                   "something", File, 1, Scope, BaseType, 2, 2,
907                                   4, 5, ExtraData));
908   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
909                                   "something", File, 1, Scope, BaseType, 2, 3,
910                                   5, 5, ExtraData));
911   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
912                                   "something", File, 1, Scope, BaseType, 2, 3,
913                                   4, 4, ExtraData));
914   EXPECT_NE(N,
915             MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
916                                File, 1, Scope, BaseType, 2, 3, 4, 5, File));
917
918   TempMDDerivedType Temp = N->clone();
919   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
920 }
921
922 TEST_F(MDDerivedTypeTest, getWithLargeValues) {
923   Metadata *File = MDTuple::getDistinct(Context, None);
924   Metadata *Scope = MDTuple::getDistinct(Context, None);
925   Metadata *BaseType = MDTuple::getDistinct(Context, None);
926   Metadata *ExtraData = MDTuple::getDistinct(Context, None);
927
928   auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
929                                File, 1, Scope, BaseType, UINT64_MAX,
930                                UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
931   EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
932   EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
933   EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
934 }
935
936 typedef MetadataTest MDCompositeTypeTest;
937
938 TEST_F(MDCompositeTypeTest, get) {
939   unsigned Tag = dwarf::DW_TAG_structure_type;
940   StringRef Name = "some name";
941   Metadata *File = MDTuple::getDistinct(Context, None);
942   unsigned Line = 1;
943   Metadata *Scope = MDTuple::getDistinct(Context, None);
944   Metadata *BaseType = MDTuple::getDistinct(Context, None);
945   uint64_t SizeInBits = 2;
946   uint64_t AlignInBits = 3;
947   uint64_t OffsetInBits = 4;
948   unsigned Flags = 5;
949   Metadata *Elements = MDTuple::getDistinct(Context, None);
950   unsigned RuntimeLang = 6;
951   Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
952   Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
953   StringRef Identifier = "some id";
954
955   auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
956                                  BaseType, SizeInBits, AlignInBits,
957                                  OffsetInBits, Flags, Elements, RuntimeLang,
958                                  VTableHolder, TemplateParams, Identifier);
959   EXPECT_EQ(Tag, N->getTag());
960   EXPECT_EQ(Name, N->getName());
961   EXPECT_EQ(File, N->getFile());
962   EXPECT_EQ(Line, N->getLine());
963   EXPECT_EQ(Scope, N->getScope());
964   EXPECT_EQ(BaseType, N->getBaseType());
965   EXPECT_EQ(SizeInBits, N->getSizeInBits());
966   EXPECT_EQ(AlignInBits, N->getAlignInBits());
967   EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
968   EXPECT_EQ(Flags, N->getFlags());
969   EXPECT_EQ(Elements, N->getElements());
970   EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
971   EXPECT_EQ(VTableHolder, N->getVTableHolder());
972   EXPECT_EQ(TemplateParams, N->getTemplateParams());
973   EXPECT_EQ(Identifier, N->getIdentifier());
974
975   EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
976                                     BaseType, SizeInBits, AlignInBits,
977                                     OffsetInBits, Flags, Elements, RuntimeLang,
978                                     VTableHolder, TemplateParams, Identifier));
979
980   EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
981                                     BaseType, SizeInBits, AlignInBits,
982                                     OffsetInBits, Flags, Elements, RuntimeLang,
983                                     VTableHolder, TemplateParams, Identifier));
984   EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
985                                     BaseType, SizeInBits, AlignInBits,
986                                     OffsetInBits, Flags, Elements, RuntimeLang,
987                                     VTableHolder, TemplateParams, Identifier));
988   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, Scope, Line, Scope,
989                                     BaseType, SizeInBits, AlignInBits,
990                                     OffsetInBits, Flags, Elements, RuntimeLang,
991                                     VTableHolder, TemplateParams, Identifier));
992   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
993                                     BaseType, SizeInBits, AlignInBits,
994                                     OffsetInBits, Flags, Elements, RuntimeLang,
995                                     VTableHolder, TemplateParams, Identifier));
996   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, File,
997                                     BaseType, SizeInBits, AlignInBits,
998                                     OffsetInBits, Flags, Elements, RuntimeLang,
999                                     VTableHolder, TemplateParams, Identifier));
1000   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
1001                                     SizeInBits, AlignInBits, OffsetInBits,
1002                                     Flags, Elements, RuntimeLang, VTableHolder,
1003                                     TemplateParams, Identifier));
1004   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1005                                     BaseType, SizeInBits + 1, AlignInBits,
1006                                     OffsetInBits, Flags, Elements, RuntimeLang,
1007                                     VTableHolder, TemplateParams, Identifier));
1008   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1009                                     BaseType, SizeInBits, AlignInBits + 1,
1010                                     OffsetInBits, Flags, Elements, RuntimeLang,
1011                                     VTableHolder, TemplateParams, Identifier));
1012   EXPECT_NE(N, MDCompositeType::get(
1013                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1014                    AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1015                    VTableHolder, TemplateParams, Identifier));
1016   EXPECT_NE(N, MDCompositeType::get(
1017                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1018                    AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1019                    VTableHolder, TemplateParams, Identifier));
1020   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1021                                     BaseType, SizeInBits, AlignInBits,
1022                                     OffsetInBits, Flags, File, RuntimeLang,
1023                                     VTableHolder, TemplateParams, Identifier));
1024   EXPECT_NE(N, MDCompositeType::get(
1025                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1026                    AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1027                    VTableHolder, TemplateParams, Identifier));
1028   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1029                                     BaseType, SizeInBits, AlignInBits,
1030                                     OffsetInBits, Flags, Elements, RuntimeLang,
1031                                     File, TemplateParams, Identifier));
1032   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1033                                     BaseType, SizeInBits, AlignInBits,
1034                                     OffsetInBits, Flags, Elements, RuntimeLang,
1035                                     VTableHolder, File, Identifier));
1036   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1037                                     BaseType, SizeInBits, AlignInBits,
1038                                     OffsetInBits, Flags, Elements, RuntimeLang,
1039                                     VTableHolder, TemplateParams, "other"));
1040
1041   // Be sure that missing identifiers get null pointers.
1042   EXPECT_FALSE(MDCompositeType::get(
1043                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1044                    AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1045                    VTableHolder, TemplateParams, "")->getRawIdentifier());
1046   EXPECT_FALSE(MDCompositeType::get(
1047                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1048                    AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1049                    VTableHolder, TemplateParams)->getRawIdentifier());
1050
1051   TempMDCompositeType Temp = N->clone();
1052   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1053 }
1054
1055 TEST_F(MDCompositeTypeTest, getWithLargeValues) {
1056   unsigned Tag = dwarf::DW_TAG_structure_type;
1057   StringRef Name = "some name";
1058   Metadata *File = MDTuple::getDistinct(Context, None);
1059   unsigned Line = 1;
1060   Metadata *Scope = MDTuple::getDistinct(Context, None);
1061   Metadata *BaseType = MDTuple::getDistinct(Context, None);
1062   uint64_t SizeInBits = UINT64_MAX;
1063   uint64_t AlignInBits = UINT64_MAX - 1;
1064   uint64_t OffsetInBits = UINT64_MAX - 2;
1065   unsigned Flags = 5;
1066   Metadata *Elements = MDTuple::getDistinct(Context, None);
1067   unsigned RuntimeLang = 6;
1068   Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
1069   Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1070   StringRef Identifier = "some id";
1071
1072   auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1073                                  BaseType, SizeInBits, AlignInBits,
1074                                  OffsetInBits, Flags, Elements, RuntimeLang,
1075                                  VTableHolder, TemplateParams, Identifier);
1076   EXPECT_EQ(SizeInBits, N->getSizeInBits());
1077   EXPECT_EQ(AlignInBits, N->getAlignInBits());
1078   EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1079 }
1080
1081 TEST_F(MDCompositeTypeTest, replaceOperands) {
1082   unsigned Tag = dwarf::DW_TAG_structure_type;
1083   StringRef Name = "some name";
1084   Metadata *File = MDTuple::getDistinct(Context, None);
1085   unsigned Line = 1;
1086   Metadata *Scope = MDTuple::getDistinct(Context, None);
1087   Metadata *BaseType = MDTuple::getDistinct(Context, None);
1088   uint64_t SizeInBits = 2;
1089   uint64_t AlignInBits = 3;
1090   uint64_t OffsetInBits = 4;
1091   unsigned Flags = 5;
1092   unsigned RuntimeLang = 6;
1093   StringRef Identifier = "some id";
1094
1095   auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1096                                  BaseType, SizeInBits, AlignInBits,
1097                                  OffsetInBits, Flags, nullptr, RuntimeLang,
1098                                  nullptr, nullptr, Identifier);
1099
1100   auto *Elements = MDTuple::getDistinct(Context, None);
1101   EXPECT_EQ(nullptr, N->getElements());
1102   N->replaceElements(Elements);
1103   EXPECT_EQ(Elements, N->getElements());
1104   N->replaceElements(nullptr);
1105   EXPECT_EQ(nullptr, N->getElements());
1106
1107   auto *VTableHolder = MDTuple::getDistinct(Context, None);
1108   EXPECT_EQ(nullptr, N->getVTableHolder());
1109   N->replaceVTableHolder(VTableHolder);
1110   EXPECT_EQ(VTableHolder, N->getVTableHolder());
1111   N->replaceVTableHolder(nullptr);
1112   EXPECT_EQ(nullptr, N->getVTableHolder());
1113
1114   auto *TemplateParams = MDTuple::getDistinct(Context, None);
1115   EXPECT_EQ(nullptr, N->getTemplateParams());
1116   N->replaceTemplateParams(TemplateParams);
1117   EXPECT_EQ(TemplateParams, N->getTemplateParams());
1118   N->replaceTemplateParams(nullptr);
1119   EXPECT_EQ(nullptr, N->getTemplateParams());
1120 }
1121
1122 typedef MetadataTest MDSubroutineTypeTest;
1123
1124 TEST_F(MDSubroutineTypeTest, get) {
1125   unsigned Flags = 1;
1126   Metadata *TypeArray = MDTuple::getDistinct(Context, None);
1127
1128   auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
1129   EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1130   EXPECT_EQ(Flags, N->getFlags());
1131   EXPECT_EQ(TypeArray, N->getTypeArray());
1132   EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
1133
1134   EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
1135   EXPECT_NE(N, MDSubroutineType::get(Context, Flags,
1136                                      MDTuple::getDistinct(Context, None)));
1137
1138   TempMDSubroutineType Temp = N->clone();
1139   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1140
1141   // Test always-empty operands.
1142   EXPECT_EQ(nullptr, N->getScope());
1143   EXPECT_EQ(nullptr, N->getFile());
1144   EXPECT_EQ("", N->getName());
1145   EXPECT_EQ(nullptr, N->getBaseType());
1146   EXPECT_EQ(nullptr, N->getVTableHolder());
1147   EXPECT_EQ(nullptr, N->getTemplateParams());
1148   EXPECT_EQ("", N->getIdentifier());
1149 }
1150
1151 typedef MetadataTest MDFileTest;
1152
1153 TEST_F(MDFileTest, get) {
1154   StringRef Filename = "file";
1155   StringRef Directory = "dir";
1156   auto *N = MDFile::get(Context, Filename, Directory);
1157
1158   EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1159   EXPECT_EQ(Filename, N->getFilename());
1160   EXPECT_EQ(Directory, N->getDirectory());
1161   EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
1162
1163   EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1164   EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
1165
1166   TempMDFile Temp = N->clone();
1167   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1168 }
1169
1170 TEST_F(MDFileTest, ScopeGetFile) {
1171   // Ensure that MDScope::getFile() returns itself.
1172   MDScope *N = MDFile::get(Context, "file", "dir");
1173   EXPECT_EQ(N, N->getFile());
1174 }
1175
1176 typedef MetadataTest MDCompileUnitTest;
1177
1178 TEST_F(MDCompileUnitTest, get) {
1179   unsigned SourceLanguage = 1;
1180   Metadata *File = MDTuple::getDistinct(Context, None);
1181   StringRef Producer = "some producer";
1182   bool IsOptimized = false;
1183   StringRef Flags = "flag after flag";
1184   unsigned RuntimeVersion = 2;
1185   StringRef SplitDebugFilename = "another/file";
1186   unsigned EmissionKind = 3;
1187   Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1188   Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1189   Metadata *Subprograms = MDTuple::getDistinct(Context, None);
1190   Metadata *GlobalVariables = MDTuple::getDistinct(Context, None);
1191   Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1192   auto *N = MDCompileUnit::get(
1193       Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1194       RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1195       RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1196
1197   EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1198   EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1199   EXPECT_EQ(File, N->getFile());
1200   EXPECT_EQ(Producer, N->getProducer());
1201   EXPECT_EQ(IsOptimized, N->isOptimized());
1202   EXPECT_EQ(Flags, N->getFlags());
1203   EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1204   EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1205   EXPECT_EQ(EmissionKind, N->getEmissionKind());
1206   EXPECT_EQ(EnumTypes, N->getEnumTypes());
1207   EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
1208   EXPECT_EQ(Subprograms, N->getSubprograms());
1209   EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1210   EXPECT_EQ(ImportedEntities, N->getImportedEntities());
1211   EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1212                                   IsOptimized, Flags, RuntimeVersion,
1213                                   SplitDebugFilename, EmissionKind, EnumTypes,
1214                                   RetainedTypes, Subprograms, GlobalVariables,
1215                                   ImportedEntities));
1216
1217   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1218                                   IsOptimized, Flags, RuntimeVersion,
1219                                   SplitDebugFilename, EmissionKind, EnumTypes,
1220                                   RetainedTypes, Subprograms, GlobalVariables,
1221                                   ImportedEntities));
1222   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer,
1223                                   IsOptimized, Flags, RuntimeVersion,
1224                                   SplitDebugFilename, EmissionKind, EnumTypes,
1225                                   RetainedTypes, Subprograms, GlobalVariables,
1226                                   ImportedEntities));
1227   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1228                                   IsOptimized, Flags, RuntimeVersion,
1229                                   SplitDebugFilename, EmissionKind, EnumTypes,
1230                                   RetainedTypes, Subprograms, GlobalVariables,
1231                                   ImportedEntities));
1232   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1233                                   !IsOptimized, Flags, RuntimeVersion,
1234                                   SplitDebugFilename, EmissionKind, EnumTypes,
1235                                   RetainedTypes, Subprograms, GlobalVariables,
1236                                   ImportedEntities));
1237   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1238                                   IsOptimized, "other", RuntimeVersion,
1239                                   SplitDebugFilename, EmissionKind, EnumTypes,
1240                                   RetainedTypes, Subprograms, GlobalVariables,
1241                                   ImportedEntities));
1242   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1243                                   IsOptimized, Flags, RuntimeVersion + 1,
1244                                   SplitDebugFilename, EmissionKind, EnumTypes,
1245                                   RetainedTypes, Subprograms, GlobalVariables,
1246                                   ImportedEntities));
1247   EXPECT_NE(N,
1248             MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1249                                IsOptimized, Flags, RuntimeVersion, "other",
1250                                EmissionKind, EnumTypes, RetainedTypes,
1251                                Subprograms, GlobalVariables, ImportedEntities));
1252   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1253                                   IsOptimized, Flags, RuntimeVersion,
1254                                   SplitDebugFilename, EmissionKind + 1,
1255                                   EnumTypes, RetainedTypes, Subprograms,
1256                                   GlobalVariables, ImportedEntities));
1257   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1258                                   IsOptimized, Flags, RuntimeVersion,
1259                                   SplitDebugFilename, EmissionKind, File,
1260                                   RetainedTypes, Subprograms, GlobalVariables,
1261                                   ImportedEntities));
1262   EXPECT_NE(N, MDCompileUnit::get(
1263                    Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1264                    RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1265                    File, Subprograms, GlobalVariables, ImportedEntities));
1266   EXPECT_NE(N, MDCompileUnit::get(
1267                    Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1268                    RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1269                    RetainedTypes, File, GlobalVariables, ImportedEntities));
1270   EXPECT_NE(N, MDCompileUnit::get(
1271                    Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1272                    RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1273                    RetainedTypes, Subprograms, File, ImportedEntities));
1274   EXPECT_NE(N, MDCompileUnit::get(
1275                    Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1276                    RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1277                    RetainedTypes, Subprograms, GlobalVariables, File));
1278
1279   TempMDCompileUnit Temp = N->clone();
1280   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1281 }
1282
1283 TEST_F(MDCompileUnitTest, replaceArrays) {
1284   unsigned SourceLanguage = 1;
1285   Metadata *File = MDTuple::getDistinct(Context, None);
1286   StringRef Producer = "some producer";
1287   bool IsOptimized = false;
1288   StringRef Flags = "flag after flag";
1289   unsigned RuntimeVersion = 2;
1290   StringRef SplitDebugFilename = "another/file";
1291   unsigned EmissionKind = 3;
1292   Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1293   Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1294   Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1295   auto *N = MDCompileUnit::get(
1296       Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1297       RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1298       RetainedTypes, nullptr, nullptr, ImportedEntities);
1299
1300   auto *Subprograms = MDTuple::getDistinct(Context, None);
1301   EXPECT_EQ(nullptr, N->getSubprograms());
1302   N->replaceSubprograms(Subprograms);
1303   EXPECT_EQ(Subprograms, N->getSubprograms());
1304   N->replaceSubprograms(nullptr);
1305   EXPECT_EQ(nullptr, N->getSubprograms());
1306
1307   auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1308   EXPECT_EQ(nullptr, N->getGlobalVariables());
1309   N->replaceGlobalVariables(GlobalVariables);
1310   EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1311   N->replaceGlobalVariables(nullptr);
1312   EXPECT_EQ(nullptr, N->getGlobalVariables());
1313 }
1314
1315 typedef MetadataTest MDSubprogramTest;
1316
1317 TEST_F(MDSubprogramTest, get) {
1318   Metadata *Scope = MDTuple::getDistinct(Context, None);
1319   StringRef Name = "name";
1320   StringRef LinkageName = "linkage";
1321   Metadata *File = MDTuple::getDistinct(Context, None);
1322   unsigned Line = 2;
1323   Metadata *Type = MDTuple::getDistinct(Context, None);
1324   bool IsLocalToUnit = false;
1325   bool IsDefinition = true;
1326   unsigned ScopeLine = 3;
1327   Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1328   unsigned Virtuality = 4;
1329   unsigned VirtualIndex = 5;
1330   unsigned Flags = 6;
1331   bool IsOptimized = false;
1332   Metadata *Function = MDTuple::getDistinct(Context, None);
1333   Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1334   Metadata *Declaration = MDTuple::getDistinct(Context, None);
1335   Metadata *Variables = MDTuple::getDistinct(Context, None);
1336
1337   auto *N = MDSubprogram::get(
1338       Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1339       IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1340       IsOptimized, Function, TemplateParams, Declaration, Variables);
1341
1342   EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1343   EXPECT_EQ(Scope, N->getScope());
1344   EXPECT_EQ(Name, N->getName());
1345   EXPECT_EQ(LinkageName, N->getLinkageName());
1346   EXPECT_EQ(File, N->getFile());
1347   EXPECT_EQ(Line, N->getLine());
1348   EXPECT_EQ(Type, N->getType());
1349   EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1350   EXPECT_EQ(IsDefinition, N->isDefinition());
1351   EXPECT_EQ(ScopeLine, N->getScopeLine());
1352   EXPECT_EQ(ContainingType, N->getContainingType());
1353   EXPECT_EQ(Virtuality, N->getVirtuality());
1354   EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1355   EXPECT_EQ(Flags, N->getFlags());
1356   EXPECT_EQ(IsOptimized, N->isOptimized());
1357   EXPECT_EQ(Function, N->getFunction());
1358   EXPECT_EQ(TemplateParams, N->getTemplateParams());
1359   EXPECT_EQ(Declaration, N->getDeclaration());
1360   EXPECT_EQ(Variables, N->getVariables());
1361   EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1362                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1363                                  ContainingType, Virtuality, VirtualIndex,
1364                                  Flags, IsOptimized, Function, TemplateParams,
1365                                  Declaration, Variables));
1366
1367   EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1368                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1369                                  ContainingType, Virtuality, VirtualIndex,
1370                                  Flags, IsOptimized, Function, TemplateParams,
1371                                  Declaration, Variables));
1372   EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1373                                  Line, Type, IsLocalToUnit, IsDefinition,
1374                                  ScopeLine, ContainingType, Virtuality,
1375                                  VirtualIndex, Flags, IsOptimized, Function,
1376                                  TemplateParams, Declaration, Variables));
1377   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1378                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1379                                  ContainingType, Virtuality, VirtualIndex,
1380                                  Flags, IsOptimized, Function, TemplateParams,
1381                                  Declaration, Variables));
1382   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1383                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1384                                  ContainingType, Virtuality, VirtualIndex,
1385                                  Flags, IsOptimized, Function, TemplateParams,
1386                                  Declaration, Variables));
1387   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1388                                  Line + 1, Type, IsLocalToUnit, IsDefinition,
1389                                  ScopeLine, ContainingType, Virtuality,
1390                                  VirtualIndex, Flags, IsOptimized, Function,
1391                                  TemplateParams, Declaration, Variables));
1392   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1393                                  Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1394                                  ContainingType, Virtuality, VirtualIndex,
1395                                  Flags, IsOptimized, Function, TemplateParams,
1396                                  Declaration, Variables));
1397   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1398                                  Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1399                                  ContainingType, Virtuality, VirtualIndex,
1400                                  Flags, IsOptimized, Function, TemplateParams,
1401                                  Declaration, Variables));
1402   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1403                                  Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1404                                  ContainingType, Virtuality, VirtualIndex,
1405                                  Flags, IsOptimized, Function, TemplateParams,
1406                                  Declaration, Variables));
1407   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1408                                  Type, IsLocalToUnit, IsDefinition,
1409                                  ScopeLine + 1, ContainingType, Virtuality,
1410                                  VirtualIndex, Flags, IsOptimized, Function,
1411                                  TemplateParams, Declaration, Variables));
1412   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1413                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1414                                  Type, Virtuality, VirtualIndex, Flags,
1415                                  IsOptimized, Function, TemplateParams,
1416                                  Declaration, Variables));
1417   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1418                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1419                                  ContainingType, Virtuality + 1, VirtualIndex,
1420                                  Flags, IsOptimized, Function, TemplateParams,
1421                                  Declaration, Variables));
1422   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1423                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1424                                  ContainingType, Virtuality, VirtualIndex + 1,
1425                                  Flags, IsOptimized, Function, TemplateParams,
1426                                  Declaration, Variables));
1427   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1428                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1429                                  ContainingType, Virtuality, VirtualIndex,
1430                                  ~Flags, IsOptimized, Function, TemplateParams,
1431                                  Declaration, Variables));
1432   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1433                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1434                                  ContainingType, Virtuality, VirtualIndex,
1435                                  Flags, !IsOptimized, Function, TemplateParams,
1436                                  Declaration, Variables));
1437   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1438                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1439                                  ContainingType, Virtuality, VirtualIndex,
1440                                  Flags, IsOptimized, Type, TemplateParams,
1441                                  Declaration, Variables));
1442   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1443                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1444                                  ContainingType, Virtuality, VirtualIndex,
1445                                  Flags, IsOptimized, Function, Type,
1446                                  Declaration, Variables));
1447   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1448                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1449                                  ContainingType, Virtuality, VirtualIndex,
1450                                  Flags, IsOptimized, Function, TemplateParams,
1451                                  Type, Variables));
1452   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1453                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1454                                  ContainingType, Virtuality, VirtualIndex,
1455                                  Flags, IsOptimized, Function, TemplateParams,
1456                                  Declaration, Type));
1457
1458   TempMDSubprogram Temp = N->clone();
1459   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1460 }
1461
1462 TEST_F(MDSubprogramTest, replaceFunction) {
1463   Metadata *Scope = MDTuple::getDistinct(Context, None);
1464   StringRef Name = "name";
1465   StringRef LinkageName = "linkage";
1466   Metadata *File = MDTuple::getDistinct(Context, None);
1467   unsigned Line = 2;
1468   Metadata *Type = MDTuple::getDistinct(Context, None);
1469   bool IsLocalToUnit = false;
1470   bool IsDefinition = true;
1471   unsigned ScopeLine = 3;
1472   Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1473   unsigned Virtuality = 4;
1474   unsigned VirtualIndex = 5;
1475   unsigned Flags = 6;
1476   bool IsOptimized = false;
1477   Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1478   Metadata *Declaration = MDTuple::getDistinct(Context, None);
1479   Metadata *Variables = MDTuple::getDistinct(Context, None);
1480
1481   auto *N = MDSubprogram::get(
1482       Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1483       IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1484       IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1485
1486   EXPECT_EQ(nullptr, N->getFunction());
1487
1488   std::unique_ptr<Function> F(
1489       Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1490                        GlobalValue::ExternalLinkage));
1491   N->replaceFunction(F.get());
1492   EXPECT_EQ(ConstantAsMetadata::get(F.get()), N->getFunction());
1493
1494   N->replaceFunction(nullptr);
1495   EXPECT_EQ(nullptr, N->getFunction());
1496 }
1497
1498 typedef MetadataTest MDLexicalBlockTest;
1499
1500 TEST_F(MDLexicalBlockTest, get) {
1501   Metadata *Scope = MDTuple::getDistinct(Context, None);
1502   Metadata *File = MDTuple::getDistinct(Context, None);
1503   unsigned Line = 5;
1504   unsigned Column = 8;
1505
1506   auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1507
1508   EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1509   EXPECT_EQ(Scope, N->getScope());
1510   EXPECT_EQ(File, N->getFile());
1511   EXPECT_EQ(Line, N->getLine());
1512   EXPECT_EQ(Column, N->getColumn());
1513   EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1514
1515   EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1516   EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1517   EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1518   EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
1519
1520   TempMDLexicalBlock Temp = N->clone();
1521   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1522 }
1523
1524 typedef MetadataTest MDLexicalBlockFileTest;
1525
1526 TEST_F(MDLexicalBlockFileTest, get) {
1527   Metadata *Scope = MDTuple::getDistinct(Context, None);
1528   Metadata *File = MDTuple::getDistinct(Context, None);
1529   unsigned Discriminator = 5;
1530
1531   auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1532
1533   EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1534   EXPECT_EQ(Scope, N->getScope());
1535   EXPECT_EQ(File, N->getFile());
1536   EXPECT_EQ(Discriminator, N->getDiscriminator());
1537   EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1538
1539   EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1540   EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1541   EXPECT_NE(N,
1542             MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1543
1544   TempMDLexicalBlockFile Temp = N->clone();
1545   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1546 }
1547
1548 typedef MetadataTest MDNamespaceTest;
1549
1550 TEST_F(MDNamespaceTest, get) {
1551   Metadata *Scope = MDTuple::getDistinct(Context, None);
1552   Metadata *File = MDTuple::getDistinct(Context, None);
1553   StringRef Name = "namespace";
1554   unsigned Line = 5;
1555
1556   auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1557
1558   EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1559   EXPECT_EQ(Scope, N->getScope());
1560   EXPECT_EQ(File, N->getFile());
1561   EXPECT_EQ(Name, N->getName());
1562   EXPECT_EQ(Line, N->getLine());
1563   EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1564
1565   EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1566   EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1567   EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1568   EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
1569
1570   TempMDNamespace Temp = N->clone();
1571   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1572 }
1573
1574 typedef MetadataTest MDTemplateTypeParameterTest;
1575
1576 TEST_F(MDTemplateTypeParameterTest, get) {
1577   StringRef Name = "template";
1578   Metadata *Type = MDTuple::getDistinct(Context, None);
1579   Metadata *Other = MDTuple::getDistinct(Context, None);
1580
1581   auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
1582
1583   EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1584   EXPECT_EQ(Name, N->getName());
1585   EXPECT_EQ(Type, N->getType());
1586   EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type));
1587
1588   EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
1589   EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Name, Other));
1590
1591   TempMDTemplateTypeParameter Temp = N->clone();
1592   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1593 }
1594
1595 typedef MetadataTest MDTemplateValueParameterTest;
1596
1597 TEST_F(MDTemplateValueParameterTest, get) {
1598   unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1599   StringRef Name = "template";
1600   Metadata *Type = MDTuple::getDistinct(Context, None);
1601   Metadata *Value = MDTuple::getDistinct(Context, None);
1602   Metadata *Other = MDTuple::getDistinct(Context, None);
1603
1604   auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
1605   EXPECT_EQ(Tag, N->getTag());
1606   EXPECT_EQ(Name, N->getName());
1607   EXPECT_EQ(Type, N->getType());
1608   EXPECT_EQ(Value, N->getValue());
1609   EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value));
1610
1611   EXPECT_NE(N, MDTemplateValueParameter::get(
1612                    Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1613                    Type, Value));
1614   EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag,  "other", Type,
1615                                              Value));
1616   EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag,  Name, Other,
1617                                              Value));
1618   EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Other));
1619
1620   TempMDTemplateValueParameter Temp = N->clone();
1621   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1622 }
1623
1624 typedef MetadataTest MDGlobalVariableTest;
1625
1626 TEST_F(MDGlobalVariableTest, get) {
1627   Metadata *Scope = MDTuple::getDistinct(Context, None);
1628   StringRef Name = "name";
1629   StringRef LinkageName = "linkage";
1630   Metadata *File = MDTuple::getDistinct(Context, None);
1631   unsigned Line = 5;
1632   Metadata *Type = MDTuple::getDistinct(Context, None);
1633   bool IsLocalToUnit = false;
1634   bool IsDefinition = true;
1635   Metadata *Variable = MDTuple::getDistinct(Context, None);
1636   Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None);
1637
1638   auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1639                                   Type, IsLocalToUnit, IsDefinition, Variable,
1640                                   StaticDataMemberDeclaration);
1641   EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1642   EXPECT_EQ(Scope, N->getScope());
1643   EXPECT_EQ(Name, N->getName());
1644   EXPECT_EQ(LinkageName, N->getLinkageName());
1645   EXPECT_EQ(File, N->getFile());
1646   EXPECT_EQ(Line, N->getLine());
1647   EXPECT_EQ(Type, N->getType());
1648   EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1649   EXPECT_EQ(IsDefinition, N->isDefinition());
1650   EXPECT_EQ(Variable, N->getVariable());
1651   EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1652   EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1653                                      Line, Type, IsLocalToUnit, IsDefinition,
1654                                      Variable, StaticDataMemberDeclaration));
1655
1656   EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File,
1657                                      Line, Type, IsLocalToUnit, IsDefinition,
1658                                      Variable, StaticDataMemberDeclaration));
1659   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1660                                      Line, Type, IsLocalToUnit, IsDefinition,
1661                                      Variable, StaticDataMemberDeclaration));
1662   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1663                                      Type, IsLocalToUnit, IsDefinition,
1664                                      Variable, StaticDataMemberDeclaration));
1665   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope,
1666                                      Line, Type, IsLocalToUnit, IsDefinition,
1667                                      Variable, StaticDataMemberDeclaration));
1668   EXPECT_NE(N,
1669             MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1670                                   Line + 1, Type, IsLocalToUnit, IsDefinition,
1671                                   Variable, StaticDataMemberDeclaration));
1672   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1673                                      Line, Scope, IsLocalToUnit, IsDefinition,
1674                                      Variable, StaticDataMemberDeclaration));
1675   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1676                                      Line, Type, !IsLocalToUnit, IsDefinition,
1677                                      Variable, StaticDataMemberDeclaration));
1678   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1679                                      Line, Type, IsLocalToUnit, !IsDefinition,
1680                                      Variable, StaticDataMemberDeclaration));
1681   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1682                                      Line, Type, IsLocalToUnit, IsDefinition,
1683                                      Type, StaticDataMemberDeclaration));
1684   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1685                                      Line, Type, IsLocalToUnit, IsDefinition,
1686                                      Variable, Type));
1687
1688   TempMDGlobalVariable Temp = N->clone();
1689   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1690 }
1691
1692 typedef MetadataTest MDLocalVariableTest;
1693
1694 TEST_F(MDLocalVariableTest, get) {
1695   unsigned Tag = dwarf::DW_TAG_arg_variable;
1696   Metadata *Scope = MDTuple::getDistinct(Context, None);
1697   StringRef Name = "name";
1698   Metadata *File = MDTuple::getDistinct(Context, None);
1699   unsigned Line = 5;
1700   Metadata *Type = MDTuple::getDistinct(Context, None);
1701   unsigned Arg = 6;
1702   unsigned Flags = 7;
1703   Metadata *InlinedAtScope = MDTuple::getDistinct(Context, None);
1704   Metadata *InlinedAt =
1705       MDLocation::getDistinct(Context, 10, 20, InlinedAtScope);
1706
1707   auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1708                                  Arg, Flags, InlinedAt);
1709   EXPECT_EQ(Tag, N->getTag());
1710   EXPECT_EQ(Scope, N->getScope());
1711   EXPECT_EQ(Name, N->getName());
1712   EXPECT_EQ(File, N->getFile());
1713   EXPECT_EQ(Line, N->getLine());
1714   EXPECT_EQ(Type, N->getType());
1715   EXPECT_EQ(Arg, N->getArg());
1716   EXPECT_EQ(Flags, N->getFlags());
1717   EXPECT_EQ(InlinedAt, N->getInlinedAt());
1718   EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1719                                     Arg, Flags, InlinedAt));
1720
1721   EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1722                                     Name, File, Line, Type, Arg, Flags,
1723                                     InlinedAt));
1724   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line,
1725                                     Type, Arg, Flags, InlinedAt));
1726   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1727                                     Type, Arg, Flags, InlinedAt));
1728   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line,
1729                                     Type, Arg, Flags, InlinedAt));
1730   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1731                                     Type, Arg, Flags, InlinedAt));
1732   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1733                                     Scope, Arg, Flags, InlinedAt));
1734   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1735                                     Arg + 1, Flags, InlinedAt));
1736   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1737                                     Arg, ~Flags, InlinedAt));
1738   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1739                                     Arg, Flags, Scope));
1740
1741   TempMDLocalVariable Temp = N->clone();
1742   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1743
1744   auto *Inlined = N->withoutInline();
1745   EXPECT_NE(N, Inlined);
1746   EXPECT_EQ(N->getTag(), Inlined->getTag());
1747   EXPECT_EQ(N->getScope(), Inlined->getScope());
1748   EXPECT_EQ(N->getName(), Inlined->getName());
1749   EXPECT_EQ(N->getFile(), Inlined->getFile());
1750   EXPECT_EQ(N->getLine(), Inlined->getLine());
1751   EXPECT_EQ(N->getType(), Inlined->getType());
1752   EXPECT_EQ(N->getArg(), Inlined->getArg());
1753   EXPECT_EQ(N->getFlags(), Inlined->getFlags());
1754   EXPECT_EQ(nullptr, Inlined->getInlinedAt());
1755   EXPECT_EQ(N, Inlined->withInline(cast<MDLocation>(InlinedAt)));
1756 }
1757
1758 typedef MetadataTest MDExpressionTest;
1759
1760 TEST_F(MDExpressionTest, get) {
1761   uint64_t Elements[] = {2, 6, 9, 78, 0};
1762   auto *N = MDExpression::get(Context, Elements);
1763   EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1764   EXPECT_EQ(N, MDExpression::get(Context, Elements));
1765
1766   EXPECT_EQ(5u, N->getNumElements());
1767   EXPECT_EQ(2u, N->getElement(0));
1768   EXPECT_EQ(6u, N->getElement(1));
1769   EXPECT_EQ(9u, N->getElement(2));
1770   EXPECT_EQ(78u, N->getElement(3));
1771   EXPECT_EQ(0u, N->getElement(4));
1772
1773   TempMDExpression Temp = N->clone();
1774   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1775 }
1776
1777 TEST_F(MDExpressionTest, isValid) {
1778 #define EXPECT_VALID(...)                                                      \
1779   do {                                                                         \
1780     uint64_t Elements[] = {__VA_ARGS__};                                       \
1781     EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid());              \
1782   } while (false)
1783 #define EXPECT_INVALID(...)                                                    \
1784   do {                                                                         \
1785     uint64_t Elements[] = {__VA_ARGS__};                                       \
1786     EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid());             \
1787   } while (false)
1788
1789   // Empty expression should be valid.
1790   EXPECT_TRUE(MDExpression::get(Context, None));
1791
1792   // Valid constructions.
1793   EXPECT_VALID(dwarf::DW_OP_plus, 6);
1794   EXPECT_VALID(dwarf::DW_OP_deref);
1795   EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1796   EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1797   EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1798   EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1799   EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1800
1801   // Invalid constructions.
1802   EXPECT_INVALID(~0u);
1803   EXPECT_INVALID(dwarf::DW_OP_plus);
1804   EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1805   EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1806   EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1807   EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1808
1809 #undef EXPECT_VALID
1810 #undef EXPECT_INVALID
1811 }
1812
1813 typedef MetadataTest MDObjCPropertyTest;
1814
1815 TEST_F(MDObjCPropertyTest, get) {
1816   StringRef Name = "name";
1817   Metadata *File = MDTuple::getDistinct(Context, None);
1818   unsigned Line = 5;
1819   StringRef GetterName = "getter";
1820   StringRef SetterName = "setter";
1821   unsigned Attributes = 7;
1822   Metadata *Type = MDTuple::getDistinct(Context, None);
1823
1824   auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1825                                 SetterName, Attributes, Type);
1826
1827   EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1828   EXPECT_EQ(Name, N->getName());
1829   EXPECT_EQ(File, N->getFile());
1830   EXPECT_EQ(Line, N->getLine());
1831   EXPECT_EQ(GetterName, N->getGetterName());
1832   EXPECT_EQ(SetterName, N->getSetterName());
1833   EXPECT_EQ(Attributes, N->getAttributes());
1834   EXPECT_EQ(Type, N->getType());
1835   EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1836                                    SetterName, Attributes, Type));
1837
1838   EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1839                                    SetterName, Attributes, Type));
1840   EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1841                                    SetterName, Attributes, Type));
1842   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1843                                    SetterName, Attributes, Type));
1844   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1845                                    SetterName, Attributes, Type));
1846   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1847                                    "other", Attributes, Type));
1848   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1849                                    SetterName, Attributes + 1, Type));
1850   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1851                                    SetterName, Attributes, File));
1852
1853   TempMDObjCProperty Temp = N->clone();
1854   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1855 }
1856
1857 typedef MetadataTest MDImportedEntityTest;
1858
1859 TEST_F(MDImportedEntityTest, get) {
1860   unsigned Tag = dwarf::DW_TAG_imported_module;
1861   Metadata *Scope = MDTuple::getDistinct(Context, None);
1862   Metadata *Entity = MDTuple::getDistinct(Context, None);
1863   unsigned Line = 5;
1864   StringRef Name = "name";
1865
1866   auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1867
1868   EXPECT_EQ(Tag, N->getTag());
1869   EXPECT_EQ(Scope, N->getScope());
1870   EXPECT_EQ(Entity, N->getEntity());
1871   EXPECT_EQ(Line, N->getLine());
1872   EXPECT_EQ(Name, N->getName());
1873   EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1874
1875   EXPECT_NE(N,
1876             MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1877                                   Scope, Entity, Line, Name));
1878   EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1879   EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1880   EXPECT_NE(N,
1881             MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1882   EXPECT_NE(N,
1883             MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
1884
1885   TempMDImportedEntity Temp = N->clone();
1886   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1887 }
1888
1889 typedef MetadataTest MetadataAsValueTest;
1890
1891 TEST_F(MetadataAsValueTest, MDNode) {
1892   MDNode *N = MDNode::get(Context, None);
1893   auto *V = MetadataAsValue::get(Context, N);
1894   EXPECT_TRUE(V->getType()->isMetadataTy());
1895   EXPECT_EQ(N, V->getMetadata());
1896
1897   auto *V2 = MetadataAsValue::get(Context, N);
1898   EXPECT_EQ(V, V2);
1899 }
1900
1901 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1902   MDNode *N = MDNode::get(Context, None);
1903   Metadata *Ops[] = {N};
1904   MDNode *N2 = MDNode::get(Context, Ops);
1905   auto *V = MetadataAsValue::get(Context, N2);
1906   EXPECT_TRUE(V->getType()->isMetadataTy());
1907   EXPECT_EQ(N2, V->getMetadata());
1908
1909   auto *V2 = MetadataAsValue::get(Context, N2);
1910   EXPECT_EQ(V, V2);
1911
1912   auto *V3 = MetadataAsValue::get(Context, N);
1913   EXPECT_TRUE(V3->getType()->isMetadataTy());
1914   EXPECT_NE(V, V3);
1915   EXPECT_EQ(N, V3->getMetadata());
1916 }
1917
1918 TEST_F(MetadataAsValueTest, MDNodeConstant) {
1919   auto *C = ConstantInt::getTrue(Context);
1920   auto *MD = ConstantAsMetadata::get(C);
1921   Metadata *Ops[] = {MD};
1922   auto *N = MDNode::get(Context, Ops);
1923
1924   auto *V = MetadataAsValue::get(Context, MD);
1925   EXPECT_TRUE(V->getType()->isMetadataTy());
1926   EXPECT_EQ(MD, V->getMetadata());
1927
1928   auto *V2 = MetadataAsValue::get(Context, N);
1929   EXPECT_EQ(MD, V2->getMetadata());
1930   EXPECT_EQ(V, V2);
1931 }
1932
1933 typedef MetadataTest ValueAsMetadataTest;
1934
1935 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1936   Type *Ty = Type::getInt1PtrTy(Context);
1937   std::unique_ptr<GlobalVariable> GV0(
1938       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1939   auto *MD = ValueAsMetadata::get(GV0.get());
1940   EXPECT_TRUE(MD->getValue() == GV0.get());
1941   ASSERT_TRUE(GV0->use_empty());
1942
1943   std::unique_ptr<GlobalVariable> GV1(
1944       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1945   GV0->replaceAllUsesWith(GV1.get());
1946   EXPECT_TRUE(MD->getValue() == GV1.get());
1947 }
1948
1949 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
1950   // Create a constant.
1951   ConstantAsMetadata *CI = ConstantAsMetadata::get(
1952       ConstantInt::get(getGlobalContext(), APInt(8, 0)));
1953
1954   // Create a temporary to prevent nodes from resolving.
1955   auto Temp = MDTuple::getTemporary(Context, None);
1956
1957   // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
1958   Metadata *Ops1[] = {CI, CI, Temp.get()};
1959   Metadata *Ops2[] = {nullptr, CI, Temp.get()};
1960
1961   auto *N1 = MDTuple::get(Context, Ops1);
1962   auto *N2 = MDTuple::get(Context, Ops2);
1963   ASSERT_NE(N1, N2);
1964
1965   // Tell metadata that the constant is getting deleted.
1966   //
1967   // After this, N1 will be invalid, so don't touch it.
1968   ValueAsMetadata::handleDeletion(CI->getValue());
1969   EXPECT_EQ(nullptr, N2->getOperand(0));
1970   EXPECT_EQ(nullptr, N2->getOperand(1));
1971   EXPECT_EQ(Temp.get(), N2->getOperand(2));
1972
1973   // Clean up Temp for teardown.
1974   Temp->replaceAllUsesWith(nullptr);
1975 }
1976
1977 typedef MetadataTest TrackingMDRefTest;
1978
1979 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
1980   Type *Ty = Type::getInt1PtrTy(Context);
1981   std::unique_ptr<GlobalVariable> GV0(
1982       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1983   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
1984   EXPECT_TRUE(MD->getValue() == GV0.get());
1985   ASSERT_TRUE(GV0->use_empty());
1986
1987   std::unique_ptr<GlobalVariable> GV1(
1988       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1989   GV0->replaceAllUsesWith(GV1.get());
1990   EXPECT_TRUE(MD->getValue() == GV1.get());
1991
1992   // Reset it, so we don't inadvertently test deletion.
1993   MD.reset();
1994 }
1995
1996 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
1997   Type *Ty = Type::getInt1PtrTy(Context);
1998   std::unique_ptr<GlobalVariable> GV(
1999       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2000   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2001   EXPECT_TRUE(MD->getValue() == GV.get());
2002   ASSERT_TRUE(GV->use_empty());
2003
2004   GV.reset();
2005   EXPECT_TRUE(!MD);
2006 }
2007
2008 TEST(NamedMDNodeTest, Search) {
2009   LLVMContext Context;
2010   ConstantAsMetadata *C =
2011       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2012   ConstantAsMetadata *C2 =
2013       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
2014
2015   Metadata *const V = C;
2016   Metadata *const V2 = C2;
2017   MDNode *n = MDNode::get(Context, V);
2018   MDNode *n2 = MDNode::get(Context, V2);
2019
2020   Module M("MyModule", Context);
2021   const char *Name = "llvm.NMD1";
2022   NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2023   NMD->addOperand(n);
2024   NMD->addOperand(n2);
2025
2026   std::string Str;
2027   raw_string_ostream oss(Str);
2028   NMD->print(oss);
2029   EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
2030                oss.str().c_str());
2031 }
2032 }