DebugInfo.cpp 56 KB

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