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 } 342 343 auto *TermInst = BB.getTerminator(); 344 if (!TermInst) 345 // This is invalid IR, but we may not have run the verifier yet 346 continue; 347 if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) { 348 auto *NewLoopID = LoopIDsMap.lookup(LoopID); 349 if (!NewLoopID) 350 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID); 351 if (NewLoopID != LoopID) 352 TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID); 353 } 354 } 355 return Changed; 356 } 357 358 bool llvm::StripDebugInfo(Module &M) { 359 bool Changed = false; 360 361 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), 362 NME = M.named_metadata_end(); NMI != NME;) { 363 NamedMDNode *NMD = &*NMI; 364 ++NMI; 365 366 // We're stripping debug info, and without them, coverage information 367 // doesn't quite make sense. 368 if (NMD->getName().startswith("llvm.dbg.") || 369 NMD->getName() == "llvm.gcov") { 370 NMD->eraseFromParent(); 371 Changed = true; 372 } 373 } 374 375 for (Function &F : M) 376 Changed |= stripDebugInfo(F); 377 378 for (auto &GV : M.globals()) { 379 Changed |= GV.eraseMetadata(LLVMContext::MD_dbg); 380 } 381 382 if (GVMaterializer *Materializer = M.getMaterializer()) 383 Materializer->setStripDebugInfo(); 384 385 return Changed; 386 } 387 388 namespace { 389 390 /// Helper class to downgrade -g metadata to -gline-tables-only metadata. 391 class DebugTypeInfoRemoval { 392 DenseMap<Metadata *, Metadata *> Replacements; 393 394 public: 395 /// The (void)() type. 396 MDNode *EmptySubroutineType; 397 398 private: 399 /// Remember what linkage name we originally had before stripping. If we end 400 /// up making two subprograms identical who originally had different linkage 401 /// names, then we need to make one of them distinct, to avoid them getting 402 /// uniqued. Maps the new node to the old linkage name. 403 DenseMap<DISubprogram *, StringRef> NewToLinkageName; 404 405 // TODO: Remember the distinct subprogram we created for a given linkage name, 406 // so that we can continue to unique whenever possible. Map <newly created 407 // node, old linkage name> to the first (possibly distinct) mdsubprogram 408 // created for that combination. This is not strictly needed for correctness, 409 // but can cut down on the number of MDNodes and let us diff cleanly with the 410 // output of -gline-tables-only. 411 412 public: 413 DebugTypeInfoRemoval(LLVMContext &C) 414 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0, 415 MDNode::get(C, {}))) {} 416 417 Metadata *map(Metadata *M) { 418 if (!M) 419 return nullptr; 420 auto Replacement = Replacements.find(M); 421 if (Replacement != Replacements.end()) 422 return Replacement->second; 423 424 return M; 425 } 426 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); } 427 428 /// Recursively remap N and all its referenced children. Does a DF post-order 429 /// traversal, so as to remap bottoms up. 430 void traverseAndRemap(MDNode *N) { traverse(N); } 431 432 private: 433 // Create a new DISubprogram, to replace the one given. 434 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) { 435 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile())); 436 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : ""; 437 DISubprogram *Declaration = nullptr; 438 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType())); 439 DIType *ContainingType = 440 cast_or_null<DIType>(map(MDS->getContainingType())); 441 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit())); 442 auto Variables = nullptr; 443 auto TemplateParams = nullptr; 444 445 // Make a distinct DISubprogram, for situations that warrent it. 446 auto distinctMDSubprogram = [&]() { 447 return DISubprogram::getDistinct( 448 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, 449 FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(), 450 ContainingType, MDS->getVirtualIndex(), MDS->getThisAdjustment(), 451 MDS->getFlags(), MDS->getSPFlags(), Unit, TemplateParams, Declaration, 452 Variables); 453 }; 454 455 if (MDS->isDistinct()) 456 return distinctMDSubprogram(); 457 458 auto *NewMDS = DISubprogram::get( 459 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, 460 FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(), ContainingType, 461 MDS->getVirtualIndex(), MDS->getThisAdjustment(), MDS->getFlags(), 462 MDS->getSPFlags(), Unit, TemplateParams, Declaration, Variables); 463 464 StringRef OldLinkageName = MDS->getLinkageName(); 465 466 // See if we need to make a distinct one. 467 auto OrigLinkage = NewToLinkageName.find(NewMDS); 468 if (OrigLinkage != NewToLinkageName.end()) { 469 if (OrigLinkage->second == OldLinkageName) 470 // We're good. 471 return NewMDS; 472 473 // Otherwise, need to make a distinct one. 474 // TODO: Query the map to see if we already have one. 475 return distinctMDSubprogram(); 476 } 477 478 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()}); 479 return NewMDS; 480 } 481 482 /// Create a new compile unit, to replace the one given 483 DICompileUnit *getReplacementCU(DICompileUnit *CU) { 484 // Drop skeleton CUs. 485 if (CU->getDWOId()) 486 return nullptr; 487 488 auto *File = cast_or_null<DIFile>(map(CU->getFile())); 489 MDTuple *EnumTypes = nullptr; 490 MDTuple *RetainedTypes = nullptr; 491 MDTuple *GlobalVariables = nullptr; 492 MDTuple *ImportedEntities = nullptr; 493 return DICompileUnit::getDistinct( 494 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(), 495 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(), 496 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes, 497 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(), 498 CU->getDWOId(), CU->getSplitDebugInlining(), 499 CU->getDebugInfoForProfiling(), CU->getNameTableKind(), 500 CU->getRangesBaseAddress(), CU->getSysRoot(), CU->getSDK()); 501 } 502 503 DILocation *getReplacementMDLocation(DILocation *MLD) { 504 auto *Scope = map(MLD->getScope()); 505 auto *InlinedAt = map(MLD->getInlinedAt()); 506 if (MLD->isDistinct()) 507 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(), 508 MLD->getColumn(), Scope, InlinedAt); 509 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(), 510 Scope, InlinedAt); 511 } 512 513 /// Create a new generic MDNode, to replace the one given 514 MDNode *getReplacementMDNode(MDNode *N) { 515 SmallVector<Metadata *, 8> Ops; 516 Ops.reserve(N->getNumOperands()); 517 for (auto &I : N->operands()) 518 if (I) 519 Ops.push_back(map(I)); 520 auto *Ret = MDNode::get(N->getContext(), Ops); 521 return Ret; 522 } 523 524 /// Attempt to re-map N to a newly created node. 525 void remap(MDNode *N) { 526 if (Replacements.count(N)) 527 return; 528 529 auto doRemap = [&](MDNode *N) -> MDNode * { 530 if (!N) 531 return nullptr; 532 if (auto *MDSub = dyn_cast<DISubprogram>(N)) { 533 remap(MDSub->getUnit()); 534 return getReplacementSubprogram(MDSub); 535 } 536 if (isa<DISubroutineType>(N)) 537 return EmptySubroutineType; 538 if (auto *CU = dyn_cast<DICompileUnit>(N)) 539 return getReplacementCU(CU); 540 if (isa<DIFile>(N)) 541 return N; 542 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N)) 543 // Remap to our referenced scope (recursively). 544 return mapNode(MDLB->getScope()); 545 if (auto *MLD = dyn_cast<DILocation>(N)) 546 return getReplacementMDLocation(MLD); 547 548 // Otherwise, if we see these, just drop them now. Not strictly necessary, 549 // but this speeds things up a little. 550 if (isa<DINode>(N)) 551 return nullptr; 552 553 return getReplacementMDNode(N); 554 }; 555 Replacements[N] = doRemap(N); 556 } 557 558 /// Do the remapping traversal. 559 void traverse(MDNode *); 560 }; 561 562 } // end anonymous namespace 563 564 void DebugTypeInfoRemoval::traverse(MDNode *N) { 565 if (!N || Replacements.count(N)) 566 return; 567 568 // To avoid cycles, as well as for efficiency sake, we will sometimes prune 569 // parts of the graph. 570 auto prune = [](MDNode *Parent, MDNode *Child) { 571 if (auto *MDS = dyn_cast<DISubprogram>(Parent)) 572 return Child == MDS->getRetainedNodes().get(); 573 return false; 574 }; 575 576 SmallVector<MDNode *, 16> ToVisit; 577 DenseSet<MDNode *> Opened; 578 579 // Visit each node starting at N in post order, and map them. 580 ToVisit.push_back(N); 581 while (!ToVisit.empty()) { 582 auto *N = ToVisit.back(); 583 if (!Opened.insert(N).second) { 584 // Close it. 585 remap(N); 586 ToVisit.pop_back(); 587 continue; 588 } 589 for (auto &I : N->operands()) 590 if (auto *MDN = dyn_cast_or_null<MDNode>(I)) 591 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) && 592 !isa<DICompileUnit>(MDN)) 593 ToVisit.push_back(MDN); 594 } 595 } 596 597 bool llvm::stripNonLineTableDebugInfo(Module &M) { 598 bool Changed = false; 599 600 // First off, delete the debug intrinsics. 601 auto RemoveUses = [&](StringRef Name) { 602 if (auto *DbgVal = M.getFunction(Name)) { 603 while (!DbgVal->use_empty()) 604 cast<Instruction>(DbgVal->user_back())->eraseFromParent(); 605 DbgVal->eraseFromParent(); 606 Changed = true; 607 } 608 }; 609 RemoveUses("llvm.dbg.declare"); 610 RemoveUses("llvm.dbg.value"); 611 612 // Delete non-CU debug info named metadata nodes. 613 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end(); 614 NMI != NME;) { 615 NamedMDNode *NMD = &*NMI; 616 ++NMI; 617 // Specifically keep dbg.cu around. 618 if (NMD->getName() == "llvm.dbg.cu") 619 continue; 620 } 621 622 // Drop all dbg attachments from global variables. 623 for (auto &GV : M.globals()) 624 GV.eraseMetadata(LLVMContext::MD_dbg); 625 626 DebugTypeInfoRemoval Mapper(M.getContext()); 627 auto remap = [&](MDNode *Node) -> MDNode * { 628 if (!Node) 629 return nullptr; 630 Mapper.traverseAndRemap(Node); 631 auto *NewNode = Mapper.mapNode(Node); 632 Changed |= Node != NewNode; 633 Node = NewNode; 634 return NewNode; 635 }; 636 637 // Rewrite the DebugLocs to be equivalent to what 638 // -gline-tables-only would have created. 639 for (auto &F : M) { 640 if (auto *SP = F.getSubprogram()) { 641 Mapper.traverseAndRemap(SP); 642 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP)); 643 Changed |= SP != NewSP; 644 F.setSubprogram(NewSP); 645 } 646 for (auto &BB : F) { 647 for (auto &I : BB) { 648 auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc { 649 auto *Scope = DL.getScope(); 650 MDNode *InlinedAt = DL.getInlinedAt(); 651 Scope = remap(Scope); 652 InlinedAt = remap(InlinedAt); 653 return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt); 654 }; 655 656 if (I.getDebugLoc() != DebugLoc()) 657 I.setDebugLoc(remapDebugLoc(I.getDebugLoc())); 658 659 // Remap DILocations in untyped MDNodes (e.g., llvm.loop). 660 SmallVector<std::pair<unsigned, MDNode *>, 2> MDs; 661 I.getAllMetadata(MDs); 662 for (auto Attachment : MDs) 663 if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second)) 664 for (unsigned N = 0; N < T->getNumOperands(); ++N) 665 if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N))) 666 if (Loc != DebugLoc()) 667 T->replaceOperandWith(N, remapDebugLoc(Loc)); 668 } 669 } 670 } 671 672 // Create a new llvm.dbg.cu, which is equivalent to the one 673 // -gline-tables-only would have created. 674 for (auto &NMD : M.getNamedMDList()) { 675 SmallVector<MDNode *, 8> Ops; 676 for (MDNode *Op : NMD.operands()) 677 Ops.push_back(remap(Op)); 678 679 if (!Changed) 680 continue; 681 682 NMD.clearOperands(); 683 for (auto *Op : Ops) 684 if (Op) 685 NMD.addOperand(Op); 686 } 687 return Changed; 688 } 689 690 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { 691 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( 692 M.getModuleFlag("Debug Info Version"))) 693 return Val->getZExtValue(); 694 return 0; 695 } 696 697 void Instruction::applyMergedLocation(const DILocation *LocA, 698 const DILocation *LocB) { 699 setDebugLoc(DILocation::getMergedLocation(LocA, LocB)); 700 } 701 702 //===----------------------------------------------------------------------===// 703 // LLVM C API implementations. 704 //===----------------------------------------------------------------------===// 705 706 static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) { 707 switch (lang) { 708 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \ 709 case LLVMDWARFSourceLanguage##NAME: \ 710 return ID; 711 #include "llvm/BinaryFormat/Dwarf.def" 712 #undef HANDLE_DW_LANG 713 } 714 llvm_unreachable("Unhandled Tag"); 715 } 716 717 template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) { 718 return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr); 719 } 720 721 static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) { 722 return static_cast<DINode::DIFlags>(Flags); 723 } 724 725 static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) { 726 return static_cast<LLVMDIFlags>(Flags); 727 } 728 729 static DISubprogram::DISPFlags 730 pack_into_DISPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized) { 731 return DISubprogram::toSPFlags(IsLocalToUnit, IsDefinition, IsOptimized); 732 } 733 734 unsigned LLVMDebugMetadataVersion() { 735 return DEBUG_METADATA_VERSION; 736 } 737 738 LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) { 739 return wrap(new DIBuilder(*unwrap(M), false)); 740 } 741 742 LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) { 743 return wrap(new DIBuilder(*unwrap(M))); 744 } 745 746 unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) { 747 return getDebugMetadataVersionFromModule(*unwrap(M)); 748 } 749 750 LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) { 751 return StripDebugInfo(*unwrap(M)); 752 } 753 754 void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) { 755 delete unwrap(Builder); 756 } 757 758 void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) { 759 unwrap(Builder)->finalize(); 760 } 761 762 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit( 763 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang, 764 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen, 765 LLVMBool isOptimized, const char *Flags, size_t FlagsLen, 766 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen, 767 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining, 768 LLVMBool DebugInfoForProfiling, const char *SysRoot, size_t SysRootLen, 769 const char *SDK, size_t SDKLen) { 770 auto File = unwrapDI<DIFile>(FileRef); 771 772 return wrap(unwrap(Builder)->createCompileUnit( 773 map_from_llvmDWARFsourcelanguage(Lang), File, 774 StringRef(Producer, ProducerLen), isOptimized, StringRef(Flags, FlagsLen), 775 RuntimeVer, StringRef(SplitName, SplitNameLen), 776 static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId, 777 SplitDebugInlining, DebugInfoForProfiling, 778 DICompileUnit::DebugNameTableKind::Default, false, 779 StringRef(SysRoot, SysRootLen), StringRef(SDK, SDKLen))); 780 } 781 782 LLVMMetadataRef 783 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename, 784 size_t FilenameLen, const char *Directory, 785 size_t DirectoryLen) { 786 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen), 787 StringRef(Directory, DirectoryLen))); 788 } 789 790 LLVMMetadataRef 791 LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope, 792 const char *Name, size_t NameLen, 793 const char *ConfigMacros, size_t ConfigMacrosLen, 794 const char *IncludePath, size_t IncludePathLen) { 795 return wrap(unwrap(Builder)->createModule( 796 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), 797 StringRef(ConfigMacros, ConfigMacrosLen), 798 StringRef(IncludePath, IncludePathLen))); 799 } 800 801 LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder, 802 LLVMMetadataRef ParentScope, 803 const char *Name, size_t NameLen, 804 LLVMBool ExportSymbols) { 805 return wrap(unwrap(Builder)->createNameSpace( 806 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols)); 807 } 808 809 LLVMMetadataRef LLVMDIBuilderCreateFunction( 810 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 811 size_t NameLen, const char *LinkageName, size_t LinkageNameLen, 812 LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty, 813 LLVMBool IsLocalToUnit, LLVMBool IsDefinition, 814 unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) { 815 return wrap(unwrap(Builder)->createFunction( 816 unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen}, 817 unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty), ScopeLine, 818 map_from_llvmDIFlags(Flags), 819 pack_into_DISPFlags(IsLocalToUnit, IsDefinition, IsOptimized), nullptr, 820 nullptr, nullptr)); 821 } 822 823 824 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock( 825 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, 826 LLVMMetadataRef File, unsigned Line, unsigned Col) { 827 return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope), 828 unwrapDI<DIFile>(File), 829 Line, Col)); 830 } 831 832 LLVMMetadataRef 833 LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder, 834 LLVMMetadataRef Scope, 835 LLVMMetadataRef File, 836 unsigned Discriminator) { 837 return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope), 838 unwrapDI<DIFile>(File), 839 Discriminator)); 840 } 841 842 LLVMMetadataRef 843 LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder, 844 LLVMMetadataRef Scope, 845 LLVMMetadataRef NS, 846 LLVMMetadataRef File, 847 unsigned Line) { 848 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope), 849 unwrapDI<DINamespace>(NS), 850 unwrapDI<DIFile>(File), 851 Line)); 852 } 853 854 LLVMMetadataRef 855 LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder, 856 LLVMMetadataRef Scope, 857 LLVMMetadataRef ImportedEntity, 858 LLVMMetadataRef File, 859 unsigned Line) { 860 return wrap(unwrap(Builder)->createImportedModule( 861 unwrapDI<DIScope>(Scope), 862 unwrapDI<DIImportedEntity>(ImportedEntity), 863 unwrapDI<DIFile>(File), Line)); 864 } 865 866 LLVMMetadataRef 867 LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder, 868 LLVMMetadataRef Scope, 869 LLVMMetadataRef M, 870 LLVMMetadataRef File, 871 unsigned Line) { 872 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope), 873 unwrapDI<DIModule>(M), 874 unwrapDI<DIFile>(File), 875 Line)); 876 } 877 878 LLVMMetadataRef 879 LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder, 880 LLVMMetadataRef Scope, 881 LLVMMetadataRef Decl, 882 LLVMMetadataRef File, 883 unsigned Line, 884 const char *Name, size_t NameLen) { 885 return wrap(unwrap(Builder)->createImportedDeclaration( 886 unwrapDI<DIScope>(Scope), 887 unwrapDI<DINode>(Decl), 888 unwrapDI<DIFile>(File), Line, {Name, NameLen})); 889 } 890 891 LLVMMetadataRef 892 LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line, 893 unsigned Column, LLVMMetadataRef Scope, 894 LLVMMetadataRef InlinedAt) { 895 return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope), 896 unwrap(InlinedAt))); 897 } 898 899 unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) { 900 return unwrapDI<DILocation>(Location)->getLine(); 901 } 902 903 unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) { 904 return unwrapDI<DILocation>(Location)->getColumn(); 905 } 906 907 LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) { 908 return wrap(unwrapDI<DILocation>(Location)->getScope()); 909 } 910 911 LLVMMetadataRef LLVMDILocationGetInlinedAt(LLVMMetadataRef Location) { 912 return wrap(unwrapDI<DILocation>(Location)->getInlinedAt()); 913 } 914 915 LLVMMetadataRef LLVMDIScopeGetFile(LLVMMetadataRef Scope) { 916 return wrap(unwrapDI<DIScope>(Scope)->getFile()); 917 } 918 919 const char *LLVMDIFileGetDirectory(LLVMMetadataRef File, unsigned *Len) { 920 auto Dir = unwrapDI<DIFile>(File)->getDirectory(); 921 *Len = Dir.size(); 922 return Dir.data(); 923 } 924 925 const char *LLVMDIFileGetFilename(LLVMMetadataRef File, unsigned *Len) { 926 auto Name = unwrapDI<DIFile>(File)->getFilename(); 927 *Len = Name.size(); 928 return Name.data(); 929 } 930 931 const char *LLVMDIFileGetSource(LLVMMetadataRef File, unsigned *Len) { 932 if (auto Src = unwrapDI<DIFile>(File)->getSource()) { 933 *Len = Src->size(); 934 return Src->data(); 935 } 936 *Len = 0; 937 return ""; 938 } 939 940 LLVMMetadataRef LLVMDIBuilderCreateMacro(LLVMDIBuilderRef Builder, 941 LLVMMetadataRef ParentMacroFile, 942 unsigned Line, 943 LLVMDWARFMacinfoRecordType RecordType, 944 const char *Name, size_t NameLen, 945 const char *Value, size_t ValueLen) { 946 return wrap( 947 unwrap(Builder)->createMacro(unwrapDI<DIMacroFile>(ParentMacroFile), Line, 948 static_cast<MacinfoRecordType>(RecordType), 949 {Name, NameLen}, {Value, ValueLen})); 950 } 951 952 LLVMMetadataRef 953 LLVMDIBuilderCreateTempMacroFile(LLVMDIBuilderRef Builder, 954 LLVMMetadataRef ParentMacroFile, unsigned Line, 955 LLVMMetadataRef File) { 956 return wrap(unwrap(Builder)->createTempMacroFile( 957 unwrapDI<DIMacroFile>(ParentMacroFile), Line, unwrapDI<DIFile>(File))); 958 } 959 960 LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder, 961 const char *Name, size_t NameLen, 962 int64_t Value, 963 LLVMBool IsUnsigned) { 964 return wrap(unwrap(Builder)->createEnumerator({Name, NameLen}, Value, 965 IsUnsigned != 0)); 966 } 967 968 LLVMMetadataRef LLVMDIBuilderCreateEnumerationType( 969 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 970 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 971 uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements, 972 unsigned NumElements, LLVMMetadataRef ClassTy) { 973 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 974 NumElements}); 975 return wrap(unwrap(Builder)->createEnumerationType( 976 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 977 LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy))); 978 } 979 980 LLVMMetadataRef LLVMDIBuilderCreateUnionType( 981 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 982 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 983 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags, 984 LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang, 985 const char *UniqueId, size_t UniqueIdLen) { 986 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 987 NumElements}); 988 return wrap(unwrap(Builder)->createUnionType( 989 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 990 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags), 991 Elts, RunTimeLang, {UniqueId, UniqueIdLen})); 992 } 993 994 995 LLVMMetadataRef 996 LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size, 997 uint32_t AlignInBits, LLVMMetadataRef Ty, 998 LLVMMetadataRef *Subscripts, 999 unsigned NumSubscripts) { 1000 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts), 1001 NumSubscripts}); 1002 return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits, 1003 unwrapDI<DIType>(Ty), Subs)); 1004 } 1005 1006 LLVMMetadataRef 1007 LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size, 1008 uint32_t AlignInBits, LLVMMetadataRef Ty, 1009 LLVMMetadataRef *Subscripts, 1010 unsigned NumSubscripts) { 1011 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts), 1012 NumSubscripts}); 1013 return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits, 1014 unwrapDI<DIType>(Ty), Subs)); 1015 } 1016 1017 LLVMMetadataRef 1018 LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name, 1019 size_t NameLen, uint64_t SizeInBits, 1020 LLVMDWARFTypeEncoding Encoding, 1021 LLVMDIFlags Flags) { 1022 return wrap(unwrap(Builder)->createBasicType({Name, NameLen}, 1023 SizeInBits, Encoding, 1024 map_from_llvmDIFlags(Flags))); 1025 } 1026 1027 LLVMMetadataRef LLVMDIBuilderCreatePointerType( 1028 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy, 1029 uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace, 1030 const char *Name, size_t NameLen) { 1031 return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy), 1032 SizeInBits, AlignInBits, 1033 AddressSpace, {Name, NameLen})); 1034 } 1035 1036 LLVMMetadataRef LLVMDIBuilderCreateStructType( 1037 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1038 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 1039 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags, 1040 LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements, 1041 unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder, 1042 const char *UniqueId, size_t UniqueIdLen) { 1043 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 1044 NumElements}); 1045 return wrap(unwrap(Builder)->createStructType( 1046 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), 1047 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags), 1048 unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang, 1049 unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen})); 1050 } 1051 1052 LLVMMetadataRef LLVMDIBuilderCreateMemberType( 1053 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1054 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits, 1055 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags, 1056 LLVMMetadataRef Ty) { 1057 return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope), 1058 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits, 1059 OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty))); 1060 } 1061 1062 LLVMMetadataRef 1063 LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name, 1064 size_t NameLen) { 1065 return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen})); 1066 } 1067 1068 LLVMMetadataRef 1069 LLVMDIBuilderCreateStaticMemberType( 1070 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1071 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, 1072 LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal, 1073 uint32_t AlignInBits) { 1074 return wrap(unwrap(Builder)->createStaticMemberType( 1075 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1076 unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type), 1077 map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal), 1078 AlignInBits)); 1079 } 1080 1081 LLVMMetadataRef 1082 LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder, 1083 const char *Name, size_t NameLen, 1084 LLVMMetadataRef File, unsigned LineNo, 1085 uint64_t SizeInBits, uint32_t AlignInBits, 1086 uint64_t OffsetInBits, LLVMDIFlags Flags, 1087 LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) { 1088 return wrap(unwrap(Builder)->createObjCIVar( 1089 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, 1090 SizeInBits, AlignInBits, OffsetInBits, 1091 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty), 1092 unwrapDI<MDNode>(PropertyNode))); 1093 } 1094 1095 LLVMMetadataRef 1096 LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder, 1097 const char *Name, size_t NameLen, 1098 LLVMMetadataRef File, unsigned LineNo, 1099 const char *GetterName, size_t GetterNameLen, 1100 const char *SetterName, size_t SetterNameLen, 1101 unsigned PropertyAttributes, 1102 LLVMMetadataRef Ty) { 1103 return wrap(unwrap(Builder)->createObjCProperty( 1104 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, 1105 {GetterName, GetterNameLen}, {SetterName, SetterNameLen}, 1106 PropertyAttributes, unwrapDI<DIType>(Ty))); 1107 } 1108 1109 LLVMMetadataRef 1110 LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder, 1111 LLVMMetadataRef Type) { 1112 return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type))); 1113 } 1114 1115 LLVMMetadataRef 1116 LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type, 1117 const char *Name, size_t NameLen, 1118 LLVMMetadataRef File, unsigned LineNo, 1119 LLVMMetadataRef Scope, uint32_t AlignInBits) { 1120 return wrap(unwrap(Builder)->createTypedef( 1121 unwrapDI<DIType>(Type), {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, 1122 unwrapDI<DIScope>(Scope), AlignInBits)); 1123 } 1124 1125 LLVMMetadataRef 1126 LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder, 1127 LLVMMetadataRef Ty, LLVMMetadataRef BaseTy, 1128 uint64_t BaseOffset, uint32_t VBPtrOffset, 1129 LLVMDIFlags Flags) { 1130 return wrap(unwrap(Builder)->createInheritance( 1131 unwrapDI<DIType>(Ty), unwrapDI<DIType>(BaseTy), 1132 BaseOffset, VBPtrOffset, map_from_llvmDIFlags(Flags))); 1133 } 1134 1135 LLVMMetadataRef 1136 LLVMDIBuilderCreateForwardDecl( 1137 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, 1138 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, 1139 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, 1140 const char *UniqueIdentifier, size_t UniqueIdentifierLen) { 1141 return wrap(unwrap(Builder)->createForwardDecl( 1142 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope), 1143 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits, 1144 AlignInBits, {UniqueIdentifier, UniqueIdentifierLen})); 1145 } 1146 1147 LLVMMetadataRef 1148 LLVMDIBuilderCreateReplaceableCompositeType( 1149 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, 1150 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, 1151 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits, 1152 LLVMDIFlags Flags, const char *UniqueIdentifier, 1153 size_t UniqueIdentifierLen) { 1154 return wrap(unwrap(Builder)->createReplaceableCompositeType( 1155 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope), 1156 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits, 1157 AlignInBits, map_from_llvmDIFlags(Flags), 1158 {UniqueIdentifier, UniqueIdentifierLen})); 1159 } 1160 1161 LLVMMetadataRef 1162 LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag, 1163 LLVMMetadataRef Type) { 1164 return wrap(unwrap(Builder)->createQualifiedType(Tag, 1165 unwrapDI<DIType>(Type))); 1166 } 1167 1168 LLVMMetadataRef 1169 LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag, 1170 LLVMMetadataRef Type) { 1171 return wrap(unwrap(Builder)->createReferenceType(Tag, 1172 unwrapDI<DIType>(Type))); 1173 } 1174 1175 LLVMMetadataRef 1176 LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) { 1177 return wrap(unwrap(Builder)->createNullPtrType()); 1178 } 1179 1180 LLVMMetadataRef 1181 LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder, 1182 LLVMMetadataRef PointeeType, 1183 LLVMMetadataRef ClassType, 1184 uint64_t SizeInBits, 1185 uint32_t AlignInBits, 1186 LLVMDIFlags Flags) { 1187 return wrap(unwrap(Builder)->createMemberPointerType( 1188 unwrapDI<DIType>(PointeeType), 1189 unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits, 1190 map_from_llvmDIFlags(Flags))); 1191 } 1192 1193 LLVMMetadataRef 1194 LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder, 1195 LLVMMetadataRef Scope, 1196 const char *Name, size_t NameLen, 1197 LLVMMetadataRef File, unsigned LineNumber, 1198 uint64_t SizeInBits, 1199 uint64_t OffsetInBits, 1200 uint64_t StorageOffsetInBits, 1201 LLVMDIFlags Flags, LLVMMetadataRef Type) { 1202 return wrap(unwrap(Builder)->createBitFieldMemberType( 1203 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1204 unwrapDI<DIFile>(File), LineNumber, 1205 SizeInBits, OffsetInBits, StorageOffsetInBits, 1206 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type))); 1207 } 1208 1209 LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder, 1210 LLVMMetadataRef Scope, const char *Name, size_t NameLen, 1211 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits, 1212 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags, 1213 LLVMMetadataRef DerivedFrom, 1214 LLVMMetadataRef *Elements, unsigned NumElements, 1215 LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode, 1216 const char *UniqueIdentifier, size_t UniqueIdentifierLen) { 1217 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), 1218 NumElements}); 1219 return wrap(unwrap(Builder)->createClassType( 1220 unwrapDI<DIScope>(Scope), {Name, NameLen}, 1221 unwrapDI<DIFile>(File), LineNumber, 1222 SizeInBits, AlignInBits, OffsetInBits, 1223 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom), 1224 Elts, unwrapDI<DIType>(VTableHolder), 1225 unwrapDI<MDNode>(TemplateParamsNode), 1226 {UniqueIdentifier, UniqueIdentifierLen})); 1227 } 1228 1229 LLVMMetadataRef 1230 LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder, 1231 LLVMMetadataRef Type) { 1232 return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type))); 1233 } 1234 1235 const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) { 1236 StringRef Str = unwrap<DIType>(DType)->getName(); 1237 *Length = Str.size(); 1238 return Str.data(); 1239 } 1240 1241 uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) { 1242 return unwrapDI<DIType>(DType)->getSizeInBits(); 1243 } 1244 1245 uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) { 1246 return unwrapDI<DIType>(DType)->getOffsetInBits(); 1247 } 1248 1249 uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) { 1250 return unwrapDI<DIType>(DType)->getAlignInBits(); 1251 } 1252 1253 unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) { 1254 return unwrapDI<DIType>(DType)->getLine(); 1255 } 1256 1257 LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) { 1258 return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags()); 1259 } 1260 1261 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder, 1262 LLVMMetadataRef *Types, 1263 size_t Length) { 1264 return wrap( 1265 unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get()); 1266 } 1267 1268 LLVMMetadataRef 1269 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder, 1270 LLVMMetadataRef File, 1271 LLVMMetadataRef *ParameterTypes, 1272 unsigned NumParameterTypes, 1273 LLVMDIFlags Flags) { 1274 auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes), 1275 NumParameterTypes}); 1276 return wrap(unwrap(Builder)->createSubroutineType( 1277 Elts, map_from_llvmDIFlags(Flags))); 1278 } 1279 1280 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder, 1281 int64_t *Addr, size_t Length) { 1282 return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr, 1283 Length))); 1284 } 1285 1286 LLVMMetadataRef 1287 LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder, 1288 int64_t Value) { 1289 return wrap(unwrap(Builder)->createConstantValueExpression(Value)); 1290 } 1291 1292 LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression( 1293 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1294 size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File, 1295 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit, 1296 LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) { 1297 return wrap(unwrap(Builder)->createGlobalVariableExpression( 1298 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen}, 1299 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit, 1300 true, unwrap<DIExpression>(Expr), unwrapDI<MDNode>(Decl), 1301 nullptr, AlignInBits)); 1302 } 1303 1304 LLVMMetadataRef LLVMDIGlobalVariableExpressionGetVariable(LLVMMetadataRef GVE) { 1305 return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getVariable()); 1306 } 1307 1308 LLVMMetadataRef LLVMDIGlobalVariableExpressionGetExpression( 1309 LLVMMetadataRef GVE) { 1310 return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getExpression()); 1311 } 1312 1313 LLVMMetadataRef LLVMDIVariableGetFile(LLVMMetadataRef Var) { 1314 return wrap(unwrapDI<DIVariable>(Var)->getFile()); 1315 } 1316 1317 LLVMMetadataRef LLVMDIVariableGetScope(LLVMMetadataRef Var) { 1318 return wrap(unwrapDI<DIVariable>(Var)->getScope()); 1319 } 1320 1321 unsigned LLVMDIVariableGetLine(LLVMMetadataRef Var) { 1322 return unwrapDI<DIVariable>(Var)->getLine(); 1323 } 1324 1325 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data, 1326 size_t Count) { 1327 return wrap( 1328 MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release()); 1329 } 1330 1331 void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) { 1332 MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode)); 1333 } 1334 1335 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata, 1336 LLVMMetadataRef Replacement) { 1337 auto *Node = unwrapDI<MDNode>(TargetMetadata); 1338 Node->replaceAllUsesWith(unwrap<Metadata>(Replacement)); 1339 MDNode::deleteTemporary(Node); 1340 } 1341 1342 LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl( 1343 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1344 size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File, 1345 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit, 1346 LLVMMetadataRef Decl, uint32_t AlignInBits) { 1347 return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl( 1348 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen}, 1349 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit, 1350 unwrapDI<MDNode>(Decl), nullptr, AlignInBits)); 1351 } 1352 1353 LLVMValueRef 1354 LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder, LLVMValueRef Storage, 1355 LLVMMetadataRef VarInfo, LLVMMetadataRef Expr, 1356 LLVMMetadataRef DL, LLVMValueRef Instr) { 1357 return wrap(unwrap(Builder)->insertDeclare( 1358 unwrap(Storage), unwrap<DILocalVariable>(VarInfo), 1359 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL), 1360 unwrap<Instruction>(Instr))); 1361 } 1362 1363 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd( 1364 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo, 1365 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) { 1366 return wrap(unwrap(Builder)->insertDeclare( 1367 unwrap(Storage), unwrap<DILocalVariable>(VarInfo), 1368 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL), 1369 unwrap(Block))); 1370 } 1371 1372 LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder, 1373 LLVMValueRef Val, 1374 LLVMMetadataRef VarInfo, 1375 LLVMMetadataRef Expr, 1376 LLVMMetadataRef DebugLoc, 1377 LLVMValueRef Instr) { 1378 return wrap(unwrap(Builder)->insertDbgValueIntrinsic( 1379 unwrap(Val), unwrap<DILocalVariable>(VarInfo), 1380 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc), 1381 unwrap<Instruction>(Instr))); 1382 } 1383 1384 LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder, 1385 LLVMValueRef Val, 1386 LLVMMetadataRef VarInfo, 1387 LLVMMetadataRef Expr, 1388 LLVMMetadataRef DebugLoc, 1389 LLVMBasicBlockRef Block) { 1390 return wrap(unwrap(Builder)->insertDbgValueIntrinsic( 1391 unwrap(Val), unwrap<DILocalVariable>(VarInfo), 1392 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc), 1393 unwrap(Block))); 1394 } 1395 1396 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable( 1397 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1398 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty, 1399 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) { 1400 return wrap(unwrap(Builder)->createAutoVariable( 1401 unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File), 1402 LineNo, unwrap<DIType>(Ty), AlwaysPreserve, 1403 map_from_llvmDIFlags(Flags), AlignInBits)); 1404 } 1405 1406 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable( 1407 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, 1408 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo, 1409 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) { 1410 return wrap(unwrap(Builder)->createParameterVariable( 1411 unwrap<DIScope>(Scope), {Name, NameLen}, ArgNo, unwrap<DIFile>(File), 1412 LineNo, unwrap<DIType>(Ty), AlwaysPreserve, 1413 map_from_llvmDIFlags(Flags))); 1414 } 1415 1416 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder, 1417 int64_t Lo, int64_t Count) { 1418 return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count)); 1419 } 1420 1421 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder, 1422 LLVMMetadataRef *Data, 1423 size_t Length) { 1424 Metadata **DataValue = unwrap(Data); 1425 return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get()); 1426 } 1427 1428 LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) { 1429 return wrap(unwrap<Function>(Func)->getSubprogram()); 1430 } 1431 1432 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) { 1433 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP)); 1434 } 1435 1436 unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram) { 1437 return unwrapDI<DISubprogram>(Subprogram)->getLine(); 1438 } 1439 1440 LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst) { 1441 return wrap(unwrap<Instruction>(Inst)->getDebugLoc().getAsMDNode()); 1442 } 1443 1444 void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc) { 1445 if (Loc) 1446 unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc(unwrap<MDNode>(Loc))); 1447 else 1448 unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc()); 1449 } 1450 1451 LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) { 1452 switch(unwrap(Metadata)->getMetadataID()) { 1453 #define HANDLE_METADATA_LEAF(CLASS) \ 1454 case Metadata::CLASS##Kind: \ 1455 return (LLVMMetadataKind)LLVM##CLASS##MetadataKind; 1456 #include "llvm/IR/Metadata.def" 1457 default: 1458 return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind; 1459 } 1460 } 1461