• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- DebugInfo.cpp - Debug Information Helper Classes -------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the helper classes used to build and interpret debug
11 // information in LLVM IR form.
12 //
13 //===----------------------------------------------------------------------===//
14 
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"
37 #include <algorithm>
38 #include <cassert>
39 #include <utility>
40 
41 using namespace llvm;
42 using namespace llvm::dwarf;
43 
getDISubprogram(const MDNode * Scope)44 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
45   if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
46     return LocalScope->getSubprogram();
47   return nullptr;
48 }
49 
50 //===----------------------------------------------------------------------===//
51 // DebugInfoFinder implementations.
52 //===----------------------------------------------------------------------===//
53 
reset()54 void DebugInfoFinder::reset() {
55   CUs.clear();
56   SPs.clear();
57   GVs.clear();
58   TYs.clear();
59   Scopes.clear();
60   NodesSeen.clear();
61 }
62 
processModule(const Module & M)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);
74   }
75 }
76 
processCompileUnit(DICompileUnit * CU)77 void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
78   if (!addCompileUnit(CU))
79     return;
80   for (auto DIG : CU->getGlobalVariables()) {
81     if (!addGlobalVariable(DIG))
82       continue;
83     auto *GV = DIG->getVariable();
84     processScope(GV->getScope());
85     processType(GV->getType().resolve());
86   }
87   for (auto *ET : CU->getEnumTypes())
88     processType(ET);
89   for (auto *RT : CU->getRetainedTypes())
90     if (auto *T = dyn_cast<DIType>(RT))
91       processType(T);
92     else
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))
97       processType(T);
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());
104   }
105 }
106 
processInstruction(const Module & M,const Instruction & I)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);
113 
114   if (auto DbgLoc = I.getDebugLoc())
115     processLocation(M, DbgLoc.get());
116 }
117 
processLocation(const Module & M,const DILocation * Loc)118 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
119   if (!Loc)
120     return;
121   processScope(Loc->getScope());
122   processLocation(M, Loc->getInlinedAt());
123 }
124 
processType(DIType * DT)125 void DebugInfoFinder::processType(DIType *DT) {
126   if (!addType(DT))
127     return;
128   processScope(DT->getScope().resolve());
129   if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
130     for (DITypeRef Ref : ST->getTypeArray())
131       processType(Ref.resolve());
132     return;
133   }
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))
138         processType(T);
139       else if (auto *SP = dyn_cast<DISubprogram>(D))
140         processSubprogram(SP);
141     }
142     return;
143   }
144   if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
145     processType(DDT->getBaseType().resolve());
146   }
147 }
148 
processScope(DIScope * Scope)149 void DebugInfoFinder::processScope(DIScope *Scope) {
150   if (!Scope)
151     return;
152   if (auto *Ty = dyn_cast<DIType>(Scope)) {
153     processType(Ty);
154     return;
155   }
156   if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
157     addCompileUnit(CU);
158     return;
159   }
160   if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
161     processSubprogram(SP);
162     return;
163   }
164   if (!addScope(Scope))
165     return;
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());
172   }
173 }
174 
processSubprogram(DISubprogram * SP)175 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
176   if (!addSubprogram(SP))
177     return;
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());
194     }
195   }
196 }
197 
processDeclare(const Module & M,const DbgDeclareInst * DDI)198 void DebugInfoFinder::processDeclare(const Module &M,
199                                      const DbgDeclareInst *DDI) {
200   auto *N = dyn_cast<MDNode>(DDI->getVariable());
201   if (!N)
202     return;
203 
204   auto *DV = dyn_cast<DILocalVariable>(N);
205   if (!DV)
206     return;
207 
208   if (!NodesSeen.insert(DV).second)
209     return;
210   processScope(DV->getScope());
211   processType(DV->getType().resolve());
212 }
213 
processValue(const Module & M,const DbgValueInst * DVI)214 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
215   auto *N = dyn_cast<MDNode>(DVI->getVariable());
216   if (!N)
217     return;
218 
219   auto *DV = dyn_cast<DILocalVariable>(N);
220   if (!DV)
221     return;
222 
223   if (!NodesSeen.insert(DV).second)
224     return;
225   processScope(DV->getScope());
226   processType(DV->getType().resolve());
227 }
228 
addType(DIType * DT)229 bool DebugInfoFinder::addType(DIType *DT) {
230   if (!DT)
231     return false;
232 
233   if (!NodesSeen.insert(DT).second)
234     return false;
235 
236   TYs.push_back(const_cast<DIType *>(DT));
237   return true;
238 }
239 
addCompileUnit(DICompileUnit * CU)240 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
241   if (!CU)
242     return false;
243   if (!NodesSeen.insert(CU).second)
244     return false;
245 
246   CUs.push_back(CU);
247   return true;
248 }
249 
addGlobalVariable(DIGlobalVariableExpression * DIG)250 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
251   if (!NodesSeen.insert(DIG).second)
252     return false;
253 
254   GVs.push_back(DIG);
255   return true;
256 }
257 
addSubprogram(DISubprogram * SP)258 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
259   if (!SP)
260     return false;
261 
262   if (!NodesSeen.insert(SP).second)
263     return false;
264 
265   SPs.push_back(SP);
266   return true;
267 }
268 
addScope(DIScope * Scope)269 bool DebugInfoFinder::addScope(DIScope *Scope) {
270   if (!Scope)
271     return false;
272   // FIXME: Ocaml binding generates a scope with no content, we treat it
273   // as null for now.
274   if (Scope->getNumOperands() == 0)
275     return false;
276   if (!NodesSeen.insert(Scope).second)
277     return false;
278   Scopes.push_back(Scope);
279   return true;
280 }
281 
stripDebugLocFromLoopID(MDNode * N)282 static MDNode *stripDebugLocFromLoopID(MDNode *N) {
283   assert(N->op_begin() != N->op_end() && "Missing self reference?");
284 
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());
288       }))
289     return N;
290 
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());
295       }))
296     return nullptr;
297 
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))
305       Args.push_back(*Op);
306   }
307 
308   // Set the first operand to itself.
309   MDNode *LoopID = MDNode::get(N->getContext(), Args);
310   LoopID->replaceOperandWith(0, LoopID);
311   return LoopID;
312 }
313 
stripDebugInfo(Function & F)314 bool llvm::stripDebugInfo(Function &F) {
315   bool Changed = false;
316   if (F.hasMetadata(LLVMContext::MD_dbg)) {
317     Changed = true;
318     F.setSubprogram(nullptr);
319   }
320 
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)) {
326         I.eraseFromParent();
327         Changed = true;
328         continue;
329       }
330       if (I.getDebugLoc()) {
331         Changed = true;
332         I.setDebugLoc(DebugLoc());
333       }
334     }
335 
336     auto *TermInst = BB.getTerminator();
337     if (!TermInst)
338       // This is invalid IR, but we may not have run the verifier yet
339       continue;
340     if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
341       auto *NewLoopID = LoopIDsMap.lookup(LoopID);
342       if (!NewLoopID)
343         NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
344       if (NewLoopID != LoopID)
345         TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
346     }
347   }
348   return Changed;
349 }
350 
StripDebugInfo(Module & M)351 bool llvm::StripDebugInfo(Module &M) {
352   bool Changed = false;
353 
354   for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
355          NME = M.named_metadata_end(); NMI != NME;) {
356     NamedMDNode *NMD = &*NMI;
357     ++NMI;
358 
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();
364       Changed = true;
365     }
366   }
367 
368   for (Function &F : M)
369     Changed |= stripDebugInfo(F);
370 
371   for (auto &GV : M.globals()) {
372     Changed |= GV.eraseMetadata(LLVMContext::MD_dbg);
373   }
374 
375   if (GVMaterializer *Materializer = M.getMaterializer())
376     Materializer->setStripDebugInfo();
377 
378   return Changed;
379 }
380 
381 namespace {
382 
383 /// Helper class to downgrade -g metadata to -gline-tables-only metadata.
384 class DebugTypeInfoRemoval {
385   DenseMap<Metadata *, Metadata *> Replacements;
386 
387 public:
388   /// The (void)() type.
389   MDNode *EmptySubroutineType;
390 
391 private:
392   /// Remember what linkage name we originally had before stripping. If we end
393   /// up making two subprograms identical who originally had different linkage
394   /// names, then we need to make one of them distinct, to avoid them getting
395   /// uniqued. Maps the new node to the old linkage name.
396   DenseMap<DISubprogram *, StringRef> NewToLinkageName;
397 
398   // TODO: Remember the distinct subprogram we created for a given linkage name,
399   // so that we can continue to unique whenever possible. Map <newly created
400   // node, old linkage name> to the first (possibly distinct) mdsubprogram
401   // created for that combination. This is not strictly needed for correctness,
402   // but can cut down on the number of MDNodes and let us diff cleanly with the
403   // output of -gline-tables-only.
404 
405 public:
DebugTypeInfoRemoval(LLVMContext & C)406   DebugTypeInfoRemoval(LLVMContext &C)
407       : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
408                                                   MDNode::get(C, {}))) {}
409 
map(Metadata * M)410   Metadata *map(Metadata *M) {
411     if (!M)
412       return nullptr;
413     auto Replacement = Replacements.find(M);
414     if (Replacement != Replacements.end())
415       return Replacement->second;
416 
417     return M;
418   }
mapNode(Metadata * N)419   MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
420 
421   /// Recursively remap N and all its referenced children. Does a DF post-order
422   /// traversal, so as to remap bottoms up.
traverseAndRemap(MDNode * N)423   void traverseAndRemap(MDNode *N) { traverse(N); }
424 
425 private:
426   // Create a new DISubprogram, to replace the one given.
getReplacementSubprogram(DISubprogram * MDS)427   DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
428     auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
429     StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
430     DISubprogram *Declaration = nullptr;
431     auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
432     DITypeRef ContainingType(map(MDS->getContainingType()));
433     auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
434     auto Variables = nullptr;
435     auto TemplateParams = nullptr;
436 
437     // Make a distinct DISubprogram, for situations that warrent it.
438     auto distinctMDSubprogram = [&]() {
439       return DISubprogram::getDistinct(
440           MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
441           FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
442           MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
443           MDS->getVirtuality(), MDS->getVirtualIndex(),
444           MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit,
445           TemplateParams, Declaration, Variables);
446     };
447 
448     if (MDS->isDistinct())
449       return distinctMDSubprogram();
450 
451     auto *NewMDS = DISubprogram::get(
452         MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
453         FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
454         MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
455         MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(),
456         MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration,
457         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
getReplacementCU(DICompileUnit * CU)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->getGnuPubnames());
495   }
496 
getReplacementMDLocation(DILocation * MLD)497   DILocation *getReplacementMDLocation(DILocation *MLD) {
498     auto *Scope = map(MLD->getScope());
499     auto *InlinedAt = map(MLD->getInlinedAt());
500     if (MLD->isDistinct())
501       return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
502                                      MLD->getColumn(), Scope, InlinedAt);
503     return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
504                            Scope, InlinedAt);
505   }
506 
507   /// Create a new generic MDNode, to replace the one given
getReplacementMDNode(MDNode * N)508   MDNode *getReplacementMDNode(MDNode *N) {
509     SmallVector<Metadata *, 8> Ops;
510     Ops.reserve(N->getNumOperands());
511     for (auto &I : N->operands())
512       if (I)
513         Ops.push_back(map(I));
514     auto *Ret = MDNode::get(N->getContext(), Ops);
515     return Ret;
516   }
517 
518   /// Attempt to re-map N to a newly created node.
remap(MDNode * N)519   void remap(MDNode *N) {
520     if (Replacements.count(N))
521       return;
522 
523     auto doRemap = [&](MDNode *N) -> MDNode * {
524       if (!N)
525         return nullptr;
526       if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
527         remap(MDSub->getUnit());
528         return getReplacementSubprogram(MDSub);
529       }
530       if (isa<DISubroutineType>(N))
531         return EmptySubroutineType;
532       if (auto *CU = dyn_cast<DICompileUnit>(N))
533         return getReplacementCU(CU);
534       if (isa<DIFile>(N))
535         return N;
536       if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
537         // Remap to our referenced scope (recursively).
538         return mapNode(MDLB->getScope());
539       if (auto *MLD = dyn_cast<DILocation>(N))
540         return getReplacementMDLocation(MLD);
541 
542       // Otherwise, if we see these, just drop them now. Not strictly necessary,
543       // but this speeds things up a little.
544       if (isa<DINode>(N))
545         return nullptr;
546 
547       return getReplacementMDNode(N);
548     };
549     Replacements[N] = doRemap(N);
550   }
551 
552   /// Do the remapping traversal.
553   void traverse(MDNode *);
554 };
555 
556 } // end anonymous namespace
557 
traverse(MDNode * N)558 void DebugTypeInfoRemoval::traverse(MDNode *N) {
559   if (!N || Replacements.count(N))
560     return;
561 
562   // To avoid cycles, as well as for efficiency sake, we will sometimes prune
563   // parts of the graph.
564   auto prune = [](MDNode *Parent, MDNode *Child) {
565     if (auto *MDS = dyn_cast<DISubprogram>(Parent))
566       return Child == MDS->getRetainedNodes().get();
567     return false;
568   };
569 
570   SmallVector<MDNode *, 16> ToVisit;
571   DenseSet<MDNode *> Opened;
572 
573   // Visit each node starting at N in post order, and map them.
574   ToVisit.push_back(N);
575   while (!ToVisit.empty()) {
576     auto *N = ToVisit.back();
577     if (!Opened.insert(N).second) {
578       // Close it.
579       remap(N);
580       ToVisit.pop_back();
581       continue;
582     }
583     for (auto &I : N->operands())
584       if (auto *MDN = dyn_cast_or_null<MDNode>(I))
585         if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
586             !isa<DICompileUnit>(MDN))
587           ToVisit.push_back(MDN);
588   }
589 }
590 
stripNonLineTableDebugInfo(Module & M)591 bool llvm::stripNonLineTableDebugInfo(Module &M) {
592   bool Changed = false;
593 
594   // First off, delete the debug intrinsics.
595   auto RemoveUses = [&](StringRef Name) {
596     if (auto *DbgVal = M.getFunction(Name)) {
597       while (!DbgVal->use_empty())
598         cast<Instruction>(DbgVal->user_back())->eraseFromParent();
599       DbgVal->eraseFromParent();
600       Changed = true;
601     }
602   };
603   RemoveUses("llvm.dbg.declare");
604   RemoveUses("llvm.dbg.value");
605 
606   // Delete non-CU debug info named metadata nodes.
607   for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
608        NMI != NME;) {
609     NamedMDNode *NMD = &*NMI;
610     ++NMI;
611     // Specifically keep dbg.cu around.
612     if (NMD->getName() == "llvm.dbg.cu")
613       continue;
614   }
615 
616   // Drop all dbg attachments from global variables.
617   for (auto &GV : M.globals())
618     GV.eraseMetadata(LLVMContext::MD_dbg);
619 
620   DebugTypeInfoRemoval Mapper(M.getContext());
621   auto remap = [&](MDNode *Node) -> MDNode * {
622     if (!Node)
623       return nullptr;
624     Mapper.traverseAndRemap(Node);
625     auto *NewNode = Mapper.mapNode(Node);
626     Changed |= Node != NewNode;
627     Node = NewNode;
628     return NewNode;
629   };
630 
631   // Rewrite the DebugLocs to be equivalent to what
632   // -gline-tables-only would have created.
633   for (auto &F : M) {
634     if (auto *SP = F.getSubprogram()) {
635       Mapper.traverseAndRemap(SP);
636       auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
637       Changed |= SP != NewSP;
638       F.setSubprogram(NewSP);
639     }
640     for (auto &BB : F) {
641       for (auto &I : BB) {
642         auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
643           auto *Scope = DL.getScope();
644           MDNode *InlinedAt = DL.getInlinedAt();
645           Scope = remap(Scope);
646           InlinedAt = remap(InlinedAt);
647           return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
648         };
649 
650         if (I.getDebugLoc() != DebugLoc())
651           I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
652 
653         // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
654         SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
655         I.getAllMetadata(MDs);
656         for (auto Attachment : MDs)
657           if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
658             for (unsigned N = 0; N < T->getNumOperands(); ++N)
659               if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
660                 if (Loc != DebugLoc())
661                   T->replaceOperandWith(N, remapDebugLoc(Loc));
662       }
663     }
664   }
665 
666   // Create a new llvm.dbg.cu, which is equivalent to the one
667   // -gline-tables-only would have created.
668   for (auto &NMD : M.getNamedMDList()) {
669     SmallVector<MDNode *, 8> Ops;
670     for (MDNode *Op : NMD.operands())
671       Ops.push_back(remap(Op));
672 
673     if (!Changed)
674       continue;
675 
676     NMD.clearOperands();
677     for (auto *Op : Ops)
678       if (Op)
679         NMD.addOperand(Op);
680   }
681   return Changed;
682 }
683 
getDebugMetadataVersionFromModule(const Module & M)684 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
685   if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
686           M.getModuleFlag("Debug Info Version")))
687     return Val->getZExtValue();
688   return 0;
689 }
690 
applyMergedLocation(const DILocation * LocA,const DILocation * LocB)691 void Instruction::applyMergedLocation(const DILocation *LocA,
692                                       const DILocation *LocB) {
693   setDebugLoc(DILocation::getMergedLocation(LocA, LocB,
694                                             DILocation::WithGeneratedLocation));
695 }
696 
697 //===----------------------------------------------------------------------===//
698 // LLVM C API implementations.
699 //===----------------------------------------------------------------------===//
700 
map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang)701 static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
702   switch (lang) {
703 #define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) \
704 case LLVMDWARFSourceLanguage##NAME: return ID;
705 #include "llvm/BinaryFormat/Dwarf.def"
706 #undef HANDLE_DW_LANG
707   }
708   llvm_unreachable("Unhandled Tag");
709 }
710 
unwrapDI(LLVMMetadataRef Ref)711 template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
712   return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
713 }
714 
map_from_llvmDIFlags(LLVMDIFlags Flags)715 static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
716   return static_cast<DINode::DIFlags>(Flags);
717 }
718 
map_to_llvmDIFlags(DINode::DIFlags Flags)719 static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) {
720   return static_cast<LLVMDIFlags>(Flags);
721 }
722 
LLVMDebugMetadataVersion()723 unsigned LLVMDebugMetadataVersion() {
724   return DEBUG_METADATA_VERSION;
725 }
726 
LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M)727 LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
728   return wrap(new DIBuilder(*unwrap(M), false));
729 }
730 
LLVMCreateDIBuilder(LLVMModuleRef M)731 LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
732   return wrap(new DIBuilder(*unwrap(M)));
733 }
734 
LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M)735 unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
736   return getDebugMetadataVersionFromModule(*unwrap(M));
737 }
738 
LLVMStripModuleDebugInfo(LLVMModuleRef M)739 LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
740   return StripDebugInfo(*unwrap(M));
741 }
742 
LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder)743 void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
744   delete unwrap(Builder);
745 }
746 
LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder)747 void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
748   unwrap(Builder)->finalize();
749 }
750 
LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Builder,LLVMDWARFSourceLanguage Lang,LLVMMetadataRef FileRef,const char * Producer,size_t ProducerLen,LLVMBool isOptimized,const char * Flags,size_t FlagsLen,unsigned RuntimeVer,const char * SplitName,size_t SplitNameLen,LLVMDWARFEmissionKind Kind,unsigned DWOId,LLVMBool SplitDebugInlining,LLVMBool DebugInfoForProfiling)751 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
752     LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
753     LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
754     LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
755     unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
756     LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
757     LLVMBool DebugInfoForProfiling) {
758   auto File = unwrapDI<DIFile>(FileRef);
759 
760   return wrap(unwrap(Builder)->createCompileUnit(
761                  map_from_llvmDWARFsourcelanguage(Lang), File,
762                  StringRef(Producer, ProducerLen), isOptimized,
763                  StringRef(Flags, FlagsLen), RuntimeVer,
764                  StringRef(SplitName, SplitNameLen),
765                  static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
766                  SplitDebugInlining, DebugInfoForProfiling));
767 }
768 
769 LLVMMetadataRef
LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder,const char * Filename,size_t FilenameLen,const char * Directory,size_t DirectoryLen)770 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
771                         size_t FilenameLen, const char *Directory,
772                         size_t DirectoryLen) {
773   return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
774                                           StringRef(Directory, DirectoryLen)));
775 }
776 
777 LLVMMetadataRef
LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder,LLVMMetadataRef ParentScope,const char * Name,size_t NameLen,const char * ConfigMacros,size_t ConfigMacrosLen,const char * IncludePath,size_t IncludePathLen,const char * ISysRoot,size_t ISysRootLen)778 LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope,
779                           const char *Name, size_t NameLen,
780                           const char *ConfigMacros, size_t ConfigMacrosLen,
781                           const char *IncludePath, size_t IncludePathLen,
782                           const char *ISysRoot, size_t ISysRootLen) {
783   return wrap(unwrap(Builder)->createModule(
784       unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen),
785       StringRef(ConfigMacros, ConfigMacrosLen),
786       StringRef(IncludePath, IncludePathLen),
787       StringRef(ISysRoot, ISysRootLen)));
788 }
789 
LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder,LLVMMetadataRef ParentScope,const char * Name,size_t NameLen,LLVMBool ExportSymbols)790 LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder,
791                                              LLVMMetadataRef ParentScope,
792                                              const char *Name, size_t NameLen,
793                                              LLVMBool ExportSymbols) {
794   return wrap(unwrap(Builder)->createNameSpace(
795       unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols));
796 }
797 
LLVMDIBuilderCreateFunction(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,const char * LinkageName,size_t LinkageNameLen,LLVMMetadataRef File,unsigned LineNo,LLVMMetadataRef Ty,LLVMBool IsLocalToUnit,LLVMBool IsDefinition,unsigned ScopeLine,LLVMDIFlags Flags,LLVMBool IsOptimized)798 LLVMMetadataRef LLVMDIBuilderCreateFunction(
799     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
800     size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
801     LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
802     LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
803     unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
804   return wrap(unwrap(Builder)->createFunction(
805       unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
806       unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty),
807       IsLocalToUnit, IsDefinition, ScopeLine, map_from_llvmDIFlags(Flags),
808       IsOptimized, nullptr, nullptr, nullptr));
809 }
810 
811 
LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,LLVMMetadataRef File,unsigned Line,unsigned Col)812 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
813     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
814     LLVMMetadataRef File, unsigned Line, unsigned Col) {
815   return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
816                                                   unwrapDI<DIFile>(File),
817                                                   Line, Col));
818 }
819 
820 LLVMMetadataRef
LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,LLVMMetadataRef File,unsigned Discriminator)821 LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
822                                     LLVMMetadataRef Scope,
823                                     LLVMMetadataRef File,
824                                     unsigned Discriminator) {
825   return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
826                                                       unwrapDI<DIFile>(File),
827                                                       Discriminator));
828 }
829 
830 LLVMMetadataRef
LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,LLVMMetadataRef NS,LLVMMetadataRef File,unsigned Line)831 LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder,
832                                                LLVMMetadataRef Scope,
833                                                LLVMMetadataRef NS,
834                                                LLVMMetadataRef File,
835                                                unsigned Line) {
836   return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
837                                                     unwrapDI<DINamespace>(NS),
838                                                     unwrapDI<DIFile>(File),
839                                                     Line));
840 }
841 
842 LLVMMetadataRef
LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,LLVMMetadataRef ImportedEntity,LLVMMetadataRef File,unsigned Line)843 LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder,
844                                            LLVMMetadataRef Scope,
845                                            LLVMMetadataRef ImportedEntity,
846                                            LLVMMetadataRef File,
847                                            unsigned Line) {
848   return wrap(unwrap(Builder)->createImportedModule(
849                   unwrapDI<DIScope>(Scope),
850                   unwrapDI<DIImportedEntity>(ImportedEntity),
851                   unwrapDI<DIFile>(File), Line));
852 }
853 
854 LLVMMetadataRef
LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,LLVMMetadataRef M,LLVMMetadataRef File,unsigned Line)855 LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder,
856                                             LLVMMetadataRef Scope,
857                                             LLVMMetadataRef M,
858                                             LLVMMetadataRef File,
859                                             unsigned Line) {
860   return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
861                                                     unwrapDI<DIModule>(M),
862                                                     unwrapDI<DIFile>(File),
863                                                     Line));
864 }
865 
866 LLVMMetadataRef
LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,LLVMMetadataRef Decl,LLVMMetadataRef File,unsigned Line,const char * Name,size_t NameLen)867 LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder,
868                                        LLVMMetadataRef Scope,
869                                        LLVMMetadataRef Decl,
870                                        LLVMMetadataRef File,
871                                        unsigned Line,
872                                        const char *Name, size_t NameLen) {
873   return wrap(unwrap(Builder)->createImportedDeclaration(
874                   unwrapDI<DIScope>(Scope),
875                   unwrapDI<DINode>(Decl),
876                   unwrapDI<DIFile>(File), Line, {Name, NameLen}));
877 }
878 
879 LLVMMetadataRef
LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx,unsigned Line,unsigned Column,LLVMMetadataRef Scope,LLVMMetadataRef InlinedAt)880 LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
881                                  unsigned Column, LLVMMetadataRef Scope,
882                                  LLVMMetadataRef InlinedAt) {
883   return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
884                               unwrap(InlinedAt)));
885 }
886 
LLVMDILocationGetLine(LLVMMetadataRef Location)887 unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) {
888   return unwrapDI<DILocation>(Location)->getLine();
889 }
890 
LLVMDILocationGetColumn(LLVMMetadataRef Location)891 unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) {
892   return unwrapDI<DILocation>(Location)->getColumn();
893 }
894 
LLVMDILocationGetScope(LLVMMetadataRef Location)895 LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) {
896   return wrap(unwrapDI<DILocation>(Location)->getScope());
897 }
898 
LLVMDIBuilderCreateEnumerationType(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNumber,uint64_t SizeInBits,uint32_t AlignInBits,LLVMMetadataRef * Elements,unsigned NumElements,LLVMMetadataRef ClassTy)899 LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
900   LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
901   size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
902   uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements,
903   unsigned NumElements, LLVMMetadataRef ClassTy) {
904 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
905                                                NumElements});
906 return wrap(unwrap(Builder)->createEnumerationType(
907     unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
908     LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
909 }
910 
LLVMDIBuilderCreateUnionType(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNumber,uint64_t SizeInBits,uint32_t AlignInBits,LLVMDIFlags Flags,LLVMMetadataRef * Elements,unsigned NumElements,unsigned RunTimeLang,const char * UniqueId,size_t UniqueIdLen)911 LLVMMetadataRef LLVMDIBuilderCreateUnionType(
912   LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
913   size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
914   uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
915   LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
916   const char *UniqueId, size_t UniqueIdLen) {
917   auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
918                                                  NumElements});
919   return wrap(unwrap(Builder)->createUnionType(
920      unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
921      LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
922      Elts, RunTimeLang, {UniqueId, UniqueIdLen}));
923 }
924 
925 
926 LLVMMetadataRef
LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder,uint64_t Size,uint32_t AlignInBits,LLVMMetadataRef Ty,LLVMMetadataRef * Subscripts,unsigned NumSubscripts)927 LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
928                              uint32_t AlignInBits, LLVMMetadataRef Ty,
929                              LLVMMetadataRef *Subscripts,
930                              unsigned NumSubscripts) {
931   auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
932                                                  NumSubscripts});
933   return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits,
934                                                unwrapDI<DIType>(Ty), Subs));
935 }
936 
937 LLVMMetadataRef
LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder,uint64_t Size,uint32_t AlignInBits,LLVMMetadataRef Ty,LLVMMetadataRef * Subscripts,unsigned NumSubscripts)938 LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size,
939                               uint32_t AlignInBits, LLVMMetadataRef Ty,
940                               LLVMMetadataRef *Subscripts,
941                               unsigned NumSubscripts) {
942   auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
943                                                  NumSubscripts});
944   return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits,
945                                                 unwrapDI<DIType>(Ty), Subs));
946 }
947 
948 LLVMMetadataRef
LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder,const char * Name,size_t NameLen,uint64_t SizeInBits,LLVMDWARFTypeEncoding Encoding)949 LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
950                              size_t NameLen, uint64_t SizeInBits,
951                              LLVMDWARFTypeEncoding Encoding) {
952   return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
953                                                SizeInBits, Encoding));
954 }
955 
LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Builder,LLVMMetadataRef PointeeTy,uint64_t SizeInBits,uint32_t AlignInBits,unsigned AddressSpace,const char * Name,size_t NameLen)956 LLVMMetadataRef LLVMDIBuilderCreatePointerType(
957     LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
958     uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
959     const char *Name, size_t NameLen) {
960   return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
961                                          SizeInBits, AlignInBits,
962                                          AddressSpace, {Name, NameLen}));
963 }
964 
LLVMDIBuilderCreateStructType(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNumber,uint64_t SizeInBits,uint32_t AlignInBits,LLVMDIFlags Flags,LLVMMetadataRef DerivedFrom,LLVMMetadataRef * Elements,unsigned NumElements,unsigned RunTimeLang,LLVMMetadataRef VTableHolder,const char * UniqueId,size_t UniqueIdLen)965 LLVMMetadataRef LLVMDIBuilderCreateStructType(
966     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
967     size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
968     uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
969     LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
970     unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
971     const char *UniqueId, size_t UniqueIdLen) {
972   auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
973                                                  NumElements});
974   return wrap(unwrap(Builder)->createStructType(
975       unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
976       LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
977       unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
978       unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
979 }
980 
LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNo,uint64_t SizeInBits,uint32_t AlignInBits,uint64_t OffsetInBits,LLVMDIFlags Flags,LLVMMetadataRef Ty)981 LLVMMetadataRef LLVMDIBuilderCreateMemberType(
982     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
983     size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
984     uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
985     LLVMMetadataRef Ty) {
986   return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
987       {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
988       OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
989 }
990 
991 LLVMMetadataRef
LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder,const char * Name,size_t NameLen)992 LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
993                                    size_t NameLen) {
994   return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
995 }
996 
997 LLVMMetadataRef
LLVMDIBuilderCreateStaticMemberType(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNumber,LLVMMetadataRef Type,LLVMDIFlags Flags,LLVMValueRef ConstantVal,uint32_t AlignInBits)998 LLVMDIBuilderCreateStaticMemberType(
999     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1000     size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1001     LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
1002     uint32_t AlignInBits) {
1003   return wrap(unwrap(Builder)->createStaticMemberType(
1004                   unwrapDI<DIScope>(Scope), {Name, NameLen},
1005                   unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
1006                   map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
1007                   AlignInBits));
1008 }
1009 
1010 LLVMMetadataRef
LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNo,uint64_t SizeInBits,uint32_t AlignInBits,uint64_t OffsetInBits,LLVMDIFlags Flags,LLVMMetadataRef Ty,LLVMMetadataRef PropertyNode)1011 LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder,
1012                             const char *Name, size_t NameLen,
1013                             LLVMMetadataRef File, unsigned LineNo,
1014                             uint64_t SizeInBits, uint32_t AlignInBits,
1015                             uint64_t OffsetInBits, LLVMDIFlags Flags,
1016                             LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) {
1017   return wrap(unwrap(Builder)->createObjCIVar(
1018                   {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
1019                   SizeInBits, AlignInBits, OffsetInBits,
1020                   map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty),
1021                   unwrapDI<MDNode>(PropertyNode)));
1022 }
1023 
1024 LLVMMetadataRef
LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNo,const char * GetterName,size_t GetterNameLen,const char * SetterName,size_t SetterNameLen,unsigned PropertyAttributes,LLVMMetadataRef Ty)1025 LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,
1026                                 const char *Name, size_t NameLen,
1027                                 LLVMMetadataRef File, unsigned LineNo,
1028                                 const char *GetterName, size_t GetterNameLen,
1029                                 const char *SetterName, size_t SetterNameLen,
1030                                 unsigned PropertyAttributes,
1031                                 LLVMMetadataRef Ty) {
1032   return wrap(unwrap(Builder)->createObjCProperty(
1033                   {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
1034                   {GetterName, GetterNameLen}, {SetterName, SetterNameLen},
1035                   PropertyAttributes, unwrapDI<DIType>(Ty)));
1036 }
1037 
1038 LLVMMetadataRef
LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,LLVMMetadataRef Type)1039 LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
1040                                      LLVMMetadataRef Type) {
1041   return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
1042 }
1043 
1044 LLVMMetadataRef
LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder,LLVMMetadataRef Type,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNo,LLVMMetadataRef Scope)1045 LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
1046                            const char *Name, size_t NameLen,
1047                            LLVMMetadataRef File, unsigned LineNo,
1048                            LLVMMetadataRef Scope) {
1049   return wrap(unwrap(Builder)->createTypedef(
1050                   unwrapDI<DIType>(Type), {Name, NameLen},
1051                   unwrapDI<DIFile>(File), LineNo,
1052                   unwrapDI<DIScope>(Scope)));
1053 }
1054 
1055 LLVMMetadataRef
LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder,LLVMMetadataRef Ty,LLVMMetadataRef BaseTy,uint64_t BaseOffset,uint32_t VBPtrOffset,LLVMDIFlags Flags)1056 LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder,
1057                                LLVMMetadataRef Ty, LLVMMetadataRef BaseTy,
1058                                uint64_t BaseOffset, uint32_t VBPtrOffset,
1059                                LLVMDIFlags Flags) {
1060   return wrap(unwrap(Builder)->createInheritance(
1061                   unwrapDI<DIType>(Ty), unwrapDI<DIType>(BaseTy),
1062                   BaseOffset, VBPtrOffset, map_from_llvmDIFlags(Flags)));
1063 }
1064 
1065 LLVMMetadataRef
LLVMDIBuilderCreateForwardDecl(LLVMDIBuilderRef Builder,unsigned Tag,const char * Name,size_t NameLen,LLVMMetadataRef Scope,LLVMMetadataRef File,unsigned Line,unsigned RuntimeLang,uint64_t SizeInBits,uint32_t AlignInBits,const char * UniqueIdentifier,size_t UniqueIdentifierLen)1066 LLVMDIBuilderCreateForwardDecl(
1067     LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1068     size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
1069     unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
1070     const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1071   return wrap(unwrap(Builder)->createForwardDecl(
1072                   Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
1073                   unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
1074                   AlignInBits, {UniqueIdentifier, UniqueIdentifierLen}));
1075 }
1076 
1077 LLVMMetadataRef
LLVMDIBuilderCreateReplaceableCompositeType(LLVMDIBuilderRef Builder,unsigned Tag,const char * Name,size_t NameLen,LLVMMetadataRef Scope,LLVMMetadataRef File,unsigned Line,unsigned RuntimeLang,uint64_t SizeInBits,uint32_t AlignInBits,LLVMDIFlags Flags,const char * UniqueIdentifier,size_t UniqueIdentifierLen)1078 LLVMDIBuilderCreateReplaceableCompositeType(
1079     LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1080     size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
1081     unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
1082     LLVMDIFlags Flags, const char *UniqueIdentifier,
1083     size_t UniqueIdentifierLen) {
1084   return wrap(unwrap(Builder)->createReplaceableCompositeType(
1085                   Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
1086                   unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
1087                   AlignInBits, map_from_llvmDIFlags(Flags),
1088                   {UniqueIdentifier, UniqueIdentifierLen}));
1089 }
1090 
1091 LLVMMetadataRef
LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder,unsigned Tag,LLVMMetadataRef Type)1092 LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
1093                                  LLVMMetadataRef Type) {
1094   return wrap(unwrap(Builder)->createQualifiedType(Tag,
1095                                                    unwrapDI<DIType>(Type)));
1096 }
1097 
1098 LLVMMetadataRef
LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder,unsigned Tag,LLVMMetadataRef Type)1099 LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
1100                                  LLVMMetadataRef Type) {
1101   return wrap(unwrap(Builder)->createReferenceType(Tag,
1102                                                    unwrapDI<DIType>(Type)));
1103 }
1104 
1105 LLVMMetadataRef
LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder)1106 LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
1107   return wrap(unwrap(Builder)->createNullPtrType());
1108 }
1109 
1110 LLVMMetadataRef
LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,LLVMMetadataRef PointeeType,LLVMMetadataRef ClassType,uint64_t SizeInBits,uint32_t AlignInBits,LLVMDIFlags Flags)1111 LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
1112                                      LLVMMetadataRef PointeeType,
1113                                      LLVMMetadataRef ClassType,
1114                                      uint64_t SizeInBits,
1115                                      uint32_t AlignInBits,
1116                                      LLVMDIFlags Flags) {
1117   return wrap(unwrap(Builder)->createMemberPointerType(
1118                   unwrapDI<DIType>(PointeeType),
1119                   unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
1120                   map_from_llvmDIFlags(Flags)));
1121 }
1122 
1123 LLVMMetadataRef
LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNumber,uint64_t SizeInBits,uint64_t OffsetInBits,uint64_t StorageOffsetInBits,LLVMDIFlags Flags,LLVMMetadataRef Type)1124 LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,
1125                                       LLVMMetadataRef Scope,
1126                                       const char *Name, size_t NameLen,
1127                                       LLVMMetadataRef File, unsigned LineNumber,
1128                                       uint64_t SizeInBits,
1129                                       uint64_t OffsetInBits,
1130                                       uint64_t StorageOffsetInBits,
1131                                       LLVMDIFlags Flags, LLVMMetadataRef Type) {
1132   return wrap(unwrap(Builder)->createBitFieldMemberType(
1133                   unwrapDI<DIScope>(Scope), {Name, NameLen},
1134                   unwrapDI<DIFile>(File), LineNumber,
1135                   SizeInBits, OffsetInBits, StorageOffsetInBits,
1136                   map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type)));
1137 }
1138 
LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNumber,uint64_t SizeInBits,uint32_t AlignInBits,uint64_t OffsetInBits,LLVMDIFlags Flags,LLVMMetadataRef DerivedFrom,LLVMMetadataRef * Elements,unsigned NumElements,LLVMMetadataRef VTableHolder,LLVMMetadataRef TemplateParamsNode,const char * UniqueIdentifier,size_t UniqueIdentifierLen)1139 LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,
1140     LLVMMetadataRef Scope, const char *Name, size_t NameLen,
1141     LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
1142     uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
1143     LLVMMetadataRef DerivedFrom,
1144     LLVMMetadataRef *Elements, unsigned NumElements,
1145     LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode,
1146     const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1147   auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
1148                                                  NumElements});
1149   return wrap(unwrap(Builder)->createClassType(
1150                   unwrapDI<DIScope>(Scope), {Name, NameLen},
1151                   unwrapDI<DIFile>(File), LineNumber,
1152                   SizeInBits, AlignInBits, OffsetInBits,
1153                   map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom),
1154                   Elts, unwrapDI<DIType>(VTableHolder),
1155                   unwrapDI<MDNode>(TemplateParamsNode),
1156                   {UniqueIdentifier, UniqueIdentifierLen}));
1157 }
1158 
1159 LLVMMetadataRef
LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,LLVMMetadataRef Type)1160 LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
1161                                   LLVMMetadataRef Type) {
1162   return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
1163 }
1164 
LLVMDITypeGetName(LLVMMetadataRef DType,size_t * Length)1165 const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
1166   StringRef Str = unwrap<DIType>(DType)->getName();
1167   *Length = Str.size();
1168   return Str.data();
1169 }
1170 
LLVMDITypeGetSizeInBits(LLVMMetadataRef DType)1171 uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) {
1172   return unwrapDI<DIType>(DType)->getSizeInBits();
1173 }
1174 
LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType)1175 uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) {
1176   return unwrapDI<DIType>(DType)->getOffsetInBits();
1177 }
1178 
LLVMDITypeGetAlignInBits(LLVMMetadataRef DType)1179 uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) {
1180   return unwrapDI<DIType>(DType)->getAlignInBits();
1181 }
1182 
LLVMDITypeGetLine(LLVMMetadataRef DType)1183 unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) {
1184   return unwrapDI<DIType>(DType)->getLine();
1185 }
1186 
LLVMDITypeGetFlags(LLVMMetadataRef DType)1187 LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) {
1188   return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags());
1189 }
1190 
LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,LLVMMetadataRef * Types,size_t Length)1191 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,
1192                                                   LLVMMetadataRef *Types,
1193                                                   size_t Length) {
1194   return wrap(
1195       unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get());
1196 }
1197 
1198 LLVMMetadataRef
LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,LLVMMetadataRef File,LLVMMetadataRef * ParameterTypes,unsigned NumParameterTypes,LLVMDIFlags Flags)1199 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
1200                                   LLVMMetadataRef File,
1201                                   LLVMMetadataRef *ParameterTypes,
1202                                   unsigned NumParameterTypes,
1203                                   LLVMDIFlags Flags) {
1204   auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
1205                                                      NumParameterTypes});
1206   return wrap(unwrap(Builder)->createSubroutineType(
1207     Elts, map_from_llvmDIFlags(Flags)));
1208 }
1209 
LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,int64_t * Addr,size_t Length)1210 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
1211                                               int64_t *Addr, size_t Length) {
1212   return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr,
1213                                                                   Length)));
1214 }
1215 
1216 LLVMMetadataRef
LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,int64_t Value)1217 LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,
1218                                            int64_t Value) {
1219   return wrap(unwrap(Builder)->createConstantValueExpression(Value));
1220 }
1221 
1222 LLVMMetadataRef
LLVMDIBuilderCreateGlobalVariableExpression(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,const char * Linkage,size_t LinkLen,LLVMMetadataRef File,unsigned LineNo,LLVMMetadataRef Ty,LLVMBool LocalToUnit,LLVMMetadataRef Expr,LLVMMetadataRef Decl,uint32_t AlignInBits)1223 LLVMDIBuilderCreateGlobalVariableExpression(LLVMDIBuilderRef Builder,
1224                                             LLVMMetadataRef Scope,
1225                                             const char *Name, size_t NameLen,
1226                                             const char *Linkage, size_t LinkLen,
1227                                             LLVMMetadataRef File,
1228                                             unsigned LineNo,
1229                                             LLVMMetadataRef Ty,
1230                                             LLVMBool LocalToUnit,
1231                                             LLVMMetadataRef Expr,
1232                                             LLVMMetadataRef Decl,
1233                                             uint32_t AlignInBits) {
1234   return wrap(unwrap(Builder)->createGlobalVariableExpression(
1235                   unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen},
1236                   unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty),
1237                   LocalToUnit, unwrap<DIExpression>(Expr),
1238                   unwrapDI<MDNode>(Decl), AlignInBits));
1239 }
1240 
LLVMTemporaryMDNode(LLVMContextRef Ctx,LLVMMetadataRef * Data,size_t Count)1241 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
1242                                     size_t Count) {
1243   return wrap(
1244       MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release());
1245 }
1246 
LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode)1247 void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) {
1248   MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode));
1249 }
1250 
LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,LLVMMetadataRef Replacement)1251 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,
1252                                     LLVMMetadataRef Replacement) {
1253   auto *Node = unwrapDI<MDNode>(TargetMetadata);
1254   Node->replaceAllUsesWith(unwrap<Metadata>(Replacement));
1255   MDNode::deleteTemporary(Node);
1256 }
1257 
1258 LLVMMetadataRef
LLVMDIBuilderCreateTempGlobalVariableFwdDecl(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,const char * Linkage,size_t LnkLen,LLVMMetadataRef File,unsigned LineNo,LLVMMetadataRef Ty,LLVMBool LocalToUnit,LLVMMetadataRef Decl,uint32_t AlignInBits)1259 LLVMDIBuilderCreateTempGlobalVariableFwdDecl(LLVMDIBuilderRef Builder,
1260                                              LLVMMetadataRef Scope,
1261                                              const char *Name, size_t NameLen,
1262                                              const char *Linkage, size_t LnkLen,
1263                                              LLVMMetadataRef File,
1264                                              unsigned LineNo,
1265                                              LLVMMetadataRef Ty,
1266                                              LLVMBool LocalToUnit,
1267                                              LLVMMetadataRef Decl,
1268                                              uint32_t AlignInBits) {
1269   return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl(
1270                   unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen},
1271                   unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty),
1272                   LocalToUnit, unwrapDI<MDNode>(Decl), AlignInBits));
1273 }
1274 
LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder,LLVMValueRef Storage,LLVMMetadataRef VarInfo,LLVMMetadataRef Expr,LLVMMetadataRef DL,LLVMValueRef Instr)1275 LLVMValueRef LLVMDIBuilderInsertDeclareBefore(
1276   LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1277   LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMValueRef Instr) {
1278   return wrap(unwrap(Builder)->insertDeclare(
1279                   unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1280                   unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1281                   unwrap<Instruction>(Instr)));
1282 }
1283 
LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Builder,LLVMValueRef Storage,LLVMMetadataRef VarInfo,LLVMMetadataRef Expr,LLVMMetadataRef DL,LLVMBasicBlockRef Block)1284 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
1285     LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1286     LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
1287   return wrap(unwrap(Builder)->insertDeclare(
1288                   unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1289                   unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1290                   unwrap(Block)));
1291 }
1292 
LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder,LLVMValueRef Val,LLVMMetadataRef VarInfo,LLVMMetadataRef Expr,LLVMMetadataRef DebugLoc,LLVMValueRef Instr)1293 LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder,
1294                                                LLVMValueRef Val,
1295                                                LLVMMetadataRef VarInfo,
1296                                                LLVMMetadataRef Expr,
1297                                                LLVMMetadataRef DebugLoc,
1298                                                LLVMValueRef Instr) {
1299   return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1300                   unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1301                   unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1302                   unwrap<Instruction>(Instr)));
1303 }
1304 
LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder,LLVMValueRef Val,LLVMMetadataRef VarInfo,LLVMMetadataRef Expr,LLVMMetadataRef DebugLoc,LLVMBasicBlockRef Block)1305 LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder,
1306                                               LLVMValueRef Val,
1307                                               LLVMMetadataRef VarInfo,
1308                                               LLVMMetadataRef Expr,
1309                                               LLVMMetadataRef DebugLoc,
1310                                               LLVMBasicBlockRef Block) {
1311   return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1312                   unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1313                   unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1314                   unwrap(Block)));
1315 }
1316 
LLVMDIBuilderCreateAutoVariable(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,LLVMMetadataRef File,unsigned LineNo,LLVMMetadataRef Ty,LLVMBool AlwaysPreserve,LLVMDIFlags Flags,uint32_t AlignInBits)1317 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
1318     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1319     size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
1320     LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
1321   return wrap(unwrap(Builder)->createAutoVariable(
1322                   unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File),
1323                   LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1324                   map_from_llvmDIFlags(Flags), AlignInBits));
1325 }
1326 
LLVMDIBuilderCreateParameterVariable(LLVMDIBuilderRef Builder,LLVMMetadataRef Scope,const char * Name,size_t NameLen,unsigned ArgNo,LLVMMetadataRef File,unsigned LineNo,LLVMMetadataRef Ty,LLVMBool AlwaysPreserve,LLVMDIFlags Flags)1327 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
1328     LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1329     size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
1330     LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
1331   return wrap(unwrap(Builder)->createParameterVariable(
1332                   unwrap<DIScope>(Scope), {Name, NameLen}, ArgNo, unwrap<DIFile>(File),
1333                   LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1334                   map_from_llvmDIFlags(Flags)));
1335 }
1336 
LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,int64_t Lo,int64_t Count)1337 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,
1338                                                  int64_t Lo, int64_t Count) {
1339   return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count));
1340 }
1341 
LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,LLVMMetadataRef * Data,size_t Length)1342 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
1343                                               LLVMMetadataRef *Data,
1344                                               size_t Length) {
1345   Metadata **DataValue = unwrap(Data);
1346   return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get());
1347 }
1348 
LLVMGetSubprogram(LLVMValueRef Func)1349 LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
1350   return wrap(unwrap<Function>(Func)->getSubprogram());
1351 }
1352 
LLVMSetSubprogram(LLVMValueRef Func,LLVMMetadataRef SP)1353 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
1354   unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
1355 }
1356