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