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