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 TEST_F(MDSubrangeTest, getEmptyArray) {
674 auto *N = MDSubrange::get(Context, -1, 0);
675 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
676 EXPECT_EQ(-1, N->getCount());
677 EXPECT_EQ(0, N->getLo());
678 EXPECT_EQ(N, MDSubrange::get(Context, -1, 0));
681 typedef MetadataTest MDEnumeratorTest;
683 TEST_F(MDEnumeratorTest, get) {
684 auto *N = MDEnumerator::get(Context, 7, "name");
685 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
686 EXPECT_EQ(7, N->getValue());
687 EXPECT_EQ("name", N->getName());
688 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
690 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
691 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
693 TempMDEnumerator Temp = N->clone();
694 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
697 typedef MetadataTest MDBasicTypeTest;
699 TEST_F(MDBasicTypeTest, get) {
701 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
702 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
703 EXPECT_EQ("special", N->getName());
704 EXPECT_EQ(33u, N->getSizeInBits());
705 EXPECT_EQ(26u, N->getAlignInBits());
706 EXPECT_EQ(7u, N->getEncoding());
707 EXPECT_EQ(0u, N->getLine());
708 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
711 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
712 "special", 33, 26, 7));
714 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
715 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
717 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
719 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
722 TempMDBasicType Temp = N->clone();
723 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
726 TEST_F(MDBasicTypeTest, getWithLargeValues) {
727 auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
728 UINT64_MAX, UINT64_MAX - 1, 7);
729 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
730 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
733 typedef MetadataTest MDDerivedTypeTest;
735 TEST_F(MDDerivedTypeTest, get) {
736 Metadata *File = MDTuple::getDistinct(Context, None);
737 Metadata *Scope = MDTuple::getDistinct(Context, None);
738 Metadata *BaseType = MDTuple::getDistinct(Context, None);
739 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
741 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
742 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
743 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
744 EXPECT_EQ("something", N->getName());
745 EXPECT_EQ(File, N->getFile());
746 EXPECT_EQ(1u, N->getLine());
747 EXPECT_EQ(Scope, N->getScope());
748 EXPECT_EQ(BaseType, N->getBaseType());
749 EXPECT_EQ(2u, N->getSizeInBits());
750 EXPECT_EQ(3u, N->getAlignInBits());
751 EXPECT_EQ(4u, N->getOffsetInBits());
752 EXPECT_EQ(5u, N->getFlags());
753 EXPECT_EQ(ExtraData, N->getExtraData());
754 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
755 "something", File, 1, Scope, BaseType, 2, 3,
758 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
759 "something", File, 1, Scope, BaseType, 2, 3,
761 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
762 File, 1, Scope, BaseType, 2, 3, 4, 5,
764 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
765 "something", Scope, 1, Scope, BaseType, 2, 3,
767 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
768 "something", File, 2, Scope, BaseType, 2, 3,
771 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
772 File, 1, File, BaseType, 2, 3, 4, 5, ExtraData));
774 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
775 File, 1, Scope, File, 2, 3, 4, 5, ExtraData));
776 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
777 "something", File, 1, Scope, BaseType, 3, 3,
779 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
780 "something", File, 1, Scope, BaseType, 2, 2,
782 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
783 "something", File, 1, Scope, BaseType, 2, 3,
785 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
786 "something", File, 1, Scope, BaseType, 2, 3,
789 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
790 File, 1, Scope, BaseType, 2, 3, 4, 5, File));
792 TempMDDerivedType Temp = N->clone();
793 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
796 TEST_F(MDDerivedTypeTest, getWithLargeValues) {
797 Metadata *File = MDTuple::getDistinct(Context, None);
798 Metadata *Scope = MDTuple::getDistinct(Context, None);
799 Metadata *BaseType = MDTuple::getDistinct(Context, None);
800 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
802 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
803 File, 1, Scope, BaseType, UINT64_MAX,
804 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
805 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
806 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
807 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
810 typedef MetadataTest MDCompositeTypeTest;
812 TEST_F(MDCompositeTypeTest, get) {
813 unsigned Tag = dwarf::DW_TAG_structure_type;
814 StringRef Name = "some name";
815 Metadata *File = MDTuple::getDistinct(Context, None);
817 Metadata *Scope = MDTuple::getDistinct(Context, None);
818 Metadata *BaseType = MDTuple::getDistinct(Context, None);
819 uint64_t SizeInBits = 2;
820 uint64_t AlignInBits = 3;
821 uint64_t OffsetInBits = 4;
823 Metadata *Elements = MDTuple::getDistinct(Context, None);
824 unsigned RuntimeLang = 6;
825 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
826 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
827 StringRef Identifier = "some id";
829 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
830 BaseType, SizeInBits, AlignInBits,
831 OffsetInBits, Flags, Elements, RuntimeLang,
832 VTableHolder, TemplateParams, Identifier);
833 EXPECT_EQ(Tag, N->getTag());
834 EXPECT_EQ(Name, N->getName());
835 EXPECT_EQ(File, N->getFile());
836 EXPECT_EQ(Line, N->getLine());
837 EXPECT_EQ(Scope, N->getScope());
838 EXPECT_EQ(BaseType, N->getBaseType());
839 EXPECT_EQ(SizeInBits, N->getSizeInBits());
840 EXPECT_EQ(AlignInBits, N->getAlignInBits());
841 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
842 EXPECT_EQ(Flags, N->getFlags());
843 EXPECT_EQ(Elements, N->getElements());
844 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
845 EXPECT_EQ(VTableHolder, N->getVTableHolder());
846 EXPECT_EQ(TemplateParams, N->getTemplateParams());
847 EXPECT_EQ(Identifier, N->getIdentifier());
849 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
850 BaseType, SizeInBits, AlignInBits,
851 OffsetInBits, Flags, Elements, RuntimeLang,
852 VTableHolder, TemplateParams, Identifier));
854 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
855 BaseType, SizeInBits, AlignInBits,
856 OffsetInBits, Flags, Elements, RuntimeLang,
857 VTableHolder, TemplateParams, Identifier));
858 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
859 BaseType, SizeInBits, AlignInBits,
860 OffsetInBits, Flags, Elements, RuntimeLang,
861 VTableHolder, TemplateParams, Identifier));
862 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, Scope, Line, Scope,
863 BaseType, SizeInBits, AlignInBits,
864 OffsetInBits, Flags, Elements, RuntimeLang,
865 VTableHolder, TemplateParams, Identifier));
866 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
867 BaseType, SizeInBits, AlignInBits,
868 OffsetInBits, Flags, Elements, RuntimeLang,
869 VTableHolder, TemplateParams, Identifier));
870 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, File,
871 BaseType, SizeInBits, AlignInBits,
872 OffsetInBits, Flags, Elements, RuntimeLang,
873 VTableHolder, TemplateParams, Identifier));
874 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
875 SizeInBits, AlignInBits, OffsetInBits,
876 Flags, Elements, RuntimeLang, VTableHolder,
877 TemplateParams, Identifier));
878 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
879 BaseType, SizeInBits + 1, AlignInBits,
880 OffsetInBits, Flags, Elements, RuntimeLang,
881 VTableHolder, TemplateParams, Identifier));
882 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
883 BaseType, SizeInBits, AlignInBits + 1,
884 OffsetInBits, Flags, Elements, RuntimeLang,
885 VTableHolder, TemplateParams, Identifier));
886 EXPECT_NE(N, MDCompositeType::get(
887 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
888 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
889 VTableHolder, TemplateParams, Identifier));
890 EXPECT_NE(N, MDCompositeType::get(
891 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
892 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
893 VTableHolder, TemplateParams, Identifier));
894 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
895 BaseType, SizeInBits, AlignInBits,
896 OffsetInBits, Flags, File, RuntimeLang,
897 VTableHolder, TemplateParams, Identifier));
898 EXPECT_NE(N, MDCompositeType::get(
899 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
900 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
901 VTableHolder, TemplateParams, Identifier));
902 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
903 BaseType, SizeInBits, AlignInBits,
904 OffsetInBits, Flags, Elements, RuntimeLang,
905 File, TemplateParams, Identifier));
906 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
907 BaseType, SizeInBits, AlignInBits,
908 OffsetInBits, Flags, Elements, RuntimeLang,
909 VTableHolder, File, Identifier));
910 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
911 BaseType, SizeInBits, AlignInBits,
912 OffsetInBits, Flags, Elements, RuntimeLang,
913 VTableHolder, TemplateParams, "other"));
915 // Be sure that missing identifiers get null pointers.
916 EXPECT_FALSE(MDCompositeType::get(
917 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
918 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
919 VTableHolder, TemplateParams, "")->getRawIdentifier());
920 EXPECT_FALSE(MDCompositeType::get(
921 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
922 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
923 VTableHolder, TemplateParams)->getRawIdentifier());
925 TempMDCompositeType Temp = N->clone();
926 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
929 TEST_F(MDCompositeTypeTest, getWithLargeValues) {
930 unsigned Tag = dwarf::DW_TAG_structure_type;
931 StringRef Name = "some name";
932 Metadata *File = MDTuple::getDistinct(Context, None);
934 Metadata *Scope = MDTuple::getDistinct(Context, None);
935 Metadata *BaseType = MDTuple::getDistinct(Context, None);
936 uint64_t SizeInBits = UINT64_MAX;
937 uint64_t AlignInBits = UINT64_MAX - 1;
938 uint64_t OffsetInBits = UINT64_MAX - 2;
940 Metadata *Elements = MDTuple::getDistinct(Context, None);
941 unsigned RuntimeLang = 6;
942 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
943 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
944 StringRef Identifier = "some id";
946 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
947 BaseType, SizeInBits, AlignInBits,
948 OffsetInBits, Flags, Elements, RuntimeLang,
949 VTableHolder, TemplateParams, Identifier);
950 EXPECT_EQ(SizeInBits, N->getSizeInBits());
951 EXPECT_EQ(AlignInBits, N->getAlignInBits());
952 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
955 TEST_F(MDCompositeTypeTest, replaceOperands) {
956 unsigned Tag = dwarf::DW_TAG_structure_type;
957 StringRef Name = "some name";
958 Metadata *File = MDTuple::getDistinct(Context, None);
960 Metadata *Scope = MDTuple::getDistinct(Context, None);
961 Metadata *BaseType = MDTuple::getDistinct(Context, None);
962 uint64_t SizeInBits = 2;
963 uint64_t AlignInBits = 3;
964 uint64_t OffsetInBits = 4;
966 unsigned RuntimeLang = 6;
967 StringRef Identifier = "some id";
969 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
970 BaseType, SizeInBits, AlignInBits,
971 OffsetInBits, Flags, nullptr, RuntimeLang,
972 nullptr, nullptr, Identifier);
974 auto *Elements = MDTuple::getDistinct(Context, None);
975 EXPECT_EQ(nullptr, N->getElements());
976 N->replaceElements(Elements);
977 EXPECT_EQ(Elements, N->getElements());
978 N->replaceElements(nullptr);
979 EXPECT_EQ(nullptr, N->getElements());
981 auto *VTableHolder = MDTuple::getDistinct(Context, None);
982 EXPECT_EQ(nullptr, N->getVTableHolder());
983 N->replaceVTableHolder(VTableHolder);
984 EXPECT_EQ(VTableHolder, N->getVTableHolder());
985 N->replaceVTableHolder(nullptr);
986 EXPECT_EQ(nullptr, N->getVTableHolder());
988 auto *TemplateParams = MDTuple::getDistinct(Context, None);
989 EXPECT_EQ(nullptr, N->getTemplateParams());
990 N->replaceTemplateParams(TemplateParams);
991 EXPECT_EQ(TemplateParams, N->getTemplateParams());
992 N->replaceTemplateParams(nullptr);
993 EXPECT_EQ(nullptr, N->getTemplateParams());
996 typedef MetadataTest MDSubroutineTypeTest;
998 TEST_F(MDSubroutineTypeTest, get) {
1000 Metadata *TypeArray = MDTuple::getDistinct(Context, None);
1002 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
1003 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1004 EXPECT_EQ(Flags, N->getFlags());
1005 EXPECT_EQ(TypeArray, N->getTypeArray());
1006 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
1008 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
1009 EXPECT_NE(N, MDSubroutineType::get(Context, Flags,
1010 MDTuple::getDistinct(Context, None)));
1012 TempMDSubroutineType Temp = N->clone();
1013 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1015 // Test always-empty operands.
1016 EXPECT_EQ(nullptr, N->getScope());
1017 EXPECT_EQ(nullptr, N->getFile());
1018 EXPECT_EQ("", N->getName());
1019 EXPECT_EQ(nullptr, N->getBaseType());
1020 EXPECT_EQ(nullptr, N->getVTableHolder());
1021 EXPECT_EQ(nullptr, N->getTemplateParams());
1022 EXPECT_EQ("", N->getIdentifier());
1025 typedef MetadataTest MDFileTest;
1027 TEST_F(MDFileTest, get) {
1028 StringRef Filename = "file";
1029 StringRef Directory = "dir";
1030 auto *N = MDFile::get(Context, Filename, Directory);
1032 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1033 EXPECT_EQ(Filename, N->getFilename());
1034 EXPECT_EQ(Directory, N->getDirectory());
1035 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
1037 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1038 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
1040 TempMDFile Temp = N->clone();
1041 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1044 typedef MetadataTest MDCompileUnitTest;
1046 TEST_F(MDCompileUnitTest, get) {
1047 unsigned SourceLanguage = 1;
1048 Metadata *File = MDTuple::getDistinct(Context, None);
1049 StringRef Producer = "some producer";
1050 bool IsOptimized = false;
1051 StringRef Flags = "flag after flag";
1052 unsigned RuntimeVersion = 2;
1053 StringRef SplitDebugFilename = "another/file";
1054 unsigned EmissionKind = 3;
1055 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1056 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1057 Metadata *Subprograms = MDTuple::getDistinct(Context, None);
1058 Metadata *GlobalVariables = MDTuple::getDistinct(Context, None);
1059 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1060 auto *N = MDCompileUnit::get(
1061 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1062 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1063 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1065 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1066 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1067 EXPECT_EQ(File, N->getFile());
1068 EXPECT_EQ(Producer, N->getProducer());
1069 EXPECT_EQ(IsOptimized, N->isOptimized());
1070 EXPECT_EQ(Flags, N->getFlags());
1071 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1072 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1073 EXPECT_EQ(EmissionKind, N->getEmissionKind());
1074 EXPECT_EQ(EnumTypes, N->getEnumTypes());
1075 EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
1076 EXPECT_EQ(Subprograms, N->getSubprograms());
1077 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1078 EXPECT_EQ(ImportedEntities, N->getImportedEntities());
1079 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1080 IsOptimized, Flags, RuntimeVersion,
1081 SplitDebugFilename, EmissionKind, EnumTypes,
1082 RetainedTypes, Subprograms, GlobalVariables,
1085 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1086 IsOptimized, Flags, RuntimeVersion,
1087 SplitDebugFilename, EmissionKind, EnumTypes,
1088 RetainedTypes, Subprograms, GlobalVariables,
1090 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer,
1091 IsOptimized, Flags, RuntimeVersion,
1092 SplitDebugFilename, EmissionKind, EnumTypes,
1093 RetainedTypes, Subprograms, GlobalVariables,
1095 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1096 IsOptimized, Flags, RuntimeVersion,
1097 SplitDebugFilename, EmissionKind, EnumTypes,
1098 RetainedTypes, Subprograms, GlobalVariables,
1100 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1101 !IsOptimized, Flags, RuntimeVersion,
1102 SplitDebugFilename, EmissionKind, EnumTypes,
1103 RetainedTypes, Subprograms, GlobalVariables,
1105 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1106 IsOptimized, "other", RuntimeVersion,
1107 SplitDebugFilename, EmissionKind, EnumTypes,
1108 RetainedTypes, Subprograms, GlobalVariables,
1110 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1111 IsOptimized, Flags, RuntimeVersion + 1,
1112 SplitDebugFilename, EmissionKind, EnumTypes,
1113 RetainedTypes, Subprograms, GlobalVariables,
1116 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1117 IsOptimized, Flags, RuntimeVersion, "other",
1118 EmissionKind, EnumTypes, RetainedTypes,
1119 Subprograms, GlobalVariables, ImportedEntities));
1120 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1121 IsOptimized, Flags, RuntimeVersion,
1122 SplitDebugFilename, EmissionKind + 1,
1123 EnumTypes, RetainedTypes, Subprograms,
1124 GlobalVariables, ImportedEntities));
1125 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1126 IsOptimized, Flags, RuntimeVersion,
1127 SplitDebugFilename, EmissionKind, File,
1128 RetainedTypes, Subprograms, GlobalVariables,
1130 EXPECT_NE(N, MDCompileUnit::get(
1131 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1132 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1133 File, Subprograms, GlobalVariables, ImportedEntities));
1134 EXPECT_NE(N, MDCompileUnit::get(
1135 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1136 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1137 RetainedTypes, File, GlobalVariables, ImportedEntities));
1138 EXPECT_NE(N, MDCompileUnit::get(
1139 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1140 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1141 RetainedTypes, Subprograms, File, ImportedEntities));
1142 EXPECT_NE(N, MDCompileUnit::get(
1143 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1144 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1145 RetainedTypes, Subprograms, GlobalVariables, File));
1147 TempMDCompileUnit Temp = N->clone();
1148 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1151 TEST_F(MDCompileUnitTest, replaceArrays) {
1152 unsigned SourceLanguage = 1;
1153 Metadata *File = MDTuple::getDistinct(Context, None);
1154 StringRef Producer = "some producer";
1155 bool IsOptimized = false;
1156 StringRef Flags = "flag after flag";
1157 unsigned RuntimeVersion = 2;
1158 StringRef SplitDebugFilename = "another/file";
1159 unsigned EmissionKind = 3;
1160 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1161 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1162 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1163 auto *N = MDCompileUnit::get(
1164 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1165 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1166 RetainedTypes, nullptr, nullptr, ImportedEntities);
1168 auto *Subprograms = MDTuple::getDistinct(Context, None);
1169 EXPECT_EQ(nullptr, N->getSubprograms());
1170 N->replaceSubprograms(Subprograms);
1171 EXPECT_EQ(Subprograms, N->getSubprograms());
1172 N->replaceSubprograms(nullptr);
1173 EXPECT_EQ(nullptr, N->getSubprograms());
1175 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1176 EXPECT_EQ(nullptr, N->getGlobalVariables());
1177 N->replaceGlobalVariables(GlobalVariables);
1178 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1179 N->replaceGlobalVariables(nullptr);
1180 EXPECT_EQ(nullptr, N->getGlobalVariables());
1183 typedef MetadataTest MDSubprogramTest;
1185 TEST_F(MDSubprogramTest, get) {
1186 Metadata *Scope = MDTuple::getDistinct(Context, None);
1187 StringRef Name = "name";
1188 StringRef LinkageName = "linkage";
1189 Metadata *File = MDTuple::getDistinct(Context, None);
1191 Metadata *Type = MDTuple::getDistinct(Context, None);
1192 bool IsLocalToUnit = false;
1193 bool IsDefinition = true;
1194 unsigned ScopeLine = 3;
1195 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1196 unsigned Virtuality = 4;
1197 unsigned VirtualIndex = 5;
1199 bool IsOptimized = false;
1200 Metadata *Function = MDTuple::getDistinct(Context, None);
1201 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1202 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1203 Metadata *Variables = MDTuple::getDistinct(Context, None);
1205 auto *N = MDSubprogram::get(
1206 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1207 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1208 IsOptimized, Function, TemplateParams, Declaration, Variables);
1210 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1211 EXPECT_EQ(Scope, N->getScope());
1212 EXPECT_EQ(Name, N->getName());
1213 EXPECT_EQ(LinkageName, N->getLinkageName());
1214 EXPECT_EQ(File, N->getFile());
1215 EXPECT_EQ(Line, N->getLine());
1216 EXPECT_EQ(Type, N->getType());
1217 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1218 EXPECT_EQ(IsDefinition, N->isDefinition());
1219 EXPECT_EQ(ScopeLine, N->getScopeLine());
1220 EXPECT_EQ(ContainingType, N->getContainingType());
1221 EXPECT_EQ(Virtuality, N->getVirtuality());
1222 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1223 EXPECT_EQ(Flags, N->getFlags());
1224 EXPECT_EQ(IsOptimized, N->isOptimized());
1225 EXPECT_EQ(Function, N->getFunction());
1226 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1227 EXPECT_EQ(Declaration, N->getDeclaration());
1228 EXPECT_EQ(Variables, N->getVariables());
1229 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1230 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1231 ContainingType, Virtuality, VirtualIndex,
1232 Flags, IsOptimized, Function, TemplateParams,
1233 Declaration, Variables));
1235 EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1236 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1237 ContainingType, Virtuality, VirtualIndex,
1238 Flags, IsOptimized, Function, TemplateParams,
1239 Declaration, Variables));
1240 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1241 Line, Type, IsLocalToUnit, IsDefinition,
1242 ScopeLine, ContainingType, Virtuality,
1243 VirtualIndex, Flags, IsOptimized, Function,
1244 TemplateParams, Declaration, Variables));
1245 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1246 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1247 ContainingType, Virtuality, VirtualIndex,
1248 Flags, IsOptimized, Function, TemplateParams,
1249 Declaration, Variables));
1250 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1251 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1252 ContainingType, Virtuality, VirtualIndex,
1253 Flags, IsOptimized, Function, TemplateParams,
1254 Declaration, Variables));
1255 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1256 Line + 1, Type, IsLocalToUnit, IsDefinition,
1257 ScopeLine, ContainingType, Virtuality,
1258 VirtualIndex, Flags, IsOptimized, Function,
1259 TemplateParams, Declaration, Variables));
1260 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1261 Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1262 ContainingType, Virtuality, VirtualIndex,
1263 Flags, IsOptimized, Function, TemplateParams,
1264 Declaration, Variables));
1265 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1266 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1267 ContainingType, Virtuality, VirtualIndex,
1268 Flags, IsOptimized, Function, TemplateParams,
1269 Declaration, Variables));
1270 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1271 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1272 ContainingType, Virtuality, VirtualIndex,
1273 Flags, IsOptimized, Function, TemplateParams,
1274 Declaration, Variables));
1275 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1276 Type, IsLocalToUnit, IsDefinition,
1277 ScopeLine + 1, ContainingType, Virtuality,
1278 VirtualIndex, Flags, IsOptimized, Function,
1279 TemplateParams, Declaration, Variables));
1280 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1281 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1282 Type, Virtuality, VirtualIndex, Flags,
1283 IsOptimized, Function, TemplateParams,
1284 Declaration, Variables));
1285 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1286 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1287 ContainingType, Virtuality + 1, VirtualIndex,
1288 Flags, IsOptimized, Function, TemplateParams,
1289 Declaration, Variables));
1290 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1291 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1292 ContainingType, Virtuality, VirtualIndex + 1,
1293 Flags, IsOptimized, Function, TemplateParams,
1294 Declaration, Variables));
1295 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1296 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1297 ContainingType, Virtuality, VirtualIndex,
1298 ~Flags, IsOptimized, Function, TemplateParams,
1299 Declaration, Variables));
1300 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1301 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1302 ContainingType, Virtuality, VirtualIndex,
1303 Flags, !IsOptimized, Function, TemplateParams,
1304 Declaration, Variables));
1305 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1306 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1307 ContainingType, Virtuality, VirtualIndex,
1308 Flags, IsOptimized, Type, TemplateParams,
1309 Declaration, Variables));
1310 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1311 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1312 ContainingType, Virtuality, VirtualIndex,
1313 Flags, IsOptimized, Function, Type,
1314 Declaration, Variables));
1315 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1316 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1317 ContainingType, Virtuality, VirtualIndex,
1318 Flags, IsOptimized, Function, TemplateParams,
1320 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1321 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1322 ContainingType, Virtuality, VirtualIndex,
1323 Flags, IsOptimized, Function, TemplateParams,
1324 Declaration, Type));
1326 TempMDSubprogram Temp = N->clone();
1327 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1330 TEST_F(MDSubprogramTest, replaceFunction) {
1331 Metadata *Scope = MDTuple::getDistinct(Context, None);
1332 StringRef Name = "name";
1333 StringRef LinkageName = "linkage";
1334 Metadata *File = MDTuple::getDistinct(Context, None);
1336 Metadata *Type = MDTuple::getDistinct(Context, None);
1337 bool IsLocalToUnit = false;
1338 bool IsDefinition = true;
1339 unsigned ScopeLine = 3;
1340 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1341 unsigned Virtuality = 4;
1342 unsigned VirtualIndex = 5;
1344 bool IsOptimized = false;
1345 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1346 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1347 Metadata *Variables = MDTuple::getDistinct(Context, None);
1349 auto *N = MDSubprogram::get(
1350 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1351 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1352 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1354 EXPECT_EQ(nullptr, N->getFunction());
1356 std::unique_ptr<Function> F(
1357 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1358 GlobalValue::ExternalLinkage));
1359 N->replaceFunction(F.get());
1360 EXPECT_EQ(ConstantAsMetadata::get(F.get()), N->getFunction());
1362 N->replaceFunction(nullptr);
1363 EXPECT_EQ(nullptr, N->getFunction());
1366 typedef MetadataTest MDLexicalBlockTest;
1368 TEST_F(MDLexicalBlockTest, get) {
1369 Metadata *Scope = MDTuple::getDistinct(Context, None);
1370 Metadata *File = MDTuple::getDistinct(Context, None);
1372 unsigned Column = 8;
1374 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1376 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1377 EXPECT_EQ(Scope, N->getScope());
1378 EXPECT_EQ(File, N->getFile());
1379 EXPECT_EQ(Line, N->getLine());
1380 EXPECT_EQ(Column, N->getColumn());
1381 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1383 EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1384 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1385 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1386 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
1388 TempMDLexicalBlock Temp = N->clone();
1389 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1392 typedef MetadataTest MDLexicalBlockFileTest;
1394 TEST_F(MDLexicalBlockFileTest, get) {
1395 Metadata *Scope = MDTuple::getDistinct(Context, None);
1396 Metadata *File = MDTuple::getDistinct(Context, None);
1397 unsigned Discriminator = 5;
1399 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1401 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1402 EXPECT_EQ(Scope, N->getScope());
1403 EXPECT_EQ(File, N->getFile());
1404 EXPECT_EQ(Discriminator, N->getDiscriminator());
1405 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1407 EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1408 EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1410 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1412 TempMDLexicalBlockFile Temp = N->clone();
1413 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1416 typedef MetadataTest MDNamespaceTest;
1418 TEST_F(MDNamespaceTest, get) {
1419 Metadata *Scope = MDTuple::getDistinct(Context, None);
1420 Metadata *File = MDTuple::getDistinct(Context, None);
1421 StringRef Name = "namespace";
1424 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1426 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1427 EXPECT_EQ(Scope, N->getScope());
1428 EXPECT_EQ(File, N->getFile());
1429 EXPECT_EQ(Name, N->getName());
1430 EXPECT_EQ(Line, N->getLine());
1431 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1433 EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1434 EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1435 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1436 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
1438 TempMDNamespace Temp = N->clone();
1439 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1442 typedef MetadataTest MDTemplateTypeParameterTest;
1444 TEST_F(MDTemplateTypeParameterTest, get) {
1445 StringRef Name = "template";
1446 Metadata *Type = MDTuple::getDistinct(Context, None);
1447 Metadata *Other = MDTuple::getDistinct(Context, None);
1449 auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
1451 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1452 EXPECT_EQ(Name, N->getName());
1453 EXPECT_EQ(Type, N->getType());
1454 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type));
1456 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
1457 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Name, Other));
1459 TempMDTemplateTypeParameter Temp = N->clone();
1460 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1463 typedef MetadataTest MDTemplateValueParameterTest;
1465 TEST_F(MDTemplateValueParameterTest, get) {
1466 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1467 StringRef Name = "template";
1468 Metadata *Type = MDTuple::getDistinct(Context, None);
1469 Metadata *Value = MDTuple::getDistinct(Context, None);
1470 Metadata *Other = MDTuple::getDistinct(Context, None);
1472 auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
1473 EXPECT_EQ(Tag, N->getTag());
1474 EXPECT_EQ(Name, N->getName());
1475 EXPECT_EQ(Type, N->getType());
1476 EXPECT_EQ(Value, N->getValue());
1477 EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value));
1479 EXPECT_NE(N, MDTemplateValueParameter::get(
1480 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1482 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type,
1484 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Other,
1486 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Other));
1488 TempMDTemplateValueParameter Temp = N->clone();
1489 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1492 typedef MetadataTest MDGlobalVariableTest;
1494 TEST_F(MDGlobalVariableTest, get) {
1495 Metadata *Scope = MDTuple::getDistinct(Context, None);
1496 StringRef Name = "name";
1497 StringRef LinkageName = "linkage";
1498 Metadata *File = MDTuple::getDistinct(Context, None);
1500 Metadata *Type = MDTuple::getDistinct(Context, None);
1501 bool IsLocalToUnit = false;
1502 bool IsDefinition = true;
1503 Metadata *Variable = MDTuple::getDistinct(Context, None);
1504 Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None);
1506 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1507 Type, IsLocalToUnit, IsDefinition, Variable,
1508 StaticDataMemberDeclaration);
1509 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1510 EXPECT_EQ(Scope, N->getScope());
1511 EXPECT_EQ(Name, N->getName());
1512 EXPECT_EQ(LinkageName, N->getLinkageName());
1513 EXPECT_EQ(File, N->getFile());
1514 EXPECT_EQ(Line, N->getLine());
1515 EXPECT_EQ(Type, N->getType());
1516 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1517 EXPECT_EQ(IsDefinition, N->isDefinition());
1518 EXPECT_EQ(Variable, N->getVariable());
1519 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1520 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1521 Line, Type, IsLocalToUnit, IsDefinition,
1522 Variable, StaticDataMemberDeclaration));
1524 EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File,
1525 Line, Type, IsLocalToUnit, IsDefinition,
1526 Variable, StaticDataMemberDeclaration));
1527 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1528 Line, Type, IsLocalToUnit, IsDefinition,
1529 Variable, StaticDataMemberDeclaration));
1530 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1531 Type, IsLocalToUnit, IsDefinition,
1532 Variable, StaticDataMemberDeclaration));
1533 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope,
1534 Line, Type, IsLocalToUnit, IsDefinition,
1535 Variable, StaticDataMemberDeclaration));
1537 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1538 Line + 1, Type, IsLocalToUnit, IsDefinition,
1539 Variable, StaticDataMemberDeclaration));
1540 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1541 Line, Scope, IsLocalToUnit, IsDefinition,
1542 Variable, StaticDataMemberDeclaration));
1543 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1544 Line, Type, !IsLocalToUnit, IsDefinition,
1545 Variable, StaticDataMemberDeclaration));
1546 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1547 Line, Type, IsLocalToUnit, !IsDefinition,
1548 Variable, StaticDataMemberDeclaration));
1549 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1550 Line, Type, IsLocalToUnit, IsDefinition,
1551 Type, StaticDataMemberDeclaration));
1552 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1553 Line, Type, IsLocalToUnit, IsDefinition,
1556 TempMDGlobalVariable Temp = N->clone();
1557 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1560 typedef MetadataTest MDLocalVariableTest;
1562 TEST_F(MDLocalVariableTest, get) {
1563 unsigned Tag = dwarf::DW_TAG_arg_variable;
1564 Metadata *Scope = MDTuple::getDistinct(Context, None);
1565 StringRef Name = "name";
1566 Metadata *File = MDTuple::getDistinct(Context, None);
1568 Metadata *Type = MDTuple::getDistinct(Context, None);
1571 Metadata *InlinedAt = MDTuple::getDistinct(Context, None);
1573 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1574 Arg, Flags, InlinedAt);
1575 EXPECT_EQ(Tag, N->getTag());
1576 EXPECT_EQ(Scope, N->getScope());
1577 EXPECT_EQ(Name, N->getName());
1578 EXPECT_EQ(File, N->getFile());
1579 EXPECT_EQ(Line, N->getLine());
1580 EXPECT_EQ(Type, N->getType());
1581 EXPECT_EQ(Arg, N->getArg());
1582 EXPECT_EQ(Flags, N->getFlags());
1583 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1584 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1585 Arg, Flags, InlinedAt));
1587 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1588 Name, File, Line, Type, Arg, Flags,
1590 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line,
1591 Type, Arg, Flags, InlinedAt));
1592 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1593 Type, Arg, Flags, InlinedAt));
1594 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line,
1595 Type, Arg, Flags, InlinedAt));
1596 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1597 Type, Arg, Flags, InlinedAt));
1598 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1599 Scope, Arg, Flags, InlinedAt));
1600 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1601 Arg + 1, Flags, InlinedAt));
1602 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1603 Arg, ~Flags, InlinedAt));
1604 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1605 Arg, Flags, Scope));
1607 TempMDLocalVariable Temp = N->clone();
1608 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1611 typedef MetadataTest MDExpressionTest;
1613 TEST_F(MDExpressionTest, get) {
1614 uint64_t Elements[] = {2, 6, 9, 78, 0};
1615 auto *N = MDExpression::get(Context, Elements);
1616 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1617 EXPECT_EQ(N, MDExpression::get(Context, Elements));
1619 EXPECT_EQ(5u, N->getNumElements());
1620 EXPECT_EQ(2u, N->getElement(0));
1621 EXPECT_EQ(6u, N->getElement(1));
1622 EXPECT_EQ(9u, N->getElement(2));
1623 EXPECT_EQ(78u, N->getElement(3));
1624 EXPECT_EQ(0u, N->getElement(4));
1626 TempMDExpression Temp = N->clone();
1627 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1630 TEST_F(MDExpressionTest, isValid) {
1631 #define EXPECT_VALID(...) \
1633 uint64_t Elements[] = {__VA_ARGS__}; \
1634 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1636 #define EXPECT_INVALID(...) \
1638 uint64_t Elements[] = {__VA_ARGS__}; \
1639 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1642 // Empty expression should be valid.
1643 EXPECT_TRUE(MDExpression::get(Context, None));
1645 // Valid constructions.
1646 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1647 EXPECT_VALID(dwarf::DW_OP_deref);
1648 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1649 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1650 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1651 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1652 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1654 // Invalid constructions.
1655 EXPECT_INVALID(~0u);
1656 EXPECT_INVALID(dwarf::DW_OP_plus);
1657 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1658 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1659 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1660 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1663 #undef EXPECT_INVALID
1666 typedef MetadataTest MDObjCPropertyTest;
1668 TEST_F(MDObjCPropertyTest, get) {
1669 StringRef Name = "name";
1670 Metadata *File = MDTuple::getDistinct(Context, None);
1672 StringRef GetterName = "getter";
1673 StringRef SetterName = "setter";
1674 unsigned Attributes = 7;
1675 Metadata *Type = MDTuple::getDistinct(Context, None);
1677 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1678 SetterName, Attributes, Type);
1680 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1681 EXPECT_EQ(Name, N->getName());
1682 EXPECT_EQ(File, N->getFile());
1683 EXPECT_EQ(Line, N->getLine());
1684 EXPECT_EQ(GetterName, N->getGetterName());
1685 EXPECT_EQ(SetterName, N->getSetterName());
1686 EXPECT_EQ(Attributes, N->getAttributes());
1687 EXPECT_EQ(Type, N->getType());
1688 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1689 SetterName, Attributes, Type));
1691 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1692 SetterName, Attributes, Type));
1693 EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1694 SetterName, Attributes, Type));
1695 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1696 SetterName, Attributes, Type));
1697 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1698 SetterName, Attributes, Type));
1699 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1700 "other", Attributes, Type));
1701 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1702 SetterName, Attributes + 1, Type));
1703 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1704 SetterName, Attributes, File));
1706 TempMDObjCProperty Temp = N->clone();
1707 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1710 typedef MetadataTest MDImportedEntityTest;
1712 TEST_F(MDImportedEntityTest, get) {
1713 unsigned Tag = dwarf::DW_TAG_imported_module;
1714 Metadata *Scope = MDTuple::getDistinct(Context, None);
1715 Metadata *Entity = MDTuple::getDistinct(Context, None);
1717 StringRef Name = "name";
1719 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1721 EXPECT_EQ(Tag, N->getTag());
1722 EXPECT_EQ(Scope, N->getScope());
1723 EXPECT_EQ(Entity, N->getEntity());
1724 EXPECT_EQ(Line, N->getLine());
1725 EXPECT_EQ(Name, N->getName());
1726 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1729 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1730 Scope, Entity, Line, Name));
1731 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1732 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1734 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1736 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
1738 TempMDImportedEntity Temp = N->clone();
1739 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1742 typedef MetadataTest MetadataAsValueTest;
1744 TEST_F(MetadataAsValueTest, MDNode) {
1745 MDNode *N = MDNode::get(Context, None);
1746 auto *V = MetadataAsValue::get(Context, N);
1747 EXPECT_TRUE(V->getType()->isMetadataTy());
1748 EXPECT_EQ(N, V->getMetadata());
1750 auto *V2 = MetadataAsValue::get(Context, N);
1754 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1755 MDNode *N = MDNode::get(Context, None);
1756 Metadata *Ops[] = {N};
1757 MDNode *N2 = MDNode::get(Context, Ops);
1758 auto *V = MetadataAsValue::get(Context, N2);
1759 EXPECT_TRUE(V->getType()->isMetadataTy());
1760 EXPECT_EQ(N2, V->getMetadata());
1762 auto *V2 = MetadataAsValue::get(Context, N2);
1765 auto *V3 = MetadataAsValue::get(Context, N);
1766 EXPECT_TRUE(V3->getType()->isMetadataTy());
1768 EXPECT_EQ(N, V3->getMetadata());
1771 TEST_F(MetadataAsValueTest, MDNodeConstant) {
1772 auto *C = ConstantInt::getTrue(Context);
1773 auto *MD = ConstantAsMetadata::get(C);
1774 Metadata *Ops[] = {MD};
1775 auto *N = MDNode::get(Context, Ops);
1777 auto *V = MetadataAsValue::get(Context, MD);
1778 EXPECT_TRUE(V->getType()->isMetadataTy());
1779 EXPECT_EQ(MD, V->getMetadata());
1781 auto *V2 = MetadataAsValue::get(Context, N);
1782 EXPECT_EQ(MD, V2->getMetadata());
1786 typedef MetadataTest ValueAsMetadataTest;
1788 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1789 Type *Ty = Type::getInt1PtrTy(Context);
1790 std::unique_ptr<GlobalVariable> GV0(
1791 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1792 auto *MD = ValueAsMetadata::get(GV0.get());
1793 EXPECT_TRUE(MD->getValue() == GV0.get());
1794 ASSERT_TRUE(GV0->use_empty());
1796 std::unique_ptr<GlobalVariable> GV1(
1797 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1798 GV0->replaceAllUsesWith(GV1.get());
1799 EXPECT_TRUE(MD->getValue() == GV1.get());
1802 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
1803 // Create a constant.
1804 ConstantAsMetadata *CI = ConstantAsMetadata::get(
1805 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
1807 // Create a temporary to prevent nodes from resolving.
1808 auto Temp = MDTuple::getTemporary(Context, None);
1810 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
1811 Metadata *Ops1[] = {CI, CI, Temp.get()};
1812 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
1814 auto *N1 = MDTuple::get(Context, Ops1);
1815 auto *N2 = MDTuple::get(Context, Ops2);
1818 // Tell metadata that the constant is getting deleted.
1820 // After this, N1 will be invalid, so don't touch it.
1821 ValueAsMetadata::handleDeletion(CI->getValue());
1822 EXPECT_EQ(nullptr, N2->getOperand(0));
1823 EXPECT_EQ(nullptr, N2->getOperand(1));
1824 EXPECT_EQ(Temp.get(), N2->getOperand(2));
1826 // Clean up Temp for teardown.
1827 Temp->replaceAllUsesWith(nullptr);
1830 typedef MetadataTest TrackingMDRefTest;
1832 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
1833 Type *Ty = Type::getInt1PtrTy(Context);
1834 std::unique_ptr<GlobalVariable> GV0(
1835 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1836 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
1837 EXPECT_TRUE(MD->getValue() == GV0.get());
1838 ASSERT_TRUE(GV0->use_empty());
1840 std::unique_ptr<GlobalVariable> GV1(
1841 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1842 GV0->replaceAllUsesWith(GV1.get());
1843 EXPECT_TRUE(MD->getValue() == GV1.get());
1845 // Reset it, so we don't inadvertently test deletion.
1849 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
1850 Type *Ty = Type::getInt1PtrTy(Context);
1851 std::unique_ptr<GlobalVariable> GV(
1852 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1853 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
1854 EXPECT_TRUE(MD->getValue() == GV.get());
1855 ASSERT_TRUE(GV->use_empty());
1861 TEST(NamedMDNodeTest, Search) {
1862 LLVMContext Context;
1863 ConstantAsMetadata *C =
1864 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
1865 ConstantAsMetadata *C2 =
1866 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
1868 Metadata *const V = C;
1869 Metadata *const V2 = C2;
1870 MDNode *n = MDNode::get(Context, V);
1871 MDNode *n2 = MDNode::get(Context, V2);
1873 Module M("MyModule", Context);
1874 const char *Name = "llvm.NMD1";
1875 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
1877 NMD->addOperand(n2);
1880 raw_string_ostream oss(Str);
1882 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",