DebugInfo.cpp 60 KB

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