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