blob: ce47ef2074343d341213d37eccc92490ff9c1785 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Bill Wendling523bea82013-11-08 08:13:15 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the helper classes used to build and interpret debug
10// information in LLVM IR form.
11//
12//===----------------------------------------------------------------------===//
13
whitequark789164d2017-11-01 22:18:52 +000014#include "llvm-c/DebugInfo.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/None.h"
whitequark789164d2017-11-01 22:18:52 +000018#include "llvm/ADT/STLExtras.h"
Bill Wendling523bea82013-11-08 08:13:15 +000019#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/IR/BasicBlock.h"
Bill Wendling523bea82013-11-08 08:13:15 +000023#include "llvm/IR/Constants.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000024#include "llvm/IR/DebugInfoMetadata.h"
25#include "llvm/IR/DebugLoc.h"
whitequark789164d2017-11-01 22:18:52 +000026#include "llvm/IR/DebugInfo.h"
27#include "llvm/IR/DIBuilder.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000028#include "llvm/IR/Function.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000029#include "llvm/IR/GVMaterializer.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000030#include "llvm/IR/Instruction.h"
Bill Wendling523bea82013-11-08 08:13:15 +000031#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000032#include "llvm/IR/LLVMContext.h"
33#include "llvm/IR/Metadata.h"
Bill Wendling523bea82013-11-08 08:13:15 +000034#include "llvm/IR/Module.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000035#include "llvm/Support/Casting.h"
36#include <algorithm>
37#include <cassert>
38#include <utility>
39
Bill Wendling523bea82013-11-08 08:13:15 +000040using namespace llvm;
41using namespace llvm::dwarf;
42
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000043DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
44 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +000045 return LocalScope->getSubprogram();
46 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000047}
48
Bill Wendling523bea82013-11-08 08:13:15 +000049//===----------------------------------------------------------------------===//
50// DebugInfoFinder implementations.
51//===----------------------------------------------------------------------===//
52
53void DebugInfoFinder::reset() {
54 CUs.clear();
55 SPs.clear();
56 GVs.clear();
57 TYs.clear();
58 Scopes.clear();
59 NodesSeen.clear();
Bill Wendling523bea82013-11-08 08:13:15 +000060}
61
Bill Wendling523bea82013-11-08 08:13:15 +000062void DebugInfoFinder::processModule(const Module &M) {
Roman Tereshindab10b52018-04-13 21:23:11 +000063 for (auto *CU : M.debug_compile_units())
64 processCompileUnit(CU);
Keno Fischer30779772017-04-11 13:32:11 +000065 for (auto &F : M.functions()) {
Adrian Prantl75819ae2016-04-15 15:57:41 +000066 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
67 processSubprogram(SP);
Keno Fischer30779772017-04-11 13:32:11 +000068 // There could be subprograms from inlined functions referenced from
69 // instructions only. Walk the function to find them.
Roman Tereshindab10b52018-04-13 21:23:11 +000070 for (const BasicBlock &BB : F)
71 for (const Instruction &I : BB)
72 processInstruction(M, I);
Keno Fischer30779772017-04-11 13:32:11 +000073 }
Bill Wendling523bea82013-11-08 08:13:15 +000074}
75
Roman Tereshind769eb32018-04-13 21:22:24 +000076void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
77 if (!addCompileUnit(CU))
78 return;
79 for (auto DIG : CU->getGlobalVariables()) {
80 if (!addGlobalVariable(DIG))
81 continue;
82 auto *GV = DIG->getVariable();
83 processScope(GV->getScope());
Fangrui Songda82ce92019-05-07 02:06:37 +000084 processType(GV->getType());
Roman Tereshind769eb32018-04-13 21:22:24 +000085 }
86 for (auto *ET : CU->getEnumTypes())
87 processType(ET);
88 for (auto *RT : CU->getRetainedTypes())
89 if (auto *T = dyn_cast<DIType>(RT))
90 processType(T);
91 else
92 processSubprogram(cast<DISubprogram>(RT));
93 for (auto *Import : CU->getImportedEntities()) {
Fangrui Songda82ce92019-05-07 02:06:37 +000094 auto *Entity = Import->getEntity();
Roman Tereshind769eb32018-04-13 21:22:24 +000095 if (auto *T = dyn_cast<DIType>(Entity))
96 processType(T);
97 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
98 processSubprogram(SP);
99 else if (auto *NS = dyn_cast<DINamespace>(Entity))
100 processScope(NS->getScope());
101 else if (auto *M = dyn_cast<DIModule>(Entity))
102 processScope(M->getScope());
Roman Tereshind769eb32018-04-13 21:22:24 +0000103 }
104}
105
Roman Tereshindab10b52018-04-13 21:23:11 +0000106void DebugInfoFinder::processInstruction(const Module &M,
107 const Instruction &I) {
108 if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
109 processDeclare(M, DDI);
110 else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
111 processValue(M, DVI);
112
113 if (auto DbgLoc = I.getDebugLoc())
114 processLocation(M, DbgLoc.get());
115}
116
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000117void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000118 if (!Loc)
119 return;
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000120 processScope(Loc->getScope());
121 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000122}
123
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000124void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000125 if (!addType(DT))
126 return;
Fangrui Songda82ce92019-05-07 02:06:37 +0000127 processScope(DT->getScope());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000128 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
Fangrui Songda82ce92019-05-07 02:06:37 +0000129 for (DIType *Ref : ST->getTypeArray())
130 processType(Ref);
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000131 return;
132 }
133 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Fangrui Songda82ce92019-05-07 02:06:37 +0000134 processType(DCT->getBaseType());
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000135 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000136 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000137 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000138 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000139 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000140 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000141 return;
142 }
143 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Fangrui Songda82ce92019-05-07 02:06:37 +0000144 processType(DDT->getBaseType());
Bill Wendling523bea82013-11-08 08:13:15 +0000145 }
146}
147
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000148void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000149 if (!Scope)
150 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000151 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000152 processType(Ty);
153 return;
154 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000155 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000156 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000157 return;
158 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000159 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000160 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000161 return;
162 }
163 if (!addScope(Scope))
164 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000165 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000166 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000167 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000168 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000169 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
170 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000171 }
172}
173
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000174void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000175 if (!addSubprogram(SP))
176 return;
Fangrui Songda82ce92019-05-07 02:06:37 +0000177 processScope(SP->getScope());
Roman Tereshind769eb32018-04-13 21:22:24 +0000178 // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a
179 // ValueMap containing identity mappings for all of the DICompileUnit's, not
180 // just DISubprogram's, referenced from anywhere within the Function being
181 // cloned prior to calling MapMetadata / RemapInstruction to avoid their
182 // duplication later as DICompileUnit's are also directly referenced by
183 // llvm.dbg.cu list. Thefore we need to collect DICompileUnit's here as well.
184 // Also, DICompileUnit's may reference DISubprogram's too and therefore need
185 // to be at least looked through.
186 processCompileUnit(SP->getUnit());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000187 processType(SP->getType());
188 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000189 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Fangrui Songda82ce92019-05-07 02:06:37 +0000190 processType(TType->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000191 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Fangrui Songda82ce92019-05-07 02:06:37 +0000192 processType(TVal->getType());
Bill Wendling523bea82013-11-08 08:13:15 +0000193 }
194 }
195}
196
Manman Ren2085ccc2013-11-17 18:42:37 +0000197void DebugInfoFinder::processDeclare(const Module &M,
198 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000199 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000200 if (!N)
201 return;
202
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000203 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000204 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000205 return;
206
David Blaikie70573dc2014-11-19 07:49:26 +0000207 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000208 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000209 processScope(DV->getScope());
Fangrui Songda82ce92019-05-07 02:06:37 +0000210 processType(DV->getType());
Bill Wendling523bea82013-11-08 08:13:15 +0000211}
212
Manman Ren2085ccc2013-11-17 18:42:37 +0000213void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000214 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000215 if (!N)
216 return;
217
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000218 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000219 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000220 return;
221
David Blaikie70573dc2014-11-19 07:49:26 +0000222 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000223 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000224 processScope(DV->getScope());
Fangrui Songda82ce92019-05-07 02:06:37 +0000225 processType(DV->getType());
Bill Wendling523bea82013-11-08 08:13:15 +0000226}
227
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000228bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000229 if (!DT)
230 return false;
231
David Blaikie70573dc2014-11-19 07:49:26 +0000232 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000233 return false;
234
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000235 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000236 return true;
237}
238
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000239bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000240 if (!CU)
241 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000242 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000243 return false;
244
245 CUs.push_back(CU);
246 return true;
247}
248
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000249bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
David Blaikie70573dc2014-11-19 07:49:26 +0000250 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000251 return false;
252
253 GVs.push_back(DIG);
254 return true;
255}
256
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000257bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000258 if (!SP)
259 return false;
260
David Blaikie70573dc2014-11-19 07:49:26 +0000261 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000262 return false;
263
264 SPs.push_back(SP);
265 return true;
266}
267
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000268bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000269 if (!Scope)
270 return false;
271 // FIXME: Ocaml binding generates a scope with no content, we treat it
272 // as null for now.
273 if (Scope->getNumOperands() == 0)
274 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000275 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000276 return false;
277 Scopes.push_back(Scope);
278 return true;
279}
280
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000281static MDNode *stripDebugLocFromLoopID(MDNode *N) {
Matthias Braun9fd397b2018-10-31 00:23:23 +0000282 assert(!empty(N->operands()) && "Missing self reference?");
Daniel Sandersb96a9452017-01-28 11:22:05 +0000283
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000284 // if there is no debug location, we do not have to rewrite this MDNode.
285 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
286 return isa<DILocation>(Op.get());
287 }))
Daniel Sandersb96a9452017-01-28 11:22:05 +0000288 return N;
289
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000290 // If there is only the debug location without any actual loop metadata, we
Daniel Sandersb96a9452017-01-28 11:22:05 +0000291 // can remove the metadata.
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000292 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
293 return !isa<DILocation>(Op.get());
294 }))
Daniel Sandersb96a9452017-01-28 11:22:05 +0000295 return nullptr;
296
297 SmallVector<Metadata *, 4> Args;
298 // Reserve operand 0 for loop id self reference.
299 auto TempNode = MDNode::getTemporary(N->getContext(), None);
300 Args.push_back(TempNode.get());
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000301 // Add all non-debug location operands back.
302 for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) {
303 if (!isa<DILocation>(*Op))
304 Args.push_back(*Op);
305 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000306
307 // Set the first operand to itself.
308 MDNode *LoopID = MDNode::get(N->getContext(), Args);
309 LoopID->replaceOperandWith(0, LoopID);
310 return LoopID;
311}
312
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000313bool llvm::stripDebugInfo(Function &F) {
314 bool Changed = false;
Benjamin Kramer0deb9a92018-05-31 13:29:58 +0000315 if (F.hasMetadata(LLVMContext::MD_dbg)) {
Peter Collingbourned4bff302015-11-05 22:03:56 +0000316 Changed = true;
317 F.setSubprogram(nullptr);
318 }
Mehdi Amini581f0e12016-05-07 04:10:52 +0000319
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000320 DenseMap<MDNode*, MDNode*> LoopIDsMap;
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000321 for (BasicBlock &BB : F) {
Mehdi Amini581f0e12016-05-07 04:10:52 +0000322 for (auto II = BB.begin(), End = BB.end(); II != End;) {
323 Instruction &I = *II++; // We may delete the instruction, increment now.
Mehdi Aminidb8dd552016-05-14 04:58:35 +0000324 if (isa<DbgInfoIntrinsic>(&I)) {
325 I.eraseFromParent();
Mehdi Amini581f0e12016-05-07 04:10:52 +0000326 Changed = true;
Mehdi Aminibbedb142016-05-07 05:07:47 +0000327 continue;
Mehdi Amini581f0e12016-05-07 04:10:52 +0000328 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000329 if (I.getDebugLoc()) {
330 Changed = true;
331 I.setDebugLoc(DebugLoc());
332 }
333 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000334
335 auto *TermInst = BB.getTerminator();
Justin Bognerb29bebe2017-08-18 21:38:03 +0000336 if (!TermInst)
337 // This is invalid IR, but we may not have run the verifier yet
338 continue;
Daniel Sandersb96a9452017-01-28 11:22:05 +0000339 if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
340 auto *NewLoopID = LoopIDsMap.lookup(LoopID);
341 if (!NewLoopID)
342 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
343 if (NewLoopID != LoopID)
344 TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
345 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000346 }
347 return Changed;
348}
349
Manman Rencb14bbc2013-11-22 22:06:31 +0000350bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000351 bool Changed = false;
352
Manman Rencb14bbc2013-11-22 22:06:31 +0000353 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
354 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000355 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000356 ++NMI;
Davide Italiano84bd58e2016-10-17 20:05:35 +0000357
358 // We're stripping debug info, and without them, coverage information
359 // doesn't quite make sense.
360 if (NMD->getName().startswith("llvm.dbg.") ||
361 NMD->getName() == "llvm.gcov") {
Manman Rencb14bbc2013-11-22 22:06:31 +0000362 NMD->eraseFromParent();
363 Changed = true;
364 }
365 }
366
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000367 for (Function &F : M)
368 Changed |= stripDebugInfo(F);
369
Adrian Prantl3bfe1092016-10-10 17:53:33 +0000370 for (auto &GV : M.globals()) {
Benjamin Kramer0deb9a92018-05-31 13:29:58 +0000371 Changed |= GV.eraseMetadata(LLVMContext::MD_dbg);
Adrian Prantl3bfe1092016-10-10 17:53:33 +0000372 }
373
Rafael Espindola468b8682015-04-01 14:44:59 +0000374 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000375 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000376
377 return Changed;
378}
Manman Ren8b4306c2013-12-02 21:29:56 +0000379
Michael Ilsemane5428042016-10-25 18:44:13 +0000380namespace {
381
382/// Helper class to downgrade -g metadata to -gline-tables-only metadata.
383class DebugTypeInfoRemoval {
384 DenseMap<Metadata *, Metadata *> Replacements;
385
386public:
387 /// The (void)() type.
388 MDNode *EmptySubroutineType;
389
390private:
391 /// Remember what linkage name we originally had before stripping. If we end
392 /// up making two subprograms identical who originally had different linkage
393 /// names, then we need to make one of them distinct, to avoid them getting
394 /// uniqued. Maps the new node to the old linkage name.
395 DenseMap<DISubprogram *, StringRef> NewToLinkageName;
396
397 // TODO: Remember the distinct subprogram we created for a given linkage name,
398 // so that we can continue to unique whenever possible. Map <newly created
399 // node, old linkage name> to the first (possibly distinct) mdsubprogram
400 // created for that combination. This is not strictly needed for correctness,
401 // but can cut down on the number of MDNodes and let us diff cleanly with the
402 // output of -gline-tables-only.
403
404public:
405 DebugTypeInfoRemoval(LLVMContext &C)
406 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
407 MDNode::get(C, {}))) {}
408
409 Metadata *map(Metadata *M) {
410 if (!M)
411 return nullptr;
412 auto Replacement = Replacements.find(M);
413 if (Replacement != Replacements.end())
414 return Replacement->second;
415
416 return M;
417 }
418 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
419
420 /// Recursively remap N and all its referenced children. Does a DF post-order
421 /// traversal, so as to remap bottoms up.
422 void traverseAndRemap(MDNode *N) { traverse(N); }
423
424private:
425 // Create a new DISubprogram, to replace the one given.
426 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
427 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
428 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
429 DISubprogram *Declaration = nullptr;
430 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
Fangrui Songda82ce92019-05-07 02:06:37 +0000431 DIType *ContainingType =
432 cast_or_null<DIType>(map(MDS->getContainingType()));
Michael Ilsemane5428042016-10-25 18:44:13 +0000433 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,
Paul Robinsoncda54212018-11-19 18:29:28 +0000441 FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(),
442 ContainingType, MDS->getVirtualIndex(), MDS->getThisAdjustment(),
443 MDS->getFlags(), MDS->getSPFlags(), Unit, TemplateParams, Declaration,
444 Variables);
Michael Ilsemane5428042016-10-25 18:44:13 +0000445 };
446
447 if (MDS->isDistinct())
448 return distinctMDSubprogram();
449
450 auto *NewMDS = DISubprogram::get(
451 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
Paul Robinsoncda54212018-11-19 18:29:28 +0000452 FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(), ContainingType,
453 MDS->getVirtualIndex(), MDS->getThisAdjustment(), MDS->getFlags(),
454 MDS->getSPFlags(), Unit, TemplateParams, Declaration, Variables);
Michael Ilsemane5428042016-10-25 18:44:13 +0000455
456 StringRef OldLinkageName = MDS->getLinkageName();
457
458 // See if we need to make a distinct one.
459 auto OrigLinkage = NewToLinkageName.find(NewMDS);
460 if (OrigLinkage != NewToLinkageName.end()) {
461 if (OrigLinkage->second == OldLinkageName)
462 // We're good.
463 return NewMDS;
464
465 // Otherwise, need to make a distinct one.
466 // TODO: Query the map to see if we already have one.
467 return distinctMDSubprogram();
468 }
469
470 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
471 return NewMDS;
472 }
473
474 /// Create a new compile unit, to replace the one given
475 DICompileUnit *getReplacementCU(DICompileUnit *CU) {
476 // Drop skeleton CUs.
477 if (CU->getDWOId())
478 return nullptr;
479
480 auto *File = cast_or_null<DIFile>(map(CU->getFile()));
481 MDTuple *EnumTypes = nullptr;
482 MDTuple *RetainedTypes = nullptr;
483 MDTuple *GlobalVariables = nullptr;
484 MDTuple *ImportedEntities = nullptr;
485 return DICompileUnit::getDistinct(
486 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
487 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
488 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
489 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
Dehao Chen0944a8c2017-02-01 22:45:09 +0000490 CU->getDWOId(), CU->getSplitDebugInlining(),
David Blaikiebb279112018-11-13 20:08:10 +0000491 CU->getDebugInfoForProfiling(), CU->getNameTableKind(),
492 CU->getRangesBaseAddress());
Michael Ilsemane5428042016-10-25 18:44:13 +0000493 }
494
495 DILocation *getReplacementMDLocation(DILocation *MLD) {
496 auto *Scope = map(MLD->getScope());
497 auto *InlinedAt = map(MLD->getInlinedAt());
498 if (MLD->isDistinct())
499 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
500 MLD->getColumn(), Scope, InlinedAt);
501 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
502 Scope, InlinedAt);
503 }
504
505 /// Create a new generic MDNode, to replace the one given
506 MDNode *getReplacementMDNode(MDNode *N) {
507 SmallVector<Metadata *, 8> Ops;
508 Ops.reserve(N->getNumOperands());
509 for (auto &I : N->operands())
510 if (I)
511 Ops.push_back(map(I));
512 auto *Ret = MDNode::get(N->getContext(), Ops);
513 return Ret;
514 }
515
516 /// Attempt to re-map N to a newly created node.
517 void remap(MDNode *N) {
518 if (Replacements.count(N))
519 return;
520
521 auto doRemap = [&](MDNode *N) -> MDNode * {
522 if (!N)
523 return nullptr;
524 if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
525 remap(MDSub->getUnit());
526 return getReplacementSubprogram(MDSub);
527 }
528 if (isa<DISubroutineType>(N))
529 return EmptySubroutineType;
530 if (auto *CU = dyn_cast<DICompileUnit>(N))
531 return getReplacementCU(CU);
532 if (isa<DIFile>(N))
533 return N;
534 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
535 // Remap to our referenced scope (recursively).
536 return mapNode(MDLB->getScope());
537 if (auto *MLD = dyn_cast<DILocation>(N))
538 return getReplacementMDLocation(MLD);
539
540 // Otherwise, if we see these, just drop them now. Not strictly necessary,
541 // but this speeds things up a little.
542 if (isa<DINode>(N))
543 return nullptr;
544
545 return getReplacementMDNode(N);
546 };
547 Replacements[N] = doRemap(N);
548 }
549
550 /// Do the remapping traversal.
551 void traverse(MDNode *);
552};
553
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000554} // end anonymous namespace
Michael Ilsemane5428042016-10-25 18:44:13 +0000555
556void DebugTypeInfoRemoval::traverse(MDNode *N) {
557 if (!N || Replacements.count(N))
558 return;
559
560 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
561 // parts of the graph.
562 auto prune = [](MDNode *Parent, MDNode *Child) {
563 if (auto *MDS = dyn_cast<DISubprogram>(Parent))
Shiva Chen2c864552018-05-09 02:40:45 +0000564 return Child == MDS->getRetainedNodes().get();
Michael Ilsemane5428042016-10-25 18:44:13 +0000565 return false;
566 };
567
568 SmallVector<MDNode *, 16> ToVisit;
569 DenseSet<MDNode *> Opened;
570
571 // Visit each node starting at N in post order, and map them.
572 ToVisit.push_back(N);
573 while (!ToVisit.empty()) {
574 auto *N = ToVisit.back();
575 if (!Opened.insert(N).second) {
576 // Close it.
577 remap(N);
578 ToVisit.pop_back();
579 continue;
580 }
581 for (auto &I : N->operands())
582 if (auto *MDN = dyn_cast_or_null<MDNode>(I))
583 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
584 !isa<DICompileUnit>(MDN))
585 ToVisit.push_back(MDN);
586 }
587}
588
589bool llvm::stripNonLineTableDebugInfo(Module &M) {
590 bool Changed = false;
591
592 // First off, delete the debug intrinsics.
593 auto RemoveUses = [&](StringRef Name) {
594 if (auto *DbgVal = M.getFunction(Name)) {
595 while (!DbgVal->use_empty())
596 cast<Instruction>(DbgVal->user_back())->eraseFromParent();
597 DbgVal->eraseFromParent();
598 Changed = true;
599 }
600 };
601 RemoveUses("llvm.dbg.declare");
602 RemoveUses("llvm.dbg.value");
603
604 // Delete non-CU debug info named metadata nodes.
605 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
606 NMI != NME;) {
607 NamedMDNode *NMD = &*NMI;
608 ++NMI;
609 // Specifically keep dbg.cu around.
610 if (NMD->getName() == "llvm.dbg.cu")
611 continue;
612 }
613
614 // Drop all dbg attachments from global variables.
615 for (auto &GV : M.globals())
616 GV.eraseMetadata(LLVMContext::MD_dbg);
617
618 DebugTypeInfoRemoval Mapper(M.getContext());
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000619 auto remap = [&](MDNode *Node) -> MDNode * {
Michael Ilsemane5428042016-10-25 18:44:13 +0000620 if (!Node)
621 return nullptr;
622 Mapper.traverseAndRemap(Node);
623 auto *NewNode = Mapper.mapNode(Node);
624 Changed |= Node != NewNode;
625 Node = NewNode;
626 return NewNode;
627 };
628
629 // Rewrite the DebugLocs to be equivalent to what
630 // -gline-tables-only would have created.
631 for (auto &F : M) {
632 if (auto *SP = F.getSubprogram()) {
633 Mapper.traverseAndRemap(SP);
634 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
635 Changed |= SP != NewSP;
636 F.setSubprogram(NewSP);
637 }
638 for (auto &BB : F) {
639 for (auto &I : BB) {
Adrian Prantl346dcaf2017-03-30 20:10:56 +0000640 auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
641 auto *Scope = DL.getScope();
642 MDNode *InlinedAt = DL.getInlinedAt();
643 Scope = remap(Scope);
644 InlinedAt = remap(InlinedAt);
645 return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
646 };
Michael Ilsemane5428042016-10-25 18:44:13 +0000647
Adrian Prantl346dcaf2017-03-30 20:10:56 +0000648 if (I.getDebugLoc() != DebugLoc())
649 I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
650
651 // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
652 SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
653 I.getAllMetadata(MDs);
654 for (auto Attachment : MDs)
655 if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
656 for (unsigned N = 0; N < T->getNumOperands(); ++N)
657 if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
658 if (Loc != DebugLoc())
659 T->replaceOperandWith(N, remapDebugLoc(Loc));
Michael Ilsemane5428042016-10-25 18:44:13 +0000660 }
661 }
662 }
663
664 // Create a new llvm.dbg.cu, which is equivalent to the one
665 // -gline-tables-only would have created.
666 for (auto &NMD : M.getNamedMDList()) {
667 SmallVector<MDNode *, 8> Ops;
668 for (MDNode *Op : NMD.operands())
669 Ops.push_back(remap(Op));
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000670
Michael Ilsemane5428042016-10-25 18:44:13 +0000671 if (!Changed)
672 continue;
Bjorn Petterssonaa025802018-07-03 12:39:52 +0000673
Michael Ilsemane5428042016-10-25 18:44:13 +0000674 NMD.clearOperands();
675 for (auto *Op : Ops)
676 if (Op)
677 NMD.addOperand(Op);
678 }
679 return Changed;
680}
681
Manman Renbd4daf82013-12-03 00:12:14 +0000682unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000683 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000684 M.getModuleFlag("Debug Info Version")))
685 return Val->getZExtValue();
686 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000687}
Dehao Chenf4646272017-10-02 18:13:14 +0000688
689void Instruction::applyMergedLocation(const DILocation *LocA,
690 const DILocation *LocB) {
David Blaikie2a813ef2018-08-23 22:35:58 +0000691 setDebugLoc(DILocation::getMergedLocation(LocA, LocB));
Dehao Chenf4646272017-10-02 18:13:14 +0000692}
whitequark789164d2017-11-01 22:18:52 +0000693
694//===----------------------------------------------------------------------===//
695// LLVM C API implementations.
696//===----------------------------------------------------------------------===//
697
698static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
699 switch (lang) {
David Blaikieac69af72018-12-19 19:34:24 +0000700#define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
701 case LLVMDWARFSourceLanguage##NAME: \
702 return ID;
whitequark789164d2017-11-01 22:18:52 +0000703#include "llvm/BinaryFormat/Dwarf.def"
704#undef HANDLE_DW_LANG
705 }
706 llvm_unreachable("Unhandled Tag");
707}
708
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000709template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
710 return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
711}
712
713static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
714 return static_cast<DINode::DIFlags>(Flags);
715}
716
Robert Widmann260b5812018-05-10 18:23:55 +0000717static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) {
718 return static_cast<LLVMDIFlags>(Flags);
719}
720
Paul Robinsoncda54212018-11-19 18:29:28 +0000721static DISubprogram::DISPFlags
722pack_into_DISPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized) {
723 return DISubprogram::toSPFlags(IsLocalToUnit, IsDefinition, IsOptimized);
724}
725
whitequark789164d2017-11-01 22:18:52 +0000726unsigned LLVMDebugMetadataVersion() {
727 return DEBUG_METADATA_VERSION;
728}
729
730LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
731 return wrap(new DIBuilder(*unwrap(M), false));
732}
733
734LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
735 return wrap(new DIBuilder(*unwrap(M)));
736}
737
738unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
739 return getDebugMetadataVersionFromModule(*unwrap(M));
740}
741
742LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
743 return StripDebugInfo(*unwrap(M));
744}
745
746void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
747 delete unwrap(Builder);
748}
749
750void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
751 unwrap(Builder)->finalize();
752}
753
754LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
755 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
756 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
757 LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
758 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
759 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
760 LLVMBool DebugInfoForProfiling) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000761 auto File = unwrapDI<DIFile>(FileRef);
whitequark789164d2017-11-01 22:18:52 +0000762
763 return wrap(unwrap(Builder)->createCompileUnit(
764 map_from_llvmDWARFsourcelanguage(Lang), File,
765 StringRef(Producer, ProducerLen), isOptimized,
766 StringRef(Flags, FlagsLen), RuntimeVer,
767 StringRef(SplitName, SplitNameLen),
768 static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
769 SplitDebugInlining, DebugInfoForProfiling));
770}
771
772LLVMMetadataRef
773LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
774 size_t FilenameLen, const char *Directory,
775 size_t DirectoryLen) {
776 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
777 StringRef(Directory, DirectoryLen)));
778}
779
Robert Widmannb02fe642018-04-23 13:51:43 +0000780LLVMMetadataRef
781LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope,
782 const char *Name, size_t NameLen,
783 const char *ConfigMacros, size_t ConfigMacrosLen,
784 const char *IncludePath, size_t IncludePathLen,
785 const char *ISysRoot, size_t ISysRootLen) {
786 return wrap(unwrap(Builder)->createModule(
787 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen),
788 StringRef(ConfigMacros, ConfigMacrosLen),
789 StringRef(IncludePath, IncludePathLen),
790 StringRef(ISysRoot, ISysRootLen)));
791}
792
793LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder,
794 LLVMMetadataRef ParentScope,
795 const char *Name, size_t NameLen,
796 LLVMBool ExportSymbols) {
797 return wrap(unwrap(Builder)->createNameSpace(
798 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols));
799}
800
Robert Widmannf53050f2018-04-07 06:07:55 +0000801LLVMMetadataRef LLVMDIBuilderCreateFunction(
802 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
803 size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
804 LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
805 LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
806 unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
807 return wrap(unwrap(Builder)->createFunction(
808 unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
Paul Robinsoncda54212018-11-19 18:29:28 +0000809 unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty), ScopeLine,
810 map_from_llvmDIFlags(Flags),
811 pack_into_DISPFlags(IsLocalToUnit, IsDefinition, IsOptimized), nullptr,
812 nullptr, nullptr));
Robert Widmannf53050f2018-04-07 06:07:55 +0000813}
814
815
816LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
817 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
818 LLVMMetadataRef File, unsigned Line, unsigned Col) {
819 return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
820 unwrapDI<DIFile>(File),
821 Line, Col));
822}
823
824LLVMMetadataRef
825LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
826 LLVMMetadataRef Scope,
827 LLVMMetadataRef File,
828 unsigned Discriminator) {
829 return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
830 unwrapDI<DIFile>(File),
831 Discriminator));
832}
833
whitequark789164d2017-11-01 22:18:52 +0000834LLVMMetadataRef
Robert Widmannaec494f32018-04-28 22:32:07 +0000835LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder,
836 LLVMMetadataRef Scope,
837 LLVMMetadataRef NS,
838 LLVMMetadataRef File,
839 unsigned Line) {
840 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
841 unwrapDI<DINamespace>(NS),
842 unwrapDI<DIFile>(File),
843 Line));
844}
845
846LLVMMetadataRef
847LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder,
848 LLVMMetadataRef Scope,
849 LLVMMetadataRef ImportedEntity,
850 LLVMMetadataRef File,
851 unsigned Line) {
852 return wrap(unwrap(Builder)->createImportedModule(
853 unwrapDI<DIScope>(Scope),
854 unwrapDI<DIImportedEntity>(ImportedEntity),
855 unwrapDI<DIFile>(File), Line));
856}
857
858LLVMMetadataRef
859LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder,
860 LLVMMetadataRef Scope,
861 LLVMMetadataRef M,
862 LLVMMetadataRef File,
863 unsigned Line) {
864 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
865 unwrapDI<DIModule>(M),
866 unwrapDI<DIFile>(File),
867 Line));
868}
869
870LLVMMetadataRef
871LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder,
872 LLVMMetadataRef Scope,
873 LLVMMetadataRef Decl,
874 LLVMMetadataRef File,
875 unsigned Line,
876 const char *Name, size_t NameLen) {
877 return wrap(unwrap(Builder)->createImportedDeclaration(
878 unwrapDI<DIScope>(Scope),
879 unwrapDI<DINode>(Decl),
880 unwrapDI<DIFile>(File), Line, {Name, NameLen}));
881}
882
883LLVMMetadataRef
whitequark789164d2017-11-01 22:18:52 +0000884LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
885 unsigned Column, LLVMMetadataRef Scope,
886 LLVMMetadataRef InlinedAt) {
887 return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
888 unwrap(InlinedAt)));
889}
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000890
Robert Widmann260b5812018-05-10 18:23:55 +0000891unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) {
892 return unwrapDI<DILocation>(Location)->getLine();
893}
894
895unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) {
896 return unwrapDI<DILocation>(Location)->getColumn();
897}
898
899LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) {
900 return wrap(unwrapDI<DILocation>(Location)->getScope());
901}
902
Robert Widmanncce47412019-04-10 14:19:05 +0000903LLVMMetadataRef LLVMDILocationGetInlinedAt(LLVMMetadataRef Location) {
904 return wrap(unwrapDI<DILocation>(Location)->getInlinedAt());
905}
906
Robert Widmannd909a5e2019-04-17 13:29:14 +0000907LLVMMetadataRef LLVMDIScopeGetFile(LLVMMetadataRef Scope) {
908 return wrap(unwrapDI<DIScope>(Scope)->getFile());
909}
910
911const char *LLVMDIFileGetDirectory(LLVMMetadataRef File, unsigned *Len) {
912 auto Dir = unwrapDI<DIFile>(File)->getDirectory();
913 *Len = Dir.size();
914 return Dir.data();
915}
916
917const char *LLVMDIFileGetFilename(LLVMMetadataRef File, unsigned *Len) {
918 auto Name = unwrapDI<DIFile>(File)->getFilename();
919 *Len = Name.size();
920 return Name.data();
921}
922
923const char *LLVMDIFileGetSource(LLVMMetadataRef File, unsigned *Len) {
924 if (auto Src = unwrapDI<DIFile>(File)->getSource()) {
925 *Len = Src->size();
926 return Src->data();
927 }
928 *Len = 0;
929 return "";
930}
931
Robert Widmanna82b6132019-02-17 21:25:47 +0000932LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder,
933 const char *Name, size_t NameLen,
934 int64_t Value,
935 LLVMBool IsUnsigned) {
936 return wrap(unwrap(Builder)->createEnumerator({Name, NameLen}, Value,
937 IsUnsigned != 0));
938}
939
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000940LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
941 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
942 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
Robert Widmann2d2698c2018-04-28 18:13:39 +0000943 uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000944 unsigned NumElements, LLVMMetadataRef ClassTy) {
945auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
946 NumElements});
947return wrap(unwrap(Builder)->createEnumerationType(
948 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
949 LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
950}
951
952LLVMMetadataRef LLVMDIBuilderCreateUnionType(
953 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
954 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
Robert Widmann2d2698c2018-04-28 18:13:39 +0000955 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000956 LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
957 const char *UniqueId, size_t UniqueIdLen) {
958 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
959 NumElements});
960 return wrap(unwrap(Builder)->createUnionType(
961 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
962 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
963 Elts, RunTimeLang, {UniqueId, UniqueIdLen}));
964}
965
966
967LLVMMetadataRef
Robert Widmann2d2698c2018-04-28 18:13:39 +0000968LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
969 uint32_t AlignInBits, LLVMMetadataRef Ty,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000970 LLVMMetadataRef *Subscripts,
971 unsigned NumSubscripts) {
972 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
973 NumSubscripts});
974 return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits,
975 unwrapDI<DIType>(Ty), Subs));
976}
977
978LLVMMetadataRef
Robert Widmann2d2698c2018-04-28 18:13:39 +0000979LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size,
980 uint32_t AlignInBits, LLVMMetadataRef Ty,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000981 LLVMMetadataRef *Subscripts,
982 unsigned NumSubscripts) {
983 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
984 NumSubscripts});
985 return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits,
986 unwrapDI<DIType>(Ty), Subs));
987}
988
989LLVMMetadataRef
990LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
Robert Widmann2d2698c2018-04-28 18:13:39 +0000991 size_t NameLen, uint64_t SizeInBits,
whitequarkb56a4d32018-08-19 23:39:47 +0000992 LLVMDWARFTypeEncoding Encoding,
993 LLVMDIFlags Flags) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000994 return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
whitequarkb56a4d32018-08-19 23:39:47 +0000995 SizeInBits, Encoding,
996 map_from_llvmDIFlags(Flags)));
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000997}
998
999LLVMMetadataRef LLVMDIBuilderCreatePointerType(
1000 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001001 uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001002 const char *Name, size_t NameLen) {
1003 return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
1004 SizeInBits, AlignInBits,
1005 AddressSpace, {Name, NameLen}));
1006}
1007
1008LLVMMetadataRef LLVMDIBuilderCreateStructType(
1009 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1010 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001011 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001012 LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
1013 unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
1014 const char *UniqueId, size_t UniqueIdLen) {
1015 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
1016 NumElements});
1017 return wrap(unwrap(Builder)->createStructType(
1018 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
1019 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
1020 unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
1021 unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
1022}
1023
1024LLVMMetadataRef LLVMDIBuilderCreateMemberType(
1025 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001026 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
1027 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001028 LLVMMetadataRef Ty) {
1029 return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
1030 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
1031 OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
1032}
1033
1034LLVMMetadataRef
1035LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
1036 size_t NameLen) {
1037 return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
1038}
1039
1040LLVMMetadataRef
1041LLVMDIBuilderCreateStaticMemberType(
1042 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1043 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1044 LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001045 uint32_t AlignInBits) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001046 return wrap(unwrap(Builder)->createStaticMemberType(
1047 unwrapDI<DIScope>(Scope), {Name, NameLen},
1048 unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
1049 map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
1050 AlignInBits));
1051}
1052
1053LLVMMetadataRef
Robert Widmann38fa7502018-05-21 16:27:35 +00001054LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder,
1055 const char *Name, size_t NameLen,
1056 LLVMMetadataRef File, unsigned LineNo,
1057 uint64_t SizeInBits, uint32_t AlignInBits,
1058 uint64_t OffsetInBits, LLVMDIFlags Flags,
1059 LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) {
1060 return wrap(unwrap(Builder)->createObjCIVar(
1061 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
1062 SizeInBits, AlignInBits, OffsetInBits,
1063 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty),
1064 unwrapDI<MDNode>(PropertyNode)));
1065}
1066
1067LLVMMetadataRef
1068LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,
1069 const char *Name, size_t NameLen,
1070 LLVMMetadataRef File, unsigned LineNo,
1071 const char *GetterName, size_t GetterNameLen,
1072 const char *SetterName, size_t SetterNameLen,
1073 unsigned PropertyAttributes,
1074 LLVMMetadataRef Ty) {
1075 return wrap(unwrap(Builder)->createObjCProperty(
1076 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
1077 {GetterName, GetterNameLen}, {SetterName, SetterNameLen},
1078 PropertyAttributes, unwrapDI<DIType>(Ty)));
1079}
1080
1081LLVMMetadataRef
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001082LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
1083 LLVMMetadataRef Type) {
1084 return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
1085}
1086
1087LLVMMetadataRef
Robert Widmann4b0084b2018-05-10 21:10:06 +00001088LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
1089 const char *Name, size_t NameLen,
1090 LLVMMetadataRef File, unsigned LineNo,
1091 LLVMMetadataRef Scope) {
1092 return wrap(unwrap(Builder)->createTypedef(
1093 unwrapDI<DIType>(Type), {Name, NameLen},
1094 unwrapDI<DIFile>(File), LineNo,
1095 unwrapDI<DIScope>(Scope)));
1096}
1097
1098LLVMMetadataRef
Robert Widmann38fa7502018-05-21 16:27:35 +00001099LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder,
1100 LLVMMetadataRef Ty, LLVMMetadataRef BaseTy,
1101 uint64_t BaseOffset, uint32_t VBPtrOffset,
1102 LLVMDIFlags Flags) {
1103 return wrap(unwrap(Builder)->createInheritance(
1104 unwrapDI<DIType>(Ty), unwrapDI<DIType>(BaseTy),
1105 BaseOffset, VBPtrOffset, map_from_llvmDIFlags(Flags)));
1106}
1107
1108LLVMMetadataRef
Robert Widmannb02fe642018-04-23 13:51:43 +00001109LLVMDIBuilderCreateForwardDecl(
1110 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1111 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001112 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
Robert Widmannb02fe642018-04-23 13:51:43 +00001113 const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1114 return wrap(unwrap(Builder)->createForwardDecl(
1115 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
1116 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
1117 AlignInBits, {UniqueIdentifier, UniqueIdentifierLen}));
1118}
1119
1120LLVMMetadataRef
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001121LLVMDIBuilderCreateReplaceableCompositeType(
Harlan Haskinsbee4b582018-04-02 19:11:44 +00001122 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1123 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001124 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001125 LLVMDIFlags Flags, const char *UniqueIdentifier,
Harlan Haskinsbee4b582018-04-02 19:11:44 +00001126 size_t UniqueIdentifierLen) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001127 return wrap(unwrap(Builder)->createReplaceableCompositeType(
1128 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
1129 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
1130 AlignInBits, map_from_llvmDIFlags(Flags),
1131 {UniqueIdentifier, UniqueIdentifierLen}));
1132}
1133
1134LLVMMetadataRef
1135LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
1136 LLVMMetadataRef Type) {
1137 return wrap(unwrap(Builder)->createQualifiedType(Tag,
1138 unwrapDI<DIType>(Type)));
1139}
1140
1141LLVMMetadataRef
1142LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
1143 LLVMMetadataRef Type) {
1144 return wrap(unwrap(Builder)->createReferenceType(Tag,
1145 unwrapDI<DIType>(Type)));
1146}
1147
1148LLVMMetadataRef
1149LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
1150 return wrap(unwrap(Builder)->createNullPtrType());
1151}
1152
1153LLVMMetadataRef
1154LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
1155 LLVMMetadataRef PointeeType,
1156 LLVMMetadataRef ClassType,
Robert Widmann2d2698c2018-04-28 18:13:39 +00001157 uint64_t SizeInBits,
1158 uint32_t AlignInBits,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001159 LLVMDIFlags Flags) {
1160 return wrap(unwrap(Builder)->createMemberPointerType(
1161 unwrapDI<DIType>(PointeeType),
1162 unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
1163 map_from_llvmDIFlags(Flags)));
1164}
1165
1166LLVMMetadataRef
Robert Widmann2d2698c2018-04-28 18:13:39 +00001167LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,
1168 LLVMMetadataRef Scope,
1169 const char *Name, size_t NameLen,
1170 LLVMMetadataRef File, unsigned LineNumber,
1171 uint64_t SizeInBits,
1172 uint64_t OffsetInBits,
1173 uint64_t StorageOffsetInBits,
1174 LLVMDIFlags Flags, LLVMMetadataRef Type) {
1175 return wrap(unwrap(Builder)->createBitFieldMemberType(
1176 unwrapDI<DIScope>(Scope), {Name, NameLen},
1177 unwrapDI<DIFile>(File), LineNumber,
1178 SizeInBits, OffsetInBits, StorageOffsetInBits,
1179 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type)));
1180}
1181
1182LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,
1183 LLVMMetadataRef Scope, const char *Name, size_t NameLen,
1184 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
1185 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
1186 LLVMMetadataRef DerivedFrom,
1187 LLVMMetadataRef *Elements, unsigned NumElements,
1188 LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode,
1189 const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1190 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
1191 NumElements});
1192 return wrap(unwrap(Builder)->createClassType(
1193 unwrapDI<DIScope>(Scope), {Name, NameLen},
1194 unwrapDI<DIFile>(File), LineNumber,
1195 SizeInBits, AlignInBits, OffsetInBits,
1196 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom),
1197 Elts, unwrapDI<DIType>(VTableHolder),
1198 unwrapDI<MDNode>(TemplateParamsNode),
1199 {UniqueIdentifier, UniqueIdentifierLen}));
1200}
1201
1202LLVMMetadataRef
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001203LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
1204 LLVMMetadataRef Type) {
1205 return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
1206}
1207
Robert Widmann260b5812018-05-10 18:23:55 +00001208const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
1209 StringRef Str = unwrap<DIType>(DType)->getName();
1210 *Length = Str.size();
1211 return Str.data();
1212}
1213
1214uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) {
1215 return unwrapDI<DIType>(DType)->getSizeInBits();
1216}
1217
1218uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) {
1219 return unwrapDI<DIType>(DType)->getOffsetInBits();
1220}
1221
1222uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) {
1223 return unwrapDI<DIType>(DType)->getAlignInBits();
1224}
1225
1226unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) {
1227 return unwrapDI<DIType>(DType)->getLine();
1228}
1229
1230LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) {
1231 return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags());
1232}
1233
Robert Widmann6978db72018-04-23 14:29:33 +00001234LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,
1235 LLVMMetadataRef *Types,
1236 size_t Length) {
1237 return wrap(
1238 unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get());
1239}
1240
Harlan Haskinsb7881bb2018-04-02 00:17:40 +00001241LLVMMetadataRef
1242LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
1243 LLVMMetadataRef File,
1244 LLVMMetadataRef *ParameterTypes,
1245 unsigned NumParameterTypes,
1246 LLVMDIFlags Flags) {
1247 auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
1248 NumParameterTypes});
1249 return wrap(unwrap(Builder)->createSubroutineType(
1250 Elts, map_from_llvmDIFlags(Flags)));
1251}
Robert Widmannf53050f2018-04-07 06:07:55 +00001252
Robert Widmann12e367b2018-04-22 19:24:44 +00001253LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
1254 int64_t *Addr, size_t Length) {
1255 return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr,
1256 Length)));
1257}
1258
Robert Widmann21fc15d2018-04-23 22:31:49 +00001259LLVMMetadataRef
1260LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,
1261 int64_t Value) {
1262 return wrap(unwrap(Builder)->createConstantValueExpression(Value));
1263}
1264
Matthew Vossf8ab35a2018-10-03 18:44:53 +00001265LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression(
1266 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1267 size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File,
1268 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
1269 LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) {
Robert Widmann21fc15d2018-04-23 22:31:49 +00001270 return wrap(unwrap(Builder)->createGlobalVariableExpression(
Matthew Vossf8ab35a2018-10-03 18:44:53 +00001271 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen},
1272 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
1273 unwrap<DIExpression>(Expr), unwrapDI<MDNode>(Decl),
1274 nullptr, AlignInBits));
Robert Widmann21fc15d2018-04-23 22:31:49 +00001275}
1276
Robert Widmannd6eb4bb2019-04-16 21:39:48 +00001277LLVMMetadataRef LLVMDIGlobalVariableExpressionGetVariable(LLVMMetadataRef GVE) {
1278 return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getVariable());
1279}
1280
1281LLVMMetadataRef LLVMDIGlobalVariableExpressionGetExpression(
1282 LLVMMetadataRef GVE) {
1283 return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getExpression());
1284}
1285
1286LLVMMetadataRef LLVMDIVariableGetFile(LLVMMetadataRef Var) {
1287 return wrap(unwrapDI<DIVariable>(Var)->getFile());
1288}
1289
1290LLVMMetadataRef LLVMDIVariableGetScope(LLVMMetadataRef Var) {
1291 return wrap(unwrapDI<DIVariable>(Var)->getScope());
1292}
1293
1294unsigned LLVMDIVariableGetLine(LLVMMetadataRef Var) {
1295 return unwrapDI<DIVariable>(Var)->getLine();
1296}
1297
Robert Widmanna428eba2018-05-10 18:09:53 +00001298LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
1299 size_t Count) {
1300 return wrap(
1301 MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release());
1302}
1303
1304void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) {
1305 MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode));
1306}
1307
1308void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,
1309 LLVMMetadataRef Replacement) {
1310 auto *Node = unwrapDI<MDNode>(TargetMetadata);
1311 Node->replaceAllUsesWith(unwrap<Metadata>(Replacement));
1312 MDNode::deleteTemporary(Node);
1313}
1314
Matthew Vossf8ab35a2018-10-03 18:44:53 +00001315LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl(
1316 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1317 size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File,
1318 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
1319 LLVMMetadataRef Decl, uint32_t AlignInBits) {
Robert Widmann21fc15d2018-04-23 22:31:49 +00001320 return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl(
Matthew Vossf8ab35a2018-10-03 18:44:53 +00001321 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen},
1322 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
1323 unwrapDI<MDNode>(Decl), nullptr, AlignInBits));
Robert Widmann21fc15d2018-04-23 22:31:49 +00001324}
1325
Matthew Vossf8ab35a2018-10-03 18:44:53 +00001326LLVMValueRef
1327LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder, LLVMValueRef Storage,
1328 LLVMMetadataRef VarInfo, LLVMMetadataRef Expr,
1329 LLVMMetadataRef DL, LLVMValueRef Instr) {
Robert Widmann12e367b2018-04-22 19:24:44 +00001330 return wrap(unwrap(Builder)->insertDeclare(
1331 unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1332 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1333 unwrap<Instruction>(Instr)));
1334}
1335
1336LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
1337 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1338 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
1339 return wrap(unwrap(Builder)->insertDeclare(
1340 unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1341 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1342 unwrap(Block)));
1343}
1344
Robert Widmann21fc15d2018-04-23 22:31:49 +00001345LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder,
1346 LLVMValueRef Val,
1347 LLVMMetadataRef VarInfo,
1348 LLVMMetadataRef Expr,
1349 LLVMMetadataRef DebugLoc,
1350 LLVMValueRef Instr) {
1351 return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1352 unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1353 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1354 unwrap<Instruction>(Instr)));
1355}
1356
1357LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder,
1358 LLVMValueRef Val,
1359 LLVMMetadataRef VarInfo,
1360 LLVMMetadataRef Expr,
1361 LLVMMetadataRef DebugLoc,
1362 LLVMBasicBlockRef Block) {
1363 return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1364 unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1365 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1366 unwrap(Block)));
1367}
1368
Robert Widmann12e367b2018-04-22 19:24:44 +00001369LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
1370 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1371 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
1372 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
1373 return wrap(unwrap(Builder)->createAutoVariable(
1374 unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File),
1375 LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1376 map_from_llvmDIFlags(Flags), AlignInBits));
1377}
1378
1379LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
1380 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1381 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
1382 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
1383 return wrap(unwrap(Builder)->createParameterVariable(
Robert Widmann106eab02018-08-25 19:54:39 +00001384 unwrap<DIScope>(Scope), {Name, NameLen}, ArgNo, unwrap<DIFile>(File),
Robert Widmann12e367b2018-04-22 19:24:44 +00001385 LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1386 map_from_llvmDIFlags(Flags)));
1387}
1388
Robert Widmann6978db72018-04-23 14:29:33 +00001389LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,
1390 int64_t Lo, int64_t Count) {
1391 return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count));
1392}
1393
1394LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
1395 LLVMMetadataRef *Data,
1396 size_t Length) {
1397 Metadata **DataValue = unwrap(Data);
1398 return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get());
1399}
1400
Robert Widmannf53050f2018-04-07 06:07:55 +00001401LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
1402 return wrap(unwrap<Function>(Func)->getSubprogram());
1403}
1404
1405void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
1406 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
1407}
Robert Widmannabda7ee2018-10-01 13:15:09 +00001408
Robert Widmannd6eb4bb2019-04-16 21:39:48 +00001409unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram) {
1410 return unwrapDI<DISubprogram>(Subprogram)->getLine();
1411}
1412
Robert Widmannbec0a452019-04-09 22:27:51 +00001413LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst) {
1414 return wrap(unwrap<Instruction>(Inst)->getDebugLoc().getAsMDNode());
1415}
1416
1417void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc) {
1418 if (Loc)
1419 unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc(unwrap<MDNode>(Loc)));
1420 else
1421 unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc());
1422}
1423
Robert Widmannabda7ee2018-10-01 13:15:09 +00001424LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) {
1425 switch(unwrap(Metadata)->getMetadataID()) {
1426#define HANDLE_METADATA_LEAF(CLASS) \
1427 case Metadata::CLASS##Kind: \
1428 return (LLVMMetadataKind)LLVM##CLASS##MetadataKind;
1429#include "llvm/IR/Metadata.def"
1430 default:
1431 return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind;
1432 }
1433}