1 //===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
26 TEST(ContextAndReplaceableUsesTest, FromContext) {
28 ContextAndReplaceableUses CRU(Context);
29 EXPECT_EQ(&Context, &CRU.getContext());
30 EXPECT_FALSE(CRU.hasReplaceableUses());
31 EXPECT_FALSE(CRU.getReplaceableUses());
34 TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
36 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
37 EXPECT_EQ(&Context, &CRU.getContext());
38 EXPECT_TRUE(CRU.hasReplaceableUses());
39 EXPECT_TRUE(CRU.getReplaceableUses());
42 TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
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());
51 TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
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());
63 class MetadataTest : public testing::Test {
65 MetadataTest() : Counter(0) {}
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);
78 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
79 MDSubprogram *getSubprogram() {
80 return MDSubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
81 nullptr, false, false, 0, nullptr, 0, 0, 0,
85 return MDFile::getDistinct(Context, "file.c", "/path/to/dir");
87 MDBasicType *getBasicType(StringRef Name) {
88 return MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
90 MDDerivedType *getDerivedType() {
91 return MDDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
93 getBasicType("basictype"), 1, 2, 0, 0);
95 ConstantAsMetadata *getConstantAsMetadata() {
96 return ConstantAsMetadata::get(
97 ConstantInt::get(Type::getInt32Ty(Context), Counter++));
99 MDCompositeType *getCompositeType() {
100 return MDCompositeType::getDistinct(
101 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, nullptr,
102 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "");
105 typedef MetadataTest MDStringTest;
107 // Test that construction of MDString with different value produces different
108 // MDString objects, even with the same string pointer and nulls in the string.
109 TEST_F(MDStringTest, CreateDifferent) {
110 char x[3] = { 'f', 0, 'A' };
111 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
113 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
117 // Test that creation of MDStrings with the same string contents produces the
118 // same MDString object, even with different pointers.
119 TEST_F(MDStringTest, CreateSame) {
120 char x[4] = { 'a', 'b', 'c', 'X' };
121 char y[4] = { 'a', 'b', 'c', 'Y' };
123 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
124 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
128 // Test that MDString prints out the string we fed it.
129 TEST_F(MDStringTest, PrintingSimple) {
130 char *str = new char[13];
131 strncpy(str, "testing 1 2 3", 13);
132 MDString *s = MDString::get(Context, StringRef(str, 13));
133 strncpy(str, "aaaaaaaaaaaaa", 13);
137 raw_string_ostream oss(Str);
139 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
142 // Test printing of MDString with non-printable characters.
143 TEST_F(MDStringTest, PrintingComplex) {
144 char str[5] = {0, '\n', '"', '\\', (char)-1};
145 MDString *s = MDString::get(Context, StringRef(str+0, 5));
147 raw_string_ostream oss(Str);
149 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
152 typedef MetadataTest MDNodeTest;
154 // Test the two constructors, and containing other Constants.
155 TEST_F(MDNodeTest, Simple) {
156 char x[3] = { 'a', 'b', 'c' };
157 char y[3] = { '1', '2', '3' };
159 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
160 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
161 ConstantAsMetadata *CI = ConstantAsMetadata::get(
162 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
164 std::vector<Metadata *> V;
169 MDNode *n1 = MDNode::get(Context, V);
170 Metadata *const c1 = n1;
171 MDNode *n2 = MDNode::get(Context, c1);
172 Metadata *const c2 = n2;
173 MDNode *n3 = MDNode::get(Context, V);
174 MDNode *n4 = MDNode::getIfExists(Context, V);
175 MDNode *n5 = MDNode::getIfExists(Context, c1);
176 MDNode *n6 = MDNode::getIfExists(Context, c2);
181 EXPECT_EQ(n6, (Metadata *)nullptr);
183 EXPECT_EQ(3u, n1->getNumOperands());
184 EXPECT_EQ(s1, n1->getOperand(0));
185 EXPECT_EQ(CI, n1->getOperand(1));
186 EXPECT_EQ(s2, n1->getOperand(2));
188 EXPECT_EQ(1u, n2->getNumOperands());
189 EXPECT_EQ(n1, n2->getOperand(0));
192 TEST_F(MDNodeTest, Delete) {
193 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
194 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
196 Metadata *const V = LocalAsMetadata::get(I);
197 MDNode *n = MDNode::get(Context, V);
198 TrackingMDRef wvh(n);
205 TEST_F(MDNodeTest, SelfReference) {
209 auto Temp = MDNode::getTemporary(Context, None);
210 Metadata *Args[] = {Temp.get()};
211 MDNode *Self = MDNode::get(Context, Args);
212 Self->replaceOperandWith(0, Self);
213 ASSERT_EQ(Self, Self->getOperand(0));
215 // Self-references should be distinct, so MDNode::get() should grab a
216 // uniqued node that references Self, not Self.
218 MDNode *Ref1 = MDNode::get(Context, Args);
219 MDNode *Ref2 = MDNode::get(Context, Args);
220 EXPECT_NE(Self, Ref1);
221 EXPECT_EQ(Ref1, Ref2);
227 auto Temp = MDNode::getTemporary(Context, None);
228 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
229 MDNode *Self = MDNode::get(Context, Args);
230 Self->replaceOperandWith(0, Self);
231 ASSERT_EQ(Self, Self->getOperand(0));
233 // Self-references should be distinct, so MDNode::get() should grab a
234 // uniqued node that references Self, not Self itself.
236 MDNode *Ref1 = MDNode::get(Context, Args);
237 MDNode *Ref2 = MDNode::get(Context, Args);
238 EXPECT_NE(Self, Ref1);
239 EXPECT_EQ(Ref1, Ref2);
243 TEST_F(MDNodeTest, Print) {
244 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
245 MDString *S = MDString::get(Context, "foo");
246 MDNode *N0 = getNode();
247 MDNode *N1 = getNode(N0);
248 MDNode *N2 = getNode(N0, N1);
250 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
251 MDNode *N = MDNode::get(Context, Args);
253 std::string Expected;
255 raw_string_ostream OS(Expected);
256 OS << "<" << (void *)N << "> = !{";
257 C->printAsOperand(OS);
259 S->printAsOperand(OS);
261 MDNode *Nodes[] = {N0, N1, N2};
262 for (auto *Node : Nodes)
263 OS << ", <" << (void *)Node << ">";
269 raw_string_ostream OS(Actual);
273 EXPECT_EQ(Expected, Actual);
276 #define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
278 std::string Actual_; \
279 raw_string_ostream OS(Actual_); \
282 std::string Expected_(EXPECTED); \
283 EXPECT_EQ(Expected_, Actual_); \
286 TEST_F(MDNodeTest, PrintTemporary) {
287 MDNode *Arg = getNode();
288 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
289 MDNode *N = getNode(Temp.get());
290 Module M("test", Context);
291 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
294 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
295 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
296 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
299 Temp->replaceAllUsesWith(Arg);
302 TEST_F(MDNodeTest, PrintFromModule) {
303 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
304 MDString *S = MDString::get(Context, "foo");
305 MDNode *N0 = getNode();
306 MDNode *N1 = getNode(N0);
307 MDNode *N2 = getNode(N0, N1);
309 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
310 MDNode *N = MDNode::get(Context, Args);
311 Module M("test", Context);
312 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
315 std::string Expected;
317 raw_string_ostream OS(Expected);
319 C->printAsOperand(OS);
321 S->printAsOperand(OS);
322 OS << ", null, !1, !2, !3}";
325 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
328 TEST_F(MDNodeTest, PrintFromFunction) {
329 Module M("test", Context);
330 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
331 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
332 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
333 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
334 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
335 auto *R0 = ReturnInst::Create(Context, BB0);
336 auto *R1 = ReturnInst::Create(Context, BB1);
337 auto *N0 = MDNode::getDistinct(Context, None);
338 auto *N1 = MDNode::getDistinct(Context, None);
339 R0->setMetadata("md", N0);
340 R1->setMetadata("md", N1);
342 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
343 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
346 TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
347 Module M("test", Context);
350 Function::Create(FunctionType::get(Type::getVoidTy(Context),
351 Type::getMetadataTy(Context), false),
352 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
354 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
355 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
356 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
357 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
358 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
359 auto *N0 = MDNode::getDistinct(Context, None);
360 auto *N1 = MDNode::getDistinct(Context, None);
361 auto *MAV0 = MetadataAsValue::get(Context, N0);
362 auto *MAV1 = MetadataAsValue::get(Context, N1);
363 CallInst::Create(Intrinsic, MAV0, "", BB0);
364 CallInst::Create(Intrinsic, MAV1, "", BB1);
366 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
367 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
368 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
369 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
370 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
371 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
373 #undef EXPECT_PRINTER_EQ
375 TEST_F(MDNodeTest, NullOperand) {
377 MDNode *Empty = MDNode::get(Context, None);
379 // metadata !{metadata !{}}
380 Metadata *Ops[] = {Empty};
381 MDNode *N = MDNode::get(Context, Ops);
382 ASSERT_EQ(Empty, N->getOperand(0));
384 // metadata !{metadata !{}} => metadata !{null}
385 N->replaceOperandWith(0, nullptr);
386 ASSERT_EQ(nullptr, N->getOperand(0));
390 MDNode *NullOp = MDNode::get(Context, Ops);
391 ASSERT_EQ(nullptr, NullOp->getOperand(0));
392 EXPECT_EQ(N, NullOp);
395 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
397 MDNode *Empty = MDNode::get(Context, None);
398 ASSERT_TRUE(Empty->isResolved());
399 EXPECT_FALSE(Empty->isDistinct());
402 Metadata *Wrapped1Ops[] = {Empty};
403 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
404 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
405 ASSERT_TRUE(Wrapped1->isResolved());
406 EXPECT_FALSE(Wrapped1->isDistinct());
409 Metadata *Wrapped2Ops[] = {Wrapped1};
410 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
411 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
412 ASSERT_TRUE(Wrapped2->isResolved());
413 EXPECT_FALSE(Wrapped2->isDistinct());
415 // !{!{!{}}} => !{!{}}
416 Wrapped2->replaceOperandWith(0, Empty);
417 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
418 EXPECT_TRUE(Wrapped2->isDistinct());
419 EXPECT_FALSE(Wrapped1->isDistinct());
422 TEST_F(MDNodeTest, getDistinct) {
424 MDNode *Empty = MDNode::get(Context, None);
425 ASSERT_TRUE(Empty->isResolved());
426 ASSERT_FALSE(Empty->isDistinct());
427 ASSERT_EQ(Empty, MDNode::get(Context, None));
430 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
431 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
432 EXPECT_TRUE(Distinct1->isResolved());
433 EXPECT_TRUE(Distinct2->isDistinct());
434 EXPECT_NE(Empty, Distinct1);
435 EXPECT_NE(Empty, Distinct2);
436 EXPECT_NE(Distinct1, Distinct2);
439 ASSERT_EQ(Empty, MDNode::get(Context, None));
442 TEST_F(MDNodeTest, isUniqued) {
443 MDNode *U = MDTuple::get(Context, None);
444 MDNode *D = MDTuple::getDistinct(Context, None);
445 auto T = MDTuple::getTemporary(Context, None);
446 EXPECT_TRUE(U->isUniqued());
447 EXPECT_FALSE(D->isUniqued());
448 EXPECT_FALSE(T->isUniqued());
451 TEST_F(MDNodeTest, isDistinct) {
452 MDNode *U = MDTuple::get(Context, None);
453 MDNode *D = MDTuple::getDistinct(Context, None);
454 auto T = MDTuple::getTemporary(Context, None);
455 EXPECT_FALSE(U->isDistinct());
456 EXPECT_TRUE(D->isDistinct());
457 EXPECT_FALSE(T->isDistinct());
460 TEST_F(MDNodeTest, isTemporary) {
461 MDNode *U = MDTuple::get(Context, None);
462 MDNode *D = MDTuple::getDistinct(Context, None);
463 auto T = MDTuple::getTemporary(Context, None);
464 EXPECT_FALSE(U->isTemporary());
465 EXPECT_FALSE(D->isTemporary());
466 EXPECT_TRUE(T->isTemporary());
469 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
471 auto Temp = MDTuple::getTemporary(Context, None);
472 ASSERT_FALSE(Temp->isResolved());
474 // distinct !{temporary !{}}
475 Metadata *Ops[] = {Temp.get()};
476 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
477 EXPECT_TRUE(Distinct->isResolved());
478 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
480 // temporary !{} => !{}
481 MDNode *Empty = MDNode::get(Context, None);
482 Temp->replaceAllUsesWith(Empty);
483 EXPECT_EQ(Empty, Distinct->getOperand(0));
486 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
488 MDNode *N0 = MDNode::get(Context, None);
491 auto Temp3 = MDTuple::getTemporary(Context, None);
492 Metadata *Ops1[] = {Temp3.get(), nullptr};
493 MDNode *N1 = MDNode::get(Context, Ops1);
496 Metadata *Ops2[] = {Temp3.get(), N0};
497 MDNode *N2 = MDNode::get(Context, Ops2);
500 Metadata *Ops3[] = {N2};
501 MDNode *N3 = MDNode::get(Context, Ops3);
502 Temp3->replaceAllUsesWith(N3);
505 Metadata *Ops4[] = {N1};
506 MDNode *N4 = MDNode::get(Context, Ops4);
508 // Confirm that the cycle prevented RAUW from getting dropped.
509 EXPECT_TRUE(N0->isResolved());
510 EXPECT_FALSE(N1->isResolved());
511 EXPECT_FALSE(N2->isResolved());
512 EXPECT_FALSE(N3->isResolved());
513 EXPECT_FALSE(N4->isResolved());
515 // Create a couple of distinct nodes to observe what's going on.
517 // !5 = distinct !{!2}
518 // !6 = distinct !{!3}
519 Metadata *Ops5[] = {N2};
520 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
521 Metadata *Ops6[] = {N3};
522 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
524 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
525 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
526 // references !3, this can cause a re-entry of handleChangedOperand() when !3
527 // is not ready for it.
529 // !2->replaceOperandWith(1, nullptr)
530 // !2: !{!3, !0} => !{!3, null}
531 // !2->replaceAllUsesWith(!1)
532 // !3: !{!2] => !{!1}
533 // !3->replaceAllUsesWith(!4)
534 N2->replaceOperandWith(1, nullptr);
536 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
537 // under us. Just check that the other nodes are sane.
541 // !5 = distinct !{!1}
542 // !6 = distinct !{!4}
543 EXPECT_EQ(N4, N1->getOperand(0));
544 EXPECT_EQ(N1, N4->getOperand(0));
545 EXPECT_EQ(N1, N5->getOperand(0));
546 EXPECT_EQ(N4, N6->getOperand(0));
549 TEST_F(MDNodeTest, replaceResolvedOperand) {
550 // Check code for replacing one resolved operand with another. If doing this
551 // directly (via replaceOperandWith()) becomes illegal, change the operand to
552 // a global value that gets RAUW'ed.
554 // Use a temporary node to keep N from being resolved.
555 auto Temp = MDTuple::getTemporary(Context, None);
556 Metadata *Ops[] = {nullptr, Temp.get()};
558 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
559 MDNode *N = MDTuple::get(Context, Ops);
560 EXPECT_EQ(nullptr, N->getOperand(0));
561 ASSERT_FALSE(N->isResolved());
563 // Check code for replacing resolved nodes.
564 N->replaceOperandWith(0, Empty);
565 EXPECT_EQ(Empty, N->getOperand(0));
567 // Check code for adding another unresolved operand.
568 N->replaceOperandWith(0, Temp.get());
569 EXPECT_EQ(Temp.get(), N->getOperand(0));
571 // Remove the references to Temp; required for teardown.
572 Temp->replaceAllUsesWith(nullptr);
575 TEST_F(MDNodeTest, replaceWithUniqued) {
576 auto *Empty = MDTuple::get(Context, None);
577 MDTuple *FirstUniqued;
579 Metadata *Ops[] = {Empty};
580 auto Temp = MDTuple::getTemporary(Context, Ops);
581 EXPECT_TRUE(Temp->isTemporary());
583 // Don't expect a collision.
584 auto *Current = Temp.get();
585 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
586 EXPECT_TRUE(FirstUniqued->isUniqued());
587 EXPECT_TRUE(FirstUniqued->isResolved());
588 EXPECT_EQ(Current, FirstUniqued);
591 Metadata *Ops[] = {Empty};
592 auto Temp = MDTuple::getTemporary(Context, Ops);
593 EXPECT_TRUE(Temp->isTemporary());
595 // Should collide with Uniqued above this time.
596 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
597 EXPECT_TRUE(Uniqued->isUniqued());
598 EXPECT_TRUE(Uniqued->isResolved());
599 EXPECT_EQ(FirstUniqued, Uniqued);
602 auto Unresolved = MDTuple::getTemporary(Context, None);
603 Metadata *Ops[] = {Unresolved.get()};
604 auto Temp = MDTuple::getTemporary(Context, Ops);
605 EXPECT_TRUE(Temp->isTemporary());
607 // Shouldn't be resolved.
608 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
609 EXPECT_TRUE(Uniqued->isUniqued());
610 EXPECT_FALSE(Uniqued->isResolved());
612 // Should be a different node.
613 EXPECT_NE(FirstUniqued, Uniqued);
615 // Should resolve when we update its node (note: be careful to avoid a
616 // collision with any other nodes above).
617 Uniqued->replaceOperandWith(0, nullptr);
618 EXPECT_TRUE(Uniqued->isResolved());
622 TEST_F(MDNodeTest, replaceWithDistinct) {
624 auto *Empty = MDTuple::get(Context, None);
625 Metadata *Ops[] = {Empty};
626 auto Temp = MDTuple::getTemporary(Context, Ops);
627 EXPECT_TRUE(Temp->isTemporary());
629 // Don't expect a collision.
630 auto *Current = Temp.get();
631 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
632 EXPECT_TRUE(Distinct->isDistinct());
633 EXPECT_TRUE(Distinct->isResolved());
634 EXPECT_EQ(Current, Distinct);
637 auto Unresolved = MDTuple::getTemporary(Context, None);
638 Metadata *Ops[] = {Unresolved.get()};
639 auto Temp = MDTuple::getTemporary(Context, Ops);
640 EXPECT_TRUE(Temp->isTemporary());
642 // Don't expect a collision.
643 auto *Current = Temp.get();
644 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
645 EXPECT_TRUE(Distinct->isDistinct());
646 EXPECT_TRUE(Distinct->isResolved());
647 EXPECT_EQ(Current, Distinct);
649 // Cleanup; required for teardown.
650 Unresolved->replaceAllUsesWith(nullptr);
654 TEST_F(MDNodeTest, replaceWithPermanent) {
655 Metadata *Ops[] = {nullptr};
656 auto Temp = MDTuple::getTemporary(Context, Ops);
657 auto *T = Temp.get();
659 // U is a normal, uniqued node that references T.
660 auto *U = MDTuple::get(Context, T);
661 EXPECT_TRUE(U->isUniqued());
663 // Make Temp self-referencing.
664 Temp->replaceOperandWith(0, T);
666 // Try to uniquify Temp. This should, despite the name in the API, give a
667 // 'distinct' node, since self-references aren't allowed to be uniqued.
669 // Since it's distinct, N should have the same address as when it was a
670 // temporary (i.e., be equal to T not U).
671 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
673 EXPECT_TRUE(N->isDistinct());
675 // U should be the canonical unique node with N as the argument.
676 EXPECT_EQ(U, MDTuple::get(Context, N));
677 EXPECT_TRUE(U->isUniqued());
679 // This temporary should collide with U when replaced, but it should still be
681 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
682 EXPECT_TRUE(U->isUniqued());
684 // This temporary should become a new uniqued node.
685 auto Temp2 = MDTuple::getTemporary(Context, U);
686 auto *V = Temp2.get();
687 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
688 EXPECT_TRUE(V->isUniqued());
689 EXPECT_EQ(U, V->getOperand(0));
692 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
694 EXPECT_EQ(nullptr, Ref.get());
696 auto Temp = MDTuple::getTemporary(Context, None);
697 Ref.reset(Temp.get());
698 EXPECT_EQ(Temp.get(), Ref.get());
700 EXPECT_EQ(nullptr, Ref.get());
703 typedef MetadataTest MDLocationTest;
705 TEST_F(MDLocationTest, Overflow) {
706 MDSubprogram *N = getSubprogram();
708 MDLocation *L = MDLocation::get(Context, 2, 7, N);
709 EXPECT_EQ(2u, L->getLine());
710 EXPECT_EQ(7u, L->getColumn());
712 unsigned U16 = 1u << 16;
714 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
715 EXPECT_EQ(UINT32_MAX, L->getLine());
716 EXPECT_EQ(U16 - 1, L->getColumn());
719 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
720 EXPECT_EQ(UINT32_MAX, L->getLine());
721 EXPECT_EQ(0u, L->getColumn());
724 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
725 EXPECT_EQ(UINT32_MAX, L->getLine());
726 EXPECT_EQ(0u, L->getColumn());
730 TEST_F(MDLocationTest, getDistinct) {
731 MDNode *N = getSubprogram();
732 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
733 EXPECT_TRUE(L0->isDistinct());
734 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
735 EXPECT_FALSE(L1->isDistinct());
736 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
739 TEST_F(MDLocationTest, getTemporary) {
740 MDNode *N = MDNode::get(Context, None);
741 auto L = MDLocation::getTemporary(Context, 2, 7, N);
742 EXPECT_TRUE(L->isTemporary());
743 EXPECT_FALSE(L->isResolved());
746 typedef MetadataTest GenericDebugNodeTest;
748 TEST_F(GenericDebugNodeTest, get) {
749 StringRef Header = "header";
750 auto *Empty = MDNode::get(Context, None);
751 Metadata *Ops1[] = {Empty};
752 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
753 EXPECT_EQ(15u, N->getTag());
754 EXPECT_EQ(2u, N->getNumOperands());
755 EXPECT_EQ(Header, N->getHeader());
756 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
757 EXPECT_EQ(1u, N->getNumDwarfOperands());
758 EXPECT_EQ(Empty, N->getDwarfOperand(0));
759 EXPECT_EQ(Empty, N->getOperand(1));
760 ASSERT_TRUE(N->isUniqued());
762 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
764 N->replaceOperandWith(1, nullptr);
765 EXPECT_EQ(15u, N->getTag());
766 EXPECT_EQ(Header, N->getHeader());
767 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
768 ASSERT_TRUE(N->isUniqued());
770 Metadata *Ops2[] = {nullptr};
771 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
773 N->replaceDwarfOperandWith(0, Empty);
774 EXPECT_EQ(15u, N->getTag());
775 EXPECT_EQ(Header, N->getHeader());
776 EXPECT_EQ(Empty, N->getDwarfOperand(0));
777 ASSERT_TRUE(N->isUniqued());
778 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
780 TempGenericDebugNode Temp = N->clone();
781 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
784 TEST_F(GenericDebugNodeTest, getEmptyHeader) {
785 // Canonicalize !"" to null.
786 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
787 EXPECT_EQ(StringRef(), N->getHeader());
788 EXPECT_EQ(nullptr, N->getOperand(0));
791 typedef MetadataTest MDSubrangeTest;
793 TEST_F(MDSubrangeTest, get) {
794 auto *N = MDSubrange::get(Context, 5, 7);
795 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
796 EXPECT_EQ(5, N->getCount());
797 EXPECT_EQ(7, N->getLo());
798 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
799 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
801 TempMDSubrange Temp = N->clone();
802 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
805 TEST_F(MDSubrangeTest, getEmptyArray) {
806 auto *N = MDSubrange::get(Context, -1, 0);
807 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
808 EXPECT_EQ(-1, N->getCount());
809 EXPECT_EQ(0, N->getLo());
810 EXPECT_EQ(N, MDSubrange::get(Context, -1, 0));
813 typedef MetadataTest MDEnumeratorTest;
815 TEST_F(MDEnumeratorTest, get) {
816 auto *N = MDEnumerator::get(Context, 7, "name");
817 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
818 EXPECT_EQ(7, N->getValue());
819 EXPECT_EQ("name", N->getName());
820 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
822 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
823 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
825 TempMDEnumerator Temp = N->clone();
826 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
829 typedef MetadataTest MDBasicTypeTest;
831 TEST_F(MDBasicTypeTest, get) {
833 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
834 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
835 EXPECT_EQ("special", N->getName());
836 EXPECT_EQ(33u, N->getSizeInBits());
837 EXPECT_EQ(26u, N->getAlignInBits());
838 EXPECT_EQ(7u, N->getEncoding());
839 EXPECT_EQ(0u, N->getLine());
840 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
843 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
844 "special", 33, 26, 7));
846 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
847 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
849 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
851 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
854 TempMDBasicType Temp = N->clone();
855 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
858 TEST_F(MDBasicTypeTest, getWithLargeValues) {
859 auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
860 UINT64_MAX, UINT64_MAX - 1, 7);
861 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
862 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
865 TEST_F(MDBasicTypeTest, getUnspecified) {
867 MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
868 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
869 EXPECT_EQ("unspecified", N->getName());
870 EXPECT_EQ(0u, N->getSizeInBits());
871 EXPECT_EQ(0u, N->getAlignInBits());
872 EXPECT_EQ(0u, N->getEncoding());
873 EXPECT_EQ(0u, N->getLine());
876 typedef MetadataTest MDTypeTest;
878 TEST_F(MDTypeTest, clone) {
879 // Check that MDType has a specialized clone that returns TempMDType.
880 MDType *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
881 dwarf::DW_ATE_signed);
883 TempMDType Temp = N->clone();
884 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
887 TEST_F(MDTypeTest, setFlags) {
889 Metadata *TypesOps[] = {nullptr};
890 Metadata *Types = MDTuple::get(Context, TypesOps);
892 MDType *D = MDSubroutineType::getDistinct(Context, 0u, Types);
893 EXPECT_EQ(0u, D->getFlags());
894 D->setFlags(DIDescriptor::FlagRValueReference);
895 EXPECT_EQ(DIDescriptor::FlagRValueReference, D->getFlags());
897 EXPECT_EQ(0u, D->getFlags());
899 TempMDType T = MDSubroutineType::getTemporary(Context, 0u, Types);
900 EXPECT_EQ(0u, T->getFlags());
901 T->setFlags(DIDescriptor::FlagRValueReference);
902 EXPECT_EQ(DIDescriptor::FlagRValueReference, T->getFlags());
904 EXPECT_EQ(0u, T->getFlags());
907 typedef MetadataTest MDDerivedTypeTest;
909 TEST_F(MDDerivedTypeTest, get) {
910 MDFile *File = getFile();
911 MDScope *Scope = getSubprogram();
912 MDType *BaseType = getBasicType("basic");
913 MDTuple *ExtraData = getTuple();
915 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
916 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
917 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
918 EXPECT_EQ("something", N->getName());
919 EXPECT_EQ(File, N->getFile());
920 EXPECT_EQ(1u, N->getLine());
921 EXPECT_EQ(Scope, N->getScope());
922 EXPECT_EQ(BaseType, N->getBaseType());
923 EXPECT_EQ(2u, N->getSizeInBits());
924 EXPECT_EQ(3u, N->getAlignInBits());
925 EXPECT_EQ(4u, N->getOffsetInBits());
926 EXPECT_EQ(5u, N->getFlags());
927 EXPECT_EQ(ExtraData, N->getExtraData());
928 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
929 "something", File, 1, Scope, BaseType, 2, 3,
932 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
933 "something", File, 1, Scope, BaseType, 2, 3,
935 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
936 File, 1, Scope, BaseType, 2, 3, 4, 5,
938 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
939 "something", getFile(), 1, Scope, BaseType, 2,
940 3, 4, 5, ExtraData));
941 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
942 "something", File, 2, Scope, BaseType, 2, 3,
944 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
945 "something", File, 1, getSubprogram(),
946 BaseType, 2, 3, 4, 5, ExtraData));
947 EXPECT_NE(N, MDDerivedType::get(
948 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
949 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
950 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
951 "something", File, 1, Scope, BaseType, 3, 3,
953 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
954 "something", File, 1, Scope, BaseType, 2, 2,
956 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
957 "something", File, 1, Scope, BaseType, 2, 3,
959 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
960 "something", File, 1, Scope, BaseType, 2, 3,
962 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
963 "something", File, 1, Scope, BaseType, 2, 3,
966 TempMDDerivedType Temp = N->clone();
967 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
970 TEST_F(MDDerivedTypeTest, getWithLargeValues) {
971 MDFile *File = getFile();
972 MDScope *Scope = getSubprogram();
973 MDType *BaseType = getBasicType("basic");
974 MDTuple *ExtraData = getTuple();
976 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
977 File, 1, Scope, BaseType, UINT64_MAX,
978 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
979 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
980 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
981 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
984 typedef MetadataTest MDCompositeTypeTest;
986 TEST_F(MDCompositeTypeTest, get) {
987 unsigned Tag = dwarf::DW_TAG_structure_type;
988 StringRef Name = "some name";
989 MDFile *File = getFile();
991 MDScope *Scope = getSubprogram();
992 MDType *BaseType = getCompositeType();
993 uint64_t SizeInBits = 2;
994 uint64_t AlignInBits = 3;
995 uint64_t OffsetInBits = 4;
997 MDTuple *Elements = getTuple();
998 unsigned RuntimeLang = 6;
999 MDType *VTableHolder = getCompositeType();
1000 MDTuple *TemplateParams = getTuple();
1001 StringRef Identifier = "some id";
1003 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1004 BaseType, SizeInBits, AlignInBits,
1005 OffsetInBits, Flags, Elements, RuntimeLang,
1006 VTableHolder, TemplateParams, Identifier);
1007 EXPECT_EQ(Tag, N->getTag());
1008 EXPECT_EQ(Name, N->getName());
1009 EXPECT_EQ(File, N->getFile());
1010 EXPECT_EQ(Line, N->getLine());
1011 EXPECT_EQ(Scope, N->getScope());
1012 EXPECT_EQ(BaseType, N->getBaseType());
1013 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1014 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1015 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1016 EXPECT_EQ(Flags, N->getFlags());
1017 EXPECT_EQ(Elements, N->getElements());
1018 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1019 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1020 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1021 EXPECT_EQ(Identifier, N->getIdentifier());
1023 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1024 BaseType, SizeInBits, AlignInBits,
1025 OffsetInBits, Flags, Elements, RuntimeLang,
1026 VTableHolder, TemplateParams, Identifier));
1028 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
1029 BaseType, SizeInBits, AlignInBits,
1030 OffsetInBits, Flags, Elements, RuntimeLang,
1031 VTableHolder, TemplateParams, Identifier));
1032 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
1033 BaseType, SizeInBits, AlignInBits,
1034 OffsetInBits, Flags, Elements, RuntimeLang,
1035 VTableHolder, TemplateParams, Identifier));
1036 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
1037 BaseType, SizeInBits, AlignInBits,
1038 OffsetInBits, Flags, Elements, RuntimeLang,
1039 VTableHolder, TemplateParams, Identifier));
1040 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
1041 BaseType, SizeInBits, AlignInBits,
1042 OffsetInBits, Flags, Elements, RuntimeLang,
1043 VTableHolder, TemplateParams, Identifier));
1044 EXPECT_NE(N, MDCompositeType::get(
1045 Context, Tag, Name, File, Line, getSubprogram(), BaseType,
1046 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1047 RuntimeLang, VTableHolder, TemplateParams, Identifier));
1048 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
1049 SizeInBits, AlignInBits, OffsetInBits,
1050 Flags, Elements, RuntimeLang, VTableHolder,
1051 TemplateParams, Identifier));
1052 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1053 BaseType, SizeInBits + 1, AlignInBits,
1054 OffsetInBits, Flags, Elements, RuntimeLang,
1055 VTableHolder, TemplateParams, Identifier));
1056 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1057 BaseType, SizeInBits, AlignInBits + 1,
1058 OffsetInBits, Flags, Elements, RuntimeLang,
1059 VTableHolder, TemplateParams, Identifier));
1060 EXPECT_NE(N, MDCompositeType::get(
1061 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1062 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1063 VTableHolder, TemplateParams, Identifier));
1064 EXPECT_NE(N, MDCompositeType::get(
1065 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1066 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1067 VTableHolder, TemplateParams, Identifier));
1068 EXPECT_NE(N, MDCompositeType::get(
1069 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1070 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1071 VTableHolder, TemplateParams, Identifier));
1072 EXPECT_NE(N, MDCompositeType::get(
1073 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1074 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1075 VTableHolder, TemplateParams, Identifier));
1076 EXPECT_NE(N, MDCompositeType::get(
1077 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1078 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1079 getCompositeType(), TemplateParams, Identifier));
1080 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1081 BaseType, SizeInBits, AlignInBits,
1082 OffsetInBits, Flags, Elements, RuntimeLang,
1083 VTableHolder, getTuple(), Identifier));
1084 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1085 BaseType, SizeInBits, AlignInBits,
1086 OffsetInBits, Flags, Elements, RuntimeLang,
1087 VTableHolder, TemplateParams, "other"));
1089 // Be sure that missing identifiers get null pointers.
1090 EXPECT_FALSE(MDCompositeType::get(
1091 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1092 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1093 VTableHolder, TemplateParams, "")->getRawIdentifier());
1094 EXPECT_FALSE(MDCompositeType::get(
1095 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1096 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1097 VTableHolder, TemplateParams)->getRawIdentifier());
1099 TempMDCompositeType Temp = N->clone();
1100 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1103 TEST_F(MDCompositeTypeTest, getWithLargeValues) {
1104 unsigned Tag = dwarf::DW_TAG_structure_type;
1105 StringRef Name = "some name";
1106 MDFile *File = getFile();
1108 MDScope *Scope = getSubprogram();
1109 MDType *BaseType = getCompositeType();
1110 uint64_t SizeInBits = UINT64_MAX;
1111 uint64_t AlignInBits = UINT64_MAX - 1;
1112 uint64_t OffsetInBits = UINT64_MAX - 2;
1114 MDTuple *Elements = getTuple();
1115 unsigned RuntimeLang = 6;
1116 MDType *VTableHolder = getCompositeType();
1117 MDTuple *TemplateParams = getTuple();
1118 StringRef Identifier = "some id";
1120 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1121 BaseType, SizeInBits, AlignInBits,
1122 OffsetInBits, Flags, Elements, RuntimeLang,
1123 VTableHolder, TemplateParams, Identifier);
1124 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1125 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1126 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1129 TEST_F(MDCompositeTypeTest, replaceOperands) {
1130 unsigned Tag = dwarf::DW_TAG_structure_type;
1131 StringRef Name = "some name";
1132 MDFile *File = getFile();
1134 MDScope *Scope = getSubprogram();
1135 MDType *BaseType = getCompositeType();
1136 uint64_t SizeInBits = 2;
1137 uint64_t AlignInBits = 3;
1138 uint64_t OffsetInBits = 4;
1140 unsigned RuntimeLang = 6;
1141 StringRef Identifier = "some id";
1143 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1144 BaseType, SizeInBits, AlignInBits,
1145 OffsetInBits, Flags, nullptr, RuntimeLang,
1146 nullptr, nullptr, Identifier);
1148 auto *Elements = MDTuple::getDistinct(Context, None);
1149 EXPECT_EQ(nullptr, N->getElements());
1150 N->replaceElements(Elements);
1151 EXPECT_EQ(Elements, N->getElements());
1152 N->replaceElements(nullptr);
1153 EXPECT_EQ(nullptr, N->getElements());
1155 auto *VTableHolder = MDTuple::getDistinct(Context, None);
1156 EXPECT_EQ(nullptr, N->getVTableHolder());
1157 N->replaceVTableHolder(VTableHolder);
1158 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1159 N->replaceVTableHolder(nullptr);
1160 EXPECT_EQ(nullptr, N->getVTableHolder());
1162 auto *TemplateParams = MDTuple::getDistinct(Context, None);
1163 EXPECT_EQ(nullptr, N->getTemplateParams());
1164 N->replaceTemplateParams(TemplateParams);
1165 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1166 N->replaceTemplateParams(nullptr);
1167 EXPECT_EQ(nullptr, N->getTemplateParams());
1170 typedef MetadataTest MDSubroutineTypeTest;
1172 TEST_F(MDSubroutineTypeTest, get) {
1174 MDTuple *TypeArray = getTuple();
1176 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
1177 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1178 EXPECT_EQ(Flags, N->getFlags());
1179 EXPECT_EQ(TypeArray, N->getTypeArray());
1180 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
1182 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
1183 EXPECT_NE(N, MDSubroutineType::get(Context, Flags, getTuple()));
1185 TempMDSubroutineType Temp = N->clone();
1186 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1188 // Test always-empty operands.
1189 EXPECT_EQ(nullptr, N->getScope());
1190 EXPECT_EQ(nullptr, N->getFile());
1191 EXPECT_EQ("", N->getName());
1192 EXPECT_EQ(nullptr, N->getBaseType());
1193 EXPECT_EQ(nullptr, N->getVTableHolder());
1194 EXPECT_EQ(nullptr, N->getTemplateParams());
1195 EXPECT_EQ("", N->getIdentifier());
1198 typedef MetadataTest MDFileTest;
1200 TEST_F(MDFileTest, get) {
1201 StringRef Filename = "file";
1202 StringRef Directory = "dir";
1203 auto *N = MDFile::get(Context, Filename, Directory);
1205 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1206 EXPECT_EQ(Filename, N->getFilename());
1207 EXPECT_EQ(Directory, N->getDirectory());
1208 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
1210 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1211 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
1213 TempMDFile Temp = N->clone();
1214 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1217 TEST_F(MDFileTest, ScopeGetFile) {
1218 // Ensure that MDScope::getFile() returns itself.
1219 MDScope *N = MDFile::get(Context, "file", "dir");
1220 EXPECT_EQ(N, N->getFile());
1223 typedef MetadataTest MDCompileUnitTest;
1225 TEST_F(MDCompileUnitTest, get) {
1226 unsigned SourceLanguage = 1;
1227 MDFile *File = getFile();
1228 StringRef Producer = "some producer";
1229 bool IsOptimized = false;
1230 StringRef Flags = "flag after flag";
1231 unsigned RuntimeVersion = 2;
1232 StringRef SplitDebugFilename = "another/file";
1233 unsigned EmissionKind = 3;
1234 MDTuple *EnumTypes = getTuple();
1235 MDTuple *RetainedTypes = getTuple();
1236 MDTuple *Subprograms = getTuple();
1237 MDTuple *GlobalVariables = getTuple();
1238 MDTuple *ImportedEntities = getTuple();
1239 auto *N = MDCompileUnit::get(
1240 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1241 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1242 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1244 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1245 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1246 EXPECT_EQ(File, N->getFile());
1247 EXPECT_EQ(Producer, N->getProducer());
1248 EXPECT_EQ(IsOptimized, N->isOptimized());
1249 EXPECT_EQ(Flags, N->getFlags());
1250 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1251 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1252 EXPECT_EQ(EmissionKind, N->getEmissionKind());
1253 EXPECT_EQ(EnumTypes, N->getEnumTypes());
1254 EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
1255 EXPECT_EQ(Subprograms, N->getSubprograms());
1256 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1257 EXPECT_EQ(ImportedEntities, N->getImportedEntities());
1258 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1259 IsOptimized, Flags, RuntimeVersion,
1260 SplitDebugFilename, EmissionKind, EnumTypes,
1261 RetainedTypes, Subprograms, GlobalVariables,
1264 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1265 IsOptimized, Flags, RuntimeVersion,
1266 SplitDebugFilename, EmissionKind, EnumTypes,
1267 RetainedTypes, Subprograms, GlobalVariables,
1269 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, getFile(), Producer,
1270 IsOptimized, Flags, RuntimeVersion,
1271 SplitDebugFilename, EmissionKind, EnumTypes,
1272 RetainedTypes, Subprograms, GlobalVariables,
1274 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1275 IsOptimized, Flags, RuntimeVersion,
1276 SplitDebugFilename, EmissionKind, EnumTypes,
1277 RetainedTypes, Subprograms, GlobalVariables,
1279 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1280 !IsOptimized, Flags, RuntimeVersion,
1281 SplitDebugFilename, EmissionKind, EnumTypes,
1282 RetainedTypes, Subprograms, GlobalVariables,
1284 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1285 IsOptimized, "other", RuntimeVersion,
1286 SplitDebugFilename, EmissionKind, EnumTypes,
1287 RetainedTypes, Subprograms, GlobalVariables,
1289 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1290 IsOptimized, Flags, RuntimeVersion + 1,
1291 SplitDebugFilename, EmissionKind, EnumTypes,
1292 RetainedTypes, Subprograms, GlobalVariables,
1295 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1296 IsOptimized, Flags, RuntimeVersion, "other",
1297 EmissionKind, EnumTypes, RetainedTypes,
1298 Subprograms, GlobalVariables, ImportedEntities));
1299 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1300 IsOptimized, Flags, RuntimeVersion,
1301 SplitDebugFilename, EmissionKind + 1,
1302 EnumTypes, RetainedTypes, Subprograms,
1303 GlobalVariables, ImportedEntities));
1304 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1305 IsOptimized, Flags, RuntimeVersion,
1306 SplitDebugFilename, EmissionKind, getTuple(),
1307 RetainedTypes, Subprograms, GlobalVariables,
1309 EXPECT_NE(N, MDCompileUnit::get(
1310 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1311 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1312 getTuple(), Subprograms, GlobalVariables, ImportedEntities));
1313 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1314 IsOptimized, Flags, RuntimeVersion,
1315 SplitDebugFilename, EmissionKind, EnumTypes,
1316 RetainedTypes, getTuple(), GlobalVariables,
1318 EXPECT_NE(N, MDCompileUnit::get(
1319 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1320 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1321 RetainedTypes, Subprograms, getTuple(), ImportedEntities));
1322 EXPECT_NE(N, MDCompileUnit::get(
1323 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1324 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1325 RetainedTypes, Subprograms, GlobalVariables, getTuple()));
1327 TempMDCompileUnit Temp = N->clone();
1328 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1331 TEST_F(MDCompileUnitTest, replaceArrays) {
1332 unsigned SourceLanguage = 1;
1333 MDFile *File = getFile();
1334 StringRef Producer = "some producer";
1335 bool IsOptimized = false;
1336 StringRef Flags = "flag after flag";
1337 unsigned RuntimeVersion = 2;
1338 StringRef SplitDebugFilename = "another/file";
1339 unsigned EmissionKind = 3;
1340 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1341 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1342 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
1343 auto *N = MDCompileUnit::get(
1344 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1345 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1346 RetainedTypes, nullptr, nullptr, ImportedEntities);
1348 auto *Subprograms = MDTuple::getDistinct(Context, None);
1349 EXPECT_EQ(nullptr, N->getSubprograms());
1350 N->replaceSubprograms(Subprograms);
1351 EXPECT_EQ(Subprograms, N->getSubprograms());
1352 N->replaceSubprograms(nullptr);
1353 EXPECT_EQ(nullptr, N->getSubprograms());
1355 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1356 EXPECT_EQ(nullptr, N->getGlobalVariables());
1357 N->replaceGlobalVariables(GlobalVariables);
1358 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1359 N->replaceGlobalVariables(nullptr);
1360 EXPECT_EQ(nullptr, N->getGlobalVariables());
1363 typedef MetadataTest MDSubprogramTest;
1365 TEST_F(MDSubprogramTest, get) {
1366 Metadata *Scope = MDTuple::getDistinct(Context, None);
1367 StringRef Name = "name";
1368 StringRef LinkageName = "linkage";
1369 Metadata *File = MDTuple::getDistinct(Context, None);
1371 Metadata *Type = MDTuple::getDistinct(Context, None);
1372 bool IsLocalToUnit = false;
1373 bool IsDefinition = true;
1374 unsigned ScopeLine = 3;
1375 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1376 unsigned Virtuality = 4;
1377 unsigned VirtualIndex = 5;
1379 bool IsOptimized = false;
1380 Metadata *Function = MDTuple::getDistinct(Context, None);
1381 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1382 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1383 Metadata *Variables = MDTuple::getDistinct(Context, None);
1385 auto *N = MDSubprogram::get(
1386 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1387 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1388 IsOptimized, Function, TemplateParams, Declaration, Variables);
1390 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1391 EXPECT_EQ(Scope, N->getScope());
1392 EXPECT_EQ(Name, N->getName());
1393 EXPECT_EQ(LinkageName, N->getLinkageName());
1394 EXPECT_EQ(File, N->getFile());
1395 EXPECT_EQ(Line, N->getLine());
1396 EXPECT_EQ(Type, N->getType());
1397 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1398 EXPECT_EQ(IsDefinition, N->isDefinition());
1399 EXPECT_EQ(ScopeLine, N->getScopeLine());
1400 EXPECT_EQ(ContainingType, N->getContainingType());
1401 EXPECT_EQ(Virtuality, N->getVirtuality());
1402 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1403 EXPECT_EQ(Flags, N->getFlags());
1404 EXPECT_EQ(IsOptimized, N->isOptimized());
1405 EXPECT_EQ(Function, N->getFunction());
1406 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1407 EXPECT_EQ(Declaration, N->getDeclaration());
1408 EXPECT_EQ(Variables, N->getVariables());
1409 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1410 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1411 ContainingType, Virtuality, VirtualIndex,
1412 Flags, IsOptimized, Function, TemplateParams,
1413 Declaration, Variables));
1415 EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1416 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1417 ContainingType, Virtuality, VirtualIndex,
1418 Flags, IsOptimized, Function, TemplateParams,
1419 Declaration, Variables));
1420 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1421 Line, Type, IsLocalToUnit, IsDefinition,
1422 ScopeLine, ContainingType, Virtuality,
1423 VirtualIndex, Flags, IsOptimized, Function,
1424 TemplateParams, Declaration, Variables));
1425 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1426 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1427 ContainingType, Virtuality, VirtualIndex,
1428 Flags, IsOptimized, Function, TemplateParams,
1429 Declaration, Variables));
1430 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1431 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1432 ContainingType, Virtuality, VirtualIndex,
1433 Flags, IsOptimized, Function, TemplateParams,
1434 Declaration, Variables));
1435 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1436 Line + 1, Type, IsLocalToUnit, IsDefinition,
1437 ScopeLine, ContainingType, Virtuality,
1438 VirtualIndex, Flags, IsOptimized, Function,
1439 TemplateParams, Declaration, Variables));
1440 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1441 Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1442 ContainingType, Virtuality, VirtualIndex,
1443 Flags, IsOptimized, Function, TemplateParams,
1444 Declaration, Variables));
1445 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1446 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1447 ContainingType, Virtuality, VirtualIndex,
1448 Flags, IsOptimized, Function, TemplateParams,
1449 Declaration, Variables));
1450 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1451 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1452 ContainingType, Virtuality, VirtualIndex,
1453 Flags, IsOptimized, Function, TemplateParams,
1454 Declaration, Variables));
1455 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1456 Type, IsLocalToUnit, IsDefinition,
1457 ScopeLine + 1, ContainingType, Virtuality,
1458 VirtualIndex, Flags, IsOptimized, Function,
1459 TemplateParams, Declaration, Variables));
1460 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1461 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1462 Type, Virtuality, VirtualIndex, Flags,
1463 IsOptimized, Function, TemplateParams,
1464 Declaration, Variables));
1465 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1466 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1467 ContainingType, Virtuality + 1, VirtualIndex,
1468 Flags, IsOptimized, Function, TemplateParams,
1469 Declaration, Variables));
1470 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1471 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1472 ContainingType, Virtuality, VirtualIndex + 1,
1473 Flags, IsOptimized, Function, TemplateParams,
1474 Declaration, Variables));
1475 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1476 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1477 ContainingType, Virtuality, VirtualIndex,
1478 ~Flags, IsOptimized, Function, TemplateParams,
1479 Declaration, Variables));
1480 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1481 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1482 ContainingType, Virtuality, VirtualIndex,
1483 Flags, !IsOptimized, Function, TemplateParams,
1484 Declaration, Variables));
1485 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1486 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1487 ContainingType, Virtuality, VirtualIndex,
1488 Flags, IsOptimized, Type, TemplateParams,
1489 Declaration, Variables));
1490 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1491 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1492 ContainingType, Virtuality, VirtualIndex,
1493 Flags, IsOptimized, Function, Type,
1494 Declaration, Variables));
1495 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1496 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1497 ContainingType, Virtuality, VirtualIndex,
1498 Flags, IsOptimized, Function, TemplateParams,
1500 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1501 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1502 ContainingType, Virtuality, VirtualIndex,
1503 Flags, IsOptimized, Function, TemplateParams,
1504 Declaration, Type));
1506 TempMDSubprogram Temp = N->clone();
1507 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1510 TEST_F(MDSubprogramTest, replaceFunction) {
1511 Metadata *Scope = MDTuple::getDistinct(Context, None);
1512 StringRef Name = "name";
1513 StringRef LinkageName = "linkage";
1514 Metadata *File = MDTuple::getDistinct(Context, None);
1516 Metadata *Type = MDTuple::getDistinct(Context, None);
1517 bool IsLocalToUnit = false;
1518 bool IsDefinition = true;
1519 unsigned ScopeLine = 3;
1520 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1521 unsigned Virtuality = 4;
1522 unsigned VirtualIndex = 5;
1524 bool IsOptimized = false;
1525 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1526 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1527 Metadata *Variables = MDTuple::getDistinct(Context, None);
1529 auto *N = MDSubprogram::get(
1530 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1531 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1532 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1534 EXPECT_EQ(nullptr, N->getFunction());
1536 std::unique_ptr<Function> F(
1537 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1538 GlobalValue::ExternalLinkage));
1539 N->replaceFunction(F.get());
1540 EXPECT_EQ(ConstantAsMetadata::get(F.get()), N->getFunction());
1542 N->replaceFunction(nullptr);
1543 EXPECT_EQ(nullptr, N->getFunction());
1546 typedef MetadataTest MDLexicalBlockTest;
1548 TEST_F(MDLexicalBlockTest, get) {
1549 Metadata *Scope = MDTuple::getDistinct(Context, None);
1550 Metadata *File = MDTuple::getDistinct(Context, None);
1552 unsigned Column = 8;
1554 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1556 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1557 EXPECT_EQ(Scope, N->getScope());
1558 EXPECT_EQ(File, N->getFile());
1559 EXPECT_EQ(Line, N->getLine());
1560 EXPECT_EQ(Column, N->getColumn());
1561 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1563 EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1564 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1565 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1566 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
1568 TempMDLexicalBlock Temp = N->clone();
1569 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1572 typedef MetadataTest MDLexicalBlockFileTest;
1574 TEST_F(MDLexicalBlockFileTest, get) {
1575 Metadata *Scope = MDTuple::getDistinct(Context, None);
1576 Metadata *File = MDTuple::getDistinct(Context, None);
1577 unsigned Discriminator = 5;
1579 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1581 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1582 EXPECT_EQ(Scope, N->getScope());
1583 EXPECT_EQ(File, N->getFile());
1584 EXPECT_EQ(Discriminator, N->getDiscriminator());
1585 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1587 EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1588 EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1590 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1592 TempMDLexicalBlockFile Temp = N->clone();
1593 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1596 typedef MetadataTest MDNamespaceTest;
1598 TEST_F(MDNamespaceTest, get) {
1599 Metadata *Scope = MDTuple::getDistinct(Context, None);
1600 Metadata *File = MDTuple::getDistinct(Context, None);
1601 StringRef Name = "namespace";
1604 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1606 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1607 EXPECT_EQ(Scope, N->getScope());
1608 EXPECT_EQ(File, N->getFile());
1609 EXPECT_EQ(Name, N->getName());
1610 EXPECT_EQ(Line, N->getLine());
1611 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1613 EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1614 EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1615 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1616 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
1618 TempMDNamespace Temp = N->clone();
1619 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1622 typedef MetadataTest MDTemplateTypeParameterTest;
1624 TEST_F(MDTemplateTypeParameterTest, get) {
1625 StringRef Name = "template";
1626 Metadata *Type = MDTuple::getDistinct(Context, None);
1627 Metadata *Other = MDTuple::getDistinct(Context, None);
1629 auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
1631 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1632 EXPECT_EQ(Name, N->getName());
1633 EXPECT_EQ(Type, N->getType());
1634 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type));
1636 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
1637 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Name, Other));
1639 TempMDTemplateTypeParameter Temp = N->clone();
1640 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1643 typedef MetadataTest MDTemplateValueParameterTest;
1645 TEST_F(MDTemplateValueParameterTest, get) {
1646 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1647 StringRef Name = "template";
1648 Metadata *Type = MDTuple::getDistinct(Context, None);
1649 Metadata *Value = MDTuple::getDistinct(Context, None);
1650 Metadata *Other = MDTuple::getDistinct(Context, None);
1652 auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
1653 EXPECT_EQ(Tag, N->getTag());
1654 EXPECT_EQ(Name, N->getName());
1655 EXPECT_EQ(Type, N->getType());
1656 EXPECT_EQ(Value, N->getValue());
1657 EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value));
1659 EXPECT_NE(N, MDTemplateValueParameter::get(
1660 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1662 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type,
1664 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Other,
1666 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Other));
1668 TempMDTemplateValueParameter Temp = N->clone();
1669 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1672 typedef MetadataTest MDGlobalVariableTest;
1674 TEST_F(MDGlobalVariableTest, get) {
1675 MDScope *Scope = getSubprogram();
1676 StringRef Name = "name";
1677 StringRef LinkageName = "linkage";
1678 MDFile *File = getFile();
1680 Metadata *Type = MDTuple::getDistinct(Context, None);
1681 bool IsLocalToUnit = false;
1682 bool IsDefinition = true;
1683 ConstantAsMetadata *Variable = getConstantAsMetadata();
1684 MDDerivedType *StaticDataMemberDeclaration = getDerivedType();
1686 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1687 Type, IsLocalToUnit, IsDefinition, Variable,
1688 StaticDataMemberDeclaration);
1689 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1690 EXPECT_EQ(Scope, N->getScope());
1691 EXPECT_EQ(Name, N->getName());
1692 EXPECT_EQ(LinkageName, N->getLinkageName());
1693 EXPECT_EQ(File, N->getFile());
1694 EXPECT_EQ(Line, N->getLine());
1695 EXPECT_EQ(Type, N->getType());
1696 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1697 EXPECT_EQ(IsDefinition, N->isDefinition());
1698 EXPECT_EQ(Variable, N->getVariable());
1699 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1700 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1701 Line, Type, IsLocalToUnit, IsDefinition,
1702 Variable, StaticDataMemberDeclaration));
1705 MDGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
1706 File, Line, Type, IsLocalToUnit, IsDefinition,
1707 Variable, StaticDataMemberDeclaration));
1708 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1709 Line, Type, IsLocalToUnit, IsDefinition,
1710 Variable, StaticDataMemberDeclaration));
1711 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1712 Type, IsLocalToUnit, IsDefinition,
1713 Variable, StaticDataMemberDeclaration));
1715 MDGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
1716 Line, Type, IsLocalToUnit, IsDefinition,
1717 Variable, StaticDataMemberDeclaration));
1719 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1720 Line + 1, Type, IsLocalToUnit, IsDefinition,
1721 Variable, StaticDataMemberDeclaration));
1722 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1723 Line, Scope, IsLocalToUnit, IsDefinition,
1724 Variable, StaticDataMemberDeclaration));
1725 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1726 Line, Type, !IsLocalToUnit, IsDefinition,
1727 Variable, StaticDataMemberDeclaration));
1728 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1729 Line, Type, IsLocalToUnit, !IsDefinition,
1730 Variable, StaticDataMemberDeclaration));
1731 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1732 Line, Type, IsLocalToUnit, IsDefinition,
1733 getConstantAsMetadata(),
1734 StaticDataMemberDeclaration));
1735 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1736 Line, Type, IsLocalToUnit, IsDefinition,
1737 Variable, getDerivedType()));
1739 TempMDGlobalVariable Temp = N->clone();
1740 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1743 typedef MetadataTest MDLocalVariableTest;
1745 TEST_F(MDLocalVariableTest, get) {
1746 unsigned Tag = dwarf::DW_TAG_arg_variable;
1747 MDLocalScope *Scope = getSubprogram();
1748 StringRef Name = "name";
1749 MDFile *File = getFile();
1751 Metadata *Type = MDTuple::getDistinct(Context, None);
1754 MDLocation *InlinedAt =
1755 MDLocation::getDistinct(Context, 10, 20, getSubprogram());
1757 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1758 Arg, Flags, InlinedAt);
1759 EXPECT_EQ(Tag, N->getTag());
1760 EXPECT_EQ(Scope, N->getScope());
1761 EXPECT_EQ(Name, N->getName());
1762 EXPECT_EQ(File, N->getFile());
1763 EXPECT_EQ(Line, N->getLine());
1764 EXPECT_EQ(Type, N->getType());
1765 EXPECT_EQ(Arg, N->getArg());
1766 EXPECT_EQ(Flags, N->getFlags());
1767 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1768 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1769 Arg, Flags, InlinedAt));
1771 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1772 Name, File, Line, Type, Arg, Flags,
1774 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, getSubprogram(), Name, File,
1775 Line, Type, Arg, Flags, InlinedAt));
1776 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1777 Type, Arg, Flags, InlinedAt));
1778 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, getFile(), Line,
1779 Type, Arg, Flags, InlinedAt));
1780 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1781 Type, Arg, Flags, InlinedAt));
1782 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1783 Scope, Arg, Flags, InlinedAt));
1784 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1785 Arg + 1, Flags, InlinedAt));
1786 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1787 Arg, ~Flags, InlinedAt));
1788 EXPECT_NE(N, MDLocalVariable::get(
1789 Context, Tag, Scope, Name, File, Line, Type, Arg, Flags,
1790 MDLocation::getDistinct(Context, 10, 20, getSubprogram())));
1792 TempMDLocalVariable Temp = N->clone();
1793 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1795 auto *Inlined = N->withoutInline();
1796 EXPECT_NE(N, Inlined);
1797 EXPECT_EQ(N->getTag(), Inlined->getTag());
1798 EXPECT_EQ(N->getScope(), Inlined->getScope());
1799 EXPECT_EQ(N->getName(), Inlined->getName());
1800 EXPECT_EQ(N->getFile(), Inlined->getFile());
1801 EXPECT_EQ(N->getLine(), Inlined->getLine());
1802 EXPECT_EQ(N->getType(), Inlined->getType());
1803 EXPECT_EQ(N->getArg(), Inlined->getArg());
1804 EXPECT_EQ(N->getFlags(), Inlined->getFlags());
1805 EXPECT_EQ(nullptr, Inlined->getInlinedAt());
1806 EXPECT_EQ(N, Inlined->withInline(cast<MDLocation>(InlinedAt)));
1809 typedef MetadataTest MDExpressionTest;
1811 TEST_F(MDExpressionTest, get) {
1812 uint64_t Elements[] = {2, 6, 9, 78, 0};
1813 auto *N = MDExpression::get(Context, Elements);
1814 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1815 EXPECT_EQ(N, MDExpression::get(Context, Elements));
1817 EXPECT_EQ(5u, N->getNumElements());
1818 EXPECT_EQ(2u, N->getElement(0));
1819 EXPECT_EQ(6u, N->getElement(1));
1820 EXPECT_EQ(9u, N->getElement(2));
1821 EXPECT_EQ(78u, N->getElement(3));
1822 EXPECT_EQ(0u, N->getElement(4));
1824 TempMDExpression Temp = N->clone();
1825 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1828 TEST_F(MDExpressionTest, isValid) {
1829 #define EXPECT_VALID(...) \
1831 uint64_t Elements[] = {__VA_ARGS__}; \
1832 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1834 #define EXPECT_INVALID(...) \
1836 uint64_t Elements[] = {__VA_ARGS__}; \
1837 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1840 // Empty expression should be valid.
1841 EXPECT_TRUE(MDExpression::get(Context, None));
1843 // Valid constructions.
1844 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1845 EXPECT_VALID(dwarf::DW_OP_deref);
1846 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1847 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1848 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1849 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1850 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1852 // Invalid constructions.
1853 EXPECT_INVALID(~0u);
1854 EXPECT_INVALID(dwarf::DW_OP_plus);
1855 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1856 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1857 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1858 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1861 #undef EXPECT_INVALID
1864 typedef MetadataTest MDObjCPropertyTest;
1866 TEST_F(MDObjCPropertyTest, get) {
1867 StringRef Name = "name";
1868 Metadata *File = MDTuple::getDistinct(Context, None);
1870 StringRef GetterName = "getter";
1871 StringRef SetterName = "setter";
1872 unsigned Attributes = 7;
1873 Metadata *Type = MDTuple::getDistinct(Context, None);
1875 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1876 SetterName, Attributes, Type);
1878 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1879 EXPECT_EQ(Name, N->getName());
1880 EXPECT_EQ(File, N->getFile());
1881 EXPECT_EQ(Line, N->getLine());
1882 EXPECT_EQ(GetterName, N->getGetterName());
1883 EXPECT_EQ(SetterName, N->getSetterName());
1884 EXPECT_EQ(Attributes, N->getAttributes());
1885 EXPECT_EQ(Type, N->getType());
1886 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1887 SetterName, Attributes, Type));
1889 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1890 SetterName, Attributes, Type));
1891 EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1892 SetterName, Attributes, Type));
1893 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1894 SetterName, Attributes, Type));
1895 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1896 SetterName, Attributes, Type));
1897 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1898 "other", Attributes, Type));
1899 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1900 SetterName, Attributes + 1, Type));
1901 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1902 SetterName, Attributes, File));
1904 TempMDObjCProperty Temp = N->clone();
1905 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1908 typedef MetadataTest MDImportedEntityTest;
1910 TEST_F(MDImportedEntityTest, get) {
1911 unsigned Tag = dwarf::DW_TAG_imported_module;
1912 Metadata *Scope = MDTuple::getDistinct(Context, None);
1913 Metadata *Entity = MDTuple::getDistinct(Context, None);
1915 StringRef Name = "name";
1917 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1919 EXPECT_EQ(Tag, N->getTag());
1920 EXPECT_EQ(Scope, N->getScope());
1921 EXPECT_EQ(Entity, N->getEntity());
1922 EXPECT_EQ(Line, N->getLine());
1923 EXPECT_EQ(Name, N->getName());
1924 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1927 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1928 Scope, Entity, Line, Name));
1929 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1930 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1932 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1934 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
1936 TempMDImportedEntity Temp = N->clone();
1937 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1940 typedef MetadataTest MetadataAsValueTest;
1942 TEST_F(MetadataAsValueTest, MDNode) {
1943 MDNode *N = MDNode::get(Context, None);
1944 auto *V = MetadataAsValue::get(Context, N);
1945 EXPECT_TRUE(V->getType()->isMetadataTy());
1946 EXPECT_EQ(N, V->getMetadata());
1948 auto *V2 = MetadataAsValue::get(Context, N);
1952 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1953 MDNode *N = MDNode::get(Context, None);
1954 Metadata *Ops[] = {N};
1955 MDNode *N2 = MDNode::get(Context, Ops);
1956 auto *V = MetadataAsValue::get(Context, N2);
1957 EXPECT_TRUE(V->getType()->isMetadataTy());
1958 EXPECT_EQ(N2, V->getMetadata());
1960 auto *V2 = MetadataAsValue::get(Context, N2);
1963 auto *V3 = MetadataAsValue::get(Context, N);
1964 EXPECT_TRUE(V3->getType()->isMetadataTy());
1966 EXPECT_EQ(N, V3->getMetadata());
1969 TEST_F(MetadataAsValueTest, MDNodeConstant) {
1970 auto *C = ConstantInt::getTrue(Context);
1971 auto *MD = ConstantAsMetadata::get(C);
1972 Metadata *Ops[] = {MD};
1973 auto *N = MDNode::get(Context, Ops);
1975 auto *V = MetadataAsValue::get(Context, MD);
1976 EXPECT_TRUE(V->getType()->isMetadataTy());
1977 EXPECT_EQ(MD, V->getMetadata());
1979 auto *V2 = MetadataAsValue::get(Context, N);
1980 EXPECT_EQ(MD, V2->getMetadata());
1984 typedef MetadataTest ValueAsMetadataTest;
1986 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1987 Type *Ty = Type::getInt1PtrTy(Context);
1988 std::unique_ptr<GlobalVariable> GV0(
1989 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1990 auto *MD = ValueAsMetadata::get(GV0.get());
1991 EXPECT_TRUE(MD->getValue() == GV0.get());
1992 ASSERT_TRUE(GV0->use_empty());
1994 std::unique_ptr<GlobalVariable> GV1(
1995 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1996 GV0->replaceAllUsesWith(GV1.get());
1997 EXPECT_TRUE(MD->getValue() == GV1.get());
2000 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2001 // Create a constant.
2002 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2003 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2005 // Create a temporary to prevent nodes from resolving.
2006 auto Temp = MDTuple::getTemporary(Context, None);
2008 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
2009 Metadata *Ops1[] = {CI, CI, Temp.get()};
2010 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
2012 auto *N1 = MDTuple::get(Context, Ops1);
2013 auto *N2 = MDTuple::get(Context, Ops2);
2016 // Tell metadata that the constant is getting deleted.
2018 // After this, N1 will be invalid, so don't touch it.
2019 ValueAsMetadata::handleDeletion(CI->getValue());
2020 EXPECT_EQ(nullptr, N2->getOperand(0));
2021 EXPECT_EQ(nullptr, N2->getOperand(1));
2022 EXPECT_EQ(Temp.get(), N2->getOperand(2));
2024 // Clean up Temp for teardown.
2025 Temp->replaceAllUsesWith(nullptr);
2028 typedef MetadataTest TrackingMDRefTest;
2030 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2031 Type *Ty = Type::getInt1PtrTy(Context);
2032 std::unique_ptr<GlobalVariable> GV0(
2033 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2034 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2035 EXPECT_TRUE(MD->getValue() == GV0.get());
2036 ASSERT_TRUE(GV0->use_empty());
2038 std::unique_ptr<GlobalVariable> GV1(
2039 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2040 GV0->replaceAllUsesWith(GV1.get());
2041 EXPECT_TRUE(MD->getValue() == GV1.get());
2043 // Reset it, so we don't inadvertently test deletion.
2047 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2048 Type *Ty = Type::getInt1PtrTy(Context);
2049 std::unique_ptr<GlobalVariable> GV(
2050 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2051 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2052 EXPECT_TRUE(MD->getValue() == GV.get());
2053 ASSERT_TRUE(GV->use_empty());
2059 TEST(NamedMDNodeTest, Search) {
2060 LLVMContext Context;
2061 ConstantAsMetadata *C =
2062 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2063 ConstantAsMetadata *C2 =
2064 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
2066 Metadata *const V = C;
2067 Metadata *const V2 = C2;
2068 MDNode *n = MDNode::get(Context, V);
2069 MDNode *n2 = MDNode::get(Context, V2);
2071 Module M("MyModule", Context);
2072 const char *Name = "llvm.NMD1";
2073 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2075 NMD->addOperand(n2);
2078 raw_string_ostream oss(Str);
2080 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",