1 //===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/IR/Constants.h" 12 #include "llvm/IR/DebugInfo.h" 13 #include "llvm/IR/DebugInfoMetadata.h" 14 #include "llvm/IR/Function.h" 15 #include "llvm/IR/Instructions.h" 16 #include "llvm/IR/LLVMContext.h" 17 #include "llvm/IR/Metadata.h" 18 #include "llvm/IR/Module.h" 19 #include "llvm/IR/Type.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include "gtest/gtest.h" 22 using namespace llvm; 23 24 namespace { 25 26 TEST(ContextAndReplaceableUsesTest, FromContext) { 27 LLVMContext Context; 28 ContextAndReplaceableUses CRU(Context); 29 EXPECT_EQ(&Context, &CRU.getContext()); 30 EXPECT_FALSE(CRU.hasReplaceableUses()); 31 EXPECT_FALSE(CRU.getReplaceableUses()); 32 } 33 34 TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) { 35 LLVMContext Context; 36 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context)); 37 EXPECT_EQ(&Context, &CRU.getContext()); 38 EXPECT_TRUE(CRU.hasReplaceableUses()); 39 EXPECT_TRUE(CRU.getReplaceableUses()); 40 } 41 42 TEST(ContextAndReplaceableUsesTest, makeReplaceable) { 43 LLVMContext Context; 44 ContextAndReplaceableUses CRU(Context); 45 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context)); 46 EXPECT_EQ(&Context, &CRU.getContext()); 47 EXPECT_TRUE(CRU.hasReplaceableUses()); 48 EXPECT_TRUE(CRU.getReplaceableUses()); 49 } 50 51 TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) { 52 LLVMContext Context; 53 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context); 54 auto *Ptr = ReplaceableUses.get(); 55 ContextAndReplaceableUses CRU(std::move(ReplaceableUses)); 56 ReplaceableUses = CRU.takeReplaceableUses(); 57 EXPECT_EQ(&Context, &CRU.getContext()); 58 EXPECT_FALSE(CRU.hasReplaceableUses()); 59 EXPECT_FALSE(CRU.getReplaceableUses()); 60 EXPECT_EQ(Ptr, ReplaceableUses.get()); 61 } 62 63 class MetadataTest : public testing::Test { 64 public: 65 MetadataTest() : M("test", Context), Counter(0) {} 66 67 protected: 68 LLVMContext Context; 69 Module M; 70 int Counter; 71 72 MDNode *getNode() { return MDNode::get(Context, None); } 73 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); } 74 MDNode *getNode(Metadata *MD1, Metadata *MD2) { 75 Metadata *MDs[] = {MD1, MD2}; 76 return MDNode::get(Context, MDs); 77 } 78 79 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); } 80 MDSubroutineType *getSubroutineType() { 81 return MDSubroutineType::getDistinct(Context, 0, getNode(nullptr)); 82 } 83 MDSubprogram *getSubprogram() { 84 return MDSubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0, 85 nullptr, false, false, 0, nullptr, 0, 0, 0, 86 0); 87 } 88 MDScopeRef getSubprogramRef() { return getSubprogram()->getRef(); } 89 MDFile *getFile() { 90 return MDFile::getDistinct(Context, "file.c", "/path/to/dir"); 91 } 92 MDTypeRef getBasicType(StringRef Name) { 93 return MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name) 94 ->getRef(); 95 } 96 MDTypeRef getDerivedType() { 97 return MDDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "", 98 nullptr, 0, nullptr, 99 getBasicType("basictype"), 1, 2, 0, 0) 100 ->getRef(); 101 } 102 Constant *getConstant() { 103 return ConstantInt::get(Type::getInt32Ty(Context), Counter++); 104 } 105 ConstantAsMetadata *getConstantAsMetadata() { 106 return ConstantAsMetadata::get(getConstant()); 107 } 108 MDTypeRef getCompositeType() { 109 return MDCompositeType::getDistinct( 110 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, 111 nullptr, 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "") 112 ->getRef(); 113 } 114 Function *getFunction(StringRef Name) { 115 return cast<Function>(M.getOrInsertFunction( 116 Name, FunctionType::get(Type::getVoidTy(Context), None, false))); 117 } 118 }; 119 typedef MetadataTest MDStringTest; 120 121 // Test that construction of MDString with different value produces different 122 // MDString objects, even with the same string pointer and nulls in the string. 123 TEST_F(MDStringTest, CreateDifferent) { 124 char x[3] = { 'f', 0, 'A' }; 125 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); 126 x[2] = 'B'; 127 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3)); 128 EXPECT_NE(s1, s2); 129 } 130 131 // Test that creation of MDStrings with the same string contents produces the 132 // same MDString object, even with different pointers. 133 TEST_F(MDStringTest, CreateSame) { 134 char x[4] = { 'a', 'b', 'c', 'X' }; 135 char y[4] = { 'a', 'b', 'c', 'Y' }; 136 137 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); 138 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); 139 EXPECT_EQ(s1, s2); 140 } 141 142 // Test that MDString prints out the string we fed it. 143 TEST_F(MDStringTest, PrintingSimple) { 144 char *str = new char[13]; 145 strncpy(str, "testing 1 2 3", 13); 146 MDString *s = MDString::get(Context, StringRef(str, 13)); 147 strncpy(str, "aaaaaaaaaaaaa", 13); 148 delete[] str; 149 150 std::string Str; 151 raw_string_ostream oss(Str); 152 s->print(oss); 153 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str()); 154 } 155 156 // Test printing of MDString with non-printable characters. 157 TEST_F(MDStringTest, PrintingComplex) { 158 char str[5] = {0, '\n', '"', '\\', (char)-1}; 159 MDString *s = MDString::get(Context, StringRef(str+0, 5)); 160 std::string Str; 161 raw_string_ostream oss(Str); 162 s->print(oss); 163 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str()); 164 } 165 166 typedef MetadataTest MDNodeTest; 167 168 // Test the two constructors, and containing other Constants. 169 TEST_F(MDNodeTest, Simple) { 170 char x[3] = { 'a', 'b', 'c' }; 171 char y[3] = { '1', '2', '3' }; 172 173 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); 174 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); 175 ConstantAsMetadata *CI = ConstantAsMetadata::get( 176 ConstantInt::get(getGlobalContext(), APInt(8, 0))); 177 178 std::vector<Metadata *> V; 179 V.push_back(s1); 180 V.push_back(CI); 181 V.push_back(s2); 182 183 MDNode *n1 = MDNode::get(Context, V); 184 Metadata *const c1 = n1; 185 MDNode *n2 = MDNode::get(Context, c1); 186 Metadata *const c2 = n2; 187 MDNode *n3 = MDNode::get(Context, V); 188 MDNode *n4 = MDNode::getIfExists(Context, V); 189 MDNode *n5 = MDNode::getIfExists(Context, c1); 190 MDNode *n6 = MDNode::getIfExists(Context, c2); 191 EXPECT_NE(n1, n2); 192 EXPECT_EQ(n1, n3); 193 EXPECT_EQ(n4, n1); 194 EXPECT_EQ(n5, n2); 195 EXPECT_EQ(n6, (Metadata *)nullptr); 196 197 EXPECT_EQ(3u, n1->getNumOperands()); 198 EXPECT_EQ(s1, n1->getOperand(0)); 199 EXPECT_EQ(CI, n1->getOperand(1)); 200 EXPECT_EQ(s2, n1->getOperand(2)); 201 202 EXPECT_EQ(1u, n2->getNumOperands()); 203 EXPECT_EQ(n1, n2->getOperand(0)); 204 } 205 206 TEST_F(MDNodeTest, Delete) { 207 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1); 208 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext())); 209 210 Metadata *const V = LocalAsMetadata::get(I); 211 MDNode *n = MDNode::get(Context, V); 212 TrackingMDRef wvh(n); 213 214 EXPECT_EQ(n, wvh); 215 216 delete I; 217 } 218 219 TEST_F(MDNodeTest, SelfReference) { 220 // !0 = !{!0} 221 // !1 = !{!0} 222 { 223 auto Temp = MDNode::getTemporary(Context, None); 224 Metadata *Args[] = {Temp.get()}; 225 MDNode *Self = MDNode::get(Context, Args); 226 Self->replaceOperandWith(0, Self); 227 ASSERT_EQ(Self, Self->getOperand(0)); 228 229 // Self-references should be distinct, so MDNode::get() should grab a 230 // uniqued node that references Self, not Self. 231 Args[0] = Self; 232 MDNode *Ref1 = MDNode::get(Context, Args); 233 MDNode *Ref2 = MDNode::get(Context, Args); 234 EXPECT_NE(Self, Ref1); 235 EXPECT_EQ(Ref1, Ref2); 236 } 237 238 // !0 = !{!0, !{}} 239 // !1 = !{!0, !{}} 240 { 241 auto Temp = MDNode::getTemporary(Context, None); 242 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)}; 243 MDNode *Self = MDNode::get(Context, Args); 244 Self->replaceOperandWith(0, Self); 245 ASSERT_EQ(Self, Self->getOperand(0)); 246 247 // Self-references should be distinct, so MDNode::get() should grab a 248 // uniqued node that references Self, not Self itself. 249 Args[0] = Self; 250 MDNode *Ref1 = MDNode::get(Context, Args); 251 MDNode *Ref2 = MDNode::get(Context, Args); 252 EXPECT_NE(Self, Ref1); 253 EXPECT_EQ(Ref1, Ref2); 254 } 255 } 256 257 TEST_F(MDNodeTest, Print) { 258 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7); 259 MDString *S = MDString::get(Context, "foo"); 260 MDNode *N0 = getNode(); 261 MDNode *N1 = getNode(N0); 262 MDNode *N2 = getNode(N0, N1); 263 264 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2}; 265 MDNode *N = MDNode::get(Context, Args); 266 267 std::string Expected; 268 { 269 raw_string_ostream OS(Expected); 270 OS << "<" << (void *)N << "> = !{"; 271 C->printAsOperand(OS); 272 OS << ", "; 273 S->printAsOperand(OS); 274 OS << ", null"; 275 MDNode *Nodes[] = {N0, N1, N2}; 276 for (auto *Node : Nodes) 277 OS << ", <" << (void *)Node << ">"; 278 OS << "}"; 279 } 280 281 std::string Actual; 282 { 283 raw_string_ostream OS(Actual); 284 N->print(OS); 285 } 286 287 EXPECT_EQ(Expected, Actual); 288 } 289 290 #define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \ 291 do { \ 292 std::string Actual_; \ 293 raw_string_ostream OS(Actual_); \ 294 PRINT; \ 295 OS.flush(); \ 296 std::string Expected_(EXPECTED); \ 297 EXPECT_EQ(Expected_, Actual_); \ 298 } while (false) 299 300 TEST_F(MDNodeTest, PrintTemporary) { 301 MDNode *Arg = getNode(); 302 TempMDNode Temp = MDNode::getTemporary(Context, Arg); 303 MDNode *N = getNode(Temp.get()); 304 Module M("test", Context); 305 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named"); 306 NMD->addOperand(N); 307 308 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M)); 309 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M)); 310 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M)); 311 312 // Cleanup. 313 Temp->replaceAllUsesWith(Arg); 314 } 315 316 TEST_F(MDNodeTest, PrintFromModule) { 317 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7); 318 MDString *S = MDString::get(Context, "foo"); 319 MDNode *N0 = getNode(); 320 MDNode *N1 = getNode(N0); 321 MDNode *N2 = getNode(N0, N1); 322 323 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2}; 324 MDNode *N = MDNode::get(Context, Args); 325 Module M("test", Context); 326 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named"); 327 NMD->addOperand(N); 328 329 std::string Expected; 330 { 331 raw_string_ostream OS(Expected); 332 OS << "!0 = !{"; 333 C->printAsOperand(OS); 334 OS << ", "; 335 S->printAsOperand(OS); 336 OS << ", null, !1, !2, !3}"; 337 } 338 339 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M)); 340 } 341 342 TEST_F(MDNodeTest, PrintFromFunction) { 343 Module M("test", Context); 344 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false); 345 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M); 346 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M); 347 auto *BB0 = BasicBlock::Create(Context, "entry", F0); 348 auto *BB1 = BasicBlock::Create(Context, "entry", F1); 349 auto *R0 = ReturnInst::Create(Context, BB0); 350 auto *R1 = ReturnInst::Create(Context, BB1); 351 auto *N0 = MDNode::getDistinct(Context, None); 352 auto *N1 = MDNode::getDistinct(Context, None); 353 R0->setMetadata("md", N0); 354 R1->setMetadata("md", N1); 355 356 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M)); 357 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M)); 358 } 359 360 TEST_F(MDNodeTest, PrintFromMetadataAsValue) { 361 Module M("test", Context); 362 363 auto *Intrinsic = 364 Function::Create(FunctionType::get(Type::getVoidTy(Context), 365 Type::getMetadataTy(Context), false), 366 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M); 367 368 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false); 369 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M); 370 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M); 371 auto *BB0 = BasicBlock::Create(Context, "entry", F0); 372 auto *BB1 = BasicBlock::Create(Context, "entry", F1); 373 auto *N0 = MDNode::getDistinct(Context, None); 374 auto *N1 = MDNode::getDistinct(Context, None); 375 auto *MAV0 = MetadataAsValue::get(Context, N0); 376 auto *MAV1 = MetadataAsValue::get(Context, N1); 377 CallInst::Create(Intrinsic, MAV0, "", BB0); 378 CallInst::Create(Intrinsic, MAV1, "", BB1); 379 380 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS)); 381 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS)); 382 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false)); 383 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false)); 384 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true)); 385 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true)); 386 } 387 #undef EXPECT_PRINTER_EQ 388 389 TEST_F(MDNodeTest, NullOperand) { 390 // metadata !{} 391 MDNode *Empty = MDNode::get(Context, None); 392 393 // metadata !{metadata !{}} 394 Metadata *Ops[] = {Empty}; 395 MDNode *N = MDNode::get(Context, Ops); 396 ASSERT_EQ(Empty, N->getOperand(0)); 397 398 // metadata !{metadata !{}} => metadata !{null} 399 N->replaceOperandWith(0, nullptr); 400 ASSERT_EQ(nullptr, N->getOperand(0)); 401 402 // metadata !{null} 403 Ops[0] = nullptr; 404 MDNode *NullOp = MDNode::get(Context, Ops); 405 ASSERT_EQ(nullptr, NullOp->getOperand(0)); 406 EXPECT_EQ(N, NullOp); 407 } 408 409 TEST_F(MDNodeTest, DistinctOnUniquingCollision) { 410 // !{} 411 MDNode *Empty = MDNode::get(Context, None); 412 ASSERT_TRUE(Empty->isResolved()); 413 EXPECT_FALSE(Empty->isDistinct()); 414 415 // !{!{}} 416 Metadata *Wrapped1Ops[] = {Empty}; 417 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops); 418 ASSERT_EQ(Empty, Wrapped1->getOperand(0)); 419 ASSERT_TRUE(Wrapped1->isResolved()); 420 EXPECT_FALSE(Wrapped1->isDistinct()); 421 422 // !{!{!{}}} 423 Metadata *Wrapped2Ops[] = {Wrapped1}; 424 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops); 425 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0)); 426 ASSERT_TRUE(Wrapped2->isResolved()); 427 EXPECT_FALSE(Wrapped2->isDistinct()); 428 429 // !{!{!{}}} => !{!{}} 430 Wrapped2->replaceOperandWith(0, Empty); 431 ASSERT_EQ(Empty, Wrapped2->getOperand(0)); 432 EXPECT_TRUE(Wrapped2->isDistinct()); 433 EXPECT_FALSE(Wrapped1->isDistinct()); 434 } 435 436 TEST_F(MDNodeTest, getDistinct) { 437 // !{} 438 MDNode *Empty = MDNode::get(Context, None); 439 ASSERT_TRUE(Empty->isResolved()); 440 ASSERT_FALSE(Empty->isDistinct()); 441 ASSERT_EQ(Empty, MDNode::get(Context, None)); 442 443 // distinct !{} 444 MDNode *Distinct1 = MDNode::getDistinct(Context, None); 445 MDNode *Distinct2 = MDNode::getDistinct(Context, None); 446 EXPECT_TRUE(Distinct1->isResolved()); 447 EXPECT_TRUE(Distinct2->isDistinct()); 448 EXPECT_NE(Empty, Distinct1); 449 EXPECT_NE(Empty, Distinct2); 450 EXPECT_NE(Distinct1, Distinct2); 451 452 // !{} 453 ASSERT_EQ(Empty, MDNode::get(Context, None)); 454 } 455 456 TEST_F(MDNodeTest, isUniqued) { 457 MDNode *U = MDTuple::get(Context, None); 458 MDNode *D = MDTuple::getDistinct(Context, None); 459 auto T = MDTuple::getTemporary(Context, None); 460 EXPECT_TRUE(U->isUniqued()); 461 EXPECT_FALSE(D->isUniqued()); 462 EXPECT_FALSE(T->isUniqued()); 463 } 464 465 TEST_F(MDNodeTest, isDistinct) { 466 MDNode *U = MDTuple::get(Context, None); 467 MDNode *D = MDTuple::getDistinct(Context, None); 468 auto T = MDTuple::getTemporary(Context, None); 469 EXPECT_FALSE(U->isDistinct()); 470 EXPECT_TRUE(D->isDistinct()); 471 EXPECT_FALSE(T->isDistinct()); 472 } 473 474 TEST_F(MDNodeTest, isTemporary) { 475 MDNode *U = MDTuple::get(Context, None); 476 MDNode *D = MDTuple::getDistinct(Context, None); 477 auto T = MDTuple::getTemporary(Context, None); 478 EXPECT_FALSE(U->isTemporary()); 479 EXPECT_FALSE(D->isTemporary()); 480 EXPECT_TRUE(T->isTemporary()); 481 } 482 483 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) { 484 // temporary !{} 485 auto Temp = MDTuple::getTemporary(Context, None); 486 ASSERT_FALSE(Temp->isResolved()); 487 488 // distinct !{temporary !{}} 489 Metadata *Ops[] = {Temp.get()}; 490 MDNode *Distinct = MDNode::getDistinct(Context, Ops); 491 EXPECT_TRUE(Distinct->isResolved()); 492 EXPECT_EQ(Temp.get(), Distinct->getOperand(0)); 493 494 // temporary !{} => !{} 495 MDNode *Empty = MDNode::get(Context, None); 496 Temp->replaceAllUsesWith(Empty); 497 EXPECT_EQ(Empty, Distinct->getOperand(0)); 498 } 499 500 TEST_F(MDNodeTest, handleChangedOperandRecursion) { 501 // !0 = !{} 502 MDNode *N0 = MDNode::get(Context, None); 503 504 // !1 = !{!3, null} 505 auto Temp3 = MDTuple::getTemporary(Context, None); 506 Metadata *Ops1[] = {Temp3.get(), nullptr}; 507 MDNode *N1 = MDNode::get(Context, Ops1); 508 509 // !2 = !{!3, !0} 510 Metadata *Ops2[] = {Temp3.get(), N0}; 511 MDNode *N2 = MDNode::get(Context, Ops2); 512 513 // !3 = !{!2} 514 Metadata *Ops3[] = {N2}; 515 MDNode *N3 = MDNode::get(Context, Ops3); 516 Temp3->replaceAllUsesWith(N3); 517 518 // !4 = !{!1} 519 Metadata *Ops4[] = {N1}; 520 MDNode *N4 = MDNode::get(Context, Ops4); 521 522 // Confirm that the cycle prevented RAUW from getting dropped. 523 EXPECT_TRUE(N0->isResolved()); 524 EXPECT_FALSE(N1->isResolved()); 525 EXPECT_FALSE(N2->isResolved()); 526 EXPECT_FALSE(N3->isResolved()); 527 EXPECT_FALSE(N4->isResolved()); 528 529 // Create a couple of distinct nodes to observe what's going on. 530 // 531 // !5 = distinct !{!2} 532 // !6 = distinct !{!3} 533 Metadata *Ops5[] = {N2}; 534 MDNode *N5 = MDNode::getDistinct(Context, Ops5); 535 Metadata *Ops6[] = {N3}; 536 MDNode *N6 = MDNode::getDistinct(Context, Ops6); 537 538 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW). 539 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2 540 // references !3, this can cause a re-entry of handleChangedOperand() when !3 541 // is not ready for it. 542 // 543 // !2->replaceOperandWith(1, nullptr) 544 // !2: !{!3, !0} => !{!3, null} 545 // !2->replaceAllUsesWith(!1) 546 // !3: !{!2] => !{!1} 547 // !3->replaceAllUsesWith(!4) 548 N2->replaceOperandWith(1, nullptr); 549 550 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from 551 // under us. Just check that the other nodes are sane. 552 // 553 // !1 = !{!4, null} 554 // !4 = !{!1} 555 // !5 = distinct !{!1} 556 // !6 = distinct !{!4} 557 EXPECT_EQ(N4, N1->getOperand(0)); 558 EXPECT_EQ(N1, N4->getOperand(0)); 559 EXPECT_EQ(N1, N5->getOperand(0)); 560 EXPECT_EQ(N4, N6->getOperand(0)); 561 } 562 563 TEST_F(MDNodeTest, replaceResolvedOperand) { 564 // Check code for replacing one resolved operand with another. If doing this 565 // directly (via replaceOperandWith()) becomes illegal, change the operand to 566 // a global value that gets RAUW'ed. 567 // 568 // Use a temporary node to keep N from being resolved. 569 auto Temp = MDTuple::getTemporary(Context, None); 570 Metadata *Ops[] = {nullptr, Temp.get()}; 571 572 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>()); 573 MDNode *N = MDTuple::get(Context, Ops); 574 EXPECT_EQ(nullptr, N->getOperand(0)); 575 ASSERT_FALSE(N->isResolved()); 576 577 // Check code for replacing resolved nodes. 578 N->replaceOperandWith(0, Empty); 579 EXPECT_EQ(Empty, N->getOperand(0)); 580 581 // Check code for adding another unresolved operand. 582 N->replaceOperandWith(0, Temp.get()); 583 EXPECT_EQ(Temp.get(), N->getOperand(0)); 584 585 // Remove the references to Temp; required for teardown. 586 Temp->replaceAllUsesWith(nullptr); 587 } 588 589 TEST_F(MDNodeTest, replaceWithUniqued) { 590 auto *Empty = MDTuple::get(Context, None); 591 MDTuple *FirstUniqued; 592 { 593 Metadata *Ops[] = {Empty}; 594 auto Temp = MDTuple::getTemporary(Context, Ops); 595 EXPECT_TRUE(Temp->isTemporary()); 596 597 // Don't expect a collision. 598 auto *Current = Temp.get(); 599 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp)); 600 EXPECT_TRUE(FirstUniqued->isUniqued()); 601 EXPECT_TRUE(FirstUniqued->isResolved()); 602 EXPECT_EQ(Current, FirstUniqued); 603 } 604 { 605 Metadata *Ops[] = {Empty}; 606 auto Temp = MDTuple::getTemporary(Context, Ops); 607 EXPECT_TRUE(Temp->isTemporary()); 608 609 // Should collide with Uniqued above this time. 610 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp)); 611 EXPECT_TRUE(Uniqued->isUniqued()); 612 EXPECT_TRUE(Uniqued->isResolved()); 613 EXPECT_EQ(FirstUniqued, Uniqued); 614 } 615 { 616 auto Unresolved = MDTuple::getTemporary(Context, None); 617 Metadata *Ops[] = {Unresolved.get()}; 618 auto Temp = MDTuple::getTemporary(Context, Ops); 619 EXPECT_TRUE(Temp->isTemporary()); 620 621 // Shouldn't be resolved. 622 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp)); 623 EXPECT_TRUE(Uniqued->isUniqued()); 624 EXPECT_FALSE(Uniqued->isResolved()); 625 626 // Should be a different node. 627 EXPECT_NE(FirstUniqued, Uniqued); 628 629 // Should resolve when we update its node (note: be careful to avoid a 630 // collision with any other nodes above). 631 Uniqued->replaceOperandWith(0, nullptr); 632 EXPECT_TRUE(Uniqued->isResolved()); 633 } 634 } 635 636 TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) { 637 // temp !{} 638 MDTuple *Op = MDTuple::getTemporary(Context, None).release(); 639 EXPECT_FALSE(Op->isResolved()); 640 641 // temp !{temp !{}} 642 Metadata *Ops[] = {Op}; 643 MDTuple *N = MDTuple::getTemporary(Context, Ops).release(); 644 EXPECT_FALSE(N->isResolved()); 645 646 // temp !{temp !{}} => !{temp !{}} 647 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N))); 648 EXPECT_FALSE(N->isResolved()); 649 650 // !{temp !{}} => !{!{}} 651 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op))); 652 EXPECT_TRUE(Op->isResolved()); 653 EXPECT_TRUE(N->isResolved()); 654 } 655 656 TEST_F(MDNodeTest, replaceWithUniquedChangingOperand) { 657 // i1* @GV 658 Type *Ty = Type::getInt1PtrTy(Context); 659 std::unique_ptr<GlobalVariable> GV( 660 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 661 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get()); 662 663 // temp !{i1* @GV} 664 Metadata *Ops[] = {Op}; 665 MDTuple *N = MDTuple::getTemporary(Context, Ops).release(); 666 667 // temp !{i1* @GV} => !{i1* @GV} 668 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N))); 669 ASSERT_TRUE(N->isUniqued()); 670 671 // !{i1* @GV} => !{null} 672 GV.reset(); 673 ASSERT_TRUE(N->isUniqued()); 674 Metadata *NullOps[] = {nullptr}; 675 ASSERT_EQ(N, MDTuple::get(Context, NullOps)); 676 } 677 678 TEST_F(MDNodeTest, replaceWithDistinct) { 679 { 680 auto *Empty = MDTuple::get(Context, None); 681 Metadata *Ops[] = {Empty}; 682 auto Temp = MDTuple::getTemporary(Context, Ops); 683 EXPECT_TRUE(Temp->isTemporary()); 684 685 // Don't expect a collision. 686 auto *Current = Temp.get(); 687 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp)); 688 EXPECT_TRUE(Distinct->isDistinct()); 689 EXPECT_TRUE(Distinct->isResolved()); 690 EXPECT_EQ(Current, Distinct); 691 } 692 { 693 auto Unresolved = MDTuple::getTemporary(Context, None); 694 Metadata *Ops[] = {Unresolved.get()}; 695 auto Temp = MDTuple::getTemporary(Context, Ops); 696 EXPECT_TRUE(Temp->isTemporary()); 697 698 // Don't expect a collision. 699 auto *Current = Temp.get(); 700 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp)); 701 EXPECT_TRUE(Distinct->isDistinct()); 702 EXPECT_TRUE(Distinct->isResolved()); 703 EXPECT_EQ(Current, Distinct); 704 705 // Cleanup; required for teardown. 706 Unresolved->replaceAllUsesWith(nullptr); 707 } 708 } 709 710 TEST_F(MDNodeTest, replaceWithPermanent) { 711 Metadata *Ops[] = {nullptr}; 712 auto Temp = MDTuple::getTemporary(Context, Ops); 713 auto *T = Temp.get(); 714 715 // U is a normal, uniqued node that references T. 716 auto *U = MDTuple::get(Context, T); 717 EXPECT_TRUE(U->isUniqued()); 718 719 // Make Temp self-referencing. 720 Temp->replaceOperandWith(0, T); 721 722 // Try to uniquify Temp. This should, despite the name in the API, give a 723 // 'distinct' node, since self-references aren't allowed to be uniqued. 724 // 725 // Since it's distinct, N should have the same address as when it was a 726 // temporary (i.e., be equal to T not U). 727 auto *N = MDNode::replaceWithPermanent(std::move(Temp)); 728 EXPECT_EQ(N, T); 729 EXPECT_TRUE(N->isDistinct()); 730 731 // U should be the canonical unique node with N as the argument. 732 EXPECT_EQ(U, MDTuple::get(Context, N)); 733 EXPECT_TRUE(U->isUniqued()); 734 735 // This temporary should collide with U when replaced, but it should still be 736 // uniqued. 737 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N))); 738 EXPECT_TRUE(U->isUniqued()); 739 740 // This temporary should become a new uniqued node. 741 auto Temp2 = MDTuple::getTemporary(Context, U); 742 auto *V = Temp2.get(); 743 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2))); 744 EXPECT_TRUE(V->isUniqued()); 745 EXPECT_EQ(U, V->getOperand(0)); 746 } 747 748 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) { 749 TrackingMDRef Ref; 750 EXPECT_EQ(nullptr, Ref.get()); 751 { 752 auto Temp = MDTuple::getTemporary(Context, None); 753 Ref.reset(Temp.get()); 754 EXPECT_EQ(Temp.get(), Ref.get()); 755 } 756 EXPECT_EQ(nullptr, Ref.get()); 757 } 758 759 typedef MetadataTest MDLocationTest; 760 761 TEST_F(MDLocationTest, Overflow) { 762 MDSubprogram *N = getSubprogram(); 763 { 764 MDLocation *L = MDLocation::get(Context, 2, 7, N); 765 EXPECT_EQ(2u, L->getLine()); 766 EXPECT_EQ(7u, L->getColumn()); 767 } 768 unsigned U16 = 1u << 16; 769 { 770 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N); 771 EXPECT_EQ(UINT32_MAX, L->getLine()); 772 EXPECT_EQ(U16 - 1, L->getColumn()); 773 } 774 { 775 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N); 776 EXPECT_EQ(UINT32_MAX, L->getLine()); 777 EXPECT_EQ(0u, L->getColumn()); 778 } 779 { 780 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N); 781 EXPECT_EQ(UINT32_MAX, L->getLine()); 782 EXPECT_EQ(0u, L->getColumn()); 783 } 784 } 785 786 TEST_F(MDLocationTest, getDistinct) { 787 MDNode *N = getSubprogram(); 788 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N); 789 EXPECT_TRUE(L0->isDistinct()); 790 MDLocation *L1 = MDLocation::get(Context, 2, 7, N); 791 EXPECT_FALSE(L1->isDistinct()); 792 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N)); 793 } 794 795 TEST_F(MDLocationTest, getTemporary) { 796 MDNode *N = MDNode::get(Context, None); 797 auto L = MDLocation::getTemporary(Context, 2, 7, N); 798 EXPECT_TRUE(L->isTemporary()); 799 EXPECT_FALSE(L->isResolved()); 800 } 801 802 typedef MetadataTest GenericDebugNodeTest; 803 804 TEST_F(GenericDebugNodeTest, get) { 805 StringRef Header = "header"; 806 auto *Empty = MDNode::get(Context, None); 807 Metadata *Ops1[] = {Empty}; 808 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1); 809 EXPECT_EQ(15u, N->getTag()); 810 EXPECT_EQ(2u, N->getNumOperands()); 811 EXPECT_EQ(Header, N->getHeader()); 812 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0)); 813 EXPECT_EQ(1u, N->getNumDwarfOperands()); 814 EXPECT_EQ(Empty, N->getDwarfOperand(0)); 815 EXPECT_EQ(Empty, N->getOperand(1)); 816 ASSERT_TRUE(N->isUniqued()); 817 818 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1)); 819 820 N->replaceOperandWith(1, nullptr); 821 EXPECT_EQ(15u, N->getTag()); 822 EXPECT_EQ(Header, N->getHeader()); 823 EXPECT_EQ(nullptr, N->getDwarfOperand(0)); 824 ASSERT_TRUE(N->isUniqued()); 825 826 Metadata *Ops2[] = {nullptr}; 827 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2)); 828 829 N->replaceDwarfOperandWith(0, Empty); 830 EXPECT_EQ(15u, N->getTag()); 831 EXPECT_EQ(Header, N->getHeader()); 832 EXPECT_EQ(Empty, N->getDwarfOperand(0)); 833 ASSERT_TRUE(N->isUniqued()); 834 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1)); 835 836 TempGenericDebugNode Temp = N->clone(); 837 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 838 } 839 840 TEST_F(GenericDebugNodeTest, getEmptyHeader) { 841 // Canonicalize !"" to null. 842 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None); 843 EXPECT_EQ(StringRef(), N->getHeader()); 844 EXPECT_EQ(nullptr, N->getOperand(0)); 845 } 846 847 typedef MetadataTest MDSubrangeTest; 848 849 TEST_F(MDSubrangeTest, get) { 850 auto *N = MDSubrange::get(Context, 5, 7); 851 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag()); 852 EXPECT_EQ(5, N->getCount()); 853 EXPECT_EQ(7, N->getLowerBound()); 854 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7)); 855 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5)); 856 857 TempMDSubrange Temp = N->clone(); 858 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 859 } 860 861 TEST_F(MDSubrangeTest, getEmptyArray) { 862 auto *N = MDSubrange::get(Context, -1, 0); 863 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag()); 864 EXPECT_EQ(-1, N->getCount()); 865 EXPECT_EQ(0, N->getLowerBound()); 866 EXPECT_EQ(N, MDSubrange::get(Context, -1, 0)); 867 } 868 869 typedef MetadataTest MDEnumeratorTest; 870 871 TEST_F(MDEnumeratorTest, get) { 872 auto *N = MDEnumerator::get(Context, 7, "name"); 873 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag()); 874 EXPECT_EQ(7, N->getValue()); 875 EXPECT_EQ("name", N->getName()); 876 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name")); 877 878 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name")); 879 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam")); 880 881 TempMDEnumerator Temp = N->clone(); 882 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 883 } 884 885 typedef MetadataTest MDBasicTypeTest; 886 887 TEST_F(MDBasicTypeTest, get) { 888 auto *N = 889 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7); 890 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag()); 891 EXPECT_EQ("special", N->getName()); 892 EXPECT_EQ(33u, N->getSizeInBits()); 893 EXPECT_EQ(26u, N->getAlignInBits()); 894 EXPECT_EQ(7u, N->getEncoding()); 895 EXPECT_EQ(0u, N->getLine()); 896 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 897 26, 7)); 898 899 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, 900 "special", 33, 26, 7)); 901 EXPECT_NE(N, 902 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7)); 903 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32, 904 26, 7)); 905 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 906 25, 7)); 907 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 908 26, 6)); 909 910 TempMDBasicType Temp = N->clone(); 911 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 912 } 913 914 TEST_F(MDBasicTypeTest, getWithLargeValues) { 915 auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 916 UINT64_MAX, UINT64_MAX - 1, 7); 917 EXPECT_EQ(UINT64_MAX, N->getSizeInBits()); 918 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits()); 919 } 920 921 TEST_F(MDBasicTypeTest, getUnspecified) { 922 auto *N = 923 MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified"); 924 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag()); 925 EXPECT_EQ("unspecified", N->getName()); 926 EXPECT_EQ(0u, N->getSizeInBits()); 927 EXPECT_EQ(0u, N->getAlignInBits()); 928 EXPECT_EQ(0u, N->getEncoding()); 929 EXPECT_EQ(0u, N->getLine()); 930 } 931 932 typedef MetadataTest MDTypeTest; 933 934 TEST_F(MDTypeTest, clone) { 935 // Check that MDType has a specialized clone that returns TempMDType. 936 MDType *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32, 937 dwarf::DW_ATE_signed); 938 939 TempMDType Temp = N->clone(); 940 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 941 } 942 943 TEST_F(MDTypeTest, setFlags) { 944 // void (void) 945 Metadata *TypesOps[] = {nullptr}; 946 Metadata *Types = MDTuple::get(Context, TypesOps); 947 948 MDType *D = MDSubroutineType::getDistinct(Context, 0u, Types); 949 EXPECT_EQ(0u, D->getFlags()); 950 D->setFlags(DebugNode::FlagRValueReference); 951 EXPECT_EQ(DebugNode::FlagRValueReference, D->getFlags()); 952 D->setFlags(0u); 953 EXPECT_EQ(0u, D->getFlags()); 954 955 TempMDType T = MDSubroutineType::getTemporary(Context, 0u, Types); 956 EXPECT_EQ(0u, T->getFlags()); 957 T->setFlags(DebugNode::FlagRValueReference); 958 EXPECT_EQ(DebugNode::FlagRValueReference, T->getFlags()); 959 T->setFlags(0u); 960 EXPECT_EQ(0u, T->getFlags()); 961 } 962 963 typedef MetadataTest MDDerivedTypeTest; 964 965 TEST_F(MDDerivedTypeTest, get) { 966 MDFile *File = getFile(); 967 MDScopeRef Scope = getSubprogramRef(); 968 MDTypeRef BaseType = getBasicType("basic"); 969 MDTuple *ExtraData = getTuple(); 970 971 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", 972 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData); 973 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag()); 974 EXPECT_EQ("something", N->getName()); 975 EXPECT_EQ(File, N->getFile()); 976 EXPECT_EQ(1u, N->getLine()); 977 EXPECT_EQ(Scope, N->getScope()); 978 EXPECT_EQ(BaseType, N->getBaseType()); 979 EXPECT_EQ(2u, N->getSizeInBits()); 980 EXPECT_EQ(3u, N->getAlignInBits()); 981 EXPECT_EQ(4u, N->getOffsetInBits()); 982 EXPECT_EQ(5u, N->getFlags()); 983 EXPECT_EQ(ExtraData, N->getExtraData()); 984 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 985 "something", File, 1, Scope, BaseType, 2, 3, 986 4, 5, ExtraData)); 987 988 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type, 989 "something", File, 1, Scope, BaseType, 2, 3, 990 4, 5, ExtraData)); 991 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else", 992 File, 1, Scope, BaseType, 2, 3, 4, 5, 993 ExtraData)); 994 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 995 "something", getFile(), 1, Scope, BaseType, 2, 996 3, 4, 5, ExtraData)); 997 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 998 "something", File, 2, Scope, BaseType, 2, 3, 999 4, 5, ExtraData)); 1000 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1001 "something", File, 1, getSubprogramRef(), 1002 BaseType, 2, 3, 4, 5, ExtraData)); 1003 EXPECT_NE(N, MDDerivedType::get( 1004 Context, dwarf::DW_TAG_pointer_type, "something", File, 1, 1005 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData)); 1006 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1007 "something", File, 1, Scope, BaseType, 3, 3, 1008 4, 5, ExtraData)); 1009 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1010 "something", File, 1, Scope, BaseType, 2, 2, 1011 4, 5, ExtraData)); 1012 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1013 "something", File, 1, Scope, BaseType, 2, 3, 1014 5, 5, ExtraData)); 1015 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1016 "something", File, 1, Scope, BaseType, 2, 3, 1017 4, 4, ExtraData)); 1018 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1019 "something", File, 1, Scope, BaseType, 2, 3, 1020 4, 5, getTuple())); 1021 1022 TempMDDerivedType Temp = N->clone(); 1023 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1024 } 1025 1026 TEST_F(MDDerivedTypeTest, getWithLargeValues) { 1027 MDFile *File = getFile(); 1028 MDScopeRef Scope = getSubprogramRef(); 1029 MDTypeRef BaseType = getBasicType("basic"); 1030 MDTuple *ExtraData = getTuple(); 1031 1032 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", 1033 File, 1, Scope, BaseType, UINT64_MAX, 1034 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData); 1035 EXPECT_EQ(UINT64_MAX, N->getSizeInBits()); 1036 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits()); 1037 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits()); 1038 } 1039 1040 typedef MetadataTest MDCompositeTypeTest; 1041 1042 TEST_F(MDCompositeTypeTest, get) { 1043 unsigned Tag = dwarf::DW_TAG_structure_type; 1044 StringRef Name = "some name"; 1045 MDFile *File = getFile(); 1046 unsigned Line = 1; 1047 MDScopeRef Scope = getSubprogramRef(); 1048 MDTypeRef BaseType = getCompositeType(); 1049 uint64_t SizeInBits = 2; 1050 uint64_t AlignInBits = 3; 1051 uint64_t OffsetInBits = 4; 1052 unsigned Flags = 5; 1053 MDTuple *Elements = getTuple(); 1054 unsigned RuntimeLang = 6; 1055 MDTypeRef VTableHolder = getCompositeType(); 1056 MDTuple *TemplateParams = getTuple(); 1057 StringRef Identifier = "some id"; 1058 1059 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope, 1060 BaseType, SizeInBits, AlignInBits, 1061 OffsetInBits, Flags, Elements, RuntimeLang, 1062 VTableHolder, TemplateParams, Identifier); 1063 EXPECT_EQ(Tag, N->getTag()); 1064 EXPECT_EQ(Name, N->getName()); 1065 EXPECT_EQ(File, N->getFile()); 1066 EXPECT_EQ(Line, N->getLine()); 1067 EXPECT_EQ(Scope, N->getScope()); 1068 EXPECT_EQ(BaseType, N->getBaseType()); 1069 EXPECT_EQ(SizeInBits, N->getSizeInBits()); 1070 EXPECT_EQ(AlignInBits, N->getAlignInBits()); 1071 EXPECT_EQ(OffsetInBits, N->getOffsetInBits()); 1072 EXPECT_EQ(Flags, N->getFlags()); 1073 EXPECT_EQ(Elements, N->getElements().get()); 1074 EXPECT_EQ(RuntimeLang, N->getRuntimeLang()); 1075 EXPECT_EQ(VTableHolder, N->getVTableHolder()); 1076 EXPECT_EQ(TemplateParams, N->getTemplateParams().get()); 1077 EXPECT_EQ(Identifier, N->getIdentifier()); 1078 1079 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, 1080 BaseType, SizeInBits, AlignInBits, 1081 OffsetInBits, Flags, Elements, RuntimeLang, 1082 VTableHolder, TemplateParams, Identifier)); 1083 1084 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope, 1085 BaseType, SizeInBits, AlignInBits, 1086 OffsetInBits, Flags, Elements, RuntimeLang, 1087 VTableHolder, TemplateParams, Identifier)); 1088 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope, 1089 BaseType, SizeInBits, AlignInBits, 1090 OffsetInBits, Flags, Elements, RuntimeLang, 1091 VTableHolder, TemplateParams, Identifier)); 1092 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, getFile(), Line, Scope, 1093 BaseType, SizeInBits, AlignInBits, 1094 OffsetInBits, Flags, Elements, RuntimeLang, 1095 VTableHolder, TemplateParams, Identifier)); 1096 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope, 1097 BaseType, SizeInBits, AlignInBits, 1098 OffsetInBits, Flags, Elements, RuntimeLang, 1099 VTableHolder, TemplateParams, Identifier)); 1100 EXPECT_NE(N, MDCompositeType::get( 1101 Context, Tag, Name, File, Line, getSubprogramRef(), BaseType, 1102 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, 1103 RuntimeLang, VTableHolder, TemplateParams, Identifier)); 1104 EXPECT_NE(N, MDCompositeType::get( 1105 Context, Tag, Name, File, Line, Scope, getBasicType("other"), 1106 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, 1107 RuntimeLang, VTableHolder, TemplateParams, Identifier)); 1108 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, 1109 BaseType, SizeInBits + 1, AlignInBits, 1110 OffsetInBits, Flags, Elements, RuntimeLang, 1111 VTableHolder, TemplateParams, Identifier)); 1112 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, 1113 BaseType, SizeInBits, AlignInBits + 1, 1114 OffsetInBits, Flags, Elements, RuntimeLang, 1115 VTableHolder, TemplateParams, Identifier)); 1116 EXPECT_NE(N, MDCompositeType::get( 1117 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1118 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang, 1119 VTableHolder, TemplateParams, Identifier)); 1120 EXPECT_NE(N, MDCompositeType::get( 1121 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1122 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang, 1123 VTableHolder, TemplateParams, Identifier)); 1124 EXPECT_NE(N, MDCompositeType::get( 1125 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1126 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang, 1127 VTableHolder, TemplateParams, Identifier)); 1128 EXPECT_NE(N, MDCompositeType::get( 1129 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1130 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1, 1131 VTableHolder, TemplateParams, Identifier)); 1132 EXPECT_NE(N, MDCompositeType::get( 1133 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1134 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, 1135 getCompositeType(), TemplateParams, Identifier)); 1136 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, 1137 BaseType, SizeInBits, AlignInBits, 1138 OffsetInBits, Flags, Elements, RuntimeLang, 1139 VTableHolder, getTuple(), Identifier)); 1140 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, 1141 BaseType, SizeInBits, AlignInBits, 1142 OffsetInBits, Flags, Elements, RuntimeLang, 1143 VTableHolder, TemplateParams, "other")); 1144 1145 // Be sure that missing identifiers get null pointers. 1146 EXPECT_FALSE(MDCompositeType::get( 1147 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1148 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, 1149 VTableHolder, TemplateParams, "")->getRawIdentifier()); 1150 EXPECT_FALSE(MDCompositeType::get( 1151 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1152 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, 1153 VTableHolder, TemplateParams)->getRawIdentifier()); 1154 1155 TempMDCompositeType Temp = N->clone(); 1156 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1157 } 1158 1159 TEST_F(MDCompositeTypeTest, getWithLargeValues) { 1160 unsigned Tag = dwarf::DW_TAG_structure_type; 1161 StringRef Name = "some name"; 1162 MDFile *File = getFile(); 1163 unsigned Line = 1; 1164 MDScopeRef Scope = getSubprogramRef(); 1165 MDTypeRef BaseType = getCompositeType(); 1166 uint64_t SizeInBits = UINT64_MAX; 1167 uint64_t AlignInBits = UINT64_MAX - 1; 1168 uint64_t OffsetInBits = UINT64_MAX - 2; 1169 unsigned Flags = 5; 1170 MDTuple *Elements = getTuple(); 1171 unsigned RuntimeLang = 6; 1172 MDTypeRef VTableHolder = getCompositeType(); 1173 MDTuple *TemplateParams = getTuple(); 1174 StringRef Identifier = "some id"; 1175 1176 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope, 1177 BaseType, SizeInBits, AlignInBits, 1178 OffsetInBits, Flags, Elements, RuntimeLang, 1179 VTableHolder, TemplateParams, Identifier); 1180 EXPECT_EQ(SizeInBits, N->getSizeInBits()); 1181 EXPECT_EQ(AlignInBits, N->getAlignInBits()); 1182 EXPECT_EQ(OffsetInBits, N->getOffsetInBits()); 1183 } 1184 1185 TEST_F(MDCompositeTypeTest, replaceOperands) { 1186 unsigned Tag = dwarf::DW_TAG_structure_type; 1187 StringRef Name = "some name"; 1188 MDFile *File = getFile(); 1189 unsigned Line = 1; 1190 MDScopeRef Scope = getSubprogramRef(); 1191 MDTypeRef BaseType = getCompositeType(); 1192 uint64_t SizeInBits = 2; 1193 uint64_t AlignInBits = 3; 1194 uint64_t OffsetInBits = 4; 1195 unsigned Flags = 5; 1196 unsigned RuntimeLang = 6; 1197 StringRef Identifier = "some id"; 1198 1199 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope, 1200 BaseType, SizeInBits, AlignInBits, 1201 OffsetInBits, Flags, nullptr, RuntimeLang, 1202 nullptr, nullptr, Identifier); 1203 1204 auto *Elements = MDTuple::getDistinct(Context, None); 1205 EXPECT_EQ(nullptr, N->getElements().get()); 1206 N->replaceElements(Elements); 1207 EXPECT_EQ(Elements, N->getElements().get()); 1208 N->replaceElements(nullptr); 1209 EXPECT_EQ(nullptr, N->getElements().get()); 1210 1211 MDTypeRef VTableHolder = getCompositeType(); 1212 EXPECT_EQ(nullptr, N->getVTableHolder()); 1213 N->replaceVTableHolder(VTableHolder); 1214 EXPECT_EQ(VTableHolder, N->getVTableHolder()); 1215 N->replaceVTableHolder(nullptr); 1216 EXPECT_EQ(nullptr, N->getVTableHolder()); 1217 1218 auto *TemplateParams = MDTuple::getDistinct(Context, None); 1219 EXPECT_EQ(nullptr, N->getTemplateParams().get()); 1220 N->replaceTemplateParams(TemplateParams); 1221 EXPECT_EQ(TemplateParams, N->getTemplateParams().get()); 1222 N->replaceTemplateParams(nullptr); 1223 EXPECT_EQ(nullptr, N->getTemplateParams().get()); 1224 } 1225 1226 typedef MetadataTest MDSubroutineTypeTest; 1227 1228 TEST_F(MDSubroutineTypeTest, get) { 1229 unsigned Flags = 1; 1230 MDTuple *TypeArray = getTuple(); 1231 1232 auto *N = MDSubroutineType::get(Context, Flags, TypeArray); 1233 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag()); 1234 EXPECT_EQ(Flags, N->getFlags()); 1235 EXPECT_EQ(TypeArray, N->getTypeArray().get()); 1236 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray)); 1237 1238 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray)); 1239 EXPECT_NE(N, MDSubroutineType::get(Context, Flags, getTuple())); 1240 1241 TempMDSubroutineType Temp = N->clone(); 1242 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1243 1244 // Test always-empty operands. 1245 EXPECT_EQ(nullptr, N->getScope()); 1246 EXPECT_EQ(nullptr, N->getFile()); 1247 EXPECT_EQ("", N->getName()); 1248 EXPECT_EQ(nullptr, N->getBaseType()); 1249 EXPECT_EQ(nullptr, N->getVTableHolder()); 1250 EXPECT_EQ(nullptr, N->getTemplateParams().get()); 1251 EXPECT_EQ("", N->getIdentifier()); 1252 } 1253 1254 typedef MetadataTest MDFileTest; 1255 1256 TEST_F(MDFileTest, get) { 1257 StringRef Filename = "file"; 1258 StringRef Directory = "dir"; 1259 auto *N = MDFile::get(Context, Filename, Directory); 1260 1261 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag()); 1262 EXPECT_EQ(Filename, N->getFilename()); 1263 EXPECT_EQ(Directory, N->getDirectory()); 1264 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory)); 1265 1266 EXPECT_NE(N, MDFile::get(Context, "other", Directory)); 1267 EXPECT_NE(N, MDFile::get(Context, Filename, "other")); 1268 1269 TempMDFile Temp = N->clone(); 1270 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1271 } 1272 1273 TEST_F(MDFileTest, ScopeGetFile) { 1274 // Ensure that MDScope::getFile() returns itself. 1275 MDScope *N = MDFile::get(Context, "file", "dir"); 1276 EXPECT_EQ(N, N->getFile()); 1277 } 1278 1279 typedef MetadataTest MDCompileUnitTest; 1280 1281 TEST_F(MDCompileUnitTest, get) { 1282 unsigned SourceLanguage = 1; 1283 MDFile *File = getFile(); 1284 StringRef Producer = "some producer"; 1285 bool IsOptimized = false; 1286 StringRef Flags = "flag after flag"; 1287 unsigned RuntimeVersion = 2; 1288 StringRef SplitDebugFilename = "another/file"; 1289 unsigned EmissionKind = 3; 1290 MDTuple *EnumTypes = getTuple(); 1291 MDTuple *RetainedTypes = getTuple(); 1292 MDTuple *Subprograms = getTuple(); 1293 MDTuple *GlobalVariables = getTuple(); 1294 MDTuple *ImportedEntities = getTuple(); 1295 auto *N = MDCompileUnit::get( 1296 Context, SourceLanguage, File, Producer, IsOptimized, Flags, 1297 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, 1298 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities); 1299 1300 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag()); 1301 EXPECT_EQ(SourceLanguage, N->getSourceLanguage()); 1302 EXPECT_EQ(File, N->getFile()); 1303 EXPECT_EQ(Producer, N->getProducer()); 1304 EXPECT_EQ(IsOptimized, N->isOptimized()); 1305 EXPECT_EQ(Flags, N->getFlags()); 1306 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion()); 1307 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename()); 1308 EXPECT_EQ(EmissionKind, N->getEmissionKind()); 1309 EXPECT_EQ(EnumTypes, N->getEnumTypes().get()); 1310 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get()); 1311 EXPECT_EQ(Subprograms, N->getSubprograms().get()); 1312 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get()); 1313 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get()); 1314 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, 1315 IsOptimized, Flags, RuntimeVersion, 1316 SplitDebugFilename, EmissionKind, EnumTypes, 1317 RetainedTypes, Subprograms, GlobalVariables, 1318 ImportedEntities)); 1319 1320 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer, 1321 IsOptimized, Flags, RuntimeVersion, 1322 SplitDebugFilename, EmissionKind, EnumTypes, 1323 RetainedTypes, Subprograms, GlobalVariables, 1324 ImportedEntities)); 1325 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, getFile(), Producer, 1326 IsOptimized, Flags, RuntimeVersion, 1327 SplitDebugFilename, EmissionKind, EnumTypes, 1328 RetainedTypes, Subprograms, GlobalVariables, 1329 ImportedEntities)); 1330 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other", 1331 IsOptimized, Flags, RuntimeVersion, 1332 SplitDebugFilename, EmissionKind, EnumTypes, 1333 RetainedTypes, Subprograms, GlobalVariables, 1334 ImportedEntities)); 1335 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, 1336 !IsOptimized, Flags, RuntimeVersion, 1337 SplitDebugFilename, EmissionKind, EnumTypes, 1338 RetainedTypes, Subprograms, GlobalVariables, 1339 ImportedEntities)); 1340 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, 1341 IsOptimized, "other", RuntimeVersion, 1342 SplitDebugFilename, EmissionKind, EnumTypes, 1343 RetainedTypes, Subprograms, GlobalVariables, 1344 ImportedEntities)); 1345 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, 1346 IsOptimized, Flags, RuntimeVersion + 1, 1347 SplitDebugFilename, EmissionKind, EnumTypes, 1348 RetainedTypes, Subprograms, GlobalVariables, 1349 ImportedEntities)); 1350 EXPECT_NE(N, 1351 MDCompileUnit::get(Context, SourceLanguage, File, Producer, 1352 IsOptimized, Flags, RuntimeVersion, "other", 1353 EmissionKind, EnumTypes, RetainedTypes, 1354 Subprograms, GlobalVariables, ImportedEntities)); 1355 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, 1356 IsOptimized, Flags, RuntimeVersion, 1357 SplitDebugFilename, EmissionKind + 1, 1358 EnumTypes, RetainedTypes, Subprograms, 1359 GlobalVariables, ImportedEntities)); 1360 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, 1361 IsOptimized, Flags, RuntimeVersion, 1362 SplitDebugFilename, EmissionKind, getTuple(), 1363 RetainedTypes, Subprograms, GlobalVariables, 1364 ImportedEntities)); 1365 EXPECT_NE(N, MDCompileUnit::get( 1366 Context, SourceLanguage, File, Producer, IsOptimized, Flags, 1367 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, 1368 getTuple(), Subprograms, GlobalVariables, ImportedEntities)); 1369 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, 1370 IsOptimized, Flags, RuntimeVersion, 1371 SplitDebugFilename, EmissionKind, EnumTypes, 1372 RetainedTypes, getTuple(), GlobalVariables, 1373 ImportedEntities)); 1374 EXPECT_NE(N, MDCompileUnit::get( 1375 Context, SourceLanguage, File, Producer, IsOptimized, Flags, 1376 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, 1377 RetainedTypes, Subprograms, getTuple(), ImportedEntities)); 1378 EXPECT_NE(N, MDCompileUnit::get( 1379 Context, SourceLanguage, File, Producer, IsOptimized, Flags, 1380 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, 1381 RetainedTypes, Subprograms, GlobalVariables, getTuple())); 1382 1383 TempMDCompileUnit Temp = N->clone(); 1384 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1385 } 1386 1387 TEST_F(MDCompileUnitTest, replaceArrays) { 1388 unsigned SourceLanguage = 1; 1389 MDFile *File = getFile(); 1390 StringRef Producer = "some producer"; 1391 bool IsOptimized = false; 1392 StringRef Flags = "flag after flag"; 1393 unsigned RuntimeVersion = 2; 1394 StringRef SplitDebugFilename = "another/file"; 1395 unsigned EmissionKind = 3; 1396 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None); 1397 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None); 1398 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None); 1399 auto *N = MDCompileUnit::get( 1400 Context, SourceLanguage, File, Producer, IsOptimized, Flags, 1401 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, 1402 RetainedTypes, nullptr, nullptr, ImportedEntities); 1403 1404 auto *Subprograms = MDTuple::getDistinct(Context, None); 1405 EXPECT_EQ(nullptr, N->getSubprograms().get()); 1406 N->replaceSubprograms(Subprograms); 1407 EXPECT_EQ(Subprograms, N->getSubprograms().get()); 1408 N->replaceSubprograms(nullptr); 1409 EXPECT_EQ(nullptr, N->getSubprograms().get()); 1410 1411 auto *GlobalVariables = MDTuple::getDistinct(Context, None); 1412 EXPECT_EQ(nullptr, N->getGlobalVariables().get()); 1413 N->replaceGlobalVariables(GlobalVariables); 1414 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get()); 1415 N->replaceGlobalVariables(nullptr); 1416 EXPECT_EQ(nullptr, N->getGlobalVariables().get()); 1417 } 1418 1419 typedef MetadataTest MDSubprogramTest; 1420 1421 TEST_F(MDSubprogramTest, get) { 1422 MDScopeRef Scope = getCompositeType(); 1423 StringRef Name = "name"; 1424 StringRef LinkageName = "linkage"; 1425 MDFile *File = getFile(); 1426 unsigned Line = 2; 1427 MDSubroutineType *Type = getSubroutineType(); 1428 bool IsLocalToUnit = false; 1429 bool IsDefinition = true; 1430 unsigned ScopeLine = 3; 1431 MDTypeRef ContainingType = getCompositeType(); 1432 unsigned Virtuality = 4; 1433 unsigned VirtualIndex = 5; 1434 unsigned Flags = 6; 1435 bool IsOptimized = false; 1436 llvm::Function *Function = getFunction("foo"); 1437 MDTuple *TemplateParams = getTuple(); 1438 MDSubprogram *Declaration = getSubprogram(); 1439 MDTuple *Variables = getTuple(); 1440 1441 auto *N = MDSubprogram::get( 1442 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, 1443 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, 1444 IsOptimized, Function, TemplateParams, Declaration, Variables); 1445 1446 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag()); 1447 EXPECT_EQ(Scope, N->getScope()); 1448 EXPECT_EQ(Name, N->getName()); 1449 EXPECT_EQ(LinkageName, N->getLinkageName()); 1450 EXPECT_EQ(File, N->getFile()); 1451 EXPECT_EQ(Line, N->getLine()); 1452 EXPECT_EQ(Type, N->getType()); 1453 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit()); 1454 EXPECT_EQ(IsDefinition, N->isDefinition()); 1455 EXPECT_EQ(ScopeLine, N->getScopeLine()); 1456 EXPECT_EQ(ContainingType, N->getContainingType()); 1457 EXPECT_EQ(Virtuality, N->getVirtuality()); 1458 EXPECT_EQ(VirtualIndex, N->getVirtualIndex()); 1459 EXPECT_EQ(Flags, N->getFlags()); 1460 EXPECT_EQ(IsOptimized, N->isOptimized()); 1461 EXPECT_EQ(Function, N->getFunction()); 1462 EXPECT_EQ(TemplateParams, N->getTemplateParams().get()); 1463 EXPECT_EQ(Declaration, N->getDeclaration()); 1464 EXPECT_EQ(Variables, N->getVariables().get()); 1465 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1466 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1467 ContainingType, Virtuality, VirtualIndex, 1468 Flags, IsOptimized, Function, TemplateParams, 1469 Declaration, Variables)); 1470 1471 EXPECT_NE(N, MDSubprogram::get(Context, getCompositeType(), Name, LinkageName, 1472 File, Line, Type, IsLocalToUnit, IsDefinition, 1473 ScopeLine, ContainingType, Virtuality, 1474 VirtualIndex, Flags, IsOptimized, Function, 1475 TemplateParams, Declaration, Variables)); 1476 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File, 1477 Line, Type, IsLocalToUnit, IsDefinition, 1478 ScopeLine, ContainingType, Virtuality, 1479 VirtualIndex, Flags, IsOptimized, Function, 1480 TemplateParams, Declaration, Variables)); 1481 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line, 1482 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1483 ContainingType, Virtuality, VirtualIndex, 1484 Flags, IsOptimized, Function, TemplateParams, 1485 Declaration, Variables)); 1486 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, getFile(), 1487 Line, Type, IsLocalToUnit, IsDefinition, 1488 ScopeLine, ContainingType, Virtuality, 1489 VirtualIndex, Flags, IsOptimized, Function, 1490 TemplateParams, Declaration, Variables)); 1491 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, 1492 Line + 1, Type, IsLocalToUnit, IsDefinition, 1493 ScopeLine, ContainingType, Virtuality, 1494 VirtualIndex, Flags, IsOptimized, Function, 1495 TemplateParams, Declaration, Variables)); 1496 EXPECT_NE(N, MDSubprogram::get( 1497 Context, Scope, Name, LinkageName, File, Line, 1498 getSubroutineType(), IsLocalToUnit, IsDefinition, ScopeLine, 1499 ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized, 1500 Function, TemplateParams, Declaration, Variables)); 1501 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1502 Type, !IsLocalToUnit, IsDefinition, ScopeLine, 1503 ContainingType, Virtuality, VirtualIndex, 1504 Flags, IsOptimized, Function, TemplateParams, 1505 Declaration, Variables)); 1506 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1507 Type, IsLocalToUnit, !IsDefinition, ScopeLine, 1508 ContainingType, Virtuality, VirtualIndex, 1509 Flags, IsOptimized, Function, TemplateParams, 1510 Declaration, Variables)); 1511 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1512 Type, IsLocalToUnit, IsDefinition, 1513 ScopeLine + 1, ContainingType, Virtuality, 1514 VirtualIndex, Flags, IsOptimized, Function, 1515 TemplateParams, Declaration, Variables)); 1516 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1517 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1518 getCompositeType(), Virtuality, VirtualIndex, 1519 Flags, IsOptimized, Function, TemplateParams, 1520 Declaration, Variables)); 1521 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1522 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1523 ContainingType, Virtuality + 1, VirtualIndex, 1524 Flags, IsOptimized, Function, TemplateParams, 1525 Declaration, Variables)); 1526 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1527 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1528 ContainingType, Virtuality, VirtualIndex + 1, 1529 Flags, IsOptimized, Function, TemplateParams, 1530 Declaration, Variables)); 1531 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1532 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1533 ContainingType, Virtuality, VirtualIndex, 1534 ~Flags, IsOptimized, Function, TemplateParams, 1535 Declaration, Variables)); 1536 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1537 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1538 ContainingType, Virtuality, VirtualIndex, 1539 Flags, !IsOptimized, Function, TemplateParams, 1540 Declaration, Variables)); 1541 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1542 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1543 ContainingType, Virtuality, VirtualIndex, 1544 Flags, IsOptimized, getFunction("bar"), 1545 TemplateParams, Declaration, Variables)); 1546 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1547 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1548 ContainingType, Virtuality, VirtualIndex, 1549 Flags, IsOptimized, Function, getTuple(), 1550 Declaration, Variables)); 1551 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1552 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1553 ContainingType, Virtuality, VirtualIndex, 1554 Flags, IsOptimized, Function, TemplateParams, 1555 getSubprogram(), Variables)); 1556 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1557 Type, IsLocalToUnit, IsDefinition, ScopeLine, 1558 ContainingType, Virtuality, VirtualIndex, 1559 Flags, IsOptimized, Function, TemplateParams, 1560 Declaration, getTuple())); 1561 1562 TempMDSubprogram Temp = N->clone(); 1563 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1564 } 1565 1566 TEST_F(MDSubprogramTest, replaceFunction) { 1567 MDScopeRef Scope = getCompositeType(); 1568 StringRef Name = "name"; 1569 StringRef LinkageName = "linkage"; 1570 MDFile *File = getFile(); 1571 unsigned Line = 2; 1572 MDSubroutineType *Type = getSubroutineType(); 1573 bool IsLocalToUnit = false; 1574 bool IsDefinition = true; 1575 unsigned ScopeLine = 3; 1576 MDTypeRef ContainingType = getCompositeType(); 1577 unsigned Virtuality = 4; 1578 unsigned VirtualIndex = 5; 1579 unsigned Flags = 6; 1580 bool IsOptimized = false; 1581 MDTuple *TemplateParams = getTuple(); 1582 MDSubprogram *Declaration = getSubprogram(); 1583 MDTuple *Variables = getTuple(); 1584 1585 auto *N = MDSubprogram::get( 1586 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, 1587 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, 1588 IsOptimized, nullptr, TemplateParams, Declaration, Variables); 1589 1590 EXPECT_EQ(nullptr, N->getFunction()); 1591 1592 std::unique_ptr<Function> F( 1593 Function::Create(FunctionType::get(Type::getVoidTy(Context), false), 1594 GlobalValue::ExternalLinkage)); 1595 N->replaceFunction(F.get()); 1596 EXPECT_EQ(F.get(), N->getFunction()); 1597 1598 N->replaceFunction(nullptr); 1599 EXPECT_EQ(nullptr, N->getFunction()); 1600 } 1601 1602 typedef MetadataTest MDLexicalBlockTest; 1603 1604 TEST_F(MDLexicalBlockTest, get) { 1605 MDLocalScope *Scope = getSubprogram(); 1606 MDFile *File = getFile(); 1607 unsigned Line = 5; 1608 unsigned Column = 8; 1609 1610 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column); 1611 1612 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag()); 1613 EXPECT_EQ(Scope, N->getScope()); 1614 EXPECT_EQ(File, N->getFile()); 1615 EXPECT_EQ(Line, N->getLine()); 1616 EXPECT_EQ(Column, N->getColumn()); 1617 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column)); 1618 1619 EXPECT_NE(N, 1620 MDLexicalBlock::get(Context, getSubprogram(), File, Line, Column)); 1621 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, getFile(), Line, Column)); 1622 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column)); 1623 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1)); 1624 1625 TempMDLexicalBlock Temp = N->clone(); 1626 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1627 } 1628 1629 typedef MetadataTest MDLexicalBlockFileTest; 1630 1631 TEST_F(MDLexicalBlockFileTest, get) { 1632 MDLocalScope *Scope = getSubprogram(); 1633 MDFile *File = getFile(); 1634 unsigned Discriminator = 5; 1635 1636 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator); 1637 1638 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag()); 1639 EXPECT_EQ(Scope, N->getScope()); 1640 EXPECT_EQ(File, N->getFile()); 1641 EXPECT_EQ(Discriminator, N->getDiscriminator()); 1642 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator)); 1643 1644 EXPECT_NE(N, MDLexicalBlockFile::get(Context, getSubprogram(), File, 1645 Discriminator)); 1646 EXPECT_NE(N, 1647 MDLexicalBlockFile::get(Context, Scope, getFile(), Discriminator)); 1648 EXPECT_NE(N, 1649 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1)); 1650 1651 TempMDLexicalBlockFile Temp = N->clone(); 1652 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1653 } 1654 1655 typedef MetadataTest MDNamespaceTest; 1656 1657 TEST_F(MDNamespaceTest, get) { 1658 MDScope *Scope = getFile(); 1659 MDFile *File = getFile(); 1660 StringRef Name = "namespace"; 1661 unsigned Line = 5; 1662 1663 auto *N = MDNamespace::get(Context, Scope, File, Name, Line); 1664 1665 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag()); 1666 EXPECT_EQ(Scope, N->getScope()); 1667 EXPECT_EQ(File, N->getFile()); 1668 EXPECT_EQ(Name, N->getName()); 1669 EXPECT_EQ(Line, N->getLine()); 1670 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line)); 1671 1672 EXPECT_NE(N, MDNamespace::get(Context, getFile(), File, Name, Line)); 1673 EXPECT_NE(N, MDNamespace::get(Context, Scope, getFile(), Name, Line)); 1674 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line)); 1675 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1)); 1676 1677 TempMDNamespace Temp = N->clone(); 1678 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1679 } 1680 1681 typedef MetadataTest MDTemplateTypeParameterTest; 1682 1683 TEST_F(MDTemplateTypeParameterTest, get) { 1684 StringRef Name = "template"; 1685 MDTypeRef Type = getBasicType("basic"); 1686 1687 auto *N = MDTemplateTypeParameter::get(Context, Name, Type); 1688 1689 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag()); 1690 EXPECT_EQ(Name, N->getName()); 1691 EXPECT_EQ(Type, N->getType()); 1692 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type)); 1693 1694 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type)); 1695 EXPECT_NE(N, 1696 MDTemplateTypeParameter::get(Context, Name, getBasicType("other"))); 1697 1698 TempMDTemplateTypeParameter Temp = N->clone(); 1699 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1700 } 1701 1702 typedef MetadataTest MDTemplateValueParameterTest; 1703 1704 TEST_F(MDTemplateValueParameterTest, get) { 1705 unsigned Tag = dwarf::DW_TAG_template_value_parameter; 1706 StringRef Name = "template"; 1707 MDTypeRef Type = getBasicType("basic"); 1708 Metadata *Value = getConstantAsMetadata(); 1709 1710 auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value); 1711 EXPECT_EQ(Tag, N->getTag()); 1712 EXPECT_EQ(Name, N->getName()); 1713 EXPECT_EQ(Type, N->getType()); 1714 EXPECT_EQ(Value, N->getValue()); 1715 EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value)); 1716 1717 EXPECT_NE(N, MDTemplateValueParameter::get( 1718 Context, dwarf::DW_TAG_GNU_template_template_param, Name, 1719 Type, Value)); 1720 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type, 1721 Value)); 1722 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, 1723 getBasicType("other"), Value)); 1724 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, 1725 getConstantAsMetadata())); 1726 1727 TempMDTemplateValueParameter Temp = N->clone(); 1728 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1729 } 1730 1731 typedef MetadataTest MDGlobalVariableTest; 1732 1733 TEST_F(MDGlobalVariableTest, get) { 1734 MDScope *Scope = getSubprogram(); 1735 StringRef Name = "name"; 1736 StringRef LinkageName = "linkage"; 1737 MDFile *File = getFile(); 1738 unsigned Line = 5; 1739 MDTypeRef Type = getDerivedType(); 1740 bool IsLocalToUnit = false; 1741 bool IsDefinition = true; 1742 Constant *Variable = getConstant(); 1743 MDDerivedType *StaticDataMemberDeclaration = 1744 cast<MDDerivedType>(getDerivedType()); 1745 1746 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line, 1747 Type, IsLocalToUnit, IsDefinition, Variable, 1748 StaticDataMemberDeclaration); 1749 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag()); 1750 EXPECT_EQ(Scope, N->getScope()); 1751 EXPECT_EQ(Name, N->getName()); 1752 EXPECT_EQ(LinkageName, N->getLinkageName()); 1753 EXPECT_EQ(File, N->getFile()); 1754 EXPECT_EQ(Line, N->getLine()); 1755 EXPECT_EQ(Type, N->getType()); 1756 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit()); 1757 EXPECT_EQ(IsDefinition, N->isDefinition()); 1758 EXPECT_EQ(Variable, N->getVariable()); 1759 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration()); 1760 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, 1761 Line, Type, IsLocalToUnit, IsDefinition, 1762 Variable, StaticDataMemberDeclaration)); 1763 1764 EXPECT_NE(N, 1765 MDGlobalVariable::get(Context, getSubprogram(), Name, LinkageName, 1766 File, Line, Type, IsLocalToUnit, IsDefinition, 1767 Variable, StaticDataMemberDeclaration)); 1768 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File, 1769 Line, Type, IsLocalToUnit, IsDefinition, 1770 Variable, StaticDataMemberDeclaration)); 1771 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line, 1772 Type, IsLocalToUnit, IsDefinition, 1773 Variable, StaticDataMemberDeclaration)); 1774 EXPECT_NE(N, 1775 MDGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(), 1776 Line, Type, IsLocalToUnit, IsDefinition, 1777 Variable, StaticDataMemberDeclaration)); 1778 EXPECT_NE(N, 1779 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, 1780 Line + 1, Type, IsLocalToUnit, IsDefinition, 1781 Variable, StaticDataMemberDeclaration)); 1782 EXPECT_NE(N, 1783 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line, 1784 getDerivedType(), IsLocalToUnit, IsDefinition, 1785 Variable, StaticDataMemberDeclaration)); 1786 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, 1787 Line, Type, !IsLocalToUnit, IsDefinition, 1788 Variable, StaticDataMemberDeclaration)); 1789 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, 1790 Line, Type, IsLocalToUnit, !IsDefinition, 1791 Variable, StaticDataMemberDeclaration)); 1792 EXPECT_NE(N, 1793 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line, 1794 Type, IsLocalToUnit, IsDefinition, 1795 getConstant(), StaticDataMemberDeclaration)); 1796 EXPECT_NE(N, 1797 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line, 1798 Type, IsLocalToUnit, IsDefinition, Variable, 1799 cast<MDDerivedType>(getDerivedType()))); 1800 1801 TempMDGlobalVariable Temp = N->clone(); 1802 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1803 } 1804 1805 typedef MetadataTest MDLocalVariableTest; 1806 1807 TEST_F(MDLocalVariableTest, get) { 1808 unsigned Tag = dwarf::DW_TAG_arg_variable; 1809 MDLocalScope *Scope = getSubprogram(); 1810 StringRef Name = "name"; 1811 MDFile *File = getFile(); 1812 unsigned Line = 5; 1813 MDTypeRef Type = getDerivedType(); 1814 unsigned Arg = 6; 1815 unsigned Flags = 7; 1816 1817 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, 1818 Arg, Flags); 1819 EXPECT_EQ(Tag, N->getTag()); 1820 EXPECT_EQ(Scope, N->getScope()); 1821 EXPECT_EQ(Name, N->getName()); 1822 EXPECT_EQ(File, N->getFile()); 1823 EXPECT_EQ(Line, N->getLine()); 1824 EXPECT_EQ(Type, N->getType()); 1825 EXPECT_EQ(Arg, N->getArg()); 1826 EXPECT_EQ(Flags, N->getFlags()); 1827 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, 1828 Arg, Flags)); 1829 1830 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope, 1831 Name, File, Line, Type, Arg, Flags)); 1832 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, getSubprogram(), Name, File, 1833 Line, Type, Arg, Flags)); 1834 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line, 1835 Type, Arg, Flags)); 1836 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, getFile(), Line, 1837 Type, Arg, Flags)); 1838 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1, 1839 Type, Arg, Flags)); 1840 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, 1841 getDerivedType(), Arg, Flags)); 1842 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, 1843 Arg + 1, Flags)); 1844 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, 1845 Arg, ~Flags)); 1846 1847 TempMDLocalVariable Temp = N->clone(); 1848 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1849 } 1850 1851 typedef MetadataTest MDExpressionTest; 1852 1853 TEST_F(MDExpressionTest, get) { 1854 uint64_t Elements[] = {2, 6, 9, 78, 0}; 1855 auto *N = MDExpression::get(Context, Elements); 1856 EXPECT_EQ(makeArrayRef(Elements), N->getElements()); 1857 EXPECT_EQ(N, MDExpression::get(Context, Elements)); 1858 1859 EXPECT_EQ(5u, N->getNumElements()); 1860 EXPECT_EQ(2u, N->getElement(0)); 1861 EXPECT_EQ(6u, N->getElement(1)); 1862 EXPECT_EQ(9u, N->getElement(2)); 1863 EXPECT_EQ(78u, N->getElement(3)); 1864 EXPECT_EQ(0u, N->getElement(4)); 1865 1866 TempMDExpression Temp = N->clone(); 1867 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1868 } 1869 1870 TEST_F(MDExpressionTest, isValid) { 1871 #define EXPECT_VALID(...) \ 1872 do { \ 1873 uint64_t Elements[] = {__VA_ARGS__}; \ 1874 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \ 1875 } while (false) 1876 #define EXPECT_INVALID(...) \ 1877 do { \ 1878 uint64_t Elements[] = {__VA_ARGS__}; \ 1879 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \ 1880 } while (false) 1881 1882 // Empty expression should be valid. 1883 EXPECT_TRUE(MDExpression::get(Context, None)); 1884 1885 // Valid constructions. 1886 EXPECT_VALID(dwarf::DW_OP_plus, 6); 1887 EXPECT_VALID(dwarf::DW_OP_deref); 1888 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7); 1889 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref); 1890 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6); 1891 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7); 1892 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7); 1893 1894 // Invalid constructions. 1895 EXPECT_INVALID(~0u); 1896 EXPECT_INVALID(dwarf::DW_OP_plus); 1897 EXPECT_INVALID(dwarf::DW_OP_bit_piece); 1898 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3); 1899 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3); 1900 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref); 1901 1902 #undef EXPECT_VALID 1903 #undef EXPECT_INVALID 1904 } 1905 1906 typedef MetadataTest MDObjCPropertyTest; 1907 1908 TEST_F(MDObjCPropertyTest, get) { 1909 StringRef Name = "name"; 1910 MDFile *File = getFile(); 1911 unsigned Line = 5; 1912 StringRef GetterName = "getter"; 1913 StringRef SetterName = "setter"; 1914 unsigned Attributes = 7; 1915 MDType *Type = cast<MDBasicType>(getBasicType("basic")); 1916 1917 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName, 1918 SetterName, Attributes, Type); 1919 1920 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag()); 1921 EXPECT_EQ(Name, N->getName()); 1922 EXPECT_EQ(File, N->getFile()); 1923 EXPECT_EQ(Line, N->getLine()); 1924 EXPECT_EQ(GetterName, N->getGetterName()); 1925 EXPECT_EQ(SetterName, N->getSetterName()); 1926 EXPECT_EQ(Attributes, N->getAttributes()); 1927 EXPECT_EQ(Type, N->getType()); 1928 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName, 1929 SetterName, Attributes, Type)); 1930 1931 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName, 1932 SetterName, Attributes, Type)); 1933 EXPECT_NE(N, MDObjCProperty::get(Context, Name, getFile(), Line, GetterName, 1934 SetterName, Attributes, Type)); 1935 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName, 1936 SetterName, Attributes, Type)); 1937 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other", 1938 SetterName, Attributes, Type)); 1939 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName, 1940 "other", Attributes, Type)); 1941 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName, 1942 SetterName, Attributes + 1, Type)); 1943 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName, 1944 SetterName, Attributes, 1945 cast<MDBasicType>(getBasicType("other")))); 1946 1947 TempMDObjCProperty Temp = N->clone(); 1948 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1949 } 1950 1951 typedef MetadataTest MDImportedEntityTest; 1952 1953 TEST_F(MDImportedEntityTest, get) { 1954 unsigned Tag = dwarf::DW_TAG_imported_module; 1955 MDScope *Scope = getSubprogram(); 1956 DebugNodeRef Entity = getCompositeType(); 1957 unsigned Line = 5; 1958 StringRef Name = "name"; 1959 1960 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name); 1961 1962 EXPECT_EQ(Tag, N->getTag()); 1963 EXPECT_EQ(Scope, N->getScope()); 1964 EXPECT_EQ(Entity, N->getEntity()); 1965 EXPECT_EQ(Line, N->getLine()); 1966 EXPECT_EQ(Name, N->getName()); 1967 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name)); 1968 1969 EXPECT_NE(N, 1970 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration, 1971 Scope, Entity, Line, Name)); 1972 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, getSubprogram(), Entity, 1973 Line, Name)); 1974 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, getCompositeType(), 1975 Line, Name)); 1976 EXPECT_NE(N, 1977 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name)); 1978 EXPECT_NE(N, 1979 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other")); 1980 1981 TempMDImportedEntity Temp = N->clone(); 1982 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1983 } 1984 1985 typedef MetadataTest MetadataAsValueTest; 1986 1987 TEST_F(MetadataAsValueTest, MDNode) { 1988 MDNode *N = MDNode::get(Context, None); 1989 auto *V = MetadataAsValue::get(Context, N); 1990 EXPECT_TRUE(V->getType()->isMetadataTy()); 1991 EXPECT_EQ(N, V->getMetadata()); 1992 1993 auto *V2 = MetadataAsValue::get(Context, N); 1994 EXPECT_EQ(V, V2); 1995 } 1996 1997 TEST_F(MetadataAsValueTest, MDNodeMDNode) { 1998 MDNode *N = MDNode::get(Context, None); 1999 Metadata *Ops[] = {N}; 2000 MDNode *N2 = MDNode::get(Context, Ops); 2001 auto *V = MetadataAsValue::get(Context, N2); 2002 EXPECT_TRUE(V->getType()->isMetadataTy()); 2003 EXPECT_EQ(N2, V->getMetadata()); 2004 2005 auto *V2 = MetadataAsValue::get(Context, N2); 2006 EXPECT_EQ(V, V2); 2007 2008 auto *V3 = MetadataAsValue::get(Context, N); 2009 EXPECT_TRUE(V3->getType()->isMetadataTy()); 2010 EXPECT_NE(V, V3); 2011 EXPECT_EQ(N, V3->getMetadata()); 2012 } 2013 2014 TEST_F(MetadataAsValueTest, MDNodeConstant) { 2015 auto *C = ConstantInt::getTrue(Context); 2016 auto *MD = ConstantAsMetadata::get(C); 2017 Metadata *Ops[] = {MD}; 2018 auto *N = MDNode::get(Context, Ops); 2019 2020 auto *V = MetadataAsValue::get(Context, MD); 2021 EXPECT_TRUE(V->getType()->isMetadataTy()); 2022 EXPECT_EQ(MD, V->getMetadata()); 2023 2024 auto *V2 = MetadataAsValue::get(Context, N); 2025 EXPECT_EQ(MD, V2->getMetadata()); 2026 EXPECT_EQ(V, V2); 2027 } 2028 2029 typedef MetadataTest ValueAsMetadataTest; 2030 2031 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) { 2032 Type *Ty = Type::getInt1PtrTy(Context); 2033 std::unique_ptr<GlobalVariable> GV0( 2034 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 2035 auto *MD = ValueAsMetadata::get(GV0.get()); 2036 EXPECT_TRUE(MD->getValue() == GV0.get()); 2037 ASSERT_TRUE(GV0->use_empty()); 2038 2039 std::unique_ptr<GlobalVariable> GV1( 2040 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 2041 GV0->replaceAllUsesWith(GV1.get()); 2042 EXPECT_TRUE(MD->getValue() == GV1.get()); 2043 } 2044 2045 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) { 2046 // Create a constant. 2047 ConstantAsMetadata *CI = ConstantAsMetadata::get( 2048 ConstantInt::get(getGlobalContext(), APInt(8, 0))); 2049 2050 // Create a temporary to prevent nodes from resolving. 2051 auto Temp = MDTuple::getTemporary(Context, None); 2052 2053 // When the first operand of N1 gets reset to nullptr, it'll collide with N2. 2054 Metadata *Ops1[] = {CI, CI, Temp.get()}; 2055 Metadata *Ops2[] = {nullptr, CI, Temp.get()}; 2056 2057 auto *N1 = MDTuple::get(Context, Ops1); 2058 auto *N2 = MDTuple::get(Context, Ops2); 2059 ASSERT_NE(N1, N2); 2060 2061 // Tell metadata that the constant is getting deleted. 2062 // 2063 // After this, N1 will be invalid, so don't touch it. 2064 ValueAsMetadata::handleDeletion(CI->getValue()); 2065 EXPECT_EQ(nullptr, N2->getOperand(0)); 2066 EXPECT_EQ(nullptr, N2->getOperand(1)); 2067 EXPECT_EQ(Temp.get(), N2->getOperand(2)); 2068 2069 // Clean up Temp for teardown. 2070 Temp->replaceAllUsesWith(nullptr); 2071 } 2072 2073 typedef MetadataTest TrackingMDRefTest; 2074 2075 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) { 2076 Type *Ty = Type::getInt1PtrTy(Context); 2077 std::unique_ptr<GlobalVariable> GV0( 2078 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 2079 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get())); 2080 EXPECT_TRUE(MD->getValue() == GV0.get()); 2081 ASSERT_TRUE(GV0->use_empty()); 2082 2083 std::unique_ptr<GlobalVariable> GV1( 2084 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 2085 GV0->replaceAllUsesWith(GV1.get()); 2086 EXPECT_TRUE(MD->getValue() == GV1.get()); 2087 2088 // Reset it, so we don't inadvertently test deletion. 2089 MD.reset(); 2090 } 2091 2092 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) { 2093 Type *Ty = Type::getInt1PtrTy(Context); 2094 std::unique_ptr<GlobalVariable> GV( 2095 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 2096 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get())); 2097 EXPECT_TRUE(MD->getValue() == GV.get()); 2098 ASSERT_TRUE(GV->use_empty()); 2099 2100 GV.reset(); 2101 EXPECT_TRUE(!MD); 2102 } 2103 2104 TEST(NamedMDNodeTest, Search) { 2105 LLVMContext Context; 2106 ConstantAsMetadata *C = 2107 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1)); 2108 ConstantAsMetadata *C2 = 2109 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2)); 2110 2111 Metadata *const V = C; 2112 Metadata *const V2 = C2; 2113 MDNode *n = MDNode::get(Context, V); 2114 MDNode *n2 = MDNode::get(Context, V2); 2115 2116 Module M("MyModule", Context); 2117 const char *Name = "llvm.NMD1"; 2118 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name); 2119 NMD->addOperand(n); 2120 NMD->addOperand(n2); 2121 2122 std::string Str; 2123 raw_string_ostream oss(Str); 2124 NMD->print(oss); 2125 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n", 2126 oss.str().c_str()); 2127 } 2128 } 2129