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