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