1 //===- DebugInfo.cpp - Debug Information Helper Classes -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the helper classes used to build and interpret debug
11 // information in LLVM IR form.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/DebugInfo.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DebugInfoMetadata.h"
26 #include "llvm/IR/DebugLoc.h"
27 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/DIBuilder.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/GVMaterializer.h"
31 #include "llvm/IR/Instruction.h"
32 #include "llvm/IR/IntrinsicInst.h"
33 #include "llvm/IR/LLVMContext.h"
34 #include "llvm/IR/Metadata.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/Support/Casting.h"
42 using namespace llvm::dwarf;
44 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
45 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
46 return LocalScope->getSubprogram();
50 //===----------------------------------------------------------------------===//
51 // DebugInfoFinder implementations.
52 //===----------------------------------------------------------------------===//
54 void DebugInfoFinder::reset() {
63 void DebugInfoFinder::processModule(const Module &M) {
64 for (auto *CU : M.debug_compile_units())
65 processCompileUnit(CU);
66 for (auto &F : M.functions()) {
67 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
68 processSubprogram(SP);
69 // There could be subprograms from inlined functions referenced from
70 // instructions only. Walk the function to find them.
71 for (const BasicBlock &BB : F)
72 for (const Instruction &I : BB)
73 processInstruction(M, I);
77 void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
78 if (!addCompileUnit(CU))
80 for (auto DIG : CU->getGlobalVariables()) {
81 if (!addGlobalVariable(DIG))
83 auto *GV = DIG->getVariable();
84 processScope(GV->getScope());
85 processType(GV->getType().resolve());
87 for (auto *ET : CU->getEnumTypes())
89 for (auto *RT : CU->getRetainedTypes())
90 if (auto *T = dyn_cast<DIType>(RT))
93 processSubprogram(cast<DISubprogram>(RT));
94 for (auto *Import : CU->getImportedEntities()) {
95 auto *Entity = Import->getEntity().resolve();
96 if (auto *T = dyn_cast<DIType>(Entity))
98 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
99 processSubprogram(SP);
100 else if (auto *NS = dyn_cast<DINamespace>(Entity))
101 processScope(NS->getScope());
102 else if (auto *M = dyn_cast<DIModule>(Entity))
103 processScope(M->getScope());
107 void DebugInfoFinder::processInstruction(const Module &M,
108 const Instruction &I) {
109 if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
110 processDeclare(M, DDI);
111 else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
112 processValue(M, DVI);
114 if (auto DbgLoc = I.getDebugLoc())
115 processLocation(M, DbgLoc.get());
118 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
121 processScope(Loc->getScope());
122 processLocation(M, Loc->getInlinedAt());
125 void DebugInfoFinder::processType(DIType *DT) {
128 processScope(DT->getScope().resolve());
129 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
130 for (DITypeRef Ref : ST->getTypeArray())
131 processType(Ref.resolve());
134 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
135 processType(DCT->getBaseType().resolve());
136 for (Metadata *D : DCT->getElements()) {
137 if (auto *T = dyn_cast<DIType>(D))
139 else if (auto *SP = dyn_cast<DISubprogram>(D))
140 processSubprogram(SP);
144 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
145 processType(DDT->getBaseType().resolve());
149 void DebugInfoFinder::processScope(DIScope *Scope) {
152 if (auto *Ty = dyn_cast<DIType>(Scope)) {
156 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
160 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
161 processSubprogram(SP);
164 if (!addScope(Scope))
166 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
167 processScope(LB->getScope());
168 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
169 processScope(NS->getScope());
170 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
171 processScope(M->getScope());
175 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
176 if (!addSubprogram(SP))
178 processScope(SP->getScope().resolve());
179 // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a
180 // ValueMap containing identity mappings for all of the DICompileUnit's, not
181 // just DISubprogram's, referenced from anywhere within the Function being
182 // cloned prior to calling MapMetadata / RemapInstruction to avoid their
183 // duplication later as DICompileUnit's are also directly referenced by
184 // llvm.dbg.cu list. Thefore we need to collect DICompileUnit's here as well.
185 // Also, DICompileUnit's may reference DISubprogram's too and therefore need
186 // to be at least looked through.
187 processCompileUnit(SP->getUnit());
188 processType(SP->getType());
189 for (auto *Element : SP->getTemplateParams()) {
190 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
191 processType(TType->getType().resolve());
192 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
193 processType(TVal->getType().resolve());
198 void DebugInfoFinder::processDeclare(const Module &M,
199 const DbgDeclareInst *DDI) {
200 auto *N = dyn_cast<MDNode>(DDI->getVariable());
204 auto *DV = dyn_cast<DILocalVariable>(N);
208 if (!NodesSeen.insert(DV).second)
210 processScope(DV->getScope());
211 processType(DV->getType().resolve());
214 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
215 auto *N = dyn_cast<MDNode>(DVI->getVariable());
219 auto *DV = dyn_cast<DILocalVariable>(N);
223 if (!NodesSeen.insert(DV).second)
225 processScope(DV->getScope());
226 processType(DV->getType().resolve());
229 bool DebugInfoFinder::addType(DIType *DT) {
233 if (!NodesSeen.insert(DT).second)
236 TYs.push_back(const_cast<DIType *>(DT));
240 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
243 if (!NodesSeen.insert(CU).second)
250 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
251 if (!NodesSeen.insert(DIG).second)
258 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
262 if (!NodesSeen.insert(SP).second)
269 bool DebugInfoFinder::addScope(DIScope *Scope) {
272 // FIXME: Ocaml binding generates a scope with no content, we treat it
274 if (Scope->getNumOperands() == 0)
276 if (!NodesSeen.insert(Scope).second)
278 Scopes.push_back(Scope);
282 static MDNode *stripDebugLocFromLoopID(MDNode *N) {
283 assert(N->op_begin() != N->op_end() && "Missing self reference?");
285 // if there is no debug location, we do not have to rewrite this MDNode.
286 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
287 return isa<DILocation>(Op.get());
291 // If there is only the debug location without any actual loop metadata, we
292 // can remove the metadata.
293 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
294 return !isa<DILocation>(Op.get());
298 SmallVector<Metadata *, 4> Args;
299 // Reserve operand 0 for loop id self reference.
300 auto TempNode = MDNode::getTemporary(N->getContext(), None);
301 Args.push_back(TempNode.get());
302 // Add all non-debug location operands back.
303 for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) {
304 if (!isa<DILocation>(*Op))
308 // Set the first operand to itself.
309 MDNode *LoopID = MDNode::get(N->getContext(), Args);
310 LoopID->replaceOperandWith(0, LoopID);
314 bool llvm::stripDebugInfo(Function &F) {
315 bool Changed = false;
316 if (F.getMetadata(LLVMContext::MD_dbg)) {
318 F.setSubprogram(nullptr);
321 DenseMap<MDNode*, MDNode*> LoopIDsMap;
322 for (BasicBlock &BB : F) {
323 for (auto II = BB.begin(), End = BB.end(); II != End;) {
324 Instruction &I = *II++; // We may delete the instruction, increment now.
325 if (isa<DbgInfoIntrinsic>(&I)) {
330 if (I.getDebugLoc()) {
332 I.setDebugLoc(DebugLoc());
336 auto *TermInst = BB.getTerminator();
338 // This is invalid IR, but we may not have run the verifier yet
340 if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
341 auto *NewLoopID = LoopIDsMap.lookup(LoopID);
343 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
344 if (NewLoopID != LoopID)
345 TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
351 bool llvm::StripDebugInfo(Module &M) {
352 bool Changed = false;
354 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
355 NME = M.named_metadata_end(); NMI != NME;) {
356 NamedMDNode *NMD = &*NMI;
359 // We're stripping debug info, and without them, coverage information
360 // doesn't quite make sense.
361 if (NMD->getName().startswith("llvm.dbg.") ||
362 NMD->getName() == "llvm.gcov") {
363 NMD->eraseFromParent();
368 for (Function &F : M)
369 Changed |= stripDebugInfo(F);
371 for (auto &GV : M.globals()) {
372 SmallVector<MDNode *, 1> MDs;
373 GV.getMetadata(LLVMContext::MD_dbg, MDs);
375 GV.eraseMetadata(LLVMContext::MD_dbg);
380 if (GVMaterializer *Materializer = M.getMaterializer())
381 Materializer->setStripDebugInfo();
388 /// Helper class to downgrade -g metadata to -gline-tables-only metadata.
389 class DebugTypeInfoRemoval {
390 DenseMap<Metadata *, Metadata *> Replacements;
393 /// The (void)() type.
394 MDNode *EmptySubroutineType;
397 /// Remember what linkage name we originally had before stripping. If we end
398 /// up making two subprograms identical who originally had different linkage
399 /// names, then we need to make one of them distinct, to avoid them getting
400 /// uniqued. Maps the new node to the old linkage name.
401 DenseMap<DISubprogram *, StringRef> NewToLinkageName;
403 // TODO: Remember the distinct subprogram we created for a given linkage name,
404 // so that we can continue to unique whenever possible. Map <newly created
405 // node, old linkage name> to the first (possibly distinct) mdsubprogram
406 // created for that combination. This is not strictly needed for correctness,
407 // but can cut down on the number of MDNodes and let us diff cleanly with the
408 // output of -gline-tables-only.
411 DebugTypeInfoRemoval(LLVMContext &C)
412 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
413 MDNode::get(C, {}))) {}
415 Metadata *map(Metadata *M) {
418 auto Replacement = Replacements.find(M);
419 if (Replacement != Replacements.end())
420 return Replacement->second;
424 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
426 /// Recursively remap N and all its referenced children. Does a DF post-order
427 /// traversal, so as to remap bottoms up.
428 void traverseAndRemap(MDNode *N) { traverse(N); }
431 // Create a new DISubprogram, to replace the one given.
432 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
433 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
434 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
435 DISubprogram *Declaration = nullptr;
436 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
437 DITypeRef ContainingType(map(MDS->getContainingType()));
438 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
439 auto Variables = nullptr;
440 auto TemplateParams = nullptr;
442 // Make a distinct DISubprogram, for situations that warrent it.
443 auto distinctMDSubprogram = [&]() {
444 return DISubprogram::getDistinct(
445 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
446 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
447 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
448 MDS->getVirtuality(), MDS->getVirtualIndex(),
449 MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit,
450 TemplateParams, Declaration, Variables);
453 if (MDS->isDistinct())
454 return distinctMDSubprogram();
456 auto *NewMDS = DISubprogram::get(
457 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
458 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
459 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
460 MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(),
461 MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration,
464 StringRef OldLinkageName = MDS->getLinkageName();
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)
473 // Otherwise, need to make a distinct one.
474 // TODO: Query the map to see if we already have one.
475 return distinctMDSubprogram();
478 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
482 /// Create a new compile unit, to replace the one given
483 DICompileUnit *getReplacementCU(DICompileUnit *CU) {
484 // Drop skeleton CUs.
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->getGnuPubnames());
502 DILocation *getReplacementMDLocation(DILocation *MLD) {
503 auto *Scope = map(MLD->getScope());
504 auto *InlinedAt = map(MLD->getInlinedAt());
505 if (MLD->isDistinct())
506 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
507 MLD->getColumn(), Scope, InlinedAt);
508 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
512 /// Create a new generic MDNode, to replace the one given
513 MDNode *getReplacementMDNode(MDNode *N) {
514 SmallVector<Metadata *, 8> Ops;
515 Ops.reserve(N->getNumOperands());
516 for (auto &I : N->operands())
518 Ops.push_back(map(I));
519 auto *Ret = MDNode::get(N->getContext(), Ops);
523 /// Attempt to re-map N to a newly created node.
524 void remap(MDNode *N) {
525 if (Replacements.count(N))
528 auto doRemap = [&](MDNode *N) -> MDNode * {
531 if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
532 remap(MDSub->getUnit());
533 return getReplacementSubprogram(MDSub);
535 if (isa<DISubroutineType>(N))
536 return EmptySubroutineType;
537 if (auto *CU = dyn_cast<DICompileUnit>(N))
538 return getReplacementCU(CU);
541 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
542 // Remap to our referenced scope (recursively).
543 return mapNode(MDLB->getScope());
544 if (auto *MLD = dyn_cast<DILocation>(N))
545 return getReplacementMDLocation(MLD);
547 // Otherwise, if we see these, just drop them now. Not strictly necessary,
548 // but this speeds things up a little.
552 return getReplacementMDNode(N);
554 Replacements[N] = doRemap(N);
557 /// Do the remapping traversal.
558 void traverse(MDNode *);
561 } // end anonymous namespace
563 void DebugTypeInfoRemoval::traverse(MDNode *N) {
564 if (!N || Replacements.count(N))
567 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
568 // parts of the graph.
569 auto prune = [](MDNode *Parent, MDNode *Child) {
570 if (auto *MDS = dyn_cast<DISubprogram>(Parent))
571 return Child == MDS->getVariables().get();
575 SmallVector<MDNode *, 16> ToVisit;
576 DenseSet<MDNode *> Opened;
578 // Visit each node starting at N in post order, and map them.
579 ToVisit.push_back(N);
580 while (!ToVisit.empty()) {
581 auto *N = ToVisit.back();
582 if (!Opened.insert(N).second) {
588 for (auto &I : N->operands())
589 if (auto *MDN = dyn_cast_or_null<MDNode>(I))
590 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
591 !isa<DICompileUnit>(MDN))
592 ToVisit.push_back(MDN);
596 bool llvm::stripNonLineTableDebugInfo(Module &M) {
597 bool Changed = false;
599 // First off, delete the debug intrinsics.
600 auto RemoveUses = [&](StringRef Name) {
601 if (auto *DbgVal = M.getFunction(Name)) {
602 while (!DbgVal->use_empty())
603 cast<Instruction>(DbgVal->user_back())->eraseFromParent();
604 DbgVal->eraseFromParent();
608 RemoveUses("llvm.dbg.declare");
609 RemoveUses("llvm.dbg.value");
611 // Delete non-CU debug info named metadata nodes.
612 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
614 NamedMDNode *NMD = &*NMI;
616 // Specifically keep dbg.cu around.
617 if (NMD->getName() == "llvm.dbg.cu")
621 // Drop all dbg attachments from global variables.
622 for (auto &GV : M.globals())
623 GV.eraseMetadata(LLVMContext::MD_dbg);
625 DebugTypeInfoRemoval Mapper(M.getContext());
626 auto remap = [&](MDNode *Node) -> MDNode * {
629 Mapper.traverseAndRemap(Node);
630 auto *NewNode = Mapper.mapNode(Node);
631 Changed |= Node != NewNode;
636 // Rewrite the DebugLocs to be equivalent to what
637 // -gline-tables-only would have created.
639 if (auto *SP = F.getSubprogram()) {
640 Mapper.traverseAndRemap(SP);
641 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
642 Changed |= SP != NewSP;
643 F.setSubprogram(NewSP);
647 auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
648 auto *Scope = DL.getScope();
649 MDNode *InlinedAt = DL.getInlinedAt();
650 Scope = remap(Scope);
651 InlinedAt = remap(InlinedAt);
652 return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
655 if (I.getDebugLoc() != DebugLoc())
656 I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
658 // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
659 SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
660 I.getAllMetadata(MDs);
661 for (auto Attachment : MDs)
662 if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
663 for (unsigned N = 0; N < T->getNumOperands(); ++N)
664 if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
665 if (Loc != DebugLoc())
666 T->replaceOperandWith(N, remapDebugLoc(Loc));
671 // Create a new llvm.dbg.cu, which is equivalent to the one
672 // -gline-tables-only would have created.
673 for (auto &NMD : M.getNamedMDList()) {
674 SmallVector<MDNode *, 8> Ops;
675 for (MDNode *Op : NMD.operands())
676 Ops.push_back(remap(Op));
689 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
690 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
691 M.getModuleFlag("Debug Info Version")))
692 return Val->getZExtValue();
696 void Instruction::applyMergedLocation(const DILocation *LocA,
697 const DILocation *LocB) {
698 setDebugLoc(DILocation::getMergedLocation(LocA, LocB,
699 DILocation::WithGeneratedLocation));
702 //===----------------------------------------------------------------------===//
703 // LLVM C API implementations.
704 //===----------------------------------------------------------------------===//
706 static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
708 #define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) \
709 case LLVMDWARFSourceLanguage##NAME: return ID;
710 #include "llvm/BinaryFormat/Dwarf.def"
711 #undef HANDLE_DW_LANG
713 llvm_unreachable("Unhandled Tag");
716 template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
717 return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
720 static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
721 return static_cast<DINode::DIFlags>(Flags);
724 unsigned LLVMDebugMetadataVersion() {
725 return DEBUG_METADATA_VERSION;
728 LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
729 return wrap(new DIBuilder(*unwrap(M), false));
732 LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
733 return wrap(new DIBuilder(*unwrap(M)));
736 unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
737 return getDebugMetadataVersionFromModule(*unwrap(M));
740 LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
741 return StripDebugInfo(*unwrap(M));
744 void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
745 delete unwrap(Builder);
748 void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
749 unwrap(Builder)->finalize();
752 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
753 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
754 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
755 LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
756 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
757 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
758 LLVMBool DebugInfoForProfiling) {
759 auto File = unwrapDI<DIFile>(FileRef);
761 return wrap(unwrap(Builder)->createCompileUnit(
762 map_from_llvmDWARFsourcelanguage(Lang), File,
763 StringRef(Producer, ProducerLen), isOptimized,
764 StringRef(Flags, FlagsLen), RuntimeVer,
765 StringRef(SplitName, SplitNameLen),
766 static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
767 SplitDebugInlining, DebugInfoForProfiling));
771 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
772 size_t FilenameLen, const char *Directory,
773 size_t DirectoryLen) {
774 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
775 StringRef(Directory, DirectoryLen)));
779 LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope,
780 const char *Name, size_t NameLen,
781 const char *ConfigMacros, size_t ConfigMacrosLen,
782 const char *IncludePath, size_t IncludePathLen,
783 const char *ISysRoot, size_t ISysRootLen) {
784 return wrap(unwrap(Builder)->createModule(
785 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen),
786 StringRef(ConfigMacros, ConfigMacrosLen),
787 StringRef(IncludePath, IncludePathLen),
788 StringRef(ISysRoot, ISysRootLen)));
791 LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder,
792 LLVMMetadataRef ParentScope,
793 const char *Name, size_t NameLen,
794 LLVMBool ExportSymbols) {
795 return wrap(unwrap(Builder)->createNameSpace(
796 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols));
799 LLVMMetadataRef LLVMDIBuilderCreateFunction(
800 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
801 size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
802 LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
803 LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
804 unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
805 return wrap(unwrap(Builder)->createFunction(
806 unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
807 unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty),
808 IsLocalToUnit, IsDefinition, ScopeLine, map_from_llvmDIFlags(Flags),
809 IsOptimized, nullptr, nullptr, nullptr));
813 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
814 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
815 LLVMMetadataRef File, unsigned Line, unsigned Col) {
816 return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
817 unwrapDI<DIFile>(File),
822 LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
823 LLVMMetadataRef Scope,
824 LLVMMetadataRef File,
825 unsigned Discriminator) {
826 return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
827 unwrapDI<DIFile>(File),
832 LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
833 unsigned Column, LLVMMetadataRef Scope,
834 LLVMMetadataRef InlinedAt) {
835 return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
839 LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
840 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
841 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
842 uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements,
843 unsigned NumElements, LLVMMetadataRef ClassTy) {
844 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
846 return wrap(unwrap(Builder)->createEnumerationType(
847 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
848 LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
851 LLVMMetadataRef LLVMDIBuilderCreateUnionType(
852 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
853 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
854 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
855 LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
856 const char *UniqueId, size_t UniqueIdLen) {
857 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
859 return wrap(unwrap(Builder)->createUnionType(
860 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
861 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
862 Elts, RunTimeLang, {UniqueId, UniqueIdLen}));
867 LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
868 uint32_t AlignInBits, LLVMMetadataRef Ty,
869 LLVMMetadataRef *Subscripts,
870 unsigned NumSubscripts) {
871 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
873 return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits,
874 unwrapDI<DIType>(Ty), Subs));
878 LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size,
879 uint32_t AlignInBits, LLVMMetadataRef Ty,
880 LLVMMetadataRef *Subscripts,
881 unsigned NumSubscripts) {
882 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
884 return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits,
885 unwrapDI<DIType>(Ty), Subs));
889 LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
890 size_t NameLen, uint64_t SizeInBits,
891 LLVMDWARFTypeEncoding Encoding) {
892 return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
893 SizeInBits, Encoding));
896 LLVMMetadataRef LLVMDIBuilderCreatePointerType(
897 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
898 uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
899 const char *Name, size_t NameLen) {
900 return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
901 SizeInBits, AlignInBits,
902 AddressSpace, {Name, NameLen}));
905 LLVMMetadataRef LLVMDIBuilderCreateStructType(
906 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
907 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
908 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
909 LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
910 unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
911 const char *UniqueId, size_t UniqueIdLen) {
912 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
914 return wrap(unwrap(Builder)->createStructType(
915 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
916 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
917 unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
918 unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
921 LLVMMetadataRef LLVMDIBuilderCreateMemberType(
922 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
923 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
924 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
925 LLVMMetadataRef Ty) {
926 return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
927 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
928 OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
932 LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
934 return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
938 LLVMDIBuilderCreateStaticMemberType(
939 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
940 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
941 LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
942 uint32_t AlignInBits) {
943 return wrap(unwrap(Builder)->createStaticMemberType(
944 unwrapDI<DIScope>(Scope), {Name, NameLen},
945 unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
946 map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
951 LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
952 LLVMMetadataRef Type) {
953 return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
957 LLVMDIBuilderCreateForwardDecl(
958 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
959 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
960 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
961 const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
962 return wrap(unwrap(Builder)->createForwardDecl(
963 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
964 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
965 AlignInBits, {UniqueIdentifier, UniqueIdentifierLen}));
969 LLVMDIBuilderCreateReplaceableCompositeType(
970 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
971 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
972 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
973 LLVMDIFlags Flags, const char *UniqueIdentifier,
974 size_t UniqueIdentifierLen) {
975 return wrap(unwrap(Builder)->createReplaceableCompositeType(
976 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
977 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
978 AlignInBits, map_from_llvmDIFlags(Flags),
979 {UniqueIdentifier, UniqueIdentifierLen}));
983 LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
984 LLVMMetadataRef Type) {
985 return wrap(unwrap(Builder)->createQualifiedType(Tag,
986 unwrapDI<DIType>(Type)));
990 LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
991 LLVMMetadataRef Type) {
992 return wrap(unwrap(Builder)->createReferenceType(Tag,
993 unwrapDI<DIType>(Type)));
997 LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
998 return wrap(unwrap(Builder)->createNullPtrType());
1002 LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
1003 LLVMMetadataRef PointeeType,
1004 LLVMMetadataRef ClassType,
1005 uint64_t SizeInBits,
1006 uint32_t AlignInBits,
1007 LLVMDIFlags Flags) {
1008 return wrap(unwrap(Builder)->createMemberPointerType(
1009 unwrapDI<DIType>(PointeeType),
1010 unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
1011 map_from_llvmDIFlags(Flags)));
1015 LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,
1016 LLVMMetadataRef Scope,
1017 const char *Name, size_t NameLen,
1018 LLVMMetadataRef File, unsigned LineNumber,
1019 uint64_t SizeInBits,
1020 uint64_t OffsetInBits,
1021 uint64_t StorageOffsetInBits,
1022 LLVMDIFlags Flags, LLVMMetadataRef Type) {
1023 return wrap(unwrap(Builder)->createBitFieldMemberType(
1024 unwrapDI<DIScope>(Scope), {Name, NameLen},
1025 unwrapDI<DIFile>(File), LineNumber,
1026 SizeInBits, OffsetInBits, StorageOffsetInBits,
1027 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type)));
1030 LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,
1031 LLVMMetadataRef Scope, const char *Name, size_t NameLen,
1032 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
1033 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
1034 LLVMMetadataRef DerivedFrom,
1035 LLVMMetadataRef *Elements, unsigned NumElements,
1036 LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode,
1037 const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1038 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
1040 return wrap(unwrap(Builder)->createClassType(
1041 unwrapDI<DIScope>(Scope), {Name, NameLen},
1042 unwrapDI<DIFile>(File), LineNumber,
1043 SizeInBits, AlignInBits, OffsetInBits,
1044 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom),
1045 Elts, unwrapDI<DIType>(VTableHolder),
1046 unwrapDI<MDNode>(TemplateParamsNode),
1047 {UniqueIdentifier, UniqueIdentifierLen}));
1051 LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
1052 LLVMMetadataRef Type) {
1053 return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
1056 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,
1057 LLVMMetadataRef *Types,
1060 unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get());
1064 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
1065 LLVMMetadataRef File,
1066 LLVMMetadataRef *ParameterTypes,
1067 unsigned NumParameterTypes,
1068 LLVMDIFlags Flags) {
1069 auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
1070 NumParameterTypes});
1071 return wrap(unwrap(Builder)->createSubroutineType(
1072 Elts, map_from_llvmDIFlags(Flags)));
1075 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
1076 int64_t *Addr, size_t Length) {
1077 return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr,
1082 LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,
1084 return wrap(unwrap(Builder)->createConstantValueExpression(Value));
1088 LLVMDIBuilderCreateGlobalVariableExpression(LLVMDIBuilderRef Builder,
1089 LLVMMetadataRef Scope,
1090 const char *Name, size_t NameLen,
1091 const char *Linkage, size_t LinkLen,
1092 LLVMMetadataRef File,
1095 LLVMBool LocalToUnit,
1096 LLVMMetadataRef Expr,
1097 LLVMMetadataRef Decl,
1098 uint32_t AlignInBits) {
1099 return wrap(unwrap(Builder)->createGlobalVariableExpression(
1100 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen},
1101 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty),
1102 LocalToUnit, unwrap<DIExpression>(Expr),
1103 unwrapDI<MDNode>(Decl), AlignInBits));
1107 LLVMDIBuilderCreateTempGlobalVariableFwdDecl(LLVMDIBuilderRef Builder,
1108 LLVMMetadataRef Scope,
1109 const char *Name, size_t NameLen,
1110 const char *Linkage, size_t LnkLen,
1111 LLVMMetadataRef File,
1114 LLVMBool LocalToUnit,
1115 LLVMMetadataRef Decl,
1116 uint32_t AlignInBits) {
1117 return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl(
1118 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen},
1119 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty),
1120 LocalToUnit, unwrapDI<MDNode>(Decl), AlignInBits));
1123 LLVMValueRef LLVMDIBuilderInsertDeclareBefore(
1124 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1125 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMValueRef Instr) {
1126 return wrap(unwrap(Builder)->insertDeclare(
1127 unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1128 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1129 unwrap<Instruction>(Instr)));
1132 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
1133 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1134 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
1135 return wrap(unwrap(Builder)->insertDeclare(
1136 unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1137 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1141 LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder,
1143 LLVMMetadataRef VarInfo,
1144 LLVMMetadataRef Expr,
1145 LLVMMetadataRef DebugLoc,
1146 LLVMValueRef Instr) {
1147 return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1148 unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1149 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1150 unwrap<Instruction>(Instr)));
1153 LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder,
1155 LLVMMetadataRef VarInfo,
1156 LLVMMetadataRef Expr,
1157 LLVMMetadataRef DebugLoc,
1158 LLVMBasicBlockRef Block) {
1159 return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1160 unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1161 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1165 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
1166 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1167 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
1168 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
1169 return wrap(unwrap(Builder)->createAutoVariable(
1170 unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File),
1171 LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1172 map_from_llvmDIFlags(Flags), AlignInBits));
1175 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
1176 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1177 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
1178 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
1179 return wrap(unwrap(Builder)->createParameterVariable(
1180 unwrap<DIScope>(Scope), Name, ArgNo, unwrap<DIFile>(File),
1181 LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1182 map_from_llvmDIFlags(Flags)));
1185 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,
1186 int64_t Lo, int64_t Count) {
1187 return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count));
1190 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
1191 LLVMMetadataRef *Data,
1193 Metadata **DataValue = unwrap(Data);
1194 return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get());
1197 LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
1198 return wrap(unwrap<Function>(Func)->getSubprogram());
1201 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
1202 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));