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