Remarks.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. /*===-- llvm-c/Remarks.h - Remarks Public C Interface -------------*- C -*-===*\
  7. |* *|
  8. |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
  9. |* Exceptions. *|
  10. |* See https://llvm.org/LICENSE.txt for license information. *|
  11. |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
  12. |* *|
  13. |*===----------------------------------------------------------------------===*|
  14. |* *|
  15. |* This header provides a public interface to a remark diagnostics library. *|
  16. |* LLVM provides an implementation of this interface. *|
  17. |* *|
  18. \*===----------------------------------------------------------------------===*/
  19. #ifndef LLVM_C_REMARKS_H
  20. #define LLVM_C_REMARKS_H
  21. #include "llvm-c/ExternC.h"
  22. #include "llvm-c/Types.h"
  23. #ifdef __cplusplus
  24. #include <cstddef>
  25. #else
  26. #include <stddef.h>
  27. #endif /* !defined(__cplusplus) */
  28. LLVM_C_EXTERN_C_BEGIN
  29. /**
  30. * @defgroup LLVMCREMARKS Remarks
  31. * @ingroup LLVMC
  32. *
  33. * @{
  34. */
  35. // 0 -> 1: Bitstream remarks support.
  36. #define REMARKS_API_VERSION 1
  37. /**
  38. * The type of the emitted remark.
  39. */
  40. enum LLVMRemarkType {
  41. LLVMRemarkTypeUnknown,
  42. LLVMRemarkTypePassed,
  43. LLVMRemarkTypeMissed,
  44. LLVMRemarkTypeAnalysis,
  45. LLVMRemarkTypeAnalysisFPCommute,
  46. LLVMRemarkTypeAnalysisAliasing,
  47. LLVMRemarkTypeFailure
  48. };
  49. /**
  50. * String containing a buffer and a length. The buffer is not guaranteed to be
  51. * zero-terminated.
  52. *
  53. * \since REMARKS_API_VERSION=0
  54. */
  55. typedef struct LLVMRemarkOpaqueString *LLVMRemarkStringRef;
  56. /**
  57. * Returns the buffer holding the string.
  58. *
  59. * \since REMARKS_API_VERSION=0
  60. */
  61. extern const char *LLVMRemarkStringGetData(LLVMRemarkStringRef String);
  62. /**
  63. * Returns the size of the string.
  64. *
  65. * \since REMARKS_API_VERSION=0
  66. */
  67. extern uint32_t LLVMRemarkStringGetLen(LLVMRemarkStringRef String);
  68. /**
  69. * DebugLoc containing File, Line and Column.
  70. *
  71. * \since REMARKS_API_VERSION=0
  72. */
  73. typedef struct LLVMRemarkOpaqueDebugLoc *LLVMRemarkDebugLocRef;
  74. /**
  75. * Return the path to the source file for a debug location.
  76. *
  77. * \since REMARKS_API_VERSION=0
  78. */
  79. extern LLVMRemarkStringRef
  80. LLVMRemarkDebugLocGetSourceFilePath(LLVMRemarkDebugLocRef DL);
  81. /**
  82. * Return the line in the source file for a debug location.
  83. *
  84. * \since REMARKS_API_VERSION=0
  85. */
  86. extern uint32_t LLVMRemarkDebugLocGetSourceLine(LLVMRemarkDebugLocRef DL);
  87. /**
  88. * Return the column in the source file for a debug location.
  89. *
  90. * \since REMARKS_API_VERSION=0
  91. */
  92. extern uint32_t LLVMRemarkDebugLocGetSourceColumn(LLVMRemarkDebugLocRef DL);
  93. /**
  94. * Element of the "Args" list. The key might give more information about what
  95. * the semantics of the value are, e.g. "Callee" will tell you that the value
  96. * is a symbol that names a function.
  97. *
  98. * \since REMARKS_API_VERSION=0
  99. */
  100. typedef struct LLVMRemarkOpaqueArg *LLVMRemarkArgRef;
  101. /**
  102. * Returns the key of an argument. The key defines what the value is, and the
  103. * same key can appear multiple times in the list of arguments.
  104. *
  105. * \since REMARKS_API_VERSION=0
  106. */
  107. extern LLVMRemarkStringRef LLVMRemarkArgGetKey(LLVMRemarkArgRef Arg);
  108. /**
  109. * Returns the value of an argument. This is a string that can contain newlines.
  110. *
  111. * \since REMARKS_API_VERSION=0
  112. */
  113. extern LLVMRemarkStringRef LLVMRemarkArgGetValue(LLVMRemarkArgRef Arg);
  114. /**
  115. * Returns the debug location that is attached to the value of this argument.
  116. *
  117. * If there is no debug location, the return value will be `NULL`.
  118. *
  119. * \since REMARKS_API_VERSION=0
  120. */
  121. extern LLVMRemarkDebugLocRef LLVMRemarkArgGetDebugLoc(LLVMRemarkArgRef Arg);
  122. /**
  123. * A remark emitted by the compiler.
  124. *
  125. * \since REMARKS_API_VERSION=0
  126. */
  127. typedef struct LLVMRemarkOpaqueEntry *LLVMRemarkEntryRef;
  128. /**
  129. * Free the resources used by the remark entry.
  130. *
  131. * \since REMARKS_API_VERSION=0
  132. */
  133. extern void LLVMRemarkEntryDispose(LLVMRemarkEntryRef Remark);
  134. /**
  135. * The type of the remark. For example, it can allow users to only keep the
  136. * missed optimizations from the compiler.
  137. *
  138. * \since REMARKS_API_VERSION=0
  139. */
  140. extern enum LLVMRemarkType LLVMRemarkEntryGetType(LLVMRemarkEntryRef Remark);
  141. /**
  142. * Get the name of the pass that emitted this remark.
  143. *
  144. * \since REMARKS_API_VERSION=0
  145. */
  146. extern LLVMRemarkStringRef
  147. LLVMRemarkEntryGetPassName(LLVMRemarkEntryRef Remark);
  148. /**
  149. * Get an identifier of the remark.
  150. *
  151. * \since REMARKS_API_VERSION=0
  152. */
  153. extern LLVMRemarkStringRef
  154. LLVMRemarkEntryGetRemarkName(LLVMRemarkEntryRef Remark);
  155. /**
  156. * Get the name of the function being processed when the remark was emitted.
  157. *
  158. * \since REMARKS_API_VERSION=0
  159. */
  160. extern LLVMRemarkStringRef
  161. LLVMRemarkEntryGetFunctionName(LLVMRemarkEntryRef Remark);
  162. /**
  163. * Returns the debug location that is attached to this remark.
  164. *
  165. * If there is no debug location, the return value will be `NULL`.
  166. *
  167. * \since REMARKS_API_VERSION=0
  168. */
  169. extern LLVMRemarkDebugLocRef
  170. LLVMRemarkEntryGetDebugLoc(LLVMRemarkEntryRef Remark);
  171. /**
  172. * Return the hotness of the remark.
  173. *
  174. * A hotness of `0` means this value is not set.
  175. *
  176. * \since REMARKS_API_VERSION=0
  177. */
  178. extern uint64_t LLVMRemarkEntryGetHotness(LLVMRemarkEntryRef Remark);
  179. /**
  180. * The number of arguments the remark holds.
  181. *
  182. * \since REMARKS_API_VERSION=0
  183. */
  184. extern uint32_t LLVMRemarkEntryGetNumArgs(LLVMRemarkEntryRef Remark);
  185. /**
  186. * Get a new iterator to iterate over a remark's argument.
  187. *
  188. * If there are no arguments in \p Remark, the return value will be `NULL`.
  189. *
  190. * The lifetime of the returned value is bound to the lifetime of \p Remark.
  191. *
  192. * \since REMARKS_API_VERSION=0
  193. */
  194. extern LLVMRemarkArgRef LLVMRemarkEntryGetFirstArg(LLVMRemarkEntryRef Remark);
  195. /**
  196. * Get the next argument in \p Remark from the position of \p It.
  197. *
  198. * Returns `NULL` if there are no more arguments available.
  199. *
  200. * The lifetime of the returned value is bound to the lifetime of \p Remark.
  201. *
  202. * \since REMARKS_API_VERSION=0
  203. */
  204. extern LLVMRemarkArgRef LLVMRemarkEntryGetNextArg(LLVMRemarkArgRef It,
  205. LLVMRemarkEntryRef Remark);
  206. typedef struct LLVMRemarkOpaqueParser *LLVMRemarkParserRef;
  207. /**
  208. * Creates a remark parser that can be used to parse the buffer located in \p
  209. * Buf of size \p Size bytes.
  210. *
  211. * \p Buf cannot be `NULL`.
  212. *
  213. * This function should be paired with LLVMRemarkParserDispose() to avoid
  214. * leaking resources.
  215. *
  216. * \since REMARKS_API_VERSION=0
  217. */
  218. extern LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
  219. uint64_t Size);
  220. /**
  221. * Creates a remark parser that can be used to parse the buffer located in \p
  222. * Buf of size \p Size bytes.
  223. *
  224. * \p Buf cannot be `NULL`.
  225. *
  226. * This function should be paired with LLVMRemarkParserDispose() to avoid
  227. * leaking resources.
  228. *
  229. * \since REMARKS_API_VERSION=1
  230. */
  231. extern LLVMRemarkParserRef LLVMRemarkParserCreateBitstream(const void *Buf,
  232. uint64_t Size);
  233. /**
  234. * Returns the next remark in the file.
  235. *
  236. * The value pointed to by the return value needs to be disposed using a call to
  237. * LLVMRemarkEntryDispose().
  238. *
  239. * All the entries in the returned value that are of LLVMRemarkStringRef type
  240. * will become invalidated once a call to LLVMRemarkParserDispose is made.
  241. *
  242. * If the parser reaches the end of the buffer, the return value will be `NULL`.
  243. *
  244. * In the case of an error, the return value will be `NULL`, and:
  245. *
  246. * 1) LLVMRemarkParserHasError() will return `1`.
  247. *
  248. * 2) LLVMRemarkParserGetErrorMessage() will return a descriptive error
  249. * message.
  250. *
  251. * An error may occur if:
  252. *
  253. * 1) An argument is invalid.
  254. *
  255. * 2) There is a parsing error. This can occur on things like malformed YAML.
  256. *
  257. * 3) There is a Remark semantic error. This can occur on well-formed files with
  258. * missing or extra fields.
  259. *
  260. * Here is a quick example of the usage:
  261. *
  262. * ```
  263. * LLVMRemarkParserRef Parser = LLVMRemarkParserCreateYAML(Buf, Size);
  264. * LLVMRemarkEntryRef Remark = NULL;
  265. * while ((Remark = LLVMRemarkParserGetNext(Parser))) {
  266. * // use Remark
  267. * LLVMRemarkEntryDispose(Remark); // Release memory.
  268. * }
  269. * bool HasError = LLVMRemarkParserHasError(Parser);
  270. * LLVMRemarkParserDispose(Parser);
  271. * ```
  272. *
  273. * \since REMARKS_API_VERSION=0
  274. */
  275. extern LLVMRemarkEntryRef LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser);
  276. /**
  277. * Returns `1` if the parser encountered an error while parsing the buffer.
  278. *
  279. * \since REMARKS_API_VERSION=0
  280. */
  281. extern LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser);
  282. /**
  283. * Returns a null-terminated string containing an error message.
  284. *
  285. * In case of no error, the result is `NULL`.
  286. *
  287. * The memory of the string is bound to the lifetime of \p Parser. If
  288. * LLVMRemarkParserDispose() is called, the memory of the string will be
  289. * released.
  290. *
  291. * \since REMARKS_API_VERSION=0
  292. */
  293. extern const char *LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser);
  294. /**
  295. * Releases all the resources used by \p Parser.
  296. *
  297. * \since REMARKS_API_VERSION=0
  298. */
  299. extern void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser);
  300. /**
  301. * Returns the version of the remarks library.
  302. *
  303. * \since REMARKS_API_VERSION=0
  304. */
  305. extern uint32_t LLVMRemarkVersion(void);
  306. /**
  307. * @} // endgoup LLVMCREMARKS
  308. */
  309. LLVM_C_EXTERN_C_END
  310. #endif /* LLVM_C_REMARKS_H */
  311. #ifdef __GNUC__
  312. #pragma GCC diagnostic pop
  313. #endif