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 {
66 MDNode *getNode() { return MDNode::get(Context, None); }
67 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
68 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
69 Metadata *MDs[] = {MD1, MD2};
70 return MDNode::get(Context, MDs);
73 typedef MetadataTest MDStringTest;
75 // Test that construction of MDString with different value produces different
76 // MDString objects, even with the same string pointer and nulls in the string.
77 TEST_F(MDStringTest, CreateDifferent) {
78 char x[3] = { 'f', 0, 'A' };
79 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
81 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
85 // Test that creation of MDStrings with the same string contents produces the
86 // same MDString object, even with different pointers.
87 TEST_F(MDStringTest, CreateSame) {
88 char x[4] = { 'a', 'b', 'c', 'X' };
89 char y[4] = { 'a', 'b', 'c', 'Y' };
91 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
92 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
96 // Test that MDString prints out the string we fed it.
97 TEST_F(MDStringTest, PrintingSimple) {
98 char *str = new char[13];
99 strncpy(str, "testing 1 2 3", 13);
100 MDString *s = MDString::get(Context, StringRef(str, 13));
101 strncpy(str, "aaaaaaaaaaaaa", 13);
105 raw_string_ostream oss(Str);
107 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
110 // Test printing of MDString with non-printable characters.
111 TEST_F(MDStringTest, PrintingComplex) {
112 char str[5] = {0, '\n', '"', '\\', (char)-1};
113 MDString *s = MDString::get(Context, StringRef(str+0, 5));
115 raw_string_ostream oss(Str);
117 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
120 typedef MetadataTest MDNodeTest;
122 // Test the two constructors, and containing other Constants.
123 TEST_F(MDNodeTest, Simple) {
124 char x[3] = { 'a', 'b', 'c' };
125 char y[3] = { '1', '2', '3' };
127 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
128 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
129 ConstantAsMetadata *CI = ConstantAsMetadata::get(
130 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
132 std::vector<Metadata *> V;
137 MDNode *n1 = MDNode::get(Context, V);
138 Metadata *const c1 = n1;
139 MDNode *n2 = MDNode::get(Context, c1);
140 Metadata *const c2 = n2;
141 MDNode *n3 = MDNode::get(Context, V);
142 MDNode *n4 = MDNode::getIfExists(Context, V);
143 MDNode *n5 = MDNode::getIfExists(Context, c1);
144 MDNode *n6 = MDNode::getIfExists(Context, c2);
149 EXPECT_EQ(n6, (Metadata *)nullptr);
151 EXPECT_EQ(3u, n1->getNumOperands());
152 EXPECT_EQ(s1, n1->getOperand(0));
153 EXPECT_EQ(CI, n1->getOperand(1));
154 EXPECT_EQ(s2, n1->getOperand(2));
156 EXPECT_EQ(1u, n2->getNumOperands());
157 EXPECT_EQ(n1, n2->getOperand(0));
160 TEST_F(MDNodeTest, Delete) {
161 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
162 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
164 Metadata *const V = LocalAsMetadata::get(I);
165 MDNode *n = MDNode::get(Context, V);
166 TrackingMDRef wvh(n);
173 TEST_F(MDNodeTest, SelfReference) {
177 auto Temp = MDNode::getTemporary(Context, None);
178 Metadata *Args[] = {Temp.get()};
179 MDNode *Self = MDNode::get(Context, Args);
180 Self->replaceOperandWith(0, Self);
181 ASSERT_EQ(Self, Self->getOperand(0));
183 // Self-references should be distinct, so MDNode::get() should grab a
184 // uniqued node that references Self, not Self.
186 MDNode *Ref1 = MDNode::get(Context, Args);
187 MDNode *Ref2 = MDNode::get(Context, Args);
188 EXPECT_NE(Self, Ref1);
189 EXPECT_EQ(Ref1, Ref2);
195 auto Temp = MDNode::getTemporary(Context, None);
196 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
197 MDNode *Self = MDNode::get(Context, Args);
198 Self->replaceOperandWith(0, Self);
199 ASSERT_EQ(Self, Self->getOperand(0));
201 // Self-references should be distinct, so MDNode::get() should grab a
202 // uniqued node that references Self, not Self itself.
204 MDNode *Ref1 = MDNode::get(Context, Args);
205 MDNode *Ref2 = MDNode::get(Context, Args);
206 EXPECT_NE(Self, Ref1);
207 EXPECT_EQ(Ref1, Ref2);
211 TEST_F(MDNodeTest, Print) {
212 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
213 MDString *S = MDString::get(Context, "foo");
214 MDNode *N0 = getNode();
215 MDNode *N1 = getNode(N0);
216 MDNode *N2 = getNode(N0, N1);
218 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
219 MDNode *N = MDNode::get(Context, Args);
221 std::string Expected;
223 raw_string_ostream OS(Expected);
224 OS << "<" << (void *)N << "> = !{";
225 C->printAsOperand(OS);
227 S->printAsOperand(OS);
229 MDNode *Nodes[] = {N0, N1, N2};
230 for (auto *Node : Nodes)
231 OS << ", <" << (void *)Node << ">";
237 raw_string_ostream OS(Actual);
241 EXPECT_EQ(Expected, Actual);
244 #define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
246 std::string Actual_; \
247 raw_string_ostream OS(Actual_); \
250 std::string Expected_(EXPECTED); \
251 EXPECT_EQ(Expected_, Actual_); \
254 TEST_F(MDNodeTest, PrintTemporary) {
255 MDNode *Arg = getNode();
256 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
257 MDNode *N = getNode(Temp.get());
258 Module M("test", Context);
259 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
262 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
263 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
264 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
267 Temp->replaceAllUsesWith(Arg);
270 TEST_F(MDNodeTest, PrintFromModule) {
271 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
272 MDString *S = MDString::get(Context, "foo");
273 MDNode *N0 = getNode();
274 MDNode *N1 = getNode(N0);
275 MDNode *N2 = getNode(N0, N1);
277 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
278 MDNode *N = MDNode::get(Context, Args);
279 Module M("test", Context);
280 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
283 std::string Expected;
285 raw_string_ostream OS(Expected);
287 C->printAsOperand(OS);
289 S->printAsOperand(OS);
290 OS << ", null, !1, !2, !3}";
293 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
296 TEST_F(MDNodeTest, PrintFromFunction) {
297 Module M("test", Context);
298 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
299 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
300 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
301 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
302 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
303 auto *R0 = ReturnInst::Create(Context, BB0);
304 auto *R1 = ReturnInst::Create(Context, BB1);
305 auto *N0 = MDNode::getDistinct(Context, None);
306 auto *N1 = MDNode::getDistinct(Context, None);
307 R0->setMetadata("md", N0);
308 R1->setMetadata("md", N1);
310 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
311 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
314 TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
315 Module M("test", Context);
318 Function::Create(FunctionType::get(Type::getVoidTy(Context),
319 Type::getMetadataTy(Context), false),
320 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
322 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
323 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
324 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
325 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
326 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
327 auto *N0 = MDNode::getDistinct(Context, None);
328 auto *N1 = MDNode::getDistinct(Context, None);
329 auto *MAV0 = MetadataAsValue::get(Context, N0);
330 auto *MAV1 = MetadataAsValue::get(Context, N1);
331 CallInst::Create(Intrinsic, MAV0, "", BB0);
332 CallInst::Create(Intrinsic, MAV1, "", BB1);
334 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
335 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
336 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
337 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
338 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
339 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
341 #undef EXPECT_PRINTER_EQ
343 TEST_F(MDNodeTest, NullOperand) {
345 MDNode *Empty = MDNode::get(Context, None);
347 // metadata !{metadata !{}}
348 Metadata *Ops[] = {Empty};
349 MDNode *N = MDNode::get(Context, Ops);
350 ASSERT_EQ(Empty, N->getOperand(0));
352 // metadata !{metadata !{}} => metadata !{null}
353 N->replaceOperandWith(0, nullptr);
354 ASSERT_EQ(nullptr, N->getOperand(0));
358 MDNode *NullOp = MDNode::get(Context, Ops);
359 ASSERT_EQ(nullptr, NullOp->getOperand(0));
360 EXPECT_EQ(N, NullOp);
363 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
365 MDNode *Empty = MDNode::get(Context, None);
366 ASSERT_TRUE(Empty->isResolved());
367 EXPECT_FALSE(Empty->isDistinct());
370 Metadata *Wrapped1Ops[] = {Empty};
371 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
372 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
373 ASSERT_TRUE(Wrapped1->isResolved());
374 EXPECT_FALSE(Wrapped1->isDistinct());
377 Metadata *Wrapped2Ops[] = {Wrapped1};
378 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
379 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
380 ASSERT_TRUE(Wrapped2->isResolved());
381 EXPECT_FALSE(Wrapped2->isDistinct());
383 // !{!{!{}}} => !{!{}}
384 Wrapped2->replaceOperandWith(0, Empty);
385 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
386 EXPECT_TRUE(Wrapped2->isDistinct());
387 EXPECT_FALSE(Wrapped1->isDistinct());
390 TEST_F(MDNodeTest, getDistinct) {
392 MDNode *Empty = MDNode::get(Context, None);
393 ASSERT_TRUE(Empty->isResolved());
394 ASSERT_FALSE(Empty->isDistinct());
395 ASSERT_EQ(Empty, MDNode::get(Context, None));
398 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
399 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
400 EXPECT_TRUE(Distinct1->isResolved());
401 EXPECT_TRUE(Distinct2->isDistinct());
402 EXPECT_NE(Empty, Distinct1);
403 EXPECT_NE(Empty, Distinct2);
404 EXPECT_NE(Distinct1, Distinct2);
407 ASSERT_EQ(Empty, MDNode::get(Context, None));
410 TEST_F(MDNodeTest, isUniqued) {
411 MDNode *U = MDTuple::get(Context, None);
412 MDNode *D = MDTuple::getDistinct(Context, None);
413 auto T = MDTuple::getTemporary(Context, None);
414 EXPECT_TRUE(U->isUniqued());
415 EXPECT_FALSE(D->isUniqued());
416 EXPECT_FALSE(T->isUniqued());
419 TEST_F(MDNodeTest, isDistinct) {
420 MDNode *U = MDTuple::get(Context, None);
421 MDNode *D = MDTuple::getDistinct(Context, None);
422 auto T = MDTuple::getTemporary(Context, None);
423 EXPECT_FALSE(U->isDistinct());
424 EXPECT_TRUE(D->isDistinct());
425 EXPECT_FALSE(T->isDistinct());
428 TEST_F(MDNodeTest, isTemporary) {
429 MDNode *U = MDTuple::get(Context, None);
430 MDNode *D = MDTuple::getDistinct(Context, None);
431 auto T = MDTuple::getTemporary(Context, None);
432 EXPECT_FALSE(U->isTemporary());
433 EXPECT_FALSE(D->isTemporary());
434 EXPECT_TRUE(T->isTemporary());
437 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
439 auto Temp = MDTuple::getTemporary(Context, None);
440 ASSERT_FALSE(Temp->isResolved());
442 // distinct !{temporary !{}}
443 Metadata *Ops[] = {Temp.get()};
444 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
445 EXPECT_TRUE(Distinct->isResolved());
446 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
448 // temporary !{} => !{}
449 MDNode *Empty = MDNode::get(Context, None);
450 Temp->replaceAllUsesWith(Empty);
451 EXPECT_EQ(Empty, Distinct->getOperand(0));
454 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
456 MDNode *N0 = MDNode::get(Context, None);
459 auto Temp3 = MDTuple::getTemporary(Context, None);
460 Metadata *Ops1[] = {Temp3.get(), nullptr};
461 MDNode *N1 = MDNode::get(Context, Ops1);
464 Metadata *Ops2[] = {Temp3.get(), N0};
465 MDNode *N2 = MDNode::get(Context, Ops2);
468 Metadata *Ops3[] = {N2};
469 MDNode *N3 = MDNode::get(Context, Ops3);
470 Temp3->replaceAllUsesWith(N3);
473 Metadata *Ops4[] = {N1};
474 MDNode *N4 = MDNode::get(Context, Ops4);
476 // Confirm that the cycle prevented RAUW from getting dropped.
477 EXPECT_TRUE(N0->isResolved());
478 EXPECT_FALSE(N1->isResolved());
479 EXPECT_FALSE(N2->isResolved());
480 EXPECT_FALSE(N3->isResolved());
481 EXPECT_FALSE(N4->isResolved());
483 // Create a couple of distinct nodes to observe what's going on.
485 // !5 = distinct !{!2}
486 // !6 = distinct !{!3}
487 Metadata *Ops5[] = {N2};
488 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
489 Metadata *Ops6[] = {N3};
490 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
492 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
493 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
494 // references !3, this can cause a re-entry of handleChangedOperand() when !3
495 // is not ready for it.
497 // !2->replaceOperandWith(1, nullptr)
498 // !2: !{!3, !0} => !{!3, null}
499 // !2->replaceAllUsesWith(!1)
500 // !3: !{!2] => !{!1}
501 // !3->replaceAllUsesWith(!4)
502 N2->replaceOperandWith(1, nullptr);
504 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
505 // under us. Just check that the other nodes are sane.
509 // !5 = distinct !{!1}
510 // !6 = distinct !{!4}
511 EXPECT_EQ(N4, N1->getOperand(0));
512 EXPECT_EQ(N1, N4->getOperand(0));
513 EXPECT_EQ(N1, N5->getOperand(0));
514 EXPECT_EQ(N4, N6->getOperand(0));
517 TEST_F(MDNodeTest, replaceResolvedOperand) {
518 // Check code for replacing one resolved operand with another. If doing this
519 // directly (via replaceOperandWith()) becomes illegal, change the operand to
520 // a global value that gets RAUW'ed.
522 // Use a temporary node to keep N from being resolved.
523 auto Temp = MDTuple::getTemporary(Context, None);
524 Metadata *Ops[] = {nullptr, Temp.get()};
526 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
527 MDNode *N = MDTuple::get(Context, Ops);
528 EXPECT_EQ(nullptr, N->getOperand(0));
529 ASSERT_FALSE(N->isResolved());
531 // Check code for replacing resolved nodes.
532 N->replaceOperandWith(0, Empty);
533 EXPECT_EQ(Empty, N->getOperand(0));
535 // Check code for adding another unresolved operand.
536 N->replaceOperandWith(0, Temp.get());
537 EXPECT_EQ(Temp.get(), N->getOperand(0));
539 // Remove the references to Temp; required for teardown.
540 Temp->replaceAllUsesWith(nullptr);
543 TEST_F(MDNodeTest, replaceWithUniqued) {
544 auto *Empty = MDTuple::get(Context, None);
545 MDTuple *FirstUniqued;
547 Metadata *Ops[] = {Empty};
548 auto Temp = MDTuple::getTemporary(Context, Ops);
549 EXPECT_TRUE(Temp->isTemporary());
551 // Don't expect a collision.
552 auto *Current = Temp.get();
553 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
554 EXPECT_TRUE(FirstUniqued->isUniqued());
555 EXPECT_TRUE(FirstUniqued->isResolved());
556 EXPECT_EQ(Current, FirstUniqued);
559 Metadata *Ops[] = {Empty};
560 auto Temp = MDTuple::getTemporary(Context, Ops);
561 EXPECT_TRUE(Temp->isTemporary());
563 // Should collide with Uniqued above this time.
564 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
565 EXPECT_TRUE(Uniqued->isUniqued());
566 EXPECT_TRUE(Uniqued->isResolved());
567 EXPECT_EQ(FirstUniqued, Uniqued);
570 auto Unresolved = MDTuple::getTemporary(Context, None);
571 Metadata *Ops[] = {Unresolved.get()};
572 auto Temp = MDTuple::getTemporary(Context, Ops);
573 EXPECT_TRUE(Temp->isTemporary());
575 // Shouldn't be resolved.
576 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
577 EXPECT_TRUE(Uniqued->isUniqued());
578 EXPECT_FALSE(Uniqued->isResolved());
580 // Should be a different node.
581 EXPECT_NE(FirstUniqued, Uniqued);
583 // Should resolve when we update its node (note: be careful to avoid a
584 // collision with any other nodes above).
585 Uniqued->replaceOperandWith(0, nullptr);
586 EXPECT_TRUE(Uniqued->isResolved());
590 TEST_F(MDNodeTest, replaceWithDistinct) {
592 auto *Empty = MDTuple::get(Context, None);
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 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
600 EXPECT_TRUE(Distinct->isDistinct());
601 EXPECT_TRUE(Distinct->isResolved());
602 EXPECT_EQ(Current, Distinct);
605 auto Unresolved = MDTuple::getTemporary(Context, None);
606 Metadata *Ops[] = {Unresolved.get()};
607 auto Temp = MDTuple::getTemporary(Context, Ops);
608 EXPECT_TRUE(Temp->isTemporary());
610 // Don't expect a collision.
611 auto *Current = Temp.get();
612 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
613 EXPECT_TRUE(Distinct->isDistinct());
614 EXPECT_TRUE(Distinct->isResolved());
615 EXPECT_EQ(Current, Distinct);
617 // Cleanup; required for teardown.
618 Unresolved->replaceAllUsesWith(nullptr);
622 TEST_F(MDNodeTest, replaceWithPermanent) {
623 Metadata *Ops[] = {nullptr};
624 auto Temp = MDTuple::getTemporary(Context, Ops);
625 auto *T = Temp.get();
627 // U is a normal, uniqued node that references T.
628 auto *U = MDTuple::get(Context, T);
629 EXPECT_TRUE(U->isUniqued());
631 // Make Temp self-referencing.
632 Temp->replaceOperandWith(0, T);
634 // Try to uniquify Temp. This should, despite the name in the API, give a
635 // 'distinct' node, since self-references aren't allowed to be uniqued.
637 // Since it's distinct, N should have the same address as when it was a
638 // temporary (i.e., be equal to T not U).
639 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
641 EXPECT_TRUE(N->isDistinct());
643 // U should be the canonical unique node with N as the argument.
644 EXPECT_EQ(U, MDTuple::get(Context, N));
645 EXPECT_TRUE(U->isUniqued());
647 // This temporary should collide with U when replaced, but it should still be
649 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
650 EXPECT_TRUE(U->isUniqued());
652 // This temporary should become a new uniqued node.
653 auto Temp2 = MDTuple::getTemporary(Context, U);
654 auto *V = Temp2.get();
655 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
656 EXPECT_TRUE(V->isUniqued());
657 EXPECT_EQ(U, V->getOperand(0));
660 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
662 EXPECT_EQ(nullptr, Ref.get());
664 auto Temp = MDTuple::getTemporary(Context, None);
665 Ref.reset(Temp.get());
666 EXPECT_EQ(Temp.get(), Ref.get());
668 EXPECT_EQ(nullptr, Ref.get());
671 typedef MetadataTest MDLocationTest;
673 TEST_F(MDLocationTest, Overflow) {
674 MDNode *N = MDNode::get(Context, None);
676 MDLocation *L = MDLocation::get(Context, 2, 7, N);
677 EXPECT_EQ(2u, L->getLine());
678 EXPECT_EQ(7u, L->getColumn());
680 unsigned U16 = 1u << 16;
682 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
683 EXPECT_EQ(UINT32_MAX, L->getLine());
684 EXPECT_EQ(U16 - 1, L->getColumn());
687 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
688 EXPECT_EQ(UINT32_MAX, L->getLine());
689 EXPECT_EQ(0u, L->getColumn());
692 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
693 EXPECT_EQ(UINT32_MAX, L->getLine());
694 EXPECT_EQ(0u, L->getColumn());
698 TEST_F(MDLocationTest, getDistinct) {
699 MDNode *N = MDNode::get(Context, None);
700 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
701 EXPECT_TRUE(L0->isDistinct());
702 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
703 EXPECT_FALSE(L1->isDistinct());
704 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
707 TEST_F(MDLocationTest, getTemporary) {
708 MDNode *N = MDNode::get(Context, None);
709 auto L = MDLocation::getTemporary(Context, 2, 7, N);
710 EXPECT_TRUE(L->isTemporary());
711 EXPECT_FALSE(L->isResolved());
714 typedef MetadataTest GenericDebugNodeTest;
716 TEST_F(GenericDebugNodeTest, get) {
717 StringRef Header = "header";
718 auto *Empty = MDNode::get(Context, None);
719 Metadata *Ops1[] = {Empty};
720 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
721 EXPECT_EQ(15u, N->getTag());
722 EXPECT_EQ(2u, N->getNumOperands());
723 EXPECT_EQ(Header, N->getHeader());
724 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
725 EXPECT_EQ(1u, N->getNumDwarfOperands());
726 EXPECT_EQ(Empty, N->getDwarfOperand(0));
727 EXPECT_EQ(Empty, N->getOperand(1));
728 ASSERT_TRUE(N->isUniqued());
730 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
732 N->replaceOperandWith(1, nullptr);
733 EXPECT_EQ(15u, N->getTag());
734 EXPECT_EQ(Header, N->getHeader());
735 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
736 ASSERT_TRUE(N->isUniqued());
738 Metadata *Ops2[] = {nullptr};
739 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
741 N->replaceDwarfOperandWith(0, Empty);
742 EXPECT_EQ(15u, N->getTag());
743 EXPECT_EQ(Header, N->getHeader());
744 EXPECT_EQ(Empty, N->getDwarfOperand(0));
745 ASSERT_TRUE(N->isUniqued());
746 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
748 TempGenericDebugNode Temp = N->clone();
749 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
752 TEST_F(GenericDebugNodeTest, getEmptyHeader) {
753 // Canonicalize !"" to null.
754 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
755 EXPECT_EQ(StringRef(), N->getHeader());
756 EXPECT_EQ(nullptr, N->getOperand(0));
759 typedef MetadataTest MDSubrangeTest;
761 TEST_F(MDSubrangeTest, get) {
762 auto *N = MDSubrange::get(Context, 5, 7);
763 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
764 EXPECT_EQ(5, N->getCount());
765 EXPECT_EQ(7, N->getLo());
766 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
767 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
769 TempMDSubrange Temp = N->clone();
770 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
773 TEST_F(MDSubrangeTest, getEmptyArray) {
774 auto *N = MDSubrange::get(Context, -1, 0);
775 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
776 EXPECT_EQ(-1, N->getCount());
777 EXPECT_EQ(0, N->getLo());
778 EXPECT_EQ(N, MDSubrange::get(Context, -1, 0));
781 typedef MetadataTest MDEnumeratorTest;
783 TEST_F(MDEnumeratorTest, get) {
784 auto *N = MDEnumerator::get(Context, 7, "name");
785 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
786 EXPECT_EQ(7, N->getValue());
787 EXPECT_EQ("name", N->getName());
788 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
790 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
791 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
793 TempMDEnumerator Temp = N->clone();
794 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
797 typedef MetadataTest MDBasicTypeTest;
799 TEST_F(MDBasicTypeTest, get) {
801 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
802 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
803 EXPECT_EQ("special", N->getName());
804 EXPECT_EQ(33u, N->getSizeInBits());
805 EXPECT_EQ(26u, N->getAlignInBits());
806 EXPECT_EQ(7u, N->getEncoding());
807 EXPECT_EQ(0u, N->getLine());
808 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
811 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
812 "special", 33, 26, 7));
814 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
815 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
817 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
819 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
822 TempMDBasicType Temp = N->clone();
823 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
826 TEST_F(MDBasicTypeTest, getWithLargeValues) {
827 auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
828 UINT64_MAX, UINT64_MAX - 1, 7);
829 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
830 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
833 TEST_F(MDBasicTypeTest, getUnspecified) {
835 MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
836 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
837 EXPECT_EQ("unspecified", N->getName());
838 EXPECT_EQ(0u, N->getSizeInBits());
839 EXPECT_EQ(0u, N->getAlignInBits());
840 EXPECT_EQ(0u, N->getEncoding());
841 EXPECT_EQ(0u, N->getLine());
844 typedef MetadataTest MDTypeTest;
846 TEST_F(MDTypeTest, clone) {
847 // Check that MDType has a specialized clone that returns TempMDType.
848 MDType *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
849 dwarf::DW_ATE_signed);
851 TempMDType Temp = N->clone();
852 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
855 TEST_F(MDTypeTest, setFlags) {
857 Metadata *TypesOps[] = {nullptr};
858 Metadata *Types = MDTuple::get(Context, TypesOps);
860 MDType *D = MDSubroutineType::getDistinct(Context, 0u, Types);
861 EXPECT_EQ(0u, D->getFlags());
862 D->setFlags(DIDescriptor::FlagRValueReference);
863 EXPECT_EQ(DIDescriptor::FlagRValueReference, D->getFlags());
865 EXPECT_EQ(0u, D->getFlags());
867 TempMDType T = MDSubroutineType::getTemporary(Context, 0u, Types);
868 EXPECT_EQ(0u, T->getFlags());
869 T->setFlags(DIDescriptor::FlagRValueReference);
870 EXPECT_EQ(DIDescriptor::FlagRValueReference, T->getFlags());
872 EXPECT_EQ(0u, T->getFlags());
875 typedef MetadataTest MDDerivedTypeTest;
877 TEST_F(MDDerivedTypeTest, get) {
878 Metadata *File = MDTuple::getDistinct(Context, None);
879 Metadata *Scope = MDTuple::getDistinct(Context, None);
880 Metadata *BaseType = MDTuple::getDistinct(Context, None);
881 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
883 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
884 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
885 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
886 EXPECT_EQ("something", N->getName());
887 EXPECT_EQ(File, N->getFile());
888 EXPECT_EQ(1u, N->getLine());
889 EXPECT_EQ(Scope, N->getScope());
890 EXPECT_EQ(BaseType, N->getBaseType());
891 EXPECT_EQ(2u, N->getSizeInBits());
892 EXPECT_EQ(3u, N->getAlignInBits());
893 EXPECT_EQ(4u, N->getOffsetInBits());
894 EXPECT_EQ(5u, N->getFlags());
895 EXPECT_EQ(ExtraData, N->getExtraData());
896 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
897 "something", File, 1, Scope, BaseType, 2, 3,
900 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
901 "something", File, 1, Scope, BaseType, 2, 3,
903 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
904 File, 1, Scope, BaseType, 2, 3, 4, 5,
906 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
907 "something", Scope, 1, Scope, BaseType, 2, 3,
909 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
910 "something", File, 2, Scope, BaseType, 2, 3,
913 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
914 File, 1, File, BaseType, 2, 3, 4, 5, ExtraData));
916 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
917 File, 1, Scope, File, 2, 3, 4, 5, ExtraData));
918 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
919 "something", File, 1, Scope, BaseType, 3, 3,
921 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
922 "something", File, 1, Scope, BaseType, 2, 2,
924 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
925 "something", File, 1, Scope, BaseType, 2, 3,
927 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
928 "something", File, 1, Scope, BaseType, 2, 3,
931 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
932 File, 1, Scope, BaseType, 2, 3, 4, 5, File));
934 TempMDDerivedType Temp = N->clone();
935 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
938 TEST_F(MDDerivedTypeTest, getWithLargeValues) {
939 Metadata *File = MDTuple::getDistinct(Context, None);
940 Metadata *Scope = MDTuple::getDistinct(Context, None);
941 Metadata *BaseType = MDTuple::getDistinct(Context, None);
942 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
944 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
945 File, 1, Scope, BaseType, UINT64_MAX,
946 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
947 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
948 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
949 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
952 typedef MetadataTest MDCompositeTypeTest;
954 TEST_F(MDCompositeTypeTest, get) {
955 unsigned Tag = dwarf::DW_TAG_structure_type;
956 StringRef Name = "some name";
957 Metadata *File = MDTuple::getDistinct(Context, None);
959 Metadata *Scope = MDTuple::getDistinct(Context, None);
960 Metadata *BaseType = MDTuple::getDistinct(Context, None);
961 uint64_t SizeInBits = 2;
962 uint64_t AlignInBits = 3;
963 uint64_t OffsetInBits = 4;
965 Metadata *Elements = MDTuple::getDistinct(Context, None);
966 unsigned RuntimeLang = 6;
967 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
968 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
969 StringRef Identifier = "some id";
971 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
972 BaseType, SizeInBits, AlignInBits,
973 OffsetInBits, Flags, Elements, RuntimeLang,
974 VTableHolder, TemplateParams, Identifier);
975 EXPECT_EQ(Tag, N->getTag());
976 EXPECT_EQ(Name, N->getName());
977 EXPECT_EQ(File, N->getFile());
978 EXPECT_EQ(Line, N->getLine());
979 EXPECT_EQ(Scope, N->getScope());
980 EXPECT_EQ(BaseType, N->getBaseType());
981 EXPECT_EQ(SizeInBits, N->getSizeInBits());
982 EXPECT_EQ(AlignInBits, N->getAlignInBits());
983 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
984 EXPECT_EQ(Flags, N->getFlags());
985 EXPECT_EQ(Elements, N->getElements());
986 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
987 EXPECT_EQ(VTableHolder, N->getVTableHolder());
988 EXPECT_EQ(TemplateParams, N->getTemplateParams());
989 EXPECT_EQ(Identifier, N->getIdentifier());
991 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
992 BaseType, SizeInBits, AlignInBits,
993 OffsetInBits, Flags, Elements, RuntimeLang,
994 VTableHolder, TemplateParams, Identifier));
996 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
997 BaseType, SizeInBits, AlignInBits,
998 OffsetInBits, Flags, Elements, RuntimeLang,
999 VTableHolder, TemplateParams, Identifier));
1000 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
1001 BaseType, SizeInBits, AlignInBits,
1002 OffsetInBits, Flags, Elements, RuntimeLang,
1003 VTableHolder, TemplateParams, Identifier));
1004 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, Scope, Line, Scope,
1005 BaseType, SizeInBits, AlignInBits,
1006 OffsetInBits, Flags, Elements, RuntimeLang,
1007 VTableHolder, TemplateParams, Identifier));
1008 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
1009 BaseType, SizeInBits, AlignInBits,
1010 OffsetInBits, Flags, Elements, RuntimeLang,
1011 VTableHolder, TemplateParams, Identifier));
1012 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, File,
1013 BaseType, SizeInBits, AlignInBits,
1014 OffsetInBits, Flags, Elements, RuntimeLang,
1015 VTableHolder, TemplateParams, Identifier));
1016 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
1017 SizeInBits, AlignInBits, OffsetInBits,
1018 Flags, Elements, RuntimeLang, VTableHolder,
1019 TemplateParams, Identifier));
1020 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1021 BaseType, SizeInBits + 1, AlignInBits,
1022 OffsetInBits, Flags, Elements, RuntimeLang,
1023 VTableHolder, TemplateParams, Identifier));
1024 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1025 BaseType, SizeInBits, AlignInBits + 1,
1026 OffsetInBits, Flags, Elements, RuntimeLang,
1027 VTableHolder, TemplateParams, Identifier));
1028 EXPECT_NE(N, MDCompositeType::get(
1029 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1030 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1031 VTableHolder, TemplateParams, Identifier));
1032 EXPECT_NE(N, MDCompositeType::get(
1033 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1034 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1035 VTableHolder, TemplateParams, Identifier));
1036 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1037 BaseType, SizeInBits, AlignInBits,
1038 OffsetInBits, Flags, File, RuntimeLang,
1039 VTableHolder, TemplateParams, Identifier));
1040 EXPECT_NE(N, MDCompositeType::get(
1041 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1042 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1043 VTableHolder, TemplateParams, Identifier));
1044 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1045 BaseType, SizeInBits, AlignInBits,
1046 OffsetInBits, Flags, Elements, RuntimeLang,
1047 File, TemplateParams, Identifier));
1048 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1049 BaseType, SizeInBits, AlignInBits,
1050 OffsetInBits, Flags, Elements, RuntimeLang,
1051 VTableHolder, File, Identifier));
1052 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1053 BaseType, SizeInBits, AlignInBits,
1054 OffsetInBits, Flags, Elements, RuntimeLang,
1055 VTableHolder, TemplateParams, "other"));
1057 // Be sure that missing identifiers get null pointers.
1058 EXPECT_FALSE(MDCompositeType::get(
1059 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1060 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1061 VTableHolder, TemplateParams, "")->getRawIdentifier());
1062 EXPECT_FALSE(MDCompositeType::get(
1063 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1064 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1065 VTableHolder, TemplateParams)->getRawIdentifier());
1067 TempMDCompositeType Temp = N->clone();
1068 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1071 TEST_F(MDCompositeTypeTest, getWithLargeValues) {
1072 unsigned Tag = dwarf::DW_TAG_structure_type;
1073 StringRef Name = "some name";
1074 Metadata *File = MDTuple::getDistinct(Context, None);
1076 Metadata *Scope = MDTuple::getDistinct(Context, None);
1077 Metadata *BaseType = MDTuple::getDistinct(Context, None);
1078 uint64_t SizeInBits = UINT64_MAX;
1079 uint64_t AlignInBits = UINT64_MAX - 1;
1080 uint64_t OffsetInBits = UINT64_MAX - 2;
1082 Metadata *Elements = MDTuple::getDistinct(Context, None);
1083 unsigned RuntimeLang = 6;
1084 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
1085 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1086 StringRef Identifier = "some id";
1088 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1089 BaseType, SizeInBits, AlignInBits,
1090 OffsetInBits, Flags, Elements, RuntimeLang,
1091 VTableHolder, TemplateParams, Identifier);
1092 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1093 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1094 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1097 TEST_F(MDCompositeTypeTest, replaceOperands) {
1098 unsigned Tag = dwarf::DW_TAG_structure_type;
1099 StringRef Name = "some name";
1100 Metadata *File = MDTuple::getDistinct(Context, None);
1102 Metadata *Scope = MDTuple::getDistinct(Context, None);
1103 Metadata *BaseType = MDTuple::getDistinct(Context, None);
1104 uint64_t SizeInBits = 2;
1105 uint64_t AlignInBits = 3;
1106 uint64_t OffsetInBits = 4;
1108 unsigned RuntimeLang = 6;
1109 StringRef Identifier = "some id";
1111 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1112 BaseType, SizeInBits, AlignInBits,
1113 OffsetInBits, Flags, nullptr, RuntimeLang,
1114 nullptr, nullptr, Identifier);
1116 auto *Elements = MDTuple::getDistinct(Context, None);
1117 EXPECT_EQ(nullptr, N->getElements());
1118 N->replaceElements(Elements);
1119 EXPECT_EQ(Elements, N->getElements());
1120 N->replaceElements(nullptr);
1121 EXPECT_EQ(nullptr, N->getElements());
1123 auto *VTableHolder = MDTuple::getDistinct(Context, None);
1124 EXPECT_EQ(nullptr, N->getVTableHolder());
1125 N->replaceVTableHolder(VTableHolder);
1126 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1127 N->replaceVTableHolder(nullptr);
1128 EXPECT_EQ(nullptr, N->getVTableHolder());
1130 auto *TemplateParams = MDTuple::getDistinct(Context, None);
1131 EXPECT_EQ(nullptr, N->getTemplateParams());
1132 N->replaceTemplateParams(TemplateParams);
1133 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1134 N->replaceTemplateParams(nullptr);
1135 EXPECT_EQ(nullptr, N->getTemplateParams());
1138 typedef MetadataTest MDSubroutineTypeTest;
1140 TEST_F(MDSubroutineTypeTest, get) {
1142 Metadata *TypeArray = MDTuple::getDistinct(Context, None);
1144 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
1145 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1146 EXPECT_EQ(Flags, N->getFlags());
1147 EXPECT_EQ(TypeArray, N->getTypeArray());
1148 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
1150 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
1151 EXPECT_NE(N, MDSubroutineType::get(Context, Flags,
1152 MDTuple::getDistinct(Context, None)));
1154 TempMDSubroutineType Temp = N->clone();
1155 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1157 // Test always-empty operands.
1158 EXPECT_EQ(nullptr, N->getScope());
1159 EXPECT_EQ(nullptr, N->getFile());
1160 EXPECT_EQ("", N->getName());
1161 EXPECT_EQ(nullptr, N->getBaseType());
1162 EXPECT_EQ(nullptr, N->getVTableHolder());
1163 EXPECT_EQ(nullptr, N->getTemplateParams());
1164 EXPECT_EQ("", N->getIdentifier());
1167 typedef MetadataTest MDFileTest;
1169 TEST_F(MDFileTest, get) {
1170 StringRef Filename = "file";
1171 StringRef Directory = "dir";
1172 auto *N = MDFile::get(Context, Filename, Directory);
1174 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1175 EXPECT_EQ(Filename, N->getFilename());
1176 EXPECT_EQ(Directory, N->getDirectory());
1177 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
1179 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1180 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
1182 TempMDFile Temp = N->clone();
1183 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1186 TEST_F(MDFileTest, ScopeGetFile) {
1187 // Ensure that MDScope::getFile() returns itself.
1188 MDScope *N = MDFile::get(Context, "file", "dir");
1189 EXPECT_EQ(N, N->getFile());
1192 typedef MetadataTest MDCompileUnitTest;
1194 TEST_F(MDCompileUnitTest, get) {
1195 unsigned SourceLanguage = 1;
1196 Metadata *File = MDTuple::getDistinct(Context, None);
1197 StringRef Producer = "some producer";
1198 bool IsOptimized = false;
1199 StringRef Flags = "flag after flag";
1200 unsigned RuntimeVersion = 2;
1201 StringRef SplitDebugFilename = "another/file";
1202 unsigned EmissionKind = 3;
1203 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1204 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1205 Metadata *Subprograms = MDTuple::getDistinct(Context, None);
1206 Metadata *GlobalVariables = MDTuple::getDistinct(Context, None);
1207 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1208 auto *N = MDCompileUnit::get(
1209 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1210 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1211 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1213 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1214 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1215 EXPECT_EQ(File, N->getFile());
1216 EXPECT_EQ(Producer, N->getProducer());
1217 EXPECT_EQ(IsOptimized, N->isOptimized());
1218 EXPECT_EQ(Flags, N->getFlags());
1219 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1220 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1221 EXPECT_EQ(EmissionKind, N->getEmissionKind());
1222 EXPECT_EQ(EnumTypes, N->getEnumTypes());
1223 EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
1224 EXPECT_EQ(Subprograms, N->getSubprograms());
1225 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1226 EXPECT_EQ(ImportedEntities, N->getImportedEntities());
1227 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1228 IsOptimized, Flags, RuntimeVersion,
1229 SplitDebugFilename, EmissionKind, EnumTypes,
1230 RetainedTypes, Subprograms, GlobalVariables,
1233 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1234 IsOptimized, Flags, RuntimeVersion,
1235 SplitDebugFilename, EmissionKind, EnumTypes,
1236 RetainedTypes, Subprograms, GlobalVariables,
1238 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer,
1239 IsOptimized, Flags, RuntimeVersion,
1240 SplitDebugFilename, EmissionKind, EnumTypes,
1241 RetainedTypes, Subprograms, GlobalVariables,
1243 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1244 IsOptimized, Flags, RuntimeVersion,
1245 SplitDebugFilename, EmissionKind, EnumTypes,
1246 RetainedTypes, Subprograms, GlobalVariables,
1248 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1249 !IsOptimized, Flags, RuntimeVersion,
1250 SplitDebugFilename, EmissionKind, EnumTypes,
1251 RetainedTypes, Subprograms, GlobalVariables,
1253 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1254 IsOptimized, "other", RuntimeVersion,
1255 SplitDebugFilename, EmissionKind, EnumTypes,
1256 RetainedTypes, Subprograms, GlobalVariables,
1258 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1259 IsOptimized, Flags, RuntimeVersion + 1,
1260 SplitDebugFilename, EmissionKind, EnumTypes,
1261 RetainedTypes, Subprograms, GlobalVariables,
1264 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1265 IsOptimized, Flags, RuntimeVersion, "other",
1266 EmissionKind, EnumTypes, RetainedTypes,
1267 Subprograms, GlobalVariables, ImportedEntities));
1268 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1269 IsOptimized, Flags, RuntimeVersion,
1270 SplitDebugFilename, EmissionKind + 1,
1271 EnumTypes, RetainedTypes, Subprograms,
1272 GlobalVariables, ImportedEntities));
1273 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1274 IsOptimized, Flags, RuntimeVersion,
1275 SplitDebugFilename, EmissionKind, File,
1276 RetainedTypes, Subprograms, GlobalVariables,
1278 EXPECT_NE(N, MDCompileUnit::get(
1279 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1280 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1281 File, Subprograms, GlobalVariables, ImportedEntities));
1282 EXPECT_NE(N, MDCompileUnit::get(
1283 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1284 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1285 RetainedTypes, File, GlobalVariables, ImportedEntities));
1286 EXPECT_NE(N, MDCompileUnit::get(
1287 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1288 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1289 RetainedTypes, Subprograms, File, ImportedEntities));
1290 EXPECT_NE(N, MDCompileUnit::get(
1291 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1292 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1293 RetainedTypes, Subprograms, GlobalVariables, File));
1295 TempMDCompileUnit Temp = N->clone();
1296 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1299 TEST_F(MDCompileUnitTest, replaceArrays) {
1300 unsigned SourceLanguage = 1;
1301 Metadata *File = MDTuple::getDistinct(Context, None);
1302 StringRef Producer = "some producer";
1303 bool IsOptimized = false;
1304 StringRef Flags = "flag after flag";
1305 unsigned RuntimeVersion = 2;
1306 StringRef SplitDebugFilename = "another/file";
1307 unsigned EmissionKind = 3;
1308 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1309 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1310 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1311 auto *N = MDCompileUnit::get(
1312 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1313 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1314 RetainedTypes, nullptr, nullptr, ImportedEntities);
1316 auto *Subprograms = MDTuple::getDistinct(Context, None);
1317 EXPECT_EQ(nullptr, N->getSubprograms());
1318 N->replaceSubprograms(Subprograms);
1319 EXPECT_EQ(Subprograms, N->getSubprograms());
1320 N->replaceSubprograms(nullptr);
1321 EXPECT_EQ(nullptr, N->getSubprograms());
1323 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1324 EXPECT_EQ(nullptr, N->getGlobalVariables());
1325 N->replaceGlobalVariables(GlobalVariables);
1326 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1327 N->replaceGlobalVariables(nullptr);
1328 EXPECT_EQ(nullptr, N->getGlobalVariables());
1331 typedef MetadataTest MDSubprogramTest;
1333 TEST_F(MDSubprogramTest, get) {
1334 Metadata *Scope = MDTuple::getDistinct(Context, None);
1335 StringRef Name = "name";
1336 StringRef LinkageName = "linkage";
1337 Metadata *File = MDTuple::getDistinct(Context, None);
1339 Metadata *Type = MDTuple::getDistinct(Context, None);
1340 bool IsLocalToUnit = false;
1341 bool IsDefinition = true;
1342 unsigned ScopeLine = 3;
1343 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1344 unsigned Virtuality = 4;
1345 unsigned VirtualIndex = 5;
1347 bool IsOptimized = false;
1348 Metadata *Function = MDTuple::getDistinct(Context, None);
1349 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1350 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1351 Metadata *Variables = MDTuple::getDistinct(Context, None);
1353 auto *N = MDSubprogram::get(
1354 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1355 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1356 IsOptimized, Function, TemplateParams, Declaration, Variables);
1358 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1359 EXPECT_EQ(Scope, N->getScope());
1360 EXPECT_EQ(Name, N->getName());
1361 EXPECT_EQ(LinkageName, N->getLinkageName());
1362 EXPECT_EQ(File, N->getFile());
1363 EXPECT_EQ(Line, N->getLine());
1364 EXPECT_EQ(Type, N->getType());
1365 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1366 EXPECT_EQ(IsDefinition, N->isDefinition());
1367 EXPECT_EQ(ScopeLine, N->getScopeLine());
1368 EXPECT_EQ(ContainingType, N->getContainingType());
1369 EXPECT_EQ(Virtuality, N->getVirtuality());
1370 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1371 EXPECT_EQ(Flags, N->getFlags());
1372 EXPECT_EQ(IsOptimized, N->isOptimized());
1373 EXPECT_EQ(Function, N->getFunction());
1374 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1375 EXPECT_EQ(Declaration, N->getDeclaration());
1376 EXPECT_EQ(Variables, N->getVariables());
1377 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1378 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1379 ContainingType, Virtuality, VirtualIndex,
1380 Flags, IsOptimized, Function, TemplateParams,
1381 Declaration, Variables));
1383 EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1384 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1385 ContainingType, Virtuality, VirtualIndex,
1386 Flags, IsOptimized, Function, TemplateParams,
1387 Declaration, Variables));
1388 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1389 Line, Type, IsLocalToUnit, IsDefinition,
1390 ScopeLine, ContainingType, Virtuality,
1391 VirtualIndex, Flags, IsOptimized, Function,
1392 TemplateParams, Declaration, Variables));
1393 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1394 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1395 ContainingType, Virtuality, VirtualIndex,
1396 Flags, IsOptimized, Function, TemplateParams,
1397 Declaration, Variables));
1398 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1399 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1400 ContainingType, Virtuality, VirtualIndex,
1401 Flags, IsOptimized, Function, TemplateParams,
1402 Declaration, Variables));
1403 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1404 Line + 1, Type, IsLocalToUnit, IsDefinition,
1405 ScopeLine, ContainingType, Virtuality,
1406 VirtualIndex, Flags, IsOptimized, Function,
1407 TemplateParams, Declaration, Variables));
1408 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1409 Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1410 ContainingType, Virtuality, VirtualIndex,
1411 Flags, IsOptimized, Function, TemplateParams,
1412 Declaration, Variables));
1413 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1414 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1415 ContainingType, Virtuality, VirtualIndex,
1416 Flags, IsOptimized, Function, TemplateParams,
1417 Declaration, Variables));
1418 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1419 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1420 ContainingType, Virtuality, VirtualIndex,
1421 Flags, IsOptimized, Function, TemplateParams,
1422 Declaration, Variables));
1423 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1424 Type, IsLocalToUnit, IsDefinition,
1425 ScopeLine + 1, ContainingType, Virtuality,
1426 VirtualIndex, Flags, IsOptimized, Function,
1427 TemplateParams, Declaration, Variables));
1428 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1429 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1430 Type, Virtuality, VirtualIndex, Flags,
1431 IsOptimized, Function, TemplateParams,
1432 Declaration, Variables));
1433 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1434 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1435 ContainingType, Virtuality + 1, VirtualIndex,
1436 Flags, IsOptimized, Function, TemplateParams,
1437 Declaration, Variables));
1438 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1439 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1440 ContainingType, Virtuality, VirtualIndex + 1,
1441 Flags, IsOptimized, Function, TemplateParams,
1442 Declaration, Variables));
1443 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1444 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1445 ContainingType, Virtuality, VirtualIndex,
1446 ~Flags, IsOptimized, Function, TemplateParams,
1447 Declaration, Variables));
1448 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1449 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1450 ContainingType, Virtuality, VirtualIndex,
1451 Flags, !IsOptimized, Function, TemplateParams,
1452 Declaration, Variables));
1453 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1454 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1455 ContainingType, Virtuality, VirtualIndex,
1456 Flags, IsOptimized, Type, TemplateParams,
1457 Declaration, Variables));
1458 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1459 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1460 ContainingType, Virtuality, VirtualIndex,
1461 Flags, IsOptimized, Function, Type,
1462 Declaration, Variables));
1463 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1464 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1465 ContainingType, Virtuality, VirtualIndex,
1466 Flags, IsOptimized, Function, TemplateParams,
1468 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1469 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1470 ContainingType, Virtuality, VirtualIndex,
1471 Flags, IsOptimized, Function, TemplateParams,
1472 Declaration, Type));
1474 TempMDSubprogram Temp = N->clone();
1475 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1478 TEST_F(MDSubprogramTest, replaceFunction) {
1479 Metadata *Scope = MDTuple::getDistinct(Context, None);
1480 StringRef Name = "name";
1481 StringRef LinkageName = "linkage";
1482 Metadata *File = MDTuple::getDistinct(Context, None);
1484 Metadata *Type = MDTuple::getDistinct(Context, None);
1485 bool IsLocalToUnit = false;
1486 bool IsDefinition = true;
1487 unsigned ScopeLine = 3;
1488 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1489 unsigned Virtuality = 4;
1490 unsigned VirtualIndex = 5;
1492 bool IsOptimized = false;
1493 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1494 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1495 Metadata *Variables = MDTuple::getDistinct(Context, None);
1497 auto *N = MDSubprogram::get(
1498 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1499 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1500 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1502 EXPECT_EQ(nullptr, N->getFunction());
1504 std::unique_ptr<Function> F(
1505 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1506 GlobalValue::ExternalLinkage));
1507 N->replaceFunction(F.get());
1508 EXPECT_EQ(ConstantAsMetadata::get(F.get()), N->getFunction());
1510 N->replaceFunction(nullptr);
1511 EXPECT_EQ(nullptr, N->getFunction());
1514 typedef MetadataTest MDLexicalBlockTest;
1516 TEST_F(MDLexicalBlockTest, get) {
1517 Metadata *Scope = MDTuple::getDistinct(Context, None);
1518 Metadata *File = MDTuple::getDistinct(Context, None);
1520 unsigned Column = 8;
1522 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1524 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1525 EXPECT_EQ(Scope, N->getScope());
1526 EXPECT_EQ(File, N->getFile());
1527 EXPECT_EQ(Line, N->getLine());
1528 EXPECT_EQ(Column, N->getColumn());
1529 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1531 EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1532 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1533 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1534 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
1536 TempMDLexicalBlock Temp = N->clone();
1537 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1540 typedef MetadataTest MDLexicalBlockFileTest;
1542 TEST_F(MDLexicalBlockFileTest, get) {
1543 Metadata *Scope = MDTuple::getDistinct(Context, None);
1544 Metadata *File = MDTuple::getDistinct(Context, None);
1545 unsigned Discriminator = 5;
1547 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1549 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1550 EXPECT_EQ(Scope, N->getScope());
1551 EXPECT_EQ(File, N->getFile());
1552 EXPECT_EQ(Discriminator, N->getDiscriminator());
1553 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1555 EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1556 EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1558 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1560 TempMDLexicalBlockFile Temp = N->clone();
1561 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1564 typedef MetadataTest MDNamespaceTest;
1566 TEST_F(MDNamespaceTest, get) {
1567 Metadata *Scope = MDTuple::getDistinct(Context, None);
1568 Metadata *File = MDTuple::getDistinct(Context, None);
1569 StringRef Name = "namespace";
1572 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1574 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1575 EXPECT_EQ(Scope, N->getScope());
1576 EXPECT_EQ(File, N->getFile());
1577 EXPECT_EQ(Name, N->getName());
1578 EXPECT_EQ(Line, N->getLine());
1579 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1581 EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1582 EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1583 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1584 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
1586 TempMDNamespace Temp = N->clone();
1587 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1590 typedef MetadataTest MDTemplateTypeParameterTest;
1592 TEST_F(MDTemplateTypeParameterTest, get) {
1593 StringRef Name = "template";
1594 Metadata *Type = MDTuple::getDistinct(Context, None);
1595 Metadata *Other = MDTuple::getDistinct(Context, None);
1597 auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
1599 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1600 EXPECT_EQ(Name, N->getName());
1601 EXPECT_EQ(Type, N->getType());
1602 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type));
1604 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
1605 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Name, Other));
1607 TempMDTemplateTypeParameter Temp = N->clone();
1608 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1611 typedef MetadataTest MDTemplateValueParameterTest;
1613 TEST_F(MDTemplateValueParameterTest, get) {
1614 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1615 StringRef Name = "template";
1616 Metadata *Type = MDTuple::getDistinct(Context, None);
1617 Metadata *Value = MDTuple::getDistinct(Context, None);
1618 Metadata *Other = MDTuple::getDistinct(Context, None);
1620 auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
1621 EXPECT_EQ(Tag, N->getTag());
1622 EXPECT_EQ(Name, N->getName());
1623 EXPECT_EQ(Type, N->getType());
1624 EXPECT_EQ(Value, N->getValue());
1625 EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value));
1627 EXPECT_NE(N, MDTemplateValueParameter::get(
1628 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1630 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type,
1632 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Other,
1634 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Other));
1636 TempMDTemplateValueParameter Temp = N->clone();
1637 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1640 typedef MetadataTest MDGlobalVariableTest;
1642 TEST_F(MDGlobalVariableTest, get) {
1643 Metadata *Scope = MDTuple::getDistinct(Context, None);
1644 StringRef Name = "name";
1645 StringRef LinkageName = "linkage";
1646 Metadata *File = MDTuple::getDistinct(Context, None);
1648 Metadata *Type = MDTuple::getDistinct(Context, None);
1649 bool IsLocalToUnit = false;
1650 bool IsDefinition = true;
1651 Metadata *Variable = MDTuple::getDistinct(Context, None);
1652 Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None);
1654 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1655 Type, IsLocalToUnit, IsDefinition, Variable,
1656 StaticDataMemberDeclaration);
1657 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1658 EXPECT_EQ(Scope, N->getScope());
1659 EXPECT_EQ(Name, N->getName());
1660 EXPECT_EQ(LinkageName, N->getLinkageName());
1661 EXPECT_EQ(File, N->getFile());
1662 EXPECT_EQ(Line, N->getLine());
1663 EXPECT_EQ(Type, N->getType());
1664 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1665 EXPECT_EQ(IsDefinition, N->isDefinition());
1666 EXPECT_EQ(Variable, N->getVariable());
1667 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1668 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1669 Line, Type, IsLocalToUnit, IsDefinition,
1670 Variable, StaticDataMemberDeclaration));
1672 EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File,
1673 Line, Type, IsLocalToUnit, IsDefinition,
1674 Variable, StaticDataMemberDeclaration));
1675 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1676 Line, Type, IsLocalToUnit, IsDefinition,
1677 Variable, StaticDataMemberDeclaration));
1678 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1679 Type, IsLocalToUnit, IsDefinition,
1680 Variable, StaticDataMemberDeclaration));
1681 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope,
1682 Line, Type, IsLocalToUnit, IsDefinition,
1683 Variable, StaticDataMemberDeclaration));
1685 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1686 Line + 1, Type, IsLocalToUnit, IsDefinition,
1687 Variable, StaticDataMemberDeclaration));
1688 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1689 Line, Scope, IsLocalToUnit, IsDefinition,
1690 Variable, StaticDataMemberDeclaration));
1691 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1692 Line, Type, !IsLocalToUnit, IsDefinition,
1693 Variable, StaticDataMemberDeclaration));
1694 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1695 Line, Type, IsLocalToUnit, !IsDefinition,
1696 Variable, StaticDataMemberDeclaration));
1697 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1698 Line, Type, IsLocalToUnit, IsDefinition,
1699 Type, StaticDataMemberDeclaration));
1700 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1701 Line, Type, IsLocalToUnit, IsDefinition,
1704 TempMDGlobalVariable Temp = N->clone();
1705 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1708 typedef MetadataTest MDLocalVariableTest;
1710 TEST_F(MDLocalVariableTest, get) {
1711 unsigned Tag = dwarf::DW_TAG_arg_variable;
1712 Metadata *Scope = MDTuple::getDistinct(Context, None);
1713 StringRef Name = "name";
1714 Metadata *File = MDTuple::getDistinct(Context, None);
1716 Metadata *Type = MDTuple::getDistinct(Context, None);
1719 Metadata *InlinedAtScope = MDTuple::getDistinct(Context, None);
1720 Metadata *InlinedAt =
1721 MDLocation::getDistinct(Context, 10, 20, InlinedAtScope);
1723 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1724 Arg, Flags, InlinedAt);
1725 EXPECT_EQ(Tag, N->getTag());
1726 EXPECT_EQ(Scope, N->getScope());
1727 EXPECT_EQ(Name, N->getName());
1728 EXPECT_EQ(File, N->getFile());
1729 EXPECT_EQ(Line, N->getLine());
1730 EXPECT_EQ(Type, N->getType());
1731 EXPECT_EQ(Arg, N->getArg());
1732 EXPECT_EQ(Flags, N->getFlags());
1733 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1734 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1735 Arg, Flags, InlinedAt));
1737 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1738 Name, File, Line, Type, Arg, Flags,
1740 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line,
1741 Type, Arg, Flags, InlinedAt));
1742 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1743 Type, Arg, Flags, InlinedAt));
1744 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line,
1745 Type, Arg, Flags, InlinedAt));
1746 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1747 Type, Arg, Flags, InlinedAt));
1748 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1749 Scope, Arg, Flags, InlinedAt));
1750 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1751 Arg + 1, Flags, InlinedAt));
1752 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1753 Arg, ~Flags, InlinedAt));
1754 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1755 Arg, Flags, Scope));
1757 TempMDLocalVariable Temp = N->clone();
1758 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1760 auto *Inlined = N->withoutInline();
1761 EXPECT_NE(N, Inlined);
1762 EXPECT_EQ(N->getTag(), Inlined->getTag());
1763 EXPECT_EQ(N->getScope(), Inlined->getScope());
1764 EXPECT_EQ(N->getName(), Inlined->getName());
1765 EXPECT_EQ(N->getFile(), Inlined->getFile());
1766 EXPECT_EQ(N->getLine(), Inlined->getLine());
1767 EXPECT_EQ(N->getType(), Inlined->getType());
1768 EXPECT_EQ(N->getArg(), Inlined->getArg());
1769 EXPECT_EQ(N->getFlags(), Inlined->getFlags());
1770 EXPECT_EQ(nullptr, Inlined->getInlinedAt());
1771 EXPECT_EQ(N, Inlined->withInline(cast<MDLocation>(InlinedAt)));
1774 typedef MetadataTest MDExpressionTest;
1776 TEST_F(MDExpressionTest, get) {
1777 uint64_t Elements[] = {2, 6, 9, 78, 0};
1778 auto *N = MDExpression::get(Context, Elements);
1779 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1780 EXPECT_EQ(N, MDExpression::get(Context, Elements));
1782 EXPECT_EQ(5u, N->getNumElements());
1783 EXPECT_EQ(2u, N->getElement(0));
1784 EXPECT_EQ(6u, N->getElement(1));
1785 EXPECT_EQ(9u, N->getElement(2));
1786 EXPECT_EQ(78u, N->getElement(3));
1787 EXPECT_EQ(0u, N->getElement(4));
1789 TempMDExpression Temp = N->clone();
1790 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1793 TEST_F(MDExpressionTest, isValid) {
1794 #define EXPECT_VALID(...) \
1796 uint64_t Elements[] = {__VA_ARGS__}; \
1797 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1799 #define EXPECT_INVALID(...) \
1801 uint64_t Elements[] = {__VA_ARGS__}; \
1802 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1805 // Empty expression should be valid.
1806 EXPECT_TRUE(MDExpression::get(Context, None));
1808 // Valid constructions.
1809 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1810 EXPECT_VALID(dwarf::DW_OP_deref);
1811 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1812 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1813 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1814 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1815 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1817 // Invalid constructions.
1818 EXPECT_INVALID(~0u);
1819 EXPECT_INVALID(dwarf::DW_OP_plus);
1820 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1821 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1822 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1823 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1826 #undef EXPECT_INVALID
1829 typedef MetadataTest MDObjCPropertyTest;
1831 TEST_F(MDObjCPropertyTest, get) {
1832 StringRef Name = "name";
1833 Metadata *File = MDTuple::getDistinct(Context, None);
1835 StringRef GetterName = "getter";
1836 StringRef SetterName = "setter";
1837 unsigned Attributes = 7;
1838 Metadata *Type = MDTuple::getDistinct(Context, None);
1840 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1841 SetterName, Attributes, Type);
1843 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1844 EXPECT_EQ(Name, N->getName());
1845 EXPECT_EQ(File, N->getFile());
1846 EXPECT_EQ(Line, N->getLine());
1847 EXPECT_EQ(GetterName, N->getGetterName());
1848 EXPECT_EQ(SetterName, N->getSetterName());
1849 EXPECT_EQ(Attributes, N->getAttributes());
1850 EXPECT_EQ(Type, N->getType());
1851 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1852 SetterName, Attributes, Type));
1854 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1855 SetterName, Attributes, Type));
1856 EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1857 SetterName, Attributes, Type));
1858 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1859 SetterName, Attributes, Type));
1860 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1861 SetterName, Attributes, Type));
1862 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1863 "other", Attributes, Type));
1864 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1865 SetterName, Attributes + 1, Type));
1866 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1867 SetterName, Attributes, File));
1869 TempMDObjCProperty Temp = N->clone();
1870 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1873 typedef MetadataTest MDImportedEntityTest;
1875 TEST_F(MDImportedEntityTest, get) {
1876 unsigned Tag = dwarf::DW_TAG_imported_module;
1877 Metadata *Scope = MDTuple::getDistinct(Context, None);
1878 Metadata *Entity = MDTuple::getDistinct(Context, None);
1880 StringRef Name = "name";
1882 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1884 EXPECT_EQ(Tag, N->getTag());
1885 EXPECT_EQ(Scope, N->getScope());
1886 EXPECT_EQ(Entity, N->getEntity());
1887 EXPECT_EQ(Line, N->getLine());
1888 EXPECT_EQ(Name, N->getName());
1889 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1892 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1893 Scope, Entity, Line, Name));
1894 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1895 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1897 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1899 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
1901 TempMDImportedEntity Temp = N->clone();
1902 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1905 typedef MetadataTest MetadataAsValueTest;
1907 TEST_F(MetadataAsValueTest, MDNode) {
1908 MDNode *N = MDNode::get(Context, None);
1909 auto *V = MetadataAsValue::get(Context, N);
1910 EXPECT_TRUE(V->getType()->isMetadataTy());
1911 EXPECT_EQ(N, V->getMetadata());
1913 auto *V2 = MetadataAsValue::get(Context, N);
1917 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1918 MDNode *N = MDNode::get(Context, None);
1919 Metadata *Ops[] = {N};
1920 MDNode *N2 = MDNode::get(Context, Ops);
1921 auto *V = MetadataAsValue::get(Context, N2);
1922 EXPECT_TRUE(V->getType()->isMetadataTy());
1923 EXPECT_EQ(N2, V->getMetadata());
1925 auto *V2 = MetadataAsValue::get(Context, N2);
1928 auto *V3 = MetadataAsValue::get(Context, N);
1929 EXPECT_TRUE(V3->getType()->isMetadataTy());
1931 EXPECT_EQ(N, V3->getMetadata());
1934 TEST_F(MetadataAsValueTest, MDNodeConstant) {
1935 auto *C = ConstantInt::getTrue(Context);
1936 auto *MD = ConstantAsMetadata::get(C);
1937 Metadata *Ops[] = {MD};
1938 auto *N = MDNode::get(Context, Ops);
1940 auto *V = MetadataAsValue::get(Context, MD);
1941 EXPECT_TRUE(V->getType()->isMetadataTy());
1942 EXPECT_EQ(MD, V->getMetadata());
1944 auto *V2 = MetadataAsValue::get(Context, N);
1945 EXPECT_EQ(MD, V2->getMetadata());
1949 typedef MetadataTest ValueAsMetadataTest;
1951 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1952 Type *Ty = Type::getInt1PtrTy(Context);
1953 std::unique_ptr<GlobalVariable> GV0(
1954 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1955 auto *MD = ValueAsMetadata::get(GV0.get());
1956 EXPECT_TRUE(MD->getValue() == GV0.get());
1957 ASSERT_TRUE(GV0->use_empty());
1959 std::unique_ptr<GlobalVariable> GV1(
1960 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1961 GV0->replaceAllUsesWith(GV1.get());
1962 EXPECT_TRUE(MD->getValue() == GV1.get());
1965 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
1966 // Create a constant.
1967 ConstantAsMetadata *CI = ConstantAsMetadata::get(
1968 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
1970 // Create a temporary to prevent nodes from resolving.
1971 auto Temp = MDTuple::getTemporary(Context, None);
1973 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
1974 Metadata *Ops1[] = {CI, CI, Temp.get()};
1975 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
1977 auto *N1 = MDTuple::get(Context, Ops1);
1978 auto *N2 = MDTuple::get(Context, Ops2);
1981 // Tell metadata that the constant is getting deleted.
1983 // After this, N1 will be invalid, so don't touch it.
1984 ValueAsMetadata::handleDeletion(CI->getValue());
1985 EXPECT_EQ(nullptr, N2->getOperand(0));
1986 EXPECT_EQ(nullptr, N2->getOperand(1));
1987 EXPECT_EQ(Temp.get(), N2->getOperand(2));
1989 // Clean up Temp for teardown.
1990 Temp->replaceAllUsesWith(nullptr);
1993 typedef MetadataTest TrackingMDRefTest;
1995 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
1996 Type *Ty = Type::getInt1PtrTy(Context);
1997 std::unique_ptr<GlobalVariable> GV0(
1998 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1999 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2000 EXPECT_TRUE(MD->getValue() == GV0.get());
2001 ASSERT_TRUE(GV0->use_empty());
2003 std::unique_ptr<GlobalVariable> GV1(
2004 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2005 GV0->replaceAllUsesWith(GV1.get());
2006 EXPECT_TRUE(MD->getValue() == GV1.get());
2008 // Reset it, so we don't inadvertently test deletion.
2012 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2013 Type *Ty = Type::getInt1PtrTy(Context);
2014 std::unique_ptr<GlobalVariable> GV(
2015 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2016 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2017 EXPECT_TRUE(MD->getValue() == GV.get());
2018 ASSERT_TRUE(GV->use_empty());
2024 TEST(NamedMDNodeTest, Search) {
2025 LLVMContext Context;
2026 ConstantAsMetadata *C =
2027 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2028 ConstantAsMetadata *C2 =
2029 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
2031 Metadata *const V = C;
2032 Metadata *const V2 = C2;
2033 MDNode *n = MDNode::get(Context, V);
2034 MDNode *n2 = MDNode::get(Context, V2);
2036 Module M("MyModule", Context);
2037 const char *Name = "llvm.NMD1";
2038 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2040 NMD->addOperand(n2);
2043 raw_string_ostream oss(Str);
2045 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",