IR: Add MDExpression::ExprOperand
[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
648 TEST_F(GenericDebugNodeTest, getEmptyHeader) {
649   // Canonicalize !"" to null.
650   auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
651   EXPECT_EQ(StringRef(), N->getHeader());
652   EXPECT_EQ(nullptr, N->getOperand(0));
653 }
654
655 typedef MetadataTest MDSubrangeTest;
656
657 TEST_F(MDSubrangeTest, get) {
658   auto *N = MDSubrange::get(Context, 5, 7);
659   EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
660   EXPECT_EQ(5, N->getCount());
661   EXPECT_EQ(7, N->getLo());
662   EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
663   EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
664 }
665
666 typedef MetadataTest MDEnumeratorTest;
667
668 TEST_F(MDEnumeratorTest, get) {
669   auto *N = MDEnumerator::get(Context, 7, "name");
670   EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
671   EXPECT_EQ(7, N->getValue());
672   EXPECT_EQ("name", N->getName());
673   EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
674
675   EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
676   EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
677 }
678
679 typedef MetadataTest MDBasicTypeTest;
680
681 TEST_F(MDBasicTypeTest, get) {
682   auto *N =
683       MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
684   EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
685   EXPECT_EQ("special", N->getName());
686   EXPECT_EQ(33u, N->getSizeInBits());
687   EXPECT_EQ(26u, N->getAlignInBits());
688   EXPECT_EQ(7u, N->getEncoding());
689   EXPECT_EQ(0u, N->getLine());
690   EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
691                                 26, 7));
692
693   EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
694                                 "special", 33, 26, 7));
695   EXPECT_NE(N,
696             MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
697   EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
698                                 26, 7));
699   EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
700                                 25, 7));
701   EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
702                                 26, 6));
703 }
704
705 typedef MetadataTest MDDerivedTypeTest;
706
707 TEST_F(MDDerivedTypeTest, get) {
708   Metadata *File = MDTuple::getDistinct(Context, None);
709   Metadata *Scope = MDTuple::getDistinct(Context, None);
710   Metadata *BaseType = MDTuple::getDistinct(Context, None);
711   Metadata *ExtraData = MDTuple::getDistinct(Context, None);
712
713   auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
714                                File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
715   EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
716   EXPECT_EQ("something", N->getName());
717   EXPECT_EQ(File, N->getFile());
718   EXPECT_EQ(1u, N->getLine());
719   EXPECT_EQ(Scope, N->getScope());
720   EXPECT_EQ(BaseType, N->getBaseType());
721   EXPECT_EQ(2u, N->getSizeInBits());
722   EXPECT_EQ(3u, N->getAlignInBits());
723   EXPECT_EQ(4u, N->getOffsetInBits());
724   EXPECT_EQ(5u, N->getFlags());
725   EXPECT_EQ(ExtraData, N->getExtraData());
726   EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
727                                   "something", File, 1, Scope, BaseType, 2, 3,
728                                   4, 5, ExtraData));
729
730   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
731                                   "something", File, 1, Scope, BaseType, 2, 3,
732                                   4, 5, ExtraData));
733   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
734                                   File, 1, Scope, BaseType, 2, 3, 4, 5,
735                                   ExtraData));
736   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
737                                   "something", Scope, 1, Scope, BaseType, 2, 3,
738                                   4, 5, ExtraData));
739   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
740                                   "something", File, 2, Scope, BaseType, 2, 3,
741                                   4, 5, ExtraData));
742   EXPECT_NE(N,
743             MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
744                                File, 1, File, BaseType, 2, 3, 4, 5, ExtraData));
745   EXPECT_NE(N,
746             MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
747                                File, 1, Scope, File, 2, 3, 4, 5, ExtraData));
748   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
749                                   "something", File, 1, Scope, BaseType, 3, 3,
750                                   4, 5, ExtraData));
751   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
752                                   "something", File, 1, Scope, BaseType, 2, 2,
753                                   4, 5, ExtraData));
754   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
755                                   "something", File, 1, Scope, BaseType, 2, 3,
756                                   5, 5, ExtraData));
757   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
758                                   "something", File, 1, Scope, BaseType, 2, 3,
759                                   4, 4, ExtraData));
760   EXPECT_NE(N,
761             MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
762                                File, 1, Scope, BaseType, 2, 3, 4, 5, File));
763 }
764
765 typedef MetadataTest MDCompositeTypeTest;
766
767 TEST_F(MDCompositeTypeTest, get) {
768   unsigned Tag = dwarf::DW_TAG_structure_type;
769   StringRef Name = "some name";
770   Metadata *File = MDTuple::getDistinct(Context, None);
771   unsigned Line = 1;
772   Metadata *Scope = MDTuple::getDistinct(Context, None);
773   Metadata *BaseType = MDTuple::getDistinct(Context, None);
774   unsigned SizeInBits = 2;
775   unsigned AlignInBits = 3;
776   unsigned OffsetInBits = 4;
777   unsigned Flags = 5;
778   Metadata *Elements = MDTuple::getDistinct(Context, None);
779   unsigned RuntimeLang = 6;
780   Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
781   Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
782   StringRef Identifier = "some id";
783
784   auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
785                                  BaseType, SizeInBits, AlignInBits,
786                                  OffsetInBits, Flags, Elements, RuntimeLang,
787                                  VTableHolder, TemplateParams, Identifier);
788   EXPECT_EQ(Tag, N->getTag());
789   EXPECT_EQ(Name, N->getName());
790   EXPECT_EQ(File, N->getFile());
791   EXPECT_EQ(Line, N->getLine());
792   EXPECT_EQ(Scope, N->getScope());
793   EXPECT_EQ(BaseType, N->getBaseType());
794   EXPECT_EQ(SizeInBits, N->getSizeInBits());
795   EXPECT_EQ(AlignInBits, N->getAlignInBits());
796   EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
797   EXPECT_EQ(Flags, N->getFlags());
798   EXPECT_EQ(Elements, N->getElements());
799   EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
800   EXPECT_EQ(VTableHolder, N->getVTableHolder());
801   EXPECT_EQ(TemplateParams, N->getTemplateParams());
802   EXPECT_EQ(Identifier, N->getIdentifier());
803
804   EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
805                                     BaseType, SizeInBits, AlignInBits,
806                                     OffsetInBits, Flags, Elements, RuntimeLang,
807                                     VTableHolder, TemplateParams, Identifier));
808
809   EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
810                                     BaseType, SizeInBits, AlignInBits,
811                                     OffsetInBits, Flags, Elements, RuntimeLang,
812                                     VTableHolder, TemplateParams, Identifier));
813   EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
814                                     BaseType, SizeInBits, AlignInBits,
815                                     OffsetInBits, Flags, Elements, RuntimeLang,
816                                     VTableHolder, TemplateParams, Identifier));
817   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, Scope, Line, Scope,
818                                     BaseType, SizeInBits, AlignInBits,
819                                     OffsetInBits, Flags, Elements, RuntimeLang,
820                                     VTableHolder, TemplateParams, Identifier));
821   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
822                                     BaseType, SizeInBits, AlignInBits,
823                                     OffsetInBits, Flags, Elements, RuntimeLang,
824                                     VTableHolder, TemplateParams, Identifier));
825   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, File,
826                                     BaseType, SizeInBits, AlignInBits,
827                                     OffsetInBits, Flags, Elements, RuntimeLang,
828                                     VTableHolder, TemplateParams, Identifier));
829   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
830                                     SizeInBits, AlignInBits, OffsetInBits,
831                                     Flags, Elements, RuntimeLang, VTableHolder,
832                                     TemplateParams, Identifier));
833   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
834                                     BaseType, SizeInBits + 1, AlignInBits,
835                                     OffsetInBits, Flags, Elements, RuntimeLang,
836                                     VTableHolder, TemplateParams, Identifier));
837   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
838                                     BaseType, SizeInBits, AlignInBits + 1,
839                                     OffsetInBits, Flags, Elements, RuntimeLang,
840                                     VTableHolder, TemplateParams, Identifier));
841   EXPECT_NE(N, MDCompositeType::get(
842                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
843                    AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
844                    VTableHolder, TemplateParams, Identifier));
845   EXPECT_NE(N, MDCompositeType::get(
846                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
847                    AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
848                    VTableHolder, TemplateParams, Identifier));
849   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
850                                     BaseType, SizeInBits, AlignInBits,
851                                     OffsetInBits, Flags, File, RuntimeLang,
852                                     VTableHolder, TemplateParams, Identifier));
853   EXPECT_NE(N, MDCompositeType::get(
854                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
855                    AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
856                    VTableHolder, TemplateParams, Identifier));
857   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
858                                     BaseType, SizeInBits, AlignInBits,
859                                     OffsetInBits, Flags, Elements, RuntimeLang,
860                                     File, TemplateParams, Identifier));
861   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
862                                     BaseType, SizeInBits, AlignInBits,
863                                     OffsetInBits, Flags, Elements, RuntimeLang,
864                                     VTableHolder, File, Identifier));
865   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
866                                     BaseType, SizeInBits, AlignInBits,
867                                     OffsetInBits, Flags, Elements, RuntimeLang,
868                                     VTableHolder, TemplateParams, "other"));
869
870   // Be sure that missing identifiers get null pointers.
871   EXPECT_FALSE(MDCompositeType::get(
872                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
873                    AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
874                    VTableHolder, TemplateParams, "")->getRawIdentifier());
875   EXPECT_FALSE(MDCompositeType::get(
876                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
877                    AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
878                    VTableHolder, TemplateParams)->getRawIdentifier());
879 }
880
881 typedef MetadataTest MDSubroutineTypeTest;
882
883 TEST_F(MDSubroutineTypeTest, get) {
884   unsigned Flags = 1;
885   Metadata *TypeArray = MDTuple::getDistinct(Context, None);
886
887   auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
888   EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
889   EXPECT_EQ(Flags, N->getFlags());
890   EXPECT_EQ(TypeArray, N->getTypeArray());
891   EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
892
893   EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
894   EXPECT_NE(N, MDSubroutineType::get(Context, Flags,
895                                      MDTuple::getDistinct(Context, None)));
896 }
897
898 typedef MetadataTest MDFileTest;
899
900 TEST_F(MDFileTest, get) {
901   StringRef Filename = "file";
902   StringRef Directory = "dir";
903   auto *N = MDFile::get(Context, Filename, Directory);
904
905   EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
906   EXPECT_EQ(Filename, N->getFilename());
907   EXPECT_EQ(Directory, N->getDirectory());
908   EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
909
910   EXPECT_NE(N, MDFile::get(Context, "other", Directory));
911   EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
912 }
913
914 typedef MetadataTest MDCompileUnitTest;
915
916 TEST_F(MDCompileUnitTest, get) {
917   unsigned SourceLanguage = 1;
918   Metadata *File = MDTuple::getDistinct(Context, None);
919   StringRef Producer = "some producer";
920   bool IsOptimized = false;
921   StringRef Flags = "flag after flag";
922   unsigned RuntimeVersion = 2;
923   StringRef SplitDebugFilename = "another/file";
924   unsigned EmissionKind = 3;
925   Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
926   Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
927   Metadata *Subprograms = MDTuple::getDistinct(Context, None);
928   Metadata *GlobalVariables = MDTuple::getDistinct(Context, None);
929   Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
930   auto *N = MDCompileUnit::get(
931       Context, SourceLanguage, File, Producer, IsOptimized, Flags,
932       RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
933       RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
934
935   EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
936   EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
937   EXPECT_EQ(File, N->getFile());
938   EXPECT_EQ(Producer, N->getProducer());
939   EXPECT_EQ(IsOptimized, N->isOptimized());
940   EXPECT_EQ(Flags, N->getFlags());
941   EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
942   EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
943   EXPECT_EQ(EmissionKind, N->getEmissionKind());
944   EXPECT_EQ(EnumTypes, N->getEnumTypes());
945   EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
946   EXPECT_EQ(Subprograms, N->getSubprograms());
947   EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
948   EXPECT_EQ(ImportedEntities, N->getImportedEntities());
949   EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
950                                   IsOptimized, Flags, RuntimeVersion,
951                                   SplitDebugFilename, EmissionKind, EnumTypes,
952                                   RetainedTypes, Subprograms, GlobalVariables,
953                                   ImportedEntities));
954
955   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
956                                   IsOptimized, Flags, RuntimeVersion,
957                                   SplitDebugFilename, EmissionKind, EnumTypes,
958                                   RetainedTypes, Subprograms, GlobalVariables,
959                                   ImportedEntities));
960   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer,
961                                   IsOptimized, Flags, RuntimeVersion,
962                                   SplitDebugFilename, EmissionKind, EnumTypes,
963                                   RetainedTypes, Subprograms, GlobalVariables,
964                                   ImportedEntities));
965   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
966                                   IsOptimized, Flags, RuntimeVersion,
967                                   SplitDebugFilename, EmissionKind, EnumTypes,
968                                   RetainedTypes, Subprograms, GlobalVariables,
969                                   ImportedEntities));
970   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
971                                   !IsOptimized, Flags, RuntimeVersion,
972                                   SplitDebugFilename, EmissionKind, EnumTypes,
973                                   RetainedTypes, Subprograms, GlobalVariables,
974                                   ImportedEntities));
975   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
976                                   IsOptimized, "other", RuntimeVersion,
977                                   SplitDebugFilename, EmissionKind, EnumTypes,
978                                   RetainedTypes, Subprograms, GlobalVariables,
979                                   ImportedEntities));
980   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
981                                   IsOptimized, Flags, RuntimeVersion + 1,
982                                   SplitDebugFilename, EmissionKind, EnumTypes,
983                                   RetainedTypes, Subprograms, GlobalVariables,
984                                   ImportedEntities));
985   EXPECT_NE(N,
986             MDCompileUnit::get(Context, SourceLanguage, File, Producer,
987                                IsOptimized, Flags, RuntimeVersion, "other",
988                                EmissionKind, EnumTypes, RetainedTypes,
989                                Subprograms, GlobalVariables, ImportedEntities));
990   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
991                                   IsOptimized, Flags, RuntimeVersion,
992                                   SplitDebugFilename, EmissionKind + 1,
993                                   EnumTypes, RetainedTypes, Subprograms,
994                                   GlobalVariables, ImportedEntities));
995   EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
996                                   IsOptimized, Flags, RuntimeVersion,
997                                   SplitDebugFilename, EmissionKind, File,
998                                   RetainedTypes, Subprograms, GlobalVariables,
999                                   ImportedEntities));
1000   EXPECT_NE(N, MDCompileUnit::get(
1001                    Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1002                    RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1003                    File, Subprograms, GlobalVariables, ImportedEntities));
1004   EXPECT_NE(N, MDCompileUnit::get(
1005                    Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1006                    RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1007                    RetainedTypes, File, GlobalVariables, ImportedEntities));
1008   EXPECT_NE(N, MDCompileUnit::get(
1009                    Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1010                    RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1011                    RetainedTypes, Subprograms, File, ImportedEntities));
1012   EXPECT_NE(N, MDCompileUnit::get(
1013                    Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1014                    RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1015                    RetainedTypes, Subprograms, GlobalVariables, File));
1016 }
1017
1018 typedef MetadataTest MDSubprogramTest;
1019
1020 TEST_F(MDSubprogramTest, get) {
1021   Metadata *Scope = MDTuple::getDistinct(Context, None);
1022   StringRef Name = "name";
1023   StringRef LinkageName = "linkage";
1024   Metadata *File = MDTuple::getDistinct(Context, None);
1025   unsigned Line = 2;
1026   Metadata *Type = MDTuple::getDistinct(Context, None);
1027   bool IsLocalToUnit = false;
1028   bool IsDefinition = true;
1029   unsigned ScopeLine = 3;
1030   Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1031   unsigned Virtuality = 4;
1032   unsigned VirtualIndex = 5;
1033   unsigned Flags = 6;
1034   bool IsOptimized = false;
1035   Metadata *Function = MDTuple::getDistinct(Context, None);
1036   Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1037   Metadata *Declaration = MDTuple::getDistinct(Context, None);
1038   Metadata *Variables = MDTuple::getDistinct(Context, None);
1039
1040   auto *N = MDSubprogram::get(
1041       Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1042       IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1043       IsOptimized, Function, TemplateParams, Declaration, Variables);
1044
1045   EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1046   EXPECT_EQ(Scope, N->getScope());
1047   EXPECT_EQ(Name, N->getName());
1048   EXPECT_EQ(LinkageName, N->getLinkageName());
1049   EXPECT_EQ(File, N->getFile());
1050   EXPECT_EQ(Line, N->getLine());
1051   EXPECT_EQ(Type, N->getType());
1052   EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1053   EXPECT_EQ(IsDefinition, N->isDefinition());
1054   EXPECT_EQ(ScopeLine, N->getScopeLine());
1055   EXPECT_EQ(ContainingType, N->getContainingType());
1056   EXPECT_EQ(Virtuality, N->getVirtuality());
1057   EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1058   EXPECT_EQ(Flags, N->getFlags());
1059   EXPECT_EQ(IsOptimized, N->isOptimized());
1060   EXPECT_EQ(Function, N->getFunction());
1061   EXPECT_EQ(TemplateParams, N->getTemplateParams());
1062   EXPECT_EQ(Declaration, N->getDeclaration());
1063   EXPECT_EQ(Variables, N->getVariables());
1064   EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1065                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1066                                  ContainingType, Virtuality, VirtualIndex,
1067                                  Flags, IsOptimized, Function, TemplateParams,
1068                                  Declaration, Variables));
1069
1070   EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1071                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1072                                  ContainingType, Virtuality, VirtualIndex,
1073                                  Flags, IsOptimized, Function, TemplateParams,
1074                                  Declaration, Variables));
1075   EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1076                                  Line, Type, IsLocalToUnit, IsDefinition,
1077                                  ScopeLine, ContainingType, Virtuality,
1078                                  VirtualIndex, Flags, IsOptimized, Function,
1079                                  TemplateParams, Declaration, Variables));
1080   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1081                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1082                                  ContainingType, Virtuality, VirtualIndex,
1083                                  Flags, IsOptimized, Function, TemplateParams,
1084                                  Declaration, Variables));
1085   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1086                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1087                                  ContainingType, Virtuality, VirtualIndex,
1088                                  Flags, IsOptimized, Function, TemplateParams,
1089                                  Declaration, Variables));
1090   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1091                                  Line + 1, Type, IsLocalToUnit, IsDefinition,
1092                                  ScopeLine, ContainingType, Virtuality,
1093                                  VirtualIndex, Flags, IsOptimized, Function,
1094                                  TemplateParams, Declaration, Variables));
1095   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1096                                  Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1097                                  ContainingType, Virtuality, VirtualIndex,
1098                                  Flags, IsOptimized, Function, TemplateParams,
1099                                  Declaration, Variables));
1100   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1101                                  Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1102                                  ContainingType, Virtuality, VirtualIndex,
1103                                  Flags, IsOptimized, Function, TemplateParams,
1104                                  Declaration, Variables));
1105   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1106                                  Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1107                                  ContainingType, Virtuality, VirtualIndex,
1108                                  Flags, IsOptimized, Function, TemplateParams,
1109                                  Declaration, Variables));
1110   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1111                                  Type, IsLocalToUnit, IsDefinition,
1112                                  ScopeLine + 1, ContainingType, Virtuality,
1113                                  VirtualIndex, Flags, IsOptimized, Function,
1114                                  TemplateParams, Declaration, Variables));
1115   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1116                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1117                                  Type, Virtuality, VirtualIndex, Flags,
1118                                  IsOptimized, Function, TemplateParams,
1119                                  Declaration, Variables));
1120   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1121                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1122                                  ContainingType, Virtuality + 1, VirtualIndex,
1123                                  Flags, IsOptimized, Function, TemplateParams,
1124                                  Declaration, Variables));
1125   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1126                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1127                                  ContainingType, Virtuality, VirtualIndex + 1,
1128                                  Flags, IsOptimized, Function, TemplateParams,
1129                                  Declaration, Variables));
1130   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1131                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1132                                  ContainingType, Virtuality, VirtualIndex,
1133                                  ~Flags, IsOptimized, Function, TemplateParams,
1134                                  Declaration, Variables));
1135   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1136                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1137                                  ContainingType, Virtuality, VirtualIndex,
1138                                  Flags, !IsOptimized, Function, TemplateParams,
1139                                  Declaration, Variables));
1140   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1141                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1142                                  ContainingType, Virtuality, VirtualIndex,
1143                                  Flags, IsOptimized, Type, TemplateParams,
1144                                  Declaration, Variables));
1145   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1146                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1147                                  ContainingType, Virtuality, VirtualIndex,
1148                                  Flags, IsOptimized, Function, Type,
1149                                  Declaration, Variables));
1150   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1151                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1152                                  ContainingType, Virtuality, VirtualIndex,
1153                                  Flags, IsOptimized, Function, TemplateParams,
1154                                  Type, Variables));
1155   EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1156                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1157                                  ContainingType, Virtuality, VirtualIndex,
1158                                  Flags, IsOptimized, Function, TemplateParams,
1159                                  Declaration, Type));
1160 }
1161
1162 typedef MetadataTest MDLexicalBlockTest;
1163
1164 TEST_F(MDLexicalBlockTest, get) {
1165   Metadata *Scope = MDTuple::getDistinct(Context, None);
1166   Metadata *File = MDTuple::getDistinct(Context, None);
1167   unsigned Line = 5;
1168   unsigned Column = 8;
1169
1170   auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1171
1172   EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1173   EXPECT_EQ(Scope, N->getScope());
1174   EXPECT_EQ(File, N->getFile());
1175   EXPECT_EQ(Line, N->getLine());
1176   EXPECT_EQ(Column, N->getColumn());
1177   EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1178
1179   EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1180   EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1181   EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1182   EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
1183 }
1184
1185 typedef MetadataTest MDLexicalBlockFileTest;
1186
1187 TEST_F(MDLexicalBlockFileTest, get) {
1188   Metadata *Scope = MDTuple::getDistinct(Context, None);
1189   Metadata *File = MDTuple::getDistinct(Context, None);
1190   unsigned Discriminator = 5;
1191
1192   auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1193
1194   EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1195   EXPECT_EQ(Scope, N->getScope());
1196   EXPECT_EQ(File, N->getFile());
1197   EXPECT_EQ(Discriminator, N->getDiscriminator());
1198   EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1199
1200   EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1201   EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1202   EXPECT_NE(N,
1203             MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1204 }
1205
1206 typedef MetadataTest MDNamespaceTest;
1207
1208 TEST_F(MDNamespaceTest, get) {
1209   Metadata *Scope = MDTuple::getDistinct(Context, None);
1210   Metadata *File = MDTuple::getDistinct(Context, None);
1211   StringRef Name = "namespace";
1212   unsigned Line = 5;
1213
1214   auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1215
1216   EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1217   EXPECT_EQ(Scope, N->getScope());
1218   EXPECT_EQ(File, N->getFile());
1219   EXPECT_EQ(Name, N->getName());
1220   EXPECT_EQ(Line, N->getLine());
1221   EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1222
1223   EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1224   EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1225   EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1226   EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
1227 }
1228
1229 typedef MetadataTest MDTemplateTypeParameterTest;
1230
1231 TEST_F(MDTemplateTypeParameterTest, get) {
1232   Metadata *Scope = MDTuple::getDistinct(Context, None);
1233   StringRef Name = "template";
1234   Metadata *Type = MDTuple::getDistinct(Context, None);
1235
1236   auto *N = MDTemplateTypeParameter::get(Context, Scope, Name, Type);
1237
1238   EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1239   EXPECT_EQ(Scope, N->getScope());
1240   EXPECT_EQ(Name, N->getName());
1241   EXPECT_EQ(Type, N->getType());
1242   EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Scope, Name, Type));
1243
1244   EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Type, Name, Type));
1245   EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Scope, "other", Type));
1246   EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Scope, Name, Scope));
1247 }
1248
1249 typedef MetadataTest MDTemplateValueParameterTest;
1250
1251 TEST_F(MDTemplateValueParameterTest, get) {
1252   unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1253   Metadata *Scope = MDTuple::getDistinct(Context, None);
1254   StringRef Name = "template";
1255   Metadata *Type = MDTuple::getDistinct(Context, None);
1256   Metadata *Value = MDTuple::getDistinct(Context, None);
1257
1258   auto *N =
1259       MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Value);
1260   EXPECT_EQ(Tag, N->getTag());
1261   EXPECT_EQ(Scope, N->getScope());
1262   EXPECT_EQ(Name, N->getName());
1263   EXPECT_EQ(Type, N->getType());
1264   EXPECT_EQ(Value, N->getValue());
1265   EXPECT_EQ(
1266       N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Value));
1267
1268   EXPECT_NE(N, MDTemplateValueParameter::get(
1269                    Context, dwarf::DW_TAG_GNU_template_template_param, Scope,
1270                    Name, Type, Value));
1271   EXPECT_NE(
1272       N, MDTemplateValueParameter::get(Context, Tag, Type, Name, Type, Value));
1273   EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Scope, "other", Type,
1274                                              Value));
1275   EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Scope,
1276                                              Value));
1277   EXPECT_NE(
1278       N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Scope));
1279 }
1280
1281 typedef MetadataTest MDGlobalVariableTest;
1282
1283 TEST_F(MDGlobalVariableTest, get) {
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 = 5;
1289   Metadata *Type = MDTuple::getDistinct(Context, None);
1290   bool IsLocalToUnit = false;
1291   bool IsDefinition = true;
1292   Metadata *Variable = MDTuple::getDistinct(Context, None);
1293   Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None);
1294
1295   auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1296                                   Type, IsLocalToUnit, IsDefinition, Variable,
1297                                   StaticDataMemberDeclaration);
1298   EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1299   EXPECT_EQ(Scope, N->getScope());
1300   EXPECT_EQ(Name, N->getName());
1301   EXPECT_EQ(LinkageName, N->getLinkageName());
1302   EXPECT_EQ(File, N->getFile());
1303   EXPECT_EQ(Line, N->getLine());
1304   EXPECT_EQ(Type, N->getType());
1305   EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1306   EXPECT_EQ(IsDefinition, N->isDefinition());
1307   EXPECT_EQ(Variable, N->getVariable());
1308   EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1309   EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1310                                      Line, Type, IsLocalToUnit, IsDefinition,
1311                                      Variable, StaticDataMemberDeclaration));
1312
1313   EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File,
1314                                      Line, Type, IsLocalToUnit, IsDefinition,
1315                                      Variable, StaticDataMemberDeclaration));
1316   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1317                                      Line, Type, IsLocalToUnit, IsDefinition,
1318                                      Variable, StaticDataMemberDeclaration));
1319   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1320                                      Type, IsLocalToUnit, IsDefinition,
1321                                      Variable, StaticDataMemberDeclaration));
1322   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope,
1323                                      Line, Type, IsLocalToUnit, IsDefinition,
1324                                      Variable, StaticDataMemberDeclaration));
1325   EXPECT_NE(N,
1326             MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1327                                   Line + 1, Type, IsLocalToUnit, IsDefinition,
1328                                   Variable, StaticDataMemberDeclaration));
1329   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1330                                      Line, Scope, IsLocalToUnit, IsDefinition,
1331                                      Variable, StaticDataMemberDeclaration));
1332   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1333                                      Line, Type, !IsLocalToUnit, IsDefinition,
1334                                      Variable, StaticDataMemberDeclaration));
1335   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1336                                      Line, Type, IsLocalToUnit, !IsDefinition,
1337                                      Variable, StaticDataMemberDeclaration));
1338   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1339                                      Line, Type, IsLocalToUnit, IsDefinition,
1340                                      Type, StaticDataMemberDeclaration));
1341   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1342                                      Line, Type, IsLocalToUnit, IsDefinition,
1343                                      Variable, Type));
1344 }
1345
1346 typedef MetadataTest MDLocalVariableTest;
1347
1348 TEST_F(MDLocalVariableTest, get) {
1349   unsigned Tag = dwarf::DW_TAG_arg_variable;
1350   Metadata *Scope = MDTuple::getDistinct(Context, None);
1351   StringRef Name = "name";
1352   Metadata *File = MDTuple::getDistinct(Context, None);
1353   unsigned Line = 5;
1354   Metadata *Type = MDTuple::getDistinct(Context, None);
1355   unsigned Arg = 6;
1356   unsigned Flags = 7;
1357   Metadata *InlinedAt = MDTuple::getDistinct(Context, None);
1358
1359   auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1360                                  Arg, Flags, InlinedAt);
1361   EXPECT_EQ(Tag, N->getTag());
1362   EXPECT_EQ(Scope, N->getScope());
1363   EXPECT_EQ(Name, N->getName());
1364   EXPECT_EQ(File, N->getFile());
1365   EXPECT_EQ(Line, N->getLine());
1366   EXPECT_EQ(Type, N->getType());
1367   EXPECT_EQ(Arg, N->getArg());
1368   EXPECT_EQ(Flags, N->getFlags());
1369   EXPECT_EQ(InlinedAt, N->getInlinedAt());
1370   EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1371                                     Arg, Flags, InlinedAt));
1372
1373   EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1374                                     Name, File, Line, Type, Arg, Flags,
1375                                     InlinedAt));
1376   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line,
1377                                     Type, Arg, Flags, InlinedAt));
1378   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1379                                     Type, Arg, Flags, InlinedAt));
1380   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line,
1381                                     Type, Arg, Flags, InlinedAt));
1382   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1383                                     Type, Arg, Flags, InlinedAt));
1384   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1385                                     Scope, Arg, Flags, InlinedAt));
1386   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1387                                     Arg + 1, Flags, InlinedAt));
1388   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1389                                     Arg, ~Flags, InlinedAt));
1390   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1391                                     Arg, Flags, Scope));
1392 }
1393
1394 typedef MetadataTest MDExpressionTest;
1395
1396 TEST_F(MDExpressionTest, get) {
1397   uint64_t Elements[] = {2, 6, 9, 78, 0};
1398   auto *N = MDExpression::get(Context, Elements);
1399   EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1400   EXPECT_EQ(N, MDExpression::get(Context, Elements));
1401
1402   EXPECT_EQ(5u, N->getNumElements());
1403   EXPECT_EQ(2u, N->getElement(0));
1404   EXPECT_EQ(6u, N->getElement(1));
1405   EXPECT_EQ(9u, N->getElement(2));
1406   EXPECT_EQ(78u, N->getElement(3));
1407   EXPECT_EQ(0u, N->getElement(4));
1408 }
1409
1410 TEST_F(MDExpressionTest, isValid) {
1411 #define EXPECT_VALID(...)                                                      \
1412   do {                                                                         \
1413     uint64_t Elements[] = {__VA_ARGS__};                                       \
1414     EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid());              \
1415   } while (false)
1416 #define EXPECT_INVALID(...)                                                    \
1417   do {                                                                         \
1418     uint64_t Elements[] = {__VA_ARGS__};                                       \
1419     EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid());             \
1420   } while (false)
1421
1422   // Empty expression should be valid.
1423   EXPECT_TRUE(MDExpression::get(Context, None));
1424
1425   // Valid constructions.
1426   EXPECT_VALID(dwarf::DW_OP_plus, 6);
1427   EXPECT_VALID(dwarf::DW_OP_deref);
1428   EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1429   EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1430   EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1431   EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1432   EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1433
1434   // Invalid constructions.
1435   EXPECT_INVALID(~0u);
1436   EXPECT_INVALID(dwarf::DW_OP_plus);
1437   EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1438   EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1439   EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1440   EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1441
1442 #undef EXPECT_VALID
1443 #undef EXPECT_INVALID
1444 }
1445
1446 typedef MetadataTest MDObjCPropertyTest;
1447
1448 TEST_F(MDObjCPropertyTest, get) {
1449   StringRef Name = "name";
1450   Metadata *File = MDTuple::getDistinct(Context, None);
1451   unsigned Line = 5;
1452   StringRef GetterName = "getter";
1453   StringRef SetterName = "setter";
1454   unsigned Attributes = 7;
1455   Metadata *Type = MDTuple::getDistinct(Context, None);
1456
1457   auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1458                                 SetterName, Attributes, Type);
1459
1460   EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1461   EXPECT_EQ(Name, N->getName());
1462   EXPECT_EQ(File, N->getFile());
1463   EXPECT_EQ(Line, N->getLine());
1464   EXPECT_EQ(GetterName, N->getGetterName());
1465   EXPECT_EQ(SetterName, N->getSetterName());
1466   EXPECT_EQ(Attributes, N->getAttributes());
1467   EXPECT_EQ(Type, N->getType());
1468   EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1469                                    SetterName, Attributes, Type));
1470
1471   EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1472                                    SetterName, Attributes, Type));
1473   EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1474                                    SetterName, Attributes, Type));
1475   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1476                                    SetterName, Attributes, Type));
1477   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1478                                    SetterName, Attributes, Type));
1479   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1480                                    "other", Attributes, Type));
1481   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1482                                    SetterName, Attributes + 1, Type));
1483   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1484                                    SetterName, Attributes, File));
1485 }
1486
1487 typedef MetadataTest MDImportedEntityTest;
1488
1489 TEST_F(MDImportedEntityTest, get) {
1490   unsigned Tag = dwarf::DW_TAG_imported_module;
1491   Metadata *Scope = MDTuple::getDistinct(Context, None);
1492   Metadata *Entity = MDTuple::getDistinct(Context, None);
1493   unsigned Line = 5;
1494   StringRef Name = "name";
1495
1496   auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1497
1498   EXPECT_EQ(Tag, N->getTag());
1499   EXPECT_EQ(Scope, N->getScope());
1500   EXPECT_EQ(Entity, N->getEntity());
1501   EXPECT_EQ(Line, N->getLine());
1502   EXPECT_EQ(Name, N->getName());
1503   EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1504
1505   EXPECT_NE(N,
1506             MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1507                                   Scope, Entity, Line, Name));
1508   EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1509   EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1510   EXPECT_NE(N,
1511             MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1512   EXPECT_NE(N,
1513             MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
1514 }
1515
1516 typedef MetadataTest MetadataAsValueTest;
1517
1518 TEST_F(MetadataAsValueTest, MDNode) {
1519   MDNode *N = MDNode::get(Context, None);
1520   auto *V = MetadataAsValue::get(Context, N);
1521   EXPECT_TRUE(V->getType()->isMetadataTy());
1522   EXPECT_EQ(N, V->getMetadata());
1523
1524   auto *V2 = MetadataAsValue::get(Context, N);
1525   EXPECT_EQ(V, V2);
1526 }
1527
1528 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1529   MDNode *N = MDNode::get(Context, None);
1530   Metadata *Ops[] = {N};
1531   MDNode *N2 = MDNode::get(Context, Ops);
1532   auto *V = MetadataAsValue::get(Context, N2);
1533   EXPECT_TRUE(V->getType()->isMetadataTy());
1534   EXPECT_EQ(N2, V->getMetadata());
1535
1536   auto *V2 = MetadataAsValue::get(Context, N2);
1537   EXPECT_EQ(V, V2);
1538
1539   auto *V3 = MetadataAsValue::get(Context, N);
1540   EXPECT_TRUE(V3->getType()->isMetadataTy());
1541   EXPECT_NE(V, V3);
1542   EXPECT_EQ(N, V3->getMetadata());
1543 }
1544
1545 TEST_F(MetadataAsValueTest, MDNodeConstant) {
1546   auto *C = ConstantInt::getTrue(Context);
1547   auto *MD = ConstantAsMetadata::get(C);
1548   Metadata *Ops[] = {MD};
1549   auto *N = MDNode::get(Context, Ops);
1550
1551   auto *V = MetadataAsValue::get(Context, MD);
1552   EXPECT_TRUE(V->getType()->isMetadataTy());
1553   EXPECT_EQ(MD, V->getMetadata());
1554
1555   auto *V2 = MetadataAsValue::get(Context, N);
1556   EXPECT_EQ(MD, V2->getMetadata());
1557   EXPECT_EQ(V, V2);
1558 }
1559
1560 typedef MetadataTest ValueAsMetadataTest;
1561
1562 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1563   Type *Ty = Type::getInt1PtrTy(Context);
1564   std::unique_ptr<GlobalVariable> GV0(
1565       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1566   auto *MD = ValueAsMetadata::get(GV0.get());
1567   EXPECT_TRUE(MD->getValue() == GV0.get());
1568   ASSERT_TRUE(GV0->use_empty());
1569
1570   std::unique_ptr<GlobalVariable> GV1(
1571       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1572   GV0->replaceAllUsesWith(GV1.get());
1573   EXPECT_TRUE(MD->getValue() == GV1.get());
1574 }
1575
1576 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
1577   // Create a constant.
1578   ConstantAsMetadata *CI = ConstantAsMetadata::get(
1579       ConstantInt::get(getGlobalContext(), APInt(8, 0)));
1580
1581   // Create a temporary to prevent nodes from resolving.
1582   auto Temp = MDTuple::getTemporary(Context, None);
1583
1584   // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
1585   Metadata *Ops1[] = {CI, CI, Temp.get()};
1586   Metadata *Ops2[] = {nullptr, CI, Temp.get()};
1587
1588   auto *N1 = MDTuple::get(Context, Ops1);
1589   auto *N2 = MDTuple::get(Context, Ops2);
1590   ASSERT_NE(N1, N2);
1591
1592   // Tell metadata that the constant is getting deleted.
1593   //
1594   // After this, N1 will be invalid, so don't touch it.
1595   ValueAsMetadata::handleDeletion(CI->getValue());
1596   EXPECT_EQ(nullptr, N2->getOperand(0));
1597   EXPECT_EQ(nullptr, N2->getOperand(1));
1598   EXPECT_EQ(Temp.get(), N2->getOperand(2));
1599
1600   // Clean up Temp for teardown.
1601   Temp->replaceAllUsesWith(nullptr);
1602 }
1603
1604 typedef MetadataTest TrackingMDRefTest;
1605
1606 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
1607   Type *Ty = Type::getInt1PtrTy(Context);
1608   std::unique_ptr<GlobalVariable> GV0(
1609       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1610   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
1611   EXPECT_TRUE(MD->getValue() == GV0.get());
1612   ASSERT_TRUE(GV0->use_empty());
1613
1614   std::unique_ptr<GlobalVariable> GV1(
1615       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1616   GV0->replaceAllUsesWith(GV1.get());
1617   EXPECT_TRUE(MD->getValue() == GV1.get());
1618
1619   // Reset it, so we don't inadvertently test deletion.
1620   MD.reset();
1621 }
1622
1623 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
1624   Type *Ty = Type::getInt1PtrTy(Context);
1625   std::unique_ptr<GlobalVariable> GV(
1626       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1627   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
1628   EXPECT_TRUE(MD->getValue() == GV.get());
1629   ASSERT_TRUE(GV->use_empty());
1630
1631   GV.reset();
1632   EXPECT_TRUE(!MD);
1633 }
1634
1635 TEST(NamedMDNodeTest, Search) {
1636   LLVMContext Context;
1637   ConstantAsMetadata *C =
1638       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
1639   ConstantAsMetadata *C2 =
1640       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
1641
1642   Metadata *const V = C;
1643   Metadata *const V2 = C2;
1644   MDNode *n = MDNode::get(Context, V);
1645   MDNode *n2 = MDNode::get(Context, V2);
1646
1647   Module M("MyModule", Context);
1648   const char *Name = "llvm.NMD1";
1649   NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
1650   NMD->addOperand(n);
1651   NMD->addOperand(n2);
1652
1653   std::string Str;
1654   raw_string_ostream oss(Str);
1655   NMD->print(oss);
1656   EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
1657                oss.str().c_str());
1658 }
1659 }