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/IR/Metadata.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/IR/Constants.h" 13 #include "llvm/IR/DebugInfo.h" 14 #include "llvm/IR/DebugInfoMetadata.h" 15 #include "llvm/IR/Function.h" 16 #include "llvm/IR/Instructions.h" 17 #include "llvm/IR/LLVMContext.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, DINode::FlagZero, 0, 84 getNode(nullptr)); 85 } 86 DISubprogram *getSubprogram() { 87 return DISubprogram::getDistinct( 88 Context, nullptr, "", "", nullptr, 0, nullptr, 0, nullptr, 0, 0, 89 DINode::FlagZero, DISubprogram::SPFlagZero, nullptr); 90 } 91 DIFile *getFile() { 92 return DIFile::getDistinct(Context, "file.c", "/path/to/dir"); 93 } 94 DICompileUnit *getUnit() { 95 return DICompileUnit::getDistinct( 96 Context, 1, getFile(), "clang", false, "-g", 2, "", 97 DICompileUnit::FullDebug, getTuple(), getTuple(), getTuple(), 98 getTuple(), getTuple(), 0, true, false, 99 DICompileUnit::DebugNameTableKind::Default, false); 100 } 101 DIType *getBasicType(StringRef Name) { 102 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name); 103 } 104 DIType *getDerivedType() { 105 return DIDerivedType::getDistinct( 106 Context, dwarf::DW_TAG_pointer_type, "", nullptr, 0, nullptr, 107 getBasicType("basictype"), 1, 2, 0, None, DINode::FlagZero); 108 } 109 Constant *getConstant() { 110 return ConstantInt::get(Type::getInt32Ty(Context), Counter++); 111 } 112 ConstantAsMetadata *getConstantAsMetadata() { 113 return ConstantAsMetadata::get(getConstant()); 114 } 115 DIType *getCompositeType() { 116 return DICompositeType::getDistinct( 117 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, nullptr, 118 32, 32, 0, DINode::FlagZero, nullptr, 0, nullptr, nullptr, ""); 119 } 120 Function *getFunction(StringRef Name) { 121 return cast<Function>(M.getOrInsertFunction( 122 Name, FunctionType::get(Type::getVoidTy(Context), None, false))); 123 } 124 }; 125 typedef MetadataTest MDStringTest; 126 127 // Test that construction of MDString with different value produces different 128 // MDString objects, even with the same string pointer and nulls in the string. 129 TEST_F(MDStringTest, CreateDifferent) { 130 char x[3] = { 'f', 0, 'A' }; 131 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); 132 x[2] = 'B'; 133 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3)); 134 EXPECT_NE(s1, s2); 135 } 136 137 // Test that creation of MDStrings with the same string contents produces the 138 // same MDString object, even with different pointers. 139 TEST_F(MDStringTest, CreateSame) { 140 char x[4] = { 'a', 'b', 'c', 'X' }; 141 char y[4] = { 'a', 'b', 'c', 'Y' }; 142 143 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); 144 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); 145 EXPECT_EQ(s1, s2); 146 } 147 148 // Test that MDString prints out the string we fed it. 149 TEST_F(MDStringTest, PrintingSimple) { 150 char str[14] = "testing 1 2 3"; 151 MDString *s = MDString::get(Context, StringRef(&str[0], 13)); 152 strncpy(str, "aaaaaaaaaaaaa", 14); 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 I->deleteValue(); 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 404 TEST_F(MDNodeTest, PrintWithDroppedCallOperand) { 405 Module M("test", Context); 406 407 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false); 408 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M); 409 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M); 410 auto *BB0 = BasicBlock::Create(Context, "entry", F0); 411 412 CallInst *CI0 = CallInst::Create(F1, "", BB0); 413 CI0->dropAllReferences(); 414 415 auto *R0 = ReturnInst::Create(Context, BB0); 416 auto *N0 = MDNode::getDistinct(Context, None); 417 R0->setMetadata("md", N0); 418 419 // Printing the metadata node would previously result in a failed assertion 420 // due to the call instruction's dropped function operand. 421 ModuleSlotTracker MST(&M); 422 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST)); 423 } 424 #undef EXPECT_PRINTER_EQ 425 426 TEST_F(MDNodeTest, NullOperand) { 427 // metadata !{} 428 MDNode *Empty = MDNode::get(Context, None); 429 430 // metadata !{metadata !{}} 431 Metadata *Ops[] = {Empty}; 432 MDNode *N = MDNode::get(Context, Ops); 433 ASSERT_EQ(Empty, N->getOperand(0)); 434 435 // metadata !{metadata !{}} => metadata !{null} 436 N->replaceOperandWith(0, nullptr); 437 ASSERT_EQ(nullptr, N->getOperand(0)); 438 439 // metadata !{null} 440 Ops[0] = nullptr; 441 MDNode *NullOp = MDNode::get(Context, Ops); 442 ASSERT_EQ(nullptr, NullOp->getOperand(0)); 443 EXPECT_EQ(N, NullOp); 444 } 445 446 TEST_F(MDNodeTest, DistinctOnUniquingCollision) { 447 // !{} 448 MDNode *Empty = MDNode::get(Context, None); 449 ASSERT_TRUE(Empty->isResolved()); 450 EXPECT_FALSE(Empty->isDistinct()); 451 452 // !{!{}} 453 Metadata *Wrapped1Ops[] = {Empty}; 454 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops); 455 ASSERT_EQ(Empty, Wrapped1->getOperand(0)); 456 ASSERT_TRUE(Wrapped1->isResolved()); 457 EXPECT_FALSE(Wrapped1->isDistinct()); 458 459 // !{!{!{}}} 460 Metadata *Wrapped2Ops[] = {Wrapped1}; 461 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops); 462 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0)); 463 ASSERT_TRUE(Wrapped2->isResolved()); 464 EXPECT_FALSE(Wrapped2->isDistinct()); 465 466 // !{!{!{}}} => !{!{}} 467 Wrapped2->replaceOperandWith(0, Empty); 468 ASSERT_EQ(Empty, Wrapped2->getOperand(0)); 469 EXPECT_TRUE(Wrapped2->isDistinct()); 470 EXPECT_FALSE(Wrapped1->isDistinct()); 471 } 472 473 TEST_F(MDNodeTest, UniquedOnDeletedOperand) { 474 // temp !{} 475 TempMDTuple T = MDTuple::getTemporary(Context, None); 476 477 // !{temp !{}} 478 Metadata *Ops[] = {T.get()}; 479 MDTuple *N = MDTuple::get(Context, Ops); 480 481 // !{temp !{}} => !{null} 482 T.reset(); 483 ASSERT_TRUE(N->isUniqued()); 484 Metadata *NullOps[] = {nullptr}; 485 ASSERT_EQ(N, MDTuple::get(Context, NullOps)); 486 } 487 488 TEST_F(MDNodeTest, DistinctOnDeletedValueOperand) { 489 // i1* @GV 490 Type *Ty = Type::getInt1PtrTy(Context); 491 std::unique_ptr<GlobalVariable> GV( 492 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 493 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get()); 494 495 // !{i1* @GV} 496 Metadata *Ops[] = {Op}; 497 MDTuple *N = MDTuple::get(Context, Ops); 498 499 // !{i1* @GV} => !{null} 500 GV.reset(); 501 ASSERT_TRUE(N->isDistinct()); 502 ASSERT_EQ(nullptr, N->getOperand(0)); 503 Metadata *NullOps[] = {nullptr}; 504 ASSERT_NE(N, MDTuple::get(Context, NullOps)); 505 } 506 507 TEST_F(MDNodeTest, getDistinct) { 508 // !{} 509 MDNode *Empty = MDNode::get(Context, None); 510 ASSERT_TRUE(Empty->isResolved()); 511 ASSERT_FALSE(Empty->isDistinct()); 512 ASSERT_EQ(Empty, MDNode::get(Context, None)); 513 514 // distinct !{} 515 MDNode *Distinct1 = MDNode::getDistinct(Context, None); 516 MDNode *Distinct2 = MDNode::getDistinct(Context, None); 517 EXPECT_TRUE(Distinct1->isResolved()); 518 EXPECT_TRUE(Distinct2->isDistinct()); 519 EXPECT_NE(Empty, Distinct1); 520 EXPECT_NE(Empty, Distinct2); 521 EXPECT_NE(Distinct1, Distinct2); 522 523 // !{} 524 ASSERT_EQ(Empty, MDNode::get(Context, None)); 525 } 526 527 TEST_F(MDNodeTest, isUniqued) { 528 MDNode *U = MDTuple::get(Context, None); 529 MDNode *D = MDTuple::getDistinct(Context, None); 530 auto T = MDTuple::getTemporary(Context, None); 531 EXPECT_TRUE(U->isUniqued()); 532 EXPECT_FALSE(D->isUniqued()); 533 EXPECT_FALSE(T->isUniqued()); 534 } 535 536 TEST_F(MDNodeTest, isDistinct) { 537 MDNode *U = MDTuple::get(Context, None); 538 MDNode *D = MDTuple::getDistinct(Context, None); 539 auto T = MDTuple::getTemporary(Context, None); 540 EXPECT_FALSE(U->isDistinct()); 541 EXPECT_TRUE(D->isDistinct()); 542 EXPECT_FALSE(T->isDistinct()); 543 } 544 545 TEST_F(MDNodeTest, isTemporary) { 546 MDNode *U = MDTuple::get(Context, None); 547 MDNode *D = MDTuple::getDistinct(Context, None); 548 auto T = MDTuple::getTemporary(Context, None); 549 EXPECT_FALSE(U->isTemporary()); 550 EXPECT_FALSE(D->isTemporary()); 551 EXPECT_TRUE(T->isTemporary()); 552 } 553 554 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) { 555 // temporary !{} 556 auto Temp = MDTuple::getTemporary(Context, None); 557 ASSERT_FALSE(Temp->isResolved()); 558 559 // distinct !{temporary !{}} 560 Metadata *Ops[] = {Temp.get()}; 561 MDNode *Distinct = MDNode::getDistinct(Context, Ops); 562 EXPECT_TRUE(Distinct->isResolved()); 563 EXPECT_EQ(Temp.get(), Distinct->getOperand(0)); 564 565 // temporary !{} => !{} 566 MDNode *Empty = MDNode::get(Context, None); 567 Temp->replaceAllUsesWith(Empty); 568 EXPECT_EQ(Empty, Distinct->getOperand(0)); 569 } 570 571 TEST_F(MDNodeTest, handleChangedOperandRecursion) { 572 // !0 = !{} 573 MDNode *N0 = MDNode::get(Context, None); 574 575 // !1 = !{!3, null} 576 auto Temp3 = MDTuple::getTemporary(Context, None); 577 Metadata *Ops1[] = {Temp3.get(), nullptr}; 578 MDNode *N1 = MDNode::get(Context, Ops1); 579 580 // !2 = !{!3, !0} 581 Metadata *Ops2[] = {Temp3.get(), N0}; 582 MDNode *N2 = MDNode::get(Context, Ops2); 583 584 // !3 = !{!2} 585 Metadata *Ops3[] = {N2}; 586 MDNode *N3 = MDNode::get(Context, Ops3); 587 Temp3->replaceAllUsesWith(N3); 588 589 // !4 = !{!1} 590 Metadata *Ops4[] = {N1}; 591 MDNode *N4 = MDNode::get(Context, Ops4); 592 593 // Confirm that the cycle prevented RAUW from getting dropped. 594 EXPECT_TRUE(N0->isResolved()); 595 EXPECT_FALSE(N1->isResolved()); 596 EXPECT_FALSE(N2->isResolved()); 597 EXPECT_FALSE(N3->isResolved()); 598 EXPECT_FALSE(N4->isResolved()); 599 600 // Create a couple of distinct nodes to observe what's going on. 601 // 602 // !5 = distinct !{!2} 603 // !6 = distinct !{!3} 604 Metadata *Ops5[] = {N2}; 605 MDNode *N5 = MDNode::getDistinct(Context, Ops5); 606 Metadata *Ops6[] = {N3}; 607 MDNode *N6 = MDNode::getDistinct(Context, Ops6); 608 609 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW). 610 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2 611 // references !3, this can cause a re-entry of handleChangedOperand() when !3 612 // is not ready for it. 613 // 614 // !2->replaceOperandWith(1, nullptr) 615 // !2: !{!3, !0} => !{!3, null} 616 // !2->replaceAllUsesWith(!1) 617 // !3: !{!2] => !{!1} 618 // !3->replaceAllUsesWith(!4) 619 N2->replaceOperandWith(1, nullptr); 620 621 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from 622 // under us. Just check that the other nodes are sane. 623 // 624 // !1 = !{!4, null} 625 // !4 = !{!1} 626 // !5 = distinct !{!1} 627 // !6 = distinct !{!4} 628 EXPECT_EQ(N4, N1->getOperand(0)); 629 EXPECT_EQ(N1, N4->getOperand(0)); 630 EXPECT_EQ(N1, N5->getOperand(0)); 631 EXPECT_EQ(N4, N6->getOperand(0)); 632 } 633 634 TEST_F(MDNodeTest, replaceResolvedOperand) { 635 // Check code for replacing one resolved operand with another. If doing this 636 // directly (via replaceOperandWith()) becomes illegal, change the operand to 637 // a global value that gets RAUW'ed. 638 // 639 // Use a temporary node to keep N from being resolved. 640 auto Temp = MDTuple::getTemporary(Context, None); 641 Metadata *Ops[] = {nullptr, Temp.get()}; 642 643 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>()); 644 MDNode *N = MDTuple::get(Context, Ops); 645 EXPECT_EQ(nullptr, N->getOperand(0)); 646 ASSERT_FALSE(N->isResolved()); 647 648 // Check code for replacing resolved nodes. 649 N->replaceOperandWith(0, Empty); 650 EXPECT_EQ(Empty, N->getOperand(0)); 651 652 // Check code for adding another unresolved operand. 653 N->replaceOperandWith(0, Temp.get()); 654 EXPECT_EQ(Temp.get(), N->getOperand(0)); 655 656 // Remove the references to Temp; required for teardown. 657 Temp->replaceAllUsesWith(nullptr); 658 } 659 660 TEST_F(MDNodeTest, replaceWithUniqued) { 661 auto *Empty = MDTuple::get(Context, None); 662 MDTuple *FirstUniqued; 663 { 664 Metadata *Ops[] = {Empty}; 665 auto Temp = MDTuple::getTemporary(Context, Ops); 666 EXPECT_TRUE(Temp->isTemporary()); 667 668 // Don't expect a collision. 669 auto *Current = Temp.get(); 670 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp)); 671 EXPECT_TRUE(FirstUniqued->isUniqued()); 672 EXPECT_TRUE(FirstUniqued->isResolved()); 673 EXPECT_EQ(Current, FirstUniqued); 674 } 675 { 676 Metadata *Ops[] = {Empty}; 677 auto Temp = MDTuple::getTemporary(Context, Ops); 678 EXPECT_TRUE(Temp->isTemporary()); 679 680 // Should collide with Uniqued above this time. 681 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp)); 682 EXPECT_TRUE(Uniqued->isUniqued()); 683 EXPECT_TRUE(Uniqued->isResolved()); 684 EXPECT_EQ(FirstUniqued, Uniqued); 685 } 686 { 687 auto Unresolved = MDTuple::getTemporary(Context, None); 688 Metadata *Ops[] = {Unresolved.get()}; 689 auto Temp = MDTuple::getTemporary(Context, Ops); 690 EXPECT_TRUE(Temp->isTemporary()); 691 692 // Shouldn't be resolved. 693 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp)); 694 EXPECT_TRUE(Uniqued->isUniqued()); 695 EXPECT_FALSE(Uniqued->isResolved()); 696 697 // Should be a different node. 698 EXPECT_NE(FirstUniqued, Uniqued); 699 700 // Should resolve when we update its node (note: be careful to avoid a 701 // collision with any other nodes above). 702 Uniqued->replaceOperandWith(0, nullptr); 703 EXPECT_TRUE(Uniqued->isResolved()); 704 } 705 } 706 707 TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) { 708 // temp !{} 709 MDTuple *Op = MDTuple::getTemporary(Context, None).release(); 710 EXPECT_FALSE(Op->isResolved()); 711 712 // temp !{temp !{}} 713 Metadata *Ops[] = {Op}; 714 MDTuple *N = MDTuple::getTemporary(Context, Ops).release(); 715 EXPECT_FALSE(N->isResolved()); 716 717 // temp !{temp !{}} => !{temp !{}} 718 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N))); 719 EXPECT_FALSE(N->isResolved()); 720 721 // !{temp !{}} => !{!{}} 722 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op))); 723 EXPECT_TRUE(Op->isResolved()); 724 EXPECT_TRUE(N->isResolved()); 725 } 726 727 TEST_F(MDNodeTest, replaceWithUniquedDeletedOperand) { 728 // i1* @GV 729 Type *Ty = Type::getInt1PtrTy(Context); 730 std::unique_ptr<GlobalVariable> GV( 731 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 732 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get()); 733 734 // temp !{i1* @GV} 735 Metadata *Ops[] = {Op}; 736 MDTuple *N = MDTuple::getTemporary(Context, Ops).release(); 737 738 // temp !{i1* @GV} => !{i1* @GV} 739 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N))); 740 ASSERT_TRUE(N->isUniqued()); 741 742 // !{i1* @GV} => !{null} 743 GV.reset(); 744 ASSERT_TRUE(N->isDistinct()); 745 ASSERT_EQ(nullptr, N->getOperand(0)); 746 Metadata *NullOps[] = {nullptr}; 747 ASSERT_NE(N, MDTuple::get(Context, NullOps)); 748 } 749 750 TEST_F(MDNodeTest, replaceWithUniquedChangedOperand) { 751 // i1* @GV 752 Type *Ty = Type::getInt1PtrTy(Context); 753 std::unique_ptr<GlobalVariable> GV( 754 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 755 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get()); 756 757 // temp !{i1* @GV} 758 Metadata *Ops[] = {Op}; 759 MDTuple *N = MDTuple::getTemporary(Context, Ops).release(); 760 761 // temp !{i1* @GV} => !{i1* @GV} 762 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N))); 763 ASSERT_TRUE(N->isUniqued()); 764 765 // !{i1* @GV} => !{i1* @GV2} 766 std::unique_ptr<GlobalVariable> GV2( 767 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 768 GV->replaceAllUsesWith(GV2.get()); 769 ASSERT_TRUE(N->isUniqued()); 770 Metadata *NullOps[] = {ConstantAsMetadata::get(GV2.get())}; 771 ASSERT_EQ(N, MDTuple::get(Context, NullOps)); 772 } 773 774 TEST_F(MDNodeTest, replaceWithDistinct) { 775 { 776 auto *Empty = MDTuple::get(Context, None); 777 Metadata *Ops[] = {Empty}; 778 auto Temp = MDTuple::getTemporary(Context, Ops); 779 EXPECT_TRUE(Temp->isTemporary()); 780 781 // Don't expect a collision. 782 auto *Current = Temp.get(); 783 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp)); 784 EXPECT_TRUE(Distinct->isDistinct()); 785 EXPECT_TRUE(Distinct->isResolved()); 786 EXPECT_EQ(Current, Distinct); 787 } 788 { 789 auto Unresolved = MDTuple::getTemporary(Context, None); 790 Metadata *Ops[] = {Unresolved.get()}; 791 auto Temp = MDTuple::getTemporary(Context, Ops); 792 EXPECT_TRUE(Temp->isTemporary()); 793 794 // Don't expect a collision. 795 auto *Current = Temp.get(); 796 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp)); 797 EXPECT_TRUE(Distinct->isDistinct()); 798 EXPECT_TRUE(Distinct->isResolved()); 799 EXPECT_EQ(Current, Distinct); 800 801 // Cleanup; required for teardown. 802 Unresolved->replaceAllUsesWith(nullptr); 803 } 804 } 805 806 TEST_F(MDNodeTest, replaceWithPermanent) { 807 Metadata *Ops[] = {nullptr}; 808 auto Temp = MDTuple::getTemporary(Context, Ops); 809 auto *T = Temp.get(); 810 811 // U is a normal, uniqued node that references T. 812 auto *U = MDTuple::get(Context, T); 813 EXPECT_TRUE(U->isUniqued()); 814 815 // Make Temp self-referencing. 816 Temp->replaceOperandWith(0, T); 817 818 // Try to uniquify Temp. This should, despite the name in the API, give a 819 // 'distinct' node, since self-references aren't allowed to be uniqued. 820 // 821 // Since it's distinct, N should have the same address as when it was a 822 // temporary (i.e., be equal to T not U). 823 auto *N = MDNode::replaceWithPermanent(std::move(Temp)); 824 EXPECT_EQ(N, T); 825 EXPECT_TRUE(N->isDistinct()); 826 827 // U should be the canonical unique node with N as the argument. 828 EXPECT_EQ(U, MDTuple::get(Context, N)); 829 EXPECT_TRUE(U->isUniqued()); 830 831 // This temporary should collide with U when replaced, but it should still be 832 // uniqued. 833 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N))); 834 EXPECT_TRUE(U->isUniqued()); 835 836 // This temporary should become a new uniqued node. 837 auto Temp2 = MDTuple::getTemporary(Context, U); 838 auto *V = Temp2.get(); 839 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2))); 840 EXPECT_TRUE(V->isUniqued()); 841 EXPECT_EQ(U, V->getOperand(0)); 842 } 843 844 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) { 845 TrackingMDRef Ref; 846 EXPECT_EQ(nullptr, Ref.get()); 847 { 848 auto Temp = MDTuple::getTemporary(Context, None); 849 Ref.reset(Temp.get()); 850 EXPECT_EQ(Temp.get(), Ref.get()); 851 } 852 EXPECT_EQ(nullptr, Ref.get()); 853 } 854 855 typedef MetadataTest DILocationTest; 856 857 TEST_F(DILocationTest, Overflow) { 858 DISubprogram *N = getSubprogram(); 859 { 860 DILocation *L = DILocation::get(Context, 2, 7, N); 861 EXPECT_EQ(2u, L->getLine()); 862 EXPECT_EQ(7u, L->getColumn()); 863 } 864 unsigned U16 = 1u << 16; 865 { 866 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N); 867 EXPECT_EQ(UINT32_MAX, L->getLine()); 868 EXPECT_EQ(U16 - 1, L->getColumn()); 869 } 870 { 871 DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N); 872 EXPECT_EQ(UINT32_MAX, L->getLine()); 873 EXPECT_EQ(0u, L->getColumn()); 874 } 875 { 876 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N); 877 EXPECT_EQ(UINT32_MAX, L->getLine()); 878 EXPECT_EQ(0u, L->getColumn()); 879 } 880 } 881 882 TEST_F(DILocationTest, Merge) { 883 DISubprogram *N = getSubprogram(); 884 DIScope *S = DILexicalBlock::get(Context, N, getFile(), 3, 4); 885 886 { 887 // Identical. 888 auto *A = DILocation::get(Context, 2, 7, N); 889 auto *B = DILocation::get(Context, 2, 7, N); 890 auto *M = DILocation::getMergedLocation(A, B); 891 EXPECT_EQ(2u, M->getLine()); 892 EXPECT_EQ(7u, M->getColumn()); 893 EXPECT_EQ(N, M->getScope()); 894 } 895 896 { 897 // Identical, different scopes. 898 auto *A = DILocation::get(Context, 2, 7, N); 899 auto *B = DILocation::get(Context, 2, 7, S); 900 auto *M = DILocation::getMergedLocation(A, B); 901 EXPECT_EQ(0u, M->getLine()); // FIXME: Should this be 2? 902 EXPECT_EQ(0u, M->getColumn()); // FIXME: Should this be 7? 903 EXPECT_EQ(N, M->getScope()); 904 } 905 906 { 907 // Different lines, same scopes. 908 auto *A = DILocation::get(Context, 1, 6, N); 909 auto *B = DILocation::get(Context, 2, 7, N); 910 auto *M = DILocation::getMergedLocation(A, B); 911 EXPECT_EQ(0u, M->getLine()); 912 EXPECT_EQ(0u, M->getColumn()); 913 EXPECT_EQ(N, M->getScope()); 914 } 915 916 { 917 // Twisty locations, all different, same function. 918 auto *A = DILocation::get(Context, 1, 6, N); 919 auto *B = DILocation::get(Context, 2, 7, S); 920 auto *M = DILocation::getMergedLocation(A, B); 921 EXPECT_EQ(0u, M->getLine()); 922 EXPECT_EQ(0u, M->getColumn()); 923 EXPECT_EQ(N, M->getScope()); 924 } 925 926 { 927 // Different function, same inlined-at. 928 auto *F = getFile(); 929 auto *SP1 = DISubprogram::getDistinct(Context, F, "a", "a", F, 0, nullptr, 930 0, nullptr, 0, 0, DINode::FlagZero, 931 DISubprogram::SPFlagZero, nullptr); 932 auto *SP2 = DISubprogram::getDistinct(Context, F, "b", "b", F, 0, nullptr, 933 0, nullptr, 0, 0, DINode::FlagZero, 934 DISubprogram::SPFlagZero, nullptr); 935 936 auto *I = DILocation::get(Context, 2, 7, N); 937 auto *A = DILocation::get(Context, 1, 6, SP1, I); 938 auto *B = DILocation::get(Context, 2, 7, SP2, I); 939 auto *M = DILocation::getMergedLocation(A, B); 940 EXPECT_EQ(0u, M->getLine()); 941 EXPECT_EQ(0u, M->getColumn()); 942 EXPECT_TRUE(isa<DILocalScope>(M->getScope())); 943 EXPECT_EQ(I, M->getInlinedAt()); 944 } 945 946 { 947 // Completely different. 948 auto *I = DILocation::get(Context, 2, 7, N); 949 auto *A = DILocation::get(Context, 1, 6, S, I); 950 auto *B = DILocation::get(Context, 2, 7, getSubprogram()); 951 auto *M = DILocation::getMergedLocation(A, B); 952 EXPECT_EQ(0u, M->getLine()); 953 EXPECT_EQ(0u, M->getColumn()); 954 EXPECT_TRUE(isa<DILocalScope>(M->getScope())); 955 EXPECT_EQ(S, M->getScope()); 956 EXPECT_EQ(nullptr, M->getInlinedAt()); 957 } 958 } 959 960 TEST_F(DILocationTest, getDistinct) { 961 MDNode *N = getSubprogram(); 962 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N); 963 EXPECT_TRUE(L0->isDistinct()); 964 DILocation *L1 = DILocation::get(Context, 2, 7, N); 965 EXPECT_FALSE(L1->isDistinct()); 966 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N)); 967 } 968 969 TEST_F(DILocationTest, getTemporary) { 970 MDNode *N = MDNode::get(Context, None); 971 auto L = DILocation::getTemporary(Context, 2, 7, N); 972 EXPECT_TRUE(L->isTemporary()); 973 EXPECT_FALSE(L->isResolved()); 974 } 975 976 TEST_F(DILocationTest, cloneTemporary) { 977 MDNode *N = MDNode::get(Context, None); 978 auto L = DILocation::getTemporary(Context, 2, 7, N); 979 EXPECT_TRUE(L->isTemporary()); 980 auto L2 = L->clone(); 981 EXPECT_TRUE(L2->isTemporary()); 982 } 983 984 TEST_F(DILocationTest, discriminatorEncoding) { 985 EXPECT_EQ(0U, DILocation::encodeDiscriminator(0, 0, 0).getValue()); 986 987 // Encode base discriminator as a component: lsb is 0, then the value. 988 // The other components are all absent, so we leave all the other bits 0. 989 EXPECT_EQ(2U, DILocation::encodeDiscriminator(1, 0, 0).getValue()); 990 991 // Base discriminator component is empty, so lsb is 1. Next component is not 992 // empty, so its lsb is 0, then its value (1). Next component is empty. 993 // So the bit pattern is 101. 994 EXPECT_EQ(5U, DILocation::encodeDiscriminator(0, 1, 0).getValue()); 995 996 // First 2 components are empty, so the bit pattern is 11. Then the 997 // next component - ending up with 1011. 998 EXPECT_EQ(0xbU, DILocation::encodeDiscriminator(0, 0, 1).getValue()); 999 1000 // The bit pattern for the first 2 components is 11. The next bit is 0, 1001 // because the last component is not empty. We have 29 bits usable for 1002 // encoding, but we cap it at 12 bits uniformously for all components. We 1003 // encode the last component over 14 bits. 1004 EXPECT_EQ(0xfffbU, DILocation::encodeDiscriminator(0, 0, 0xfff).getValue()); 1005 1006 EXPECT_EQ(0x102U, DILocation::encodeDiscriminator(1, 1, 0).getValue()); 1007 1008 EXPECT_EQ(0x13eU, DILocation::encodeDiscriminator(0x1f, 1, 0).getValue()); 1009 1010 EXPECT_EQ(0x87feU, DILocation::encodeDiscriminator(0x1ff, 1, 0).getValue()); 1011 1012 EXPECT_EQ(0x1f3eU, DILocation::encodeDiscriminator(0x1f, 0x1f, 0).getValue()); 1013 1014 EXPECT_EQ(0x3ff3eU, 1015 DILocation::encodeDiscriminator(0x1f, 0x1ff, 0).getValue()); 1016 1017 EXPECT_EQ(0x1ff87feU, 1018 DILocation::encodeDiscriminator(0x1ff, 0x1ff, 0).getValue()); 1019 1020 EXPECT_EQ(0xfff9f3eU, 1021 DILocation::encodeDiscriminator(0x1f, 0x1f, 0xfff).getValue()); 1022 1023 EXPECT_EQ(0xffc3ff3eU, 1024 DILocation::encodeDiscriminator(0x1f, 0x1ff, 0x1ff).getValue()); 1025 1026 EXPECT_EQ(0xffcf87feU, 1027 DILocation::encodeDiscriminator(0x1ff, 0x1f, 0x1ff).getValue()); 1028 1029 EXPECT_EQ(0xe1ff87feU, 1030 DILocation::encodeDiscriminator(0x1ff, 0x1ff, 7).getValue()); 1031 } 1032 1033 TEST_F(DILocationTest, discriminatorEncodingNegativeTests) { 1034 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0, 0x1000)); 1035 EXPECT_EQ(None, DILocation::encodeDiscriminator(0x1000, 0, 0)); 1036 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0x1000, 0)); 1037 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0, 0x1000)); 1038 EXPECT_EQ(None, DILocation::encodeDiscriminator(0x1ff, 0x1ff, 8)); 1039 EXPECT_EQ(None, 1040 DILocation::encodeDiscriminator(std::numeric_limits<uint32_t>::max(), 1041 std::numeric_limits<uint32_t>::max(), 1042 0)); 1043 } 1044 1045 TEST_F(DILocationTest, discriminatorSpecialCases) { 1046 // We don't test getCopyIdentifier here because the only way 1047 // to set it is by constructing an encoded discriminator using 1048 // encodeDiscriminator, which is already tested. 1049 auto L1 = DILocation::get(Context, 1, 2, getSubprogram()); 1050 EXPECT_EQ(0U, L1->getBaseDiscriminator()); 1051 EXPECT_EQ(1U, L1->getDuplicationFactor()); 1052 1053 auto L2 = L1->setBaseDiscriminator(1).getValue(); 1054 EXPECT_EQ(0U, L1->getBaseDiscriminator()); 1055 EXPECT_EQ(1U, L1->getDuplicationFactor()); 1056 1057 EXPECT_EQ(1U, L2->getBaseDiscriminator()); 1058 EXPECT_EQ(1U, L2->getDuplicationFactor()); 1059 1060 auto L3 = L2->cloneWithDuplicationFactor(2).getValue(); 1061 EXPECT_EQ(1U, L3->getBaseDiscriminator()); 1062 EXPECT_EQ(2U, L3->getDuplicationFactor()); 1063 1064 auto L4 = L3->cloneWithDuplicationFactor(4).getValue(); 1065 EXPECT_EQ(1U, L4->getBaseDiscriminator()); 1066 EXPECT_EQ(8U, L4->getDuplicationFactor()); 1067 1068 auto L5 = L4->setBaseDiscriminator(2).getValue(); 1069 EXPECT_EQ(2U, L5->getBaseDiscriminator()); 1070 EXPECT_EQ(1U, L5->getDuplicationFactor()); 1071 1072 // Check extreme cases 1073 auto L6 = L1->setBaseDiscriminator(0xfff).getValue(); 1074 EXPECT_EQ(0xfffU, L6->getBaseDiscriminator()); 1075 EXPECT_EQ( 1076 0xfffU, 1077 L6->cloneWithDuplicationFactor(0xfff).getValue()->getDuplicationFactor()); 1078 1079 // Check we return None for unencodable cases. 1080 EXPECT_EQ(None, L4->setBaseDiscriminator(0x1000)); 1081 EXPECT_EQ(None, L4->cloneWithDuplicationFactor(0x1000)); 1082 } 1083 1084 1085 typedef MetadataTest GenericDINodeTest; 1086 1087 TEST_F(GenericDINodeTest, get) { 1088 StringRef Header = "header"; 1089 auto *Empty = MDNode::get(Context, None); 1090 Metadata *Ops1[] = {Empty}; 1091 auto *N = GenericDINode::get(Context, 15, Header, Ops1); 1092 EXPECT_EQ(15u, N->getTag()); 1093 EXPECT_EQ(2u, N->getNumOperands()); 1094 EXPECT_EQ(Header, N->getHeader()); 1095 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0)); 1096 EXPECT_EQ(1u, N->getNumDwarfOperands()); 1097 EXPECT_EQ(Empty, N->getDwarfOperand(0)); 1098 EXPECT_EQ(Empty, N->getOperand(1)); 1099 ASSERT_TRUE(N->isUniqued()); 1100 1101 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1)); 1102 1103 N->replaceOperandWith(1, nullptr); 1104 EXPECT_EQ(15u, N->getTag()); 1105 EXPECT_EQ(Header, N->getHeader()); 1106 EXPECT_EQ(nullptr, N->getDwarfOperand(0)); 1107 ASSERT_TRUE(N->isUniqued()); 1108 1109 Metadata *Ops2[] = {nullptr}; 1110 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2)); 1111 1112 N->replaceDwarfOperandWith(0, Empty); 1113 EXPECT_EQ(15u, N->getTag()); 1114 EXPECT_EQ(Header, N->getHeader()); 1115 EXPECT_EQ(Empty, N->getDwarfOperand(0)); 1116 ASSERT_TRUE(N->isUniqued()); 1117 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1)); 1118 1119 TempGenericDINode Temp = N->clone(); 1120 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1121 } 1122 1123 TEST_F(GenericDINodeTest, getEmptyHeader) { 1124 // Canonicalize !"" to null. 1125 auto *N = GenericDINode::get(Context, 15, StringRef(), None); 1126 EXPECT_EQ(StringRef(), N->getHeader()); 1127 EXPECT_EQ(nullptr, N->getOperand(0)); 1128 } 1129 1130 typedef MetadataTest DISubrangeTest; 1131 1132 TEST_F(DISubrangeTest, get) { 1133 auto *N = DISubrange::get(Context, 5, 7); 1134 auto Count = N->getCount(); 1135 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag()); 1136 ASSERT_TRUE(Count); 1137 ASSERT_TRUE(Count.is<ConstantInt*>()); 1138 EXPECT_EQ(5, Count.get<ConstantInt*>()->getSExtValue()); 1139 EXPECT_EQ(7, N->getLowerBound()); 1140 EXPECT_EQ(N, DISubrange::get(Context, 5, 7)); 1141 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5)); 1142 1143 TempDISubrange Temp = N->clone(); 1144 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1145 } 1146 1147 TEST_F(DISubrangeTest, getEmptyArray) { 1148 auto *N = DISubrange::get(Context, -1, 0); 1149 auto Count = N->getCount(); 1150 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag()); 1151 ASSERT_TRUE(Count); 1152 ASSERT_TRUE(Count.is<ConstantInt*>()); 1153 EXPECT_EQ(-1, Count.get<ConstantInt*>()->getSExtValue()); 1154 EXPECT_EQ(0, N->getLowerBound()); 1155 EXPECT_EQ(N, DISubrange::get(Context, -1, 0)); 1156 } 1157 1158 TEST_F(DISubrangeTest, getVariableCount) { 1159 DILocalScope *Scope = getSubprogram(); 1160 DIFile *File = getFile(); 1161 DIType *Type = getDerivedType(); 1162 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7); 1163 auto *VlaExpr = DILocalVariable::get(Context, Scope, "vla_expr", File, 8, 1164 Type, 2, Flags, 8); 1165 1166 auto *N = DISubrange::get(Context, VlaExpr, 0); 1167 auto Count = N->getCount(); 1168 ASSERT_TRUE(Count); 1169 ASSERT_TRUE(Count.is<DIVariable*>()); 1170 EXPECT_EQ(VlaExpr, Count.get<DIVariable*>()); 1171 ASSERT_TRUE(isa<DIVariable>(N->getRawCountNode())); 1172 EXPECT_EQ(0, N->getLowerBound()); 1173 EXPECT_EQ("vla_expr", Count.get<DIVariable*>()->getName()); 1174 EXPECT_EQ(N, DISubrange::get(Context, VlaExpr, 0)); 1175 } 1176 1177 typedef MetadataTest DIEnumeratorTest; 1178 1179 TEST_F(DIEnumeratorTest, get) { 1180 auto *N = DIEnumerator::get(Context, 7, false, "name"); 1181 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag()); 1182 EXPECT_EQ(7, N->getValue()); 1183 EXPECT_FALSE(N->isUnsigned()); 1184 EXPECT_EQ("name", N->getName()); 1185 EXPECT_EQ(N, DIEnumerator::get(Context, 7, false, "name")); 1186 1187 EXPECT_NE(N, DIEnumerator::get(Context, 7, true, "name")); 1188 EXPECT_NE(N, DIEnumerator::get(Context, 8, false, "name")); 1189 EXPECT_NE(N, DIEnumerator::get(Context, 7, false, "nam")); 1190 1191 TempDIEnumerator Temp = N->clone(); 1192 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1193 } 1194 1195 typedef MetadataTest DIBasicTypeTest; 1196 1197 TEST_F(DIBasicTypeTest, get) { 1198 auto *N = 1199 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7, 1200 DINode::FlagZero); 1201 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag()); 1202 EXPECT_EQ("special", N->getName()); 1203 EXPECT_EQ(33u, N->getSizeInBits()); 1204 EXPECT_EQ(26u, N->getAlignInBits()); 1205 EXPECT_EQ(7u, N->getEncoding()); 1206 EXPECT_EQ(0u, N->getLine()); 1207 EXPECT_EQ(DINode::FlagZero, N->getFlags()); 1208 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 1209 26, 7, DINode::FlagZero)); 1210 1211 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, 1212 "special", 33, 26, 7, DINode::FlagZero)); 1213 EXPECT_NE(N, 1214 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7, 1215 DINode::FlagZero)); 1216 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32, 1217 26, 7, DINode::FlagZero)); 1218 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 1219 25, 7, DINode::FlagZero)); 1220 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 1221 26, 6, DINode::FlagZero)); 1222 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 1223 26, 7, DINode::FlagBigEndian)); 1224 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 1225 26, 7, DINode::FlagLittleEndian)); 1226 1227 TempDIBasicType Temp = N->clone(); 1228 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1229 } 1230 1231 TEST_F(DIBasicTypeTest, getWithLargeValues) { 1232 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 1233 UINT64_MAX, UINT32_MAX - 1, 7, DINode::FlagZero); 1234 EXPECT_EQ(UINT64_MAX, N->getSizeInBits()); 1235 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits()); 1236 } 1237 1238 TEST_F(DIBasicTypeTest, getUnspecified) { 1239 auto *N = 1240 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified"); 1241 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag()); 1242 EXPECT_EQ("unspecified", N->getName()); 1243 EXPECT_EQ(0u, N->getSizeInBits()); 1244 EXPECT_EQ(0u, N->getAlignInBits()); 1245 EXPECT_EQ(0u, N->getEncoding()); 1246 EXPECT_EQ(0u, N->getLine()); 1247 EXPECT_EQ(DINode::FlagZero, N->getFlags()); 1248 } 1249 1250 typedef MetadataTest DITypeTest; 1251 1252 TEST_F(DITypeTest, clone) { 1253 // Check that DIType has a specialized clone that returns TempDIType. 1254 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32, 1255 dwarf::DW_ATE_signed, DINode::FlagZero); 1256 1257 TempDIType Temp = N->clone(); 1258 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1259 } 1260 1261 TEST_F(DITypeTest, cloneWithFlags) { 1262 // void (void) 1263 Metadata *TypesOps[] = {nullptr}; 1264 Metadata *Types = MDTuple::get(Context, TypesOps); 1265 1266 DIType *D = 1267 DISubroutineType::getDistinct(Context, DINode::FlagZero, 0, Types); 1268 EXPECT_EQ(DINode::FlagZero, D->getFlags()); 1269 TempDIType D2 = D->cloneWithFlags(DINode::FlagRValueReference); 1270 EXPECT_EQ(DINode::FlagRValueReference, D2->getFlags()); 1271 EXPECT_EQ(DINode::FlagZero, D->getFlags()); 1272 1273 TempDIType T = 1274 DISubroutineType::getTemporary(Context, DINode::FlagZero, 0, Types); 1275 EXPECT_EQ(DINode::FlagZero, T->getFlags()); 1276 TempDIType T2 = T->cloneWithFlags(DINode::FlagRValueReference); 1277 EXPECT_EQ(DINode::FlagRValueReference, T2->getFlags()); 1278 EXPECT_EQ(DINode::FlagZero, T->getFlags()); 1279 } 1280 1281 typedef MetadataTest DIDerivedTypeTest; 1282 1283 TEST_F(DIDerivedTypeTest, get) { 1284 DIFile *File = getFile(); 1285 DIScope *Scope = getSubprogram(); 1286 DIType *BaseType = getBasicType("basic"); 1287 MDTuple *ExtraData = getTuple(); 1288 unsigned DWARFAddressSpace = 8; 1289 DINode::DIFlags Flags5 = static_cast<DINode::DIFlags>(5); 1290 DINode::DIFlags Flags4 = static_cast<DINode::DIFlags>(4); 1291 1292 auto *N = 1293 DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", File, 1294 1, Scope, BaseType, 2, 3, 4, DWARFAddressSpace, Flags5, 1295 ExtraData); 1296 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag()); 1297 EXPECT_EQ("something", N->getName()); 1298 EXPECT_EQ(File, N->getFile()); 1299 EXPECT_EQ(1u, N->getLine()); 1300 EXPECT_EQ(Scope, N->getScope()); 1301 EXPECT_EQ(BaseType, N->getBaseType()); 1302 EXPECT_EQ(2u, N->getSizeInBits()); 1303 EXPECT_EQ(3u, N->getAlignInBits()); 1304 EXPECT_EQ(4u, N->getOffsetInBits()); 1305 EXPECT_EQ(DWARFAddressSpace, N->getDWARFAddressSpace().getValue()); 1306 EXPECT_EQ(5u, N->getFlags()); 1307 EXPECT_EQ(ExtraData, N->getExtraData()); 1308 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1309 "something", File, 1, Scope, BaseType, 2, 3, 1310 4, DWARFAddressSpace, Flags5, ExtraData)); 1311 1312 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type, 1313 "something", File, 1, Scope, BaseType, 2, 3, 1314 4, DWARFAddressSpace, Flags5, ExtraData)); 1315 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else", 1316 File, 1, Scope, BaseType, 2, 3, 1317 4, DWARFAddressSpace, Flags5, ExtraData)); 1318 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1319 "something", getFile(), 1, Scope, BaseType, 2, 1320 3, 4, DWARFAddressSpace, Flags5, ExtraData)); 1321 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1322 "something", File, 2, Scope, BaseType, 2, 3, 1323 4, DWARFAddressSpace, Flags5, ExtraData)); 1324 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1325 "something", File, 1, getSubprogram(), 1326 BaseType, 2, 3, 4, DWARFAddressSpace, Flags5, 1327 ExtraData)); 1328 EXPECT_NE(N, DIDerivedType::get( 1329 Context, dwarf::DW_TAG_pointer_type, "something", File, 1, 1330 Scope, getBasicType("basic2"), 2, 3, 4, DWARFAddressSpace, 1331 Flags5, ExtraData)); 1332 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1333 "something", File, 1, Scope, BaseType, 3, 3, 1334 4, DWARFAddressSpace, Flags5, ExtraData)); 1335 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1336 "something", File, 1, Scope, BaseType, 2, 2, 1337 4, DWARFAddressSpace, Flags5, ExtraData)); 1338 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1339 "something", File, 1, Scope, BaseType, 2, 3, 1340 5, DWARFAddressSpace, Flags5, ExtraData)); 1341 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1342 "something", File, 1, Scope, BaseType, 2, 3, 1343 4, DWARFAddressSpace + 1, Flags5, ExtraData)); 1344 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1345 "something", File, 1, Scope, BaseType, 2, 3, 1346 4, DWARFAddressSpace, Flags4, ExtraData)); 1347 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, 1348 "something", File, 1, Scope, BaseType, 2, 3, 1349 4, DWARFAddressSpace, Flags5, getTuple())); 1350 1351 TempDIDerivedType Temp = N->clone(); 1352 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1353 } 1354 1355 TEST_F(DIDerivedTypeTest, getWithLargeValues) { 1356 DIFile *File = getFile(); 1357 DIScope *Scope = getSubprogram(); 1358 DIType *BaseType = getBasicType("basic"); 1359 MDTuple *ExtraData = getTuple(); 1360 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5); 1361 1362 auto *N = DIDerivedType::get( 1363 Context, dwarf::DW_TAG_pointer_type, "something", File, 1, Scope, 1364 BaseType, UINT64_MAX, UINT32_MAX - 1, UINT64_MAX - 2, UINT32_MAX - 3, 1365 Flags, ExtraData); 1366 EXPECT_EQ(UINT64_MAX, N->getSizeInBits()); 1367 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits()); 1368 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits()); 1369 EXPECT_EQ(UINT32_MAX - 3, N->getDWARFAddressSpace().getValue()); 1370 } 1371 1372 typedef MetadataTest DICompositeTypeTest; 1373 1374 TEST_F(DICompositeTypeTest, get) { 1375 unsigned Tag = dwarf::DW_TAG_structure_type; 1376 StringRef Name = "some name"; 1377 DIFile *File = getFile(); 1378 unsigned Line = 1; 1379 DIScope *Scope = getSubprogram(); 1380 DIType *BaseType = getCompositeType(); 1381 uint64_t SizeInBits = 2; 1382 uint32_t AlignInBits = 3; 1383 uint64_t OffsetInBits = 4; 1384 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5); 1385 MDTuple *Elements = getTuple(); 1386 unsigned RuntimeLang = 6; 1387 DIType *VTableHolder = getCompositeType(); 1388 MDTuple *TemplateParams = getTuple(); 1389 StringRef Identifier = "some id"; 1390 1391 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope, 1392 BaseType, SizeInBits, AlignInBits, 1393 OffsetInBits, Flags, Elements, RuntimeLang, 1394 VTableHolder, TemplateParams, Identifier); 1395 EXPECT_EQ(Tag, N->getTag()); 1396 EXPECT_EQ(Name, N->getName()); 1397 EXPECT_EQ(File, N->getFile()); 1398 EXPECT_EQ(Line, N->getLine()); 1399 EXPECT_EQ(Scope, N->getScope()); 1400 EXPECT_EQ(BaseType, N->getBaseType()); 1401 EXPECT_EQ(SizeInBits, N->getSizeInBits()); 1402 EXPECT_EQ(AlignInBits, N->getAlignInBits()); 1403 EXPECT_EQ(OffsetInBits, N->getOffsetInBits()); 1404 EXPECT_EQ(Flags, N->getFlags()); 1405 EXPECT_EQ(Elements, N->getElements().get()); 1406 EXPECT_EQ(RuntimeLang, N->getRuntimeLang()); 1407 EXPECT_EQ(VTableHolder, N->getVTableHolder()); 1408 EXPECT_EQ(TemplateParams, N->getTemplateParams().get()); 1409 EXPECT_EQ(Identifier, N->getIdentifier()); 1410 1411 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope, 1412 BaseType, SizeInBits, AlignInBits, 1413 OffsetInBits, Flags, Elements, RuntimeLang, 1414 VTableHolder, TemplateParams, Identifier)); 1415 1416 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope, 1417 BaseType, SizeInBits, AlignInBits, 1418 OffsetInBits, Flags, Elements, RuntimeLang, 1419 VTableHolder, TemplateParams, Identifier)); 1420 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope, 1421 BaseType, SizeInBits, AlignInBits, 1422 OffsetInBits, Flags, Elements, RuntimeLang, 1423 VTableHolder, TemplateParams, Identifier)); 1424 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope, 1425 BaseType, SizeInBits, AlignInBits, 1426 OffsetInBits, Flags, Elements, RuntimeLang, 1427 VTableHolder, TemplateParams, Identifier)); 1428 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope, 1429 BaseType, SizeInBits, AlignInBits, 1430 OffsetInBits, Flags, Elements, RuntimeLang, 1431 VTableHolder, TemplateParams, Identifier)); 1432 EXPECT_NE(N, DICompositeType::get( 1433 Context, Tag, Name, File, Line, getSubprogram(), BaseType, 1434 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, 1435 RuntimeLang, VTableHolder, TemplateParams, Identifier)); 1436 EXPECT_NE(N, DICompositeType::get( 1437 Context, Tag, Name, File, Line, Scope, getBasicType("other"), 1438 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, 1439 RuntimeLang, VTableHolder, TemplateParams, Identifier)); 1440 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope, 1441 BaseType, SizeInBits + 1, AlignInBits, 1442 OffsetInBits, Flags, Elements, RuntimeLang, 1443 VTableHolder, TemplateParams, Identifier)); 1444 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope, 1445 BaseType, SizeInBits, AlignInBits + 1, 1446 OffsetInBits, Flags, Elements, RuntimeLang, 1447 VTableHolder, TemplateParams, Identifier)); 1448 EXPECT_NE(N, DICompositeType::get( 1449 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1450 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang, 1451 VTableHolder, TemplateParams, Identifier)); 1452 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1); 1453 EXPECT_NE(N, DICompositeType::get( 1454 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1455 AlignInBits, OffsetInBits, FlagsPOne, Elements, RuntimeLang, 1456 VTableHolder, TemplateParams, Identifier)); 1457 EXPECT_NE(N, DICompositeType::get( 1458 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1459 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang, 1460 VTableHolder, TemplateParams, Identifier)); 1461 EXPECT_NE(N, DICompositeType::get( 1462 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1463 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1, 1464 VTableHolder, TemplateParams, Identifier)); 1465 EXPECT_NE(N, DICompositeType::get( 1466 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 1467 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, 1468 getCompositeType(), TemplateParams, Identifier)); 1469 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope, 1470 BaseType, SizeInBits, AlignInBits, 1471 OffsetInBits, Flags, Elements, RuntimeLang, 1472 VTableHolder, getTuple(), Identifier)); 1473 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope, 1474 BaseType, SizeInBits, AlignInBits, 1475 OffsetInBits, Flags, Elements, RuntimeLang, 1476 VTableHolder, TemplateParams, "other")); 1477 1478 // Be sure that missing identifiers get null pointers. 1479 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope, 1480 BaseType, SizeInBits, AlignInBits, 1481 OffsetInBits, Flags, Elements, RuntimeLang, 1482 VTableHolder, TemplateParams, "") 1483 ->getRawIdentifier()); 1484 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope, 1485 BaseType, SizeInBits, AlignInBits, 1486 OffsetInBits, Flags, Elements, RuntimeLang, 1487 VTableHolder, TemplateParams) 1488 ->getRawIdentifier()); 1489 1490 TempDICompositeType Temp = N->clone(); 1491 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1492 } 1493 1494 TEST_F(DICompositeTypeTest, getWithLargeValues) { 1495 unsigned Tag = dwarf::DW_TAG_structure_type; 1496 StringRef Name = "some name"; 1497 DIFile *File = getFile(); 1498 unsigned Line = 1; 1499 DIScope *Scope = getSubprogram(); 1500 DIType *BaseType = getCompositeType(); 1501 uint64_t SizeInBits = UINT64_MAX; 1502 uint32_t AlignInBits = UINT32_MAX - 1; 1503 uint64_t OffsetInBits = UINT64_MAX - 2; 1504 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5); 1505 MDTuple *Elements = getTuple(); 1506 unsigned RuntimeLang = 6; 1507 DIType *VTableHolder = getCompositeType(); 1508 MDTuple *TemplateParams = getTuple(); 1509 StringRef Identifier = "some id"; 1510 1511 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope, 1512 BaseType, SizeInBits, AlignInBits, 1513 OffsetInBits, Flags, Elements, RuntimeLang, 1514 VTableHolder, TemplateParams, Identifier); 1515 EXPECT_EQ(SizeInBits, N->getSizeInBits()); 1516 EXPECT_EQ(AlignInBits, N->getAlignInBits()); 1517 EXPECT_EQ(OffsetInBits, N->getOffsetInBits()); 1518 } 1519 1520 TEST_F(DICompositeTypeTest, replaceOperands) { 1521 unsigned Tag = dwarf::DW_TAG_structure_type; 1522 StringRef Name = "some name"; 1523 DIFile *File = getFile(); 1524 unsigned Line = 1; 1525 DIScope *Scope = getSubprogram(); 1526 DIType *BaseType = getCompositeType(); 1527 uint64_t SizeInBits = 2; 1528 uint32_t AlignInBits = 3; 1529 uint64_t OffsetInBits = 4; 1530 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5); 1531 unsigned RuntimeLang = 6; 1532 StringRef Identifier = "some id"; 1533 1534 auto *N = DICompositeType::get( 1535 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 1536 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier); 1537 1538 auto *Elements = MDTuple::getDistinct(Context, None); 1539 EXPECT_EQ(nullptr, N->getElements().get()); 1540 N->replaceElements(Elements); 1541 EXPECT_EQ(Elements, N->getElements().get()); 1542 N->replaceElements(nullptr); 1543 EXPECT_EQ(nullptr, N->getElements().get()); 1544 1545 DIType *VTableHolder = getCompositeType(); 1546 EXPECT_EQ(nullptr, N->getVTableHolder()); 1547 N->replaceVTableHolder(VTableHolder); 1548 EXPECT_EQ(VTableHolder, N->getVTableHolder()); 1549 // As an extension, the containing type can be anything. This is 1550 // used by Rust to associate vtables with their concrete type. 1551 DIType *BasicType = getBasicType("basic"); 1552 N->replaceVTableHolder(BasicType); 1553 EXPECT_EQ(BasicType, N->getVTableHolder()); 1554 N->replaceVTableHolder(nullptr); 1555 EXPECT_EQ(nullptr, N->getVTableHolder()); 1556 1557 auto *TemplateParams = MDTuple::getDistinct(Context, None); 1558 EXPECT_EQ(nullptr, N->getTemplateParams().get()); 1559 N->replaceTemplateParams(TemplateParams); 1560 EXPECT_EQ(TemplateParams, N->getTemplateParams().get()); 1561 N->replaceTemplateParams(nullptr); 1562 EXPECT_EQ(nullptr, N->getTemplateParams().get()); 1563 } 1564 1565 TEST_F(DICompositeTypeTest, variant_part) { 1566 unsigned Tag = dwarf::DW_TAG_variant_part; 1567 StringRef Name = "some name"; 1568 DIFile *File = getFile(); 1569 unsigned Line = 1; 1570 DIScope *Scope = getSubprogram(); 1571 DIType *BaseType = getCompositeType(); 1572 uint64_t SizeInBits = 2; 1573 uint32_t AlignInBits = 3; 1574 uint64_t OffsetInBits = 4; 1575 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5); 1576 unsigned RuntimeLang = 6; 1577 StringRef Identifier = "some id"; 1578 DIDerivedType *Discriminator = cast<DIDerivedType>(getDerivedType()); 1579 DIDerivedType *Discriminator2 = cast<DIDerivedType>(getDerivedType()); 1580 1581 EXPECT_NE(Discriminator, Discriminator2); 1582 1583 auto *N = DICompositeType::get( 1584 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 1585 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier, 1586 Discriminator); 1587 1588 // Test the hashing. 1589 auto *Same = DICompositeType::get( 1590 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 1591 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier, 1592 Discriminator); 1593 auto *Other = DICompositeType::get( 1594 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 1595 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier, 1596 Discriminator2); 1597 auto *NoDisc = DICompositeType::get( 1598 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 1599 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier, 1600 nullptr); 1601 1602 EXPECT_EQ(N, Same); 1603 EXPECT_NE(Same, Other); 1604 EXPECT_NE(Same, NoDisc); 1605 EXPECT_NE(Other, NoDisc); 1606 1607 EXPECT_EQ(N->getDiscriminator(), Discriminator); 1608 } 1609 1610 typedef MetadataTest DISubroutineTypeTest; 1611 1612 TEST_F(DISubroutineTypeTest, get) { 1613 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(1); 1614 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1); 1615 MDTuple *TypeArray = getTuple(); 1616 1617 auto *N = DISubroutineType::get(Context, Flags, 0, TypeArray); 1618 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag()); 1619 EXPECT_EQ(Flags, N->getFlags()); 1620 EXPECT_EQ(TypeArray, N->getTypeArray().get()); 1621 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, 0, TypeArray)); 1622 1623 EXPECT_NE(N, DISubroutineType::get(Context, FlagsPOne, 0, TypeArray)); 1624 EXPECT_NE(N, DISubroutineType::get(Context, Flags, 0, getTuple())); 1625 1626 // Test the hashing of calling conventions. 1627 auto *Fast = DISubroutineType::get( 1628 Context, Flags, dwarf::DW_CC_BORLAND_msfastcall, TypeArray); 1629 auto *Std = DISubroutineType::get(Context, Flags, 1630 dwarf::DW_CC_BORLAND_stdcall, TypeArray); 1631 EXPECT_EQ(Fast, 1632 DISubroutineType::get(Context, Flags, 1633 dwarf::DW_CC_BORLAND_msfastcall, TypeArray)); 1634 EXPECT_EQ(Std, DISubroutineType::get( 1635 Context, Flags, dwarf::DW_CC_BORLAND_stdcall, TypeArray)); 1636 1637 EXPECT_NE(N, Fast); 1638 EXPECT_NE(N, Std); 1639 EXPECT_NE(Fast, Std); 1640 1641 TempDISubroutineType Temp = N->clone(); 1642 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1643 1644 // Test always-empty operands. 1645 EXPECT_EQ(nullptr, N->getScope()); 1646 EXPECT_EQ(nullptr, N->getFile()); 1647 EXPECT_EQ("", N->getName()); 1648 } 1649 1650 typedef MetadataTest DIFileTest; 1651 1652 TEST_F(DIFileTest, get) { 1653 StringRef Filename = "file"; 1654 StringRef Directory = "dir"; 1655 DIFile::ChecksumKind CSKind = DIFile::ChecksumKind::CSK_MD5; 1656 StringRef ChecksumString = "000102030405060708090a0b0c0d0e0f"; 1657 DIFile::ChecksumInfo<StringRef> Checksum(CSKind, ChecksumString); 1658 StringRef Source = "source"; 1659 auto *N = DIFile::get(Context, Filename, Directory, Checksum, Source); 1660 1661 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag()); 1662 EXPECT_EQ(Filename, N->getFilename()); 1663 EXPECT_EQ(Directory, N->getDirectory()); 1664 EXPECT_EQ(Checksum, N->getChecksum()); 1665 EXPECT_EQ(Source, N->getSource()); 1666 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory, Checksum, Source)); 1667 1668 EXPECT_NE(N, DIFile::get(Context, "other", Directory, Checksum, Source)); 1669 EXPECT_NE(N, DIFile::get(Context, Filename, "other", Checksum, Source)); 1670 DIFile::ChecksumInfo<StringRef> OtherChecksum(DIFile::ChecksumKind::CSK_SHA1, ChecksumString); 1671 EXPECT_NE( 1672 N, DIFile::get(Context, Filename, Directory, OtherChecksum)); 1673 StringRef OtherSource = "other"; 1674 EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum, OtherSource)); 1675 EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum)); 1676 EXPECT_NE(N, DIFile::get(Context, Filename, Directory)); 1677 1678 TempDIFile Temp = N->clone(); 1679 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1680 } 1681 1682 TEST_F(DIFileTest, ScopeGetFile) { 1683 // Ensure that DIScope::getFile() returns itself. 1684 DIScope *N = DIFile::get(Context, "file", "dir"); 1685 EXPECT_EQ(N, N->getFile()); 1686 } 1687 1688 typedef MetadataTest DICompileUnitTest; 1689 1690 TEST_F(DICompileUnitTest, get) { 1691 unsigned SourceLanguage = 1; 1692 DIFile *File = getFile(); 1693 StringRef Producer = "some producer"; 1694 bool IsOptimized = false; 1695 StringRef Flags = "flag after flag"; 1696 unsigned RuntimeVersion = 2; 1697 StringRef SplitDebugFilename = "another/file"; 1698 auto EmissionKind = DICompileUnit::FullDebug; 1699 MDTuple *EnumTypes = getTuple(); 1700 MDTuple *RetainedTypes = getTuple(); 1701 MDTuple *GlobalVariables = getTuple(); 1702 MDTuple *ImportedEntities = getTuple(); 1703 uint64_t DWOId = 0x10000000c0ffee; 1704 MDTuple *Macros = getTuple(); 1705 auto *N = DICompileUnit::getDistinct( 1706 Context, SourceLanguage, File, Producer, IsOptimized, Flags, 1707 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, 1708 RetainedTypes, GlobalVariables, ImportedEntities, Macros, DWOId, true, 1709 false, DICompileUnit::DebugNameTableKind::Default, false); 1710 1711 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag()); 1712 EXPECT_EQ(SourceLanguage, N->getSourceLanguage()); 1713 EXPECT_EQ(File, N->getFile()); 1714 EXPECT_EQ(Producer, N->getProducer()); 1715 EXPECT_EQ(IsOptimized, N->isOptimized()); 1716 EXPECT_EQ(Flags, N->getFlags()); 1717 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion()); 1718 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename()); 1719 EXPECT_EQ(EmissionKind, N->getEmissionKind()); 1720 EXPECT_EQ(EnumTypes, N->getEnumTypes().get()); 1721 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get()); 1722 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get()); 1723 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get()); 1724 EXPECT_EQ(Macros, N->getMacros().get()); 1725 EXPECT_EQ(DWOId, N->getDWOId()); 1726 1727 TempDICompileUnit Temp = N->clone(); 1728 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag()); 1729 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage()); 1730 EXPECT_EQ(File, Temp->getFile()); 1731 EXPECT_EQ(Producer, Temp->getProducer()); 1732 EXPECT_EQ(IsOptimized, Temp->isOptimized()); 1733 EXPECT_EQ(Flags, Temp->getFlags()); 1734 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion()); 1735 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename()); 1736 EXPECT_EQ(EmissionKind, Temp->getEmissionKind()); 1737 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get()); 1738 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get()); 1739 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get()); 1740 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get()); 1741 EXPECT_EQ(Macros, Temp->getMacros().get()); 1742 EXPECT_EQ(DWOId, Temp->getDWOId()); 1743 1744 auto *TempAddress = Temp.get(); 1745 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp)); 1746 EXPECT_TRUE(Clone->isDistinct()); 1747 EXPECT_EQ(TempAddress, Clone); 1748 } 1749 1750 TEST_F(DICompileUnitTest, replaceArrays) { 1751 unsigned SourceLanguage = 1; 1752 DIFile *File = getFile(); 1753 StringRef Producer = "some producer"; 1754 bool IsOptimized = false; 1755 StringRef Flags = "flag after flag"; 1756 unsigned RuntimeVersion = 2; 1757 StringRef SplitDebugFilename = "another/file"; 1758 auto EmissionKind = DICompileUnit::FullDebug; 1759 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None); 1760 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None); 1761 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None); 1762 uint64_t DWOId = 0xc0ffee; 1763 auto *N = DICompileUnit::getDistinct( 1764 Context, SourceLanguage, File, Producer, IsOptimized, Flags, 1765 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, 1766 RetainedTypes, nullptr, ImportedEntities, nullptr, DWOId, true, false, 1767 DICompileUnit::DebugNameTableKind::Default, false); 1768 1769 auto *GlobalVariables = MDTuple::getDistinct(Context, None); 1770 EXPECT_EQ(nullptr, N->getGlobalVariables().get()); 1771 N->replaceGlobalVariables(GlobalVariables); 1772 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get()); 1773 N->replaceGlobalVariables(nullptr); 1774 EXPECT_EQ(nullptr, N->getGlobalVariables().get()); 1775 1776 auto *Macros = MDTuple::getDistinct(Context, None); 1777 EXPECT_EQ(nullptr, N->getMacros().get()); 1778 N->replaceMacros(Macros); 1779 EXPECT_EQ(Macros, N->getMacros().get()); 1780 N->replaceMacros(nullptr); 1781 EXPECT_EQ(nullptr, N->getMacros().get()); 1782 } 1783 1784 typedef MetadataTest DISubprogramTest; 1785 1786 TEST_F(DISubprogramTest, get) { 1787 DIScope *Scope = getCompositeType(); 1788 StringRef Name = "name"; 1789 StringRef LinkageName = "linkage"; 1790 DIFile *File = getFile(); 1791 unsigned Line = 2; 1792 DISubroutineType *Type = getSubroutineType(); 1793 bool IsLocalToUnit = false; 1794 bool IsDefinition = true; 1795 unsigned ScopeLine = 3; 1796 DIType *ContainingType = getCompositeType(); 1797 unsigned Virtuality = 2; 1798 unsigned VirtualIndex = 5; 1799 int ThisAdjustment = -3; 1800 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(6); 1801 bool IsOptimized = false; 1802 MDTuple *TemplateParams = getTuple(); 1803 DISubprogram *Declaration = getSubprogram(); 1804 MDTuple *RetainedNodes = getTuple(); 1805 MDTuple *ThrownTypes = getTuple(); 1806 DICompileUnit *Unit = getUnit(); 1807 DISubprogram::DISPFlags SPFlags = 1808 static_cast<DISubprogram::DISPFlags>(Virtuality); 1809 assert(!IsLocalToUnit && IsDefinition && !IsOptimized && 1810 "bools and SPFlags have to match"); 1811 SPFlags |= DISubprogram::SPFlagDefinition; 1812 1813 auto *N = DISubprogram::get( 1814 Context, Scope, Name, LinkageName, File, Line, Type, ScopeLine, 1815 ContainingType, VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, 1816 TemplateParams, Declaration, RetainedNodes, ThrownTypes); 1817 1818 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag()); 1819 EXPECT_EQ(Scope, N->getScope()); 1820 EXPECT_EQ(Name, N->getName()); 1821 EXPECT_EQ(LinkageName, N->getLinkageName()); 1822 EXPECT_EQ(File, N->getFile()); 1823 EXPECT_EQ(Line, N->getLine()); 1824 EXPECT_EQ(Type, N->getType()); 1825 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit()); 1826 EXPECT_EQ(IsDefinition, N->isDefinition()); 1827 EXPECT_EQ(ScopeLine, N->getScopeLine()); 1828 EXPECT_EQ(ContainingType, N->getContainingType()); 1829 EXPECT_EQ(Virtuality, N->getVirtuality()); 1830 EXPECT_EQ(VirtualIndex, N->getVirtualIndex()); 1831 EXPECT_EQ(ThisAdjustment, N->getThisAdjustment()); 1832 EXPECT_EQ(Flags, N->getFlags()); 1833 EXPECT_EQ(IsOptimized, N->isOptimized()); 1834 EXPECT_EQ(Unit, N->getUnit()); 1835 EXPECT_EQ(TemplateParams, N->getTemplateParams().get()); 1836 EXPECT_EQ(Declaration, N->getDeclaration()); 1837 EXPECT_EQ(RetainedNodes, N->getRetainedNodes().get()); 1838 EXPECT_EQ(ThrownTypes, N->getThrownTypes().get()); 1839 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1840 Type, ScopeLine, ContainingType, VirtualIndex, 1841 ThisAdjustment, Flags, SPFlags, Unit, 1842 TemplateParams, Declaration, RetainedNodes, 1843 ThrownTypes)); 1844 1845 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName, 1846 File, Line, Type, ScopeLine, ContainingType, 1847 VirtualIndex, ThisAdjustment, Flags, SPFlags, 1848 Unit, TemplateParams, Declaration, 1849 RetainedNodes, ThrownTypes)); 1850 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File, 1851 Line, Type, ScopeLine, ContainingType, 1852 VirtualIndex, ThisAdjustment, Flags, SPFlags, 1853 Unit, TemplateParams, Declaration, 1854 RetainedNodes, ThrownTypes)); 1855 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line, 1856 Type, ScopeLine, ContainingType, VirtualIndex, 1857 ThisAdjustment, Flags, SPFlags, Unit, 1858 TemplateParams, Declaration, RetainedNodes, 1859 ThrownTypes)); 1860 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(), 1861 Line, Type, ScopeLine, ContainingType, 1862 VirtualIndex, ThisAdjustment, Flags, SPFlags, 1863 Unit, TemplateParams, Declaration, 1864 RetainedNodes, ThrownTypes)); 1865 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, 1866 Line + 1, Type, ScopeLine, ContainingType, 1867 VirtualIndex, ThisAdjustment, Flags, SPFlags, 1868 Unit, TemplateParams, Declaration, 1869 RetainedNodes, ThrownTypes)); 1870 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1871 getSubroutineType(), ScopeLine, ContainingType, 1872 VirtualIndex, ThisAdjustment, Flags, SPFlags, 1873 Unit, TemplateParams, Declaration, 1874 RetainedNodes, ThrownTypes)); 1875 EXPECT_NE(N, DISubprogram::get( 1876 Context, Scope, Name, LinkageName, File, Line, Type, 1877 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment, 1878 Flags, SPFlags ^ DISubprogram::SPFlagLocalToUnit, Unit, 1879 TemplateParams, Declaration, RetainedNodes, ThrownTypes)); 1880 EXPECT_NE(N, DISubprogram::get( 1881 Context, Scope, Name, LinkageName, File, Line, Type, 1882 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment, 1883 Flags, SPFlags ^ DISubprogram::SPFlagDefinition, Unit, 1884 TemplateParams, Declaration, RetainedNodes, ThrownTypes)); 1885 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1886 Type, ScopeLine + 1, ContainingType, 1887 VirtualIndex, ThisAdjustment, Flags, SPFlags, 1888 Unit, TemplateParams, Declaration, 1889 RetainedNodes, ThrownTypes)); 1890 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1891 Type, ScopeLine, getCompositeType(), 1892 VirtualIndex, ThisAdjustment, Flags, SPFlags, 1893 Unit, TemplateParams, Declaration, 1894 RetainedNodes, ThrownTypes)); 1895 EXPECT_NE(N, DISubprogram::get( 1896 Context, Scope, Name, LinkageName, File, Line, Type, 1897 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment, 1898 Flags, SPFlags ^ DISubprogram::SPFlagVirtual, Unit, 1899 TemplateParams, Declaration, RetainedNodes, ThrownTypes)); 1900 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1901 Type, ScopeLine, ContainingType, 1902 VirtualIndex + 1, ThisAdjustment, Flags, 1903 SPFlags, Unit, TemplateParams, Declaration, 1904 RetainedNodes, ThrownTypes)); 1905 EXPECT_NE(N, DISubprogram::get( 1906 Context, Scope, Name, LinkageName, File, Line, Type, 1907 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment, 1908 Flags, SPFlags ^ DISubprogram::SPFlagOptimized, Unit, 1909 TemplateParams, Declaration, RetainedNodes, ThrownTypes)); 1910 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1911 Type, ScopeLine, ContainingType, VirtualIndex, 1912 ThisAdjustment, Flags, SPFlags, nullptr, 1913 TemplateParams, Declaration, RetainedNodes, 1914 ThrownTypes)); 1915 EXPECT_NE(N, 1916 DISubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1917 Type, ScopeLine, ContainingType, VirtualIndex, 1918 ThisAdjustment, Flags, SPFlags, Unit, getTuple(), 1919 Declaration, RetainedNodes, ThrownTypes)); 1920 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1921 Type, ScopeLine, ContainingType, VirtualIndex, 1922 ThisAdjustment, Flags, SPFlags, Unit, 1923 TemplateParams, getSubprogram(), RetainedNodes, 1924 ThrownTypes)); 1925 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1926 Type, ScopeLine, ContainingType, VirtualIndex, 1927 ThisAdjustment, Flags, SPFlags, Unit, 1928 TemplateParams, Declaration, getTuple())); 1929 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line, 1930 Type, ScopeLine, ContainingType, VirtualIndex, 1931 ThisAdjustment, Flags, SPFlags, Unit, 1932 TemplateParams, Declaration, RetainedNodes, 1933 getTuple())); 1934 1935 TempDISubprogram Temp = N->clone(); 1936 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1937 } 1938 1939 typedef MetadataTest DILexicalBlockTest; 1940 1941 TEST_F(DILexicalBlockTest, get) { 1942 DILocalScope *Scope = getSubprogram(); 1943 DIFile *File = getFile(); 1944 unsigned Line = 5; 1945 unsigned Column = 8; 1946 1947 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column); 1948 1949 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag()); 1950 EXPECT_EQ(Scope, N->getScope()); 1951 EXPECT_EQ(File, N->getFile()); 1952 EXPECT_EQ(Line, N->getLine()); 1953 EXPECT_EQ(Column, N->getColumn()); 1954 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column)); 1955 1956 EXPECT_NE(N, 1957 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column)); 1958 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column)); 1959 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column)); 1960 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1)); 1961 1962 TempDILexicalBlock Temp = N->clone(); 1963 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 1964 } 1965 1966 TEST_F(DILexicalBlockTest, Overflow) { 1967 DISubprogram *SP = getSubprogram(); 1968 DIFile *F = getFile(); 1969 { 1970 auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7); 1971 EXPECT_EQ(2u, LB->getLine()); 1972 EXPECT_EQ(7u, LB->getColumn()); 1973 } 1974 unsigned U16 = 1u << 16; 1975 { 1976 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1); 1977 EXPECT_EQ(UINT32_MAX, LB->getLine()); 1978 EXPECT_EQ(U16 - 1, LB->getColumn()); 1979 } 1980 { 1981 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16); 1982 EXPECT_EQ(UINT32_MAX, LB->getLine()); 1983 EXPECT_EQ(0u, LB->getColumn()); 1984 } 1985 { 1986 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1); 1987 EXPECT_EQ(UINT32_MAX, LB->getLine()); 1988 EXPECT_EQ(0u, LB->getColumn()); 1989 } 1990 } 1991 1992 typedef MetadataTest DILexicalBlockFileTest; 1993 1994 TEST_F(DILexicalBlockFileTest, get) { 1995 DILocalScope *Scope = getSubprogram(); 1996 DIFile *File = getFile(); 1997 unsigned Discriminator = 5; 1998 1999 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator); 2000 2001 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag()); 2002 EXPECT_EQ(Scope, N->getScope()); 2003 EXPECT_EQ(File, N->getFile()); 2004 EXPECT_EQ(Discriminator, N->getDiscriminator()); 2005 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator)); 2006 2007 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File, 2008 Discriminator)); 2009 EXPECT_NE(N, 2010 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator)); 2011 EXPECT_NE(N, 2012 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1)); 2013 2014 TempDILexicalBlockFile Temp = N->clone(); 2015 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2016 } 2017 2018 typedef MetadataTest DINamespaceTest; 2019 2020 TEST_F(DINamespaceTest, get) { 2021 DIScope *Scope = getFile(); 2022 StringRef Name = "namespace"; 2023 bool ExportSymbols = true; 2024 2025 auto *N = DINamespace::get(Context, Scope, Name, ExportSymbols); 2026 2027 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag()); 2028 EXPECT_EQ(Scope, N->getScope()); 2029 EXPECT_EQ(Name, N->getName()); 2030 EXPECT_EQ(N, DINamespace::get(Context, Scope, Name, ExportSymbols)); 2031 EXPECT_NE(N, DINamespace::get(Context, getFile(), Name, ExportSymbols)); 2032 EXPECT_NE(N, DINamespace::get(Context, Scope, "other", ExportSymbols)); 2033 EXPECT_NE(N, DINamespace::get(Context, Scope, Name, !ExportSymbols)); 2034 2035 TempDINamespace Temp = N->clone(); 2036 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2037 } 2038 2039 typedef MetadataTest DIModuleTest; 2040 2041 TEST_F(DIModuleTest, get) { 2042 DIScope *Scope = getFile(); 2043 StringRef Name = "module"; 2044 StringRef ConfigMacro = "-DNDEBUG"; 2045 StringRef Includes = "-I."; 2046 StringRef Sysroot = "/"; 2047 2048 auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot); 2049 2050 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag()); 2051 EXPECT_EQ(Scope, N->getScope()); 2052 EXPECT_EQ(Name, N->getName()); 2053 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros()); 2054 EXPECT_EQ(Includes, N->getIncludePath()); 2055 EXPECT_EQ(Sysroot, N->getISysRoot()); 2056 EXPECT_EQ(N, DIModule::get(Context, Scope, Name, 2057 ConfigMacro, Includes, Sysroot)); 2058 EXPECT_NE(N, DIModule::get(Context, getFile(), Name, 2059 ConfigMacro, Includes, Sysroot)); 2060 EXPECT_NE(N, DIModule::get(Context, Scope, "other", 2061 ConfigMacro, Includes, Sysroot)); 2062 EXPECT_NE(N, DIModule::get(Context, Scope, Name, 2063 "other", Includes, Sysroot)); 2064 EXPECT_NE(N, DIModule::get(Context, Scope, Name, 2065 ConfigMacro, "other", Sysroot)); 2066 EXPECT_NE(N, DIModule::get(Context, Scope, Name, 2067 ConfigMacro, Includes, "other")); 2068 2069 TempDIModule Temp = N->clone(); 2070 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2071 } 2072 2073 typedef MetadataTest DITemplateTypeParameterTest; 2074 2075 TEST_F(DITemplateTypeParameterTest, get) { 2076 StringRef Name = "template"; 2077 DIType *Type = getBasicType("basic"); 2078 2079 auto *N = DITemplateTypeParameter::get(Context, Name, Type); 2080 2081 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag()); 2082 EXPECT_EQ(Name, N->getName()); 2083 EXPECT_EQ(Type, N->getType()); 2084 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type)); 2085 2086 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type)); 2087 EXPECT_NE(N, 2088 DITemplateTypeParameter::get(Context, Name, getBasicType("other"))); 2089 2090 TempDITemplateTypeParameter Temp = N->clone(); 2091 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2092 } 2093 2094 typedef MetadataTest DITemplateValueParameterTest; 2095 2096 TEST_F(DITemplateValueParameterTest, get) { 2097 unsigned Tag = dwarf::DW_TAG_template_value_parameter; 2098 StringRef Name = "template"; 2099 DIType *Type = getBasicType("basic"); 2100 Metadata *Value = getConstantAsMetadata(); 2101 2102 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value); 2103 EXPECT_EQ(Tag, N->getTag()); 2104 EXPECT_EQ(Name, N->getName()); 2105 EXPECT_EQ(Type, N->getType()); 2106 EXPECT_EQ(Value, N->getValue()); 2107 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value)); 2108 2109 EXPECT_NE(N, DITemplateValueParameter::get( 2110 Context, dwarf::DW_TAG_GNU_template_template_param, Name, 2111 Type, Value)); 2112 EXPECT_NE(N, 2113 DITemplateValueParameter::get(Context, Tag, "other", Type, Value)); 2114 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, 2115 getBasicType("other"), Value)); 2116 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type, 2117 getConstantAsMetadata())); 2118 2119 TempDITemplateValueParameter Temp = N->clone(); 2120 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2121 } 2122 2123 typedef MetadataTest DIGlobalVariableTest; 2124 2125 TEST_F(DIGlobalVariableTest, get) { 2126 DIScope *Scope = getSubprogram(); 2127 StringRef Name = "name"; 2128 StringRef LinkageName = "linkage"; 2129 DIFile *File = getFile(); 2130 unsigned Line = 5; 2131 DIType *Type = getDerivedType(); 2132 bool IsLocalToUnit = false; 2133 bool IsDefinition = true; 2134 MDTuple *templateParams = getTuple(); 2135 DIDerivedType *StaticDataMemberDeclaration = 2136 cast<DIDerivedType>(getDerivedType()); 2137 2138 uint32_t AlignInBits = 8; 2139 2140 auto *N = DIGlobalVariable::get( 2141 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, 2142 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits); 2143 2144 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag()); 2145 EXPECT_EQ(Scope, N->getScope()); 2146 EXPECT_EQ(Name, N->getName()); 2147 EXPECT_EQ(LinkageName, N->getLinkageName()); 2148 EXPECT_EQ(File, N->getFile()); 2149 EXPECT_EQ(Line, N->getLine()); 2150 EXPECT_EQ(Type, N->getType()); 2151 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit()); 2152 EXPECT_EQ(IsDefinition, N->isDefinition()); 2153 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration()); 2154 EXPECT_EQ(templateParams, N->getTemplateParams()); 2155 EXPECT_EQ(AlignInBits, N->getAlignInBits()); 2156 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, 2157 Line, Type, IsLocalToUnit, IsDefinition, 2158 StaticDataMemberDeclaration, 2159 templateParams, AlignInBits)); 2160 2161 EXPECT_NE(N, DIGlobalVariable::get( 2162 Context, getSubprogram(), Name, LinkageName, File, Line, 2163 Type, IsLocalToUnit, IsDefinition, 2164 StaticDataMemberDeclaration, templateParams, AlignInBits)); 2165 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File, 2166 Line, Type, IsLocalToUnit, IsDefinition, 2167 StaticDataMemberDeclaration, 2168 templateParams, AlignInBits)); 2169 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line, 2170 Type, IsLocalToUnit, IsDefinition, 2171 StaticDataMemberDeclaration, 2172 templateParams, AlignInBits)); 2173 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, 2174 getFile(), Line, Type, IsLocalToUnit, 2175 IsDefinition, StaticDataMemberDeclaration, 2176 templateParams, AlignInBits)); 2177 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, 2178 Line + 1, Type, IsLocalToUnit, 2179 IsDefinition, StaticDataMemberDeclaration, 2180 templateParams, AlignInBits)); 2181 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, 2182 Line, getDerivedType(), IsLocalToUnit, 2183 IsDefinition, StaticDataMemberDeclaration, 2184 templateParams, AlignInBits)); 2185 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, 2186 Line, Type, !IsLocalToUnit, IsDefinition, 2187 StaticDataMemberDeclaration, 2188 templateParams, AlignInBits)); 2189 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, 2190 Line, Type, IsLocalToUnit, !IsDefinition, 2191 StaticDataMemberDeclaration, 2192 templateParams, AlignInBits)); 2193 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, 2194 Line, Type, IsLocalToUnit, IsDefinition, 2195 cast<DIDerivedType>(getDerivedType()), 2196 templateParams, AlignInBits)); 2197 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, 2198 Line, Type, IsLocalToUnit, IsDefinition, 2199 StaticDataMemberDeclaration, nullptr, 2200 AlignInBits)); 2201 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, 2202 Line, Type, IsLocalToUnit, IsDefinition, 2203 StaticDataMemberDeclaration, 2204 templateParams, (AlignInBits << 1))); 2205 2206 TempDIGlobalVariable Temp = N->clone(); 2207 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2208 } 2209 2210 typedef MetadataTest DIGlobalVariableExpressionTest; 2211 2212 TEST_F(DIGlobalVariableExpressionTest, get) { 2213 DIScope *Scope = getSubprogram(); 2214 StringRef Name = "name"; 2215 StringRef LinkageName = "linkage"; 2216 DIFile *File = getFile(); 2217 unsigned Line = 5; 2218 DIType *Type = getDerivedType(); 2219 bool IsLocalToUnit = false; 2220 bool IsDefinition = true; 2221 MDTuple *templateParams = getTuple(); 2222 auto *Expr = DIExpression::get(Context, {1, 2}); 2223 auto *Expr2 = DIExpression::get(Context, {1, 2, 3}); 2224 DIDerivedType *StaticDataMemberDeclaration = 2225 cast<DIDerivedType>(getDerivedType()); 2226 uint32_t AlignInBits = 8; 2227 2228 auto *Var = DIGlobalVariable::get( 2229 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, 2230 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits); 2231 auto *Var2 = DIGlobalVariable::get( 2232 Context, Scope, "other", LinkageName, File, Line, Type, IsLocalToUnit, 2233 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits); 2234 auto *N = DIGlobalVariableExpression::get(Context, Var, Expr); 2235 2236 EXPECT_EQ(Var, N->getVariable()); 2237 EXPECT_EQ(Expr, N->getExpression()); 2238 EXPECT_EQ(N, DIGlobalVariableExpression::get(Context, Var, Expr)); 2239 EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var2, Expr)); 2240 EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var, Expr2)); 2241 2242 TempDIGlobalVariableExpression Temp = N->clone(); 2243 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2244 } 2245 2246 typedef MetadataTest DILocalVariableTest; 2247 2248 TEST_F(DILocalVariableTest, get) { 2249 DILocalScope *Scope = getSubprogram(); 2250 StringRef Name = "name"; 2251 DIFile *File = getFile(); 2252 unsigned Line = 5; 2253 DIType *Type = getDerivedType(); 2254 unsigned Arg = 6; 2255 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7); 2256 uint32_t AlignInBits = 8; 2257 2258 auto *N = 2259 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags, 2260 AlignInBits); 2261 EXPECT_TRUE(N->isParameter()); 2262 EXPECT_EQ(Scope, N->getScope()); 2263 EXPECT_EQ(Name, N->getName()); 2264 EXPECT_EQ(File, N->getFile()); 2265 EXPECT_EQ(Line, N->getLine()); 2266 EXPECT_EQ(Type, N->getType()); 2267 EXPECT_EQ(Arg, N->getArg()); 2268 EXPECT_EQ(Flags, N->getFlags()); 2269 EXPECT_EQ(AlignInBits, N->getAlignInBits()); 2270 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, 2271 Flags, AlignInBits)); 2272 2273 EXPECT_FALSE( 2274 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags, 2275 AlignInBits)->isParameter()); 2276 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line, 2277 Type, Arg, Flags, AlignInBits)); 2278 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type, 2279 Arg, Flags, AlignInBits)); 2280 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type, 2281 Arg, Flags, AlignInBits)); 2282 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type, 2283 Arg, Flags, AlignInBits)); 2284 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, 2285 getDerivedType(), Arg, Flags, AlignInBits)); 2286 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, 2287 Arg + 1, Flags, AlignInBits)); 2288 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, 2289 Arg, Flags, (AlignInBits << 1))); 2290 2291 TempDILocalVariable Temp = N->clone(); 2292 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2293 } 2294 2295 TEST_F(DILocalVariableTest, getArg256) { 2296 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(), 2297 0, nullptr, 255, DINode::FlagZero, 0) 2298 ->getArg()); 2299 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(), 2300 0, nullptr, 256, DINode::FlagZero, 0) 2301 ->getArg()); 2302 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(), 2303 0, nullptr, 257, DINode::FlagZero, 0) 2304 ->getArg()); 2305 unsigned Max = UINT16_MAX; 2306 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(), 2307 0, nullptr, Max, DINode::FlagZero, 0) 2308 ->getArg()); 2309 } 2310 2311 typedef MetadataTest DIExpressionTest; 2312 2313 TEST_F(DIExpressionTest, get) { 2314 uint64_t Elements[] = {2, 6, 9, 78, 0}; 2315 auto *N = DIExpression::get(Context, Elements); 2316 EXPECT_EQ(makeArrayRef(Elements), N->getElements()); 2317 EXPECT_EQ(N, DIExpression::get(Context, Elements)); 2318 2319 EXPECT_EQ(5u, N->getNumElements()); 2320 EXPECT_EQ(2u, N->getElement(0)); 2321 EXPECT_EQ(6u, N->getElement(1)); 2322 EXPECT_EQ(9u, N->getElement(2)); 2323 EXPECT_EQ(78u, N->getElement(3)); 2324 EXPECT_EQ(0u, N->getElement(4)); 2325 2326 TempDIExpression Temp = N->clone(); 2327 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2328 2329 // Test DIExpression::prepend(). 2330 uint64_t Elts0[] = {dwarf::DW_OP_LLVM_fragment, 0, 32}; 2331 auto *N0 = DIExpression::get(Context, Elts0); 2332 auto *N0WithPrependedOps = DIExpression::prepend(N0, true, 64, true, true); 2333 uint64_t Elts1[] = {dwarf::DW_OP_deref, 2334 dwarf::DW_OP_plus_uconst, 64, 2335 dwarf::DW_OP_deref, 2336 dwarf::DW_OP_stack_value, 2337 dwarf::DW_OP_LLVM_fragment, 0, 32}; 2338 auto *N1 = DIExpression::get(Context, Elts1); 2339 EXPECT_EQ(N0WithPrependedOps, N1); 2340 2341 // Test DIExpression::append(). 2342 uint64_t Elts2[] = {dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 64, 2343 dwarf::DW_OP_deref, dwarf::DW_OP_stack_value}; 2344 auto *N2 = DIExpression::append(N0, Elts2); 2345 EXPECT_EQ(N0WithPrependedOps, N2); 2346 } 2347 2348 TEST_F(DIExpressionTest, isValid) { 2349 #define EXPECT_VALID(...) \ 2350 do { \ 2351 uint64_t Elements[] = {__VA_ARGS__}; \ 2352 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \ 2353 } while (false) 2354 #define EXPECT_INVALID(...) \ 2355 do { \ 2356 uint64_t Elements[] = {__VA_ARGS__}; \ 2357 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \ 2358 } while (false) 2359 2360 // Empty expression should be valid. 2361 EXPECT_TRUE(DIExpression::get(Context, None)); 2362 2363 // Valid constructions. 2364 EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6); 2365 EXPECT_VALID(dwarf::DW_OP_constu, 6, dwarf::DW_OP_plus); 2366 EXPECT_VALID(dwarf::DW_OP_deref); 2367 EXPECT_VALID(dwarf::DW_OP_LLVM_fragment, 3, 7); 2368 EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6, dwarf::DW_OP_deref); 2369 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6); 2370 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_LLVM_fragment, 3, 7); 2371 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6, 2372 dwarf::DW_OP_LLVM_fragment, 3, 7); 2373 2374 // Invalid constructions. 2375 EXPECT_INVALID(~0u); 2376 EXPECT_INVALID(dwarf::DW_OP_plus, 0); 2377 EXPECT_INVALID(dwarf::DW_OP_plus_uconst); 2378 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment); 2379 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3); 2380 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_plus_uconst, 3); 2381 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_deref); 2382 2383 #undef EXPECT_VALID 2384 #undef EXPECT_INVALID 2385 } 2386 2387 typedef MetadataTest DIObjCPropertyTest; 2388 2389 TEST_F(DIObjCPropertyTest, get) { 2390 StringRef Name = "name"; 2391 DIFile *File = getFile(); 2392 unsigned Line = 5; 2393 StringRef GetterName = "getter"; 2394 StringRef SetterName = "setter"; 2395 unsigned Attributes = 7; 2396 DIType *Type = getBasicType("basic"); 2397 2398 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName, 2399 SetterName, Attributes, Type); 2400 2401 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag()); 2402 EXPECT_EQ(Name, N->getName()); 2403 EXPECT_EQ(File, N->getFile()); 2404 EXPECT_EQ(Line, N->getLine()); 2405 EXPECT_EQ(GetterName, N->getGetterName()); 2406 EXPECT_EQ(SetterName, N->getSetterName()); 2407 EXPECT_EQ(Attributes, N->getAttributes()); 2408 EXPECT_EQ(Type, N->getType()); 2409 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName, 2410 SetterName, Attributes, Type)); 2411 2412 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName, 2413 SetterName, Attributes, Type)); 2414 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName, 2415 SetterName, Attributes, Type)); 2416 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName, 2417 SetterName, Attributes, Type)); 2418 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other", 2419 SetterName, Attributes, Type)); 2420 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName, 2421 "other", Attributes, Type)); 2422 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName, 2423 SetterName, Attributes + 1, Type)); 2424 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName, 2425 SetterName, Attributes, 2426 getBasicType("other"))); 2427 2428 TempDIObjCProperty Temp = N->clone(); 2429 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2430 } 2431 2432 typedef MetadataTest DIImportedEntityTest; 2433 2434 TEST_F(DIImportedEntityTest, get) { 2435 unsigned Tag = dwarf::DW_TAG_imported_module; 2436 DIScope *Scope = getSubprogram(); 2437 DINode *Entity = getCompositeType(); 2438 DIFile *File = getFile(); 2439 unsigned Line = 5; 2440 StringRef Name = "name"; 2441 2442 auto *N = 2443 DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name); 2444 2445 EXPECT_EQ(Tag, N->getTag()); 2446 EXPECT_EQ(Scope, N->getScope()); 2447 EXPECT_EQ(Entity, N->getEntity()); 2448 EXPECT_EQ(File, N->getFile()); 2449 EXPECT_EQ(Line, N->getLine()); 2450 EXPECT_EQ(Name, N->getName()); 2451 EXPECT_EQ( 2452 N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name)); 2453 2454 EXPECT_NE(N, 2455 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration, 2456 Scope, Entity, File, Line, Name)); 2457 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity, 2458 File, Line, Name)); 2459 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(), 2460 File, Line, Name)); 2461 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, nullptr, Line, 2462 Name)); 2463 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, 2464 Line + 1, Name)); 2465 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, 2466 "other")); 2467 2468 TempDIImportedEntity Temp = N->clone(); 2469 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); 2470 } 2471 2472 typedef MetadataTest MetadataAsValueTest; 2473 2474 TEST_F(MetadataAsValueTest, MDNode) { 2475 MDNode *N = MDNode::get(Context, None); 2476 auto *V = MetadataAsValue::get(Context, N); 2477 EXPECT_TRUE(V->getType()->isMetadataTy()); 2478 EXPECT_EQ(N, V->getMetadata()); 2479 2480 auto *V2 = MetadataAsValue::get(Context, N); 2481 EXPECT_EQ(V, V2); 2482 } 2483 2484 TEST_F(MetadataAsValueTest, MDNodeMDNode) { 2485 MDNode *N = MDNode::get(Context, None); 2486 Metadata *Ops[] = {N}; 2487 MDNode *N2 = MDNode::get(Context, Ops); 2488 auto *V = MetadataAsValue::get(Context, N2); 2489 EXPECT_TRUE(V->getType()->isMetadataTy()); 2490 EXPECT_EQ(N2, V->getMetadata()); 2491 2492 auto *V2 = MetadataAsValue::get(Context, N2); 2493 EXPECT_EQ(V, V2); 2494 2495 auto *V3 = MetadataAsValue::get(Context, N); 2496 EXPECT_TRUE(V3->getType()->isMetadataTy()); 2497 EXPECT_NE(V, V3); 2498 EXPECT_EQ(N, V3->getMetadata()); 2499 } 2500 2501 TEST_F(MetadataAsValueTest, MDNodeConstant) { 2502 auto *C = ConstantInt::getTrue(Context); 2503 auto *MD = ConstantAsMetadata::get(C); 2504 Metadata *Ops[] = {MD}; 2505 auto *N = MDNode::get(Context, Ops); 2506 2507 auto *V = MetadataAsValue::get(Context, MD); 2508 EXPECT_TRUE(V->getType()->isMetadataTy()); 2509 EXPECT_EQ(MD, V->getMetadata()); 2510 2511 auto *V2 = MetadataAsValue::get(Context, N); 2512 EXPECT_EQ(MD, V2->getMetadata()); 2513 EXPECT_EQ(V, V2); 2514 } 2515 2516 typedef MetadataTest ValueAsMetadataTest; 2517 2518 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) { 2519 Type *Ty = Type::getInt1PtrTy(Context); 2520 std::unique_ptr<GlobalVariable> GV0( 2521 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 2522 auto *MD = ValueAsMetadata::get(GV0.get()); 2523 EXPECT_TRUE(MD->getValue() == GV0.get()); 2524 ASSERT_TRUE(GV0->use_empty()); 2525 2526 std::unique_ptr<GlobalVariable> GV1( 2527 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 2528 GV0->replaceAllUsesWith(GV1.get()); 2529 EXPECT_TRUE(MD->getValue() == GV1.get()); 2530 } 2531 2532 TEST_F(ValueAsMetadataTest, TempTempReplacement) { 2533 // Create a constant. 2534 ConstantAsMetadata *CI = 2535 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0))); 2536 2537 auto Temp1 = MDTuple::getTemporary(Context, None); 2538 auto Temp2 = MDTuple::getTemporary(Context, {CI}); 2539 auto *N = MDTuple::get(Context, {Temp1.get()}); 2540 2541 // Test replacing a temporary node with another temporary node. 2542 Temp1->replaceAllUsesWith(Temp2.get()); 2543 EXPECT_EQ(N->getOperand(0), Temp2.get()); 2544 2545 // Clean up Temp2 for teardown. 2546 Temp2->replaceAllUsesWith(nullptr); 2547 } 2548 2549 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) { 2550 // Create a constant. 2551 ConstantAsMetadata *CI = 2552 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0))); 2553 2554 // Create a temporary to prevent nodes from resolving. 2555 auto Temp = MDTuple::getTemporary(Context, None); 2556 2557 // When the first operand of N1 gets reset to nullptr, it'll collide with N2. 2558 Metadata *Ops1[] = {CI, CI, Temp.get()}; 2559 Metadata *Ops2[] = {nullptr, CI, Temp.get()}; 2560 2561 auto *N1 = MDTuple::get(Context, Ops1); 2562 auto *N2 = MDTuple::get(Context, Ops2); 2563 ASSERT_NE(N1, N2); 2564 2565 // Tell metadata that the constant is getting deleted. 2566 // 2567 // After this, N1 will be invalid, so don't touch it. 2568 ValueAsMetadata::handleDeletion(CI->getValue()); 2569 EXPECT_EQ(nullptr, N2->getOperand(0)); 2570 EXPECT_EQ(nullptr, N2->getOperand(1)); 2571 EXPECT_EQ(Temp.get(), N2->getOperand(2)); 2572 2573 // Clean up Temp for teardown. 2574 Temp->replaceAllUsesWith(nullptr); 2575 } 2576 2577 typedef MetadataTest TrackingMDRefTest; 2578 2579 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) { 2580 Type *Ty = Type::getInt1PtrTy(Context); 2581 std::unique_ptr<GlobalVariable> GV0( 2582 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 2583 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get())); 2584 EXPECT_TRUE(MD->getValue() == GV0.get()); 2585 ASSERT_TRUE(GV0->use_empty()); 2586 2587 std::unique_ptr<GlobalVariable> GV1( 2588 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 2589 GV0->replaceAllUsesWith(GV1.get()); 2590 EXPECT_TRUE(MD->getValue() == GV1.get()); 2591 2592 // Reset it, so we don't inadvertently test deletion. 2593 MD.reset(); 2594 } 2595 2596 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) { 2597 Type *Ty = Type::getInt1PtrTy(Context); 2598 std::unique_ptr<GlobalVariable> GV( 2599 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); 2600 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get())); 2601 EXPECT_TRUE(MD->getValue() == GV.get()); 2602 ASSERT_TRUE(GV->use_empty()); 2603 2604 GV.reset(); 2605 EXPECT_TRUE(!MD); 2606 } 2607 2608 TEST(NamedMDNodeTest, Search) { 2609 LLVMContext Context; 2610 ConstantAsMetadata *C = 2611 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1)); 2612 ConstantAsMetadata *C2 = 2613 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2)); 2614 2615 Metadata *const V = C; 2616 Metadata *const V2 = C2; 2617 MDNode *n = MDNode::get(Context, V); 2618 MDNode *n2 = MDNode::get(Context, V2); 2619 2620 Module M("MyModule", Context); 2621 const char *Name = "llvm.NMD1"; 2622 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name); 2623 NMD->addOperand(n); 2624 NMD->addOperand(n2); 2625 2626 std::string Str; 2627 raw_string_ostream oss(Str); 2628 NMD->print(oss); 2629 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n", 2630 oss.str().c_str()); 2631 } 2632 2633 typedef MetadataTest FunctionAttachmentTest; 2634 TEST_F(FunctionAttachmentTest, setMetadata) { 2635 Function *F = getFunction("foo"); 2636 ASSERT_FALSE(F->hasMetadata()); 2637 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg)); 2638 EXPECT_EQ(nullptr, F->getMetadata("dbg")); 2639 EXPECT_EQ(nullptr, F->getMetadata("other")); 2640 2641 DISubprogram *SP1 = getSubprogram(); 2642 DISubprogram *SP2 = getSubprogram(); 2643 ASSERT_NE(SP1, SP2); 2644 2645 F->setMetadata("dbg", SP1); 2646 EXPECT_TRUE(F->hasMetadata()); 2647 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg)); 2648 EXPECT_EQ(SP1, F->getMetadata("dbg")); 2649 EXPECT_EQ(nullptr, F->getMetadata("other")); 2650 2651 F->setMetadata(LLVMContext::MD_dbg, SP2); 2652 EXPECT_TRUE(F->hasMetadata()); 2653 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg)); 2654 EXPECT_EQ(SP2, F->getMetadata("dbg")); 2655 EXPECT_EQ(nullptr, F->getMetadata("other")); 2656 2657 F->setMetadata("dbg", nullptr); 2658 EXPECT_FALSE(F->hasMetadata()); 2659 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg)); 2660 EXPECT_EQ(nullptr, F->getMetadata("dbg")); 2661 EXPECT_EQ(nullptr, F->getMetadata("other")); 2662 2663 MDTuple *T1 = getTuple(); 2664 MDTuple *T2 = getTuple(); 2665 ASSERT_NE(T1, T2); 2666 2667 F->setMetadata("other1", T1); 2668 F->setMetadata("other2", T2); 2669 EXPECT_TRUE(F->hasMetadata()); 2670 EXPECT_EQ(T1, F->getMetadata("other1")); 2671 EXPECT_EQ(T2, F->getMetadata("other2")); 2672 EXPECT_EQ(nullptr, F->getMetadata("dbg")); 2673 2674 F->setMetadata("other1", T2); 2675 F->setMetadata("other2", T1); 2676 EXPECT_EQ(T2, F->getMetadata("other1")); 2677 EXPECT_EQ(T1, F->getMetadata("other2")); 2678 2679 F->setMetadata("other1", nullptr); 2680 F->setMetadata("other2", nullptr); 2681 EXPECT_FALSE(F->hasMetadata()); 2682 EXPECT_EQ(nullptr, F->getMetadata("other1")); 2683 EXPECT_EQ(nullptr, F->getMetadata("other2")); 2684 } 2685 2686 TEST_F(FunctionAttachmentTest, getAll) { 2687 Function *F = getFunction("foo"); 2688 2689 MDTuple *T1 = getTuple(); 2690 MDTuple *T2 = getTuple(); 2691 MDTuple *P = getTuple(); 2692 DISubprogram *SP = getSubprogram(); 2693 2694 F->setMetadata("other1", T2); 2695 F->setMetadata(LLVMContext::MD_dbg, SP); 2696 F->setMetadata("other2", T1); 2697 F->setMetadata(LLVMContext::MD_prof, P); 2698 F->setMetadata("other2", T2); 2699 F->setMetadata("other1", T1); 2700 2701 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 2702 F->getAllMetadata(MDs); 2703 ASSERT_EQ(4u, MDs.size()); 2704 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first); 2705 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first); 2706 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first); 2707 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first); 2708 EXPECT_EQ(SP, MDs[0].second); 2709 EXPECT_EQ(P, MDs[1].second); 2710 EXPECT_EQ(T1, MDs[2].second); 2711 EXPECT_EQ(T2, MDs[3].second); 2712 } 2713 2714 TEST_F(FunctionAttachmentTest, Verifier) { 2715 Function *F = getFunction("foo"); 2716 F->setMetadata("attach", getTuple()); 2717 F->setIsMaterializable(true); 2718 2719 // Confirm this is materializable. 2720 ASSERT_TRUE(F->isMaterializable()); 2721 2722 // Materializable functions cannot have metadata attachments. 2723 EXPECT_TRUE(verifyFunction(*F)); 2724 2725 // Function declarations can. 2726 F->setIsMaterializable(false); 2727 EXPECT_FALSE(verifyModule(*F->getParent())); 2728 EXPECT_FALSE(verifyFunction(*F)); 2729 2730 // So can definitions. 2731 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F)); 2732 EXPECT_FALSE(verifyModule(*F->getParent())); 2733 EXPECT_FALSE(verifyFunction(*F)); 2734 } 2735 2736 TEST_F(FunctionAttachmentTest, EntryCount) { 2737 Function *F = getFunction("foo"); 2738 EXPECT_FALSE(F->getEntryCount().hasValue()); 2739 F->setEntryCount(12304, Function::PCT_Real); 2740 auto Count = F->getEntryCount(); 2741 EXPECT_TRUE(Count.hasValue()); 2742 EXPECT_EQ(12304u, Count.getCount()); 2743 EXPECT_EQ(Function::PCT_Real, Count.getType()); 2744 2745 // Repeat the same for synthetic counts. 2746 F = getFunction("bar"); 2747 EXPECT_FALSE(F->getEntryCount().hasValue()); 2748 F->setEntryCount(123, Function::PCT_Synthetic); 2749 Count = F->getEntryCount(); 2750 EXPECT_TRUE(Count.hasValue()); 2751 EXPECT_EQ(123u, Count.getCount()); 2752 EXPECT_EQ(Function::PCT_Synthetic, Count.getType()); 2753 } 2754 2755 TEST_F(FunctionAttachmentTest, SubprogramAttachment) { 2756 Function *F = getFunction("foo"); 2757 DISubprogram *SP = getSubprogram(); 2758 F->setSubprogram(SP); 2759 2760 // Note that the static_cast confirms that F->getSubprogram() actually 2761 // returns an DISubprogram. 2762 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram())); 2763 EXPECT_EQ(SP, F->getMetadata("dbg")); 2764 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg)); 2765 } 2766 2767 typedef MetadataTest DistinctMDOperandPlaceholderTest; 2768 TEST_F(DistinctMDOperandPlaceholderTest, getID) { 2769 EXPECT_EQ(7u, DistinctMDOperandPlaceholder(7).getID()); 2770 } 2771 2772 TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWith) { 2773 // Set up some placeholders. 2774 DistinctMDOperandPlaceholder PH0(7); 2775 DistinctMDOperandPlaceholder PH1(3); 2776 DistinctMDOperandPlaceholder PH2(0); 2777 Metadata *Ops[] = {&PH0, &PH1, &PH2}; 2778 auto *D = MDTuple::getDistinct(Context, Ops); 2779 ASSERT_EQ(&PH0, D->getOperand(0)); 2780 ASSERT_EQ(&PH1, D->getOperand(1)); 2781 ASSERT_EQ(&PH2, D->getOperand(2)); 2782 2783 // Replace them. 2784 auto *N0 = MDTuple::get(Context, None); 2785 auto *N1 = MDTuple::get(Context, N0); 2786 PH0.replaceUseWith(N0); 2787 PH1.replaceUseWith(N1); 2788 PH2.replaceUseWith(nullptr); 2789 EXPECT_EQ(N0, D->getOperand(0)); 2790 EXPECT_EQ(N1, D->getOperand(1)); 2791 EXPECT_EQ(nullptr, D->getOperand(2)); 2792 } 2793 2794 TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWithNoUser) { 2795 // There is no user, but we can still call replace. 2796 DistinctMDOperandPlaceholder(7).replaceUseWith(MDTuple::get(Context, None)); 2797 } 2798 2799 // Test various assertions in metadata tracking. Don't run these tests if gtest 2800 // will use SEH to recover from them. Two of these tests get halfway through 2801 // inserting metadata into DenseMaps for tracking purposes, and then they 2802 // assert, and we attempt to destroy an LLVMContext with broken invariants, 2803 // leading to infinite loops. 2804 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) && !defined(GTEST_HAS_SEH) 2805 TEST_F(DistinctMDOperandPlaceholderTest, MetadataAsValue) { 2806 // This shouldn't crash. 2807 DistinctMDOperandPlaceholder PH(7); 2808 EXPECT_DEATH(MetadataAsValue::get(Context, &PH), 2809 "Unexpected callback to owner"); 2810 } 2811 2812 TEST_F(DistinctMDOperandPlaceholderTest, UniquedMDNode) { 2813 // This shouldn't crash. 2814 DistinctMDOperandPlaceholder PH(7); 2815 EXPECT_DEATH(MDTuple::get(Context, &PH), "Unexpected callback to owner"); 2816 } 2817 2818 TEST_F(DistinctMDOperandPlaceholderTest, SecondDistinctMDNode) { 2819 // This shouldn't crash. 2820 DistinctMDOperandPlaceholder PH(7); 2821 MDTuple::getDistinct(Context, &PH); 2822 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH), 2823 "Placeholders can only be used once"); 2824 } 2825 2826 TEST_F(DistinctMDOperandPlaceholderTest, TrackingMDRefAndDistinctMDNode) { 2827 // TrackingMDRef doesn't install an owner callback, so it can't be detected 2828 // as an invalid use. However, using a placeholder in a TrackingMDRef *and* 2829 // a distinct node isn't possible and we should assert. 2830 // 2831 // (There's no positive test for using TrackingMDRef because it's not a 2832 // useful thing to do.) 2833 { 2834 DistinctMDOperandPlaceholder PH(7); 2835 MDTuple::getDistinct(Context, &PH); 2836 EXPECT_DEATH(TrackingMDRef Ref(&PH), "Placeholders can only be used once"); 2837 } 2838 { 2839 DistinctMDOperandPlaceholder PH(7); 2840 TrackingMDRef Ref(&PH); 2841 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH), 2842 "Placeholders can only be used once"); 2843 } 2844 } 2845 #endif 2846 2847 } // end namespace 2848