blob: 8f383875ad2bbcdb0247131e1079af2fba6b98a8 [file] [log] [blame]
Eugene Zelenkof53a7b42017-05-05 22:30:37 +00001//===- DebugInfo.cpp - Debug Information Helper Classes -------------------===//
Bill Wendling523bea82013-11-08 08:13:15 +00002//
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
whitequark789164d2017-11-01 22:18:52 +000015#include "llvm-c/DebugInfo.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/None.h"
whitequark789164d2017-11-01 22:18:52 +000019#include "llvm/ADT/STLExtras.h"
Bill Wendling523bea82013-11-08 08:13:15 +000020#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/IR/BasicBlock.h"
Bill Wendling523bea82013-11-08 08:13:15 +000024#include "llvm/IR/Constants.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000025#include "llvm/IR/DebugInfoMetadata.h"
26#include "llvm/IR/DebugLoc.h"
whitequark789164d2017-11-01 22:18:52 +000027#include "llvm/IR/DebugInfo.h"
28#include "llvm/IR/DIBuilder.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000029#include "llvm/IR/Function.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000030#include "llvm/IR/GVMaterializer.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000031#include "llvm/IR/Instruction.h"
Bill Wendling523bea82013-11-08 08:13:15 +000032#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000033#include "llvm/IR/LLVMContext.h"
34#include "llvm/IR/Metadata.h"
Bill Wendling523bea82013-11-08 08:13:15 +000035#include "llvm/IR/Module.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000036#include "llvm/Support/Casting.h"
37#include <algorithm>
38#include <cassert>
39#include <utility>
40
Bill Wendling523bea82013-11-08 08:13:15 +000041using namespace llvm;
42using namespace llvm::dwarf;
43
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000044DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
45 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +000046 return LocalScope->getSubprogram();
47 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000048}
49
Bill Wendling523bea82013-11-08 08:13:15 +000050//===----------------------------------------------------------------------===//
51// DebugInfoFinder implementations.
52//===----------------------------------------------------------------------===//
53
54void DebugInfoFinder::reset() {
55 CUs.clear();
56 SPs.clear();
57 GVs.clear();
58 TYs.clear();
59 Scopes.clear();
60 NodesSeen.clear();
Bill Wendling523bea82013-11-08 08:13:15 +000061}
62
Bill Wendling523bea82013-11-08 08:13:15 +000063void DebugInfoFinder::processModule(const Module &M) {
Roman Tereshindab10b52018-04-13 21:23:11 +000064 for (auto *CU : M.debug_compile_units())
65 processCompileUnit(CU);
Keno Fischer30779772017-04-11 13:32:11 +000066 for (auto &F : M.functions()) {
Adrian Prantl75819ae2016-04-15 15:57:41 +000067 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
68 processSubprogram(SP);
Keno Fischer30779772017-04-11 13:32:11 +000069 // There could be subprograms from inlined functions referenced from
70 // instructions only. Walk the function to find them.
Roman Tereshindab10b52018-04-13 21:23:11 +000071 for (const BasicBlock &BB : F)
72 for (const Instruction &I : BB)
73 processInstruction(M, I);
Keno Fischer30779772017-04-11 13:32:11 +000074 }
Bill Wendling523bea82013-11-08 08:13:15 +000075}
76
Roman Tereshind769eb32018-04-13 21:22:24 +000077void 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());
Roman Tereshind769eb32018-04-13 21:22:24 +0000104 }
105}
106
Roman Tereshindab10b52018-04-13 21:23:11 +0000107void 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
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000118void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000119 if (!Loc)
120 return;
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000121 processScope(Loc->getScope());
122 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000123}
124
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000125void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000126 if (!addType(DT))
127 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000128 processScope(DT->getScope().resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000129 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
130 for (DITypeRef Ref : ST->getTypeArray())
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000131 processType(Ref.resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000132 return;
133 }
134 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000135 processType(DCT->getBaseType().resolve());
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000136 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000137 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000138 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000139 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000140 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000141 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000142 return;
143 }
144 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000145 processType(DDT->getBaseType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000146 }
147}
148
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000149void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000150 if (!Scope)
151 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000152 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000153 processType(Ty);
154 return;
155 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000156 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000157 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000158 return;
159 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000160 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000161 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000162 return;
163 }
164 if (!addScope(Scope))
165 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000166 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000167 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000168 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000169 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000170 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
171 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000172 }
173}
174
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000175void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000176 if (!addSubprogram(SP))
177 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000178 processScope(SP->getScope().resolve());
Roman Tereshind769eb32018-04-13 21:22:24 +0000179 // 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());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000188 processType(SP->getType());
189 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000190 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000191 processType(TType->getType().resolve());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000192 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000193 processType(TVal->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000194 }
195 }
196}
197
Manman Ren2085ccc2013-11-17 18:42:37 +0000198void DebugInfoFinder::processDeclare(const Module &M,
199 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000200 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000201 if (!N)
202 return;
203
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000204 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000205 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000206 return;
207
David Blaikie70573dc2014-11-19 07:49:26 +0000208 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000209 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000210 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000211 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000212}
213
Manman Ren2085ccc2013-11-17 18:42:37 +0000214void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000215 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000216 if (!N)
217 return;
218
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000219 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000220 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000221 return;
222
David Blaikie70573dc2014-11-19 07:49:26 +0000223 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000224 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000225 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000226 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000227}
228
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000229bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000230 if (!DT)
231 return false;
232
David Blaikie70573dc2014-11-19 07:49:26 +0000233 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000234 return false;
235
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000236 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000237 return true;
238}
239
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000240bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000241 if (!CU)
242 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000243 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000244 return false;
245
246 CUs.push_back(CU);
247 return true;
248}
249
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000250bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
David Blaikie70573dc2014-11-19 07:49:26 +0000251 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000252 return false;
253
254 GVs.push_back(DIG);
255 return true;
256}
257
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000258bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000259 if (!SP)
260 return false;
261
David Blaikie70573dc2014-11-19 07:49:26 +0000262 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000263 return false;
264
265 SPs.push_back(SP);
266 return true;
267}
268
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000269bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000270 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;
David Blaikie70573dc2014-11-19 07:49:26 +0000276 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000277 return false;
278 Scopes.push_back(Scope);
279 return true;
280}
281
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000282static MDNode *stripDebugLocFromLoopID(MDNode *N) {
Matthias Braun9fd397b2018-10-31 00:23:23 +0000283 assert(!empty(N->operands()) && "Missing self reference?");
Daniel Sandersb96a9452017-01-28 11:22:05 +0000284
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000285 // 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 }))
Daniel Sandersb96a9452017-01-28 11:22:05 +0000289 return N;
290
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000291 // If there is only the debug location without any actual loop metadata, we
Daniel Sandersb96a9452017-01-28 11:22:05 +0000292 // can remove the metadata.
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000293 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
294 return !isa<DILocation>(Op.get());
295 }))
Daniel Sandersb96a9452017-01-28 11:22:05 +0000296 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());
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000302 // 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 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000307
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
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000314bool llvm::stripDebugInfo(Function &F) {
315 bool Changed = false;
Benjamin Kramer0deb9a92018-05-31 13:29:58 +0000316 if (F.hasMetadata(LLVMContext::MD_dbg)) {
Peter Collingbourned4bff302015-11-05 22:03:56 +0000317 Changed = true;
318 F.setSubprogram(nullptr);
319 }
Mehdi Amini581f0e12016-05-07 04:10:52 +0000320
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000321 DenseMap<MDNode*, MDNode*> LoopIDsMap;
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000322 for (BasicBlock &BB : F) {
Mehdi Amini581f0e12016-05-07 04:10:52 +0000323 for (auto II = BB.begin(), End = BB.end(); II != End;) {
324 Instruction &I = *II++; // We may delete the instruction, increment now.
Mehdi Aminidb8dd552016-05-14 04:58:35 +0000325 if (isa<DbgInfoIntrinsic>(&I)) {
326 I.eraseFromParent();
Mehdi Amini581f0e12016-05-07 04:10:52 +0000327 Changed = true;
Mehdi Aminibbedb142016-05-07 05:07:47 +0000328 continue;
Mehdi Amini581f0e12016-05-07 04:10:52 +0000329 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000330 if (I.getDebugLoc()) {
331 Changed = true;
332 I.setDebugLoc(DebugLoc());
333 }
334 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000335
336 auto *TermInst = BB.getTerminator();
Justin Bognerb29bebe2017-08-18 21:38:03 +0000337 if (!TermInst)
338 // This is invalid IR, but we may not have run the verifier yet
339 continue;
Daniel Sandersb96a9452017-01-28 11:22:05 +0000340 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 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000347 }
348 return Changed;
349}
350
Manman Rencb14bbc2013-11-22 22:06:31 +0000351bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000352 bool Changed = false;
353
Manman Rencb14bbc2013-11-22 22:06:31 +0000354 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
355 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000356 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000357 ++NMI;
Davide Italiano84bd58e2016-10-17 20:05:35 +0000358
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") {
Manman Rencb14bbc2013-11-22 22:06:31 +0000363 NMD->eraseFromParent();
364 Changed = true;
365 }
366 }
367
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000368 for (Function &F : M)
369 Changed |= stripDebugInfo(F);
370
Adrian Prantl3bfe1092016-10-10 17:53:33 +0000371 for (auto &GV : M.globals()) {
Benjamin Kramer0deb9a92018-05-31 13:29:58 +0000372 Changed |= GV.eraseMetadata(LLVMContext::MD_dbg);
Adrian Prantl3bfe1092016-10-10 17:53:33 +0000373 }
374
Rafael Espindola468b8682015-04-01 14:44:59 +0000375 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000376 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000377
378 return Changed;
379}
Manman Ren8b4306c2013-12-02 21:29:56 +0000380
Michael Ilsemane5428042016-10-25 18:44:13 +0000381namespace {
382
383/// Helper class to downgrade -g metadata to -gline-tables-only metadata.
384class DebugTypeInfoRemoval {
385 DenseMap<Metadata *, Metadata *> Replacements;
386
387public:
388 /// The (void)() type.
389 MDNode *EmptySubroutineType;
390
391private:
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
405public:
406 DebugTypeInfoRemoval(LLVMContext &C)
407 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
408 MDNode::get(C, {}))) {}
409
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 }
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.
423 void traverseAndRemap(MDNode *N) { traverse(N); }
424
425private:
426 // Create a new DISubprogram, to replace the one given.
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
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(),
Dehao Chen0944a8c2017-02-01 22:45:09 +0000493 CU->getDWOId(), CU->getSplitDebugInlining(),
David Blaikiebb279112018-11-13 20:08:10 +0000494 CU->getDebugInfoForProfiling(), CU->getNameTableKind(),
495 CU->getRangesBaseAddress());
Michael Ilsemane5428042016-10-25 18:44:13 +0000496 }
497
498 DILocation *getReplacementMDLocation(DILocation *MLD) {
499 auto *Scope = map(MLD->getScope());
500 auto *InlinedAt = map(MLD->getInlinedAt());
501 if (MLD->isDistinct())
502 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
503 MLD->getColumn(), Scope, InlinedAt);
504 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
505 Scope, InlinedAt);
506 }
507
508 /// Create a new generic MDNode, to replace the one given
509 MDNode *getReplacementMDNode(MDNode *N) {
510 SmallVector<Metadata *, 8> Ops;
511 Ops.reserve(N->getNumOperands());
512 for (auto &I : N->operands())
513 if (I)
514 Ops.push_back(map(I));
515 auto *Ret = MDNode::get(N->getContext(), Ops);
516 return Ret;
517 }
518
519 /// Attempt to re-map N to a newly created node.
520 void remap(MDNode *N) {
521 if (Replacements.count(N))
522 return;
523
524 auto doRemap = [&](MDNode *N) -> MDNode * {
525 if (!N)
526 return nullptr;
527 if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
528 remap(MDSub->getUnit());
529 return getReplacementSubprogram(MDSub);
530 }
531 if (isa<DISubroutineType>(N))
532 return EmptySubroutineType;
533 if (auto *CU = dyn_cast<DICompileUnit>(N))
534 return getReplacementCU(CU);
535 if (isa<DIFile>(N))
536 return N;
537 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
538 // Remap to our referenced scope (recursively).
539 return mapNode(MDLB->getScope());
540 if (auto *MLD = dyn_cast<DILocation>(N))
541 return getReplacementMDLocation(MLD);
542
543 // Otherwise, if we see these, just drop them now. Not strictly necessary,
544 // but this speeds things up a little.
545 if (isa<DINode>(N))
546 return nullptr;
547
548 return getReplacementMDNode(N);
549 };
550 Replacements[N] = doRemap(N);
551 }
552
553 /// Do the remapping traversal.
554 void traverse(MDNode *);
555};
556
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000557} // end anonymous namespace
Michael Ilsemane5428042016-10-25 18:44:13 +0000558
559void DebugTypeInfoRemoval::traverse(MDNode *N) {
560 if (!N || Replacements.count(N))
561 return;
562
563 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
564 // parts of the graph.
565 auto prune = [](MDNode *Parent, MDNode *Child) {
566 if (auto *MDS = dyn_cast<DISubprogram>(Parent))
Shiva Chen2c864552018-05-09 02:40:45 +0000567 return Child == MDS->getRetainedNodes().get();
Michael Ilsemane5428042016-10-25 18:44:13 +0000568 return false;
569 };
570
571 SmallVector<MDNode *, 16> ToVisit;
572 DenseSet<MDNode *> Opened;
573
574 // Visit each node starting at N in post order, and map them.
575 ToVisit.push_back(N);
576 while (!ToVisit.empty()) {
577 auto *N = ToVisit.back();
578 if (!Opened.insert(N).second) {
579 // Close it.
580 remap(N);
581 ToVisit.pop_back();
582 continue;
583 }
584 for (auto &I : N->operands())
585 if (auto *MDN = dyn_cast_or_null<MDNode>(I))
586 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
587 !isa<DICompileUnit>(MDN))
588 ToVisit.push_back(MDN);
589 }
590}
591
592bool llvm::stripNonLineTableDebugInfo(Module &M) {
593 bool Changed = false;
594
595 // First off, delete the debug intrinsics.
596 auto RemoveUses = [&](StringRef Name) {
597 if (auto *DbgVal = M.getFunction(Name)) {
598 while (!DbgVal->use_empty())
599 cast<Instruction>(DbgVal->user_back())->eraseFromParent();
600 DbgVal->eraseFromParent();
601 Changed = true;
602 }
603 };
604 RemoveUses("llvm.dbg.declare");
605 RemoveUses("llvm.dbg.value");
606
607 // Delete non-CU debug info named metadata nodes.
608 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
609 NMI != NME;) {
610 NamedMDNode *NMD = &*NMI;
611 ++NMI;
612 // Specifically keep dbg.cu around.
613 if (NMD->getName() == "llvm.dbg.cu")
614 continue;
615 }
616
617 // Drop all dbg attachments from global variables.
618 for (auto &GV : M.globals())
619 GV.eraseMetadata(LLVMContext::MD_dbg);
620
621 DebugTypeInfoRemoval Mapper(M.getContext());
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000622 auto remap = [&](MDNode *Node) -> MDNode * {
Michael Ilsemane5428042016-10-25 18:44:13 +0000623 if (!Node)
624 return nullptr;
625 Mapper.traverseAndRemap(Node);
626 auto *NewNode = Mapper.mapNode(Node);
627 Changed |= Node != NewNode;
628 Node = NewNode;
629 return NewNode;
630 };
631
632 // Rewrite the DebugLocs to be equivalent to what
633 // -gline-tables-only would have created.
634 for (auto &F : M) {
635 if (auto *SP = F.getSubprogram()) {
636 Mapper.traverseAndRemap(SP);
637 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
638 Changed |= SP != NewSP;
639 F.setSubprogram(NewSP);
640 }
641 for (auto &BB : F) {
642 for (auto &I : BB) {
Adrian Prantl346dcaf2017-03-30 20:10:56 +0000643 auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
644 auto *Scope = DL.getScope();
645 MDNode *InlinedAt = DL.getInlinedAt();
646 Scope = remap(Scope);
647 InlinedAt = remap(InlinedAt);
648 return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
649 };
Michael Ilsemane5428042016-10-25 18:44:13 +0000650
Adrian Prantl346dcaf2017-03-30 20:10:56 +0000651 if (I.getDebugLoc() != DebugLoc())
652 I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
653
654 // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
655 SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
656 I.getAllMetadata(MDs);
657 for (auto Attachment : MDs)
658 if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
659 for (unsigned N = 0; N < T->getNumOperands(); ++N)
660 if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
661 if (Loc != DebugLoc())
662 T->replaceOperandWith(N, remapDebugLoc(Loc));
Michael Ilsemane5428042016-10-25 18:44:13 +0000663 }
664 }
665 }
666
667 // Create a new llvm.dbg.cu, which is equivalent to the one
668 // -gline-tables-only would have created.
669 for (auto &NMD : M.getNamedMDList()) {
670 SmallVector<MDNode *, 8> Ops;
671 for (MDNode *Op : NMD.operands())
672 Ops.push_back(remap(Op));
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000673
Michael Ilsemane5428042016-10-25 18:44:13 +0000674 if (!Changed)
675 continue;
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000676
Michael Ilsemane5428042016-10-25 18:44:13 +0000677 NMD.clearOperands();
678 for (auto *Op : Ops)
679 if (Op)
680 NMD.addOperand(Op);
681 }
682 return Changed;
683}
684
Manman Renbd4daf82013-12-03 00:12:14 +0000685unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000686 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000687 M.getModuleFlag("Debug Info Version")))
688 return Val->getZExtValue();
689 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000690}
Dehao Chenf4646272017-10-02 18:13:14 +0000691
692void Instruction::applyMergedLocation(const DILocation *LocA,
693 const DILocation *LocB) {
David Blaikie2a813ef2018-08-23 22:35:58 +0000694 setDebugLoc(DILocation::getMergedLocation(LocA, LocB));
Dehao Chenf4646272017-10-02 18:13:14 +0000695}
whitequark789164d2017-11-01 22:18:52 +0000696
697//===----------------------------------------------------------------------===//
698// LLVM C API implementations.
699//===----------------------------------------------------------------------===//
700
701static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
702 switch (lang) {
703#define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) \
704case LLVMDWARFSourceLanguage##NAME: return ID;
705#include "llvm/BinaryFormat/Dwarf.def"
706#undef HANDLE_DW_LANG
707 }
708 llvm_unreachable("Unhandled Tag");
709}
710
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000711template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
712 return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
713}
714
715static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
716 return static_cast<DINode::DIFlags>(Flags);
717}
718
Robert Widmann260b5812018-05-10 18:23:55 +0000719static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) {
720 return static_cast<LLVMDIFlags>(Flags);
721}
722
whitequark789164d2017-11-01 22:18:52 +0000723unsigned LLVMDebugMetadataVersion() {
724 return DEBUG_METADATA_VERSION;
725}
726
727LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
728 return wrap(new DIBuilder(*unwrap(M), false));
729}
730
731LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
732 return wrap(new DIBuilder(*unwrap(M)));
733}
734
735unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
736 return getDebugMetadataVersionFromModule(*unwrap(M));
737}
738
739LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
740 return StripDebugInfo(*unwrap(M));
741}
742
743void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
744 delete unwrap(Builder);
745}
746
747void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
748 unwrap(Builder)->finalize();
749}
750
751LLVMMetadataRef 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) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000758 auto File = unwrapDI<DIFile>(FileRef);
whitequark789164d2017-11-01 22:18:52 +0000759
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
769LLVMMetadataRef
770LLVMDIBuilderCreateFile(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
Robert Widmannb02fe642018-04-23 13:51:43 +0000777LLVMMetadataRef
778LLVMDIBuilderCreateModule(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
790LLVMMetadataRef 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
Robert Widmannf53050f2018-04-07 06:07:55 +0000798LLVMMetadataRef 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
812LLVMMetadataRef 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
820LLVMMetadataRef
821LLVMDIBuilderCreateLexicalBlockFile(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
whitequark789164d2017-11-01 22:18:52 +0000830LLVMMetadataRef
Robert Widmannaec494f32018-04-28 22:32:07 +0000831LLVMDIBuilderCreateImportedModuleFromNamespace(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
842LLVMMetadataRef
843LLVMDIBuilderCreateImportedModuleFromAlias(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
854LLVMMetadataRef
855LLVMDIBuilderCreateImportedModuleFromModule(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
866LLVMMetadataRef
867LLVMDIBuilderCreateImportedDeclaration(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
879LLVMMetadataRef
whitequark789164d2017-11-01 22:18:52 +0000880LLVMDIBuilderCreateDebugLocation(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}
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000886
Robert Widmann260b5812018-05-10 18:23:55 +0000887unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) {
888 return unwrapDI<DILocation>(Location)->getLine();
889}
890
891unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) {
892 return unwrapDI<DILocation>(Location)->getColumn();
893}
894
895LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) {
896 return wrap(unwrapDI<DILocation>(Location)->getScope());
897}
898
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000899LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
900 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
901 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
Robert Widmann2d2698c2018-04-28 18:13:39 +0000902 uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000903 unsigned NumElements, LLVMMetadataRef ClassTy) {
904auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
905 NumElements});
906return wrap(unwrap(Builder)->createEnumerationType(
907 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
908 LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
909}
910
911LLVMMetadataRef LLVMDIBuilderCreateUnionType(
912 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
913 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
Robert Widmann2d2698c2018-04-28 18:13:39 +0000914 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000915 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
926LLVMMetadataRef
Robert Widmann2d2698c2018-04-28 18:13:39 +0000927LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
928 uint32_t AlignInBits, LLVMMetadataRef Ty,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000929 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
937LLVMMetadataRef
Robert Widmann2d2698c2018-04-28 18:13:39 +0000938LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size,
939 uint32_t AlignInBits, LLVMMetadataRef Ty,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000940 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
948LLVMMetadataRef
949LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
Robert Widmann2d2698c2018-04-28 18:13:39 +0000950 size_t NameLen, uint64_t SizeInBits,
whitequarkb56a4d32018-08-19 23:39:47 +0000951 LLVMDWARFTypeEncoding Encoding,
952 LLVMDIFlags Flags) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000953 return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
whitequarkb56a4d32018-08-19 23:39:47 +0000954 SizeInBits, Encoding,
955 map_from_llvmDIFlags(Flags)));
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000956}
957
958LLVMMetadataRef LLVMDIBuilderCreatePointerType(
959 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
Robert Widmann2d2698c2018-04-28 18:13:39 +0000960 uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000961 const char *Name, size_t NameLen) {
962 return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
963 SizeInBits, AlignInBits,
964 AddressSpace, {Name, NameLen}));
965}
966
967LLVMMetadataRef LLVMDIBuilderCreateStructType(
968 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
969 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
Robert Widmann2d2698c2018-04-28 18:13:39 +0000970 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000971 LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
972 unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
973 const char *UniqueId, size_t UniqueIdLen) {
974 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
975 NumElements});
976 return wrap(unwrap(Builder)->createStructType(
977 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
978 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
979 unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
980 unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
981}
982
983LLVMMetadataRef LLVMDIBuilderCreateMemberType(
984 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
Robert Widmann2d2698c2018-04-28 18:13:39 +0000985 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
986 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000987 LLVMMetadataRef Ty) {
988 return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
989 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
990 OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
991}
992
993LLVMMetadataRef
994LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
995 size_t NameLen) {
996 return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
997}
998
999LLVMMetadataRef
1000LLVMDIBuilderCreateStaticMemberType(
1001 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1002 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1003 LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001004 uint32_t AlignInBits) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001005 return wrap(unwrap(Builder)->createStaticMemberType(
1006 unwrapDI<DIScope>(Scope), {Name, NameLen},
1007 unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
1008 map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
1009 AlignInBits));
1010}
1011
1012LLVMMetadataRef
Robert Widmann38fa7502018-05-21 16:27:35 +00001013LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder,
1014 const char *Name, size_t NameLen,
1015 LLVMMetadataRef File, unsigned LineNo,
1016 uint64_t SizeInBits, uint32_t AlignInBits,
1017 uint64_t OffsetInBits, LLVMDIFlags Flags,
1018 LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) {
1019 return wrap(unwrap(Builder)->createObjCIVar(
1020 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
1021 SizeInBits, AlignInBits, OffsetInBits,
1022 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty),
1023 unwrapDI<MDNode>(PropertyNode)));
1024}
1025
1026LLVMMetadataRef
1027LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,
1028 const char *Name, size_t NameLen,
1029 LLVMMetadataRef File, unsigned LineNo,
1030 const char *GetterName, size_t GetterNameLen,
1031 const char *SetterName, size_t SetterNameLen,
1032 unsigned PropertyAttributes,
1033 LLVMMetadataRef Ty) {
1034 return wrap(unwrap(Builder)->createObjCProperty(
1035 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
1036 {GetterName, GetterNameLen}, {SetterName, SetterNameLen},
1037 PropertyAttributes, unwrapDI<DIType>(Ty)));
1038}
1039
1040LLVMMetadataRef
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001041LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
1042 LLVMMetadataRef Type) {
1043 return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
1044}
1045
1046LLVMMetadataRef
Robert Widmann4b0084b2018-05-10 21:10:06 +00001047LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
1048 const char *Name, size_t NameLen,
1049 LLVMMetadataRef File, unsigned LineNo,
1050 LLVMMetadataRef Scope) {
1051 return wrap(unwrap(Builder)->createTypedef(
1052 unwrapDI<DIType>(Type), {Name, NameLen},
1053 unwrapDI<DIFile>(File), LineNo,
1054 unwrapDI<DIScope>(Scope)));
1055}
1056
1057LLVMMetadataRef
Robert Widmann38fa7502018-05-21 16:27:35 +00001058LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder,
1059 LLVMMetadataRef Ty, LLVMMetadataRef BaseTy,
1060 uint64_t BaseOffset, uint32_t VBPtrOffset,
1061 LLVMDIFlags Flags) {
1062 return wrap(unwrap(Builder)->createInheritance(
1063 unwrapDI<DIType>(Ty), unwrapDI<DIType>(BaseTy),
1064 BaseOffset, VBPtrOffset, map_from_llvmDIFlags(Flags)));
1065}
1066
1067LLVMMetadataRef
Robert Widmannb02fe642018-04-23 13:51:43 +00001068LLVMDIBuilderCreateForwardDecl(
1069 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1070 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001071 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
Robert Widmannb02fe642018-04-23 13:51:43 +00001072 const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1073 return wrap(unwrap(Builder)->createForwardDecl(
1074 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
1075 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
1076 AlignInBits, {UniqueIdentifier, UniqueIdentifierLen}));
1077}
1078
1079LLVMMetadataRef
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001080LLVMDIBuilderCreateReplaceableCompositeType(
Harlan Haskinsbee4b582018-04-02 19:11:44 +00001081 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1082 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001083 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001084 LLVMDIFlags Flags, const char *UniqueIdentifier,
Harlan Haskinsbee4b582018-04-02 19:11:44 +00001085 size_t UniqueIdentifierLen) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001086 return wrap(unwrap(Builder)->createReplaceableCompositeType(
1087 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
1088 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
1089 AlignInBits, map_from_llvmDIFlags(Flags),
1090 {UniqueIdentifier, UniqueIdentifierLen}));
1091}
1092
1093LLVMMetadataRef
1094LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
1095 LLVMMetadataRef Type) {
1096 return wrap(unwrap(Builder)->createQualifiedType(Tag,
1097 unwrapDI<DIType>(Type)));
1098}
1099
1100LLVMMetadataRef
1101LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
1102 LLVMMetadataRef Type) {
1103 return wrap(unwrap(Builder)->createReferenceType(Tag,
1104 unwrapDI<DIType>(Type)));
1105}
1106
1107LLVMMetadataRef
1108LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
1109 return wrap(unwrap(Builder)->createNullPtrType());
1110}
1111
1112LLVMMetadataRef
1113LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
1114 LLVMMetadataRef PointeeType,
1115 LLVMMetadataRef ClassType,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001116 uint64_t SizeInBits,
1117 uint32_t AlignInBits,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001118 LLVMDIFlags Flags) {
1119 return wrap(unwrap(Builder)->createMemberPointerType(
1120 unwrapDI<DIType>(PointeeType),
1121 unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
1122 map_from_llvmDIFlags(Flags)));
1123}
1124
1125LLVMMetadataRef
Robert Widmann2d2698c2018-04-28 18:13:39 +00001126LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,
1127 LLVMMetadataRef Scope,
1128 const char *Name, size_t NameLen,
1129 LLVMMetadataRef File, unsigned LineNumber,
1130 uint64_t SizeInBits,
1131 uint64_t OffsetInBits,
1132 uint64_t StorageOffsetInBits,
1133 LLVMDIFlags Flags, LLVMMetadataRef Type) {
1134 return wrap(unwrap(Builder)->createBitFieldMemberType(
1135 unwrapDI<DIScope>(Scope), {Name, NameLen},
1136 unwrapDI<DIFile>(File), LineNumber,
1137 SizeInBits, OffsetInBits, StorageOffsetInBits,
1138 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type)));
1139}
1140
1141LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,
1142 LLVMMetadataRef Scope, const char *Name, size_t NameLen,
1143 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
1144 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
1145 LLVMMetadataRef DerivedFrom,
1146 LLVMMetadataRef *Elements, unsigned NumElements,
1147 LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode,
1148 const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1149 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
1150 NumElements});
1151 return wrap(unwrap(Builder)->createClassType(
1152 unwrapDI<DIScope>(Scope), {Name, NameLen},
1153 unwrapDI<DIFile>(File), LineNumber,
1154 SizeInBits, AlignInBits, OffsetInBits,
1155 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom),
1156 Elts, unwrapDI<DIType>(VTableHolder),
1157 unwrapDI<MDNode>(TemplateParamsNode),
1158 {UniqueIdentifier, UniqueIdentifierLen}));
1159}
1160
1161LLVMMetadataRef
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001162LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
1163 LLVMMetadataRef Type) {
1164 return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
1165}
1166
Robert Widmann260b5812018-05-10 18:23:55 +00001167const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
1168 StringRef Str = unwrap<DIType>(DType)->getName();
1169 *Length = Str.size();
1170 return Str.data();
1171}
1172
1173uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) {
1174 return unwrapDI<DIType>(DType)->getSizeInBits();
1175}
1176
1177uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) {
1178 return unwrapDI<DIType>(DType)->getOffsetInBits();
1179}
1180
1181uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) {
1182 return unwrapDI<DIType>(DType)->getAlignInBits();
1183}
1184
1185unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) {
1186 return unwrapDI<DIType>(DType)->getLine();
1187}
1188
1189LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) {
1190 return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags());
1191}
1192
Robert Widmann6978db72018-04-23 14:29:33 +00001193LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,
1194 LLVMMetadataRef *Types,
1195 size_t Length) {
1196 return wrap(
1197 unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get());
1198}
1199
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001200LLVMMetadataRef
1201LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
1202 LLVMMetadataRef File,
1203 LLVMMetadataRef *ParameterTypes,
1204 unsigned NumParameterTypes,
1205 LLVMDIFlags Flags) {
1206 auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
1207 NumParameterTypes});
1208 return wrap(unwrap(Builder)->createSubroutineType(
1209 Elts, map_from_llvmDIFlags(Flags)));
1210}
Robert Widmannf53050f2018-04-07 06:07:55 +00001211
Robert Widmann12e367b2018-04-22 19:24:44 +00001212LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
1213 int64_t *Addr, size_t Length) {
1214 return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr,
1215 Length)));
1216}
1217
Robert Widmann21fc15d2018-04-23 22:31:49 +00001218LLVMMetadataRef
1219LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,
1220 int64_t Value) {
1221 return wrap(unwrap(Builder)->createConstantValueExpression(Value));
1222}
1223
Matthew Vossf8ab35a2018-10-03 18:44:53 +00001224LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression(
1225 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1226 size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File,
1227 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
1228 LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) {
Robert Widmann21fc15d2018-04-23 22:31:49 +00001229 return wrap(unwrap(Builder)->createGlobalVariableExpression(
Matthew Vossf8ab35a2018-10-03 18:44:53 +00001230 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen},
1231 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
1232 unwrap<DIExpression>(Expr), unwrapDI<MDNode>(Decl),
1233 nullptr, AlignInBits));
Robert Widmann21fc15d2018-04-23 22:31:49 +00001234}
1235
Robert Widmanna428eba2018-05-10 18:09:53 +00001236LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
1237 size_t Count) {
1238 return wrap(
1239 MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release());
1240}
1241
1242void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) {
1243 MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode));
1244}
1245
1246void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,
1247 LLVMMetadataRef Replacement) {
1248 auto *Node = unwrapDI<MDNode>(TargetMetadata);
1249 Node->replaceAllUsesWith(unwrap<Metadata>(Replacement));
1250 MDNode::deleteTemporary(Node);
1251}
1252
Matthew Vossf8ab35a2018-10-03 18:44:53 +00001253LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl(
1254 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1255 size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File,
1256 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
1257 LLVMMetadataRef Decl, uint32_t AlignInBits) {
Robert Widmann21fc15d2018-04-23 22:31:49 +00001258 return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl(
Matthew Vossf8ab35a2018-10-03 18:44:53 +00001259 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen},
1260 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
1261 unwrapDI<MDNode>(Decl), nullptr, AlignInBits));
Robert Widmann21fc15d2018-04-23 22:31:49 +00001262}
1263
Matthew Vossf8ab35a2018-10-03 18:44:53 +00001264LLVMValueRef
1265LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder, LLVMValueRef Storage,
1266 LLVMMetadataRef VarInfo, LLVMMetadataRef Expr,
1267 LLVMMetadataRef DL, LLVMValueRef Instr) {
Robert Widmann12e367b2018-04-22 19:24:44 +00001268 return wrap(unwrap(Builder)->insertDeclare(
1269 unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1270 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1271 unwrap<Instruction>(Instr)));
1272}
1273
1274LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
1275 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1276 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
1277 return wrap(unwrap(Builder)->insertDeclare(
1278 unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1279 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1280 unwrap(Block)));
1281}
1282
Robert Widmann21fc15d2018-04-23 22:31:49 +00001283LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder,
1284 LLVMValueRef Val,
1285 LLVMMetadataRef VarInfo,
1286 LLVMMetadataRef Expr,
1287 LLVMMetadataRef DebugLoc,
1288 LLVMValueRef Instr) {
1289 return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1290 unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1291 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1292 unwrap<Instruction>(Instr)));
1293}
1294
1295LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder,
1296 LLVMValueRef Val,
1297 LLVMMetadataRef VarInfo,
1298 LLVMMetadataRef Expr,
1299 LLVMMetadataRef DebugLoc,
1300 LLVMBasicBlockRef Block) {
1301 return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1302 unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1303 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1304 unwrap(Block)));
1305}
1306
Robert Widmann12e367b2018-04-22 19:24:44 +00001307LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
1308 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1309 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
1310 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
1311 return wrap(unwrap(Builder)->createAutoVariable(
1312 unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File),
1313 LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1314 map_from_llvmDIFlags(Flags), AlignInBits));
1315}
1316
1317LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
1318 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1319 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
1320 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
1321 return wrap(unwrap(Builder)->createParameterVariable(
Robert Widmann106eab02018-08-25 19:54:39 +00001322 unwrap<DIScope>(Scope), {Name, NameLen}, ArgNo, unwrap<DIFile>(File),
Robert Widmann12e367b2018-04-22 19:24:44 +00001323 LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1324 map_from_llvmDIFlags(Flags)));
1325}
1326
Robert Widmann6978db72018-04-23 14:29:33 +00001327LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,
1328 int64_t Lo, int64_t Count) {
1329 return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count));
1330}
1331
1332LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
1333 LLVMMetadataRef *Data,
1334 size_t Length) {
1335 Metadata **DataValue = unwrap(Data);
1336 return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get());
1337}
1338
Robert Widmannf53050f2018-04-07 06:07:55 +00001339LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
1340 return wrap(unwrap<Function>(Func)->getSubprogram());
1341}
1342
1343void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
1344 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
1345}
Robert Widmannabda7ee2018-10-01 13:15:09 +00001346
1347LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) {
1348 switch(unwrap(Metadata)->getMetadataID()) {
1349#define HANDLE_METADATA_LEAF(CLASS) \
1350 case Metadata::CLASS##Kind: \
1351 return (LLVMMetadataKind)LLVM##CLASS##MetadataKind;
1352#include "llvm/IR/Metadata.def"
1353 default:
1354 return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind;
1355 }
1356}