CXDiagnostic.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. /*===-- clang-c/CXDiagnostic.h - C Index Diagnostics --------------*- 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 the interface to C Index diagnostics. *|
  16. |* *|
  17. \*===----------------------------------------------------------------------===*/
  18. #ifndef LLVM_CLANG_C_CXDIAGNOSTIC_H
  19. #define LLVM_CLANG_C_CXDIAGNOSTIC_H
  20. #include "clang-c/CXSourceLocation.h"
  21. #include "clang-c/CXString.h"
  22. #include "clang-c/ExternC.h"
  23. #include "clang-c/Platform.h"
  24. LLVM_CLANG_C_EXTERN_C_BEGIN
  25. /**
  26. * \defgroup CINDEX_DIAG Diagnostic reporting
  27. *
  28. * @{
  29. */
  30. /**
  31. * Describes the severity of a particular diagnostic.
  32. */
  33. enum CXDiagnosticSeverity {
  34. /**
  35. * A diagnostic that has been suppressed, e.g., by a command-line
  36. * option.
  37. */
  38. CXDiagnostic_Ignored = 0,
  39. /**
  40. * This diagnostic is a note that should be attached to the
  41. * previous (non-note) diagnostic.
  42. */
  43. CXDiagnostic_Note = 1,
  44. /**
  45. * This diagnostic indicates suspicious code that may not be
  46. * wrong.
  47. */
  48. CXDiagnostic_Warning = 2,
  49. /**
  50. * This diagnostic indicates that the code is ill-formed.
  51. */
  52. CXDiagnostic_Error = 3,
  53. /**
  54. * This diagnostic indicates that the code is ill-formed such
  55. * that future parser recovery is unlikely to produce useful
  56. * results.
  57. */
  58. CXDiagnostic_Fatal = 4
  59. };
  60. /**
  61. * A single diagnostic, containing the diagnostic's severity,
  62. * location, text, source ranges, and fix-it hints.
  63. */
  64. typedef void *CXDiagnostic;
  65. /**
  66. * A group of CXDiagnostics.
  67. */
  68. typedef void *CXDiagnosticSet;
  69. /**
  70. * Determine the number of diagnostics in a CXDiagnosticSet.
  71. */
  72. CINDEX_LINKAGE unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags);
  73. /**
  74. * Retrieve a diagnostic associated with the given CXDiagnosticSet.
  75. *
  76. * \param Diags the CXDiagnosticSet to query.
  77. * \param Index the zero-based diagnostic number to retrieve.
  78. *
  79. * \returns the requested diagnostic. This diagnostic must be freed
  80. * via a call to \c clang_disposeDiagnostic().
  81. */
  82. CINDEX_LINKAGE CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags,
  83. unsigned Index);
  84. /**
  85. * Describes the kind of error that occurred (if any) in a call to
  86. * \c clang_loadDiagnostics.
  87. */
  88. enum CXLoadDiag_Error {
  89. /**
  90. * Indicates that no error occurred.
  91. */
  92. CXLoadDiag_None = 0,
  93. /**
  94. * Indicates that an unknown error occurred while attempting to
  95. * deserialize diagnostics.
  96. */
  97. CXLoadDiag_Unknown = 1,
  98. /**
  99. * Indicates that the file containing the serialized diagnostics
  100. * could not be opened.
  101. */
  102. CXLoadDiag_CannotLoad = 2,
  103. /**
  104. * Indicates that the serialized diagnostics file is invalid or
  105. * corrupt.
  106. */
  107. CXLoadDiag_InvalidFile = 3
  108. };
  109. /**
  110. * Deserialize a set of diagnostics from a Clang diagnostics bitcode
  111. * file.
  112. *
  113. * \param file The name of the file to deserialize.
  114. * \param error A pointer to a enum value recording if there was a problem
  115. * deserializing the diagnostics.
  116. * \param errorString A pointer to a CXString for recording the error string
  117. * if the file was not successfully loaded.
  118. *
  119. * \returns A loaded CXDiagnosticSet if successful, and NULL otherwise. These
  120. * diagnostics should be released using clang_disposeDiagnosticSet().
  121. */
  122. CINDEX_LINKAGE CXDiagnosticSet clang_loadDiagnostics(
  123. const char *file, enum CXLoadDiag_Error *error, CXString *errorString);
  124. /**
  125. * Release a CXDiagnosticSet and all of its contained diagnostics.
  126. */
  127. CINDEX_LINKAGE void clang_disposeDiagnosticSet(CXDiagnosticSet Diags);
  128. /**
  129. * Retrieve the child diagnostics of a CXDiagnostic.
  130. *
  131. * This CXDiagnosticSet does not need to be released by
  132. * clang_disposeDiagnosticSet.
  133. */
  134. CINDEX_LINKAGE CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic D);
  135. /**
  136. * Destroy a diagnostic.
  137. */
  138. CINDEX_LINKAGE void clang_disposeDiagnostic(CXDiagnostic Diagnostic);
  139. /**
  140. * Options to control the display of diagnostics.
  141. *
  142. * The values in this enum are meant to be combined to customize the
  143. * behavior of \c clang_formatDiagnostic().
  144. */
  145. enum CXDiagnosticDisplayOptions {
  146. /**
  147. * Display the source-location information where the
  148. * diagnostic was located.
  149. *
  150. * When set, diagnostics will be prefixed by the file, line, and
  151. * (optionally) column to which the diagnostic refers. For example,
  152. *
  153. * \code
  154. * test.c:28: warning: extra tokens at end of #endif directive
  155. * \endcode
  156. *
  157. * This option corresponds to the clang flag \c -fshow-source-location.
  158. */
  159. CXDiagnostic_DisplaySourceLocation = 0x01,
  160. /**
  161. * If displaying the source-location information of the
  162. * diagnostic, also include the column number.
  163. *
  164. * This option corresponds to the clang flag \c -fshow-column.
  165. */
  166. CXDiagnostic_DisplayColumn = 0x02,
  167. /**
  168. * If displaying the source-location information of the
  169. * diagnostic, also include information about source ranges in a
  170. * machine-parsable format.
  171. *
  172. * This option corresponds to the clang flag
  173. * \c -fdiagnostics-print-source-range-info.
  174. */
  175. CXDiagnostic_DisplaySourceRanges = 0x04,
  176. /**
  177. * Display the option name associated with this diagnostic, if any.
  178. *
  179. * The option name displayed (e.g., -Wconversion) will be placed in brackets
  180. * after the diagnostic text. This option corresponds to the clang flag
  181. * \c -fdiagnostics-show-option.
  182. */
  183. CXDiagnostic_DisplayOption = 0x08,
  184. /**
  185. * Display the category number associated with this diagnostic, if any.
  186. *
  187. * The category number is displayed within brackets after the diagnostic text.
  188. * This option corresponds to the clang flag
  189. * \c -fdiagnostics-show-category=id.
  190. */
  191. CXDiagnostic_DisplayCategoryId = 0x10,
  192. /**
  193. * Display the category name associated with this diagnostic, if any.
  194. *
  195. * The category name is displayed within brackets after the diagnostic text.
  196. * This option corresponds to the clang flag
  197. * \c -fdiagnostics-show-category=name.
  198. */
  199. CXDiagnostic_DisplayCategoryName = 0x20
  200. };
  201. /**
  202. * Format the given diagnostic in a manner that is suitable for display.
  203. *
  204. * This routine will format the given diagnostic to a string, rendering
  205. * the diagnostic according to the various options given. The
  206. * \c clang_defaultDiagnosticDisplayOptions() function returns the set of
  207. * options that most closely mimics the behavior of the clang compiler.
  208. *
  209. * \param Diagnostic The diagnostic to print.
  210. *
  211. * \param Options A set of options that control the diagnostic display,
  212. * created by combining \c CXDiagnosticDisplayOptions values.
  213. *
  214. * \returns A new string containing for formatted diagnostic.
  215. */
  216. CINDEX_LINKAGE CXString clang_formatDiagnostic(CXDiagnostic Diagnostic,
  217. unsigned Options);
  218. /**
  219. * Retrieve the set of display options most similar to the
  220. * default behavior of the clang compiler.
  221. *
  222. * \returns A set of display options suitable for use with \c
  223. * clang_formatDiagnostic().
  224. */
  225. CINDEX_LINKAGE unsigned clang_defaultDiagnosticDisplayOptions(void);
  226. /**
  227. * Determine the severity of the given diagnostic.
  228. */
  229. CINDEX_LINKAGE enum CXDiagnosticSeverity
  230. clang_getDiagnosticSeverity(CXDiagnostic);
  231. /**
  232. * Retrieve the source location of the given diagnostic.
  233. *
  234. * This location is where Clang would print the caret ('^') when
  235. * displaying the diagnostic on the command line.
  236. */
  237. CINDEX_LINKAGE CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic);
  238. /**
  239. * Retrieve the text of the given diagnostic.
  240. */
  241. CINDEX_LINKAGE CXString clang_getDiagnosticSpelling(CXDiagnostic);
  242. /**
  243. * Retrieve the name of the command-line option that enabled this
  244. * diagnostic.
  245. *
  246. * \param Diag The diagnostic to be queried.
  247. *
  248. * \param Disable If non-NULL, will be set to the option that disables this
  249. * diagnostic (if any).
  250. *
  251. * \returns A string that contains the command-line option used to enable this
  252. * warning, such as "-Wconversion" or "-pedantic".
  253. */
  254. CINDEX_LINKAGE CXString clang_getDiagnosticOption(CXDiagnostic Diag,
  255. CXString *Disable);
  256. /**
  257. * Retrieve the category number for this diagnostic.
  258. *
  259. * Diagnostics can be categorized into groups along with other, related
  260. * diagnostics (e.g., diagnostics under the same warning flag). This routine
  261. * retrieves the category number for the given diagnostic.
  262. *
  263. * \returns The number of the category that contains this diagnostic, or zero
  264. * if this diagnostic is uncategorized.
  265. */
  266. CINDEX_LINKAGE unsigned clang_getDiagnosticCategory(CXDiagnostic);
  267. /**
  268. * Retrieve the name of a particular diagnostic category. This
  269. * is now deprecated. Use clang_getDiagnosticCategoryText()
  270. * instead.
  271. *
  272. * \param Category A diagnostic category number, as returned by
  273. * \c clang_getDiagnosticCategory().
  274. *
  275. * \returns The name of the given diagnostic category.
  276. */
  277. CINDEX_DEPRECATED CINDEX_LINKAGE CXString
  278. clang_getDiagnosticCategoryName(unsigned Category);
  279. /**
  280. * Retrieve the diagnostic category text for a given diagnostic.
  281. *
  282. * \returns The text of the given diagnostic category.
  283. */
  284. CINDEX_LINKAGE CXString clang_getDiagnosticCategoryText(CXDiagnostic);
  285. /**
  286. * Determine the number of source ranges associated with the given
  287. * diagnostic.
  288. */
  289. CINDEX_LINKAGE unsigned clang_getDiagnosticNumRanges(CXDiagnostic);
  290. /**
  291. * Retrieve a source range associated with the diagnostic.
  292. *
  293. * A diagnostic's source ranges highlight important elements in the source
  294. * code. On the command line, Clang displays source ranges by
  295. * underlining them with '~' characters.
  296. *
  297. * \param Diagnostic the diagnostic whose range is being extracted.
  298. *
  299. * \param Range the zero-based index specifying which range to
  300. *
  301. * \returns the requested source range.
  302. */
  303. CINDEX_LINKAGE CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diagnostic,
  304. unsigned Range);
  305. /**
  306. * Determine the number of fix-it hints associated with the
  307. * given diagnostic.
  308. */
  309. CINDEX_LINKAGE unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diagnostic);
  310. /**
  311. * Retrieve the replacement information for a given fix-it.
  312. *
  313. * Fix-its are described in terms of a source range whose contents
  314. * should be replaced by a string. This approach generalizes over
  315. * three kinds of operations: removal of source code (the range covers
  316. * the code to be removed and the replacement string is empty),
  317. * replacement of source code (the range covers the code to be
  318. * replaced and the replacement string provides the new code), and
  319. * insertion (both the start and end of the range point at the
  320. * insertion location, and the replacement string provides the text to
  321. * insert).
  322. *
  323. * \param Diagnostic The diagnostic whose fix-its are being queried.
  324. *
  325. * \param FixIt The zero-based index of the fix-it.
  326. *
  327. * \param ReplacementRange The source range whose contents will be
  328. * replaced with the returned replacement string. Note that source
  329. * ranges are half-open ranges [a, b), so the source code should be
  330. * replaced from a and up to (but not including) b.
  331. *
  332. * \returns A string containing text that should be replace the source
  333. * code indicated by the \c ReplacementRange.
  334. */
  335. CINDEX_LINKAGE CXString clang_getDiagnosticFixIt(
  336. CXDiagnostic Diagnostic, unsigned FixIt, CXSourceRange *ReplacementRange);
  337. /**
  338. * @}
  339. */
  340. LLVM_CLANG_C_EXTERN_C_END
  341. #endif
  342. #ifdef __GNUC__
  343. #pragma GCC diagnostic pop
  344. #endif