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() : M("test", Context), Counter(0) {}
72 MDNode *getNode() { return MDNode::get(Context, None); }
73 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
74 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
75 Metadata *MDs[] = {MD1, MD2};
76 return MDNode::get(Context, MDs);
79 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
80 MDSubroutineType *getSubroutineType() {
81 return MDSubroutineType::getDistinct(Context, 0, getNode(nullptr));
83 MDSubprogram *getSubprogram() {
84 return MDSubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
85 nullptr, false, false, 0, nullptr, 0, 0, 0,
88 MDScopeRef getSubprogramRef() { return getSubprogram()->getRef(); }
90 return MDFile::getDistinct(Context, "file.c", "/path/to/dir");
92 MDTypeRef getBasicType(StringRef Name) {
93 return MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name)
96 MDTypeRef getDerivedType() {
97 return MDDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
99 getBasicType("basictype"), 1, 2, 0, 0)
102 ConstantAsMetadata *getConstantAsMetadata() {
103 return ConstantAsMetadata::get(
104 ConstantInt::get(Type::getInt32Ty(Context), Counter++));
106 MDTypeRef getCompositeType() {
107 return MDCompositeType::getDistinct(
108 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr,
109 nullptr, 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "")
112 ConstantAsMetadata *getFunctionAsMetadata(StringRef Name) {
113 return ConstantAsMetadata::get(M.getOrInsertFunction(
114 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
117 typedef MetadataTest MDStringTest;
119 // Test that construction of MDString with different value produces different
120 // MDString objects, even with the same string pointer and nulls in the string.
121 TEST_F(MDStringTest, CreateDifferent) {
122 char x[3] = { 'f', 0, 'A' };
123 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
125 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
129 // Test that creation of MDStrings with the same string contents produces the
130 // same MDString object, even with different pointers.
131 TEST_F(MDStringTest, CreateSame) {
132 char x[4] = { 'a', 'b', 'c', 'X' };
133 char y[4] = { 'a', 'b', 'c', 'Y' };
135 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
136 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
140 // Test that MDString prints out the string we fed it.
141 TEST_F(MDStringTest, PrintingSimple) {
142 char *str = new char[13];
143 strncpy(str, "testing 1 2 3", 13);
144 MDString *s = MDString::get(Context, StringRef(str, 13));
145 strncpy(str, "aaaaaaaaaaaaa", 13);
149 raw_string_ostream oss(Str);
151 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
154 // Test printing of MDString with non-printable characters.
155 TEST_F(MDStringTest, PrintingComplex) {
156 char str[5] = {0, '\n', '"', '\\', (char)-1};
157 MDString *s = MDString::get(Context, StringRef(str+0, 5));
159 raw_string_ostream oss(Str);
161 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
164 typedef MetadataTest MDNodeTest;
166 // Test the two constructors, and containing other Constants.
167 TEST_F(MDNodeTest, Simple) {
168 char x[3] = { 'a', 'b', 'c' };
169 char y[3] = { '1', '2', '3' };
171 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
172 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
173 ConstantAsMetadata *CI = ConstantAsMetadata::get(
174 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
176 std::vector<Metadata *> V;
181 MDNode *n1 = MDNode::get(Context, V);
182 Metadata *const c1 = n1;
183 MDNode *n2 = MDNode::get(Context, c1);
184 Metadata *const c2 = n2;
185 MDNode *n3 = MDNode::get(Context, V);
186 MDNode *n4 = MDNode::getIfExists(Context, V);
187 MDNode *n5 = MDNode::getIfExists(Context, c1);
188 MDNode *n6 = MDNode::getIfExists(Context, c2);
193 EXPECT_EQ(n6, (Metadata *)nullptr);
195 EXPECT_EQ(3u, n1->getNumOperands());
196 EXPECT_EQ(s1, n1->getOperand(0));
197 EXPECT_EQ(CI, n1->getOperand(1));
198 EXPECT_EQ(s2, n1->getOperand(2));
200 EXPECT_EQ(1u, n2->getNumOperands());
201 EXPECT_EQ(n1, n2->getOperand(0));
204 TEST_F(MDNodeTest, Delete) {
205 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
206 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
208 Metadata *const V = LocalAsMetadata::get(I);
209 MDNode *n = MDNode::get(Context, V);
210 TrackingMDRef wvh(n);
217 TEST_F(MDNodeTest, SelfReference) {
221 auto Temp = MDNode::getTemporary(Context, None);
222 Metadata *Args[] = {Temp.get()};
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.
230 MDNode *Ref1 = MDNode::get(Context, Args);
231 MDNode *Ref2 = MDNode::get(Context, Args);
232 EXPECT_NE(Self, Ref1);
233 EXPECT_EQ(Ref1, Ref2);
239 auto Temp = MDNode::getTemporary(Context, None);
240 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
241 MDNode *Self = MDNode::get(Context, Args);
242 Self->replaceOperandWith(0, Self);
243 ASSERT_EQ(Self, Self->getOperand(0));
245 // Self-references should be distinct, so MDNode::get() should grab a
246 // uniqued node that references Self, not Self itself.
248 MDNode *Ref1 = MDNode::get(Context, Args);
249 MDNode *Ref2 = MDNode::get(Context, Args);
250 EXPECT_NE(Self, Ref1);
251 EXPECT_EQ(Ref1, Ref2);
255 TEST_F(MDNodeTest, Print) {
256 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
257 MDString *S = MDString::get(Context, "foo");
258 MDNode *N0 = getNode();
259 MDNode *N1 = getNode(N0);
260 MDNode *N2 = getNode(N0, N1);
262 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
263 MDNode *N = MDNode::get(Context, Args);
265 std::string Expected;
267 raw_string_ostream OS(Expected);
268 OS << "<" << (void *)N << "> = !{";
269 C->printAsOperand(OS);
271 S->printAsOperand(OS);
273 MDNode *Nodes[] = {N0, N1, N2};
274 for (auto *Node : Nodes)
275 OS << ", <" << (void *)Node << ">";
281 raw_string_ostream OS(Actual);
285 EXPECT_EQ(Expected, Actual);
288 #define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
290 std::string Actual_; \
291 raw_string_ostream OS(Actual_); \
294 std::string Expected_(EXPECTED); \
295 EXPECT_EQ(Expected_, Actual_); \
298 TEST_F(MDNodeTest, PrintTemporary) {
299 MDNode *Arg = getNode();
300 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
301 MDNode *N = getNode(Temp.get());
302 Module M("test", Context);
303 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
306 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
307 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
308 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
311 Temp->replaceAllUsesWith(Arg);
314 TEST_F(MDNodeTest, PrintFromModule) {
315 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
316 MDString *S = MDString::get(Context, "foo");
317 MDNode *N0 = getNode();
318 MDNode *N1 = getNode(N0);
319 MDNode *N2 = getNode(N0, N1);
321 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
322 MDNode *N = MDNode::get(Context, Args);
323 Module M("test", Context);
324 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
327 std::string Expected;
329 raw_string_ostream OS(Expected);
331 C->printAsOperand(OS);
333 S->printAsOperand(OS);
334 OS << ", null, !1, !2, !3}";
337 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
340 TEST_F(MDNodeTest, PrintFromFunction) {
341 Module M("test", Context);
342 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
343 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
344 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
345 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
346 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
347 auto *R0 = ReturnInst::Create(Context, BB0);
348 auto *R1 = ReturnInst::Create(Context, BB1);
349 auto *N0 = MDNode::getDistinct(Context, None);
350 auto *N1 = MDNode::getDistinct(Context, None);
351 R0->setMetadata("md", N0);
352 R1->setMetadata("md", N1);
354 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
355 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
358 TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
359 Module M("test", Context);
362 Function::Create(FunctionType::get(Type::getVoidTy(Context),
363 Type::getMetadataTy(Context), false),
364 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
366 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
367 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
368 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
369 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
370 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
371 auto *N0 = MDNode::getDistinct(Context, None);
372 auto *N1 = MDNode::getDistinct(Context, None);
373 auto *MAV0 = MetadataAsValue::get(Context, N0);
374 auto *MAV1 = MetadataAsValue::get(Context, N1);
375 CallInst::Create(Intrinsic, MAV0, "", BB0);
376 CallInst::Create(Intrinsic, MAV1, "", BB1);
378 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
379 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
380 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
381 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
382 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
383 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
385 #undef EXPECT_PRINTER_EQ
387 TEST_F(MDNodeTest, NullOperand) {
389 MDNode *Empty = MDNode::get(Context, None);
391 // metadata !{metadata !{}}
392 Metadata *Ops[] = {Empty};
393 MDNode *N = MDNode::get(Context, Ops);
394 ASSERT_EQ(Empty, N->getOperand(0));
396 // metadata !{metadata !{}} => metadata !{null}
397 N->replaceOperandWith(0, nullptr);
398 ASSERT_EQ(nullptr, N->getOperand(0));
402 MDNode *NullOp = MDNode::get(Context, Ops);
403 ASSERT_EQ(nullptr, NullOp->getOperand(0));
404 EXPECT_EQ(N, NullOp);
407 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
409 MDNode *Empty = MDNode::get(Context, None);
410 ASSERT_TRUE(Empty->isResolved());
411 EXPECT_FALSE(Empty->isDistinct());
414 Metadata *Wrapped1Ops[] = {Empty};
415 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
416 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
417 ASSERT_TRUE(Wrapped1->isResolved());
418 EXPECT_FALSE(Wrapped1->isDistinct());
421 Metadata *Wrapped2Ops[] = {Wrapped1};
422 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
423 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
424 ASSERT_TRUE(Wrapped2->isResolved());
425 EXPECT_FALSE(Wrapped2->isDistinct());
427 // !{!{!{}}} => !{!{}}
428 Wrapped2->replaceOperandWith(0, Empty);
429 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
430 EXPECT_TRUE(Wrapped2->isDistinct());
431 EXPECT_FALSE(Wrapped1->isDistinct());
434 TEST_F(MDNodeTest, getDistinct) {
436 MDNode *Empty = MDNode::get(Context, None);
437 ASSERT_TRUE(Empty->isResolved());
438 ASSERT_FALSE(Empty->isDistinct());
439 ASSERT_EQ(Empty, MDNode::get(Context, None));
442 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
443 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
444 EXPECT_TRUE(Distinct1->isResolved());
445 EXPECT_TRUE(Distinct2->isDistinct());
446 EXPECT_NE(Empty, Distinct1);
447 EXPECT_NE(Empty, Distinct2);
448 EXPECT_NE(Distinct1, Distinct2);
451 ASSERT_EQ(Empty, MDNode::get(Context, None));
454 TEST_F(MDNodeTest, isUniqued) {
455 MDNode *U = MDTuple::get(Context, None);
456 MDNode *D = MDTuple::getDistinct(Context, None);
457 auto T = MDTuple::getTemporary(Context, None);
458 EXPECT_TRUE(U->isUniqued());
459 EXPECT_FALSE(D->isUniqued());
460 EXPECT_FALSE(T->isUniqued());
463 TEST_F(MDNodeTest, isDistinct) {
464 MDNode *U = MDTuple::get(Context, None);
465 MDNode *D = MDTuple::getDistinct(Context, None);
466 auto T = MDTuple::getTemporary(Context, None);
467 EXPECT_FALSE(U->isDistinct());
468 EXPECT_TRUE(D->isDistinct());
469 EXPECT_FALSE(T->isDistinct());
472 TEST_F(MDNodeTest, isTemporary) {
473 MDNode *U = MDTuple::get(Context, None);
474 MDNode *D = MDTuple::getDistinct(Context, None);
475 auto T = MDTuple::getTemporary(Context, None);
476 EXPECT_FALSE(U->isTemporary());
477 EXPECT_FALSE(D->isTemporary());
478 EXPECT_TRUE(T->isTemporary());
481 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
483 auto Temp = MDTuple::getTemporary(Context, None);
484 ASSERT_FALSE(Temp->isResolved());
486 // distinct !{temporary !{}}
487 Metadata *Ops[] = {Temp.get()};
488 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
489 EXPECT_TRUE(Distinct->isResolved());
490 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
492 // temporary !{} => !{}
493 MDNode *Empty = MDNode::get(Context, None);
494 Temp->replaceAllUsesWith(Empty);
495 EXPECT_EQ(Empty, Distinct->getOperand(0));
498 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
500 MDNode *N0 = MDNode::get(Context, None);
503 auto Temp3 = MDTuple::getTemporary(Context, None);
504 Metadata *Ops1[] = {Temp3.get(), nullptr};
505 MDNode *N1 = MDNode::get(Context, Ops1);
508 Metadata *Ops2[] = {Temp3.get(), N0};
509 MDNode *N2 = MDNode::get(Context, Ops2);
512 Metadata *Ops3[] = {N2};
513 MDNode *N3 = MDNode::get(Context, Ops3);
514 Temp3->replaceAllUsesWith(N3);
517 Metadata *Ops4[] = {N1};
518 MDNode *N4 = MDNode::get(Context, Ops4);
520 // Confirm that the cycle prevented RAUW from getting dropped.
521 EXPECT_TRUE(N0->isResolved());
522 EXPECT_FALSE(N1->isResolved());
523 EXPECT_FALSE(N2->isResolved());
524 EXPECT_FALSE(N3->isResolved());
525 EXPECT_FALSE(N4->isResolved());
527 // Create a couple of distinct nodes to observe what's going on.
529 // !5 = distinct !{!2}
530 // !6 = distinct !{!3}
531 Metadata *Ops5[] = {N2};
532 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
533 Metadata *Ops6[] = {N3};
534 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
536 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
537 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
538 // references !3, this can cause a re-entry of handleChangedOperand() when !3
539 // is not ready for it.
541 // !2->replaceOperandWith(1, nullptr)
542 // !2: !{!3, !0} => !{!3, null}
543 // !2->replaceAllUsesWith(!1)
544 // !3: !{!2] => !{!1}
545 // !3->replaceAllUsesWith(!4)
546 N2->replaceOperandWith(1, nullptr);
548 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
549 // under us. Just check that the other nodes are sane.
553 // !5 = distinct !{!1}
554 // !6 = distinct !{!4}
555 EXPECT_EQ(N4, N1->getOperand(0));
556 EXPECT_EQ(N1, N4->getOperand(0));
557 EXPECT_EQ(N1, N5->getOperand(0));
558 EXPECT_EQ(N4, N6->getOperand(0));
561 TEST_F(MDNodeTest, replaceResolvedOperand) {
562 // Check code for replacing one resolved operand with another. If doing this
563 // directly (via replaceOperandWith()) becomes illegal, change the operand to
564 // a global value that gets RAUW'ed.
566 // Use a temporary node to keep N from being resolved.
567 auto Temp = MDTuple::getTemporary(Context, None);
568 Metadata *Ops[] = {nullptr, Temp.get()};
570 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
571 MDNode *N = MDTuple::get(Context, Ops);
572 EXPECT_EQ(nullptr, N->getOperand(0));
573 ASSERT_FALSE(N->isResolved());
575 // Check code for replacing resolved nodes.
576 N->replaceOperandWith(0, Empty);
577 EXPECT_EQ(Empty, N->getOperand(0));
579 // Check code for adding another unresolved operand.
580 N->replaceOperandWith(0, Temp.get());
581 EXPECT_EQ(Temp.get(), N->getOperand(0));
583 // Remove the references to Temp; required for teardown.
584 Temp->replaceAllUsesWith(nullptr);
587 TEST_F(MDNodeTest, replaceWithUniqued) {
588 auto *Empty = MDTuple::get(Context, None);
589 MDTuple *FirstUniqued;
591 Metadata *Ops[] = {Empty};
592 auto Temp = MDTuple::getTemporary(Context, Ops);
593 EXPECT_TRUE(Temp->isTemporary());
595 // Don't expect a collision.
596 auto *Current = Temp.get();
597 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
598 EXPECT_TRUE(FirstUniqued->isUniqued());
599 EXPECT_TRUE(FirstUniqued->isResolved());
600 EXPECT_EQ(Current, FirstUniqued);
603 Metadata *Ops[] = {Empty};
604 auto Temp = MDTuple::getTemporary(Context, Ops);
605 EXPECT_TRUE(Temp->isTemporary());
607 // Should collide with Uniqued above this time.
608 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
609 EXPECT_TRUE(Uniqued->isUniqued());
610 EXPECT_TRUE(Uniqued->isResolved());
611 EXPECT_EQ(FirstUniqued, Uniqued);
614 auto Unresolved = MDTuple::getTemporary(Context, None);
615 Metadata *Ops[] = {Unresolved.get()};
616 auto Temp = MDTuple::getTemporary(Context, Ops);
617 EXPECT_TRUE(Temp->isTemporary());
619 // Shouldn't be resolved.
620 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
621 EXPECT_TRUE(Uniqued->isUniqued());
622 EXPECT_FALSE(Uniqued->isResolved());
624 // Should be a different node.
625 EXPECT_NE(FirstUniqued, Uniqued);
627 // Should resolve when we update its node (note: be careful to avoid a
628 // collision with any other nodes above).
629 Uniqued->replaceOperandWith(0, nullptr);
630 EXPECT_TRUE(Uniqued->isResolved());
634 TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
636 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
637 EXPECT_FALSE(Op->isResolved());
640 Metadata *Ops[] = {Op};
641 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
642 EXPECT_FALSE(N->isResolved());
644 // temp !{temp !{}} => !{temp !{}}
645 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
646 EXPECT_FALSE(N->isResolved());
648 // !{temp !{}} => !{!{}}
649 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
650 EXPECT_TRUE(Op->isResolved());
651 EXPECT_TRUE(N->isResolved());
654 TEST_F(MDNodeTest, replaceWithUniquedChangingOperand) {
656 Type *Ty = Type::getInt1PtrTy(Context);
657 std::unique_ptr<GlobalVariable> GV(
658 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
659 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
662 Metadata *Ops[] = {Op};
663 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
665 // temp !{i1* @GV} => !{i1* @GV}
666 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
667 ASSERT_TRUE(N->isUniqued());
669 // !{i1* @GV} => !{null}
671 ASSERT_TRUE(N->isUniqued());
672 Metadata *NullOps[] = {nullptr};
673 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
676 TEST_F(MDNodeTest, replaceWithDistinct) {
678 auto *Empty = MDTuple::get(Context, None);
679 Metadata *Ops[] = {Empty};
680 auto Temp = MDTuple::getTemporary(Context, Ops);
681 EXPECT_TRUE(Temp->isTemporary());
683 // Don't expect a collision.
684 auto *Current = Temp.get();
685 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
686 EXPECT_TRUE(Distinct->isDistinct());
687 EXPECT_TRUE(Distinct->isResolved());
688 EXPECT_EQ(Current, Distinct);
691 auto Unresolved = MDTuple::getTemporary(Context, None);
692 Metadata *Ops[] = {Unresolved.get()};
693 auto Temp = MDTuple::getTemporary(Context, Ops);
694 EXPECT_TRUE(Temp->isTemporary());
696 // Don't expect a collision.
697 auto *Current = Temp.get();
698 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
699 EXPECT_TRUE(Distinct->isDistinct());
700 EXPECT_TRUE(Distinct->isResolved());
701 EXPECT_EQ(Current, Distinct);
703 // Cleanup; required for teardown.
704 Unresolved->replaceAllUsesWith(nullptr);
708 TEST_F(MDNodeTest, replaceWithPermanent) {
709 Metadata *Ops[] = {nullptr};
710 auto Temp = MDTuple::getTemporary(Context, Ops);
711 auto *T = Temp.get();
713 // U is a normal, uniqued node that references T.
714 auto *U = MDTuple::get(Context, T);
715 EXPECT_TRUE(U->isUniqued());
717 // Make Temp self-referencing.
718 Temp->replaceOperandWith(0, T);
720 // Try to uniquify Temp. This should, despite the name in the API, give a
721 // 'distinct' node, since self-references aren't allowed to be uniqued.
723 // Since it's distinct, N should have the same address as when it was a
724 // temporary (i.e., be equal to T not U).
725 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
727 EXPECT_TRUE(N->isDistinct());
729 // U should be the canonical unique node with N as the argument.
730 EXPECT_EQ(U, MDTuple::get(Context, N));
731 EXPECT_TRUE(U->isUniqued());
733 // This temporary should collide with U when replaced, but it should still be
735 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
736 EXPECT_TRUE(U->isUniqued());
738 // This temporary should become a new uniqued node.
739 auto Temp2 = MDTuple::getTemporary(Context, U);
740 auto *V = Temp2.get();
741 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
742 EXPECT_TRUE(V->isUniqued());
743 EXPECT_EQ(U, V->getOperand(0));
746 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
748 EXPECT_EQ(nullptr, Ref.get());
750 auto Temp = MDTuple::getTemporary(Context, None);
751 Ref.reset(Temp.get());
752 EXPECT_EQ(Temp.get(), Ref.get());
754 EXPECT_EQ(nullptr, Ref.get());
757 typedef MetadataTest MDLocationTest;
759 TEST_F(MDLocationTest, Overflow) {
760 MDSubprogram *N = getSubprogram();
762 MDLocation *L = MDLocation::get(Context, 2, 7, N);
763 EXPECT_EQ(2u, L->getLine());
764 EXPECT_EQ(7u, L->getColumn());
766 unsigned U16 = 1u << 16;
768 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
769 EXPECT_EQ(UINT32_MAX, L->getLine());
770 EXPECT_EQ(U16 - 1, L->getColumn());
773 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
774 EXPECT_EQ(UINT32_MAX, L->getLine());
775 EXPECT_EQ(0u, L->getColumn());
778 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
779 EXPECT_EQ(UINT32_MAX, L->getLine());
780 EXPECT_EQ(0u, L->getColumn());
784 TEST_F(MDLocationTest, getDistinct) {
785 MDNode *N = getSubprogram();
786 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
787 EXPECT_TRUE(L0->isDistinct());
788 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
789 EXPECT_FALSE(L1->isDistinct());
790 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
793 TEST_F(MDLocationTest, getTemporary) {
794 MDNode *N = MDNode::get(Context, None);
795 auto L = MDLocation::getTemporary(Context, 2, 7, N);
796 EXPECT_TRUE(L->isTemporary());
797 EXPECT_FALSE(L->isResolved());
800 typedef MetadataTest GenericDebugNodeTest;
802 TEST_F(GenericDebugNodeTest, get) {
803 StringRef Header = "header";
804 auto *Empty = MDNode::get(Context, None);
805 Metadata *Ops1[] = {Empty};
806 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
807 EXPECT_EQ(15u, N->getTag());
808 EXPECT_EQ(2u, N->getNumOperands());
809 EXPECT_EQ(Header, N->getHeader());
810 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
811 EXPECT_EQ(1u, N->getNumDwarfOperands());
812 EXPECT_EQ(Empty, N->getDwarfOperand(0));
813 EXPECT_EQ(Empty, N->getOperand(1));
814 ASSERT_TRUE(N->isUniqued());
816 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
818 N->replaceOperandWith(1, nullptr);
819 EXPECT_EQ(15u, N->getTag());
820 EXPECT_EQ(Header, N->getHeader());
821 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
822 ASSERT_TRUE(N->isUniqued());
824 Metadata *Ops2[] = {nullptr};
825 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
827 N->replaceDwarfOperandWith(0, Empty);
828 EXPECT_EQ(15u, N->getTag());
829 EXPECT_EQ(Header, N->getHeader());
830 EXPECT_EQ(Empty, N->getDwarfOperand(0));
831 ASSERT_TRUE(N->isUniqued());
832 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
834 TempGenericDebugNode Temp = N->clone();
835 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
838 TEST_F(GenericDebugNodeTest, getEmptyHeader) {
839 // Canonicalize !"" to null.
840 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
841 EXPECT_EQ(StringRef(), N->getHeader());
842 EXPECT_EQ(nullptr, N->getOperand(0));
845 typedef MetadataTest MDSubrangeTest;
847 TEST_F(MDSubrangeTest, get) {
848 auto *N = MDSubrange::get(Context, 5, 7);
849 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
850 EXPECT_EQ(5, N->getCount());
851 EXPECT_EQ(7, N->getLowerBound());
852 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
853 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
855 TempMDSubrange Temp = N->clone();
856 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
859 TEST_F(MDSubrangeTest, getEmptyArray) {
860 auto *N = MDSubrange::get(Context, -1, 0);
861 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
862 EXPECT_EQ(-1, N->getCount());
863 EXPECT_EQ(0, N->getLowerBound());
864 EXPECT_EQ(N, MDSubrange::get(Context, -1, 0));
867 typedef MetadataTest MDEnumeratorTest;
869 TEST_F(MDEnumeratorTest, get) {
870 auto *N = MDEnumerator::get(Context, 7, "name");
871 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
872 EXPECT_EQ(7, N->getValue());
873 EXPECT_EQ("name", N->getName());
874 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
876 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
877 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
879 TempMDEnumerator Temp = N->clone();
880 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
883 typedef MetadataTest MDBasicTypeTest;
885 TEST_F(MDBasicTypeTest, get) {
887 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
888 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
889 EXPECT_EQ("special", N->getName());
890 EXPECT_EQ(33u, N->getSizeInBits());
891 EXPECT_EQ(26u, N->getAlignInBits());
892 EXPECT_EQ(7u, N->getEncoding());
893 EXPECT_EQ(0u, N->getLine());
894 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
897 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
898 "special", 33, 26, 7));
900 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
901 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
903 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
905 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
908 TempMDBasicType Temp = N->clone();
909 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
912 TEST_F(MDBasicTypeTest, getWithLargeValues) {
913 auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
914 UINT64_MAX, UINT64_MAX - 1, 7);
915 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
916 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
919 TEST_F(MDBasicTypeTest, getUnspecified) {
921 MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
922 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
923 EXPECT_EQ("unspecified", N->getName());
924 EXPECT_EQ(0u, N->getSizeInBits());
925 EXPECT_EQ(0u, N->getAlignInBits());
926 EXPECT_EQ(0u, N->getEncoding());
927 EXPECT_EQ(0u, N->getLine());
930 typedef MetadataTest MDTypeTest;
932 TEST_F(MDTypeTest, clone) {
933 // Check that MDType has a specialized clone that returns TempMDType.
934 MDType *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
935 dwarf::DW_ATE_signed);
937 TempMDType Temp = N->clone();
938 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
941 TEST_F(MDTypeTest, setFlags) {
943 Metadata *TypesOps[] = {nullptr};
944 Metadata *Types = MDTuple::get(Context, TypesOps);
946 MDType *D = MDSubroutineType::getDistinct(Context, 0u, Types);
947 EXPECT_EQ(0u, D->getFlags());
948 D->setFlags(DIDescriptor::FlagRValueReference);
949 EXPECT_EQ(DIDescriptor::FlagRValueReference, D->getFlags());
951 EXPECT_EQ(0u, D->getFlags());
953 TempMDType T = MDSubroutineType::getTemporary(Context, 0u, Types);
954 EXPECT_EQ(0u, T->getFlags());
955 T->setFlags(DIDescriptor::FlagRValueReference);
956 EXPECT_EQ(DIDescriptor::FlagRValueReference, T->getFlags());
958 EXPECT_EQ(0u, T->getFlags());
961 typedef MetadataTest MDDerivedTypeTest;
963 TEST_F(MDDerivedTypeTest, get) {
964 MDFile *File = getFile();
965 MDScopeRef Scope = getSubprogramRef();
966 MDTypeRef BaseType = getBasicType("basic");
967 MDTuple *ExtraData = getTuple();
969 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
970 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
971 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
972 EXPECT_EQ("something", N->getName());
973 EXPECT_EQ(File, N->getFile());
974 EXPECT_EQ(1u, N->getLine());
975 EXPECT_EQ(Scope, N->getScope());
976 EXPECT_EQ(BaseType, N->getBaseType());
977 EXPECT_EQ(2u, N->getSizeInBits());
978 EXPECT_EQ(3u, N->getAlignInBits());
979 EXPECT_EQ(4u, N->getOffsetInBits());
980 EXPECT_EQ(5u, N->getFlags());
981 EXPECT_EQ(ExtraData, N->getExtraData());
982 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
983 "something", File, 1, Scope, BaseType, 2, 3,
986 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
987 "something", File, 1, Scope, BaseType, 2, 3,
989 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
990 File, 1, Scope, BaseType, 2, 3, 4, 5,
992 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
993 "something", getFile(), 1, Scope, BaseType, 2,
994 3, 4, 5, ExtraData));
995 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
996 "something", File, 2, Scope, BaseType, 2, 3,
998 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
999 "something", File, 1, getSubprogramRef(),
1000 BaseType, 2, 3, 4, 5, ExtraData));
1001 EXPECT_NE(N, MDDerivedType::get(
1002 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1003 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
1004 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1005 "something", File, 1, Scope, BaseType, 3, 3,
1007 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1008 "something", File, 1, Scope, BaseType, 2, 2,
1010 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1011 "something", File, 1, Scope, BaseType, 2, 3,
1013 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1014 "something", File, 1, Scope, BaseType, 2, 3,
1016 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1017 "something", File, 1, Scope, BaseType, 2, 3,
1020 TempMDDerivedType Temp = N->clone();
1021 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1024 TEST_F(MDDerivedTypeTest, getWithLargeValues) {
1025 MDFile *File = getFile();
1026 MDScopeRef Scope = getSubprogramRef();
1027 MDTypeRef BaseType = getBasicType("basic");
1028 MDTuple *ExtraData = getTuple();
1030 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
1031 File, 1, Scope, BaseType, UINT64_MAX,
1032 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1033 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1034 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1035 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1038 typedef MetadataTest MDCompositeTypeTest;
1040 TEST_F(MDCompositeTypeTest, get) {
1041 unsigned Tag = dwarf::DW_TAG_structure_type;
1042 StringRef Name = "some name";
1043 MDFile *File = getFile();
1045 MDScopeRef Scope = getSubprogramRef();
1046 MDTypeRef BaseType = getCompositeType();
1047 uint64_t SizeInBits = 2;
1048 uint64_t AlignInBits = 3;
1049 uint64_t OffsetInBits = 4;
1051 MDTuple *Elements = getTuple();
1052 unsigned RuntimeLang = 6;
1053 MDTypeRef VTableHolder = getCompositeType();
1054 MDTuple *TemplateParams = getTuple();
1055 StringRef Identifier = "some id";
1057 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1058 BaseType, SizeInBits, AlignInBits,
1059 OffsetInBits, Flags, Elements, RuntimeLang,
1060 VTableHolder, TemplateParams, Identifier);
1061 EXPECT_EQ(Tag, N->getTag());
1062 EXPECT_EQ(Name, N->getName());
1063 EXPECT_EQ(File, N->getFile());
1064 EXPECT_EQ(Line, N->getLine());
1065 EXPECT_EQ(Scope, N->getScope());
1066 EXPECT_EQ(BaseType, N->getBaseType());
1067 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1068 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1069 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1070 EXPECT_EQ(Flags, N->getFlags());
1071 EXPECT_EQ(Elements, N->getElements().get());
1072 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1073 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1074 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1075 EXPECT_EQ(Identifier, N->getIdentifier());
1077 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1078 BaseType, SizeInBits, AlignInBits,
1079 OffsetInBits, Flags, Elements, RuntimeLang,
1080 VTableHolder, TemplateParams, Identifier));
1082 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
1083 BaseType, SizeInBits, AlignInBits,
1084 OffsetInBits, Flags, Elements, RuntimeLang,
1085 VTableHolder, TemplateParams, Identifier));
1086 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
1087 BaseType, SizeInBits, AlignInBits,
1088 OffsetInBits, Flags, Elements, RuntimeLang,
1089 VTableHolder, TemplateParams, Identifier));
1090 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
1091 BaseType, SizeInBits, AlignInBits,
1092 OffsetInBits, Flags, Elements, RuntimeLang,
1093 VTableHolder, TemplateParams, Identifier));
1094 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
1095 BaseType, SizeInBits, AlignInBits,
1096 OffsetInBits, Flags, Elements, RuntimeLang,
1097 VTableHolder, TemplateParams, Identifier));
1098 EXPECT_NE(N, MDCompositeType::get(
1099 Context, Tag, Name, File, Line, getSubprogramRef(), BaseType,
1100 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1101 RuntimeLang, VTableHolder, TemplateParams, Identifier));
1102 EXPECT_NE(N, MDCompositeType::get(
1103 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1104 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1105 RuntimeLang, VTableHolder, TemplateParams, Identifier));
1106 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1107 BaseType, SizeInBits + 1, AlignInBits,
1108 OffsetInBits, Flags, Elements, RuntimeLang,
1109 VTableHolder, TemplateParams, Identifier));
1110 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1111 BaseType, SizeInBits, AlignInBits + 1,
1112 OffsetInBits, Flags, Elements, RuntimeLang,
1113 VTableHolder, TemplateParams, Identifier));
1114 EXPECT_NE(N, MDCompositeType::get(
1115 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1116 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1117 VTableHolder, TemplateParams, Identifier));
1118 EXPECT_NE(N, MDCompositeType::get(
1119 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1120 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1121 VTableHolder, TemplateParams, Identifier));
1122 EXPECT_NE(N, MDCompositeType::get(
1123 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1124 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1125 VTableHolder, TemplateParams, Identifier));
1126 EXPECT_NE(N, MDCompositeType::get(
1127 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1128 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1129 VTableHolder, TemplateParams, Identifier));
1130 EXPECT_NE(N, MDCompositeType::get(
1131 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1132 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1133 getCompositeType(), TemplateParams, Identifier));
1134 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1135 BaseType, SizeInBits, AlignInBits,
1136 OffsetInBits, Flags, Elements, RuntimeLang,
1137 VTableHolder, getTuple(), Identifier));
1138 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1139 BaseType, SizeInBits, AlignInBits,
1140 OffsetInBits, Flags, Elements, RuntimeLang,
1141 VTableHolder, TemplateParams, "other"));
1143 // Be sure that missing identifiers get null pointers.
1144 EXPECT_FALSE(MDCompositeType::get(
1145 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1146 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1147 VTableHolder, TemplateParams, "")->getRawIdentifier());
1148 EXPECT_FALSE(MDCompositeType::get(
1149 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1150 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1151 VTableHolder, TemplateParams)->getRawIdentifier());
1153 TempMDCompositeType Temp = N->clone();
1154 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1157 TEST_F(MDCompositeTypeTest, getWithLargeValues) {
1158 unsigned Tag = dwarf::DW_TAG_structure_type;
1159 StringRef Name = "some name";
1160 MDFile *File = getFile();
1162 MDScopeRef Scope = getSubprogramRef();
1163 MDTypeRef BaseType = getCompositeType();
1164 uint64_t SizeInBits = UINT64_MAX;
1165 uint64_t AlignInBits = UINT64_MAX - 1;
1166 uint64_t OffsetInBits = UINT64_MAX - 2;
1168 MDTuple *Elements = getTuple();
1169 unsigned RuntimeLang = 6;
1170 MDTypeRef VTableHolder = getCompositeType();
1171 MDTuple *TemplateParams = getTuple();
1172 StringRef Identifier = "some id";
1174 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1175 BaseType, SizeInBits, AlignInBits,
1176 OffsetInBits, Flags, Elements, RuntimeLang,
1177 VTableHolder, TemplateParams, Identifier);
1178 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1179 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1180 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1183 TEST_F(MDCompositeTypeTest, replaceOperands) {
1184 unsigned Tag = dwarf::DW_TAG_structure_type;
1185 StringRef Name = "some name";
1186 MDFile *File = getFile();
1188 MDScopeRef Scope = getSubprogramRef();
1189 MDTypeRef BaseType = getCompositeType();
1190 uint64_t SizeInBits = 2;
1191 uint64_t AlignInBits = 3;
1192 uint64_t OffsetInBits = 4;
1194 unsigned RuntimeLang = 6;
1195 StringRef Identifier = "some id";
1197 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1198 BaseType, SizeInBits, AlignInBits,
1199 OffsetInBits, Flags, nullptr, RuntimeLang,
1200 nullptr, nullptr, Identifier);
1202 auto *Elements = MDTuple::getDistinct(Context, None);
1203 EXPECT_EQ(nullptr, N->getElements().get());
1204 N->replaceElements(Elements);
1205 EXPECT_EQ(Elements, N->getElements().get());
1206 N->replaceElements(nullptr);
1207 EXPECT_EQ(nullptr, N->getElements().get());
1209 MDTypeRef VTableHolder = getCompositeType();
1210 EXPECT_EQ(nullptr, N->getVTableHolder());
1211 N->replaceVTableHolder(VTableHolder);
1212 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1213 N->replaceVTableHolder(nullptr);
1214 EXPECT_EQ(nullptr, N->getVTableHolder());
1216 auto *TemplateParams = MDTuple::getDistinct(Context, None);
1217 EXPECT_EQ(nullptr, N->getTemplateParams().get());
1218 N->replaceTemplateParams(TemplateParams);
1219 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1220 N->replaceTemplateParams(nullptr);
1221 EXPECT_EQ(nullptr, N->getTemplateParams().get());
1224 typedef MetadataTest MDSubroutineTypeTest;
1226 TEST_F(MDSubroutineTypeTest, get) {
1228 MDTuple *TypeArray = getTuple();
1230 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
1231 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1232 EXPECT_EQ(Flags, N->getFlags());
1233 EXPECT_EQ(TypeArray, N->getTypeArray().get());
1234 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
1236 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
1237 EXPECT_NE(N, MDSubroutineType::get(Context, Flags, getTuple()));
1239 TempMDSubroutineType Temp = N->clone();
1240 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1242 // Test always-empty operands.
1243 EXPECT_EQ(nullptr, N->getScope());
1244 EXPECT_EQ(nullptr, N->getFile());
1245 EXPECT_EQ("", N->getName());
1246 EXPECT_EQ(nullptr, N->getBaseType());
1247 EXPECT_EQ(nullptr, N->getVTableHolder());
1248 EXPECT_EQ(nullptr, N->getTemplateParams().get());
1249 EXPECT_EQ("", N->getIdentifier());
1252 typedef MetadataTest MDFileTest;
1254 TEST_F(MDFileTest, get) {
1255 StringRef Filename = "file";
1256 StringRef Directory = "dir";
1257 auto *N = MDFile::get(Context, Filename, Directory);
1259 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1260 EXPECT_EQ(Filename, N->getFilename());
1261 EXPECT_EQ(Directory, N->getDirectory());
1262 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
1264 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1265 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
1267 TempMDFile Temp = N->clone();
1268 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1271 TEST_F(MDFileTest, ScopeGetFile) {
1272 // Ensure that MDScope::getFile() returns itself.
1273 MDScope *N = MDFile::get(Context, "file", "dir");
1274 EXPECT_EQ(N, N->getFile());
1277 typedef MetadataTest MDCompileUnitTest;
1279 TEST_F(MDCompileUnitTest, get) {
1280 unsigned SourceLanguage = 1;
1281 MDFile *File = getFile();
1282 StringRef Producer = "some producer";
1283 bool IsOptimized = false;
1284 StringRef Flags = "flag after flag";
1285 unsigned RuntimeVersion = 2;
1286 StringRef SplitDebugFilename = "another/file";
1287 unsigned EmissionKind = 3;
1288 MDTuple *EnumTypes = getTuple();
1289 MDTuple *RetainedTypes = getTuple();
1290 MDTuple *Subprograms = getTuple();
1291 MDTuple *GlobalVariables = getTuple();
1292 MDTuple *ImportedEntities = getTuple();
1293 auto *N = MDCompileUnit::get(
1294 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1295 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1296 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1298 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1299 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1300 EXPECT_EQ(File, N->getFile());
1301 EXPECT_EQ(Producer, N->getProducer());
1302 EXPECT_EQ(IsOptimized, N->isOptimized());
1303 EXPECT_EQ(Flags, N->getFlags());
1304 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1305 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1306 EXPECT_EQ(EmissionKind, N->getEmissionKind());
1307 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1308 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
1309 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1310 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1311 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
1312 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1313 IsOptimized, Flags, RuntimeVersion,
1314 SplitDebugFilename, EmissionKind, EnumTypes,
1315 RetainedTypes, Subprograms, GlobalVariables,
1318 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1319 IsOptimized, Flags, RuntimeVersion,
1320 SplitDebugFilename, EmissionKind, EnumTypes,
1321 RetainedTypes, Subprograms, GlobalVariables,
1323 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, getFile(), Producer,
1324 IsOptimized, Flags, RuntimeVersion,
1325 SplitDebugFilename, EmissionKind, EnumTypes,
1326 RetainedTypes, Subprograms, GlobalVariables,
1328 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1329 IsOptimized, Flags, RuntimeVersion,
1330 SplitDebugFilename, EmissionKind, EnumTypes,
1331 RetainedTypes, Subprograms, GlobalVariables,
1333 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1334 !IsOptimized, Flags, RuntimeVersion,
1335 SplitDebugFilename, EmissionKind, EnumTypes,
1336 RetainedTypes, Subprograms, GlobalVariables,
1338 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1339 IsOptimized, "other", RuntimeVersion,
1340 SplitDebugFilename, EmissionKind, EnumTypes,
1341 RetainedTypes, Subprograms, GlobalVariables,
1343 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1344 IsOptimized, Flags, RuntimeVersion + 1,
1345 SplitDebugFilename, EmissionKind, EnumTypes,
1346 RetainedTypes, Subprograms, GlobalVariables,
1349 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1350 IsOptimized, Flags, RuntimeVersion, "other",
1351 EmissionKind, EnumTypes, RetainedTypes,
1352 Subprograms, GlobalVariables, ImportedEntities));
1353 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1354 IsOptimized, Flags, RuntimeVersion,
1355 SplitDebugFilename, EmissionKind + 1,
1356 EnumTypes, RetainedTypes, Subprograms,
1357 GlobalVariables, ImportedEntities));
1358 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1359 IsOptimized, Flags, RuntimeVersion,
1360 SplitDebugFilename, EmissionKind, getTuple(),
1361 RetainedTypes, Subprograms, GlobalVariables,
1363 EXPECT_NE(N, MDCompileUnit::get(
1364 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1365 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1366 getTuple(), Subprograms, GlobalVariables, ImportedEntities));
1367 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1368 IsOptimized, Flags, RuntimeVersion,
1369 SplitDebugFilename, EmissionKind, EnumTypes,
1370 RetainedTypes, getTuple(), GlobalVariables,
1372 EXPECT_NE(N, MDCompileUnit::get(
1373 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1374 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1375 RetainedTypes, Subprograms, getTuple(), ImportedEntities));
1376 EXPECT_NE(N, MDCompileUnit::get(
1377 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1378 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1379 RetainedTypes, Subprograms, GlobalVariables, getTuple()));
1381 TempMDCompileUnit Temp = N->clone();
1382 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1385 TEST_F(MDCompileUnitTest, replaceArrays) {
1386 unsigned SourceLanguage = 1;
1387 MDFile *File = getFile();
1388 StringRef Producer = "some producer";
1389 bool IsOptimized = false;
1390 StringRef Flags = "flag after flag";
1391 unsigned RuntimeVersion = 2;
1392 StringRef SplitDebugFilename = "another/file";
1393 unsigned EmissionKind = 3;
1394 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1395 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1396 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
1397 auto *N = MDCompileUnit::get(
1398 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1399 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1400 RetainedTypes, nullptr, nullptr, ImportedEntities);
1402 auto *Subprograms = MDTuple::getDistinct(Context, None);
1403 EXPECT_EQ(nullptr, N->getSubprograms().get());
1404 N->replaceSubprograms(Subprograms);
1405 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1406 N->replaceSubprograms(nullptr);
1407 EXPECT_EQ(nullptr, N->getSubprograms().get());
1409 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1410 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
1411 N->replaceGlobalVariables(GlobalVariables);
1412 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1413 N->replaceGlobalVariables(nullptr);
1414 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
1417 typedef MetadataTest MDSubprogramTest;
1419 TEST_F(MDSubprogramTest, get) {
1420 MDScopeRef Scope = getCompositeType();
1421 StringRef Name = "name";
1422 StringRef LinkageName = "linkage";
1423 MDFile *File = getFile();
1425 MDSubroutineType *Type = getSubroutineType();
1426 bool IsLocalToUnit = false;
1427 bool IsDefinition = true;
1428 unsigned ScopeLine = 3;
1429 MDTypeRef ContainingType = getCompositeType();
1430 unsigned Virtuality = 4;
1431 unsigned VirtualIndex = 5;
1433 bool IsOptimized = false;
1434 ConstantAsMetadata *Function = getFunctionAsMetadata("foo");
1435 MDTuple *TemplateParams = getTuple();
1436 MDSubprogram *Declaration = getSubprogram();
1437 MDTuple *Variables = getTuple();
1439 auto *N = MDSubprogram::get(
1440 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1441 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1442 IsOptimized, Function, TemplateParams, Declaration, Variables);
1444 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1445 EXPECT_EQ(Scope, N->getScope());
1446 EXPECT_EQ(Name, N->getName());
1447 EXPECT_EQ(LinkageName, N->getLinkageName());
1448 EXPECT_EQ(File, N->getFile());
1449 EXPECT_EQ(Line, N->getLine());
1450 EXPECT_EQ(Type, N->getType());
1451 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1452 EXPECT_EQ(IsDefinition, N->isDefinition());
1453 EXPECT_EQ(ScopeLine, N->getScopeLine());
1454 EXPECT_EQ(ContainingType, N->getContainingType());
1455 EXPECT_EQ(Virtuality, N->getVirtuality());
1456 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1457 EXPECT_EQ(Flags, N->getFlags());
1458 EXPECT_EQ(IsOptimized, N->isOptimized());
1459 EXPECT_EQ(Function, N->getFunction());
1460 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1461 EXPECT_EQ(Declaration, N->getDeclaration());
1462 EXPECT_EQ(Variables, N->getVariables().get());
1463 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1464 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1465 ContainingType, Virtuality, VirtualIndex,
1466 Flags, IsOptimized, Function, TemplateParams,
1467 Declaration, Variables));
1469 EXPECT_NE(N, MDSubprogram::get(Context, getCompositeType(), Name, LinkageName,
1470 File, Line, Type, IsLocalToUnit, IsDefinition,
1471 ScopeLine, ContainingType, Virtuality,
1472 VirtualIndex, Flags, IsOptimized, Function,
1473 TemplateParams, Declaration, Variables));
1474 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1475 Line, Type, IsLocalToUnit, IsDefinition,
1476 ScopeLine, ContainingType, Virtuality,
1477 VirtualIndex, Flags, IsOptimized, Function,
1478 TemplateParams, Declaration, Variables));
1479 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1480 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1481 ContainingType, Virtuality, VirtualIndex,
1482 Flags, IsOptimized, Function, TemplateParams,
1483 Declaration, Variables));
1484 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, getFile(),
1485 Line, Type, IsLocalToUnit, IsDefinition,
1486 ScopeLine, ContainingType, Virtuality,
1487 VirtualIndex, Flags, IsOptimized, Function,
1488 TemplateParams, Declaration, Variables));
1489 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1490 Line + 1, Type, IsLocalToUnit, IsDefinition,
1491 ScopeLine, ContainingType, Virtuality,
1492 VirtualIndex, Flags, IsOptimized, Function,
1493 TemplateParams, Declaration, Variables));
1494 EXPECT_NE(N, MDSubprogram::get(
1495 Context, Scope, Name, LinkageName, File, Line,
1496 getSubroutineType(), IsLocalToUnit, IsDefinition, ScopeLine,
1497 ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1498 Function, TemplateParams, Declaration, Variables));
1499 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1500 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1501 ContainingType, Virtuality, VirtualIndex,
1502 Flags, IsOptimized, Function, TemplateParams,
1503 Declaration, Variables));
1504 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1505 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1506 ContainingType, Virtuality, VirtualIndex,
1507 Flags, IsOptimized, Function, TemplateParams,
1508 Declaration, Variables));
1509 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1510 Type, IsLocalToUnit, IsDefinition,
1511 ScopeLine + 1, ContainingType, Virtuality,
1512 VirtualIndex, Flags, IsOptimized, Function,
1513 TemplateParams, Declaration, Variables));
1514 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1515 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1516 getCompositeType(), Virtuality, VirtualIndex,
1517 Flags, IsOptimized, Function, TemplateParams,
1518 Declaration, Variables));
1519 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1520 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1521 ContainingType, Virtuality + 1, VirtualIndex,
1522 Flags, IsOptimized, Function, TemplateParams,
1523 Declaration, Variables));
1524 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1525 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1526 ContainingType, Virtuality, VirtualIndex + 1,
1527 Flags, IsOptimized, Function, TemplateParams,
1528 Declaration, Variables));
1529 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1530 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1531 ContainingType, Virtuality, VirtualIndex,
1532 ~Flags, IsOptimized, Function, TemplateParams,
1533 Declaration, Variables));
1534 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1535 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1536 ContainingType, Virtuality, VirtualIndex,
1537 Flags, !IsOptimized, Function, TemplateParams,
1538 Declaration, Variables));
1540 MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1541 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1542 ContainingType, Virtuality, VirtualIndex, Flags,
1543 IsOptimized, getFunctionAsMetadata("bar"),
1544 TemplateParams, Declaration, Variables));
1545 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1546 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1547 ContainingType, Virtuality, VirtualIndex,
1548 Flags, IsOptimized, Function, getTuple(),
1549 Declaration, Variables));
1550 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1551 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1552 ContainingType, Virtuality, VirtualIndex,
1553 Flags, IsOptimized, Function, TemplateParams,
1554 getSubprogram(), Variables));
1555 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1556 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1557 ContainingType, Virtuality, VirtualIndex,
1558 Flags, IsOptimized, Function, TemplateParams,
1559 Declaration, getTuple()));
1561 TempMDSubprogram Temp = N->clone();
1562 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1565 TEST_F(MDSubprogramTest, replaceFunction) {
1566 MDScopeRef Scope = getCompositeType();
1567 StringRef Name = "name";
1568 StringRef LinkageName = "linkage";
1569 MDFile *File = getFile();
1571 MDSubroutineType *Type = getSubroutineType();
1572 bool IsLocalToUnit = false;
1573 bool IsDefinition = true;
1574 unsigned ScopeLine = 3;
1575 MDTypeRef ContainingType = getCompositeType();
1576 unsigned Virtuality = 4;
1577 unsigned VirtualIndex = 5;
1579 bool IsOptimized = false;
1580 MDTuple *TemplateParams = getTuple();
1581 MDSubprogram *Declaration = getSubprogram();
1582 MDTuple *Variables = getTuple();
1584 auto *N = MDSubprogram::get(
1585 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1586 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1587 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1589 EXPECT_EQ(nullptr, N->getFunction());
1591 std::unique_ptr<Function> F(
1592 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1593 GlobalValue::ExternalLinkage));
1594 N->replaceFunction(F.get());
1595 EXPECT_EQ(ConstantAsMetadata::get(F.get()), N->getFunction());
1597 N->replaceFunction(nullptr);
1598 EXPECT_EQ(nullptr, N->getFunction());
1601 typedef MetadataTest MDLexicalBlockTest;
1603 TEST_F(MDLexicalBlockTest, get) {
1604 MDLocalScope *Scope = getSubprogram();
1605 MDFile *File = getFile();
1607 unsigned Column = 8;
1609 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1611 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1612 EXPECT_EQ(Scope, N->getScope());
1613 EXPECT_EQ(File, N->getFile());
1614 EXPECT_EQ(Line, N->getLine());
1615 EXPECT_EQ(Column, N->getColumn());
1616 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1619 MDLexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1620 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, getFile(), Line, Column));
1621 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1622 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
1624 TempMDLexicalBlock Temp = N->clone();
1625 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1628 typedef MetadataTest MDLexicalBlockFileTest;
1630 TEST_F(MDLexicalBlockFileTest, get) {
1631 MDLocalScope *Scope = getSubprogram();
1632 MDFile *File = getFile();
1633 unsigned Discriminator = 5;
1635 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1637 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1638 EXPECT_EQ(Scope, N->getScope());
1639 EXPECT_EQ(File, N->getFile());
1640 EXPECT_EQ(Discriminator, N->getDiscriminator());
1641 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1643 EXPECT_NE(N, MDLexicalBlockFile::get(Context, getSubprogram(), File,
1646 MDLexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
1648 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1650 TempMDLexicalBlockFile Temp = N->clone();
1651 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1654 typedef MetadataTest MDNamespaceTest;
1656 TEST_F(MDNamespaceTest, get) {
1657 MDScope *Scope = getFile();
1658 MDFile *File = getFile();
1659 StringRef Name = "namespace";
1662 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1664 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1665 EXPECT_EQ(Scope, N->getScope());
1666 EXPECT_EQ(File, N->getFile());
1667 EXPECT_EQ(Name, N->getName());
1668 EXPECT_EQ(Line, N->getLine());
1669 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1671 EXPECT_NE(N, MDNamespace::get(Context, getFile(), File, Name, Line));
1672 EXPECT_NE(N, MDNamespace::get(Context, Scope, getFile(), Name, Line));
1673 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1674 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
1676 TempMDNamespace Temp = N->clone();
1677 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1680 typedef MetadataTest MDTemplateTypeParameterTest;
1682 TEST_F(MDTemplateTypeParameterTest, get) {
1683 StringRef Name = "template";
1684 MDTypeRef Type = getBasicType("basic");
1686 auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
1688 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1689 EXPECT_EQ(Name, N->getName());
1690 EXPECT_EQ(Type, N->getType());
1691 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type));
1693 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
1695 MDTemplateTypeParameter::get(Context, Name, getBasicType("other")));
1697 TempMDTemplateTypeParameter Temp = N->clone();
1698 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1701 typedef MetadataTest MDTemplateValueParameterTest;
1703 TEST_F(MDTemplateValueParameterTest, get) {
1704 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1705 StringRef Name = "template";
1706 MDTypeRef Type = getBasicType("basic");
1707 Metadata *Value = getConstantAsMetadata();
1709 auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
1710 EXPECT_EQ(Tag, N->getTag());
1711 EXPECT_EQ(Name, N->getName());
1712 EXPECT_EQ(Type, N->getType());
1713 EXPECT_EQ(Value, N->getValue());
1714 EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value));
1716 EXPECT_NE(N, MDTemplateValueParameter::get(
1717 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1719 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type,
1721 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name,
1722 getBasicType("other"), Value));
1723 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type,
1724 getConstantAsMetadata()));
1726 TempMDTemplateValueParameter Temp = N->clone();
1727 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1730 typedef MetadataTest MDGlobalVariableTest;
1732 TEST_F(MDGlobalVariableTest, get) {
1733 MDScope *Scope = getSubprogram();
1734 StringRef Name = "name";
1735 StringRef LinkageName = "linkage";
1736 MDFile *File = getFile();
1738 MDTypeRef Type = getDerivedType();
1739 bool IsLocalToUnit = false;
1740 bool IsDefinition = true;
1741 ConstantAsMetadata *Variable = getConstantAsMetadata();
1742 MDDerivedType *StaticDataMemberDeclaration =
1743 cast<MDDerivedType>(getDerivedType());
1745 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1746 Type, IsLocalToUnit, IsDefinition, Variable,
1747 StaticDataMemberDeclaration);
1748 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1749 EXPECT_EQ(Scope, N->getScope());
1750 EXPECT_EQ(Name, N->getName());
1751 EXPECT_EQ(LinkageName, N->getLinkageName());
1752 EXPECT_EQ(File, N->getFile());
1753 EXPECT_EQ(Line, N->getLine());
1754 EXPECT_EQ(Type, N->getType());
1755 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1756 EXPECT_EQ(IsDefinition, N->isDefinition());
1757 EXPECT_EQ(Variable, N->getVariable());
1758 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1759 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1760 Line, Type, IsLocalToUnit, IsDefinition,
1761 Variable, StaticDataMemberDeclaration));
1764 MDGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
1765 File, Line, Type, IsLocalToUnit, IsDefinition,
1766 Variable, StaticDataMemberDeclaration));
1767 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1768 Line, Type, IsLocalToUnit, IsDefinition,
1769 Variable, StaticDataMemberDeclaration));
1770 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1771 Type, IsLocalToUnit, IsDefinition,
1772 Variable, StaticDataMemberDeclaration));
1774 MDGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
1775 Line, Type, IsLocalToUnit, IsDefinition,
1776 Variable, StaticDataMemberDeclaration));
1778 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1779 Line + 1, Type, IsLocalToUnit, IsDefinition,
1780 Variable, StaticDataMemberDeclaration));
1782 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1783 getDerivedType(), IsLocalToUnit, IsDefinition,
1784 Variable, StaticDataMemberDeclaration));
1785 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1786 Line, Type, !IsLocalToUnit, IsDefinition,
1787 Variable, StaticDataMemberDeclaration));
1788 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1789 Line, Type, IsLocalToUnit, !IsDefinition,
1790 Variable, StaticDataMemberDeclaration));
1791 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1792 Line, Type, IsLocalToUnit, IsDefinition,
1793 getConstantAsMetadata(),
1794 StaticDataMemberDeclaration));
1796 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1797 Type, IsLocalToUnit, IsDefinition, Variable,
1798 cast<MDDerivedType>(getDerivedType())));
1800 TempMDGlobalVariable Temp = N->clone();
1801 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1804 typedef MetadataTest MDLocalVariableTest;
1806 TEST_F(MDLocalVariableTest, get) {
1807 unsigned Tag = dwarf::DW_TAG_arg_variable;
1808 MDLocalScope *Scope = getSubprogram();
1809 StringRef Name = "name";
1810 MDFile *File = getFile();
1812 MDTypeRef Type = getDerivedType();
1815 MDLocation *InlinedAt =
1816 MDLocation::getDistinct(Context, 10, 20, getSubprogram());
1818 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1819 Arg, Flags, InlinedAt);
1820 EXPECT_EQ(Tag, N->getTag());
1821 EXPECT_EQ(Scope, N->getScope());
1822 EXPECT_EQ(Name, N->getName());
1823 EXPECT_EQ(File, N->getFile());
1824 EXPECT_EQ(Line, N->getLine());
1825 EXPECT_EQ(Type, N->getType());
1826 EXPECT_EQ(Arg, N->getArg());
1827 EXPECT_EQ(Flags, N->getFlags());
1828 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1829 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1830 Arg, Flags, InlinedAt));
1832 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1833 Name, File, Line, Type, Arg, Flags,
1835 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, getSubprogram(), Name, File,
1836 Line, Type, Arg, Flags, InlinedAt));
1837 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1838 Type, Arg, Flags, InlinedAt));
1839 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, getFile(), Line,
1840 Type, Arg, Flags, InlinedAt));
1841 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1842 Type, Arg, Flags, InlinedAt));
1843 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1844 getDerivedType(), Arg, Flags, InlinedAt));
1845 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1846 Arg + 1, Flags, InlinedAt));
1847 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1848 Arg, ~Flags, InlinedAt));
1849 EXPECT_NE(N, MDLocalVariable::get(
1850 Context, Tag, Scope, Name, File, Line, Type, Arg, Flags,
1851 MDLocation::getDistinct(Context, 10, 20, getSubprogram())));
1853 TempMDLocalVariable Temp = N->clone();
1854 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1856 auto *Inlined = N->withoutInline();
1857 EXPECT_NE(N, Inlined);
1858 EXPECT_EQ(N->getTag(), Inlined->getTag());
1859 EXPECT_EQ(N->getScope(), Inlined->getScope());
1860 EXPECT_EQ(N->getName(), Inlined->getName());
1861 EXPECT_EQ(N->getFile(), Inlined->getFile());
1862 EXPECT_EQ(N->getLine(), Inlined->getLine());
1863 EXPECT_EQ(N->getType(), Inlined->getType());
1864 EXPECT_EQ(N->getArg(), Inlined->getArg());
1865 EXPECT_EQ(N->getFlags(), Inlined->getFlags());
1866 EXPECT_EQ(nullptr, Inlined->getInlinedAt());
1867 EXPECT_EQ(N, Inlined->withInline(cast<MDLocation>(InlinedAt)));
1870 typedef MetadataTest MDExpressionTest;
1872 TEST_F(MDExpressionTest, get) {
1873 uint64_t Elements[] = {2, 6, 9, 78, 0};
1874 auto *N = MDExpression::get(Context, Elements);
1875 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1876 EXPECT_EQ(N, MDExpression::get(Context, Elements));
1878 EXPECT_EQ(5u, N->getNumElements());
1879 EXPECT_EQ(2u, N->getElement(0));
1880 EXPECT_EQ(6u, N->getElement(1));
1881 EXPECT_EQ(9u, N->getElement(2));
1882 EXPECT_EQ(78u, N->getElement(3));
1883 EXPECT_EQ(0u, N->getElement(4));
1885 TempMDExpression Temp = N->clone();
1886 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1889 TEST_F(MDExpressionTest, isValid) {
1890 #define EXPECT_VALID(...) \
1892 uint64_t Elements[] = {__VA_ARGS__}; \
1893 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1895 #define EXPECT_INVALID(...) \
1897 uint64_t Elements[] = {__VA_ARGS__}; \
1898 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1901 // Empty expression should be valid.
1902 EXPECT_TRUE(MDExpression::get(Context, None));
1904 // Valid constructions.
1905 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1906 EXPECT_VALID(dwarf::DW_OP_deref);
1907 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1908 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1909 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1910 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1911 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1913 // Invalid constructions.
1914 EXPECT_INVALID(~0u);
1915 EXPECT_INVALID(dwarf::DW_OP_plus);
1916 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1917 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1918 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1919 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1922 #undef EXPECT_INVALID
1925 typedef MetadataTest MDObjCPropertyTest;
1927 TEST_F(MDObjCPropertyTest, get) {
1928 StringRef Name = "name";
1929 MDFile *File = getFile();
1931 StringRef GetterName = "getter";
1932 StringRef SetterName = "setter";
1933 unsigned Attributes = 7;
1934 MDType *Type = cast<MDBasicType>(getBasicType("basic"));
1936 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1937 SetterName, Attributes, Type);
1939 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1940 EXPECT_EQ(Name, N->getName());
1941 EXPECT_EQ(File, N->getFile());
1942 EXPECT_EQ(Line, N->getLine());
1943 EXPECT_EQ(GetterName, N->getGetterName());
1944 EXPECT_EQ(SetterName, N->getSetterName());
1945 EXPECT_EQ(Attributes, N->getAttributes());
1946 EXPECT_EQ(Type, N->getType());
1947 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1948 SetterName, Attributes, Type));
1950 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1951 SetterName, Attributes, Type));
1952 EXPECT_NE(N, MDObjCProperty::get(Context, Name, getFile(), Line, GetterName,
1953 SetterName, Attributes, Type));
1954 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1955 SetterName, Attributes, Type));
1956 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1957 SetterName, Attributes, Type));
1958 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1959 "other", Attributes, Type));
1960 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1961 SetterName, Attributes + 1, Type));
1962 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1963 SetterName, Attributes,
1964 cast<MDBasicType>(getBasicType("other"))));
1966 TempMDObjCProperty Temp = N->clone();
1967 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1970 typedef MetadataTest MDImportedEntityTest;
1972 TEST_F(MDImportedEntityTest, get) {
1973 unsigned Tag = dwarf::DW_TAG_imported_module;
1974 MDScope *Scope = getSubprogram();
1975 DebugNodeRef Entity = getCompositeType();
1977 StringRef Name = "name";
1979 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1981 EXPECT_EQ(Tag, N->getTag());
1982 EXPECT_EQ(Scope, N->getScope());
1983 EXPECT_EQ(Entity, N->getEntity());
1984 EXPECT_EQ(Line, N->getLine());
1985 EXPECT_EQ(Name, N->getName());
1986 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1989 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1990 Scope, Entity, Line, Name));
1991 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, getSubprogram(), Entity,
1993 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, getCompositeType(),
1996 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1998 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
2000 TempMDImportedEntity Temp = N->clone();
2001 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2004 typedef MetadataTest MetadataAsValueTest;
2006 TEST_F(MetadataAsValueTest, MDNode) {
2007 MDNode *N = MDNode::get(Context, None);
2008 auto *V = MetadataAsValue::get(Context, N);
2009 EXPECT_TRUE(V->getType()->isMetadataTy());
2010 EXPECT_EQ(N, V->getMetadata());
2012 auto *V2 = MetadataAsValue::get(Context, N);
2016 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2017 MDNode *N = MDNode::get(Context, None);
2018 Metadata *Ops[] = {N};
2019 MDNode *N2 = MDNode::get(Context, Ops);
2020 auto *V = MetadataAsValue::get(Context, N2);
2021 EXPECT_TRUE(V->getType()->isMetadataTy());
2022 EXPECT_EQ(N2, V->getMetadata());
2024 auto *V2 = MetadataAsValue::get(Context, N2);
2027 auto *V3 = MetadataAsValue::get(Context, N);
2028 EXPECT_TRUE(V3->getType()->isMetadataTy());
2030 EXPECT_EQ(N, V3->getMetadata());
2033 TEST_F(MetadataAsValueTest, MDNodeConstant) {
2034 auto *C = ConstantInt::getTrue(Context);
2035 auto *MD = ConstantAsMetadata::get(C);
2036 Metadata *Ops[] = {MD};
2037 auto *N = MDNode::get(Context, Ops);
2039 auto *V = MetadataAsValue::get(Context, MD);
2040 EXPECT_TRUE(V->getType()->isMetadataTy());
2041 EXPECT_EQ(MD, V->getMetadata());
2043 auto *V2 = MetadataAsValue::get(Context, N);
2044 EXPECT_EQ(MD, V2->getMetadata());
2048 typedef MetadataTest ValueAsMetadataTest;
2050 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2051 Type *Ty = Type::getInt1PtrTy(Context);
2052 std::unique_ptr<GlobalVariable> GV0(
2053 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2054 auto *MD = ValueAsMetadata::get(GV0.get());
2055 EXPECT_TRUE(MD->getValue() == GV0.get());
2056 ASSERT_TRUE(GV0->use_empty());
2058 std::unique_ptr<GlobalVariable> GV1(
2059 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2060 GV0->replaceAllUsesWith(GV1.get());
2061 EXPECT_TRUE(MD->getValue() == GV1.get());
2064 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2065 // Create a constant.
2066 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2067 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2069 // Create a temporary to prevent nodes from resolving.
2070 auto Temp = MDTuple::getTemporary(Context, None);
2072 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
2073 Metadata *Ops1[] = {CI, CI, Temp.get()};
2074 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
2076 auto *N1 = MDTuple::get(Context, Ops1);
2077 auto *N2 = MDTuple::get(Context, Ops2);
2080 // Tell metadata that the constant is getting deleted.
2082 // After this, N1 will be invalid, so don't touch it.
2083 ValueAsMetadata::handleDeletion(CI->getValue());
2084 EXPECT_EQ(nullptr, N2->getOperand(0));
2085 EXPECT_EQ(nullptr, N2->getOperand(1));
2086 EXPECT_EQ(Temp.get(), N2->getOperand(2));
2088 // Clean up Temp for teardown.
2089 Temp->replaceAllUsesWith(nullptr);
2092 typedef MetadataTest TrackingMDRefTest;
2094 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2095 Type *Ty = Type::getInt1PtrTy(Context);
2096 std::unique_ptr<GlobalVariable> GV0(
2097 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2098 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2099 EXPECT_TRUE(MD->getValue() == GV0.get());
2100 ASSERT_TRUE(GV0->use_empty());
2102 std::unique_ptr<GlobalVariable> GV1(
2103 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2104 GV0->replaceAllUsesWith(GV1.get());
2105 EXPECT_TRUE(MD->getValue() == GV1.get());
2107 // Reset it, so we don't inadvertently test deletion.
2111 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2112 Type *Ty = Type::getInt1PtrTy(Context);
2113 std::unique_ptr<GlobalVariable> GV(
2114 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2115 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2116 EXPECT_TRUE(MD->getValue() == GV.get());
2117 ASSERT_TRUE(GV->use_empty());
2123 TEST(NamedMDNodeTest, Search) {
2124 LLVMContext Context;
2125 ConstantAsMetadata *C =
2126 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2127 ConstantAsMetadata *C2 =
2128 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
2130 Metadata *const V = C;
2131 Metadata *const V2 = C2;
2132 MDNode *n = MDNode::get(Context, V);
2133 MDNode *n2 = MDNode::get(Context, V2);
2135 Module M("MyModule", Context);
2136 const char *Name = "llvm.NMD1";
2137 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2139 NMD->addOperand(n2);
2142 raw_string_ostream oss(Str);
2144 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",