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 Constant *getConstant() {
103 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
105 ConstantAsMetadata *getConstantAsMetadata() {
106 return ConstantAsMetadata::get(getConstant());
108 MDTypeRef getCompositeType() {
109 return MDCompositeType::getDistinct(
110 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr,
111 nullptr, 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "")
114 Function *getFunction(StringRef Name) {
115 return cast<Function>(M.getOrInsertFunction(
116 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
119 typedef MetadataTest MDStringTest;
121 // Test that construction of MDString with different value produces different
122 // MDString objects, even with the same string pointer and nulls in the string.
123 TEST_F(MDStringTest, CreateDifferent) {
124 char x[3] = { 'f', 0, 'A' };
125 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
127 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
131 // Test that creation of MDStrings with the same string contents produces the
132 // same MDString object, even with different pointers.
133 TEST_F(MDStringTest, CreateSame) {
134 char x[4] = { 'a', 'b', 'c', 'X' };
135 char y[4] = { 'a', 'b', 'c', 'Y' };
137 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
138 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
142 // Test that MDString prints out the string we fed it.
143 TEST_F(MDStringTest, PrintingSimple) {
144 char *str = new char[13];
145 strncpy(str, "testing 1 2 3", 13);
146 MDString *s = MDString::get(Context, StringRef(str, 13));
147 strncpy(str, "aaaaaaaaaaaaa", 13);
151 raw_string_ostream oss(Str);
153 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
156 // Test printing of MDString with non-printable characters.
157 TEST_F(MDStringTest, PrintingComplex) {
158 char str[5] = {0, '\n', '"', '\\', (char)-1};
159 MDString *s = MDString::get(Context, StringRef(str+0, 5));
161 raw_string_ostream oss(Str);
163 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
166 typedef MetadataTest MDNodeTest;
168 // Test the two constructors, and containing other Constants.
169 TEST_F(MDNodeTest, Simple) {
170 char x[3] = { 'a', 'b', 'c' };
171 char y[3] = { '1', '2', '3' };
173 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
174 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
175 ConstantAsMetadata *CI = ConstantAsMetadata::get(
176 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
178 std::vector<Metadata *> V;
183 MDNode *n1 = MDNode::get(Context, V);
184 Metadata *const c1 = n1;
185 MDNode *n2 = MDNode::get(Context, c1);
186 Metadata *const c2 = n2;
187 MDNode *n3 = MDNode::get(Context, V);
188 MDNode *n4 = MDNode::getIfExists(Context, V);
189 MDNode *n5 = MDNode::getIfExists(Context, c1);
190 MDNode *n6 = MDNode::getIfExists(Context, c2);
195 EXPECT_EQ(n6, (Metadata *)nullptr);
197 EXPECT_EQ(3u, n1->getNumOperands());
198 EXPECT_EQ(s1, n1->getOperand(0));
199 EXPECT_EQ(CI, n1->getOperand(1));
200 EXPECT_EQ(s2, n1->getOperand(2));
202 EXPECT_EQ(1u, n2->getNumOperands());
203 EXPECT_EQ(n1, n2->getOperand(0));
206 TEST_F(MDNodeTest, Delete) {
207 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
208 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
210 Metadata *const V = LocalAsMetadata::get(I);
211 MDNode *n = MDNode::get(Context, V);
212 TrackingMDRef wvh(n);
219 TEST_F(MDNodeTest, SelfReference) {
223 auto Temp = MDNode::getTemporary(Context, None);
224 Metadata *Args[] = {Temp.get()};
225 MDNode *Self = MDNode::get(Context, Args);
226 Self->replaceOperandWith(0, Self);
227 ASSERT_EQ(Self, Self->getOperand(0));
229 // Self-references should be distinct, so MDNode::get() should grab a
230 // uniqued node that references Self, not Self.
232 MDNode *Ref1 = MDNode::get(Context, Args);
233 MDNode *Ref2 = MDNode::get(Context, Args);
234 EXPECT_NE(Self, Ref1);
235 EXPECT_EQ(Ref1, Ref2);
241 auto Temp = MDNode::getTemporary(Context, None);
242 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
243 MDNode *Self = MDNode::get(Context, Args);
244 Self->replaceOperandWith(0, Self);
245 ASSERT_EQ(Self, Self->getOperand(0));
247 // Self-references should be distinct, so MDNode::get() should grab a
248 // uniqued node that references Self, not Self itself.
250 MDNode *Ref1 = MDNode::get(Context, Args);
251 MDNode *Ref2 = MDNode::get(Context, Args);
252 EXPECT_NE(Self, Ref1);
253 EXPECT_EQ(Ref1, Ref2);
257 TEST_F(MDNodeTest, Print) {
258 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
259 MDString *S = MDString::get(Context, "foo");
260 MDNode *N0 = getNode();
261 MDNode *N1 = getNode(N0);
262 MDNode *N2 = getNode(N0, N1);
264 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
265 MDNode *N = MDNode::get(Context, Args);
267 std::string Expected;
269 raw_string_ostream OS(Expected);
270 OS << "<" << (void *)N << "> = !{";
271 C->printAsOperand(OS);
273 S->printAsOperand(OS);
275 MDNode *Nodes[] = {N0, N1, N2};
276 for (auto *Node : Nodes)
277 OS << ", <" << (void *)Node << ">";
283 raw_string_ostream OS(Actual);
287 EXPECT_EQ(Expected, Actual);
290 #define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
292 std::string Actual_; \
293 raw_string_ostream OS(Actual_); \
296 std::string Expected_(EXPECTED); \
297 EXPECT_EQ(Expected_, Actual_); \
300 TEST_F(MDNodeTest, PrintTemporary) {
301 MDNode *Arg = getNode();
302 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
303 MDNode *N = getNode(Temp.get());
304 Module M("test", Context);
305 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
308 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
309 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
310 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
313 Temp->replaceAllUsesWith(Arg);
316 TEST_F(MDNodeTest, PrintFromModule) {
317 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
318 MDString *S = MDString::get(Context, "foo");
319 MDNode *N0 = getNode();
320 MDNode *N1 = getNode(N0);
321 MDNode *N2 = getNode(N0, N1);
323 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
324 MDNode *N = MDNode::get(Context, Args);
325 Module M("test", Context);
326 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
329 std::string Expected;
331 raw_string_ostream OS(Expected);
333 C->printAsOperand(OS);
335 S->printAsOperand(OS);
336 OS << ", null, !1, !2, !3}";
339 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
342 TEST_F(MDNodeTest, PrintFromFunction) {
343 Module M("test", Context);
344 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
345 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
346 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
347 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
348 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
349 auto *R0 = ReturnInst::Create(Context, BB0);
350 auto *R1 = ReturnInst::Create(Context, BB1);
351 auto *N0 = MDNode::getDistinct(Context, None);
352 auto *N1 = MDNode::getDistinct(Context, None);
353 R0->setMetadata("md", N0);
354 R1->setMetadata("md", N1);
356 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
357 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
360 TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
361 Module M("test", Context);
364 Function::Create(FunctionType::get(Type::getVoidTy(Context),
365 Type::getMetadataTy(Context), false),
366 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
368 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
369 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
370 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
371 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
372 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
373 auto *N0 = MDNode::getDistinct(Context, None);
374 auto *N1 = MDNode::getDistinct(Context, None);
375 auto *MAV0 = MetadataAsValue::get(Context, N0);
376 auto *MAV1 = MetadataAsValue::get(Context, N1);
377 CallInst::Create(Intrinsic, MAV0, "", BB0);
378 CallInst::Create(Intrinsic, MAV1, "", BB1);
380 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
381 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
382 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
383 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
384 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
385 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
387 #undef EXPECT_PRINTER_EQ
389 TEST_F(MDNodeTest, NullOperand) {
391 MDNode *Empty = MDNode::get(Context, None);
393 // metadata !{metadata !{}}
394 Metadata *Ops[] = {Empty};
395 MDNode *N = MDNode::get(Context, Ops);
396 ASSERT_EQ(Empty, N->getOperand(0));
398 // metadata !{metadata !{}} => metadata !{null}
399 N->replaceOperandWith(0, nullptr);
400 ASSERT_EQ(nullptr, N->getOperand(0));
404 MDNode *NullOp = MDNode::get(Context, Ops);
405 ASSERT_EQ(nullptr, NullOp->getOperand(0));
406 EXPECT_EQ(N, NullOp);
409 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
411 MDNode *Empty = MDNode::get(Context, None);
412 ASSERT_TRUE(Empty->isResolved());
413 EXPECT_FALSE(Empty->isDistinct());
416 Metadata *Wrapped1Ops[] = {Empty};
417 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
418 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
419 ASSERT_TRUE(Wrapped1->isResolved());
420 EXPECT_FALSE(Wrapped1->isDistinct());
423 Metadata *Wrapped2Ops[] = {Wrapped1};
424 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
425 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
426 ASSERT_TRUE(Wrapped2->isResolved());
427 EXPECT_FALSE(Wrapped2->isDistinct());
429 // !{!{!{}}} => !{!{}}
430 Wrapped2->replaceOperandWith(0, Empty);
431 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
432 EXPECT_TRUE(Wrapped2->isDistinct());
433 EXPECT_FALSE(Wrapped1->isDistinct());
436 TEST_F(MDNodeTest, getDistinct) {
438 MDNode *Empty = MDNode::get(Context, None);
439 ASSERT_TRUE(Empty->isResolved());
440 ASSERT_FALSE(Empty->isDistinct());
441 ASSERT_EQ(Empty, MDNode::get(Context, None));
444 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
445 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
446 EXPECT_TRUE(Distinct1->isResolved());
447 EXPECT_TRUE(Distinct2->isDistinct());
448 EXPECT_NE(Empty, Distinct1);
449 EXPECT_NE(Empty, Distinct2);
450 EXPECT_NE(Distinct1, Distinct2);
453 ASSERT_EQ(Empty, MDNode::get(Context, None));
456 TEST_F(MDNodeTest, isUniqued) {
457 MDNode *U = MDTuple::get(Context, None);
458 MDNode *D = MDTuple::getDistinct(Context, None);
459 auto T = MDTuple::getTemporary(Context, None);
460 EXPECT_TRUE(U->isUniqued());
461 EXPECT_FALSE(D->isUniqued());
462 EXPECT_FALSE(T->isUniqued());
465 TEST_F(MDNodeTest, isDistinct) {
466 MDNode *U = MDTuple::get(Context, None);
467 MDNode *D = MDTuple::getDistinct(Context, None);
468 auto T = MDTuple::getTemporary(Context, None);
469 EXPECT_FALSE(U->isDistinct());
470 EXPECT_TRUE(D->isDistinct());
471 EXPECT_FALSE(T->isDistinct());
474 TEST_F(MDNodeTest, isTemporary) {
475 MDNode *U = MDTuple::get(Context, None);
476 MDNode *D = MDTuple::getDistinct(Context, None);
477 auto T = MDTuple::getTemporary(Context, None);
478 EXPECT_FALSE(U->isTemporary());
479 EXPECT_FALSE(D->isTemporary());
480 EXPECT_TRUE(T->isTemporary());
483 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
485 auto Temp = MDTuple::getTemporary(Context, None);
486 ASSERT_FALSE(Temp->isResolved());
488 // distinct !{temporary !{}}
489 Metadata *Ops[] = {Temp.get()};
490 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
491 EXPECT_TRUE(Distinct->isResolved());
492 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
494 // temporary !{} => !{}
495 MDNode *Empty = MDNode::get(Context, None);
496 Temp->replaceAllUsesWith(Empty);
497 EXPECT_EQ(Empty, Distinct->getOperand(0));
500 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
502 MDNode *N0 = MDNode::get(Context, None);
505 auto Temp3 = MDTuple::getTemporary(Context, None);
506 Metadata *Ops1[] = {Temp3.get(), nullptr};
507 MDNode *N1 = MDNode::get(Context, Ops1);
510 Metadata *Ops2[] = {Temp3.get(), N0};
511 MDNode *N2 = MDNode::get(Context, Ops2);
514 Metadata *Ops3[] = {N2};
515 MDNode *N3 = MDNode::get(Context, Ops3);
516 Temp3->replaceAllUsesWith(N3);
519 Metadata *Ops4[] = {N1};
520 MDNode *N4 = MDNode::get(Context, Ops4);
522 // Confirm that the cycle prevented RAUW from getting dropped.
523 EXPECT_TRUE(N0->isResolved());
524 EXPECT_FALSE(N1->isResolved());
525 EXPECT_FALSE(N2->isResolved());
526 EXPECT_FALSE(N3->isResolved());
527 EXPECT_FALSE(N4->isResolved());
529 // Create a couple of distinct nodes to observe what's going on.
531 // !5 = distinct !{!2}
532 // !6 = distinct !{!3}
533 Metadata *Ops5[] = {N2};
534 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
535 Metadata *Ops6[] = {N3};
536 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
538 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
539 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
540 // references !3, this can cause a re-entry of handleChangedOperand() when !3
541 // is not ready for it.
543 // !2->replaceOperandWith(1, nullptr)
544 // !2: !{!3, !0} => !{!3, null}
545 // !2->replaceAllUsesWith(!1)
546 // !3: !{!2] => !{!1}
547 // !3->replaceAllUsesWith(!4)
548 N2->replaceOperandWith(1, nullptr);
550 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
551 // under us. Just check that the other nodes are sane.
555 // !5 = distinct !{!1}
556 // !6 = distinct !{!4}
557 EXPECT_EQ(N4, N1->getOperand(0));
558 EXPECT_EQ(N1, N4->getOperand(0));
559 EXPECT_EQ(N1, N5->getOperand(0));
560 EXPECT_EQ(N4, N6->getOperand(0));
563 TEST_F(MDNodeTest, replaceResolvedOperand) {
564 // Check code for replacing one resolved operand with another. If doing this
565 // directly (via replaceOperandWith()) becomes illegal, change the operand to
566 // a global value that gets RAUW'ed.
568 // Use a temporary node to keep N from being resolved.
569 auto Temp = MDTuple::getTemporary(Context, None);
570 Metadata *Ops[] = {nullptr, Temp.get()};
572 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
573 MDNode *N = MDTuple::get(Context, Ops);
574 EXPECT_EQ(nullptr, N->getOperand(0));
575 ASSERT_FALSE(N->isResolved());
577 // Check code for replacing resolved nodes.
578 N->replaceOperandWith(0, Empty);
579 EXPECT_EQ(Empty, N->getOperand(0));
581 // Check code for adding another unresolved operand.
582 N->replaceOperandWith(0, Temp.get());
583 EXPECT_EQ(Temp.get(), N->getOperand(0));
585 // Remove the references to Temp; required for teardown.
586 Temp->replaceAllUsesWith(nullptr);
589 TEST_F(MDNodeTest, replaceWithUniqued) {
590 auto *Empty = MDTuple::get(Context, None);
591 MDTuple *FirstUniqued;
593 Metadata *Ops[] = {Empty};
594 auto Temp = MDTuple::getTemporary(Context, Ops);
595 EXPECT_TRUE(Temp->isTemporary());
597 // Don't expect a collision.
598 auto *Current = Temp.get();
599 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
600 EXPECT_TRUE(FirstUniqued->isUniqued());
601 EXPECT_TRUE(FirstUniqued->isResolved());
602 EXPECT_EQ(Current, FirstUniqued);
605 Metadata *Ops[] = {Empty};
606 auto Temp = MDTuple::getTemporary(Context, Ops);
607 EXPECT_TRUE(Temp->isTemporary());
609 // Should collide with Uniqued above this time.
610 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
611 EXPECT_TRUE(Uniqued->isUniqued());
612 EXPECT_TRUE(Uniqued->isResolved());
613 EXPECT_EQ(FirstUniqued, Uniqued);
616 auto Unresolved = MDTuple::getTemporary(Context, None);
617 Metadata *Ops[] = {Unresolved.get()};
618 auto Temp = MDTuple::getTemporary(Context, Ops);
619 EXPECT_TRUE(Temp->isTemporary());
621 // Shouldn't be resolved.
622 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
623 EXPECT_TRUE(Uniqued->isUniqued());
624 EXPECT_FALSE(Uniqued->isResolved());
626 // Should be a different node.
627 EXPECT_NE(FirstUniqued, Uniqued);
629 // Should resolve when we update its node (note: be careful to avoid a
630 // collision with any other nodes above).
631 Uniqued->replaceOperandWith(0, nullptr);
632 EXPECT_TRUE(Uniqued->isResolved());
636 TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
638 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
639 EXPECT_FALSE(Op->isResolved());
642 Metadata *Ops[] = {Op};
643 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
644 EXPECT_FALSE(N->isResolved());
646 // temp !{temp !{}} => !{temp !{}}
647 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
648 EXPECT_FALSE(N->isResolved());
650 // !{temp !{}} => !{!{}}
651 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
652 EXPECT_TRUE(Op->isResolved());
653 EXPECT_TRUE(N->isResolved());
656 TEST_F(MDNodeTest, replaceWithUniquedChangingOperand) {
658 Type *Ty = Type::getInt1PtrTy(Context);
659 std::unique_ptr<GlobalVariable> GV(
660 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
661 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
664 Metadata *Ops[] = {Op};
665 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
667 // temp !{i1* @GV} => !{i1* @GV}
668 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
669 ASSERT_TRUE(N->isUniqued());
671 // !{i1* @GV} => !{null}
673 ASSERT_TRUE(N->isUniqued());
674 Metadata *NullOps[] = {nullptr};
675 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
678 TEST_F(MDNodeTest, replaceWithDistinct) {
680 auto *Empty = MDTuple::get(Context, None);
681 Metadata *Ops[] = {Empty};
682 auto Temp = MDTuple::getTemporary(Context, Ops);
683 EXPECT_TRUE(Temp->isTemporary());
685 // Don't expect a collision.
686 auto *Current = Temp.get();
687 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
688 EXPECT_TRUE(Distinct->isDistinct());
689 EXPECT_TRUE(Distinct->isResolved());
690 EXPECT_EQ(Current, Distinct);
693 auto Unresolved = MDTuple::getTemporary(Context, None);
694 Metadata *Ops[] = {Unresolved.get()};
695 auto Temp = MDTuple::getTemporary(Context, Ops);
696 EXPECT_TRUE(Temp->isTemporary());
698 // Don't expect a collision.
699 auto *Current = Temp.get();
700 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
701 EXPECT_TRUE(Distinct->isDistinct());
702 EXPECT_TRUE(Distinct->isResolved());
703 EXPECT_EQ(Current, Distinct);
705 // Cleanup; required for teardown.
706 Unresolved->replaceAllUsesWith(nullptr);
710 TEST_F(MDNodeTest, replaceWithPermanent) {
711 Metadata *Ops[] = {nullptr};
712 auto Temp = MDTuple::getTemporary(Context, Ops);
713 auto *T = Temp.get();
715 // U is a normal, uniqued node that references T.
716 auto *U = MDTuple::get(Context, T);
717 EXPECT_TRUE(U->isUniqued());
719 // Make Temp self-referencing.
720 Temp->replaceOperandWith(0, T);
722 // Try to uniquify Temp. This should, despite the name in the API, give a
723 // 'distinct' node, since self-references aren't allowed to be uniqued.
725 // Since it's distinct, N should have the same address as when it was a
726 // temporary (i.e., be equal to T not U).
727 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
729 EXPECT_TRUE(N->isDistinct());
731 // U should be the canonical unique node with N as the argument.
732 EXPECT_EQ(U, MDTuple::get(Context, N));
733 EXPECT_TRUE(U->isUniqued());
735 // This temporary should collide with U when replaced, but it should still be
737 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
738 EXPECT_TRUE(U->isUniqued());
740 // This temporary should become a new uniqued node.
741 auto Temp2 = MDTuple::getTemporary(Context, U);
742 auto *V = Temp2.get();
743 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
744 EXPECT_TRUE(V->isUniqued());
745 EXPECT_EQ(U, V->getOperand(0));
748 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
750 EXPECT_EQ(nullptr, Ref.get());
752 auto Temp = MDTuple::getTemporary(Context, None);
753 Ref.reset(Temp.get());
754 EXPECT_EQ(Temp.get(), Ref.get());
756 EXPECT_EQ(nullptr, Ref.get());
759 typedef MetadataTest MDLocationTest;
761 TEST_F(MDLocationTest, Overflow) {
762 MDSubprogram *N = getSubprogram();
764 MDLocation *L = MDLocation::get(Context, 2, 7, N);
765 EXPECT_EQ(2u, L->getLine());
766 EXPECT_EQ(7u, L->getColumn());
768 unsigned U16 = 1u << 16;
770 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
771 EXPECT_EQ(UINT32_MAX, L->getLine());
772 EXPECT_EQ(U16 - 1, L->getColumn());
775 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
776 EXPECT_EQ(UINT32_MAX, L->getLine());
777 EXPECT_EQ(0u, L->getColumn());
780 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
781 EXPECT_EQ(UINT32_MAX, L->getLine());
782 EXPECT_EQ(0u, L->getColumn());
786 TEST_F(MDLocationTest, getDistinct) {
787 MDNode *N = getSubprogram();
788 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
789 EXPECT_TRUE(L0->isDistinct());
790 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
791 EXPECT_FALSE(L1->isDistinct());
792 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
795 TEST_F(MDLocationTest, getTemporary) {
796 MDNode *N = MDNode::get(Context, None);
797 auto L = MDLocation::getTemporary(Context, 2, 7, N);
798 EXPECT_TRUE(L->isTemporary());
799 EXPECT_FALSE(L->isResolved());
802 typedef MetadataTest GenericDebugNodeTest;
804 TEST_F(GenericDebugNodeTest, get) {
805 StringRef Header = "header";
806 auto *Empty = MDNode::get(Context, None);
807 Metadata *Ops1[] = {Empty};
808 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
809 EXPECT_EQ(15u, N->getTag());
810 EXPECT_EQ(2u, N->getNumOperands());
811 EXPECT_EQ(Header, N->getHeader());
812 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
813 EXPECT_EQ(1u, N->getNumDwarfOperands());
814 EXPECT_EQ(Empty, N->getDwarfOperand(0));
815 EXPECT_EQ(Empty, N->getOperand(1));
816 ASSERT_TRUE(N->isUniqued());
818 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
820 N->replaceOperandWith(1, nullptr);
821 EXPECT_EQ(15u, N->getTag());
822 EXPECT_EQ(Header, N->getHeader());
823 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
824 ASSERT_TRUE(N->isUniqued());
826 Metadata *Ops2[] = {nullptr};
827 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
829 N->replaceDwarfOperandWith(0, Empty);
830 EXPECT_EQ(15u, N->getTag());
831 EXPECT_EQ(Header, N->getHeader());
832 EXPECT_EQ(Empty, N->getDwarfOperand(0));
833 ASSERT_TRUE(N->isUniqued());
834 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
836 TempGenericDebugNode Temp = N->clone();
837 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
840 TEST_F(GenericDebugNodeTest, getEmptyHeader) {
841 // Canonicalize !"" to null.
842 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
843 EXPECT_EQ(StringRef(), N->getHeader());
844 EXPECT_EQ(nullptr, N->getOperand(0));
847 typedef MetadataTest MDSubrangeTest;
849 TEST_F(MDSubrangeTest, get) {
850 auto *N = MDSubrange::get(Context, 5, 7);
851 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
852 EXPECT_EQ(5, N->getCount());
853 EXPECT_EQ(7, N->getLowerBound());
854 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
855 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
857 TempMDSubrange Temp = N->clone();
858 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
861 TEST_F(MDSubrangeTest, getEmptyArray) {
862 auto *N = MDSubrange::get(Context, -1, 0);
863 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
864 EXPECT_EQ(-1, N->getCount());
865 EXPECT_EQ(0, N->getLowerBound());
866 EXPECT_EQ(N, MDSubrange::get(Context, -1, 0));
869 typedef MetadataTest MDEnumeratorTest;
871 TEST_F(MDEnumeratorTest, get) {
872 auto *N = MDEnumerator::get(Context, 7, "name");
873 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
874 EXPECT_EQ(7, N->getValue());
875 EXPECT_EQ("name", N->getName());
876 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
878 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
879 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
881 TempMDEnumerator Temp = N->clone();
882 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
885 typedef MetadataTest MDBasicTypeTest;
887 TEST_F(MDBasicTypeTest, get) {
889 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
890 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
891 EXPECT_EQ("special", N->getName());
892 EXPECT_EQ(33u, N->getSizeInBits());
893 EXPECT_EQ(26u, N->getAlignInBits());
894 EXPECT_EQ(7u, N->getEncoding());
895 EXPECT_EQ(0u, N->getLine());
896 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
899 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
900 "special", 33, 26, 7));
902 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
903 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
905 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
907 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
910 TempMDBasicType Temp = N->clone();
911 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
914 TEST_F(MDBasicTypeTest, getWithLargeValues) {
915 auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
916 UINT64_MAX, UINT64_MAX - 1, 7);
917 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
918 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
921 TEST_F(MDBasicTypeTest, getUnspecified) {
923 MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
924 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
925 EXPECT_EQ("unspecified", N->getName());
926 EXPECT_EQ(0u, N->getSizeInBits());
927 EXPECT_EQ(0u, N->getAlignInBits());
928 EXPECT_EQ(0u, N->getEncoding());
929 EXPECT_EQ(0u, N->getLine());
932 typedef MetadataTest MDTypeTest;
934 TEST_F(MDTypeTest, clone) {
935 // Check that MDType has a specialized clone that returns TempMDType.
936 MDType *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
937 dwarf::DW_ATE_signed);
939 TempMDType Temp = N->clone();
940 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
943 TEST_F(MDTypeTest, setFlags) {
945 Metadata *TypesOps[] = {nullptr};
946 Metadata *Types = MDTuple::get(Context, TypesOps);
948 MDType *D = MDSubroutineType::getDistinct(Context, 0u, Types);
949 EXPECT_EQ(0u, D->getFlags());
950 D->setFlags(DIDescriptor::FlagRValueReference);
951 EXPECT_EQ(DIDescriptor::FlagRValueReference, D->getFlags());
953 EXPECT_EQ(0u, D->getFlags());
955 TempMDType T = MDSubroutineType::getTemporary(Context, 0u, Types);
956 EXPECT_EQ(0u, T->getFlags());
957 T->setFlags(DIDescriptor::FlagRValueReference);
958 EXPECT_EQ(DIDescriptor::FlagRValueReference, T->getFlags());
960 EXPECT_EQ(0u, T->getFlags());
963 typedef MetadataTest MDDerivedTypeTest;
965 TEST_F(MDDerivedTypeTest, get) {
966 MDFile *File = getFile();
967 MDScopeRef Scope = getSubprogramRef();
968 MDTypeRef BaseType = getBasicType("basic");
969 MDTuple *ExtraData = getTuple();
971 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
972 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
973 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
974 EXPECT_EQ("something", N->getName());
975 EXPECT_EQ(File, N->getFile());
976 EXPECT_EQ(1u, N->getLine());
977 EXPECT_EQ(Scope, N->getScope());
978 EXPECT_EQ(BaseType, N->getBaseType());
979 EXPECT_EQ(2u, N->getSizeInBits());
980 EXPECT_EQ(3u, N->getAlignInBits());
981 EXPECT_EQ(4u, N->getOffsetInBits());
982 EXPECT_EQ(5u, N->getFlags());
983 EXPECT_EQ(ExtraData, N->getExtraData());
984 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
985 "something", File, 1, Scope, BaseType, 2, 3,
988 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
989 "something", File, 1, Scope, BaseType, 2, 3,
991 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
992 File, 1, Scope, BaseType, 2, 3, 4, 5,
994 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
995 "something", getFile(), 1, Scope, BaseType, 2,
996 3, 4, 5, ExtraData));
997 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
998 "something", File, 2, Scope, BaseType, 2, 3,
1000 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1001 "something", File, 1, getSubprogramRef(),
1002 BaseType, 2, 3, 4, 5, ExtraData));
1003 EXPECT_NE(N, MDDerivedType::get(
1004 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1005 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
1006 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1007 "something", File, 1, Scope, BaseType, 3, 3,
1009 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1010 "something", File, 1, Scope, BaseType, 2, 2,
1012 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1013 "something", File, 1, Scope, BaseType, 2, 3,
1015 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1016 "something", File, 1, Scope, BaseType, 2, 3,
1018 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1019 "something", File, 1, Scope, BaseType, 2, 3,
1022 TempMDDerivedType Temp = N->clone();
1023 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1026 TEST_F(MDDerivedTypeTest, getWithLargeValues) {
1027 MDFile *File = getFile();
1028 MDScopeRef Scope = getSubprogramRef();
1029 MDTypeRef BaseType = getBasicType("basic");
1030 MDTuple *ExtraData = getTuple();
1032 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
1033 File, 1, Scope, BaseType, UINT64_MAX,
1034 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1035 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1036 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1037 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1040 typedef MetadataTest MDCompositeTypeTest;
1042 TEST_F(MDCompositeTypeTest, get) {
1043 unsigned Tag = dwarf::DW_TAG_structure_type;
1044 StringRef Name = "some name";
1045 MDFile *File = getFile();
1047 MDScopeRef Scope = getSubprogramRef();
1048 MDTypeRef BaseType = getCompositeType();
1049 uint64_t SizeInBits = 2;
1050 uint64_t AlignInBits = 3;
1051 uint64_t OffsetInBits = 4;
1053 MDTuple *Elements = getTuple();
1054 unsigned RuntimeLang = 6;
1055 MDTypeRef VTableHolder = getCompositeType();
1056 MDTuple *TemplateParams = getTuple();
1057 StringRef Identifier = "some id";
1059 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1060 BaseType, SizeInBits, AlignInBits,
1061 OffsetInBits, Flags, Elements, RuntimeLang,
1062 VTableHolder, TemplateParams, Identifier);
1063 EXPECT_EQ(Tag, N->getTag());
1064 EXPECT_EQ(Name, N->getName());
1065 EXPECT_EQ(File, N->getFile());
1066 EXPECT_EQ(Line, N->getLine());
1067 EXPECT_EQ(Scope, N->getScope());
1068 EXPECT_EQ(BaseType, N->getBaseType());
1069 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1070 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1071 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1072 EXPECT_EQ(Flags, N->getFlags());
1073 EXPECT_EQ(Elements, N->getElements().get());
1074 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1075 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1076 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1077 EXPECT_EQ(Identifier, N->getIdentifier());
1079 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1080 BaseType, SizeInBits, AlignInBits,
1081 OffsetInBits, Flags, Elements, RuntimeLang,
1082 VTableHolder, TemplateParams, Identifier));
1084 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
1085 BaseType, SizeInBits, AlignInBits,
1086 OffsetInBits, Flags, Elements, RuntimeLang,
1087 VTableHolder, TemplateParams, Identifier));
1088 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
1089 BaseType, SizeInBits, AlignInBits,
1090 OffsetInBits, Flags, Elements, RuntimeLang,
1091 VTableHolder, TemplateParams, Identifier));
1092 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
1093 BaseType, SizeInBits, AlignInBits,
1094 OffsetInBits, Flags, Elements, RuntimeLang,
1095 VTableHolder, TemplateParams, Identifier));
1096 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
1097 BaseType, SizeInBits, AlignInBits,
1098 OffsetInBits, Flags, Elements, RuntimeLang,
1099 VTableHolder, TemplateParams, Identifier));
1100 EXPECT_NE(N, MDCompositeType::get(
1101 Context, Tag, Name, File, Line, getSubprogramRef(), BaseType,
1102 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1103 RuntimeLang, VTableHolder, TemplateParams, Identifier));
1104 EXPECT_NE(N, MDCompositeType::get(
1105 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1106 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1107 RuntimeLang, VTableHolder, TemplateParams, Identifier));
1108 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1109 BaseType, SizeInBits + 1, AlignInBits,
1110 OffsetInBits, Flags, Elements, RuntimeLang,
1111 VTableHolder, TemplateParams, Identifier));
1112 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1113 BaseType, SizeInBits, AlignInBits + 1,
1114 OffsetInBits, Flags, Elements, RuntimeLang,
1115 VTableHolder, TemplateParams, Identifier));
1116 EXPECT_NE(N, MDCompositeType::get(
1117 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1118 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1119 VTableHolder, TemplateParams, Identifier));
1120 EXPECT_NE(N, MDCompositeType::get(
1121 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1122 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1123 VTableHolder, TemplateParams, Identifier));
1124 EXPECT_NE(N, MDCompositeType::get(
1125 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1126 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1127 VTableHolder, TemplateParams, Identifier));
1128 EXPECT_NE(N, MDCompositeType::get(
1129 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1130 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1131 VTableHolder, TemplateParams, Identifier));
1132 EXPECT_NE(N, MDCompositeType::get(
1133 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1134 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1135 getCompositeType(), TemplateParams, Identifier));
1136 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1137 BaseType, SizeInBits, AlignInBits,
1138 OffsetInBits, Flags, Elements, RuntimeLang,
1139 VTableHolder, getTuple(), Identifier));
1140 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1141 BaseType, SizeInBits, AlignInBits,
1142 OffsetInBits, Flags, Elements, RuntimeLang,
1143 VTableHolder, TemplateParams, "other"));
1145 // Be sure that missing identifiers get null pointers.
1146 EXPECT_FALSE(MDCompositeType::get(
1147 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1148 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1149 VTableHolder, TemplateParams, "")->getRawIdentifier());
1150 EXPECT_FALSE(MDCompositeType::get(
1151 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1152 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1153 VTableHolder, TemplateParams)->getRawIdentifier());
1155 TempMDCompositeType Temp = N->clone();
1156 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1159 TEST_F(MDCompositeTypeTest, getWithLargeValues) {
1160 unsigned Tag = dwarf::DW_TAG_structure_type;
1161 StringRef Name = "some name";
1162 MDFile *File = getFile();
1164 MDScopeRef Scope = getSubprogramRef();
1165 MDTypeRef BaseType = getCompositeType();
1166 uint64_t SizeInBits = UINT64_MAX;
1167 uint64_t AlignInBits = UINT64_MAX - 1;
1168 uint64_t OffsetInBits = UINT64_MAX - 2;
1170 MDTuple *Elements = getTuple();
1171 unsigned RuntimeLang = 6;
1172 MDTypeRef VTableHolder = getCompositeType();
1173 MDTuple *TemplateParams = getTuple();
1174 StringRef Identifier = "some id";
1176 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1177 BaseType, SizeInBits, AlignInBits,
1178 OffsetInBits, Flags, Elements, RuntimeLang,
1179 VTableHolder, TemplateParams, Identifier);
1180 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1181 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1182 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1185 TEST_F(MDCompositeTypeTest, replaceOperands) {
1186 unsigned Tag = dwarf::DW_TAG_structure_type;
1187 StringRef Name = "some name";
1188 MDFile *File = getFile();
1190 MDScopeRef Scope = getSubprogramRef();
1191 MDTypeRef BaseType = getCompositeType();
1192 uint64_t SizeInBits = 2;
1193 uint64_t AlignInBits = 3;
1194 uint64_t OffsetInBits = 4;
1196 unsigned RuntimeLang = 6;
1197 StringRef Identifier = "some id";
1199 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1200 BaseType, SizeInBits, AlignInBits,
1201 OffsetInBits, Flags, nullptr, RuntimeLang,
1202 nullptr, nullptr, Identifier);
1204 auto *Elements = MDTuple::getDistinct(Context, None);
1205 EXPECT_EQ(nullptr, N->getElements().get());
1206 N->replaceElements(Elements);
1207 EXPECT_EQ(Elements, N->getElements().get());
1208 N->replaceElements(nullptr);
1209 EXPECT_EQ(nullptr, N->getElements().get());
1211 MDTypeRef VTableHolder = getCompositeType();
1212 EXPECT_EQ(nullptr, N->getVTableHolder());
1213 N->replaceVTableHolder(VTableHolder);
1214 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1215 N->replaceVTableHolder(nullptr);
1216 EXPECT_EQ(nullptr, N->getVTableHolder());
1218 auto *TemplateParams = MDTuple::getDistinct(Context, None);
1219 EXPECT_EQ(nullptr, N->getTemplateParams().get());
1220 N->replaceTemplateParams(TemplateParams);
1221 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1222 N->replaceTemplateParams(nullptr);
1223 EXPECT_EQ(nullptr, N->getTemplateParams().get());
1226 typedef MetadataTest MDSubroutineTypeTest;
1228 TEST_F(MDSubroutineTypeTest, get) {
1230 MDTuple *TypeArray = getTuple();
1232 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
1233 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1234 EXPECT_EQ(Flags, N->getFlags());
1235 EXPECT_EQ(TypeArray, N->getTypeArray().get());
1236 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
1238 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
1239 EXPECT_NE(N, MDSubroutineType::get(Context, Flags, getTuple()));
1241 TempMDSubroutineType Temp = N->clone();
1242 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1244 // Test always-empty operands.
1245 EXPECT_EQ(nullptr, N->getScope());
1246 EXPECT_EQ(nullptr, N->getFile());
1247 EXPECT_EQ("", N->getName());
1248 EXPECT_EQ(nullptr, N->getBaseType());
1249 EXPECT_EQ(nullptr, N->getVTableHolder());
1250 EXPECT_EQ(nullptr, N->getTemplateParams().get());
1251 EXPECT_EQ("", N->getIdentifier());
1254 typedef MetadataTest MDFileTest;
1256 TEST_F(MDFileTest, get) {
1257 StringRef Filename = "file";
1258 StringRef Directory = "dir";
1259 auto *N = MDFile::get(Context, Filename, Directory);
1261 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1262 EXPECT_EQ(Filename, N->getFilename());
1263 EXPECT_EQ(Directory, N->getDirectory());
1264 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
1266 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1267 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
1269 TempMDFile Temp = N->clone();
1270 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1273 TEST_F(MDFileTest, ScopeGetFile) {
1274 // Ensure that MDScope::getFile() returns itself.
1275 MDScope *N = MDFile::get(Context, "file", "dir");
1276 EXPECT_EQ(N, N->getFile());
1279 typedef MetadataTest MDCompileUnitTest;
1281 TEST_F(MDCompileUnitTest, get) {
1282 unsigned SourceLanguage = 1;
1283 MDFile *File = getFile();
1284 StringRef Producer = "some producer";
1285 bool IsOptimized = false;
1286 StringRef Flags = "flag after flag";
1287 unsigned RuntimeVersion = 2;
1288 StringRef SplitDebugFilename = "another/file";
1289 unsigned EmissionKind = 3;
1290 MDTuple *EnumTypes = getTuple();
1291 MDTuple *RetainedTypes = getTuple();
1292 MDTuple *Subprograms = getTuple();
1293 MDTuple *GlobalVariables = getTuple();
1294 MDTuple *ImportedEntities = getTuple();
1295 auto *N = MDCompileUnit::get(
1296 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1297 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1298 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1300 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1301 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1302 EXPECT_EQ(File, N->getFile());
1303 EXPECT_EQ(Producer, N->getProducer());
1304 EXPECT_EQ(IsOptimized, N->isOptimized());
1305 EXPECT_EQ(Flags, N->getFlags());
1306 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1307 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1308 EXPECT_EQ(EmissionKind, N->getEmissionKind());
1309 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1310 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
1311 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1312 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1313 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
1314 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1315 IsOptimized, Flags, RuntimeVersion,
1316 SplitDebugFilename, EmissionKind, EnumTypes,
1317 RetainedTypes, Subprograms, GlobalVariables,
1320 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1321 IsOptimized, Flags, RuntimeVersion,
1322 SplitDebugFilename, EmissionKind, EnumTypes,
1323 RetainedTypes, Subprograms, GlobalVariables,
1325 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, getFile(), Producer,
1326 IsOptimized, Flags, RuntimeVersion,
1327 SplitDebugFilename, EmissionKind, EnumTypes,
1328 RetainedTypes, Subprograms, GlobalVariables,
1330 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1331 IsOptimized, Flags, RuntimeVersion,
1332 SplitDebugFilename, EmissionKind, EnumTypes,
1333 RetainedTypes, Subprograms, GlobalVariables,
1335 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1336 !IsOptimized, Flags, RuntimeVersion,
1337 SplitDebugFilename, EmissionKind, EnumTypes,
1338 RetainedTypes, Subprograms, GlobalVariables,
1340 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1341 IsOptimized, "other", RuntimeVersion,
1342 SplitDebugFilename, EmissionKind, EnumTypes,
1343 RetainedTypes, Subprograms, GlobalVariables,
1345 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1346 IsOptimized, Flags, RuntimeVersion + 1,
1347 SplitDebugFilename, EmissionKind, EnumTypes,
1348 RetainedTypes, Subprograms, GlobalVariables,
1351 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1352 IsOptimized, Flags, RuntimeVersion, "other",
1353 EmissionKind, EnumTypes, RetainedTypes,
1354 Subprograms, GlobalVariables, ImportedEntities));
1355 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1356 IsOptimized, Flags, RuntimeVersion,
1357 SplitDebugFilename, EmissionKind + 1,
1358 EnumTypes, RetainedTypes, Subprograms,
1359 GlobalVariables, ImportedEntities));
1360 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1361 IsOptimized, Flags, RuntimeVersion,
1362 SplitDebugFilename, EmissionKind, getTuple(),
1363 RetainedTypes, Subprograms, GlobalVariables,
1365 EXPECT_NE(N, MDCompileUnit::get(
1366 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1367 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1368 getTuple(), Subprograms, GlobalVariables, ImportedEntities));
1369 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1370 IsOptimized, Flags, RuntimeVersion,
1371 SplitDebugFilename, EmissionKind, EnumTypes,
1372 RetainedTypes, getTuple(), GlobalVariables,
1374 EXPECT_NE(N, MDCompileUnit::get(
1375 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1376 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1377 RetainedTypes, Subprograms, getTuple(), ImportedEntities));
1378 EXPECT_NE(N, MDCompileUnit::get(
1379 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1380 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1381 RetainedTypes, Subprograms, GlobalVariables, getTuple()));
1383 TempMDCompileUnit Temp = N->clone();
1384 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1387 TEST_F(MDCompileUnitTest, replaceArrays) {
1388 unsigned SourceLanguage = 1;
1389 MDFile *File = getFile();
1390 StringRef Producer = "some producer";
1391 bool IsOptimized = false;
1392 StringRef Flags = "flag after flag";
1393 unsigned RuntimeVersion = 2;
1394 StringRef SplitDebugFilename = "another/file";
1395 unsigned EmissionKind = 3;
1396 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1397 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1398 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
1399 auto *N = MDCompileUnit::get(
1400 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1401 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1402 RetainedTypes, nullptr, nullptr, ImportedEntities);
1404 auto *Subprograms = MDTuple::getDistinct(Context, None);
1405 EXPECT_EQ(nullptr, N->getSubprograms().get());
1406 N->replaceSubprograms(Subprograms);
1407 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1408 N->replaceSubprograms(nullptr);
1409 EXPECT_EQ(nullptr, N->getSubprograms().get());
1411 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1412 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
1413 N->replaceGlobalVariables(GlobalVariables);
1414 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1415 N->replaceGlobalVariables(nullptr);
1416 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
1419 typedef MetadataTest MDSubprogramTest;
1421 TEST_F(MDSubprogramTest, get) {
1422 MDScopeRef Scope = getCompositeType();
1423 StringRef Name = "name";
1424 StringRef LinkageName = "linkage";
1425 MDFile *File = getFile();
1427 MDSubroutineType *Type = getSubroutineType();
1428 bool IsLocalToUnit = false;
1429 bool IsDefinition = true;
1430 unsigned ScopeLine = 3;
1431 MDTypeRef ContainingType = getCompositeType();
1432 unsigned Virtuality = 4;
1433 unsigned VirtualIndex = 5;
1435 bool IsOptimized = false;
1436 llvm::Function *Function = getFunction("foo");
1437 MDTuple *TemplateParams = getTuple();
1438 MDSubprogram *Declaration = getSubprogram();
1439 MDTuple *Variables = getTuple();
1441 auto *N = MDSubprogram::get(
1442 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1443 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1444 IsOptimized, Function, TemplateParams, Declaration, Variables);
1446 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1447 EXPECT_EQ(Scope, N->getScope());
1448 EXPECT_EQ(Name, N->getName());
1449 EXPECT_EQ(LinkageName, N->getLinkageName());
1450 EXPECT_EQ(File, N->getFile());
1451 EXPECT_EQ(Line, N->getLine());
1452 EXPECT_EQ(Type, N->getType());
1453 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1454 EXPECT_EQ(IsDefinition, N->isDefinition());
1455 EXPECT_EQ(ScopeLine, N->getScopeLine());
1456 EXPECT_EQ(ContainingType, N->getContainingType());
1457 EXPECT_EQ(Virtuality, N->getVirtuality());
1458 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1459 EXPECT_EQ(Flags, N->getFlags());
1460 EXPECT_EQ(IsOptimized, N->isOptimized());
1461 EXPECT_EQ(Function, N->getFunction());
1462 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1463 EXPECT_EQ(Declaration, N->getDeclaration());
1464 EXPECT_EQ(Variables, N->getVariables().get());
1465 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1466 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1467 ContainingType, Virtuality, VirtualIndex,
1468 Flags, IsOptimized, Function, TemplateParams,
1469 Declaration, Variables));
1471 EXPECT_NE(N, MDSubprogram::get(Context, getCompositeType(), Name, LinkageName,
1472 File, Line, Type, IsLocalToUnit, IsDefinition,
1473 ScopeLine, ContainingType, Virtuality,
1474 VirtualIndex, Flags, IsOptimized, Function,
1475 TemplateParams, Declaration, Variables));
1476 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1477 Line, Type, IsLocalToUnit, IsDefinition,
1478 ScopeLine, ContainingType, Virtuality,
1479 VirtualIndex, Flags, IsOptimized, Function,
1480 TemplateParams, Declaration, Variables));
1481 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1482 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1483 ContainingType, Virtuality, VirtualIndex,
1484 Flags, IsOptimized, Function, TemplateParams,
1485 Declaration, Variables));
1486 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, getFile(),
1487 Line, Type, IsLocalToUnit, IsDefinition,
1488 ScopeLine, ContainingType, Virtuality,
1489 VirtualIndex, Flags, IsOptimized, Function,
1490 TemplateParams, Declaration, Variables));
1491 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1492 Line + 1, Type, IsLocalToUnit, IsDefinition,
1493 ScopeLine, ContainingType, Virtuality,
1494 VirtualIndex, Flags, IsOptimized, Function,
1495 TemplateParams, Declaration, Variables));
1496 EXPECT_NE(N, MDSubprogram::get(
1497 Context, Scope, Name, LinkageName, File, Line,
1498 getSubroutineType(), IsLocalToUnit, IsDefinition, ScopeLine,
1499 ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1500 Function, TemplateParams, Declaration, Variables));
1501 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1502 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1503 ContainingType, Virtuality, VirtualIndex,
1504 Flags, IsOptimized, Function, TemplateParams,
1505 Declaration, Variables));
1506 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1507 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1508 ContainingType, Virtuality, VirtualIndex,
1509 Flags, IsOptimized, Function, TemplateParams,
1510 Declaration, Variables));
1511 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1512 Type, IsLocalToUnit, IsDefinition,
1513 ScopeLine + 1, ContainingType, Virtuality,
1514 VirtualIndex, Flags, IsOptimized, Function,
1515 TemplateParams, Declaration, Variables));
1516 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1517 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1518 getCompositeType(), Virtuality, VirtualIndex,
1519 Flags, IsOptimized, Function, TemplateParams,
1520 Declaration, Variables));
1521 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1522 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1523 ContainingType, Virtuality + 1, VirtualIndex,
1524 Flags, IsOptimized, Function, TemplateParams,
1525 Declaration, Variables));
1526 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1527 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1528 ContainingType, Virtuality, VirtualIndex + 1,
1529 Flags, IsOptimized, Function, TemplateParams,
1530 Declaration, Variables));
1531 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1532 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1533 ContainingType, Virtuality, VirtualIndex,
1534 ~Flags, IsOptimized, Function, TemplateParams,
1535 Declaration, Variables));
1536 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1537 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1538 ContainingType, Virtuality, VirtualIndex,
1539 Flags, !IsOptimized, Function, TemplateParams,
1540 Declaration, Variables));
1541 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1542 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1543 ContainingType, Virtuality, VirtualIndex,
1544 Flags, IsOptimized, getFunction("bar"),
1545 TemplateParams, Declaration, Variables));
1546 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1547 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1548 ContainingType, Virtuality, VirtualIndex,
1549 Flags, IsOptimized, Function, getTuple(),
1550 Declaration, Variables));
1551 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1552 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1553 ContainingType, Virtuality, VirtualIndex,
1554 Flags, IsOptimized, Function, TemplateParams,
1555 getSubprogram(), Variables));
1556 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1557 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1558 ContainingType, Virtuality, VirtualIndex,
1559 Flags, IsOptimized, Function, TemplateParams,
1560 Declaration, getTuple()));
1562 TempMDSubprogram Temp = N->clone();
1563 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1566 TEST_F(MDSubprogramTest, replaceFunction) {
1567 MDScopeRef Scope = getCompositeType();
1568 StringRef Name = "name";
1569 StringRef LinkageName = "linkage";
1570 MDFile *File = getFile();
1572 MDSubroutineType *Type = getSubroutineType();
1573 bool IsLocalToUnit = false;
1574 bool IsDefinition = true;
1575 unsigned ScopeLine = 3;
1576 MDTypeRef ContainingType = getCompositeType();
1577 unsigned Virtuality = 4;
1578 unsigned VirtualIndex = 5;
1580 bool IsOptimized = false;
1581 MDTuple *TemplateParams = getTuple();
1582 MDSubprogram *Declaration = getSubprogram();
1583 MDTuple *Variables = getTuple();
1585 auto *N = MDSubprogram::get(
1586 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1587 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1588 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1590 EXPECT_EQ(nullptr, N->getFunction());
1592 std::unique_ptr<Function> F(
1593 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1594 GlobalValue::ExternalLinkage));
1595 N->replaceFunction(F.get());
1596 EXPECT_EQ(F.get(), N->getFunction());
1598 N->replaceFunction(nullptr);
1599 EXPECT_EQ(nullptr, N->getFunction());
1602 typedef MetadataTest MDLexicalBlockTest;
1604 TEST_F(MDLexicalBlockTest, get) {
1605 MDLocalScope *Scope = getSubprogram();
1606 MDFile *File = getFile();
1608 unsigned Column = 8;
1610 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1612 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1613 EXPECT_EQ(Scope, N->getScope());
1614 EXPECT_EQ(File, N->getFile());
1615 EXPECT_EQ(Line, N->getLine());
1616 EXPECT_EQ(Column, N->getColumn());
1617 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1620 MDLexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1621 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, getFile(), Line, Column));
1622 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1623 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
1625 TempMDLexicalBlock Temp = N->clone();
1626 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1629 typedef MetadataTest MDLexicalBlockFileTest;
1631 TEST_F(MDLexicalBlockFileTest, get) {
1632 MDLocalScope *Scope = getSubprogram();
1633 MDFile *File = getFile();
1634 unsigned Discriminator = 5;
1636 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1638 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1639 EXPECT_EQ(Scope, N->getScope());
1640 EXPECT_EQ(File, N->getFile());
1641 EXPECT_EQ(Discriminator, N->getDiscriminator());
1642 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1644 EXPECT_NE(N, MDLexicalBlockFile::get(Context, getSubprogram(), File,
1647 MDLexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
1649 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1651 TempMDLexicalBlockFile Temp = N->clone();
1652 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1655 typedef MetadataTest MDNamespaceTest;
1657 TEST_F(MDNamespaceTest, get) {
1658 MDScope *Scope = getFile();
1659 MDFile *File = getFile();
1660 StringRef Name = "namespace";
1663 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1665 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1666 EXPECT_EQ(Scope, N->getScope());
1667 EXPECT_EQ(File, N->getFile());
1668 EXPECT_EQ(Name, N->getName());
1669 EXPECT_EQ(Line, N->getLine());
1670 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1672 EXPECT_NE(N, MDNamespace::get(Context, getFile(), File, Name, Line));
1673 EXPECT_NE(N, MDNamespace::get(Context, Scope, getFile(), Name, Line));
1674 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1675 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
1677 TempMDNamespace Temp = N->clone();
1678 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1681 typedef MetadataTest MDTemplateTypeParameterTest;
1683 TEST_F(MDTemplateTypeParameterTest, get) {
1684 StringRef Name = "template";
1685 MDTypeRef Type = getBasicType("basic");
1687 auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
1689 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1690 EXPECT_EQ(Name, N->getName());
1691 EXPECT_EQ(Type, N->getType());
1692 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type));
1694 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
1696 MDTemplateTypeParameter::get(Context, Name, getBasicType("other")));
1698 TempMDTemplateTypeParameter Temp = N->clone();
1699 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1702 typedef MetadataTest MDTemplateValueParameterTest;
1704 TEST_F(MDTemplateValueParameterTest, get) {
1705 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1706 StringRef Name = "template";
1707 MDTypeRef Type = getBasicType("basic");
1708 Metadata *Value = getConstantAsMetadata();
1710 auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
1711 EXPECT_EQ(Tag, N->getTag());
1712 EXPECT_EQ(Name, N->getName());
1713 EXPECT_EQ(Type, N->getType());
1714 EXPECT_EQ(Value, N->getValue());
1715 EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value));
1717 EXPECT_NE(N, MDTemplateValueParameter::get(
1718 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1720 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type,
1722 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name,
1723 getBasicType("other"), Value));
1724 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type,
1725 getConstantAsMetadata()));
1727 TempMDTemplateValueParameter Temp = N->clone();
1728 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1731 typedef MetadataTest MDGlobalVariableTest;
1733 TEST_F(MDGlobalVariableTest, get) {
1734 MDScope *Scope = getSubprogram();
1735 StringRef Name = "name";
1736 StringRef LinkageName = "linkage";
1737 MDFile *File = getFile();
1739 MDTypeRef Type = getDerivedType();
1740 bool IsLocalToUnit = false;
1741 bool IsDefinition = true;
1742 Constant *Variable = getConstant();
1743 MDDerivedType *StaticDataMemberDeclaration =
1744 cast<MDDerivedType>(getDerivedType());
1746 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1747 Type, IsLocalToUnit, IsDefinition, Variable,
1748 StaticDataMemberDeclaration);
1749 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1750 EXPECT_EQ(Scope, N->getScope());
1751 EXPECT_EQ(Name, N->getName());
1752 EXPECT_EQ(LinkageName, N->getLinkageName());
1753 EXPECT_EQ(File, N->getFile());
1754 EXPECT_EQ(Line, N->getLine());
1755 EXPECT_EQ(Type, N->getType());
1756 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1757 EXPECT_EQ(IsDefinition, N->isDefinition());
1758 EXPECT_EQ(Variable, N->getVariable());
1759 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1760 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1761 Line, Type, IsLocalToUnit, IsDefinition,
1762 Variable, StaticDataMemberDeclaration));
1765 MDGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
1766 File, Line, Type, IsLocalToUnit, IsDefinition,
1767 Variable, StaticDataMemberDeclaration));
1768 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1769 Line, Type, IsLocalToUnit, IsDefinition,
1770 Variable, StaticDataMemberDeclaration));
1771 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1772 Type, IsLocalToUnit, IsDefinition,
1773 Variable, StaticDataMemberDeclaration));
1775 MDGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
1776 Line, Type, IsLocalToUnit, IsDefinition,
1777 Variable, StaticDataMemberDeclaration));
1779 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1780 Line + 1, Type, IsLocalToUnit, IsDefinition,
1781 Variable, StaticDataMemberDeclaration));
1783 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1784 getDerivedType(), IsLocalToUnit, IsDefinition,
1785 Variable, StaticDataMemberDeclaration));
1786 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1787 Line, Type, !IsLocalToUnit, IsDefinition,
1788 Variable, StaticDataMemberDeclaration));
1789 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1790 Line, Type, IsLocalToUnit, !IsDefinition,
1791 Variable, StaticDataMemberDeclaration));
1793 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1794 Type, IsLocalToUnit, IsDefinition,
1795 getConstant(), StaticDataMemberDeclaration));
1797 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1798 Type, IsLocalToUnit, IsDefinition, Variable,
1799 cast<MDDerivedType>(getDerivedType())));
1801 TempMDGlobalVariable Temp = N->clone();
1802 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1805 typedef MetadataTest MDLocalVariableTest;
1807 TEST_F(MDLocalVariableTest, get) {
1808 unsigned Tag = dwarf::DW_TAG_arg_variable;
1809 MDLocalScope *Scope = getSubprogram();
1810 StringRef Name = "name";
1811 MDFile *File = getFile();
1813 MDTypeRef Type = getDerivedType();
1816 MDLocation *InlinedAt =
1817 MDLocation::getDistinct(Context, 10, 20, getSubprogram());
1819 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1820 Arg, Flags, InlinedAt);
1821 EXPECT_EQ(Tag, N->getTag());
1822 EXPECT_EQ(Scope, N->getScope());
1823 EXPECT_EQ(Name, N->getName());
1824 EXPECT_EQ(File, N->getFile());
1825 EXPECT_EQ(Line, N->getLine());
1826 EXPECT_EQ(Type, N->getType());
1827 EXPECT_EQ(Arg, N->getArg());
1828 EXPECT_EQ(Flags, N->getFlags());
1829 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1830 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1831 Arg, Flags, InlinedAt));
1833 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1834 Name, File, Line, Type, Arg, Flags,
1836 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, getSubprogram(), Name, File,
1837 Line, Type, Arg, Flags, InlinedAt));
1838 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1839 Type, Arg, Flags, InlinedAt));
1840 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, getFile(), Line,
1841 Type, Arg, Flags, InlinedAt));
1842 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1843 Type, Arg, Flags, InlinedAt));
1844 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1845 getDerivedType(), Arg, Flags, InlinedAt));
1846 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1847 Arg + 1, Flags, InlinedAt));
1848 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1849 Arg, ~Flags, InlinedAt));
1850 EXPECT_NE(N, MDLocalVariable::get(
1851 Context, Tag, Scope, Name, File, Line, Type, Arg, Flags,
1852 MDLocation::getDistinct(Context, 10, 20, getSubprogram())));
1854 TempMDLocalVariable Temp = N->clone();
1855 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1857 auto *Inlined = N->withoutInline();
1858 EXPECT_NE(N, Inlined);
1859 EXPECT_EQ(N->getTag(), Inlined->getTag());
1860 EXPECT_EQ(N->getScope(), Inlined->getScope());
1861 EXPECT_EQ(N->getName(), Inlined->getName());
1862 EXPECT_EQ(N->getFile(), Inlined->getFile());
1863 EXPECT_EQ(N->getLine(), Inlined->getLine());
1864 EXPECT_EQ(N->getType(), Inlined->getType());
1865 EXPECT_EQ(N->getArg(), Inlined->getArg());
1866 EXPECT_EQ(N->getFlags(), Inlined->getFlags());
1867 EXPECT_EQ(nullptr, Inlined->getInlinedAt());
1868 EXPECT_EQ(N, Inlined->withInline(cast<MDLocation>(InlinedAt)));
1871 typedef MetadataTest MDExpressionTest;
1873 TEST_F(MDExpressionTest, get) {
1874 uint64_t Elements[] = {2, 6, 9, 78, 0};
1875 auto *N = MDExpression::get(Context, Elements);
1876 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1877 EXPECT_EQ(N, MDExpression::get(Context, Elements));
1879 EXPECT_EQ(5u, N->getNumElements());
1880 EXPECT_EQ(2u, N->getElement(0));
1881 EXPECT_EQ(6u, N->getElement(1));
1882 EXPECT_EQ(9u, N->getElement(2));
1883 EXPECT_EQ(78u, N->getElement(3));
1884 EXPECT_EQ(0u, N->getElement(4));
1886 TempMDExpression Temp = N->clone();
1887 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1890 TEST_F(MDExpressionTest, isValid) {
1891 #define EXPECT_VALID(...) \
1893 uint64_t Elements[] = {__VA_ARGS__}; \
1894 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1896 #define EXPECT_INVALID(...) \
1898 uint64_t Elements[] = {__VA_ARGS__}; \
1899 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1902 // Empty expression should be valid.
1903 EXPECT_TRUE(MDExpression::get(Context, None));
1905 // Valid constructions.
1906 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1907 EXPECT_VALID(dwarf::DW_OP_deref);
1908 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1909 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1910 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1911 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1912 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1914 // Invalid constructions.
1915 EXPECT_INVALID(~0u);
1916 EXPECT_INVALID(dwarf::DW_OP_plus);
1917 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1918 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1919 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1920 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1923 #undef EXPECT_INVALID
1926 typedef MetadataTest MDObjCPropertyTest;
1928 TEST_F(MDObjCPropertyTest, get) {
1929 StringRef Name = "name";
1930 MDFile *File = getFile();
1932 StringRef GetterName = "getter";
1933 StringRef SetterName = "setter";
1934 unsigned Attributes = 7;
1935 MDType *Type = cast<MDBasicType>(getBasicType("basic"));
1937 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1938 SetterName, Attributes, Type);
1940 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1941 EXPECT_EQ(Name, N->getName());
1942 EXPECT_EQ(File, N->getFile());
1943 EXPECT_EQ(Line, N->getLine());
1944 EXPECT_EQ(GetterName, N->getGetterName());
1945 EXPECT_EQ(SetterName, N->getSetterName());
1946 EXPECT_EQ(Attributes, N->getAttributes());
1947 EXPECT_EQ(Type, N->getType());
1948 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1949 SetterName, Attributes, Type));
1951 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1952 SetterName, Attributes, Type));
1953 EXPECT_NE(N, MDObjCProperty::get(Context, Name, getFile(), Line, GetterName,
1954 SetterName, Attributes, Type));
1955 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1956 SetterName, Attributes, Type));
1957 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1958 SetterName, Attributes, Type));
1959 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1960 "other", Attributes, Type));
1961 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1962 SetterName, Attributes + 1, Type));
1963 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1964 SetterName, Attributes,
1965 cast<MDBasicType>(getBasicType("other"))));
1967 TempMDObjCProperty Temp = N->clone();
1968 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1971 typedef MetadataTest MDImportedEntityTest;
1973 TEST_F(MDImportedEntityTest, get) {
1974 unsigned Tag = dwarf::DW_TAG_imported_module;
1975 MDScope *Scope = getSubprogram();
1976 DebugNodeRef Entity = getCompositeType();
1978 StringRef Name = "name";
1980 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1982 EXPECT_EQ(Tag, N->getTag());
1983 EXPECT_EQ(Scope, N->getScope());
1984 EXPECT_EQ(Entity, N->getEntity());
1985 EXPECT_EQ(Line, N->getLine());
1986 EXPECT_EQ(Name, N->getName());
1987 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1990 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1991 Scope, Entity, Line, Name));
1992 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, getSubprogram(), Entity,
1994 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, getCompositeType(),
1997 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1999 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
2001 TempMDImportedEntity Temp = N->clone();
2002 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2005 typedef MetadataTest MetadataAsValueTest;
2007 TEST_F(MetadataAsValueTest, MDNode) {
2008 MDNode *N = MDNode::get(Context, None);
2009 auto *V = MetadataAsValue::get(Context, N);
2010 EXPECT_TRUE(V->getType()->isMetadataTy());
2011 EXPECT_EQ(N, V->getMetadata());
2013 auto *V2 = MetadataAsValue::get(Context, N);
2017 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2018 MDNode *N = MDNode::get(Context, None);
2019 Metadata *Ops[] = {N};
2020 MDNode *N2 = MDNode::get(Context, Ops);
2021 auto *V = MetadataAsValue::get(Context, N2);
2022 EXPECT_TRUE(V->getType()->isMetadataTy());
2023 EXPECT_EQ(N2, V->getMetadata());
2025 auto *V2 = MetadataAsValue::get(Context, N2);
2028 auto *V3 = MetadataAsValue::get(Context, N);
2029 EXPECT_TRUE(V3->getType()->isMetadataTy());
2031 EXPECT_EQ(N, V3->getMetadata());
2034 TEST_F(MetadataAsValueTest, MDNodeConstant) {
2035 auto *C = ConstantInt::getTrue(Context);
2036 auto *MD = ConstantAsMetadata::get(C);
2037 Metadata *Ops[] = {MD};
2038 auto *N = MDNode::get(Context, Ops);
2040 auto *V = MetadataAsValue::get(Context, MD);
2041 EXPECT_TRUE(V->getType()->isMetadataTy());
2042 EXPECT_EQ(MD, V->getMetadata());
2044 auto *V2 = MetadataAsValue::get(Context, N);
2045 EXPECT_EQ(MD, V2->getMetadata());
2049 typedef MetadataTest ValueAsMetadataTest;
2051 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2052 Type *Ty = Type::getInt1PtrTy(Context);
2053 std::unique_ptr<GlobalVariable> GV0(
2054 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2055 auto *MD = ValueAsMetadata::get(GV0.get());
2056 EXPECT_TRUE(MD->getValue() == GV0.get());
2057 ASSERT_TRUE(GV0->use_empty());
2059 std::unique_ptr<GlobalVariable> GV1(
2060 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2061 GV0->replaceAllUsesWith(GV1.get());
2062 EXPECT_TRUE(MD->getValue() == GV1.get());
2065 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2066 // Create a constant.
2067 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2068 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2070 // Create a temporary to prevent nodes from resolving.
2071 auto Temp = MDTuple::getTemporary(Context, None);
2073 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
2074 Metadata *Ops1[] = {CI, CI, Temp.get()};
2075 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
2077 auto *N1 = MDTuple::get(Context, Ops1);
2078 auto *N2 = MDTuple::get(Context, Ops2);
2081 // Tell metadata that the constant is getting deleted.
2083 // After this, N1 will be invalid, so don't touch it.
2084 ValueAsMetadata::handleDeletion(CI->getValue());
2085 EXPECT_EQ(nullptr, N2->getOperand(0));
2086 EXPECT_EQ(nullptr, N2->getOperand(1));
2087 EXPECT_EQ(Temp.get(), N2->getOperand(2));
2089 // Clean up Temp for teardown.
2090 Temp->replaceAllUsesWith(nullptr);
2093 typedef MetadataTest TrackingMDRefTest;
2095 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2096 Type *Ty = Type::getInt1PtrTy(Context);
2097 std::unique_ptr<GlobalVariable> GV0(
2098 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2099 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2100 EXPECT_TRUE(MD->getValue() == GV0.get());
2101 ASSERT_TRUE(GV0->use_empty());
2103 std::unique_ptr<GlobalVariable> GV1(
2104 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2105 GV0->replaceAllUsesWith(GV1.get());
2106 EXPECT_TRUE(MD->getValue() == GV1.get());
2108 // Reset it, so we don't inadvertently test deletion.
2112 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2113 Type *Ty = Type::getInt1PtrTy(Context);
2114 std::unique_ptr<GlobalVariable> GV(
2115 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2116 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2117 EXPECT_TRUE(MD->getValue() == GV.get());
2118 ASSERT_TRUE(GV->use_empty());
2124 TEST(NamedMDNodeTest, Search) {
2125 LLVMContext Context;
2126 ConstantAsMetadata *C =
2127 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2128 ConstantAsMetadata *C2 =
2129 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
2131 Metadata *const V = C;
2132 Metadata *const V2 = C2;
2133 MDNode *n = MDNode::get(Context, V);
2134 MDNode *n2 = MDNode::get(Context, V2);
2136 Module M("MyModule", Context);
2137 const char *Name = "llvm.NMD1";
2138 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2140 NMD->addOperand(n2);
2143 raw_string_ostream oss(Str);
2145 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",