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 MDSubprogram *getSubprogram() {
79 return MDSubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
80 nullptr, false, false, 0, nullptr, 0, 0, 0,
84 return MDFile::getDistinct(Context, "file.c", "/path/to/dir");
86 MDBasicType *getBasicType(StringRef Name) {
87 return MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
89 MDDerivedType *getDerivedType() {
90 return MDDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
92 getBasicType("basictype"), 1, 2, 0, 0);
94 ConstantAsMetadata *getConstantAsMetadata() {
95 return ConstantAsMetadata::get(
96 ConstantInt::get(Type::getInt32Ty(Context), Counter++));
99 typedef MetadataTest MDStringTest;
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));
107 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
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' };
117 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
118 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
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);
131 raw_string_ostream oss(Str);
133 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
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));
141 raw_string_ostream oss(Str);
143 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
146 typedef MetadataTest MDNodeTest;
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' };
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)));
158 std::vector<Metadata *> V;
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);
175 EXPECT_EQ(n6, (Metadata *)nullptr);
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));
182 EXPECT_EQ(1u, n2->getNumOperands());
183 EXPECT_EQ(n1, n2->getOperand(0));
186 TEST_F(MDNodeTest, Delete) {
187 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
188 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
190 Metadata *const V = LocalAsMetadata::get(I);
191 MDNode *n = MDNode::get(Context, V);
192 TrackingMDRef wvh(n);
199 TEST_F(MDNodeTest, SelfReference) {
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));
209 // Self-references should be distinct, so MDNode::get() should grab a
210 // uniqued node that references Self, not 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);
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));
227 // Self-references should be distinct, so MDNode::get() should grab a
228 // uniqued node that references Self, not Self itself.
230 MDNode *Ref1 = MDNode::get(Context, Args);
231 MDNode *Ref2 = MDNode::get(Context, Args);
232 EXPECT_NE(Self, Ref1);
233 EXPECT_EQ(Ref1, Ref2);
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);
244 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
245 MDNode *N = MDNode::get(Context, Args);
247 std::string Expected;
249 raw_string_ostream OS(Expected);
250 OS << "<" << (void *)N << "> = !{";
251 C->printAsOperand(OS);
253 S->printAsOperand(OS);
255 MDNode *Nodes[] = {N0, N1, N2};
256 for (auto *Node : Nodes)
257 OS << ", <" << (void *)Node << ">";
263 raw_string_ostream OS(Actual);
267 EXPECT_EQ(Expected, Actual);
270 #define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
272 std::string Actual_; \
273 raw_string_ostream OS(Actual_); \
276 std::string Expected_(EXPECTED); \
277 EXPECT_EQ(Expected_, Actual_); \
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");
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));
293 Temp->replaceAllUsesWith(Arg);
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);
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");
309 std::string Expected;
311 raw_string_ostream OS(Expected);
313 C->printAsOperand(OS);
315 S->printAsOperand(OS);
316 OS << ", null, !1, !2, !3}";
319 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
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);
336 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
337 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
340 TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
341 Module M("test", Context);
344 Function::Create(FunctionType::get(Type::getVoidTy(Context),
345 Type::getMetadataTy(Context), false),
346 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
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);
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));
367 #undef EXPECT_PRINTER_EQ
369 TEST_F(MDNodeTest, NullOperand) {
371 MDNode *Empty = MDNode::get(Context, None);
373 // metadata !{metadata !{}}
374 Metadata *Ops[] = {Empty};
375 MDNode *N = MDNode::get(Context, Ops);
376 ASSERT_EQ(Empty, N->getOperand(0));
378 // metadata !{metadata !{}} => metadata !{null}
379 N->replaceOperandWith(0, nullptr);
380 ASSERT_EQ(nullptr, N->getOperand(0));
384 MDNode *NullOp = MDNode::get(Context, Ops);
385 ASSERT_EQ(nullptr, NullOp->getOperand(0));
386 EXPECT_EQ(N, NullOp);
389 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
391 MDNode *Empty = MDNode::get(Context, None);
392 ASSERT_TRUE(Empty->isResolved());
393 EXPECT_FALSE(Empty->isDistinct());
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());
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());
409 // !{!{!{}}} => !{!{}}
410 Wrapped2->replaceOperandWith(0, Empty);
411 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
412 EXPECT_TRUE(Wrapped2->isDistinct());
413 EXPECT_FALSE(Wrapped1->isDistinct());
416 TEST_F(MDNodeTest, getDistinct) {
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));
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);
433 ASSERT_EQ(Empty, MDNode::get(Context, None));
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());
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());
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());
463 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
465 auto Temp = MDTuple::getTemporary(Context, None);
466 ASSERT_FALSE(Temp->isResolved());
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));
474 // temporary !{} => !{}
475 MDNode *Empty = MDNode::get(Context, None);
476 Temp->replaceAllUsesWith(Empty);
477 EXPECT_EQ(Empty, Distinct->getOperand(0));
480 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
482 MDNode *N0 = MDNode::get(Context, None);
485 auto Temp3 = MDTuple::getTemporary(Context, None);
486 Metadata *Ops1[] = {Temp3.get(), nullptr};
487 MDNode *N1 = MDNode::get(Context, Ops1);
490 Metadata *Ops2[] = {Temp3.get(), N0};
491 MDNode *N2 = MDNode::get(Context, Ops2);
494 Metadata *Ops3[] = {N2};
495 MDNode *N3 = MDNode::get(Context, Ops3);
496 Temp3->replaceAllUsesWith(N3);
499 Metadata *Ops4[] = {N1};
500 MDNode *N4 = MDNode::get(Context, Ops4);
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());
509 // Create a couple of distinct nodes to observe what's going on.
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);
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.
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);
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.
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));
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.
548 // Use a temporary node to keep N from being resolved.
549 auto Temp = MDTuple::getTemporary(Context, None);
550 Metadata *Ops[] = {nullptr, Temp.get()};
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());
557 // Check code for replacing resolved nodes.
558 N->replaceOperandWith(0, Empty);
559 EXPECT_EQ(Empty, N->getOperand(0));
561 // Check code for adding another unresolved operand.
562 N->replaceOperandWith(0, Temp.get());
563 EXPECT_EQ(Temp.get(), N->getOperand(0));
565 // Remove the references to Temp; required for teardown.
566 Temp->replaceAllUsesWith(nullptr);
569 TEST_F(MDNodeTest, replaceWithUniqued) {
570 auto *Empty = MDTuple::get(Context, None);
571 MDTuple *FirstUniqued;
573 Metadata *Ops[] = {Empty};
574 auto Temp = MDTuple::getTemporary(Context, Ops);
575 EXPECT_TRUE(Temp->isTemporary());
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);
585 Metadata *Ops[] = {Empty};
586 auto Temp = MDTuple::getTemporary(Context, Ops);
587 EXPECT_TRUE(Temp->isTemporary());
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);
596 auto Unresolved = MDTuple::getTemporary(Context, None);
597 Metadata *Ops[] = {Unresolved.get()};
598 auto Temp = MDTuple::getTemporary(Context, Ops);
599 EXPECT_TRUE(Temp->isTemporary());
601 // Shouldn't be resolved.
602 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
603 EXPECT_TRUE(Uniqued->isUniqued());
604 EXPECT_FALSE(Uniqued->isResolved());
606 // Should be a different node.
607 EXPECT_NE(FirstUniqued, Uniqued);
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());
616 TEST_F(MDNodeTest, replaceWithDistinct) {
618 auto *Empty = MDTuple::get(Context, None);
619 Metadata *Ops[] = {Empty};
620 auto Temp = MDTuple::getTemporary(Context, Ops);
621 EXPECT_TRUE(Temp->isTemporary());
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);
631 auto Unresolved = MDTuple::getTemporary(Context, None);
632 Metadata *Ops[] = {Unresolved.get()};
633 auto Temp = MDTuple::getTemporary(Context, Ops);
634 EXPECT_TRUE(Temp->isTemporary());
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);
643 // Cleanup; required for teardown.
644 Unresolved->replaceAllUsesWith(nullptr);
648 TEST_F(MDNodeTest, replaceWithPermanent) {
649 Metadata *Ops[] = {nullptr};
650 auto Temp = MDTuple::getTemporary(Context, Ops);
651 auto *T = Temp.get();
653 // U is a normal, uniqued node that references T.
654 auto *U = MDTuple::get(Context, T);
655 EXPECT_TRUE(U->isUniqued());
657 // Make Temp self-referencing.
658 Temp->replaceOperandWith(0, T);
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.
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));
667 EXPECT_TRUE(N->isDistinct());
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());
673 // This temporary should collide with U when replaced, but it should still be
675 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
676 EXPECT_TRUE(U->isUniqued());
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));
686 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
688 EXPECT_EQ(nullptr, Ref.get());
690 auto Temp = MDTuple::getTemporary(Context, None);
691 Ref.reset(Temp.get());
692 EXPECT_EQ(Temp.get(), Ref.get());
694 EXPECT_EQ(nullptr, Ref.get());
697 typedef MetadataTest MDLocationTest;
699 TEST_F(MDLocationTest, Overflow) {
700 MDSubprogram *N = getSubprogram();
702 MDLocation *L = MDLocation::get(Context, 2, 7, N);
703 EXPECT_EQ(2u, L->getLine());
704 EXPECT_EQ(7u, L->getColumn());
706 unsigned U16 = 1u << 16;
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());
713 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
714 EXPECT_EQ(UINT32_MAX, L->getLine());
715 EXPECT_EQ(0u, L->getColumn());
718 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
719 EXPECT_EQ(UINT32_MAX, L->getLine());
720 EXPECT_EQ(0u, L->getColumn());
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));
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());
740 typedef MetadataTest GenericDebugNodeTest;
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());
756 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
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());
764 Metadata *Ops2[] = {nullptr};
765 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
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));
774 TempGenericDebugNode Temp = N->clone();
775 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
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));
785 typedef MetadataTest MDSubrangeTest;
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));
795 TempMDSubrange Temp = N->clone();
796 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
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));
807 typedef MetadataTest MDEnumeratorTest;
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"));
816 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
817 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
819 TempMDEnumerator Temp = N->clone();
820 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
823 typedef MetadataTest MDBasicTypeTest;
825 TEST_F(MDBasicTypeTest, get) {
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,
837 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
838 "special", 33, 26, 7));
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,
843 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
845 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
848 TempMDBasicType Temp = N->clone();
849 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
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());
859 TEST_F(MDBasicTypeTest, getUnspecified) {
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());
870 typedef MetadataTest MDTypeTest;
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);
877 TempMDType Temp = N->clone();
878 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
881 TEST_F(MDTypeTest, setFlags) {
883 Metadata *TypesOps[] = {nullptr};
884 Metadata *Types = MDTuple::get(Context, TypesOps);
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());
891 EXPECT_EQ(0u, D->getFlags());
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());
898 EXPECT_EQ(0u, T->getFlags());
901 typedef MetadataTest MDDerivedTypeTest;
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);
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,
926 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
927 "something", File, 1, Scope, BaseType, 2, 3,
929 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
930 File, 1, Scope, BaseType, 2, 3, 4, 5,
932 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
933 "something", Scope, 1, Scope, BaseType, 2, 3,
935 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
936 "something", File, 2, Scope, BaseType, 2, 3,
939 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
940 File, 1, File, BaseType, 2, 3, 4, 5, ExtraData));
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,
947 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
948 "something", File, 1, Scope, BaseType, 2, 2,
950 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
951 "something", File, 1, Scope, BaseType, 2, 3,
953 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
954 "something", File, 1, Scope, BaseType, 2, 3,
957 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
958 File, 1, Scope, BaseType, 2, 3, 4, 5, File));
960 TempMDDerivedType Temp = N->clone();
961 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
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);
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());
978 typedef MetadataTest MDCompositeTypeTest;
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);
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;
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";
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());
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));
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"));
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());
1093 TempMDCompositeType Temp = N->clone();
1094 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
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);
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;
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";
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());
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);
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;
1134 unsigned RuntimeLang = 6;
1135 StringRef Identifier = "some id";
1137 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1138 BaseType, SizeInBits, AlignInBits,
1139 OffsetInBits, Flags, nullptr, RuntimeLang,
1140 nullptr, nullptr, Identifier);
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());
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());
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());
1164 typedef MetadataTest MDSubroutineTypeTest;
1166 TEST_F(MDSubroutineTypeTest, get) {
1168 Metadata *TypeArray = MDTuple::getDistinct(Context, None);
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));
1176 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
1177 EXPECT_NE(N, MDSubroutineType::get(Context, Flags,
1178 MDTuple::getDistinct(Context, None)));
1180 TempMDSubroutineType Temp = N->clone();
1181 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
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());
1193 typedef MetadataTest MDFileTest;
1195 TEST_F(MDFileTest, get) {
1196 StringRef Filename = "file";
1197 StringRef Directory = "dir";
1198 auto *N = MDFile::get(Context, Filename, Directory);
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));
1205 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1206 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
1208 TempMDFile Temp = N->clone();
1209 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
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());
1218 typedef MetadataTest MDCompileUnitTest;
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);
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,
1259 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1260 IsOptimized, Flags, RuntimeVersion,
1261 SplitDebugFilename, EmissionKind, EnumTypes,
1262 RetainedTypes, Subprograms, GlobalVariables,
1264 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer,
1265 IsOptimized, Flags, RuntimeVersion,
1266 SplitDebugFilename, EmissionKind, EnumTypes,
1267 RetainedTypes, Subprograms, GlobalVariables,
1269 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1270 IsOptimized, Flags, RuntimeVersion,
1271 SplitDebugFilename, EmissionKind, EnumTypes,
1272 RetainedTypes, Subprograms, GlobalVariables,
1274 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1275 !IsOptimized, Flags, RuntimeVersion,
1276 SplitDebugFilename, EmissionKind, EnumTypes,
1277 RetainedTypes, Subprograms, GlobalVariables,
1279 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1280 IsOptimized, "other", RuntimeVersion,
1281 SplitDebugFilename, EmissionKind, EnumTypes,
1282 RetainedTypes, Subprograms, GlobalVariables,
1284 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1285 IsOptimized, Flags, RuntimeVersion + 1,
1286 SplitDebugFilename, EmissionKind, EnumTypes,
1287 RetainedTypes, Subprograms, GlobalVariables,
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,
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));
1321 TempMDCompileUnit Temp = N->clone();
1322 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
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);
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());
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());
1357 typedef MetadataTest MDSubprogramTest;
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);
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;
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);
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);
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));
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,
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));
1500 TempMDSubprogram Temp = N->clone();
1501 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
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);
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;
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);
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);
1528 EXPECT_EQ(nullptr, N->getFunction());
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());
1536 N->replaceFunction(nullptr);
1537 EXPECT_EQ(nullptr, N->getFunction());
1540 typedef MetadataTest MDLexicalBlockTest;
1542 TEST_F(MDLexicalBlockTest, get) {
1543 Metadata *Scope = MDTuple::getDistinct(Context, None);
1544 Metadata *File = MDTuple::getDistinct(Context, None);
1546 unsigned Column = 8;
1548 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
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));
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));
1562 TempMDLexicalBlock Temp = N->clone();
1563 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1566 typedef MetadataTest MDLexicalBlockFileTest;
1568 TEST_F(MDLexicalBlockFileTest, get) {
1569 Metadata *Scope = MDTuple::getDistinct(Context, None);
1570 Metadata *File = MDTuple::getDistinct(Context, None);
1571 unsigned Discriminator = 5;
1573 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
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));
1581 EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1582 EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1584 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1586 TempMDLexicalBlockFile Temp = N->clone();
1587 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1590 typedef MetadataTest MDNamespaceTest;
1592 TEST_F(MDNamespaceTest, get) {
1593 Metadata *Scope = MDTuple::getDistinct(Context, None);
1594 Metadata *File = MDTuple::getDistinct(Context, None);
1595 StringRef Name = "namespace";
1598 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
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));
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));
1612 TempMDNamespace Temp = N->clone();
1613 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1616 typedef MetadataTest MDTemplateTypeParameterTest;
1618 TEST_F(MDTemplateTypeParameterTest, get) {
1619 StringRef Name = "template";
1620 Metadata *Type = MDTuple::getDistinct(Context, None);
1621 Metadata *Other = MDTuple::getDistinct(Context, None);
1623 auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
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));
1630 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
1631 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Name, Other));
1633 TempMDTemplateTypeParameter Temp = N->clone();
1634 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1637 typedef MetadataTest MDTemplateValueParameterTest;
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);
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));
1653 EXPECT_NE(N, MDTemplateValueParameter::get(
1654 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1656 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type,
1658 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Other,
1660 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Other));
1662 TempMDTemplateValueParameter Temp = N->clone();
1663 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1666 typedef MetadataTest MDGlobalVariableTest;
1668 TEST_F(MDGlobalVariableTest, get) {
1669 MDScope *Scope = getSubprogram();
1670 StringRef Name = "name";
1671 StringRef LinkageName = "linkage";
1672 MDFile *File = getFile();
1674 Metadata *Type = MDTuple::getDistinct(Context, None);
1675 bool IsLocalToUnit = false;
1676 bool IsDefinition = true;
1677 ConstantAsMetadata *Variable = getConstantAsMetadata();
1678 MDDerivedType *StaticDataMemberDeclaration = getDerivedType();
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));
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));
1709 MDGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
1710 Line, Type, IsLocalToUnit, IsDefinition,
1711 Variable, StaticDataMemberDeclaration));
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()));
1733 TempMDGlobalVariable Temp = N->clone();
1734 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1737 typedef MetadataTest MDLocalVariableTest;
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();
1745 Metadata *Type = MDTuple::getDistinct(Context, None);
1748 MDLocation *InlinedAt =
1749 MDLocation::getDistinct(Context, 10, 20, getSubprogram());
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));
1765 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1766 Name, File, Line, Type, Arg, Flags,
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())));
1786 TempMDLocalVariable Temp = N->clone();
1787 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
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)));
1803 typedef MetadataTest MDExpressionTest;
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));
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));
1818 TempMDExpression Temp = N->clone();
1819 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1822 TEST_F(MDExpressionTest, isValid) {
1823 #define EXPECT_VALID(...) \
1825 uint64_t Elements[] = {__VA_ARGS__}; \
1826 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1828 #define EXPECT_INVALID(...) \
1830 uint64_t Elements[] = {__VA_ARGS__}; \
1831 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1834 // Empty expression should be valid.
1835 EXPECT_TRUE(MDExpression::get(Context, None));
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);
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);
1855 #undef EXPECT_INVALID
1858 typedef MetadataTest MDObjCPropertyTest;
1860 TEST_F(MDObjCPropertyTest, get) {
1861 StringRef Name = "name";
1862 Metadata *File = MDTuple::getDistinct(Context, None);
1864 StringRef GetterName = "getter";
1865 StringRef SetterName = "setter";
1866 unsigned Attributes = 7;
1867 Metadata *Type = MDTuple::getDistinct(Context, None);
1869 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1870 SetterName, Attributes, Type);
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));
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));
1898 TempMDObjCProperty Temp = N->clone();
1899 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1902 typedef MetadataTest MDImportedEntityTest;
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);
1909 StringRef Name = "name";
1911 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
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));
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));
1926 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1928 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
1930 TempMDImportedEntity Temp = N->clone();
1931 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1934 typedef MetadataTest MetadataAsValueTest;
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());
1942 auto *V2 = MetadataAsValue::get(Context, N);
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());
1954 auto *V2 = MetadataAsValue::get(Context, N2);
1957 auto *V3 = MetadataAsValue::get(Context, N);
1958 EXPECT_TRUE(V3->getType()->isMetadataTy());
1960 EXPECT_EQ(N, V3->getMetadata());
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);
1969 auto *V = MetadataAsValue::get(Context, MD);
1970 EXPECT_TRUE(V->getType()->isMetadataTy());
1971 EXPECT_EQ(MD, V->getMetadata());
1973 auto *V2 = MetadataAsValue::get(Context, N);
1974 EXPECT_EQ(MD, V2->getMetadata());
1978 typedef MetadataTest ValueAsMetadataTest;
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());
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());
1994 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
1995 // Create a constant.
1996 ConstantAsMetadata *CI = ConstantAsMetadata::get(
1997 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
1999 // Create a temporary to prevent nodes from resolving.
2000 auto Temp = MDTuple::getTemporary(Context, None);
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()};
2006 auto *N1 = MDTuple::get(Context, Ops1);
2007 auto *N2 = MDTuple::get(Context, Ops2);
2010 // Tell metadata that the constant is getting deleted.
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));
2018 // Clean up Temp for teardown.
2019 Temp->replaceAllUsesWith(nullptr);
2022 typedef MetadataTest TrackingMDRefTest;
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());
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());
2037 // Reset it, so we don't inadvertently test deletion.
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());
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));
2060 Metadata *const V = C;
2061 Metadata *const V2 = C2;
2062 MDNode *n = MDNode::get(Context, V);
2063 MDNode *n2 = MDNode::get(Context, V2);
2065 Module M("MyModule", Context);
2066 const char *Name = "llvm.NMD1";
2067 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2069 NMD->addOperand(n2);
2072 raw_string_ostream oss(Str);
2074 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",