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/DebugInfoMetadata.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Metadata.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/Type.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "gtest/gtest.h"
25 TEST(ContextAndReplaceableUsesTest, FromContext) {
27 ContextAndReplaceableUses CRU(Context);
28 EXPECT_EQ(&Context, &CRU.getContext());
29 EXPECT_FALSE(CRU.hasReplaceableUses());
30 EXPECT_FALSE(CRU.getReplaceableUses());
33 TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
35 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
36 EXPECT_EQ(&Context, &CRU.getContext());
37 EXPECT_TRUE(CRU.hasReplaceableUses());
38 EXPECT_TRUE(CRU.getReplaceableUses());
41 TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
43 ContextAndReplaceableUses CRU(Context);
44 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
45 EXPECT_EQ(&Context, &CRU.getContext());
46 EXPECT_TRUE(CRU.hasReplaceableUses());
47 EXPECT_TRUE(CRU.getReplaceableUses());
50 TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
52 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
53 auto *Ptr = ReplaceableUses.get();
54 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
55 ReplaceableUses = CRU.takeReplaceableUses();
56 EXPECT_EQ(&Context, &CRU.getContext());
57 EXPECT_FALSE(CRU.hasReplaceableUses());
58 EXPECT_FALSE(CRU.getReplaceableUses());
59 EXPECT_EQ(Ptr, ReplaceableUses.get());
62 class MetadataTest : public testing::Test {
65 MDNode *getNode() { return MDNode::get(Context, None); }
66 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
67 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
68 Metadata *MDs[] = {MD1, MD2};
69 return MDNode::get(Context, MDs);
72 typedef MetadataTest MDStringTest;
74 // Test that construction of MDString with different value produces different
75 // MDString objects, even with the same string pointer and nulls in the string.
76 TEST_F(MDStringTest, CreateDifferent) {
77 char x[3] = { 'f', 0, 'A' };
78 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
80 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
84 // Test that creation of MDStrings with the same string contents produces the
85 // same MDString object, even with different pointers.
86 TEST_F(MDStringTest, CreateSame) {
87 char x[4] = { 'a', 'b', 'c', 'X' };
88 char y[4] = { 'a', 'b', 'c', 'Y' };
90 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
91 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
95 // Test that MDString prints out the string we fed it.
96 TEST_F(MDStringTest, PrintingSimple) {
97 char *str = new char[13];
98 strncpy(str, "testing 1 2 3", 13);
99 MDString *s = MDString::get(Context, StringRef(str, 13));
100 strncpy(str, "aaaaaaaaaaaaa", 13);
104 raw_string_ostream oss(Str);
106 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
109 // Test printing of MDString with non-printable characters.
110 TEST_F(MDStringTest, PrintingComplex) {
111 char str[5] = {0, '\n', '"', '\\', (char)-1};
112 MDString *s = MDString::get(Context, StringRef(str+0, 5));
114 raw_string_ostream oss(Str);
116 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
119 typedef MetadataTest MDNodeTest;
121 // Test the two constructors, and containing other Constants.
122 TEST_F(MDNodeTest, Simple) {
123 char x[3] = { 'a', 'b', 'c' };
124 char y[3] = { '1', '2', '3' };
126 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
127 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
128 ConstantAsMetadata *CI = ConstantAsMetadata::get(
129 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
131 std::vector<Metadata *> V;
136 MDNode *n1 = MDNode::get(Context, V);
137 Metadata *const c1 = n1;
138 MDNode *n2 = MDNode::get(Context, c1);
139 Metadata *const c2 = n2;
140 MDNode *n3 = MDNode::get(Context, V);
141 MDNode *n4 = MDNode::getIfExists(Context, V);
142 MDNode *n5 = MDNode::getIfExists(Context, c1);
143 MDNode *n6 = MDNode::getIfExists(Context, c2);
148 EXPECT_EQ(n6, (Metadata *)nullptr);
150 EXPECT_EQ(3u, n1->getNumOperands());
151 EXPECT_EQ(s1, n1->getOperand(0));
152 EXPECT_EQ(CI, n1->getOperand(1));
153 EXPECT_EQ(s2, n1->getOperand(2));
155 EXPECT_EQ(1u, n2->getNumOperands());
156 EXPECT_EQ(n1, n2->getOperand(0));
159 TEST_F(MDNodeTest, Delete) {
160 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
161 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
163 Metadata *const V = LocalAsMetadata::get(I);
164 MDNode *n = MDNode::get(Context, V);
165 TrackingMDRef wvh(n);
172 TEST_F(MDNodeTest, SelfReference) {
176 auto Temp = MDNode::getTemporary(Context, None);
177 Metadata *Args[] = {Temp.get()};
178 MDNode *Self = MDNode::get(Context, Args);
179 Self->replaceOperandWith(0, Self);
180 ASSERT_EQ(Self, Self->getOperand(0));
182 // Self-references should be distinct, so MDNode::get() should grab a
183 // uniqued node that references Self, not Self.
185 MDNode *Ref1 = MDNode::get(Context, Args);
186 MDNode *Ref2 = MDNode::get(Context, Args);
187 EXPECT_NE(Self, Ref1);
188 EXPECT_EQ(Ref1, Ref2);
194 auto Temp = MDNode::getTemporary(Context, None);
195 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
196 MDNode *Self = MDNode::get(Context, Args);
197 Self->replaceOperandWith(0, Self);
198 ASSERT_EQ(Self, Self->getOperand(0));
200 // Self-references should be distinct, so MDNode::get() should grab a
201 // uniqued node that references Self, not Self itself.
203 MDNode *Ref1 = MDNode::get(Context, Args);
204 MDNode *Ref2 = MDNode::get(Context, Args);
205 EXPECT_NE(Self, Ref1);
206 EXPECT_EQ(Ref1, Ref2);
210 TEST_F(MDNodeTest, Print) {
211 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
212 MDString *S = MDString::get(Context, "foo");
213 MDNode *N0 = getNode();
214 MDNode *N1 = getNode(N0);
215 MDNode *N2 = getNode(N0, N1);
217 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
218 MDNode *N = MDNode::get(Context, Args);
220 std::string Expected;
222 raw_string_ostream OS(Expected);
224 C->printAsOperand(OS);
226 S->printAsOperand(OS);
228 MDNode *Nodes[] = {N0, N1, N2};
229 for (auto *Node : Nodes)
230 OS << ", <" << (void *)Node << ">";
236 raw_string_ostream OS(Actual);
240 EXPECT_EQ(Expected, Actual);
243 TEST_F(MDNodeTest, NullOperand) {
245 MDNode *Empty = MDNode::get(Context, None);
247 // metadata !{metadata !{}}
248 Metadata *Ops[] = {Empty};
249 MDNode *N = MDNode::get(Context, Ops);
250 ASSERT_EQ(Empty, N->getOperand(0));
252 // metadata !{metadata !{}} => metadata !{null}
253 N->replaceOperandWith(0, nullptr);
254 ASSERT_EQ(nullptr, N->getOperand(0));
258 MDNode *NullOp = MDNode::get(Context, Ops);
259 ASSERT_EQ(nullptr, NullOp->getOperand(0));
260 EXPECT_EQ(N, NullOp);
263 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
265 MDNode *Empty = MDNode::get(Context, None);
266 ASSERT_TRUE(Empty->isResolved());
267 EXPECT_FALSE(Empty->isDistinct());
270 Metadata *Wrapped1Ops[] = {Empty};
271 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
272 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
273 ASSERT_TRUE(Wrapped1->isResolved());
274 EXPECT_FALSE(Wrapped1->isDistinct());
277 Metadata *Wrapped2Ops[] = {Wrapped1};
278 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
279 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
280 ASSERT_TRUE(Wrapped2->isResolved());
281 EXPECT_FALSE(Wrapped2->isDistinct());
283 // !{!{!{}}} => !{!{}}
284 Wrapped2->replaceOperandWith(0, Empty);
285 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
286 EXPECT_TRUE(Wrapped2->isDistinct());
287 EXPECT_FALSE(Wrapped1->isDistinct());
290 TEST_F(MDNodeTest, getDistinct) {
292 MDNode *Empty = MDNode::get(Context, None);
293 ASSERT_TRUE(Empty->isResolved());
294 ASSERT_FALSE(Empty->isDistinct());
295 ASSERT_EQ(Empty, MDNode::get(Context, None));
298 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
299 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
300 EXPECT_TRUE(Distinct1->isResolved());
301 EXPECT_TRUE(Distinct2->isDistinct());
302 EXPECT_NE(Empty, Distinct1);
303 EXPECT_NE(Empty, Distinct2);
304 EXPECT_NE(Distinct1, Distinct2);
307 ASSERT_EQ(Empty, MDNode::get(Context, None));
310 TEST_F(MDNodeTest, isUniqued) {
311 MDNode *U = MDTuple::get(Context, None);
312 MDNode *D = MDTuple::getDistinct(Context, None);
313 auto T = MDTuple::getTemporary(Context, None);
314 EXPECT_TRUE(U->isUniqued());
315 EXPECT_FALSE(D->isUniqued());
316 EXPECT_FALSE(T->isUniqued());
319 TEST_F(MDNodeTest, isDistinct) {
320 MDNode *U = MDTuple::get(Context, None);
321 MDNode *D = MDTuple::getDistinct(Context, None);
322 auto T = MDTuple::getTemporary(Context, None);
323 EXPECT_FALSE(U->isDistinct());
324 EXPECT_TRUE(D->isDistinct());
325 EXPECT_FALSE(T->isDistinct());
328 TEST_F(MDNodeTest, isTemporary) {
329 MDNode *U = MDTuple::get(Context, None);
330 MDNode *D = MDTuple::getDistinct(Context, None);
331 auto T = MDTuple::getTemporary(Context, None);
332 EXPECT_FALSE(U->isTemporary());
333 EXPECT_FALSE(D->isTemporary());
334 EXPECT_TRUE(T->isTemporary());
337 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
339 auto Temp = MDTuple::getTemporary(Context, None);
340 ASSERT_FALSE(Temp->isResolved());
342 // distinct !{temporary !{}}
343 Metadata *Ops[] = {Temp.get()};
344 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
345 EXPECT_TRUE(Distinct->isResolved());
346 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
348 // temporary !{} => !{}
349 MDNode *Empty = MDNode::get(Context, None);
350 Temp->replaceAllUsesWith(Empty);
351 EXPECT_EQ(Empty, Distinct->getOperand(0));
354 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
356 MDNode *N0 = MDNode::get(Context, None);
359 auto Temp3 = MDTuple::getTemporary(Context, None);
360 Metadata *Ops1[] = {Temp3.get(), nullptr};
361 MDNode *N1 = MDNode::get(Context, Ops1);
364 Metadata *Ops2[] = {Temp3.get(), N0};
365 MDNode *N2 = MDNode::get(Context, Ops2);
368 Metadata *Ops3[] = {N2};
369 MDNode *N3 = MDNode::get(Context, Ops3);
370 Temp3->replaceAllUsesWith(N3);
373 Metadata *Ops4[] = {N1};
374 MDNode *N4 = MDNode::get(Context, Ops4);
376 // Confirm that the cycle prevented RAUW from getting dropped.
377 EXPECT_TRUE(N0->isResolved());
378 EXPECT_FALSE(N1->isResolved());
379 EXPECT_FALSE(N2->isResolved());
380 EXPECT_FALSE(N3->isResolved());
381 EXPECT_FALSE(N4->isResolved());
383 // Create a couple of distinct nodes to observe what's going on.
385 // !5 = distinct !{!2}
386 // !6 = distinct !{!3}
387 Metadata *Ops5[] = {N2};
388 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
389 Metadata *Ops6[] = {N3};
390 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
392 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
393 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
394 // references !3, this can cause a re-entry of handleChangedOperand() when !3
395 // is not ready for it.
397 // !2->replaceOperandWith(1, nullptr)
398 // !2: !{!3, !0} => !{!3, null}
399 // !2->replaceAllUsesWith(!1)
400 // !3: !{!2] => !{!1}
401 // !3->replaceAllUsesWith(!4)
402 N2->replaceOperandWith(1, nullptr);
404 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
405 // under us. Just check that the other nodes are sane.
409 // !5 = distinct !{!1}
410 // !6 = distinct !{!4}
411 EXPECT_EQ(N4, N1->getOperand(0));
412 EXPECT_EQ(N1, N4->getOperand(0));
413 EXPECT_EQ(N1, N5->getOperand(0));
414 EXPECT_EQ(N4, N6->getOperand(0));
417 TEST_F(MDNodeTest, replaceResolvedOperand) {
418 // Check code for replacing one resolved operand with another. If doing this
419 // directly (via replaceOperandWith()) becomes illegal, change the operand to
420 // a global value that gets RAUW'ed.
422 // Use a temporary node to keep N from being resolved.
423 auto Temp = MDTuple::getTemporary(Context, None);
424 Metadata *Ops[] = {nullptr, Temp.get()};
426 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
427 MDNode *N = MDTuple::get(Context, Ops);
428 EXPECT_EQ(nullptr, N->getOperand(0));
429 ASSERT_FALSE(N->isResolved());
431 // Check code for replacing resolved nodes.
432 N->replaceOperandWith(0, Empty);
433 EXPECT_EQ(Empty, N->getOperand(0));
435 // Check code for adding another unresolved operand.
436 N->replaceOperandWith(0, Temp.get());
437 EXPECT_EQ(Temp.get(), N->getOperand(0));
439 // Remove the references to Temp; required for teardown.
440 Temp->replaceAllUsesWith(nullptr);
443 TEST_F(MDNodeTest, replaceWithUniqued) {
444 auto *Empty = MDTuple::get(Context, None);
445 MDTuple *FirstUniqued;
447 Metadata *Ops[] = {Empty};
448 auto Temp = MDTuple::getTemporary(Context, Ops);
449 EXPECT_TRUE(Temp->isTemporary());
451 // Don't expect a collision.
452 auto *Current = Temp.get();
453 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
454 EXPECT_TRUE(FirstUniqued->isUniqued());
455 EXPECT_TRUE(FirstUniqued->isResolved());
456 EXPECT_EQ(Current, FirstUniqued);
459 Metadata *Ops[] = {Empty};
460 auto Temp = MDTuple::getTemporary(Context, Ops);
461 EXPECT_TRUE(Temp->isTemporary());
463 // Should collide with Uniqued above this time.
464 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
465 EXPECT_TRUE(Uniqued->isUniqued());
466 EXPECT_TRUE(Uniqued->isResolved());
467 EXPECT_EQ(FirstUniqued, Uniqued);
470 auto Unresolved = MDTuple::getTemporary(Context, None);
471 Metadata *Ops[] = {Unresolved.get()};
472 auto Temp = MDTuple::getTemporary(Context, Ops);
473 EXPECT_TRUE(Temp->isTemporary());
475 // Shouldn't be resolved.
476 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
477 EXPECT_TRUE(Uniqued->isUniqued());
478 EXPECT_FALSE(Uniqued->isResolved());
480 // Should be a different node.
481 EXPECT_NE(FirstUniqued, Uniqued);
483 // Should resolve when we update its node (note: be careful to avoid a
484 // collision with any other nodes above).
485 Uniqued->replaceOperandWith(0, nullptr);
486 EXPECT_TRUE(Uniqued->isResolved());
490 TEST_F(MDNodeTest, replaceWithDistinct) {
492 auto *Empty = MDTuple::get(Context, None);
493 Metadata *Ops[] = {Empty};
494 auto Temp = MDTuple::getTemporary(Context, Ops);
495 EXPECT_TRUE(Temp->isTemporary());
497 // Don't expect a collision.
498 auto *Current = Temp.get();
499 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
500 EXPECT_TRUE(Distinct->isDistinct());
501 EXPECT_TRUE(Distinct->isResolved());
502 EXPECT_EQ(Current, Distinct);
505 auto Unresolved = MDTuple::getTemporary(Context, None);
506 Metadata *Ops[] = {Unresolved.get()};
507 auto Temp = MDTuple::getTemporary(Context, Ops);
508 EXPECT_TRUE(Temp->isTemporary());
510 // Don't expect a collision.
511 auto *Current = Temp.get();
512 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
513 EXPECT_TRUE(Distinct->isDistinct());
514 EXPECT_TRUE(Distinct->isResolved());
515 EXPECT_EQ(Current, Distinct);
517 // Cleanup; required for teardown.
518 Unresolved->replaceAllUsesWith(nullptr);
522 TEST_F(MDNodeTest, replaceWithPermanent) {
523 Metadata *Ops[] = {nullptr};
524 auto Temp = MDTuple::getTemporary(Context, Ops);
525 auto *T = Temp.get();
527 // U is a normal, uniqued node that references T.
528 auto *U = MDTuple::get(Context, T);
529 EXPECT_TRUE(U->isUniqued());
531 // Make Temp self-referencing.
532 Temp->replaceOperandWith(0, T);
534 // Try to uniquify Temp. This should, despite the name in the API, give a
535 // 'distinct' node, since self-references aren't allowed to be uniqued.
537 // Since it's distinct, N should have the same address as when it was a
538 // temporary (i.e., be equal to T not U).
539 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
541 EXPECT_TRUE(N->isDistinct());
543 // U should be the canonical unique node with N as the argument.
544 EXPECT_EQ(U, MDTuple::get(Context, N));
545 EXPECT_TRUE(U->isUniqued());
547 // This temporary should collide with U when replaced, but it should still be
549 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
550 EXPECT_TRUE(U->isUniqued());
552 // This temporary should become a new uniqued node.
553 auto Temp2 = MDTuple::getTemporary(Context, U);
554 auto *V = Temp2.get();
555 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
556 EXPECT_TRUE(V->isUniqued());
557 EXPECT_EQ(U, V->getOperand(0));
560 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
562 EXPECT_EQ(nullptr, Ref.get());
564 auto Temp = MDTuple::getTemporary(Context, None);
565 Ref.reset(Temp.get());
566 EXPECT_EQ(Temp.get(), Ref.get());
568 EXPECT_EQ(nullptr, Ref.get());
571 typedef MetadataTest MDLocationTest;
573 TEST_F(MDLocationTest, Overflow) {
574 MDNode *N = MDNode::get(Context, None);
576 MDLocation *L = MDLocation::get(Context, 2, 7, N);
577 EXPECT_EQ(2u, L->getLine());
578 EXPECT_EQ(7u, L->getColumn());
580 unsigned U16 = 1u << 16;
582 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
583 EXPECT_EQ(UINT32_MAX, L->getLine());
584 EXPECT_EQ(U16 - 1, L->getColumn());
587 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
588 EXPECT_EQ(UINT32_MAX, L->getLine());
589 EXPECT_EQ(0u, L->getColumn());
592 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
593 EXPECT_EQ(UINT32_MAX, L->getLine());
594 EXPECT_EQ(0u, L->getColumn());
598 TEST_F(MDLocationTest, getDistinct) {
599 MDNode *N = MDNode::get(Context, None);
600 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
601 EXPECT_TRUE(L0->isDistinct());
602 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
603 EXPECT_FALSE(L1->isDistinct());
604 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
607 TEST_F(MDLocationTest, getTemporary) {
608 MDNode *N = MDNode::get(Context, None);
609 auto L = MDLocation::getTemporary(Context, 2, 7, N);
610 EXPECT_TRUE(L->isTemporary());
611 EXPECT_FALSE(L->isResolved());
614 typedef MetadataTest GenericDebugNodeTest;
616 TEST_F(GenericDebugNodeTest, get) {
617 StringRef Header = "header";
618 auto *Empty = MDNode::get(Context, None);
619 Metadata *Ops1[] = {Empty};
620 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
621 EXPECT_EQ(15u, N->getTag());
622 EXPECT_EQ(2u, N->getNumOperands());
623 EXPECT_EQ(Header, N->getHeader());
624 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
625 EXPECT_EQ(1u, N->getNumDwarfOperands());
626 EXPECT_EQ(Empty, N->getDwarfOperand(0));
627 EXPECT_EQ(Empty, N->getOperand(1));
628 ASSERT_TRUE(N->isUniqued());
630 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
632 N->replaceOperandWith(1, nullptr);
633 EXPECT_EQ(15u, N->getTag());
634 EXPECT_EQ(Header, N->getHeader());
635 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
636 ASSERT_TRUE(N->isUniqued());
638 Metadata *Ops2[] = {nullptr};
639 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
641 N->replaceDwarfOperandWith(0, Empty);
642 EXPECT_EQ(15u, N->getTag());
643 EXPECT_EQ(Header, N->getHeader());
644 EXPECT_EQ(Empty, N->getDwarfOperand(0));
645 ASSERT_TRUE(N->isUniqued());
646 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
648 TempGenericDebugNode Temp = N->clone();
649 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
652 TEST_F(GenericDebugNodeTest, getEmptyHeader) {
653 // Canonicalize !"" to null.
654 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
655 EXPECT_EQ(StringRef(), N->getHeader());
656 EXPECT_EQ(nullptr, N->getOperand(0));
659 typedef MetadataTest MDSubrangeTest;
661 TEST_F(MDSubrangeTest, get) {
662 auto *N = MDSubrange::get(Context, 5, 7);
663 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
664 EXPECT_EQ(5, N->getCount());
665 EXPECT_EQ(7, N->getLo());
666 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
667 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
669 TempMDSubrange Temp = N->clone();
670 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
673 typedef MetadataTest MDEnumeratorTest;
675 TEST_F(MDEnumeratorTest, get) {
676 auto *N = MDEnumerator::get(Context, 7, "name");
677 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
678 EXPECT_EQ(7, N->getValue());
679 EXPECT_EQ("name", N->getName());
680 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
682 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
683 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
685 TempMDEnumerator Temp = N->clone();
686 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
689 typedef MetadataTest MDBasicTypeTest;
691 TEST_F(MDBasicTypeTest, get) {
693 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
694 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
695 EXPECT_EQ("special", N->getName());
696 EXPECT_EQ(33u, N->getSizeInBits());
697 EXPECT_EQ(26u, N->getAlignInBits());
698 EXPECT_EQ(7u, N->getEncoding());
699 EXPECT_EQ(0u, N->getLine());
700 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
703 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
704 "special", 33, 26, 7));
706 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
707 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
709 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
711 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
714 TempMDBasicType Temp = N->clone();
715 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
718 typedef MetadataTest MDDerivedTypeTest;
720 TEST_F(MDDerivedTypeTest, get) {
721 Metadata *File = MDTuple::getDistinct(Context, None);
722 Metadata *Scope = MDTuple::getDistinct(Context, None);
723 Metadata *BaseType = MDTuple::getDistinct(Context, None);
724 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
726 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
727 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
728 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
729 EXPECT_EQ("something", N->getName());
730 EXPECT_EQ(File, N->getFile());
731 EXPECT_EQ(1u, N->getLine());
732 EXPECT_EQ(Scope, N->getScope());
733 EXPECT_EQ(BaseType, N->getBaseType());
734 EXPECT_EQ(2u, N->getSizeInBits());
735 EXPECT_EQ(3u, N->getAlignInBits());
736 EXPECT_EQ(4u, N->getOffsetInBits());
737 EXPECT_EQ(5u, N->getFlags());
738 EXPECT_EQ(ExtraData, N->getExtraData());
739 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
740 "something", File, 1, Scope, BaseType, 2, 3,
743 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
744 "something", File, 1, Scope, BaseType, 2, 3,
746 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
747 File, 1, Scope, BaseType, 2, 3, 4, 5,
749 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
750 "something", Scope, 1, Scope, BaseType, 2, 3,
752 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
753 "something", File, 2, Scope, BaseType, 2, 3,
756 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
757 File, 1, File, BaseType, 2, 3, 4, 5, ExtraData));
759 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
760 File, 1, Scope, File, 2, 3, 4, 5, ExtraData));
761 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
762 "something", File, 1, Scope, BaseType, 3, 3,
764 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
765 "something", File, 1, Scope, BaseType, 2, 2,
767 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
768 "something", File, 1, Scope, BaseType, 2, 3,
770 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
771 "something", File, 1, Scope, BaseType, 2, 3,
774 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
775 File, 1, Scope, BaseType, 2, 3, 4, 5, File));
777 TempMDDerivedType Temp = N->clone();
778 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
781 typedef MetadataTest MDCompositeTypeTest;
783 TEST_F(MDCompositeTypeTest, get) {
784 unsigned Tag = dwarf::DW_TAG_structure_type;
785 StringRef Name = "some name";
786 Metadata *File = MDTuple::getDistinct(Context, None);
788 Metadata *Scope = MDTuple::getDistinct(Context, None);
789 Metadata *BaseType = MDTuple::getDistinct(Context, None);
790 unsigned SizeInBits = 2;
791 unsigned AlignInBits = 3;
792 unsigned OffsetInBits = 4;
794 Metadata *Elements = MDTuple::getDistinct(Context, None);
795 unsigned RuntimeLang = 6;
796 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
797 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
798 StringRef Identifier = "some id";
800 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
801 BaseType, SizeInBits, AlignInBits,
802 OffsetInBits, Flags, Elements, RuntimeLang,
803 VTableHolder, TemplateParams, Identifier);
804 EXPECT_EQ(Tag, N->getTag());
805 EXPECT_EQ(Name, N->getName());
806 EXPECT_EQ(File, N->getFile());
807 EXPECT_EQ(Line, N->getLine());
808 EXPECT_EQ(Scope, N->getScope());
809 EXPECT_EQ(BaseType, N->getBaseType());
810 EXPECT_EQ(SizeInBits, N->getSizeInBits());
811 EXPECT_EQ(AlignInBits, N->getAlignInBits());
812 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
813 EXPECT_EQ(Flags, N->getFlags());
814 EXPECT_EQ(Elements, N->getElements());
815 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
816 EXPECT_EQ(VTableHolder, N->getVTableHolder());
817 EXPECT_EQ(TemplateParams, N->getTemplateParams());
818 EXPECT_EQ(Identifier, N->getIdentifier());
820 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
821 BaseType, SizeInBits, AlignInBits,
822 OffsetInBits, Flags, Elements, RuntimeLang,
823 VTableHolder, TemplateParams, Identifier));
825 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
826 BaseType, SizeInBits, AlignInBits,
827 OffsetInBits, Flags, Elements, RuntimeLang,
828 VTableHolder, TemplateParams, Identifier));
829 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
830 BaseType, SizeInBits, AlignInBits,
831 OffsetInBits, Flags, Elements, RuntimeLang,
832 VTableHolder, TemplateParams, Identifier));
833 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, Scope, Line, Scope,
834 BaseType, SizeInBits, AlignInBits,
835 OffsetInBits, Flags, Elements, RuntimeLang,
836 VTableHolder, TemplateParams, Identifier));
837 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
838 BaseType, SizeInBits, AlignInBits,
839 OffsetInBits, Flags, Elements, RuntimeLang,
840 VTableHolder, TemplateParams, Identifier));
841 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, File,
842 BaseType, SizeInBits, AlignInBits,
843 OffsetInBits, Flags, Elements, RuntimeLang,
844 VTableHolder, TemplateParams, Identifier));
845 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
846 SizeInBits, AlignInBits, OffsetInBits,
847 Flags, Elements, RuntimeLang, VTableHolder,
848 TemplateParams, Identifier));
849 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
850 BaseType, SizeInBits + 1, AlignInBits,
851 OffsetInBits, Flags, Elements, RuntimeLang,
852 VTableHolder, TemplateParams, Identifier));
853 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
854 BaseType, SizeInBits, AlignInBits + 1,
855 OffsetInBits, Flags, Elements, RuntimeLang,
856 VTableHolder, TemplateParams, Identifier));
857 EXPECT_NE(N, MDCompositeType::get(
858 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
859 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
860 VTableHolder, TemplateParams, Identifier));
861 EXPECT_NE(N, MDCompositeType::get(
862 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
863 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
864 VTableHolder, TemplateParams, Identifier));
865 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
866 BaseType, SizeInBits, AlignInBits,
867 OffsetInBits, Flags, File, RuntimeLang,
868 VTableHolder, TemplateParams, Identifier));
869 EXPECT_NE(N, MDCompositeType::get(
870 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
871 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
872 VTableHolder, TemplateParams, Identifier));
873 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
874 BaseType, SizeInBits, AlignInBits,
875 OffsetInBits, Flags, Elements, RuntimeLang,
876 File, TemplateParams, Identifier));
877 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
878 BaseType, SizeInBits, AlignInBits,
879 OffsetInBits, Flags, Elements, RuntimeLang,
880 VTableHolder, File, Identifier));
881 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
882 BaseType, SizeInBits, AlignInBits,
883 OffsetInBits, Flags, Elements, RuntimeLang,
884 VTableHolder, TemplateParams, "other"));
886 // Be sure that missing identifiers get null pointers.
887 EXPECT_FALSE(MDCompositeType::get(
888 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
889 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
890 VTableHolder, TemplateParams, "")->getRawIdentifier());
891 EXPECT_FALSE(MDCompositeType::get(
892 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
893 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
894 VTableHolder, TemplateParams)->getRawIdentifier());
896 TempMDCompositeType Temp = N->clone();
897 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
900 TEST_F(MDCompositeTypeTest, replaceOperands) {
901 unsigned Tag = dwarf::DW_TAG_structure_type;
902 StringRef Name = "some name";
903 Metadata *File = MDTuple::getDistinct(Context, None);
905 Metadata *Scope = MDTuple::getDistinct(Context, None);
906 Metadata *BaseType = MDTuple::getDistinct(Context, None);
907 unsigned SizeInBits = 2;
908 unsigned AlignInBits = 3;
909 unsigned OffsetInBits = 4;
911 unsigned RuntimeLang = 6;
912 StringRef Identifier = "some id";
914 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
915 BaseType, SizeInBits, AlignInBits,
916 OffsetInBits, Flags, nullptr, RuntimeLang,
917 nullptr, nullptr, Identifier);
919 auto *Elements = MDTuple::getDistinct(Context, None);
920 EXPECT_EQ(nullptr, N->getElements());
921 N->replaceElements(Elements);
922 EXPECT_EQ(Elements, N->getElements());
923 N->replaceElements(nullptr);
924 EXPECT_EQ(nullptr, N->getElements());
926 auto *VTableHolder = MDTuple::getDistinct(Context, None);
927 EXPECT_EQ(nullptr, N->getVTableHolder());
928 N->replaceVTableHolder(VTableHolder);
929 EXPECT_EQ(VTableHolder, N->getVTableHolder());
930 N->replaceVTableHolder(nullptr);
931 EXPECT_EQ(nullptr, N->getVTableHolder());
933 auto *TemplateParams = MDTuple::getDistinct(Context, None);
934 EXPECT_EQ(nullptr, N->getTemplateParams());
935 N->replaceTemplateParams(TemplateParams);
936 EXPECT_EQ(TemplateParams, N->getTemplateParams());
937 N->replaceTemplateParams(nullptr);
938 EXPECT_EQ(nullptr, N->getTemplateParams());
941 typedef MetadataTest MDSubroutineTypeTest;
943 TEST_F(MDSubroutineTypeTest, get) {
945 Metadata *TypeArray = MDTuple::getDistinct(Context, None);
947 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
948 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
949 EXPECT_EQ(Flags, N->getFlags());
950 EXPECT_EQ(TypeArray, N->getTypeArray());
951 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
953 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
954 EXPECT_NE(N, MDSubroutineType::get(Context, Flags,
955 MDTuple::getDistinct(Context, None)));
957 TempMDSubroutineType Temp = N->clone();
958 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
961 typedef MetadataTest MDFileTest;
963 TEST_F(MDFileTest, get) {
964 StringRef Filename = "file";
965 StringRef Directory = "dir";
966 auto *N = MDFile::get(Context, Filename, Directory);
968 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
969 EXPECT_EQ(Filename, N->getFilename());
970 EXPECT_EQ(Directory, N->getDirectory());
971 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
973 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
974 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
976 TempMDFile Temp = N->clone();
977 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
980 typedef MetadataTest MDCompileUnitTest;
982 TEST_F(MDCompileUnitTest, get) {
983 unsigned SourceLanguage = 1;
984 Metadata *File = MDTuple::getDistinct(Context, None);
985 StringRef Producer = "some producer";
986 bool IsOptimized = false;
987 StringRef Flags = "flag after flag";
988 unsigned RuntimeVersion = 2;
989 StringRef SplitDebugFilename = "another/file";
990 unsigned EmissionKind = 3;
991 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
992 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
993 Metadata *Subprograms = MDTuple::getDistinct(Context, None);
994 Metadata *GlobalVariables = MDTuple::getDistinct(Context, None);
995 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
996 auto *N = MDCompileUnit::get(
997 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
998 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
999 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1001 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1002 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1003 EXPECT_EQ(File, N->getFile());
1004 EXPECT_EQ(Producer, N->getProducer());
1005 EXPECT_EQ(IsOptimized, N->isOptimized());
1006 EXPECT_EQ(Flags, N->getFlags());
1007 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1008 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1009 EXPECT_EQ(EmissionKind, N->getEmissionKind());
1010 EXPECT_EQ(EnumTypes, N->getEnumTypes());
1011 EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
1012 EXPECT_EQ(Subprograms, N->getSubprograms());
1013 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1014 EXPECT_EQ(ImportedEntities, N->getImportedEntities());
1015 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1016 IsOptimized, Flags, RuntimeVersion,
1017 SplitDebugFilename, EmissionKind, EnumTypes,
1018 RetainedTypes, Subprograms, GlobalVariables,
1021 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1022 IsOptimized, Flags, RuntimeVersion,
1023 SplitDebugFilename, EmissionKind, EnumTypes,
1024 RetainedTypes, Subprograms, GlobalVariables,
1026 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer,
1027 IsOptimized, Flags, RuntimeVersion,
1028 SplitDebugFilename, EmissionKind, EnumTypes,
1029 RetainedTypes, Subprograms, GlobalVariables,
1031 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1032 IsOptimized, Flags, RuntimeVersion,
1033 SplitDebugFilename, EmissionKind, EnumTypes,
1034 RetainedTypes, Subprograms, GlobalVariables,
1036 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1037 !IsOptimized, Flags, RuntimeVersion,
1038 SplitDebugFilename, EmissionKind, EnumTypes,
1039 RetainedTypes, Subprograms, GlobalVariables,
1041 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1042 IsOptimized, "other", RuntimeVersion,
1043 SplitDebugFilename, EmissionKind, EnumTypes,
1044 RetainedTypes, Subprograms, GlobalVariables,
1046 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1047 IsOptimized, Flags, RuntimeVersion + 1,
1048 SplitDebugFilename, EmissionKind, EnumTypes,
1049 RetainedTypes, Subprograms, GlobalVariables,
1052 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1053 IsOptimized, Flags, RuntimeVersion, "other",
1054 EmissionKind, EnumTypes, RetainedTypes,
1055 Subprograms, GlobalVariables, ImportedEntities));
1056 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1057 IsOptimized, Flags, RuntimeVersion,
1058 SplitDebugFilename, EmissionKind + 1,
1059 EnumTypes, RetainedTypes, Subprograms,
1060 GlobalVariables, ImportedEntities));
1061 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1062 IsOptimized, Flags, RuntimeVersion,
1063 SplitDebugFilename, EmissionKind, File,
1064 RetainedTypes, Subprograms, GlobalVariables,
1066 EXPECT_NE(N, MDCompileUnit::get(
1067 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1068 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1069 File, Subprograms, GlobalVariables, ImportedEntities));
1070 EXPECT_NE(N, MDCompileUnit::get(
1071 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1072 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1073 RetainedTypes, File, GlobalVariables, ImportedEntities));
1074 EXPECT_NE(N, MDCompileUnit::get(
1075 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1076 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1077 RetainedTypes, Subprograms, File, ImportedEntities));
1078 EXPECT_NE(N, MDCompileUnit::get(
1079 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1080 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1081 RetainedTypes, Subprograms, GlobalVariables, File));
1083 TempMDCompileUnit Temp = N->clone();
1084 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1087 TEST_F(MDCompileUnitTest, replaceArrays) {
1088 unsigned SourceLanguage = 1;
1089 Metadata *File = MDTuple::getDistinct(Context, None);
1090 StringRef Producer = "some producer";
1091 bool IsOptimized = false;
1092 StringRef Flags = "flag after flag";
1093 unsigned RuntimeVersion = 2;
1094 StringRef SplitDebugFilename = "another/file";
1095 unsigned EmissionKind = 3;
1096 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1097 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1098 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1099 auto *N = MDCompileUnit::get(
1100 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1101 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1102 RetainedTypes, nullptr, nullptr, ImportedEntities);
1104 auto *Subprograms = MDTuple::getDistinct(Context, None);
1105 EXPECT_EQ(nullptr, N->getSubprograms());
1106 N->replaceSubprograms(Subprograms);
1107 EXPECT_EQ(Subprograms, N->getSubprograms());
1108 N->replaceSubprograms(nullptr);
1109 EXPECT_EQ(nullptr, N->getSubprograms());
1111 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1112 EXPECT_EQ(nullptr, N->getGlobalVariables());
1113 N->replaceGlobalVariables(GlobalVariables);
1114 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1115 N->replaceGlobalVariables(nullptr);
1116 EXPECT_EQ(nullptr, N->getGlobalVariables());
1119 typedef MetadataTest MDSubprogramTest;
1121 TEST_F(MDSubprogramTest, get) {
1122 Metadata *Scope = MDTuple::getDistinct(Context, None);
1123 StringRef Name = "name";
1124 StringRef LinkageName = "linkage";
1125 Metadata *File = MDTuple::getDistinct(Context, None);
1127 Metadata *Type = MDTuple::getDistinct(Context, None);
1128 bool IsLocalToUnit = false;
1129 bool IsDefinition = true;
1130 unsigned ScopeLine = 3;
1131 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1132 unsigned Virtuality = 4;
1133 unsigned VirtualIndex = 5;
1135 bool IsOptimized = false;
1136 Metadata *Function = MDTuple::getDistinct(Context, None);
1137 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1138 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1139 Metadata *Variables = MDTuple::getDistinct(Context, None);
1141 auto *N = MDSubprogram::get(
1142 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1143 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1144 IsOptimized, Function, TemplateParams, Declaration, Variables);
1146 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1147 EXPECT_EQ(Scope, N->getScope());
1148 EXPECT_EQ(Name, N->getName());
1149 EXPECT_EQ(LinkageName, N->getLinkageName());
1150 EXPECT_EQ(File, N->getFile());
1151 EXPECT_EQ(Line, N->getLine());
1152 EXPECT_EQ(Type, N->getType());
1153 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1154 EXPECT_EQ(IsDefinition, N->isDefinition());
1155 EXPECT_EQ(ScopeLine, N->getScopeLine());
1156 EXPECT_EQ(ContainingType, N->getContainingType());
1157 EXPECT_EQ(Virtuality, N->getVirtuality());
1158 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1159 EXPECT_EQ(Flags, N->getFlags());
1160 EXPECT_EQ(IsOptimized, N->isOptimized());
1161 EXPECT_EQ(Function, N->getFunction());
1162 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1163 EXPECT_EQ(Declaration, N->getDeclaration());
1164 EXPECT_EQ(Variables, N->getVariables());
1165 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1166 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1167 ContainingType, Virtuality, VirtualIndex,
1168 Flags, IsOptimized, Function, TemplateParams,
1169 Declaration, Variables));
1171 EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1172 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1173 ContainingType, Virtuality, VirtualIndex,
1174 Flags, IsOptimized, Function, TemplateParams,
1175 Declaration, Variables));
1176 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1177 Line, Type, IsLocalToUnit, IsDefinition,
1178 ScopeLine, ContainingType, Virtuality,
1179 VirtualIndex, Flags, IsOptimized, Function,
1180 TemplateParams, Declaration, Variables));
1181 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1182 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1183 ContainingType, Virtuality, VirtualIndex,
1184 Flags, IsOptimized, Function, TemplateParams,
1185 Declaration, Variables));
1186 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1187 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1188 ContainingType, Virtuality, VirtualIndex,
1189 Flags, IsOptimized, Function, TemplateParams,
1190 Declaration, Variables));
1191 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1192 Line + 1, Type, IsLocalToUnit, IsDefinition,
1193 ScopeLine, ContainingType, Virtuality,
1194 VirtualIndex, Flags, IsOptimized, Function,
1195 TemplateParams, Declaration, Variables));
1196 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1197 Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1198 ContainingType, Virtuality, VirtualIndex,
1199 Flags, IsOptimized, Function, TemplateParams,
1200 Declaration, Variables));
1201 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1202 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1203 ContainingType, Virtuality, VirtualIndex,
1204 Flags, IsOptimized, Function, TemplateParams,
1205 Declaration, Variables));
1206 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1207 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1208 ContainingType, Virtuality, VirtualIndex,
1209 Flags, IsOptimized, Function, TemplateParams,
1210 Declaration, Variables));
1211 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1212 Type, IsLocalToUnit, IsDefinition,
1213 ScopeLine + 1, ContainingType, Virtuality,
1214 VirtualIndex, Flags, IsOptimized, Function,
1215 TemplateParams, Declaration, Variables));
1216 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1217 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1218 Type, Virtuality, VirtualIndex, Flags,
1219 IsOptimized, Function, TemplateParams,
1220 Declaration, Variables));
1221 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1222 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1223 ContainingType, Virtuality + 1, VirtualIndex,
1224 Flags, IsOptimized, Function, TemplateParams,
1225 Declaration, Variables));
1226 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1227 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1228 ContainingType, Virtuality, VirtualIndex + 1,
1229 Flags, IsOptimized, Function, TemplateParams,
1230 Declaration, Variables));
1231 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1232 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1233 ContainingType, Virtuality, VirtualIndex,
1234 ~Flags, IsOptimized, Function, TemplateParams,
1235 Declaration, Variables));
1236 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1237 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1238 ContainingType, Virtuality, VirtualIndex,
1239 Flags, !IsOptimized, Function, TemplateParams,
1240 Declaration, Variables));
1241 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1242 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1243 ContainingType, Virtuality, VirtualIndex,
1244 Flags, IsOptimized, Type, TemplateParams,
1245 Declaration, Variables));
1246 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1247 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1248 ContainingType, Virtuality, VirtualIndex,
1249 Flags, IsOptimized, Function, Type,
1250 Declaration, Variables));
1251 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1252 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1253 ContainingType, Virtuality, VirtualIndex,
1254 Flags, IsOptimized, Function, TemplateParams,
1256 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1257 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1258 ContainingType, Virtuality, VirtualIndex,
1259 Flags, IsOptimized, Function, TemplateParams,
1260 Declaration, Type));
1262 TempMDSubprogram Temp = N->clone();
1263 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1266 TEST_F(MDSubprogramTest, replaceFunction) {
1267 Metadata *Scope = MDTuple::getDistinct(Context, None);
1268 StringRef Name = "name";
1269 StringRef LinkageName = "linkage";
1270 Metadata *File = MDTuple::getDistinct(Context, None);
1272 Metadata *Type = MDTuple::getDistinct(Context, None);
1273 bool IsLocalToUnit = false;
1274 bool IsDefinition = true;
1275 unsigned ScopeLine = 3;
1276 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1277 unsigned Virtuality = 4;
1278 unsigned VirtualIndex = 5;
1280 bool IsOptimized = false;
1281 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1282 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1283 Metadata *Variables = MDTuple::getDistinct(Context, None);
1285 auto *N = MDSubprogram::get(
1286 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1287 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1288 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1290 EXPECT_EQ(nullptr, N->getFunction());
1292 std::unique_ptr<Function> F(
1293 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1294 GlobalValue::ExternalLinkage));
1295 N->replaceFunction(F.get());
1296 EXPECT_EQ(ConstantAsMetadata::get(F.get()), N->getFunction());
1298 N->replaceFunction(nullptr);
1299 EXPECT_EQ(nullptr, N->getFunction());
1302 typedef MetadataTest MDLexicalBlockTest;
1304 TEST_F(MDLexicalBlockTest, get) {
1305 Metadata *Scope = MDTuple::getDistinct(Context, None);
1306 Metadata *File = MDTuple::getDistinct(Context, None);
1308 unsigned Column = 8;
1310 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1312 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1313 EXPECT_EQ(Scope, N->getScope());
1314 EXPECT_EQ(File, N->getFile());
1315 EXPECT_EQ(Line, N->getLine());
1316 EXPECT_EQ(Column, N->getColumn());
1317 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1319 EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1320 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1321 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1322 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
1324 TempMDLexicalBlock Temp = N->clone();
1325 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1328 typedef MetadataTest MDLexicalBlockFileTest;
1330 TEST_F(MDLexicalBlockFileTest, get) {
1331 Metadata *Scope = MDTuple::getDistinct(Context, None);
1332 Metadata *File = MDTuple::getDistinct(Context, None);
1333 unsigned Discriminator = 5;
1335 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1337 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1338 EXPECT_EQ(Scope, N->getScope());
1339 EXPECT_EQ(File, N->getFile());
1340 EXPECT_EQ(Discriminator, N->getDiscriminator());
1341 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1343 EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1344 EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1346 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1348 TempMDLexicalBlockFile Temp = N->clone();
1349 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1352 typedef MetadataTest MDNamespaceTest;
1354 TEST_F(MDNamespaceTest, get) {
1355 Metadata *Scope = MDTuple::getDistinct(Context, None);
1356 Metadata *File = MDTuple::getDistinct(Context, None);
1357 StringRef Name = "namespace";
1360 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1362 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1363 EXPECT_EQ(Scope, N->getScope());
1364 EXPECT_EQ(File, N->getFile());
1365 EXPECT_EQ(Name, N->getName());
1366 EXPECT_EQ(Line, N->getLine());
1367 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1369 EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1370 EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1371 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1372 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
1374 TempMDNamespace Temp = N->clone();
1375 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1378 typedef MetadataTest MDTemplateTypeParameterTest;
1380 TEST_F(MDTemplateTypeParameterTest, get) {
1381 Metadata *Scope = MDTuple::getDistinct(Context, None);
1382 StringRef Name = "template";
1383 Metadata *Type = MDTuple::getDistinct(Context, None);
1385 auto *N = MDTemplateTypeParameter::get(Context, Scope, Name, Type);
1387 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1388 EXPECT_EQ(Scope, N->getScope());
1389 EXPECT_EQ(Name, N->getName());
1390 EXPECT_EQ(Type, N->getType());
1391 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Scope, Name, Type));
1393 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Type, Name, Type));
1394 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Scope, "other", Type));
1395 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Scope, Name, Scope));
1397 TempMDTemplateTypeParameter Temp = N->clone();
1398 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1401 typedef MetadataTest MDTemplateValueParameterTest;
1403 TEST_F(MDTemplateValueParameterTest, get) {
1404 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1405 Metadata *Scope = MDTuple::getDistinct(Context, None);
1406 StringRef Name = "template";
1407 Metadata *Type = MDTuple::getDistinct(Context, None);
1408 Metadata *Value = MDTuple::getDistinct(Context, None);
1411 MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Value);
1412 EXPECT_EQ(Tag, N->getTag());
1413 EXPECT_EQ(Scope, N->getScope());
1414 EXPECT_EQ(Name, N->getName());
1415 EXPECT_EQ(Type, N->getType());
1416 EXPECT_EQ(Value, N->getValue());
1418 N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Value));
1420 EXPECT_NE(N, MDTemplateValueParameter::get(
1421 Context, dwarf::DW_TAG_GNU_template_template_param, Scope,
1422 Name, Type, Value));
1424 N, MDTemplateValueParameter::get(Context, Tag, Type, Name, Type, Value));
1425 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Scope, "other", Type,
1427 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Scope,
1430 N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Scope));
1432 TempMDTemplateValueParameter Temp = N->clone();
1433 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1436 typedef MetadataTest MDGlobalVariableTest;
1438 TEST_F(MDGlobalVariableTest, get) {
1439 Metadata *Scope = MDTuple::getDistinct(Context, None);
1440 StringRef Name = "name";
1441 StringRef LinkageName = "linkage";
1442 Metadata *File = MDTuple::getDistinct(Context, None);
1444 Metadata *Type = MDTuple::getDistinct(Context, None);
1445 bool IsLocalToUnit = false;
1446 bool IsDefinition = true;
1447 Metadata *Variable = MDTuple::getDistinct(Context, None);
1448 Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None);
1450 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1451 Type, IsLocalToUnit, IsDefinition, Variable,
1452 StaticDataMemberDeclaration);
1453 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1454 EXPECT_EQ(Scope, N->getScope());
1455 EXPECT_EQ(Name, N->getName());
1456 EXPECT_EQ(LinkageName, N->getLinkageName());
1457 EXPECT_EQ(File, N->getFile());
1458 EXPECT_EQ(Line, N->getLine());
1459 EXPECT_EQ(Type, N->getType());
1460 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1461 EXPECT_EQ(IsDefinition, N->isDefinition());
1462 EXPECT_EQ(Variable, N->getVariable());
1463 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1464 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1465 Line, Type, IsLocalToUnit, IsDefinition,
1466 Variable, StaticDataMemberDeclaration));
1468 EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File,
1469 Line, Type, IsLocalToUnit, IsDefinition,
1470 Variable, StaticDataMemberDeclaration));
1471 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1472 Line, Type, IsLocalToUnit, IsDefinition,
1473 Variable, StaticDataMemberDeclaration));
1474 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1475 Type, IsLocalToUnit, IsDefinition,
1476 Variable, StaticDataMemberDeclaration));
1477 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope,
1478 Line, Type, IsLocalToUnit, IsDefinition,
1479 Variable, StaticDataMemberDeclaration));
1481 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1482 Line + 1, Type, IsLocalToUnit, IsDefinition,
1483 Variable, StaticDataMemberDeclaration));
1484 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1485 Line, Scope, IsLocalToUnit, IsDefinition,
1486 Variable, StaticDataMemberDeclaration));
1487 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1488 Line, Type, !IsLocalToUnit, IsDefinition,
1489 Variable, StaticDataMemberDeclaration));
1490 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1491 Line, Type, IsLocalToUnit, !IsDefinition,
1492 Variable, StaticDataMemberDeclaration));
1493 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1494 Line, Type, IsLocalToUnit, IsDefinition,
1495 Type, StaticDataMemberDeclaration));
1496 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1497 Line, Type, IsLocalToUnit, IsDefinition,
1500 TempMDGlobalVariable Temp = N->clone();
1501 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1504 typedef MetadataTest MDLocalVariableTest;
1506 TEST_F(MDLocalVariableTest, get) {
1507 unsigned Tag = dwarf::DW_TAG_arg_variable;
1508 Metadata *Scope = MDTuple::getDistinct(Context, None);
1509 StringRef Name = "name";
1510 Metadata *File = MDTuple::getDistinct(Context, None);
1512 Metadata *Type = MDTuple::getDistinct(Context, None);
1515 Metadata *InlinedAt = MDTuple::getDistinct(Context, None);
1517 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1518 Arg, Flags, InlinedAt);
1519 EXPECT_EQ(Tag, N->getTag());
1520 EXPECT_EQ(Scope, N->getScope());
1521 EXPECT_EQ(Name, N->getName());
1522 EXPECT_EQ(File, N->getFile());
1523 EXPECT_EQ(Line, N->getLine());
1524 EXPECT_EQ(Type, N->getType());
1525 EXPECT_EQ(Arg, N->getArg());
1526 EXPECT_EQ(Flags, N->getFlags());
1527 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1528 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1529 Arg, Flags, InlinedAt));
1531 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1532 Name, File, Line, Type, Arg, Flags,
1534 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line,
1535 Type, Arg, Flags, InlinedAt));
1536 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1537 Type, Arg, Flags, InlinedAt));
1538 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line,
1539 Type, Arg, Flags, InlinedAt));
1540 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1541 Type, Arg, Flags, InlinedAt));
1542 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1543 Scope, Arg, Flags, InlinedAt));
1544 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1545 Arg + 1, Flags, InlinedAt));
1546 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1547 Arg, ~Flags, InlinedAt));
1548 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1549 Arg, Flags, Scope));
1551 TempMDLocalVariable Temp = N->clone();
1552 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1555 typedef MetadataTest MDExpressionTest;
1557 TEST_F(MDExpressionTest, get) {
1558 uint64_t Elements[] = {2, 6, 9, 78, 0};
1559 auto *N = MDExpression::get(Context, Elements);
1560 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1561 EXPECT_EQ(N, MDExpression::get(Context, Elements));
1563 EXPECT_EQ(5u, N->getNumElements());
1564 EXPECT_EQ(2u, N->getElement(0));
1565 EXPECT_EQ(6u, N->getElement(1));
1566 EXPECT_EQ(9u, N->getElement(2));
1567 EXPECT_EQ(78u, N->getElement(3));
1568 EXPECT_EQ(0u, N->getElement(4));
1570 TempMDExpression Temp = N->clone();
1571 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1574 TEST_F(MDExpressionTest, isValid) {
1575 #define EXPECT_VALID(...) \
1577 uint64_t Elements[] = {__VA_ARGS__}; \
1578 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1580 #define EXPECT_INVALID(...) \
1582 uint64_t Elements[] = {__VA_ARGS__}; \
1583 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1586 // Empty expression should be valid.
1587 EXPECT_TRUE(MDExpression::get(Context, None));
1589 // Valid constructions.
1590 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1591 EXPECT_VALID(dwarf::DW_OP_deref);
1592 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1593 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1594 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1595 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1596 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1598 // Invalid constructions.
1599 EXPECT_INVALID(~0u);
1600 EXPECT_INVALID(dwarf::DW_OP_plus);
1601 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1602 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1603 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1604 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1607 #undef EXPECT_INVALID
1610 typedef MetadataTest MDObjCPropertyTest;
1612 TEST_F(MDObjCPropertyTest, get) {
1613 StringRef Name = "name";
1614 Metadata *File = MDTuple::getDistinct(Context, None);
1616 StringRef GetterName = "getter";
1617 StringRef SetterName = "setter";
1618 unsigned Attributes = 7;
1619 Metadata *Type = MDTuple::getDistinct(Context, None);
1621 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1622 SetterName, Attributes, Type);
1624 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1625 EXPECT_EQ(Name, N->getName());
1626 EXPECT_EQ(File, N->getFile());
1627 EXPECT_EQ(Line, N->getLine());
1628 EXPECT_EQ(GetterName, N->getGetterName());
1629 EXPECT_EQ(SetterName, N->getSetterName());
1630 EXPECT_EQ(Attributes, N->getAttributes());
1631 EXPECT_EQ(Type, N->getType());
1632 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1633 SetterName, Attributes, Type));
1635 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1636 SetterName, Attributes, Type));
1637 EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1638 SetterName, Attributes, Type));
1639 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1640 SetterName, Attributes, Type));
1641 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1642 SetterName, Attributes, Type));
1643 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1644 "other", Attributes, Type));
1645 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1646 SetterName, Attributes + 1, Type));
1647 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1648 SetterName, Attributes, File));
1650 TempMDObjCProperty Temp = N->clone();
1651 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1654 typedef MetadataTest MDImportedEntityTest;
1656 TEST_F(MDImportedEntityTest, get) {
1657 unsigned Tag = dwarf::DW_TAG_imported_module;
1658 Metadata *Scope = MDTuple::getDistinct(Context, None);
1659 Metadata *Entity = MDTuple::getDistinct(Context, None);
1661 StringRef Name = "name";
1663 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1665 EXPECT_EQ(Tag, N->getTag());
1666 EXPECT_EQ(Scope, N->getScope());
1667 EXPECT_EQ(Entity, N->getEntity());
1668 EXPECT_EQ(Line, N->getLine());
1669 EXPECT_EQ(Name, N->getName());
1670 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1673 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1674 Scope, Entity, Line, Name));
1675 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1676 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1678 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1680 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
1682 TempMDImportedEntity Temp = N->clone();
1683 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1686 typedef MetadataTest MetadataAsValueTest;
1688 TEST_F(MetadataAsValueTest, MDNode) {
1689 MDNode *N = MDNode::get(Context, None);
1690 auto *V = MetadataAsValue::get(Context, N);
1691 EXPECT_TRUE(V->getType()->isMetadataTy());
1692 EXPECT_EQ(N, V->getMetadata());
1694 auto *V2 = MetadataAsValue::get(Context, N);
1698 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1699 MDNode *N = MDNode::get(Context, None);
1700 Metadata *Ops[] = {N};
1701 MDNode *N2 = MDNode::get(Context, Ops);
1702 auto *V = MetadataAsValue::get(Context, N2);
1703 EXPECT_TRUE(V->getType()->isMetadataTy());
1704 EXPECT_EQ(N2, V->getMetadata());
1706 auto *V2 = MetadataAsValue::get(Context, N2);
1709 auto *V3 = MetadataAsValue::get(Context, N);
1710 EXPECT_TRUE(V3->getType()->isMetadataTy());
1712 EXPECT_EQ(N, V3->getMetadata());
1715 TEST_F(MetadataAsValueTest, MDNodeConstant) {
1716 auto *C = ConstantInt::getTrue(Context);
1717 auto *MD = ConstantAsMetadata::get(C);
1718 Metadata *Ops[] = {MD};
1719 auto *N = MDNode::get(Context, Ops);
1721 auto *V = MetadataAsValue::get(Context, MD);
1722 EXPECT_TRUE(V->getType()->isMetadataTy());
1723 EXPECT_EQ(MD, V->getMetadata());
1725 auto *V2 = MetadataAsValue::get(Context, N);
1726 EXPECT_EQ(MD, V2->getMetadata());
1730 typedef MetadataTest ValueAsMetadataTest;
1732 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1733 Type *Ty = Type::getInt1PtrTy(Context);
1734 std::unique_ptr<GlobalVariable> GV0(
1735 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1736 auto *MD = ValueAsMetadata::get(GV0.get());
1737 EXPECT_TRUE(MD->getValue() == GV0.get());
1738 ASSERT_TRUE(GV0->use_empty());
1740 std::unique_ptr<GlobalVariable> GV1(
1741 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1742 GV0->replaceAllUsesWith(GV1.get());
1743 EXPECT_TRUE(MD->getValue() == GV1.get());
1746 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
1747 // Create a constant.
1748 ConstantAsMetadata *CI = ConstantAsMetadata::get(
1749 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
1751 // Create a temporary to prevent nodes from resolving.
1752 auto Temp = MDTuple::getTemporary(Context, None);
1754 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
1755 Metadata *Ops1[] = {CI, CI, Temp.get()};
1756 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
1758 auto *N1 = MDTuple::get(Context, Ops1);
1759 auto *N2 = MDTuple::get(Context, Ops2);
1762 // Tell metadata that the constant is getting deleted.
1764 // After this, N1 will be invalid, so don't touch it.
1765 ValueAsMetadata::handleDeletion(CI->getValue());
1766 EXPECT_EQ(nullptr, N2->getOperand(0));
1767 EXPECT_EQ(nullptr, N2->getOperand(1));
1768 EXPECT_EQ(Temp.get(), N2->getOperand(2));
1770 // Clean up Temp for teardown.
1771 Temp->replaceAllUsesWith(nullptr);
1774 typedef MetadataTest TrackingMDRefTest;
1776 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
1777 Type *Ty = Type::getInt1PtrTy(Context);
1778 std::unique_ptr<GlobalVariable> GV0(
1779 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1780 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
1781 EXPECT_TRUE(MD->getValue() == GV0.get());
1782 ASSERT_TRUE(GV0->use_empty());
1784 std::unique_ptr<GlobalVariable> GV1(
1785 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1786 GV0->replaceAllUsesWith(GV1.get());
1787 EXPECT_TRUE(MD->getValue() == GV1.get());
1789 // Reset it, so we don't inadvertently test deletion.
1793 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
1794 Type *Ty = Type::getInt1PtrTy(Context);
1795 std::unique_ptr<GlobalVariable> GV(
1796 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1797 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
1798 EXPECT_TRUE(MD->getValue() == GV.get());
1799 ASSERT_TRUE(GV->use_empty());
1805 TEST(NamedMDNodeTest, Search) {
1806 LLVMContext Context;
1807 ConstantAsMetadata *C =
1808 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
1809 ConstantAsMetadata *C2 =
1810 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
1812 Metadata *const V = C;
1813 Metadata *const V2 = C2;
1814 MDNode *n = MDNode::get(Context, V);
1815 MDNode *n2 = MDNode::get(Context, V2);
1817 Module M("MyModule", Context);
1818 const char *Name = "llvm.NMD1";
1819 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
1821 NMD->addOperand(n2);
1824 raw_string_ostream oss(Str);
1826 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",