1 /*===-- clang-c/CXDiagnostic.h - C Index Diagnostics --------------*- C -*-===*\
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
5 |* See https://llvm.org/LICENSE.txt for license information. *|
6 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
8 |*===----------------------------------------------------------------------===*|
10 |* This header provides the interface to C Index diagnostics. *|
12 \*===----------------------------------------------------------------------===*/
14 #ifndef LLVM_CLANG_C_CXDIAGNOSTIC_H
15 #define LLVM_CLANG_C_CXDIAGNOSTIC_H
17 #include "clang-c/CXSourceLocation.h"
18 #include "clang-c/CXString.h"
19 #include "clang-c/ExternC.h"
20 #include "clang-c/Platform.h"
22 LLVM_CLANG_C_EXTERN_C_BEGIN
25 * \defgroup CINDEX_DIAG Diagnostic reporting
31 * Describes the severity of a particular diagnostic.
33 enum CXDiagnosticSeverity
{
35 * A diagnostic that has been suppressed, e.g., by a command-line
38 CXDiagnostic_Ignored
= 0,
41 * This diagnostic is a note that should be attached to the
42 * previous (non-note) diagnostic.
44 CXDiagnostic_Note
= 1,
47 * This diagnostic indicates suspicious code that may not be
50 CXDiagnostic_Warning
= 2,
53 * This diagnostic indicates that the code is ill-formed.
55 CXDiagnostic_Error
= 3,
58 * This diagnostic indicates that the code is ill-formed such
59 * that future parser recovery is unlikely to produce useful
62 CXDiagnostic_Fatal
= 4
66 * A single diagnostic, containing the diagnostic's severity,
67 * location, text, source ranges, and fix-it hints.
69 typedef void *CXDiagnostic
;
72 * A group of CXDiagnostics.
74 typedef void *CXDiagnosticSet
;
77 * Determine the number of diagnostics in a CXDiagnosticSet.
79 CINDEX_LINKAGE
unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags
);
82 * Retrieve a diagnostic associated with the given CXDiagnosticSet.
84 * \param Diags the CXDiagnosticSet to query.
85 * \param Index the zero-based diagnostic number to retrieve.
87 * \returns the requested diagnostic. This diagnostic must be freed
88 * via a call to \c clang_disposeDiagnostic().
90 CINDEX_LINKAGE CXDiagnostic
clang_getDiagnosticInSet(CXDiagnosticSet Diags
,
94 * Describes the kind of error that occurred (if any) in a call to
95 * \c clang_loadDiagnostics.
97 enum CXLoadDiag_Error
{
99 * Indicates that no error occurred.
104 * Indicates that an unknown error occurred while attempting to
105 * deserialize diagnostics.
107 CXLoadDiag_Unknown
= 1,
110 * Indicates that the file containing the serialized diagnostics
111 * could not be opened.
113 CXLoadDiag_CannotLoad
= 2,
116 * Indicates that the serialized diagnostics file is invalid or
119 CXLoadDiag_InvalidFile
= 3
123 * Deserialize a set of diagnostics from a Clang diagnostics bitcode
126 * \param file The name of the file to deserialize.
127 * \param error A pointer to a enum value recording if there was a problem
128 * deserializing the diagnostics.
129 * \param errorString A pointer to a CXString for recording the error string
130 * if the file was not successfully loaded.
132 * \returns A loaded CXDiagnosticSet if successful, and NULL otherwise. These
133 * diagnostics should be released using clang_disposeDiagnosticSet().
135 CINDEX_LINKAGE CXDiagnosticSet
clang_loadDiagnostics(
136 const char *file
, enum CXLoadDiag_Error
*error
, CXString
*errorString
);
139 * Release a CXDiagnosticSet and all of its contained diagnostics.
141 CINDEX_LINKAGE
void clang_disposeDiagnosticSet(CXDiagnosticSet Diags
);
144 * Retrieve the child diagnostics of a CXDiagnostic.
146 * This CXDiagnosticSet does not need to be released by
147 * clang_disposeDiagnosticSet.
149 CINDEX_LINKAGE CXDiagnosticSet
clang_getChildDiagnostics(CXDiagnostic D
);
152 * Destroy a diagnostic.
154 CINDEX_LINKAGE
void clang_disposeDiagnostic(CXDiagnostic Diagnostic
);
157 * Options to control the display of diagnostics.
159 * The values in this enum are meant to be combined to customize the
160 * behavior of \c clang_formatDiagnostic().
162 enum CXDiagnosticDisplayOptions
{
164 * Display the source-location information where the
165 * diagnostic was located.
167 * When set, diagnostics will be prefixed by the file, line, and
168 * (optionally) column to which the diagnostic refers. For example,
171 * test.c:28: warning: extra tokens at end of #endif directive
174 * This option corresponds to the clang flag \c -fshow-source-location.
176 CXDiagnostic_DisplaySourceLocation
= 0x01,
179 * If displaying the source-location information of the
180 * diagnostic, also include the column number.
182 * This option corresponds to the clang flag \c -fshow-column.
184 CXDiagnostic_DisplayColumn
= 0x02,
187 * If displaying the source-location information of the
188 * diagnostic, also include information about source ranges in a
189 * machine-parsable format.
191 * This option corresponds to the clang flag
192 * \c -fdiagnostics-print-source-range-info.
194 CXDiagnostic_DisplaySourceRanges
= 0x04,
197 * Display the option name associated with this diagnostic, if any.
199 * The option name displayed (e.g., -Wconversion) will be placed in brackets
200 * after the diagnostic text. This option corresponds to the clang flag
201 * \c -fdiagnostics-show-option.
203 CXDiagnostic_DisplayOption
= 0x08,
206 * Display the category number associated with this diagnostic, if any.
208 * The category number is displayed within brackets after the diagnostic text.
209 * This option corresponds to the clang flag
210 * \c -fdiagnostics-show-category=id.
212 CXDiagnostic_DisplayCategoryId
= 0x10,
215 * Display the category name associated with this diagnostic, if any.
217 * The category name is displayed within brackets after the diagnostic text.
218 * This option corresponds to the clang flag
219 * \c -fdiagnostics-show-category=name.
221 CXDiagnostic_DisplayCategoryName
= 0x20
225 * Format the given diagnostic in a manner that is suitable for display.
227 * This routine will format the given diagnostic to a string, rendering
228 * the diagnostic according to the various options given. The
229 * \c clang_defaultDiagnosticDisplayOptions() function returns the set of
230 * options that most closely mimics the behavior of the clang compiler.
232 * \param Diagnostic The diagnostic to print.
234 * \param Options A set of options that control the diagnostic display,
235 * created by combining \c CXDiagnosticDisplayOptions values.
237 * \returns A new string containing for formatted diagnostic.
239 CINDEX_LINKAGE CXString
clang_formatDiagnostic(CXDiagnostic Diagnostic
,
243 * Retrieve the set of display options most similar to the
244 * default behavior of the clang compiler.
246 * \returns A set of display options suitable for use with \c
247 * clang_formatDiagnostic().
249 CINDEX_LINKAGE
unsigned clang_defaultDiagnosticDisplayOptions(void);
252 * Determine the severity of the given diagnostic.
254 CINDEX_LINKAGE
enum CXDiagnosticSeverity
255 clang_getDiagnosticSeverity(CXDiagnostic
);
258 * Retrieve the source location of the given diagnostic.
260 * This location is where Clang would print the caret ('^') when
261 * displaying the diagnostic on the command line.
263 CINDEX_LINKAGE CXSourceLocation
clang_getDiagnosticLocation(CXDiagnostic
);
266 * Retrieve the text of the given diagnostic.
268 CINDEX_LINKAGE CXString
clang_getDiagnosticSpelling(CXDiagnostic
);
271 * Retrieve the name of the command-line option that enabled this
274 * \param Diag The diagnostic to be queried.
276 * \param Disable If non-NULL, will be set to the option that disables this
277 * diagnostic (if any).
279 * \returns A string that contains the command-line option used to enable this
280 * warning, such as "-Wconversion" or "-pedantic".
282 CINDEX_LINKAGE CXString
clang_getDiagnosticOption(CXDiagnostic Diag
,
286 * Retrieve the category number for this diagnostic.
288 * Diagnostics can be categorized into groups along with other, related
289 * diagnostics (e.g., diagnostics under the same warning flag). This routine
290 * retrieves the category number for the given diagnostic.
292 * \returns The number of the category that contains this diagnostic, or zero
293 * if this diagnostic is uncategorized.
295 CINDEX_LINKAGE
unsigned clang_getDiagnosticCategory(CXDiagnostic
);
298 * Retrieve the name of a particular diagnostic category. This
299 * is now deprecated. Use clang_getDiagnosticCategoryText()
302 * \param Category A diagnostic category number, as returned by
303 * \c clang_getDiagnosticCategory().
305 * \returns The name of the given diagnostic category.
307 CINDEX_DEPRECATED CINDEX_LINKAGE CXString
308 clang_getDiagnosticCategoryName(unsigned Category
);
311 * Retrieve the diagnostic category text for a given diagnostic.
313 * \returns The text of the given diagnostic category.
315 CINDEX_LINKAGE CXString
clang_getDiagnosticCategoryText(CXDiagnostic
);
318 * Determine the number of source ranges associated with the given
321 CINDEX_LINKAGE
unsigned clang_getDiagnosticNumRanges(CXDiagnostic
);
324 * Retrieve a source range associated with the diagnostic.
326 * A diagnostic's source ranges highlight important elements in the source
327 * code. On the command line, Clang displays source ranges by
328 * underlining them with '~' characters.
330 * \param Diagnostic the diagnostic whose range is being extracted.
332 * \param Range the zero-based index specifying which range to
334 * \returns the requested source range.
336 CINDEX_LINKAGE CXSourceRange
clang_getDiagnosticRange(CXDiagnostic Diagnostic
,
340 * Determine the number of fix-it hints associated with the
343 CINDEX_LINKAGE
unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diagnostic
);
346 * Retrieve the replacement information for a given fix-it.
348 * Fix-its are described in terms of a source range whose contents
349 * should be replaced by a string. This approach generalizes over
350 * three kinds of operations: removal of source code (the range covers
351 * the code to be removed and the replacement string is empty),
352 * replacement of source code (the range covers the code to be
353 * replaced and the replacement string provides the new code), and
354 * insertion (both the start and end of the range point at the
355 * insertion location, and the replacement string provides the text to
358 * \param Diagnostic The diagnostic whose fix-its are being queried.
360 * \param FixIt The zero-based index of the fix-it.
362 * \param ReplacementRange The source range whose contents will be
363 * replaced with the returned replacement string. Note that source
364 * ranges are half-open ranges [a, b), so the source code should be
365 * replaced from a and up to (but not including) b.
367 * \returns A string containing text that should be replace the source
368 * code indicated by the \c ReplacementRange.
370 CINDEX_LINKAGE CXString
clang_getDiagnosticFixIt(
371 CXDiagnostic Diagnostic
, unsigned FixIt
, CXSourceRange
*ReplacementRange
);
377 LLVM_CLANG_C_EXTERN_C_END