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