1 //===- DebugInfo.cpp - Debug Information Helper Classes -------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the helper classes used to build and interpret debug 10 // information in LLVM IR form. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm-c/DebugInfo.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/DenseSet.h" 17 #include "llvm/ADT/None.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallPtrSet.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/IR/BasicBlock.h" 23 #include "llvm/IR/Constants.h" 24 #include "llvm/IR/DebugInfoMetadata.h" 25 #include "llvm/IR/DebugLoc.h" 26 #include "llvm/IR/DebugInfo.h" 27 #include "llvm/IR/DIBuilder.h" 28 #include "llvm/IR/Function.h" 29 #include "llvm/IR/GVMaterializer.h" 30 #include "llvm/IR/Instruction.h" 31 #include "llvm/IR/IntrinsicInst.h" 32 #include "llvm/IR/LLVMContext.h" 33 #include "llvm/IR/Metadata.h" 34 #include "llvm/IR/Module.h" 35 #include "llvm/Support/Casting.h" 36 #include <algorithm> 37 #include <cassert> 38 #include <utility> 39 40 using namespace llvm; 41 using namespace llvm::dwarf; 42 43 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) { 44 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope)) 45 return LocalScope->getSubprogram(); 46 return nullptr; 47 } 48 49 //===----------------------------------------------------------------------===// 50 // DebugInfoFinder implementations. 51 //===----------------------------------------------------------------------===// 52 53 void DebugInfoFinder::reset() { 54 CUs.clear(); 55 SPs.clear(); 56 GVs.clear(); 57 TYs.clear(); 58 Scopes.clear(); 59 NodesSeen.clear(); 60 } 61 62 void DebugInfoFinder::processModule(const Module &M) { 63 for (auto *CU : M.debug_compile_units()) 64 processCompileUnit(CU); 65 for (auto &F : M.functions()) { 66 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram())) 67 processSubprogram(SP); 68 // There could be subprograms from inlined functions referenced from 69 // instructions only. Walk the function to find them. 70 for (const BasicBlock &BB : F) 71 for (const Instruction &I : BB) 72 processInstruction(M, I); 73 } 74 } 75 76 void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) { 77 if (!addCompileUnit(CU)) 78 return; 79 for (auto DIG : CU->getGlobalVariables()) { 80 if (!addGlobalVariable(DIG)) 81 continue; 82 auto *GV = DIG->getVariable(); 83 processScope(GV->getScope()); 84 processType(GV->getType()); 85 } 86 for (auto *ET : CU->getEnumTypes()) 87 processType(ET); 88 for (auto *RT : CU->getRetainedTypes()) 89 if (auto *T = dyn_cast<DIType>(RT)) 90 processType(T); 91 else 92 processSubprogram(cast<DISubprogram>(RT)); 93 for (auto *Import : CU->getImportedEntities()) { 94 auto *Entity = Import->getEntity(); 95 if (auto *T = dyn_cast<DIType>(Entity)) 96 processType(T); 97 else if (auto *SP = dyn_cast<DISubprogram>(Entity)) 98 processSubprogram(SP); 99 else if (auto *NS = dyn_cast<DINamespace>(Entity)) 100 processScope(NS->getScope()); 101 else if (auto *M = dyn_cast<DIModule>(Entity)) 102 processScope(M->getScope()); 103 } 104 } 105 106 void DebugInfoFinder::processInstruction(const Module &M, 107 const Instruction &I) { 108 if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I)) 109 processVariable(M, *DVI); 110 111 if (auto DbgLoc = I.getDebugLoc()) 112 processLocation(M, DbgLoc.get()); 113 } 114 115 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { 116 if (!Loc) 117 return; 118 processScope(Loc->getScope()); 119 processLocation(M, Loc->getInlinedAt()); 120 } 121 122 void DebugInfoFinder::processType(DIType *DT) { 123 if (!addType(DT)) 124 return; 125 processScope(DT->getScope()); 126 if (auto *ST = dyn_cast<DISubroutineType>(DT)) { 127 for (DIType *Ref : ST->getTypeArray()) 128 processType(Ref); 129 return; 130 } 131 if (auto *DCT = dyn_cast<DICompositeType>(DT)) { 132 processType(DCT->getBaseType()); 133 for (Metadata *D : DCT->getElements()) { 134 if (auto *T = dyn_cast<DIType>(D)) 135 processType(T); 136 else if (auto *SP = dyn_cast<DISubprogram>(D)) 137 processSubprogram(SP); 138 } 139 return; 140 } 141 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) { 142 processType(DDT->getBaseType()); 143 } 144 } 145 146 void DebugInfoFinder::processScope(DIScope *Scope) { 147 if (!Scope) 148 return; 149 if (auto *Ty = dyn_cast<DIType>(Scope)) { 150 processType(Ty); 151 return; 152 } 153 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) { 154 addCompileUnit(CU); 155 return; 156 } 157 if (auto *SP = dyn_cast<DISubprogram>(Scope)) { 158 processSubprogram(SP); 159 return; 160 } 161 if (!addScope(Scope)) 162 return; 163 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) { 164 processScope(LB->getScope()); 165 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) { 166 processScope(NS->getScope()); 167 } else if (auto *M = dyn_cast<DIModule>(Scope)) { 168 processScope(M->getScope()); 169 } 170 } 171 172 void DebugInfoFinder::processSubprogram(DISubprogram *SP) { 173 if (!addSubprogram(SP)) 174 return; 175 processScope(SP->getScope()); 176 // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a 177 // ValueMap containing identity mappings for all of the DICompileUnit's, not 178 // just DISubprogram's, referenced from anywhere within the Function being 179 // cloned prior to calling MapMetadata / RemapInstruction to avoid their 180 // duplication later as DICompileUnit's are also directly referenced by 181 // llvm.dbg.cu list. Thefore we need to collect DICompileUnit's here as well. 182 // Also, DICompileUnit's may reference DISubprogram's too and therefore need 183 // to be at least looked through. 184 processCompileUnit(SP->getUnit()); 185 processType(SP->getType()); 186 for (auto *Element : SP->getTemplateParams()) { 187 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) { 188 processType(TType->getType()); 189 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) { 190 processType(TVal->getType()); 191 } 192 } 193 } 194 195 void DebugInfoFinder::processVariable(const Module &M, 196 const DbgVariableIntrinsic &DVI) { 197 auto *N = dyn_cast<MDNode>(DVI.getVariable()); 198 if (!N) 199 return; 200 201 auto *DV = dyn_cast<DILocalVariable>(N); 202 if (!DV) 203 return; 204 205 if (!NodesSeen.insert(DV).second) 206 return; 207 processScope(DV->getScope()); 208 processType(DV->getType()); 209 } 210 211 bool DebugInfoFinder::addType(DIType *DT) { 212 if (!DT) 213 return false; 214 215 if (!NodesSeen.insert(DT).second) 216 return false; 217 218 TYs.push_back(const_cast<DIType *>(DT)); 219 return true; 220 } 221 222 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { 223 if (!CU) 224 return false; 225 if (!NodesSeen.insert(CU).second) 226 return false; 227 228 CUs.push_back(CU); 229 return true; 230 } 231 232 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) { 233 if (!NodesSeen.insert(DIG).second) 234 return false; 235 236 GVs.push_back(DIG); 237 return true; 238 } 239 240 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { 241 if (!SP) 242 return false; 243 244 if (!NodesSeen.insert(SP).second) 245 return false; 246 247 SPs.push_back(SP); 248 return true; 249 } 250 251 bool DebugInfoFinder::addScope(DIScope *Scope) { 252 if (!Scope) 253 return false; 254 // FIXME: Ocaml binding generates a scope with no content, we treat it 255 // as null for now. 256 if (Scope->getNumOperands() == 0) 257 return false; 258 if (!NodesSeen.insert(Scope).second) 259 return false; 260 Scopes.push_back(Scope); 261 return true; 262 } 263 264 static MDNode *updateLoopMetadataDebugLocationsImpl( 265 MDNode *OrigLoopID, 266 function_ref<DILocation *(const DILocation &)> Updater) { 267 assert(OrigLoopID && OrigLoopID->getNumOperands() > 0 && 268 "Loop ID needs at least one operand"); 269 assert(OrigLoopID && OrigLoopID->getOperand(0).get() == OrigLoopID && 270 "Loop ID should refer to itself"); 271 272 // Save space for the self-referential LoopID. 273 SmallVector<Metadata *, 4> MDs = {nullptr}; 274 275 for (unsigned i = 1; i < OrigLoopID->getNumOperands(); ++i) { 276 Metadata *MD = OrigLoopID->getOperand(i); 277 if (DILocation *DL = dyn_cast<DILocation>(MD)) { 278 if (DILocation *NewDL = Updater(*DL)) 279 MDs.push_back(NewDL); 280 } else 281 MDs.push_back(MD); 282 } 283 284 MDNode *NewLoopID = MDNode::getDistinct(OrigLoopID->getContext(), MDs); 285 // Insert the self-referential LoopID. 286 NewLoopID->replaceOperandWith(0, NewLoopID); 287 return NewLoopID; 288 } 289 290 void llvm::updateLoopMetadataDebugLocations( 291 Instruction &I, function_ref<DILocation *(const DILocation &)> Updater) { 292 MDNode *OrigLoopID = I.getMetadata(LLVMContext::MD_loop); 293 if (!OrigLoopID) 294 return; 295 MDNode *NewLoopID = updateLoopMetadataDebugLocationsImpl(OrigLoopID, Updater); 296 I.setMetadata(LLVMContext::MD_loop, NewLoopID); 297 } 298 299 static MDNode *stripDebugLocFromLoopID(MDNode *N) { 300 assert(!N->operands().empty() && "Missing self reference?"); 301 302 // if there is no debug location, we do not have to rewrite this MDNode. 303 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { 304 return isa<DILocation>(Op.get()); 305 })) 306 return N; 307 308 // If there is only the debug location without any actual loop metadata, we 309 // can remove the metadata. 310 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { 311 return !isa<DILocation>(Op.get()); 312 })) 313 return nullptr; 314 315 auto dropDebugLoc = [](const DILocation &) -> DILocation * { 316 return nullptr; 317 }; 318 return updateLoopMetadataDebugLocationsImpl(N, dropDebugLoc); 319 } 320 321 bool llvm::stripDebugInfo(Function &F) { 322 bool Changed = false; 323 if (F.hasMetadata(LLVMContext::MD_dbg)) { 324 Changed = true; 325 F.setSubprogram(nullptr); 326 } 327 328 DenseMap<MDNode*, MDNode*> LoopIDsMap; 329 for (BasicBlock &BB : F) { 330 for (auto II = BB.begin(), End = BB.end(); II != End;) { 331 Instruction &I = *II++; // We may delete the instruction, increment now. 332 if (isa<DbgInfoIntrinsic>(&I)) { 333 I.eraseFromParent(); 334 Changed = true; 335 continue; 336 } 337 if (I.getDebugLoc()) { 338 Changed = true; 339 I.setDebugLoc(DebugLoc()); 340 } 341 if (auto *LoopID = I.getMetadata(LLVMContext::MD_loop)) { 342 auto *NewLoopID = LoopIDsMap.lookup(LoopID); 343 if (!NewLoopID) 344 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID); 345 if (NewLoopID != LoopID) 346 I.setMetadata(LLVMContext::MD_loop, NewLoopID); 347 } 348 } 349 } 350 return Changed; 351 } 352 353 bool llvm::StripDebugInfo(Module &M) { 354 bool Changed = false; 355 356 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), 357 NME = M.named_metadata_end(); NMI != NME;) { 358 NamedMDNode *NMD = &*NMI; 359 ++NMI; 360 361 // We're stripping debug info, and without them, coverage information 362 // doesn't quite make sense. 363 if (NMD->getName().startswith("llvm.dbg.") || 364 NMD->getName() == "llvm.gcov") { 365 NMD->eraseFromParent(); 366 Changed = true; 367 } 368 } 369 370 for (Function &F : M) 371 Changed |= stripDebugInfo(F); 372 373 for (auto &GV : M.globals()) { 374 Changed |= GV.eraseMetadata(LLVMContext::MD_dbg); 375 } 376 377 if (GVMaterializer *Materializer = M.getMaterializer()) 378 Materializer->setStripDebugInfo(); 379 380 return Changed; 381 } 382 383 namespace { 384 385 /// Helper class to downgrade -g metadata to -gline-tables-only metadata. 386 class DebugTypeInfoRemoval { 387 DenseMap<Metadata *, Metadata *> Replacements; 388 389 public: 390 /// The (void)() type. 391 MDNode *EmptySubroutineType; 392 393 private: 394 /// Remember what linkage name we originally had before stripping. If we end 395 /// up making two subprograms identical who originally had different linkage 396 /// names, then we need to make one of them distinct, to avoid them getting 397 /// uniqued. Maps the new node to the old linkage name. 398 DenseMap<DISubprogram *, StringRef> NewToLinkageName; 399 400 // TODO: Remember the distinct subprogram we created for a given linkage name, 401 // so that we can continue to unique whenever possible. Map <newly created 402 // node, old linkage name> to the first (possibly distinct) mdsubprogram 403 // created for that combination. This is not strictly needed for correctness, 404 // but can cut down on the number of MDNodes and let us diff cleanly with the 405 // output of -gline-tables-only. 406 407 public: 408 DebugTypeInfoRemoval(LLVMContext &C) 409 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0, 410 MDNode::get(C, {}))) {} 411 412 Metadata *map(Metadata *M) { 413 if (!M) 414 return nullptr; 415 auto Replacement = Replacements.find(M); 416 if (Replacement != Replacements.end()) 417 return Replacement->second; 418 419 return M; 420 } 421 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); } 422 423 /// Recursively remap N and all its referenced children. Does a DF post-order 424 /// traversal, so as to remap bottoms up. 425 void traverseAndRemap(MDNode *N) { traverse(N); } 426 427 private: 428 // Create a new DISubprogram, to replace the one given. 429 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) { 430 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile())); 431 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : ""; 432 DISubprogram *Declaration = nullptr; 433 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType())); 434 DIType *ContainingType = 435 cast_or_null<DIType>(map(MDS->getContainingType())); 436 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit())); 437 auto Variables = nullptr; 438 auto TemplateParams = nullptr; 439 440 // Make a distinct DISubprogram, for situations that warrent it. 441 auto distinctMDSubprogram = [&]() { 442 return DISubprogram::getDistinct( 443 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, 444 FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(), 445 ContainingType, MDS->getVirtualIndex(), MDS->getThisAdjustment(), 446 MDS->getFlags(), MDS->getSPFlags(), Unit, TemplateParams, Declaration, 447 Variables); 448 }; 449 450 if (MDS->isDistinct()) 451 return distinctMDSubprogram(); 452 453 auto *NewMDS = DISubprogram::get( 454 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, 455 FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(), ContainingType, 456 MDS->getVirtualIndex(), MDS->getThisAdjustment(), MDS->getFlags(), 457 MDS->getSPFlags(), Unit, TemplateParams, Declaration, Variables); 458 459 StringRef OldLinkageName = MDS->getLinkageName(); 460 461 // See if we need to make a distinct one. 462 auto OrigLinkage = NewToLinkageName.find(NewMDS); 463 if (OrigLinkage != NewToLinkageName.end()) { 464 if (OrigLinkage->second == OldLinkageName) 465 // We're good. 466 return NewMDS; 467 468 // Otherwise, need to make a distinct one. 469 // TODO: Query the map to see if we already have one. 470 return distinctMDSubprogram(); 471 } 472 473 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()}); 474 return NewMDS; 475 } 476 477 /// Create a new compile unit, to replace the one given 478 DICompileUnit *getReplacementCU(DICompileUnit *CU) { 479 // Drop skeleton CUs. 480 if (CU->getDWOId()) 481 return nullptr; 482 483 auto *File = cast_or_null<DIFile>(map(CU->getFile())); 484 MDTuple *EnumTypes = nullptr; 485 MDTuple *RetainedTypes = nullptr; 486 MDTuple *GlobalVariables = nullptr; 487 MDTuple *ImportedEntities = nullptr; 488 return DICompileUnit::getDistinct( 489 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(), 490 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(), 491 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes, 492 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(), 493 CU->getDWOId(), CU->getSplitDebugInlining(), 494 CU->getDebugInfoForProfiling(), CU->getNameTableKind(), 495 CU->getRangesBaseAddress(), CU->getSysRoot(), CU->getSDK()); 496 } 497 498 DILocation *getReplacementMDLocation(DILocation *MLD) { 499 auto *Scope = map(MLD->getScope()); 500 auto *InlinedAt = map(MLD->getInlinedAt()); 501 if (MLD->isDistinct()) 502 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(), 503 MLD->getColumn(), Scope, InlinedAt); 504 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(), 505 Scope, InlinedAt); 506 } 507 508 /// Create a new generic MDNode, to replace the one given 509 MDNode *getReplacementMDNode(MDNode *N) { 510 SmallVector<Metadata *, 8> Ops; 511 Ops.reserve(N->getNumOperands()); 512 for (auto &I : N->operands()) 513 if (I) 514 Ops.push_back(map(I)); 515 auto *Ret = MDNode::get(N->getContext(), Ops); 516 return Ret; 517 } 518 519 /// Attempt to re-map N to a newly created node. 520 void remap(MDNode *N) { 521 if (Replacements.count(N)) 522 return; 523 524 auto doRemap = [&](MDNode *N) -> MDNode * { 525 if (!N) 526 return nullptr; 527 if (auto *MDSub = dyn_cast<DISubprogram>(N)) { 528 remap(MDSub->getUnit()); 529 return getReplacementSubprogram(MDSub); 530 } 531 if (isa<DISubroutineType>(N)) 532 return EmptySubroutineType; 533 if (auto *CU = dyn_cast<DICompileUnit>(N)) 534 return getReplacementCU(CU); 535 if (isa<DIFile>(N)) 536 return N; 537 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N)) 538 // Remap to our referenced scope (recursively). 539 return mapNode(MDLB->getScope()); 540 if (auto *MLD = dyn_cast<DILocation>(N)) 541 return getReplacementMDLocation(MLD); 542 543 // Otherwise, if we see these, just drop them now. Not strictly necessary, 544 // but this speeds things up a little. 545 if (isa<DINode>(N)) 546 return nullptr; 547 548 return getReplacementMDNode(N); 549 }; 550 Replacements[N] = doRemap(N); 551 } 552 553 /// Do the remapping traversal. 554 void traverse(MDNode *); 555 }; 556 557 } // end anonymous namespace 558 559 void DebugTypeInfoRemoval::traverse(MDNode *N) { 560 if (!N || Replacements.count(N)) 561 return; 562 563 // To avoid cycles, as well as for efficiency sake, we will sometimes prune 564 // parts of the graph. 565 auto prune = [](MDNode *Parent, MDNode *Child) { 566 if (auto *MDS = dyn_cast<DISubprogram>(Parent)) 567 return Child == MDS->getRetainedNodes().get(); 568 return false; 569 }; 570 571 SmallVector<MDNode *, 16> ToVisit; 572 DenseSet<MDNode *> Opened; 573 574 // Visit each node starting at N in post order, and map them. 575 ToVisit.push_back(N); 576 while (!ToVisit.empty()) { 577 auto *N = ToVisit.back(); 578 if (!Opened.insert(N).second) { 579 // Close it. 580 remap(N); 581 ToVisit.pop_back(); 582 continue; 583 } 584 for (auto &I : N->operands()) 585 if (auto *MDN = dyn_cast_or_null<MDNode>(I)) 586 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) && 587 !isa<DICompileUnit>(MDN)) 588 ToVisit.push_back(MDN); 589 } 590 } 591 592 bool llvm::stripNonLineTableDebugInfo(Module &M) { 593 bool Changed = false; 594 595 // First off, delete the debug intrinsics. 596 auto RemoveUses = [&](StringRef Name) { 597 if (auto *DbgVal = M.getFunction(Name)) { 598 while (!DbgVal->use_empty()) 599 cast<Instruction>(DbgVal->user_back())->eraseFromParent(); 600 DbgVal->eraseFromParent(); 601 Changed = true; 602 } 603 }; 604 RemoveUses("llvm.dbg.addr"); 605 RemoveUses("llvm.dbg.declare"); 606 RemoveUses("llvm.dbg.label"); 607 RemoveUses("llvm.dbg.value"); 608 609 // Delete non-CU debug info named metadata nodes. 610 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end(); 611 NMI != NME;) { 612 NamedMDNode *NMD = &*NMI; 613 ++NMI; 614 // Specifically keep dbg.cu around. 615 if (NMD->getName() == "llvm.dbg.cu") 616 continue; 617 } 618 619 // Drop all dbg attachments from global variables. 620 for (auto &GV : M.globals()) 621 GV.eraseMetadata(LLVMContext::MD_dbg); 622 623 DebugTypeInfoRemoval Mapper(M.getContext()); 624 auto remap = [&](MDNode *Node) -> MDNode * { 625 if (!Node) 626 return nullptr; 627 Mapper.traverseAndRemap(Node); 628 auto *NewNode = Mapper.mapNode(Node); 629 Changed |= Node != NewNode; 630 Node = NewNode; 631 return NewNode; 632 }; 633 634 // Rewrite the DebugLocs to be equivalent to what 635 // -gline-tables-only would have created. 636 for (auto &F : M) { 637 if (auto *SP = F.getSubprogram()) { 638 Mapper.traverseAndRemap(SP); 639 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP)); 640 Changed |= SP != NewSP; 641 F.setSubprogram(NewSP); 642 } 643 for (auto &BB : F) { 644 for (auto &I : BB) { 645 auto remapDebugLoc = [&](const DebugLoc &DL) -> DebugLoc { 646 auto *Scope = DL.getScope(); 647 MDNode *InlinedAt = DL.getInlinedAt(); 648 Scope = remap(Scope); 649 InlinedAt = remap(InlinedAt); 650 return DILocation::get(M.getContext(), DL.getLine(), DL.getCol(), 651 Scope, InlinedAt); 652 }; 653 654 if (I.getDebugLoc() != DebugLoc()) 655 I.setDebugLoc(remapDebugLoc(I.getDebugLoc())); 656 657 // Remap DILocations in llvm.loop attachments. 658 updateLoopMetadataDebugLocations(I, [&](const DILocation &Loc) { 659 return remapDebugLoc(&Loc).get(); 660 }); 661 } 662 } 663 } 664 665 // Create a new llvm.dbg.cu, which is equivalent to the one 666 // -gline-tables-only would have created. 667 for (auto &NMD : M.getNamedMDList()) { 668 SmallVector<MDNode *, 8> Ops; 669 for (MDNode *Op : NMD.operands()) 670 Ops.push_back(remap(Op)); 671 672 if (!Changed) 673 continue; 674 675 NMD.clearOperands(); 676 for (auto *Op : Ops) 677 if (Op) 678 NMD.addOperand(Op); 679 } 680 return Changed; 681 } 682 683 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { 684 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( 685 M.getModuleFlag("Debug Info Version"))) 686 return Val->getZExtValue(); 687 return 0; 688 } 689 690 void Instruction::applyMergedLocation(const DILocation *LocA, 691 const DILocation *LocB) { 692 setDebugLoc(DILocation::getMergedLocation(LocA, LocB)); 693 } 694 695 void Instruction::updateLocationAfterHoist() { dropLocation(); } 696 697 void Instruction::dropLocation() { 698 const DebugLoc &DL = getDebugLoc(); 699 if (!DL) 700 return; 701 702 // If this isn't a call, drop the location to allow a location from a 703 // preceding instruction to propagate. 704 if (!isa<CallBase>(this)) { 705 setDebugLoc(DebugLoc()); 706 return; 707 } 708 709 // Set a line 0 location for calls to preserve scope information in case 710 // inlining occurs. 711 DISubprogram *SP = getFunction()->getSubprogram(); 712 if (SP) 713 // If a function scope is available, set it on the line 0 location. When 714 // hoisting a call to a predecessor block, using the function scope avoids 715 // making it look like the callee was reached earlier than it should be. 716 setDebugLoc(DILocation::get(getContext(), 0, 0, SP)); 717 else 718 // The parent function has no scope. Go ahead and drop the location. If 719 // the parent function is inlined, and the callee has a subprogram, the 720 // inliner will attach a location to the call. 721 // 722 // One alternative is to set a line 0 location with the existing scope and 723 // inlinedAt info. The location might be sensitive to when inlining occurs. 724 setDebugLoc(DebugLoc()); 725 } 726 727 //===----------------------------------------------------------------------===// 728 // LLVM C API implementations. 729 //===----------------------------------------------------------------------===// 730 731 static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) { 732 switch (lang) { 733 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \ 734 case LLVMDWARFSourceLanguage##NAME: \ 735 return ID; 736 #include "llvm/BinaryFormat/Dwarf.def" 737 #undef HANDLE_DW_LANG 738 } 739 llvm_unreachable("Unhandled Tag"); 740 } 741 742 template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) { 743 return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr); 744 } 745 746 static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) { 747 return static_cast<DINode::DIFlags>(Flags); 748 } 749 750 static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) { 751 return static_cast<LLVMDIFlags>(Flags); 752 } 753 754 static DISubprogram::DISPFlags 755 pack_into_DISPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized) { 756 return DISubprogram::toSPFlags(IsLocalToUnit, IsDefinition, IsOptimized); 757 } 758 759 unsigned LLVMDebugMetadataVersion() { 760 return DEBUG_METADATA_VERSION; 761 } 762 763 LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) { 764 return wrap(new DIBuilder(*unwrap(M), false)); 765 } 766 767 LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) { 768 return wrap(new DIBuilder(*unwrap(M))); 769 } 770 771 unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) { 772 return getDebugMetadataVersionFromModule(*unwrap(M)); 773 } 774 775 LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) { 776 return StripDebugInfo(*unwrap(M)); 777 } 778 779 void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) { 780 delete unwrap(Builder); 781 } 782 783 void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) { 784 unwrap(Builder)->finalize(); 785 } 786 787 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit( 788 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang, 789 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen, 790 LLVMBool isOptimized, const char *Flags, size_t FlagsLen, 791 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen, 792 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining, 793 LLVMBool DebugInfoForProfiling, const char *SysRoot, size_t SysRootLen, 794 const char *SDK, size_t SDKLen) { 795 auto File = unwrapDI<DIFile>(FileRef); 796 797 return wrap(unwrap(Builder)->createCompileUnit( 798 map_from_llvmDWARFsourcelanguage(Lang), File, 799 StringRef(Producer, ProducerLen), isOptimized, StringRef(Flags, FlagsLen), 800 RuntimeVer, StringRef(SplitName, SplitNameLen), 801 static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId, 802 SplitDebugInlining, DebugInfoForProfiling, 803 DICompileUnit::DebugNameTableKind::Default, false, 804 StringRef(SysRoot, SysRootLen), StringRef(SDK, SDKLen))); 805 } 806 807 LLVMMetadataRef 808 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename, 809 size_t FilenameLen, const char *Directory, 810 size_t DirectoryLen) { 811 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen), 812 StringRef(Directory, DirectoryLen))); 813 } 814 815 LLVMMetadataRef 816 LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope, 817 const char *Name, size_t NameLen, 818 const char *ConfigMacros, size_t ConfigMacrosLen, 819 const char *IncludePath, size_t IncludePathLen, 820 const char *APINotesFile, size_t APINotesFileLen) { 821 return wrap(unwrap(Builder)->createModule( 822 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), 823 StringRef(ConfigMacros, ConfigMacrosLen), 824 StringRef(IncludePath, IncludePathLen), 825 StringRef(APINotesFile, APINotesFileLen))); 826 } 827 828 LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder, 829 LLVMMetadataRef ParentScope, 830 const char *Name, size_t NameLen, 831 LLVMBool ExportSymbols) { 832 return wrap(unwrap(Builder)->createNameSpace( 833 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols)); 834 } 835 836 LLVMMetadataRef LLVMDIBuilderCreateFunction( 837 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 838 size_t NameLen, const char *LinkageName, size_t LinkageNameLen, 839 LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty, 840 LLVMBool IsLocalToUnit, LLVMBool IsDefinition, 841 unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) { 842 return wrap(unwrap(Builder)->createFunction( 843 unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen}, 844 unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty), ScopeLine, 845 map_from_llvmDIFlags(Flags), 846 pack_into_DISPFlags(IsLocalToUnit, IsDefinition, IsOptimized), nullptr, 847 nullptr, nullptr)); 848 } 849 850 851 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock( 852 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, 853 LLVMMetadataRef File, unsigned Line, unsigned Col) { 854 return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope), 855 unwrapDI<DIFile>(File), 856 Line, Col)); 857 } 858 859 LLVMMetadataRef 860 LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder, 861 LLVMMetadataRef Scope, 862 LLVMMetadataRef File, 863 unsigned Discriminator) { 864 return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope), 865 unwrapDI<DIFile>(File), 866 Discriminator)); 867 } 868 869 LLVMMetadataRef 870 LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder, 871 LLVMMetadataRef Scope, 872 LLVMMetadataRef NS, 873 LLVMMetadataRef File, 874 unsigned Line) { 875 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope), 876 unwrapDI<DINamespace>(NS), 877 unwrapDI<DIFile>(File), 878 Line)); 879 } 880 881 LLVMMetadataRef 882 LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder, 883 LLVMMetadataRef Scope, 884 LLVMMetadataRef ImportedEntity, 885 LLVMMetadataRef File, 886 unsigned Line) { 887 return wrap(unwrap(Builder)->createImportedModule( 888 unwrapDI<DIScope>(Scope), 889 unwrapDI<DIImportedEntity>(ImportedEntity), 890 unwrapDI<DIFile>(File), Line)); 891 } 892 893 LLVMMetadataRef 894 LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder, 895 LLVMMetadataRef Scope, 896 LLVMMetadataRef M, 897 LLVMMetadataRef File, 898 unsigned Line) { 899 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope), 900 unwrapDI<DIModule>(M), 901 unwrapDI<DIFile>(File), 902 Line)); 903 } 904 905 LLVMMetadataRef 906 LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder, 907 LLVMMetadataRef Scope, 908 LLVMMetadataRef Decl, 909 LLVMMetadataRef File, 910 unsigned Line, 911 const char *Name, size_t NameLen) { 912 return wrap(unwrap(Builder)->createImportedDeclaration( 913 unwrapDI<DIScope>(Scope), 914 unwrapDI<DINode>(Decl), 915 unwrapDI<DIFile>(File), Line, {Name, NameLen})); 916 } 917 918 LLVMMetadataRef 919 LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line, 920 unsigned Column, LLVMMetadataRef Scope, 921 LLVMMetadataRef InlinedAt) { 922 return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope), 923 unwrap(InlinedAt))); 924 } 925 926 unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) { 927 return unwrapDI<DILocation>(Location)->getLine(); 928 } 929 930 unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) { 931 return unwrapDI<DILocation>(Location)->getColumn(); 932 } 933 934 LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) { 935 return wrap(unwrapDI<DILocation>(Location)->getScope()); 936 } 937 938 LLVMMetadataRef LLVMDILocationGetInlinedAt(LLVMMetadataRef Location) { 939 return wrap(unwrapDI<DILocation>(Location)->getInlinedAt()); 940 } 941 942 LLVMMetadataRef LLVMDIScopeGetFile(LLVMMetadataRef Scope) { 943 return wrap(unwrapDI<DIScope>(Scope)->getFile()); 944 } 945 946 const char *LLVMDIFileGetDirectory(LLVMMetadataRef File, unsigned *Len) { 947 auto Dir = unwrapDI<DIFile>(File)->getDirectory(); 948 *Len = Dir.size(); 949 return Dir.data(); 950 } 951 952 const char *LLVMDIFileGetFilename(LLVMMetadataRef File, unsigned *Len) { 953 auto Name = unwrapDI<DIFile>(File)->getFilename(); 954 *Len = Name.size(); 955 return Name.data(); 956 } 957 958 const char *LLVMDIFileGetSource(LLVMMetadataRef File, unsigned *Len) { 959 if (auto Src = unwrapDI<DIFile>(File)->getSource()) { 960 *Len = Src->size(); 961 return Src->data(); 962 } 963 *Len = 0; 964 return ""; 965 } 966 967 LLVMMetadataRef LLVMDIBuilderCreateMacro(LLVMDIBuilderRef Builder, 968 LLVMMetadataRef ParentMacroFile, 969 unsigned Line, 970 LLVMDWARFMacinfoRecordType RecordType, 971 const char *Name, size_t NameLen, 972 const char *Value, size_t ValueLen) { 973 return wrap( 974 unwrap(Builder)->createMacro(unwrapDI<DIMacroFile>(ParentMacroFile), Line, 975 static_cast<MacinfoRecordType>(RecordType), 976 {Name, NameLen}, {Value, ValueLen})); 977 } 978 979 LLVMMetadataRef 980 LLVMDIBuilderCreateTempMacroFile(LLVMDIBuilderRef Builder, 981 LLVMMetadataRef ParentMacroFile, unsigned Line, 982 LLVMMetadataRef File) { 983 return wrap(unwrap(Builder)->createTempMacroFile( 984 unwrapDI<DIMacroFile>(ParentMacroFile), Line, unwrapDI<DIFile>(File))); 985 } 986 987 LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder, 988 const char *Name, size_t NameLen, 989 int64_t Value, 990 LLVMBool IsUnsigned) { 991 return wrap(unwrap(Builder)->createEnumerator({Name, NameLen}, Value, 992 IsUnsigned != 0)); 993 } 994 995 LLVMMetadataRef LLVMDIBuilderCreateEnumerationType( 996 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 997 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 998 uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements, 999 unsigned NumElements, LLVMMetadataRef ClassTy) { 1000 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 1001 NumElements}); 1002 return wrap(unwrap(Builder)->createEnumerationType( 1003 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 1004 LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy))); 1005 } 1006 1007 LLVMMetadataRef LLVMDIBuilderCreateUnionType( 1008 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1009 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 1010 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags, 1011 LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang, 1012 const char *UniqueId, size_t UniqueIdLen) { 1013 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 1014 NumElements}); 1015 return wrap(unwrap(Builder)->createUnionType( 1016 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 1017 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags), 1018 Elts, RunTimeLang, {UniqueId, UniqueIdLen})); 1019 } 1020 1021 1022 LLVMMetadataRef 1023 LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size, 1024 uint32_t AlignInBits, LLVMMetadataRef Ty, 1025 LLVMMetadataRef *Subscripts, 1026 unsigned NumSubscripts) { 1027 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts), 1028 NumSubscripts}); 1029 return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits, 1030 unwrapDI<DIType>(Ty), Subs)); 1031 } 1032 1033 LLVMMetadataRef 1034 LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size, 1035 uint32_t AlignInBits, LLVMMetadataRef Ty, 1036 LLVMMetadataRef *Subscripts, 1037 unsigned NumSubscripts) { 1038 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts), 1039 NumSubscripts}); 1040 return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits, 1041 unwrapDI<DIType>(Ty), Subs)); 1042 } 1043 1044 LLVMMetadataRef 1045 LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name, 1046 size_t NameLen, uint64_t SizeInBits, 1047 LLVMDWARFTypeEncoding Encoding, 1048 LLVMDIFlags Flags) { 1049 return wrap(unwrap(Builder)->createBasicType({Name, NameLen}, 1050 SizeInBits, Encoding, 1051 map_from_llvmDIFlags(Flags))); 1052 } 1053 1054 LLVMMetadataRef LLVMDIBuilderCreatePointerType( 1055 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy, 1056 uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace, 1057 const char *Name, size_t NameLen) { 1058 return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy), 1059 SizeInBits, AlignInBits, 1060 AddressSpace, {Name, NameLen})); 1061 } 1062 1063 LLVMMetadataRef LLVMDIBuilderCreateStructType( 1064 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1065 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 1066 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags, 1067 LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements, 1068 unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder, 1069 const char *UniqueId, size_t UniqueIdLen) { 1070 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 1071 NumElements}); 1072 return wrap(unwrap(Builder)->createStructType( 1073 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 1074 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags), 1075 unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang, 1076 unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen})); 1077 } 1078 1079 LLVMMetadataRef LLVMDIBuilderCreateMemberType( 1080 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1081 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits, 1082 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags, 1083 LLVMMetadataRef Ty) { 1084 return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope), 1085 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits, 1086 OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty))); 1087 } 1088 1089 LLVMMetadataRef 1090 LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name, 1091 size_t NameLen) { 1092 return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen})); 1093 } 1094 1095 LLVMMetadataRef 1096 LLVMDIBuilderCreateStaticMemberType( 1097 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1098 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 1099 LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal, 1100 uint32_t AlignInBits) { 1101 return wrap(unwrap(Builder)->createStaticMemberType( 1102 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1103 unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type), 1104 map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal), 1105 AlignInBits)); 1106 } 1107 1108 LLVMMetadataRef 1109 LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder, 1110 const char *Name, size_t NameLen, 1111 LLVMMetadataRef File, unsigned LineNo, 1112 uint64_t SizeInBits, uint32_t AlignInBits, 1113 uint64_t OffsetInBits, LLVMDIFlags Flags, 1114 LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) { 1115 return wrap(unwrap(Builder)->createObjCIVar( 1116 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, 1117 SizeInBits, AlignInBits, OffsetInBits, 1118 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty), 1119 unwrapDI<MDNode>(PropertyNode))); 1120 } 1121 1122 LLVMMetadataRef 1123 LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder, 1124 const char *Name, size_t NameLen, 1125 LLVMMetadataRef File, unsigned LineNo, 1126 const char *GetterName, size_t GetterNameLen, 1127 const char *SetterName, size_t SetterNameLen, 1128 unsigned PropertyAttributes, 1129 LLVMMetadataRef Ty) { 1130 return wrap(unwrap(Builder)->createObjCProperty( 1131 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, 1132 {GetterName, GetterNameLen}, {SetterName, SetterNameLen}, 1133 PropertyAttributes, unwrapDI<DIType>(Ty))); 1134 } 1135 1136 LLVMMetadataRef 1137 LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder, 1138 LLVMMetadataRef Type) { 1139 return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type))); 1140 } 1141 1142 LLVMMetadataRef 1143 LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type, 1144 const char *Name, size_t NameLen, 1145 LLVMMetadataRef File, unsigned LineNo, 1146 LLVMMetadataRef Scope, uint32_t AlignInBits) { 1147 return wrap(unwrap(Builder)->createTypedef( 1148 unwrapDI<DIType>(Type), {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, 1149 unwrapDI<DIScope>(Scope), AlignInBits)); 1150 } 1151 1152 LLVMMetadataRef 1153 LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder, 1154 LLVMMetadataRef Ty, LLVMMetadataRef BaseTy, 1155 uint64_t BaseOffset, uint32_t VBPtrOffset, 1156 LLVMDIFlags Flags) { 1157 return wrap(unwrap(Builder)->createInheritance( 1158 unwrapDI<DIType>(Ty), unwrapDI<DIType>(BaseTy), 1159 BaseOffset, VBPtrOffset, map_from_llvmDIFlags(Flags))); 1160 } 1161 1162 LLVMMetadataRef 1163 LLVMDIBuilderCreateForwardDecl( 1164 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, 1165 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, 1166 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, 1167 const char *UniqueIdentifier, size_t UniqueIdentifierLen) { 1168 return wrap(unwrap(Builder)->createForwardDecl( 1169 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope), 1170 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits, 1171 AlignInBits, {UniqueIdentifier, UniqueIdentifierLen})); 1172 } 1173 1174 LLVMMetadataRef 1175 LLVMDIBuilderCreateReplaceableCompositeType( 1176 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, 1177 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, 1178 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, 1179 LLVMDIFlags Flags, const char *UniqueIdentifier, 1180 size_t UniqueIdentifierLen) { 1181 return wrap(unwrap(Builder)->createReplaceableCompositeType( 1182 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope), 1183 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits, 1184 AlignInBits, map_from_llvmDIFlags(Flags), 1185 {UniqueIdentifier, UniqueIdentifierLen})); 1186 } 1187 1188 LLVMMetadataRef 1189 LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag, 1190 LLVMMetadataRef Type) { 1191 return wrap(unwrap(Builder)->createQualifiedType(Tag, 1192 unwrapDI<DIType>(Type))); 1193 } 1194 1195 LLVMMetadataRef 1196 LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag, 1197 LLVMMetadataRef Type) { 1198 return wrap(unwrap(Builder)->createReferenceType(Tag, 1199 unwrapDI<DIType>(Type))); 1200 } 1201 1202 LLVMMetadataRef 1203 LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) { 1204 return wrap(unwrap(Builder)->createNullPtrType()); 1205 } 1206 1207 LLVMMetadataRef 1208 LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder, 1209 LLVMMetadataRef PointeeType, 1210 LLVMMetadataRef ClassType, 1211 uint64_t SizeInBits, 1212 uint32_t AlignInBits, 1213 LLVMDIFlags Flags) { 1214 return wrap(unwrap(Builder)->createMemberPointerType( 1215 unwrapDI<DIType>(PointeeType), 1216 unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits, 1217 map_from_llvmDIFlags(Flags))); 1218 } 1219 1220 LLVMMetadataRef 1221 LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder, 1222 LLVMMetadataRef Scope, 1223 const char *Name, size_t NameLen, 1224 LLVMMetadataRef File, unsigned LineNumber, 1225 uint64_t SizeInBits, 1226 uint64_t OffsetInBits, 1227 uint64_t StorageOffsetInBits, 1228 LLVMDIFlags Flags, LLVMMetadataRef Type) { 1229 return wrap(unwrap(Builder)->createBitFieldMemberType( 1230 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1231 unwrapDI<DIFile>(File), LineNumber, 1232 SizeInBits, OffsetInBits, StorageOffsetInBits, 1233 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type))); 1234 } 1235 1236 LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder, 1237 LLVMMetadataRef Scope, const char *Name, size_t NameLen, 1238 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits, 1239 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags, 1240 LLVMMetadataRef DerivedFrom, 1241 LLVMMetadataRef *Elements, unsigned NumElements, 1242 LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode, 1243 const char *UniqueIdentifier, size_t UniqueIdentifierLen) { 1244 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 1245 NumElements}); 1246 return wrap(unwrap(Builder)->createClassType( 1247 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1248 unwrapDI<DIFile>(File), LineNumber, 1249 SizeInBits, AlignInBits, OffsetInBits, 1250 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom), 1251 Elts, unwrapDI<DIType>(VTableHolder), 1252 unwrapDI<MDNode>(TemplateParamsNode), 1253 {UniqueIdentifier, UniqueIdentifierLen})); 1254 } 1255 1256 LLVMMetadataRef 1257 LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder, 1258 LLVMMetadataRef Type) { 1259 return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type))); 1260 } 1261 1262 const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) { 1263 StringRef Str = unwrap<DIType>(DType)->getName(); 1264 *Length = Str.size(); 1265 return Str.data(); 1266 } 1267 1268 uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) { 1269 return unwrapDI<DIType>(DType)->getSizeInBits(); 1270 } 1271 1272 uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) { 1273 return unwrapDI<DIType>(DType)->getOffsetInBits(); 1274 } 1275 1276 uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) { 1277 return unwrapDI<DIType>(DType)->getAlignInBits(); 1278 } 1279 1280 unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) { 1281 return unwrapDI<DIType>(DType)->getLine(); 1282 } 1283 1284 LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) { 1285 return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags()); 1286 } 1287 1288 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder, 1289 LLVMMetadataRef *Types, 1290 size_t Length) { 1291 return wrap( 1292 unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get()); 1293 } 1294 1295 LLVMMetadataRef 1296 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder, 1297 LLVMMetadataRef File, 1298 LLVMMetadataRef *ParameterTypes, 1299 unsigned NumParameterTypes, 1300 LLVMDIFlags Flags) { 1301 auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes), 1302 NumParameterTypes}); 1303 return wrap(unwrap(Builder)->createSubroutineType( 1304 Elts, map_from_llvmDIFlags(Flags))); 1305 } 1306 1307 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder, 1308 int64_t *Addr, size_t Length) { 1309 return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr, 1310 Length))); 1311 } 1312 1313 LLVMMetadataRef 1314 LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder, 1315 int64_t Value) { 1316 return wrap(unwrap(Builder)->createConstantValueExpression(Value)); 1317 } 1318 1319 LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression( 1320 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1321 size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File, 1322 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit, 1323 LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) { 1324 return wrap(unwrap(Builder)->createGlobalVariableExpression( 1325 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen}, 1326 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit, 1327 true, unwrap<DIExpression>(Expr), unwrapDI<MDNode>(Decl), 1328 nullptr, AlignInBits)); 1329 } 1330 1331 LLVMMetadataRef LLVMDIGlobalVariableExpressionGetVariable(LLVMMetadataRef GVE) { 1332 return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getVariable()); 1333 } 1334 1335 LLVMMetadataRef LLVMDIGlobalVariableExpressionGetExpression( 1336 LLVMMetadataRef GVE) { 1337 return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getExpression()); 1338 } 1339 1340 LLVMMetadataRef LLVMDIVariableGetFile(LLVMMetadataRef Var) { 1341 return wrap(unwrapDI<DIVariable>(Var)->getFile()); 1342 } 1343 1344 LLVMMetadataRef LLVMDIVariableGetScope(LLVMMetadataRef Var) { 1345 return wrap(unwrapDI<DIVariable>(Var)->getScope()); 1346 } 1347 1348 unsigned LLVMDIVariableGetLine(LLVMMetadataRef Var) { 1349 return unwrapDI<DIVariable>(Var)->getLine(); 1350 } 1351 1352 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data, 1353 size_t Count) { 1354 return wrap( 1355 MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release()); 1356 } 1357 1358 void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) { 1359 MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode)); 1360 } 1361 1362 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata, 1363 LLVMMetadataRef Replacement) { 1364 auto *Node = unwrapDI<MDNode>(TargetMetadata); 1365 Node->replaceAllUsesWith(unwrap<Metadata>(Replacement)); 1366 MDNode::deleteTemporary(Node); 1367 } 1368 1369 LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl( 1370 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1371 size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File, 1372 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit, 1373 LLVMMetadataRef Decl, uint32_t AlignInBits) { 1374 return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl( 1375 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen}, 1376 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit, 1377 unwrapDI<MDNode>(Decl), nullptr, AlignInBits)); 1378 } 1379 1380 LLVMValueRef 1381 LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder, LLVMValueRef Storage, 1382 LLVMMetadataRef VarInfo, LLVMMetadataRef Expr, 1383 LLVMMetadataRef DL, LLVMValueRef Instr) { 1384 return wrap(unwrap(Builder)->insertDeclare( 1385 unwrap(Storage), unwrap<DILocalVariable>(VarInfo), 1386 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL), 1387 unwrap<Instruction>(Instr))); 1388 } 1389 1390 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd( 1391 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo, 1392 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) { 1393 return wrap(unwrap(Builder)->insertDeclare( 1394 unwrap(Storage), unwrap<DILocalVariable>(VarInfo), 1395 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL), 1396 unwrap(Block))); 1397 } 1398 1399 LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder, 1400 LLVMValueRef Val, 1401 LLVMMetadataRef VarInfo, 1402 LLVMMetadataRef Expr, 1403 LLVMMetadataRef DebugLoc, 1404 LLVMValueRef Instr) { 1405 return wrap(unwrap(Builder)->insertDbgValueIntrinsic( 1406 unwrap(Val), unwrap<DILocalVariable>(VarInfo), 1407 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc), 1408 unwrap<Instruction>(Instr))); 1409 } 1410 1411 LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder, 1412 LLVMValueRef Val, 1413 LLVMMetadataRef VarInfo, 1414 LLVMMetadataRef Expr, 1415 LLVMMetadataRef DebugLoc, 1416 LLVMBasicBlockRef Block) { 1417 return wrap(unwrap(Builder)->insertDbgValueIntrinsic( 1418 unwrap(Val), unwrap<DILocalVariable>(VarInfo), 1419 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc), 1420 unwrap(Block))); 1421 } 1422 1423 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable( 1424 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1425 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty, 1426 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) { 1427 return wrap(unwrap(Builder)->createAutoVariable( 1428 unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File), 1429 LineNo, unwrap<DIType>(Ty), AlwaysPreserve, 1430 map_from_llvmDIFlags(Flags), AlignInBits)); 1431 } 1432 1433 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable( 1434 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1435 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo, 1436 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) { 1437 return wrap(unwrap(Builder)->createParameterVariable( 1438 unwrap<DIScope>(Scope), {Name, NameLen}, ArgNo, unwrap<DIFile>(File), 1439 LineNo, unwrap<DIType>(Ty), AlwaysPreserve, 1440 map_from_llvmDIFlags(Flags))); 1441 } 1442 1443 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder, 1444 int64_t Lo, int64_t Count) { 1445 return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count)); 1446 } 1447 1448 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder, 1449 LLVMMetadataRef *Data, 1450 size_t Length) { 1451 Metadata **DataValue = unwrap(Data); 1452 return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get()); 1453 } 1454 1455 LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) { 1456 return wrap(unwrap<Function>(Func)->getSubprogram()); 1457 } 1458 1459 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) { 1460 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP)); 1461 } 1462 1463 unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram) { 1464 return unwrapDI<DISubprogram>(Subprogram)->getLine(); 1465 } 1466 1467 LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst) { 1468 return wrap(unwrap<Instruction>(Inst)->getDebugLoc().getAsMDNode()); 1469 } 1470 1471 void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc) { 1472 if (Loc) 1473 unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc(unwrap<MDNode>(Loc))); 1474 else 1475 unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc()); 1476 } 1477 1478 LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) { 1479 switch(unwrap(Metadata)->getMetadataID()) { 1480 #define HANDLE_METADATA_LEAF(CLASS) \ 1481 case Metadata::CLASS##Kind: \ 1482 return (LLVMMetadataKind)LLVM##CLASS##MetadataKind; 1483 #include "llvm/IR/Metadata.def" 1484 default: 1485 return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind; 1486 } 1487 } 1488