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/IR/Verifier.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "gtest/gtest.h"
27 TEST(ContextAndReplaceableUsesTest, FromContext) {
29 ContextAndReplaceableUses CRU(Context);
30 EXPECT_EQ(&Context, &CRU.getContext());
31 EXPECT_FALSE(CRU.hasReplaceableUses());
32 EXPECT_FALSE(CRU.getReplaceableUses());
35 TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
37 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
38 EXPECT_EQ(&Context, &CRU.getContext());
39 EXPECT_TRUE(CRU.hasReplaceableUses());
40 EXPECT_TRUE(CRU.getReplaceableUses());
43 TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
45 ContextAndReplaceableUses CRU(Context);
46 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
47 EXPECT_EQ(&Context, &CRU.getContext());
48 EXPECT_TRUE(CRU.hasReplaceableUses());
49 EXPECT_TRUE(CRU.getReplaceableUses());
52 TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
54 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
55 auto *Ptr = ReplaceableUses.get();
56 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
57 ReplaceableUses = CRU.takeReplaceableUses();
58 EXPECT_EQ(&Context, &CRU.getContext());
59 EXPECT_FALSE(CRU.hasReplaceableUses());
60 EXPECT_FALSE(CRU.getReplaceableUses());
61 EXPECT_EQ(Ptr, ReplaceableUses.get());
64 class MetadataTest : public testing::Test {
66 MetadataTest() : M("test", Context), Counter(0) {}
73 MDNode *getNode() { return MDNode::get(Context, None); }
74 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
75 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
76 Metadata *MDs[] = {MD1, MD2};
77 return MDNode::get(Context, MDs);
80 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
81 DISubroutineType *getSubroutineType() {
82 return DISubroutineType::getDistinct(Context, 0, getNode(nullptr));
84 DISubprogram *getSubprogram() {
85 return DISubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
86 nullptr, false, false, 0, nullptr, 0, 0, 0,
89 DIScopeRef getSubprogramRef() { return getSubprogram()->getRef(); }
91 return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
93 DITypeRef getBasicType(StringRef Name) {
94 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name)
97 DITypeRef getDerivedType() {
98 return DIDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
100 getBasicType("basictype"), 1, 2, 0, 0)
103 Constant *getConstant() {
104 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
106 ConstantAsMetadata *getConstantAsMetadata() {
107 return ConstantAsMetadata::get(getConstant());
109 DITypeRef getCompositeType() {
110 return DICompositeType::getDistinct(
111 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr,
112 nullptr, 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "")
115 Function *getFunction(StringRef Name) {
116 return cast<Function>(M.getOrInsertFunction(
117 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
120 typedef MetadataTest MDStringTest;
122 // Test that construction of MDString with different value produces different
123 // MDString objects, even with the same string pointer and nulls in the string.
124 TEST_F(MDStringTest, CreateDifferent) {
125 char x[3] = { 'f', 0, 'A' };
126 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
128 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
132 // Test that creation of MDStrings with the same string contents produces the
133 // same MDString object, even with different pointers.
134 TEST_F(MDStringTest, CreateSame) {
135 char x[4] = { 'a', 'b', 'c', 'X' };
136 char y[4] = { 'a', 'b', 'c', 'Y' };
138 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
139 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
143 // Test that MDString prints out the string we fed it.
144 TEST_F(MDStringTest, PrintingSimple) {
145 char *str = new char[13];
146 strncpy(str, "testing 1 2 3", 13);
147 MDString *s = MDString::get(Context, StringRef(str, 13));
148 strncpy(str, "aaaaaaaaaaaaa", 13);
152 raw_string_ostream oss(Str);
154 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
157 // Test printing of MDString with non-printable characters.
158 TEST_F(MDStringTest, PrintingComplex) {
159 char str[5] = {0, '\n', '"', '\\', (char)-1};
160 MDString *s = MDString::get(Context, StringRef(str+0, 5));
162 raw_string_ostream oss(Str);
164 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
167 typedef MetadataTest MDNodeTest;
169 // Test the two constructors, and containing other Constants.
170 TEST_F(MDNodeTest, Simple) {
171 char x[3] = { 'a', 'b', 'c' };
172 char y[3] = { '1', '2', '3' };
174 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
175 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
176 ConstantAsMetadata *CI = ConstantAsMetadata::get(
177 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
179 std::vector<Metadata *> V;
184 MDNode *n1 = MDNode::get(Context, V);
185 Metadata *const c1 = n1;
186 MDNode *n2 = MDNode::get(Context, c1);
187 Metadata *const c2 = n2;
188 MDNode *n3 = MDNode::get(Context, V);
189 MDNode *n4 = MDNode::getIfExists(Context, V);
190 MDNode *n5 = MDNode::getIfExists(Context, c1);
191 MDNode *n6 = MDNode::getIfExists(Context, c2);
196 EXPECT_EQ(n6, (Metadata *)nullptr);
198 EXPECT_EQ(3u, n1->getNumOperands());
199 EXPECT_EQ(s1, n1->getOperand(0));
200 EXPECT_EQ(CI, n1->getOperand(1));
201 EXPECT_EQ(s2, n1->getOperand(2));
203 EXPECT_EQ(1u, n2->getNumOperands());
204 EXPECT_EQ(n1, n2->getOperand(0));
207 TEST_F(MDNodeTest, Delete) {
208 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
209 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
211 Metadata *const V = LocalAsMetadata::get(I);
212 MDNode *n = MDNode::get(Context, V);
213 TrackingMDRef wvh(n);
220 TEST_F(MDNodeTest, SelfReference) {
224 auto Temp = MDNode::getTemporary(Context, None);
225 Metadata *Args[] = {Temp.get()};
226 MDNode *Self = MDNode::get(Context, Args);
227 Self->replaceOperandWith(0, Self);
228 ASSERT_EQ(Self, Self->getOperand(0));
230 // Self-references should be distinct, so MDNode::get() should grab a
231 // uniqued node that references Self, not Self.
233 MDNode *Ref1 = MDNode::get(Context, Args);
234 MDNode *Ref2 = MDNode::get(Context, Args);
235 EXPECT_NE(Self, Ref1);
236 EXPECT_EQ(Ref1, Ref2);
242 auto Temp = MDNode::getTemporary(Context, None);
243 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
244 MDNode *Self = MDNode::get(Context, Args);
245 Self->replaceOperandWith(0, Self);
246 ASSERT_EQ(Self, Self->getOperand(0));
248 // Self-references should be distinct, so MDNode::get() should grab a
249 // uniqued node that references Self, not Self itself.
251 MDNode *Ref1 = MDNode::get(Context, Args);
252 MDNode *Ref2 = MDNode::get(Context, Args);
253 EXPECT_NE(Self, Ref1);
254 EXPECT_EQ(Ref1, Ref2);
258 TEST_F(MDNodeTest, Print) {
259 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
260 MDString *S = MDString::get(Context, "foo");
261 MDNode *N0 = getNode();
262 MDNode *N1 = getNode(N0);
263 MDNode *N2 = getNode(N0, N1);
265 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
266 MDNode *N = MDNode::get(Context, Args);
268 std::string Expected;
270 raw_string_ostream OS(Expected);
271 OS << "<" << (void *)N << "> = !{";
272 C->printAsOperand(OS);
274 S->printAsOperand(OS);
276 MDNode *Nodes[] = {N0, N1, N2};
277 for (auto *Node : Nodes)
278 OS << ", <" << (void *)Node << ">";
284 raw_string_ostream OS(Actual);
288 EXPECT_EQ(Expected, Actual);
291 #define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
293 std::string Actual_; \
294 raw_string_ostream OS(Actual_); \
297 std::string Expected_(EXPECTED); \
298 EXPECT_EQ(Expected_, Actual_); \
301 TEST_F(MDNodeTest, PrintTemporary) {
302 MDNode *Arg = getNode();
303 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
304 MDNode *N = getNode(Temp.get());
305 Module M("test", Context);
306 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
309 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
310 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
311 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
314 Temp->replaceAllUsesWith(Arg);
317 TEST_F(MDNodeTest, PrintFromModule) {
318 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
319 MDString *S = MDString::get(Context, "foo");
320 MDNode *N0 = getNode();
321 MDNode *N1 = getNode(N0);
322 MDNode *N2 = getNode(N0, N1);
324 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
325 MDNode *N = MDNode::get(Context, Args);
326 Module M("test", Context);
327 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
330 std::string Expected;
332 raw_string_ostream OS(Expected);
334 C->printAsOperand(OS);
336 S->printAsOperand(OS);
337 OS << ", null, !1, !2, !3}";
340 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
343 TEST_F(MDNodeTest, PrintFromFunction) {
344 Module M("test", Context);
345 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
346 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
347 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
348 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
349 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
350 auto *R0 = ReturnInst::Create(Context, BB0);
351 auto *R1 = ReturnInst::Create(Context, BB1);
352 auto *N0 = MDNode::getDistinct(Context, None);
353 auto *N1 = MDNode::getDistinct(Context, None);
354 R0->setMetadata("md", N0);
355 R1->setMetadata("md", N1);
357 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
358 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
361 TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
362 Module M("test", Context);
365 Function::Create(FunctionType::get(Type::getVoidTy(Context),
366 Type::getMetadataTy(Context), false),
367 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
369 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
370 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
371 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
372 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
373 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
374 auto *N0 = MDNode::getDistinct(Context, None);
375 auto *N1 = MDNode::getDistinct(Context, None);
376 auto *MAV0 = MetadataAsValue::get(Context, N0);
377 auto *MAV1 = MetadataAsValue::get(Context, N1);
378 CallInst::Create(Intrinsic, MAV0, "", BB0);
379 CallInst::Create(Intrinsic, MAV1, "", BB1);
381 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
382 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
383 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
384 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
385 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
386 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
388 #undef EXPECT_PRINTER_EQ
390 TEST_F(MDNodeTest, NullOperand) {
392 MDNode *Empty = MDNode::get(Context, None);
394 // metadata !{metadata !{}}
395 Metadata *Ops[] = {Empty};
396 MDNode *N = MDNode::get(Context, Ops);
397 ASSERT_EQ(Empty, N->getOperand(0));
399 // metadata !{metadata !{}} => metadata !{null}
400 N->replaceOperandWith(0, nullptr);
401 ASSERT_EQ(nullptr, N->getOperand(0));
405 MDNode *NullOp = MDNode::get(Context, Ops);
406 ASSERT_EQ(nullptr, NullOp->getOperand(0));
407 EXPECT_EQ(N, NullOp);
410 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
412 MDNode *Empty = MDNode::get(Context, None);
413 ASSERT_TRUE(Empty->isResolved());
414 EXPECT_FALSE(Empty->isDistinct());
417 Metadata *Wrapped1Ops[] = {Empty};
418 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
419 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
420 ASSERT_TRUE(Wrapped1->isResolved());
421 EXPECT_FALSE(Wrapped1->isDistinct());
424 Metadata *Wrapped2Ops[] = {Wrapped1};
425 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
426 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
427 ASSERT_TRUE(Wrapped2->isResolved());
428 EXPECT_FALSE(Wrapped2->isDistinct());
430 // !{!{!{}}} => !{!{}}
431 Wrapped2->replaceOperandWith(0, Empty);
432 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
433 EXPECT_TRUE(Wrapped2->isDistinct());
434 EXPECT_FALSE(Wrapped1->isDistinct());
437 TEST_F(MDNodeTest, getDistinct) {
439 MDNode *Empty = MDNode::get(Context, None);
440 ASSERT_TRUE(Empty->isResolved());
441 ASSERT_FALSE(Empty->isDistinct());
442 ASSERT_EQ(Empty, MDNode::get(Context, None));
445 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
446 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
447 EXPECT_TRUE(Distinct1->isResolved());
448 EXPECT_TRUE(Distinct2->isDistinct());
449 EXPECT_NE(Empty, Distinct1);
450 EXPECT_NE(Empty, Distinct2);
451 EXPECT_NE(Distinct1, Distinct2);
454 ASSERT_EQ(Empty, MDNode::get(Context, None));
457 TEST_F(MDNodeTest, isUniqued) {
458 MDNode *U = MDTuple::get(Context, None);
459 MDNode *D = MDTuple::getDistinct(Context, None);
460 auto T = MDTuple::getTemporary(Context, None);
461 EXPECT_TRUE(U->isUniqued());
462 EXPECT_FALSE(D->isUniqued());
463 EXPECT_FALSE(T->isUniqued());
466 TEST_F(MDNodeTest, isDistinct) {
467 MDNode *U = MDTuple::get(Context, None);
468 MDNode *D = MDTuple::getDistinct(Context, None);
469 auto T = MDTuple::getTemporary(Context, None);
470 EXPECT_FALSE(U->isDistinct());
471 EXPECT_TRUE(D->isDistinct());
472 EXPECT_FALSE(T->isDistinct());
475 TEST_F(MDNodeTest, isTemporary) {
476 MDNode *U = MDTuple::get(Context, None);
477 MDNode *D = MDTuple::getDistinct(Context, None);
478 auto T = MDTuple::getTemporary(Context, None);
479 EXPECT_FALSE(U->isTemporary());
480 EXPECT_FALSE(D->isTemporary());
481 EXPECT_TRUE(T->isTemporary());
484 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
486 auto Temp = MDTuple::getTemporary(Context, None);
487 ASSERT_FALSE(Temp->isResolved());
489 // distinct !{temporary !{}}
490 Metadata *Ops[] = {Temp.get()};
491 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
492 EXPECT_TRUE(Distinct->isResolved());
493 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
495 // temporary !{} => !{}
496 MDNode *Empty = MDNode::get(Context, None);
497 Temp->replaceAllUsesWith(Empty);
498 EXPECT_EQ(Empty, Distinct->getOperand(0));
501 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
503 MDNode *N0 = MDNode::get(Context, None);
506 auto Temp3 = MDTuple::getTemporary(Context, None);
507 Metadata *Ops1[] = {Temp3.get(), nullptr};
508 MDNode *N1 = MDNode::get(Context, Ops1);
511 Metadata *Ops2[] = {Temp3.get(), N0};
512 MDNode *N2 = MDNode::get(Context, Ops2);
515 Metadata *Ops3[] = {N2};
516 MDNode *N3 = MDNode::get(Context, Ops3);
517 Temp3->replaceAllUsesWith(N3);
520 Metadata *Ops4[] = {N1};
521 MDNode *N4 = MDNode::get(Context, Ops4);
523 // Confirm that the cycle prevented RAUW from getting dropped.
524 EXPECT_TRUE(N0->isResolved());
525 EXPECT_FALSE(N1->isResolved());
526 EXPECT_FALSE(N2->isResolved());
527 EXPECT_FALSE(N3->isResolved());
528 EXPECT_FALSE(N4->isResolved());
530 // Create a couple of distinct nodes to observe what's going on.
532 // !5 = distinct !{!2}
533 // !6 = distinct !{!3}
534 Metadata *Ops5[] = {N2};
535 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
536 Metadata *Ops6[] = {N3};
537 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
539 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
540 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
541 // references !3, this can cause a re-entry of handleChangedOperand() when !3
542 // is not ready for it.
544 // !2->replaceOperandWith(1, nullptr)
545 // !2: !{!3, !0} => !{!3, null}
546 // !2->replaceAllUsesWith(!1)
547 // !3: !{!2] => !{!1}
548 // !3->replaceAllUsesWith(!4)
549 N2->replaceOperandWith(1, nullptr);
551 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
552 // under us. Just check that the other nodes are sane.
556 // !5 = distinct !{!1}
557 // !6 = distinct !{!4}
558 EXPECT_EQ(N4, N1->getOperand(0));
559 EXPECT_EQ(N1, N4->getOperand(0));
560 EXPECT_EQ(N1, N5->getOperand(0));
561 EXPECT_EQ(N4, N6->getOperand(0));
564 TEST_F(MDNodeTest, replaceResolvedOperand) {
565 // Check code for replacing one resolved operand with another. If doing this
566 // directly (via replaceOperandWith()) becomes illegal, change the operand to
567 // a global value that gets RAUW'ed.
569 // Use a temporary node to keep N from being resolved.
570 auto Temp = MDTuple::getTemporary(Context, None);
571 Metadata *Ops[] = {nullptr, Temp.get()};
573 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
574 MDNode *N = MDTuple::get(Context, Ops);
575 EXPECT_EQ(nullptr, N->getOperand(0));
576 ASSERT_FALSE(N->isResolved());
578 // Check code for replacing resolved nodes.
579 N->replaceOperandWith(0, Empty);
580 EXPECT_EQ(Empty, N->getOperand(0));
582 // Check code for adding another unresolved operand.
583 N->replaceOperandWith(0, Temp.get());
584 EXPECT_EQ(Temp.get(), N->getOperand(0));
586 // Remove the references to Temp; required for teardown.
587 Temp->replaceAllUsesWith(nullptr);
590 TEST_F(MDNodeTest, replaceWithUniqued) {
591 auto *Empty = MDTuple::get(Context, None);
592 MDTuple *FirstUniqued;
594 Metadata *Ops[] = {Empty};
595 auto Temp = MDTuple::getTemporary(Context, Ops);
596 EXPECT_TRUE(Temp->isTemporary());
598 // Don't expect a collision.
599 auto *Current = Temp.get();
600 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
601 EXPECT_TRUE(FirstUniqued->isUniqued());
602 EXPECT_TRUE(FirstUniqued->isResolved());
603 EXPECT_EQ(Current, FirstUniqued);
606 Metadata *Ops[] = {Empty};
607 auto Temp = MDTuple::getTemporary(Context, Ops);
608 EXPECT_TRUE(Temp->isTemporary());
610 // Should collide with Uniqued above this time.
611 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
612 EXPECT_TRUE(Uniqued->isUniqued());
613 EXPECT_TRUE(Uniqued->isResolved());
614 EXPECT_EQ(FirstUniqued, Uniqued);
617 auto Unresolved = MDTuple::getTemporary(Context, None);
618 Metadata *Ops[] = {Unresolved.get()};
619 auto Temp = MDTuple::getTemporary(Context, Ops);
620 EXPECT_TRUE(Temp->isTemporary());
622 // Shouldn't be resolved.
623 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
624 EXPECT_TRUE(Uniqued->isUniqued());
625 EXPECT_FALSE(Uniqued->isResolved());
627 // Should be a different node.
628 EXPECT_NE(FirstUniqued, Uniqued);
630 // Should resolve when we update its node (note: be careful to avoid a
631 // collision with any other nodes above).
632 Uniqued->replaceOperandWith(0, nullptr);
633 EXPECT_TRUE(Uniqued->isResolved());
637 TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
639 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
640 EXPECT_FALSE(Op->isResolved());
643 Metadata *Ops[] = {Op};
644 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
645 EXPECT_FALSE(N->isResolved());
647 // temp !{temp !{}} => !{temp !{}}
648 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
649 EXPECT_FALSE(N->isResolved());
651 // !{temp !{}} => !{!{}}
652 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
653 EXPECT_TRUE(Op->isResolved());
654 EXPECT_TRUE(N->isResolved());
657 TEST_F(MDNodeTest, replaceWithUniquedChangingOperand) {
659 Type *Ty = Type::getInt1PtrTy(Context);
660 std::unique_ptr<GlobalVariable> GV(
661 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
662 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
665 Metadata *Ops[] = {Op};
666 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
668 // temp !{i1* @GV} => !{i1* @GV}
669 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
670 ASSERT_TRUE(N->isUniqued());
672 // !{i1* @GV} => !{null}
674 ASSERT_TRUE(N->isUniqued());
675 Metadata *NullOps[] = {nullptr};
676 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
679 TEST_F(MDNodeTest, replaceWithDistinct) {
681 auto *Empty = MDTuple::get(Context, None);
682 Metadata *Ops[] = {Empty};
683 auto Temp = MDTuple::getTemporary(Context, Ops);
684 EXPECT_TRUE(Temp->isTemporary());
686 // Don't expect a collision.
687 auto *Current = Temp.get();
688 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
689 EXPECT_TRUE(Distinct->isDistinct());
690 EXPECT_TRUE(Distinct->isResolved());
691 EXPECT_EQ(Current, Distinct);
694 auto Unresolved = MDTuple::getTemporary(Context, None);
695 Metadata *Ops[] = {Unresolved.get()};
696 auto Temp = MDTuple::getTemporary(Context, Ops);
697 EXPECT_TRUE(Temp->isTemporary());
699 // Don't expect a collision.
700 auto *Current = Temp.get();
701 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
702 EXPECT_TRUE(Distinct->isDistinct());
703 EXPECT_TRUE(Distinct->isResolved());
704 EXPECT_EQ(Current, Distinct);
706 // Cleanup; required for teardown.
707 Unresolved->replaceAllUsesWith(nullptr);
711 TEST_F(MDNodeTest, replaceWithPermanent) {
712 Metadata *Ops[] = {nullptr};
713 auto Temp = MDTuple::getTemporary(Context, Ops);
714 auto *T = Temp.get();
716 // U is a normal, uniqued node that references T.
717 auto *U = MDTuple::get(Context, T);
718 EXPECT_TRUE(U->isUniqued());
720 // Make Temp self-referencing.
721 Temp->replaceOperandWith(0, T);
723 // Try to uniquify Temp. This should, despite the name in the API, give a
724 // 'distinct' node, since self-references aren't allowed to be uniqued.
726 // Since it's distinct, N should have the same address as when it was a
727 // temporary (i.e., be equal to T not U).
728 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
730 EXPECT_TRUE(N->isDistinct());
732 // U should be the canonical unique node with N as the argument.
733 EXPECT_EQ(U, MDTuple::get(Context, N));
734 EXPECT_TRUE(U->isUniqued());
736 // This temporary should collide with U when replaced, but it should still be
738 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
739 EXPECT_TRUE(U->isUniqued());
741 // This temporary should become a new uniqued node.
742 auto Temp2 = MDTuple::getTemporary(Context, U);
743 auto *V = Temp2.get();
744 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
745 EXPECT_TRUE(V->isUniqued());
746 EXPECT_EQ(U, V->getOperand(0));
749 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
751 EXPECT_EQ(nullptr, Ref.get());
753 auto Temp = MDTuple::getTemporary(Context, None);
754 Ref.reset(Temp.get());
755 EXPECT_EQ(Temp.get(), Ref.get());
757 EXPECT_EQ(nullptr, Ref.get());
760 typedef MetadataTest DILocationTest;
762 TEST_F(DILocationTest, Overflow) {
763 DISubprogram *N = getSubprogram();
765 DILocation *L = DILocation::get(Context, 2, 7, N);
766 EXPECT_EQ(2u, L->getLine());
767 EXPECT_EQ(7u, L->getColumn());
769 unsigned U16 = 1u << 16;
771 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
772 EXPECT_EQ(UINT32_MAX, L->getLine());
773 EXPECT_EQ(U16 - 1, L->getColumn());
776 DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
777 EXPECT_EQ(UINT32_MAX, L->getLine());
778 EXPECT_EQ(0u, L->getColumn());
781 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
782 EXPECT_EQ(UINT32_MAX, L->getLine());
783 EXPECT_EQ(0u, L->getColumn());
787 TEST_F(DILocationTest, getDistinct) {
788 MDNode *N = getSubprogram();
789 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
790 EXPECT_TRUE(L0->isDistinct());
791 DILocation *L1 = DILocation::get(Context, 2, 7, N);
792 EXPECT_FALSE(L1->isDistinct());
793 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
796 TEST_F(DILocationTest, getTemporary) {
797 MDNode *N = MDNode::get(Context, None);
798 auto L = DILocation::getTemporary(Context, 2, 7, N);
799 EXPECT_TRUE(L->isTemporary());
800 EXPECT_FALSE(L->isResolved());
803 typedef MetadataTest GenericDINodeTest;
805 TEST_F(GenericDINodeTest, get) {
806 StringRef Header = "header";
807 auto *Empty = MDNode::get(Context, None);
808 Metadata *Ops1[] = {Empty};
809 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
810 EXPECT_EQ(15u, N->getTag());
811 EXPECT_EQ(2u, N->getNumOperands());
812 EXPECT_EQ(Header, N->getHeader());
813 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
814 EXPECT_EQ(1u, N->getNumDwarfOperands());
815 EXPECT_EQ(Empty, N->getDwarfOperand(0));
816 EXPECT_EQ(Empty, N->getOperand(1));
817 ASSERT_TRUE(N->isUniqued());
819 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
821 N->replaceOperandWith(1, nullptr);
822 EXPECT_EQ(15u, N->getTag());
823 EXPECT_EQ(Header, N->getHeader());
824 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
825 ASSERT_TRUE(N->isUniqued());
827 Metadata *Ops2[] = {nullptr};
828 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
830 N->replaceDwarfOperandWith(0, Empty);
831 EXPECT_EQ(15u, N->getTag());
832 EXPECT_EQ(Header, N->getHeader());
833 EXPECT_EQ(Empty, N->getDwarfOperand(0));
834 ASSERT_TRUE(N->isUniqued());
835 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
837 TempGenericDINode Temp = N->clone();
838 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
841 TEST_F(GenericDINodeTest, getEmptyHeader) {
842 // Canonicalize !"" to null.
843 auto *N = GenericDINode::get(Context, 15, StringRef(), None);
844 EXPECT_EQ(StringRef(), N->getHeader());
845 EXPECT_EQ(nullptr, N->getOperand(0));
848 typedef MetadataTest DISubrangeTest;
850 TEST_F(DISubrangeTest, get) {
851 auto *N = DISubrange::get(Context, 5, 7);
852 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
853 EXPECT_EQ(5, N->getCount());
854 EXPECT_EQ(7, N->getLowerBound());
855 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
856 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
858 TempDISubrange Temp = N->clone();
859 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
862 TEST_F(DISubrangeTest, getEmptyArray) {
863 auto *N = DISubrange::get(Context, -1, 0);
864 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
865 EXPECT_EQ(-1, N->getCount());
866 EXPECT_EQ(0, N->getLowerBound());
867 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
870 typedef MetadataTest DIEnumeratorTest;
872 TEST_F(DIEnumeratorTest, get) {
873 auto *N = DIEnumerator::get(Context, 7, "name");
874 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
875 EXPECT_EQ(7, N->getValue());
876 EXPECT_EQ("name", N->getName());
877 EXPECT_EQ(N, DIEnumerator::get(Context, 7, "name"));
879 EXPECT_NE(N, DIEnumerator::get(Context, 8, "name"));
880 EXPECT_NE(N, DIEnumerator::get(Context, 7, "nam"));
882 TempDIEnumerator Temp = N->clone();
883 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
886 typedef MetadataTest DIBasicTypeTest;
888 TEST_F(DIBasicTypeTest, get) {
890 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
891 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
892 EXPECT_EQ("special", N->getName());
893 EXPECT_EQ(33u, N->getSizeInBits());
894 EXPECT_EQ(26u, N->getAlignInBits());
895 EXPECT_EQ(7u, N->getEncoding());
896 EXPECT_EQ(0u, N->getLine());
897 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
900 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
901 "special", 33, 26, 7));
903 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
904 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
906 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
908 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
911 TempDIBasicType Temp = N->clone();
912 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
915 TEST_F(DIBasicTypeTest, getWithLargeValues) {
916 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
917 UINT64_MAX, UINT64_MAX - 1, 7);
918 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
919 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
922 TEST_F(DIBasicTypeTest, getUnspecified) {
924 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
925 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
926 EXPECT_EQ("unspecified", N->getName());
927 EXPECT_EQ(0u, N->getSizeInBits());
928 EXPECT_EQ(0u, N->getAlignInBits());
929 EXPECT_EQ(0u, N->getEncoding());
930 EXPECT_EQ(0u, N->getLine());
933 typedef MetadataTest DITypeTest;
935 TEST_F(DITypeTest, clone) {
936 // Check that DIType has a specialized clone that returns TempDIType.
937 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
938 dwarf::DW_ATE_signed);
940 TempDIType Temp = N->clone();
941 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
944 TEST_F(DITypeTest, setFlags) {
946 Metadata *TypesOps[] = {nullptr};
947 Metadata *Types = MDTuple::get(Context, TypesOps);
949 DIType *D = DISubroutineType::getDistinct(Context, 0u, Types);
950 EXPECT_EQ(0u, D->getFlags());
951 D->setFlags(DINode::FlagRValueReference);
952 EXPECT_EQ(DINode::FlagRValueReference, D->getFlags());
954 EXPECT_EQ(0u, D->getFlags());
956 TempDIType T = DISubroutineType::getTemporary(Context, 0u, Types);
957 EXPECT_EQ(0u, T->getFlags());
958 T->setFlags(DINode::FlagRValueReference);
959 EXPECT_EQ(DINode::FlagRValueReference, T->getFlags());
961 EXPECT_EQ(0u, T->getFlags());
964 typedef MetadataTest DIDerivedTypeTest;
966 TEST_F(DIDerivedTypeTest, get) {
967 DIFile *File = getFile();
968 DIScopeRef Scope = getSubprogramRef();
969 DITypeRef BaseType = getBasicType("basic");
970 MDTuple *ExtraData = getTuple();
972 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
973 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
974 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
975 EXPECT_EQ("something", N->getName());
976 EXPECT_EQ(File, N->getFile());
977 EXPECT_EQ(1u, N->getLine());
978 EXPECT_EQ(Scope, N->getScope());
979 EXPECT_EQ(BaseType, N->getBaseType());
980 EXPECT_EQ(2u, N->getSizeInBits());
981 EXPECT_EQ(3u, N->getAlignInBits());
982 EXPECT_EQ(4u, N->getOffsetInBits());
983 EXPECT_EQ(5u, N->getFlags());
984 EXPECT_EQ(ExtraData, N->getExtraData());
985 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
986 "something", File, 1, Scope, BaseType, 2, 3,
989 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
990 "something", File, 1, Scope, BaseType, 2, 3,
992 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
993 File, 1, Scope, BaseType, 2, 3, 4, 5,
995 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
996 "something", getFile(), 1, Scope, BaseType, 2,
997 3, 4, 5, ExtraData));
998 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
999 "something", File, 2, Scope, BaseType, 2, 3,
1001 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1002 "something", File, 1, getSubprogramRef(),
1003 BaseType, 2, 3, 4, 5, ExtraData));
1004 EXPECT_NE(N, DIDerivedType::get(
1005 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1006 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
1007 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1008 "something", File, 1, Scope, BaseType, 3, 3,
1010 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1011 "something", File, 1, Scope, BaseType, 2, 2,
1013 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1014 "something", File, 1, Scope, BaseType, 2, 3,
1016 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1017 "something", File, 1, Scope, BaseType, 2, 3,
1019 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1020 "something", File, 1, Scope, BaseType, 2, 3,
1023 TempDIDerivedType Temp = N->clone();
1024 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1027 TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1028 DIFile *File = getFile();
1029 DIScopeRef Scope = getSubprogramRef();
1030 DITypeRef BaseType = getBasicType("basic");
1031 MDTuple *ExtraData = getTuple();
1033 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
1034 File, 1, Scope, BaseType, UINT64_MAX,
1035 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1036 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1037 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1038 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1041 typedef MetadataTest DICompositeTypeTest;
1043 TEST_F(DICompositeTypeTest, get) {
1044 unsigned Tag = dwarf::DW_TAG_structure_type;
1045 StringRef Name = "some name";
1046 DIFile *File = getFile();
1048 DIScopeRef Scope = getSubprogramRef();
1049 DITypeRef BaseType = getCompositeType();
1050 uint64_t SizeInBits = 2;
1051 uint64_t AlignInBits = 3;
1052 uint64_t OffsetInBits = 4;
1054 MDTuple *Elements = getTuple();
1055 unsigned RuntimeLang = 6;
1056 DITypeRef VTableHolder = getCompositeType();
1057 MDTuple *TemplateParams = getTuple();
1058 StringRef Identifier = "some id";
1060 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1061 BaseType, SizeInBits, AlignInBits,
1062 OffsetInBits, Flags, Elements, RuntimeLang,
1063 VTableHolder, TemplateParams, Identifier);
1064 EXPECT_EQ(Tag, N->getTag());
1065 EXPECT_EQ(Name, N->getName());
1066 EXPECT_EQ(File, N->getFile());
1067 EXPECT_EQ(Line, N->getLine());
1068 EXPECT_EQ(Scope, N->getScope());
1069 EXPECT_EQ(BaseType, N->getBaseType());
1070 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1071 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1072 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1073 EXPECT_EQ(Flags, N->getFlags());
1074 EXPECT_EQ(Elements, N->getElements().get());
1075 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1076 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1077 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1078 EXPECT_EQ(Identifier, N->getIdentifier());
1080 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1081 BaseType, SizeInBits, AlignInBits,
1082 OffsetInBits, Flags, Elements, RuntimeLang,
1083 VTableHolder, TemplateParams, Identifier));
1085 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
1086 BaseType, SizeInBits, AlignInBits,
1087 OffsetInBits, Flags, Elements, RuntimeLang,
1088 VTableHolder, TemplateParams, Identifier));
1089 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
1090 BaseType, SizeInBits, AlignInBits,
1091 OffsetInBits, Flags, Elements, RuntimeLang,
1092 VTableHolder, TemplateParams, Identifier));
1093 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
1094 BaseType, SizeInBits, AlignInBits,
1095 OffsetInBits, Flags, Elements, RuntimeLang,
1096 VTableHolder, TemplateParams, Identifier));
1097 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
1098 BaseType, SizeInBits, AlignInBits,
1099 OffsetInBits, Flags, Elements, RuntimeLang,
1100 VTableHolder, TemplateParams, Identifier));
1101 EXPECT_NE(N, DICompositeType::get(
1102 Context, Tag, Name, File, Line, getSubprogramRef(), BaseType,
1103 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1104 RuntimeLang, VTableHolder, TemplateParams, Identifier));
1105 EXPECT_NE(N, DICompositeType::get(
1106 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1107 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1108 RuntimeLang, VTableHolder, TemplateParams, Identifier));
1109 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1110 BaseType, SizeInBits + 1, AlignInBits,
1111 OffsetInBits, Flags, Elements, RuntimeLang,
1112 VTableHolder, TemplateParams, Identifier));
1113 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1114 BaseType, SizeInBits, AlignInBits + 1,
1115 OffsetInBits, Flags, Elements, RuntimeLang,
1116 VTableHolder, TemplateParams, Identifier));
1117 EXPECT_NE(N, DICompositeType::get(
1118 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1119 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1120 VTableHolder, TemplateParams, Identifier));
1121 EXPECT_NE(N, DICompositeType::get(
1122 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1123 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1124 VTableHolder, TemplateParams, Identifier));
1125 EXPECT_NE(N, DICompositeType::get(
1126 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1127 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1128 VTableHolder, TemplateParams, Identifier));
1129 EXPECT_NE(N, DICompositeType::get(
1130 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1131 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1132 VTableHolder, TemplateParams, Identifier));
1133 EXPECT_NE(N, DICompositeType::get(
1134 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1135 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1136 getCompositeType(), TemplateParams, Identifier));
1137 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1138 BaseType, SizeInBits, AlignInBits,
1139 OffsetInBits, Flags, Elements, RuntimeLang,
1140 VTableHolder, getTuple(), Identifier));
1141 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1142 BaseType, SizeInBits, AlignInBits,
1143 OffsetInBits, Flags, Elements, RuntimeLang,
1144 VTableHolder, TemplateParams, "other"));
1146 // Be sure that missing identifiers get null pointers.
1147 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1148 BaseType, SizeInBits, AlignInBits,
1149 OffsetInBits, Flags, Elements, RuntimeLang,
1150 VTableHolder, TemplateParams, "")
1151 ->getRawIdentifier());
1152 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1153 BaseType, SizeInBits, AlignInBits,
1154 OffsetInBits, Flags, Elements, RuntimeLang,
1155 VTableHolder, TemplateParams)
1156 ->getRawIdentifier());
1158 TempDICompositeType Temp = N->clone();
1159 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1162 TEST_F(DICompositeTypeTest, getWithLargeValues) {
1163 unsigned Tag = dwarf::DW_TAG_structure_type;
1164 StringRef Name = "some name";
1165 DIFile *File = getFile();
1167 DIScopeRef Scope = getSubprogramRef();
1168 DITypeRef BaseType = getCompositeType();
1169 uint64_t SizeInBits = UINT64_MAX;
1170 uint64_t AlignInBits = UINT64_MAX - 1;
1171 uint64_t OffsetInBits = UINT64_MAX - 2;
1173 MDTuple *Elements = getTuple();
1174 unsigned RuntimeLang = 6;
1175 DITypeRef VTableHolder = getCompositeType();
1176 MDTuple *TemplateParams = getTuple();
1177 StringRef Identifier = "some id";
1179 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1180 BaseType, SizeInBits, AlignInBits,
1181 OffsetInBits, Flags, Elements, RuntimeLang,
1182 VTableHolder, TemplateParams, Identifier);
1183 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1184 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1185 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1188 TEST_F(DICompositeTypeTest, replaceOperands) {
1189 unsigned Tag = dwarf::DW_TAG_structure_type;
1190 StringRef Name = "some name";
1191 DIFile *File = getFile();
1193 DIScopeRef Scope = getSubprogramRef();
1194 DITypeRef BaseType = getCompositeType();
1195 uint64_t SizeInBits = 2;
1196 uint64_t AlignInBits = 3;
1197 uint64_t OffsetInBits = 4;
1199 unsigned RuntimeLang = 6;
1200 StringRef Identifier = "some id";
1202 auto *N = DICompositeType::get(
1203 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1204 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
1206 auto *Elements = MDTuple::getDistinct(Context, None);
1207 EXPECT_EQ(nullptr, N->getElements().get());
1208 N->replaceElements(Elements);
1209 EXPECT_EQ(Elements, N->getElements().get());
1210 N->replaceElements(nullptr);
1211 EXPECT_EQ(nullptr, N->getElements().get());
1213 DITypeRef VTableHolder = getCompositeType();
1214 EXPECT_EQ(nullptr, N->getVTableHolder());
1215 N->replaceVTableHolder(VTableHolder);
1216 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1217 N->replaceVTableHolder(nullptr);
1218 EXPECT_EQ(nullptr, N->getVTableHolder());
1220 auto *TemplateParams = MDTuple::getDistinct(Context, None);
1221 EXPECT_EQ(nullptr, N->getTemplateParams().get());
1222 N->replaceTemplateParams(TemplateParams);
1223 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1224 N->replaceTemplateParams(nullptr);
1225 EXPECT_EQ(nullptr, N->getTemplateParams().get());
1228 typedef MetadataTest DISubroutineTypeTest;
1230 TEST_F(DISubroutineTypeTest, get) {
1232 MDTuple *TypeArray = getTuple();
1234 auto *N = DISubroutineType::get(Context, Flags, TypeArray);
1235 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1236 EXPECT_EQ(Flags, N->getFlags());
1237 EXPECT_EQ(TypeArray, N->getTypeArray().get());
1238 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, TypeArray));
1240 EXPECT_NE(N, DISubroutineType::get(Context, Flags + 1, TypeArray));
1241 EXPECT_NE(N, DISubroutineType::get(Context, Flags, getTuple()));
1243 TempDISubroutineType Temp = N->clone();
1244 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1246 // Test always-empty operands.
1247 EXPECT_EQ(nullptr, N->getScope());
1248 EXPECT_EQ(nullptr, N->getFile());
1249 EXPECT_EQ("", N->getName());
1250 EXPECT_EQ(nullptr, N->getBaseType());
1251 EXPECT_EQ(nullptr, N->getVTableHolder());
1252 EXPECT_EQ(nullptr, N->getTemplateParams().get());
1253 EXPECT_EQ("", N->getIdentifier());
1256 typedef MetadataTest DIFileTest;
1258 TEST_F(DIFileTest, get) {
1259 StringRef Filename = "file";
1260 StringRef Directory = "dir";
1261 auto *N = DIFile::get(Context, Filename, Directory);
1263 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1264 EXPECT_EQ(Filename, N->getFilename());
1265 EXPECT_EQ(Directory, N->getDirectory());
1266 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory));
1268 EXPECT_NE(N, DIFile::get(Context, "other", Directory));
1269 EXPECT_NE(N, DIFile::get(Context, Filename, "other"));
1271 TempDIFile Temp = N->clone();
1272 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1275 TEST_F(DIFileTest, ScopeGetFile) {
1276 // Ensure that DIScope::getFile() returns itself.
1277 DIScope *N = DIFile::get(Context, "file", "dir");
1278 EXPECT_EQ(N, N->getFile());
1281 typedef MetadataTest DICompileUnitTest;
1283 TEST_F(DICompileUnitTest, get) {
1284 unsigned SourceLanguage = 1;
1285 DIFile *File = getFile();
1286 StringRef Producer = "some producer";
1287 bool IsOptimized = false;
1288 StringRef Flags = "flag after flag";
1289 unsigned RuntimeVersion = 2;
1290 StringRef SplitDebugFilename = "another/file";
1291 unsigned EmissionKind = 3;
1292 MDTuple *EnumTypes = getTuple();
1293 MDTuple *RetainedTypes = getTuple();
1294 MDTuple *Subprograms = getTuple();
1295 MDTuple *GlobalVariables = getTuple();
1296 MDTuple *ImportedEntities = getTuple();
1297 auto *N = DICompileUnit::get(
1298 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1299 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1300 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1302 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1303 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1304 EXPECT_EQ(File, N->getFile());
1305 EXPECT_EQ(Producer, N->getProducer());
1306 EXPECT_EQ(IsOptimized, N->isOptimized());
1307 EXPECT_EQ(Flags, N->getFlags());
1308 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1309 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1310 EXPECT_EQ(EmissionKind, N->getEmissionKind());
1311 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1312 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
1313 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1314 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1315 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
1316 EXPECT_EQ(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1317 IsOptimized, Flags, RuntimeVersion,
1318 SplitDebugFilename, EmissionKind, EnumTypes,
1319 RetainedTypes, Subprograms, GlobalVariables,
1322 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1323 IsOptimized, Flags, RuntimeVersion,
1324 SplitDebugFilename, EmissionKind, EnumTypes,
1325 RetainedTypes, Subprograms, GlobalVariables,
1327 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, getFile(), Producer,
1328 IsOptimized, Flags, RuntimeVersion,
1329 SplitDebugFilename, EmissionKind, EnumTypes,
1330 RetainedTypes, Subprograms, GlobalVariables,
1332 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, "other",
1333 IsOptimized, Flags, RuntimeVersion,
1334 SplitDebugFilename, EmissionKind, EnumTypes,
1335 RetainedTypes, Subprograms, GlobalVariables,
1337 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1338 !IsOptimized, Flags, RuntimeVersion,
1339 SplitDebugFilename, EmissionKind, EnumTypes,
1340 RetainedTypes, Subprograms, GlobalVariables,
1342 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1343 IsOptimized, "other", RuntimeVersion,
1344 SplitDebugFilename, EmissionKind, EnumTypes,
1345 RetainedTypes, Subprograms, GlobalVariables,
1347 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1348 IsOptimized, Flags, RuntimeVersion + 1,
1349 SplitDebugFilename, EmissionKind, EnumTypes,
1350 RetainedTypes, Subprograms, GlobalVariables,
1353 DICompileUnit::get(Context, SourceLanguage, File, Producer,
1354 IsOptimized, Flags, RuntimeVersion, "other",
1355 EmissionKind, EnumTypes, RetainedTypes,
1356 Subprograms, GlobalVariables, ImportedEntities));
1357 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1358 IsOptimized, Flags, RuntimeVersion,
1359 SplitDebugFilename, EmissionKind + 1,
1360 EnumTypes, RetainedTypes, Subprograms,
1361 GlobalVariables, ImportedEntities));
1362 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1363 IsOptimized, Flags, RuntimeVersion,
1364 SplitDebugFilename, EmissionKind, getTuple(),
1365 RetainedTypes, Subprograms, GlobalVariables,
1367 EXPECT_NE(N, DICompileUnit::get(
1368 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1369 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1370 getTuple(), Subprograms, GlobalVariables, ImportedEntities));
1371 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1372 IsOptimized, Flags, RuntimeVersion,
1373 SplitDebugFilename, EmissionKind, EnumTypes,
1374 RetainedTypes, getTuple(), GlobalVariables,
1376 EXPECT_NE(N, DICompileUnit::get(
1377 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1378 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1379 RetainedTypes, Subprograms, getTuple(), ImportedEntities));
1380 EXPECT_NE(N, DICompileUnit::get(
1381 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1382 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1383 RetainedTypes, Subprograms, GlobalVariables, getTuple()));
1385 TempDICompileUnit Temp = N->clone();
1386 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1389 TEST_F(DICompileUnitTest, replaceArrays) {
1390 unsigned SourceLanguage = 1;
1391 DIFile *File = getFile();
1392 StringRef Producer = "some producer";
1393 bool IsOptimized = false;
1394 StringRef Flags = "flag after flag";
1395 unsigned RuntimeVersion = 2;
1396 StringRef SplitDebugFilename = "another/file";
1397 unsigned EmissionKind = 3;
1398 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1399 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1400 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
1401 auto *N = DICompileUnit::get(
1402 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1403 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1404 RetainedTypes, nullptr, nullptr, ImportedEntities);
1406 auto *Subprograms = MDTuple::getDistinct(Context, None);
1407 EXPECT_EQ(nullptr, N->getSubprograms().get());
1408 N->replaceSubprograms(Subprograms);
1409 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1410 N->replaceSubprograms(nullptr);
1411 EXPECT_EQ(nullptr, N->getSubprograms().get());
1413 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1414 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
1415 N->replaceGlobalVariables(GlobalVariables);
1416 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1417 N->replaceGlobalVariables(nullptr);
1418 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
1421 typedef MetadataTest DISubprogramTest;
1423 TEST_F(DISubprogramTest, get) {
1424 DIScopeRef Scope = getCompositeType();
1425 StringRef Name = "name";
1426 StringRef LinkageName = "linkage";
1427 DIFile *File = getFile();
1429 DISubroutineType *Type = getSubroutineType();
1430 bool IsLocalToUnit = false;
1431 bool IsDefinition = true;
1432 unsigned ScopeLine = 3;
1433 DITypeRef ContainingType = getCompositeType();
1434 unsigned Virtuality = 4;
1435 unsigned VirtualIndex = 5;
1437 bool IsOptimized = false;
1438 llvm::Function *Function = getFunction("foo");
1439 MDTuple *TemplateParams = getTuple();
1440 DISubprogram *Declaration = getSubprogram();
1441 MDTuple *Variables = getTuple();
1443 auto *N = DISubprogram::get(
1444 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1445 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1446 IsOptimized, Function, TemplateParams, Declaration, Variables);
1448 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1449 EXPECT_EQ(Scope, N->getScope());
1450 EXPECT_EQ(Name, N->getName());
1451 EXPECT_EQ(LinkageName, N->getLinkageName());
1452 EXPECT_EQ(File, N->getFile());
1453 EXPECT_EQ(Line, N->getLine());
1454 EXPECT_EQ(Type, N->getType());
1455 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1456 EXPECT_EQ(IsDefinition, N->isDefinition());
1457 EXPECT_EQ(ScopeLine, N->getScopeLine());
1458 EXPECT_EQ(ContainingType, N->getContainingType());
1459 EXPECT_EQ(Virtuality, N->getVirtuality());
1460 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1461 EXPECT_EQ(Flags, N->getFlags());
1462 EXPECT_EQ(IsOptimized, N->isOptimized());
1463 EXPECT_EQ(Function, N->getFunction());
1464 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1465 EXPECT_EQ(Declaration, N->getDeclaration());
1466 EXPECT_EQ(Variables, N->getVariables().get());
1467 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1468 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1469 ContainingType, Virtuality, VirtualIndex,
1470 Flags, IsOptimized, Function, TemplateParams,
1471 Declaration, Variables));
1473 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
1474 File, Line, Type, IsLocalToUnit, IsDefinition,
1475 ScopeLine, ContainingType, Virtuality,
1476 VirtualIndex, Flags, IsOptimized, Function,
1477 TemplateParams, Declaration, Variables));
1478 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
1479 Line, Type, IsLocalToUnit, IsDefinition,
1480 ScopeLine, ContainingType, Virtuality,
1481 VirtualIndex, Flags, IsOptimized, Function,
1482 TemplateParams, Declaration, Variables));
1483 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
1484 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1485 ContainingType, Virtuality, VirtualIndex,
1486 Flags, IsOptimized, Function, TemplateParams,
1487 Declaration, Variables));
1488 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
1489 Line, Type, IsLocalToUnit, IsDefinition,
1490 ScopeLine, ContainingType, Virtuality,
1491 VirtualIndex, Flags, IsOptimized, Function,
1492 TemplateParams, Declaration, Variables));
1493 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
1494 Line + 1, Type, IsLocalToUnit, IsDefinition,
1495 ScopeLine, ContainingType, Virtuality,
1496 VirtualIndex, Flags, IsOptimized, Function,
1497 TemplateParams, Declaration, Variables));
1498 EXPECT_NE(N, DISubprogram::get(
1499 Context, Scope, Name, LinkageName, File, Line,
1500 getSubroutineType(), IsLocalToUnit, IsDefinition, ScopeLine,
1501 ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1502 Function, TemplateParams, Declaration, Variables));
1503 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1504 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1505 ContainingType, Virtuality, VirtualIndex,
1506 Flags, IsOptimized, Function, TemplateParams,
1507 Declaration, Variables));
1508 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1509 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1510 ContainingType, Virtuality, VirtualIndex,
1511 Flags, IsOptimized, Function, TemplateParams,
1512 Declaration, Variables));
1513 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1514 Type, IsLocalToUnit, IsDefinition,
1515 ScopeLine + 1, ContainingType, Virtuality,
1516 VirtualIndex, Flags, IsOptimized, Function,
1517 TemplateParams, Declaration, Variables));
1518 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1519 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1520 getCompositeType(), Virtuality, VirtualIndex,
1521 Flags, IsOptimized, Function, TemplateParams,
1522 Declaration, Variables));
1523 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1524 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1525 ContainingType, Virtuality + 1, VirtualIndex,
1526 Flags, IsOptimized, Function, TemplateParams,
1527 Declaration, Variables));
1528 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1529 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1530 ContainingType, Virtuality, VirtualIndex + 1,
1531 Flags, IsOptimized, Function, TemplateParams,
1532 Declaration, Variables));
1533 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1534 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1535 ContainingType, Virtuality, VirtualIndex,
1536 ~Flags, IsOptimized, Function, TemplateParams,
1537 Declaration, Variables));
1538 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1539 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1540 ContainingType, Virtuality, VirtualIndex,
1541 Flags, !IsOptimized, Function, TemplateParams,
1542 Declaration, Variables));
1543 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1544 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1545 ContainingType, Virtuality, VirtualIndex,
1546 Flags, IsOptimized, getFunction("bar"),
1547 TemplateParams, Declaration, Variables));
1548 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1549 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1550 ContainingType, Virtuality, VirtualIndex,
1551 Flags, IsOptimized, Function, getTuple(),
1552 Declaration, Variables));
1553 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1554 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1555 ContainingType, Virtuality, VirtualIndex,
1556 Flags, IsOptimized, Function, TemplateParams,
1557 getSubprogram(), Variables));
1558 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1559 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1560 ContainingType, Virtuality, VirtualIndex,
1561 Flags, IsOptimized, Function, TemplateParams,
1562 Declaration, getTuple()));
1564 TempDISubprogram Temp = N->clone();
1565 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1568 TEST_F(DISubprogramTest, replaceFunction) {
1569 DIScopeRef Scope = getCompositeType();
1570 StringRef Name = "name";
1571 StringRef LinkageName = "linkage";
1572 DIFile *File = getFile();
1574 DISubroutineType *Type = getSubroutineType();
1575 bool IsLocalToUnit = false;
1576 bool IsDefinition = true;
1577 unsigned ScopeLine = 3;
1578 DITypeRef ContainingType = getCompositeType();
1579 unsigned Virtuality = 4;
1580 unsigned VirtualIndex = 5;
1582 bool IsOptimized = false;
1583 MDTuple *TemplateParams = getTuple();
1584 DISubprogram *Declaration = getSubprogram();
1585 MDTuple *Variables = getTuple();
1587 auto *N = DISubprogram::get(
1588 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1589 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1590 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1592 EXPECT_EQ(nullptr, N->getFunction());
1594 std::unique_ptr<Function> F(
1595 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1596 GlobalValue::ExternalLinkage));
1597 N->replaceFunction(F.get());
1598 EXPECT_EQ(F.get(), N->getFunction());
1600 N->replaceFunction(nullptr);
1601 EXPECT_EQ(nullptr, N->getFunction());
1604 typedef MetadataTest DILexicalBlockTest;
1606 TEST_F(DILexicalBlockTest, get) {
1607 DILocalScope *Scope = getSubprogram();
1608 DIFile *File = getFile();
1610 unsigned Column = 8;
1612 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
1614 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1615 EXPECT_EQ(Scope, N->getScope());
1616 EXPECT_EQ(File, N->getFile());
1617 EXPECT_EQ(Line, N->getLine());
1618 EXPECT_EQ(Column, N->getColumn());
1619 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
1622 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1623 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1624 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1625 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
1627 TempDILexicalBlock Temp = N->clone();
1628 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1631 typedef MetadataTest DILexicalBlockFileTest;
1633 TEST_F(DILexicalBlockFileTest, get) {
1634 DILocalScope *Scope = getSubprogram();
1635 DIFile *File = getFile();
1636 unsigned Discriminator = 5;
1638 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
1640 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1641 EXPECT_EQ(Scope, N->getScope());
1642 EXPECT_EQ(File, N->getFile());
1643 EXPECT_EQ(Discriminator, N->getDiscriminator());
1644 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
1646 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
1649 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
1651 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1653 TempDILexicalBlockFile Temp = N->clone();
1654 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1657 typedef MetadataTest DINamespaceTest;
1659 TEST_F(DINamespaceTest, get) {
1660 DIScope *Scope = getFile();
1661 DIFile *File = getFile();
1662 StringRef Name = "namespace";
1665 auto *N = DINamespace::get(Context, Scope, File, Name, Line);
1667 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1668 EXPECT_EQ(Scope, N->getScope());
1669 EXPECT_EQ(File, N->getFile());
1670 EXPECT_EQ(Name, N->getName());
1671 EXPECT_EQ(Line, N->getLine());
1672 EXPECT_EQ(N, DINamespace::get(Context, Scope, File, Name, Line));
1674 EXPECT_NE(N, DINamespace::get(Context, getFile(), File, Name, Line));
1675 EXPECT_NE(N, DINamespace::get(Context, Scope, getFile(), Name, Line));
1676 EXPECT_NE(N, DINamespace::get(Context, Scope, File, "other", Line));
1677 EXPECT_NE(N, DINamespace::get(Context, Scope, File, Name, Line + 1));
1679 TempDINamespace Temp = N->clone();
1680 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1683 typedef MetadataTest DITemplateTypeParameterTest;
1685 TEST_F(DITemplateTypeParameterTest, get) {
1686 StringRef Name = "template";
1687 DITypeRef Type = getBasicType("basic");
1689 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
1691 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1692 EXPECT_EQ(Name, N->getName());
1693 EXPECT_EQ(Type, N->getType());
1694 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
1696 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
1698 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
1700 TempDITemplateTypeParameter Temp = N->clone();
1701 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1704 typedef MetadataTest DITemplateValueParameterTest;
1706 TEST_F(DITemplateValueParameterTest, get) {
1707 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1708 StringRef Name = "template";
1709 DITypeRef Type = getBasicType("basic");
1710 Metadata *Value = getConstantAsMetadata();
1712 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
1713 EXPECT_EQ(Tag, N->getTag());
1714 EXPECT_EQ(Name, N->getName());
1715 EXPECT_EQ(Type, N->getType());
1716 EXPECT_EQ(Value, N->getValue());
1717 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
1719 EXPECT_NE(N, DITemplateValueParameter::get(
1720 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1723 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1724 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
1725 getBasicType("other"), Value));
1726 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
1727 getConstantAsMetadata()));
1729 TempDITemplateValueParameter Temp = N->clone();
1730 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1733 typedef MetadataTest DIGlobalVariableTest;
1735 TEST_F(DIGlobalVariableTest, get) {
1736 DIScope *Scope = getSubprogram();
1737 StringRef Name = "name";
1738 StringRef LinkageName = "linkage";
1739 DIFile *File = getFile();
1741 DITypeRef Type = getDerivedType();
1742 bool IsLocalToUnit = false;
1743 bool IsDefinition = true;
1744 Constant *Variable = getConstant();
1745 DIDerivedType *StaticDataMemberDeclaration =
1746 cast<DIDerivedType>(getDerivedType());
1748 auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1749 Type, IsLocalToUnit, IsDefinition, Variable,
1750 StaticDataMemberDeclaration);
1751 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1752 EXPECT_EQ(Scope, N->getScope());
1753 EXPECT_EQ(Name, N->getName());
1754 EXPECT_EQ(LinkageName, N->getLinkageName());
1755 EXPECT_EQ(File, N->getFile());
1756 EXPECT_EQ(Line, N->getLine());
1757 EXPECT_EQ(Type, N->getType());
1758 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1759 EXPECT_EQ(IsDefinition, N->isDefinition());
1760 EXPECT_EQ(Variable, N->getVariable());
1761 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1762 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1763 Line, Type, IsLocalToUnit, IsDefinition,
1764 Variable, StaticDataMemberDeclaration));
1767 DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
1768 File, Line, Type, IsLocalToUnit, IsDefinition,
1769 Variable, StaticDataMemberDeclaration));
1770 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1771 Line, Type, IsLocalToUnit, IsDefinition,
1772 Variable, StaticDataMemberDeclaration));
1773 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1774 Type, IsLocalToUnit, IsDefinition,
1775 Variable, StaticDataMemberDeclaration));
1777 DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
1778 Line, Type, IsLocalToUnit, IsDefinition,
1779 Variable, StaticDataMemberDeclaration));
1781 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1782 Line + 1, Type, IsLocalToUnit, IsDefinition,
1783 Variable, StaticDataMemberDeclaration));
1785 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1786 getDerivedType(), IsLocalToUnit, IsDefinition,
1787 Variable, StaticDataMemberDeclaration));
1788 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1789 Line, Type, !IsLocalToUnit, IsDefinition,
1790 Variable, StaticDataMemberDeclaration));
1791 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1792 Line, Type, IsLocalToUnit, !IsDefinition,
1793 Variable, StaticDataMemberDeclaration));
1795 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1796 Type, IsLocalToUnit, IsDefinition,
1797 getConstant(), StaticDataMemberDeclaration));
1799 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1800 Type, IsLocalToUnit, IsDefinition, Variable,
1801 cast<DIDerivedType>(getDerivedType())));
1803 TempDIGlobalVariable Temp = N->clone();
1804 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1807 typedef MetadataTest DILocalVariableTest;
1809 TEST_F(DILocalVariableTest, get) {
1810 unsigned Tag = dwarf::DW_TAG_arg_variable;
1811 DILocalScope *Scope = getSubprogram();
1812 StringRef Name = "name";
1813 DIFile *File = getFile();
1815 DITypeRef Type = getDerivedType();
1819 auto *N = DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
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(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1832 EXPECT_NE(N, DILocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1833 Name, File, Line, Type, Arg, Flags));
1834 EXPECT_NE(N, DILocalVariable::get(Context, Tag, getSubprogram(), Name, File,
1835 Line, Type, Arg, Flags));
1836 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, "other", File, Line,
1838 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, getFile(), Line,
1840 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1842 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line,
1843 getDerivedType(), Arg, Flags));
1844 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1846 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1849 TempDILocalVariable Temp = N->clone();
1850 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1853 TEST_F(DILocalVariableTest, getArg256) {
1854 EXPECT_EQ(255u, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
1855 getSubprogram(), "", getFile(), 0,
1858 EXPECT_EQ(256u, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
1859 getSubprogram(), "", getFile(), 0,
1862 EXPECT_EQ(257u, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
1863 getSubprogram(), "", getFile(), 0,
1866 unsigned Max = UINT16_MAX;
1867 EXPECT_EQ(Max, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
1868 getSubprogram(), "", getFile(), 0,
1873 typedef MetadataTest DIExpressionTest;
1875 TEST_F(DIExpressionTest, get) {
1876 uint64_t Elements[] = {2, 6, 9, 78, 0};
1877 auto *N = DIExpression::get(Context, Elements);
1878 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1879 EXPECT_EQ(N, DIExpression::get(Context, Elements));
1881 EXPECT_EQ(5u, N->getNumElements());
1882 EXPECT_EQ(2u, N->getElement(0));
1883 EXPECT_EQ(6u, N->getElement(1));
1884 EXPECT_EQ(9u, N->getElement(2));
1885 EXPECT_EQ(78u, N->getElement(3));
1886 EXPECT_EQ(0u, N->getElement(4));
1888 TempDIExpression Temp = N->clone();
1889 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1892 TEST_F(DIExpressionTest, isValid) {
1893 #define EXPECT_VALID(...) \
1895 uint64_t Elements[] = {__VA_ARGS__}; \
1896 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
1898 #define EXPECT_INVALID(...) \
1900 uint64_t Elements[] = {__VA_ARGS__}; \
1901 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
1904 // Empty expression should be valid.
1905 EXPECT_TRUE(DIExpression::get(Context, None));
1907 // Valid constructions.
1908 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1909 EXPECT_VALID(dwarf::DW_OP_deref);
1910 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1911 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1912 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1913 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1914 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1916 // Invalid constructions.
1917 EXPECT_INVALID(~0u);
1918 EXPECT_INVALID(dwarf::DW_OP_plus);
1919 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1920 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1921 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1922 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1925 #undef EXPECT_INVALID
1928 typedef MetadataTest DIObjCPropertyTest;
1930 TEST_F(DIObjCPropertyTest, get) {
1931 StringRef Name = "name";
1932 DIFile *File = getFile();
1934 StringRef GetterName = "getter";
1935 StringRef SetterName = "setter";
1936 unsigned Attributes = 7;
1937 DIType *Type = cast<DIBasicType>(getBasicType("basic"));
1939 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
1940 SetterName, Attributes, Type);
1942 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1943 EXPECT_EQ(Name, N->getName());
1944 EXPECT_EQ(File, N->getFile());
1945 EXPECT_EQ(Line, N->getLine());
1946 EXPECT_EQ(GetterName, N->getGetterName());
1947 EXPECT_EQ(SetterName, N->getSetterName());
1948 EXPECT_EQ(Attributes, N->getAttributes());
1949 EXPECT_EQ(Type, N->getType());
1950 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
1951 SetterName, Attributes, Type));
1953 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
1954 SetterName, Attributes, Type));
1955 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
1956 SetterName, Attributes, Type));
1957 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1958 SetterName, Attributes, Type));
1959 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
1960 SetterName, Attributes, Type));
1961 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
1962 "other", Attributes, Type));
1963 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
1964 SetterName, Attributes + 1, Type));
1965 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
1966 SetterName, Attributes,
1967 cast<DIBasicType>(getBasicType("other"))));
1969 TempDIObjCProperty Temp = N->clone();
1970 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1973 typedef MetadataTest DIImportedEntityTest;
1975 TEST_F(DIImportedEntityTest, get) {
1976 unsigned Tag = dwarf::DW_TAG_imported_module;
1977 DIScope *Scope = getSubprogram();
1978 DINodeRef Entity = getCompositeType();
1980 StringRef Name = "name";
1982 auto *N = DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1984 EXPECT_EQ(Tag, N->getTag());
1985 EXPECT_EQ(Scope, N->getScope());
1986 EXPECT_EQ(Entity, N->getEntity());
1987 EXPECT_EQ(Line, N->getLine());
1988 EXPECT_EQ(Name, N->getName());
1989 EXPECT_EQ(N, DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1992 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1993 Scope, Entity, Line, Name));
1994 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
1996 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
1999 DIImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
2001 DIImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
2003 TempDIImportedEntity Temp = N->clone();
2004 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2007 typedef MetadataTest MetadataAsValueTest;
2009 TEST_F(MetadataAsValueTest, MDNode) {
2010 MDNode *N = MDNode::get(Context, None);
2011 auto *V = MetadataAsValue::get(Context, N);
2012 EXPECT_TRUE(V->getType()->isMetadataTy());
2013 EXPECT_EQ(N, V->getMetadata());
2015 auto *V2 = MetadataAsValue::get(Context, N);
2019 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2020 MDNode *N = MDNode::get(Context, None);
2021 Metadata *Ops[] = {N};
2022 MDNode *N2 = MDNode::get(Context, Ops);
2023 auto *V = MetadataAsValue::get(Context, N2);
2024 EXPECT_TRUE(V->getType()->isMetadataTy());
2025 EXPECT_EQ(N2, V->getMetadata());
2027 auto *V2 = MetadataAsValue::get(Context, N2);
2030 auto *V3 = MetadataAsValue::get(Context, N);
2031 EXPECT_TRUE(V3->getType()->isMetadataTy());
2033 EXPECT_EQ(N, V3->getMetadata());
2036 TEST_F(MetadataAsValueTest, MDNodeConstant) {
2037 auto *C = ConstantInt::getTrue(Context);
2038 auto *MD = ConstantAsMetadata::get(C);
2039 Metadata *Ops[] = {MD};
2040 auto *N = MDNode::get(Context, Ops);
2042 auto *V = MetadataAsValue::get(Context, MD);
2043 EXPECT_TRUE(V->getType()->isMetadataTy());
2044 EXPECT_EQ(MD, V->getMetadata());
2046 auto *V2 = MetadataAsValue::get(Context, N);
2047 EXPECT_EQ(MD, V2->getMetadata());
2051 typedef MetadataTest ValueAsMetadataTest;
2053 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2054 Type *Ty = Type::getInt1PtrTy(Context);
2055 std::unique_ptr<GlobalVariable> GV0(
2056 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2057 auto *MD = ValueAsMetadata::get(GV0.get());
2058 EXPECT_TRUE(MD->getValue() == GV0.get());
2059 ASSERT_TRUE(GV0->use_empty());
2061 std::unique_ptr<GlobalVariable> GV1(
2062 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2063 GV0->replaceAllUsesWith(GV1.get());
2064 EXPECT_TRUE(MD->getValue() == GV1.get());
2067 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2068 // Create a constant.
2069 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2070 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2072 // Create a temporary to prevent nodes from resolving.
2073 auto Temp = MDTuple::getTemporary(Context, None);
2075 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
2076 Metadata *Ops1[] = {CI, CI, Temp.get()};
2077 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
2079 auto *N1 = MDTuple::get(Context, Ops1);
2080 auto *N2 = MDTuple::get(Context, Ops2);
2083 // Tell metadata that the constant is getting deleted.
2085 // After this, N1 will be invalid, so don't touch it.
2086 ValueAsMetadata::handleDeletion(CI->getValue());
2087 EXPECT_EQ(nullptr, N2->getOperand(0));
2088 EXPECT_EQ(nullptr, N2->getOperand(1));
2089 EXPECT_EQ(Temp.get(), N2->getOperand(2));
2091 // Clean up Temp for teardown.
2092 Temp->replaceAllUsesWith(nullptr);
2095 typedef MetadataTest TrackingMDRefTest;
2097 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2098 Type *Ty = Type::getInt1PtrTy(Context);
2099 std::unique_ptr<GlobalVariable> GV0(
2100 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2101 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2102 EXPECT_TRUE(MD->getValue() == GV0.get());
2103 ASSERT_TRUE(GV0->use_empty());
2105 std::unique_ptr<GlobalVariable> GV1(
2106 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2107 GV0->replaceAllUsesWith(GV1.get());
2108 EXPECT_TRUE(MD->getValue() == GV1.get());
2110 // Reset it, so we don't inadvertently test deletion.
2114 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2115 Type *Ty = Type::getInt1PtrTy(Context);
2116 std::unique_ptr<GlobalVariable> GV(
2117 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2118 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2119 EXPECT_TRUE(MD->getValue() == GV.get());
2120 ASSERT_TRUE(GV->use_empty());
2126 TEST(NamedMDNodeTest, Search) {
2127 LLVMContext Context;
2128 ConstantAsMetadata *C =
2129 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2130 ConstantAsMetadata *C2 =
2131 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
2133 Metadata *const V = C;
2134 Metadata *const V2 = C2;
2135 MDNode *n = MDNode::get(Context, V);
2136 MDNode *n2 = MDNode::get(Context, V2);
2138 Module M("MyModule", Context);
2139 const char *Name = "llvm.NMD1";
2140 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2142 NMD->addOperand(n2);
2145 raw_string_ostream oss(Str);
2147 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
2151 typedef MetadataTest FunctionAttachmentTest;
2152 TEST_F(FunctionAttachmentTest, setMetadata) {
2153 Function *F = getFunction("foo");
2154 ASSERT_FALSE(F->hasMetadata());
2155 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2156 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2157 EXPECT_EQ(nullptr, F->getMetadata("other"));
2159 DISubprogram *SP1 = getSubprogram();
2160 DISubprogram *SP2 = getSubprogram();
2161 ASSERT_NE(SP1, SP2);
2163 F->setMetadata("dbg", SP1);
2164 EXPECT_TRUE(F->hasMetadata());
2165 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2166 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2167 EXPECT_EQ(nullptr, F->getMetadata("other"));
2169 F->setMetadata(LLVMContext::MD_dbg, SP2);
2170 EXPECT_TRUE(F->hasMetadata());
2171 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2172 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2173 EXPECT_EQ(nullptr, F->getMetadata("other"));
2175 F->setMetadata("dbg", nullptr);
2176 EXPECT_FALSE(F->hasMetadata());
2177 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2178 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2179 EXPECT_EQ(nullptr, F->getMetadata("other"));
2181 MDTuple *T1 = getTuple();
2182 MDTuple *T2 = getTuple();
2185 F->setMetadata("other1", T1);
2186 F->setMetadata("other2", T2);
2187 EXPECT_TRUE(F->hasMetadata());
2188 EXPECT_EQ(T1, F->getMetadata("other1"));
2189 EXPECT_EQ(T2, F->getMetadata("other2"));
2190 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2192 F->setMetadata("other1", T2);
2193 F->setMetadata("other2", T1);
2194 EXPECT_EQ(T2, F->getMetadata("other1"));
2195 EXPECT_EQ(T1, F->getMetadata("other2"));
2197 F->setMetadata("other1", nullptr);
2198 F->setMetadata("other2", nullptr);
2199 EXPECT_FALSE(F->hasMetadata());
2200 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2201 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2204 TEST_F(FunctionAttachmentTest, getAll) {
2205 Function *F = getFunction("foo");
2207 MDTuple *T1 = getTuple();
2208 MDTuple *T2 = getTuple();
2209 MDTuple *P = getTuple();
2210 DISubprogram *SP = getSubprogram();
2212 F->setMetadata("other1", T2);
2213 F->setMetadata(LLVMContext::MD_dbg, SP);
2214 F->setMetadata("other2", T1);
2215 F->setMetadata(LLVMContext::MD_prof, P);
2216 F->setMetadata("other2", T2);
2217 F->setMetadata("other1", T1);
2219 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2220 F->getAllMetadata(MDs);
2221 ASSERT_EQ(4u, MDs.size());
2222 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2223 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2224 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2225 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2226 EXPECT_EQ(SP, MDs[0].second);
2227 EXPECT_EQ(P, MDs[1].second);
2228 EXPECT_EQ(T1, MDs[2].second);
2229 EXPECT_EQ(T2, MDs[3].second);
2232 TEST_F(FunctionAttachmentTest, dropUnknownMetadata) {
2233 Function *F = getFunction("foo");
2235 MDTuple *T1 = getTuple();
2236 MDTuple *T2 = getTuple();
2237 MDTuple *P = getTuple();
2238 DISubprogram *SP = getSubprogram();
2240 F->setMetadata("other1", T1);
2241 F->setMetadata(LLVMContext::MD_dbg, SP);
2242 F->setMetadata("other2", T2);
2243 F->setMetadata(LLVMContext::MD_prof, P);
2245 unsigned Known[] = {Context.getMDKindID("other2"), LLVMContext::MD_prof};
2246 F->dropUnknownMetadata(Known);
2248 EXPECT_EQ(T2, F->getMetadata("other2"));
2249 EXPECT_EQ(P, F->getMetadata(LLVMContext::MD_prof));
2250 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2251 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2253 F->setMetadata("other2", nullptr);
2254 F->setMetadata(LLVMContext::MD_prof, nullptr);
2255 EXPECT_FALSE(F->hasMetadata());
2258 TEST_F(FunctionAttachmentTest, Verifier) {
2259 Function *F = getFunction("foo");
2260 F->setMetadata("attach", getTuple());
2262 // Confirm this has no body.
2263 ASSERT_TRUE(F->empty());
2265 // Functions without a body cannot have metadata attachments (they also can't
2266 // be verified directly, so check that the module fails to verify).
2267 EXPECT_TRUE(verifyModule(*F->getParent()));
2269 // Functions with a body can.
2270 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2271 EXPECT_FALSE(verifyModule(*F->getParent()));
2272 EXPECT_FALSE(verifyFunction(*F));
2275 TEST_F(FunctionAttachmentTest, EntryCount) {
2276 Function *F = getFunction("foo");
2277 EXPECT_FALSE(F->getEntryCount().hasValue());
2278 F->setEntryCount(12304);
2279 EXPECT_TRUE(F->getEntryCount().hasValue());
2280 EXPECT_EQ(12304u, *F->getEntryCount());