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