Diagnostic.h revision 4ad4b3ebbe5769143389dccfcfadb666a4ba5940
1//===--- Diagnostic.h - C Language Family Diagnostic Handling ---*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the Diagnostic-related interfaces. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_DIAGNOSTIC_H 15#define LLVM_CLANG_DIAGNOSTIC_H 16 17#include "clang/Basic/SourceLocation.h" 18#include <string> 19#include <cassert> 20 21namespace llvm { 22 template <typename T> class SmallVectorImpl; 23} 24 25namespace clang { 26 class DiagnosticClient; 27 class SourceRange; 28 class DiagnosticBuilder; 29 class IdentifierInfo; 30 31 // Import the diagnostic enums themselves. 32 namespace diag { 33 // Start position for diagnostics. 34 enum { 35 DIAG_START_DRIVER = 300, 36 DIAG_START_LEX = DIAG_START_DRIVER + 100, 37 DIAG_START_PARSE = DIAG_START_LEX + 300, 38 DIAG_START_AST = DIAG_START_PARSE + 300, 39 DIAG_START_SEMA = DIAG_START_AST + 100, 40 DIAG_START_ANALYSIS = DIAG_START_SEMA + 1000, 41 DIAG_UPPER_LIMIT = DIAG_START_ANALYSIS + 100 42 }; 43 44 class CustomDiagInfo; 45 46 /// diag::kind - All of the diagnostics that can be emitted by the frontend. 47 typedef unsigned kind; 48 49 // Get typedefs for common diagnostics. 50 enum { 51#define DIAG(ENUM,FLAGS,DESC) ENUM, 52#include "clang/Basic/DiagnosticCommonKinds.def" 53 NUM_BUILTIN_COMMON_DIAGNOSTICS 54#undef DIAG 55 }; 56 57 /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs 58 /// to either MAP_IGNORE (nothing), MAP_WARNING (emit a warning), MAP_ERROR 59 /// (emit as an error), or MAP_DEFAULT (handle the default way). It allows 60 /// clients to map errors to MAP_ERROR/MAP_DEFAULT or MAP_FATAL (stop 61 /// emitting diagnostics after this one). 62 enum Mapping { 63 MAP_DEFAULT = 0, //< Do not map this diagnostic. 64 MAP_IGNORE = 1, //< Map this diagnostic to nothing, ignore it. 65 MAP_WARNING = 2, //< Map this diagnostic to a warning. 66 MAP_ERROR = 3, //< Map this diagnostic to an error. 67 MAP_FATAL = 4 //< Map this diagnostic to a fatal error. 68 }; 69 } 70 71/// \brief Annotates a diagnostic with some code that should be 72/// inserted, removed, or replaced to fix the problem. 73/// 74/// This kind of hint should be used when we are certain that the 75/// introduction, removal, or modification of a particular (small!) 76/// amount of code will correct a compilation error. The compiler 77/// should also provide full recovery from such errors, such that 78/// suppressing the diagnostic output can still result in successful 79/// compilation. 80class CodeModificationHint { 81public: 82 /// \brief Tokens that should be removed to correct the error. 83 SourceRange RemoveRange; 84 85 /// \brief The location at which we should insert code to correct 86 /// the error. 87 SourceLocation InsertionLoc; 88 89 /// \brief The actual code to insert at the insertion location, as a 90 /// string. 91 std::string CodeToInsert; 92 93 /// \brief Empty code modification hint, indicating that no code 94 /// modification is known. 95 CodeModificationHint() : RemoveRange(), InsertionLoc() { } 96 97 /// \brief Create a code modification hint that inserts the given 98 /// code string at a specific location. 99 static CodeModificationHint CreateInsertion(SourceLocation InsertionLoc, 100 const std::string &Code) { 101 CodeModificationHint Hint; 102 Hint.InsertionLoc = InsertionLoc; 103 Hint.CodeToInsert = Code; 104 return Hint; 105 } 106 107 /// \brief Create a code modification hint that removes the given 108 /// source range. 109 static CodeModificationHint CreateRemoval(SourceRange RemoveRange) { 110 CodeModificationHint Hint; 111 Hint.RemoveRange = RemoveRange; 112 return Hint; 113 } 114 115 /// \brief Create a code modification hint that replaces the given 116 /// source range with the given code string. 117 static CodeModificationHint CreateReplacement(SourceRange RemoveRange, 118 const std::string &Code) { 119 CodeModificationHint Hint; 120 Hint.RemoveRange = RemoveRange; 121 Hint.InsertionLoc = RemoveRange.getBegin(); 122 Hint.CodeToInsert = Code; 123 return Hint; 124 } 125}; 126 127/// \brief A hook function that will be invoked after we have 128/// completed processing of the current diagnostic. 129/// 130/// Hook functions are typically used to emit further diagnostics 131/// (typically notes) that give more information about this 132/// diagnostic. 133struct PostDiagnosticHook { 134 /// \brief The type of the hook function itself. 135 /// 136 /// DiagID is the ID of the diagnostic to which the hook was 137 /// attached, and Cookie is a user-specified value that can be used 138 /// to store extra data for the hook. 139 typedef void (*HookTy)(unsigned DiagID, void *Cookie); 140 141 PostDiagnosticHook() {} 142 143 PostDiagnosticHook(HookTy Hook, void *Cookie) : Hook(Hook), Cookie(Cookie) { 144 assert(Hook && "No hook provided!"); 145 } 146 147 /// \brief The hook function. 148 HookTy Hook; 149 150 /// \brief The cookie that will be passed along to the hook function. 151 void *Cookie; 152}; 153 154/// Diagnostic - This concrete class is used by the front-end to report 155/// problems and issues. It massages the diagnostics (e.g. handling things like 156/// "report warnings as errors" and passes them off to the DiagnosticClient for 157/// reporting to the user. 158class Diagnostic { 159public: 160 /// Level - The level of the diagnostic, after it has been through mapping. 161 enum Level { 162 Ignored, Note, Warning, Error, Fatal 163 }; 164 165 enum ArgumentKind { 166 ak_std_string, // std::string 167 ak_c_string, // const char * 168 ak_sint, // int 169 ak_uint, // unsigned 170 ak_identifierinfo, // IdentifierInfo 171 ak_qualtype, // QualType 172 ak_declarationname, // DeclarationName 173 ak_nameddecl // NamedDecl * 174 }; 175 176private: 177 bool IgnoreAllWarnings; // Ignore all warnings: -w 178 bool WarningsAsErrors; // Treat warnings like errors: 179 bool WarnOnExtensions; // Enables warnings for gcc extensions: -pedantic. 180 bool ErrorOnExtensions; // Error on extensions: -pedantic-errors. 181 bool SuppressSystemWarnings;// Suppress warnings in system headers. 182 DiagnosticClient *Client; 183 184 /// DiagMappings - Mapping information for diagnostics. Mapping info is 185 /// packed into four bits per diagnostic. 186 unsigned char DiagMappings[diag::DIAG_UPPER_LIMIT/2]; 187 188 /// ErrorOccurred / FatalErrorOccurred - This is set to true when an error or 189 /// fatal error is emitted, and is sticky. 190 bool ErrorOccurred; 191 bool FatalErrorOccurred; 192 193 /// LastDiagLevel - This is the level of the last diagnostic emitted. This is 194 /// used to emit continuation diagnostics with the same level as the 195 /// diagnostic that they follow. 196 Diagnostic::Level LastDiagLevel; 197 198 unsigned NumDiagnostics; // Number of diagnostics reported 199 unsigned NumErrors; // Number of diagnostics that are errors 200 201 /// CustomDiagInfo - Information for uniquing and looking up custom diags. 202 diag::CustomDiagInfo *CustomDiagInfo; 203 204 /// ArgToStringFn - A function pointer that converts an opaque diagnostic 205 /// argument to a strings. This takes the modifiers and argument that was 206 /// present in the diagnostic. 207 /// This is a hack to avoid a layering violation between libbasic and libsema. 208 typedef void (*ArgToStringFnTy)(ArgumentKind Kind, intptr_t Val, 209 const char *Modifier, unsigned ModifierLen, 210 const char *Argument, unsigned ArgumentLen, 211 llvm::SmallVectorImpl<char> &Output, 212 void *Cookie); 213 void *ArgToStringCookie; 214 ArgToStringFnTy ArgToStringFn; 215public: 216 explicit Diagnostic(DiagnosticClient *client = 0); 217 ~Diagnostic(); 218 219 //===--------------------------------------------------------------------===// 220 // Diagnostic characterization methods, used by a client to customize how 221 // 222 223 DiagnosticClient *getClient() { return Client; }; 224 const DiagnosticClient *getClient() const { return Client; }; 225 226 void setClient(DiagnosticClient* client) { Client = client; } 227 228 /// setIgnoreAllWarnings - When set to true, any unmapped warnings are 229 /// ignored. If this and WarningsAsErrors are both set, then this one wins. 230 void setIgnoreAllWarnings(bool Val) { IgnoreAllWarnings = Val; } 231 bool getIgnoreAllWarnings() const { return IgnoreAllWarnings; } 232 233 /// setWarningsAsErrors - When set to true, any warnings reported are issued 234 /// as errors. 235 void setWarningsAsErrors(bool Val) { WarningsAsErrors = Val; } 236 bool getWarningsAsErrors() const { return WarningsAsErrors; } 237 238 /// setWarnOnExtensions - When set to true, issue warnings on GCC extensions, 239 /// the equivalent of GCC's -pedantic. 240 void setWarnOnExtensions(bool Val) { WarnOnExtensions = Val; } 241 bool getWarnOnExtensions() const { return WarnOnExtensions; } 242 243 /// setErrorOnExtensions - When set to true issue errors for GCC extensions 244 /// instead of warnings. This is the equivalent to GCC's -pedantic-errors. 245 void setErrorOnExtensions(bool Val) { ErrorOnExtensions = Val; } 246 bool getErrorOnExtensions() const { return ErrorOnExtensions; } 247 248 /// setSuppressSystemWarnings - When set to true mask warnings that 249 /// come from system headers. 250 void setSuppressSystemWarnings(bool Val) { SuppressSystemWarnings = Val; } 251 bool getSuppressSystemWarnings() const { return SuppressSystemWarnings; } 252 253 /// setDiagnosticMapping - This allows the client to specify that certain 254 /// warnings are ignored. Only WARNINGs and EXTENSIONs can be mapped. 255 void setDiagnosticMapping(diag::kind Diag, diag::Mapping Map) { 256 assert(Diag < diag::DIAG_UPPER_LIMIT && 257 "Can only map builtin diagnostics"); 258 assert((isBuiltinWarningOrExtension(Diag) || Map == diag::MAP_FATAL) && 259 "Cannot map errors!"); 260 unsigned char &Slot = DiagMappings[Diag/2]; 261 unsigned Bits = (Diag & 1)*4; 262 Slot &= ~(7 << Bits); 263 Slot |= Map << Bits; 264 } 265 266 /// getDiagnosticMapping - Return the mapping currently set for the specified 267 /// diagnostic. 268 diag::Mapping getDiagnosticMapping(diag::kind Diag) const { 269 return (diag::Mapping)((DiagMappings[Diag/2] >> (Diag & 1)*4) & 7); 270 } 271 272 bool hasErrorOccurred() const { return ErrorOccurred; } 273 bool hasFatalErrorOccurred() const { return FatalErrorOccurred; } 274 275 unsigned getNumErrors() const { return NumErrors; } 276 unsigned getNumDiagnostics() const { return NumDiagnostics; } 277 278 /// getCustomDiagID - Return an ID for a diagnostic with the specified message 279 /// and level. If this is the first request for this diagnosic, it is 280 /// registered and created, otherwise the existing ID is returned. 281 unsigned getCustomDiagID(Level L, const char *Message); 282 283 284 /// ConvertArgToString - This method converts a diagnostic argument (as an 285 /// intptr_t) into the string that represents it. 286 void ConvertArgToString(ArgumentKind Kind, intptr_t Val, 287 const char *Modifier, unsigned ModLen, 288 const char *Argument, unsigned ArgLen, 289 llvm::SmallVectorImpl<char> &Output) const { 290 ArgToStringFn(Kind, Val, Modifier, ModLen, Argument, ArgLen, Output, 291 ArgToStringCookie); 292 } 293 294 void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie) { 295 ArgToStringFn = Fn; 296 ArgToStringCookie = Cookie; 297 } 298 299 //===--------------------------------------------------------------------===// 300 // Diagnostic classification and reporting interfaces. 301 // 302 303 /// getDescription - Given a diagnostic ID, return a description of the 304 /// issue. 305 const char *getDescription(unsigned DiagID) const; 306 307 /// isNoteWarningOrExtension - Return true if the unmapped diagnostic 308 /// level of the specified diagnostic ID is a Warning or Extension. 309 /// This only works on builtin diagnostics, not custom ones, and is not legal to 310 /// call on NOTEs. 311 static bool isBuiltinWarningOrExtension(unsigned DiagID); 312 313 /// \brief Determine whether the given built-in diagnostic ID is a 314 /// Note. 315 static bool isBuiltinNote(unsigned DiagID); 316 317 /// getDiagnosticLevel - Based on the way the client configured the Diagnostic 318 /// object, classify the specified diagnostic ID into a Level, consumable by 319 /// the DiagnosticClient. 320 Level getDiagnosticLevel(unsigned DiagID) const; 321 322 /// Report - Issue the message to the client. @c DiagID is a member of the 323 /// @c diag::kind enum. This actually returns aninstance of DiagnosticBuilder 324 /// which emits the diagnostics (through @c ProcessDiag) when it is destroyed. 325 /// @c Pos represents the source location associated with the diagnostic, 326 /// which can be an invalid location if no position information is available. 327 inline DiagnosticBuilder Report(FullSourceLoc Pos, unsigned DiagID); 328 329private: 330 /// getDiagnosticLevel - This is an internal implementation helper used when 331 /// DiagClass is already known. 332 Level getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const; 333 334 // This is private state used by DiagnosticBuilder. We put it here instead of 335 // in DiagnosticBuilder in order to keep DiagnosticBuilder a small lightweight 336 // object. This implementation choice means that we can only have one 337 // diagnostic "in flight" at a time, but this seems to be a reasonable 338 // tradeoff to keep these objects small. Assertions verify that only one 339 // diagnostic is in flight at a time. 340 friend class DiagnosticBuilder; 341 friend class DiagnosticInfo; 342 343 /// CurDiagLoc - This is the location of the current diagnostic that is in 344 /// flight. 345 FullSourceLoc CurDiagLoc; 346 347 /// CurDiagID - This is the ID of the current diagnostic that is in flight. 348 /// This is set to ~0U when there is no diagnostic in flight. 349 unsigned CurDiagID; 350 351 enum { 352 /// MaxArguments - The maximum number of arguments we can hold. We currently 353 /// only support up to 10 arguments (%0-%9). A single diagnostic with more 354 /// than that almost certainly has to be simplified anyway. 355 MaxArguments = 10 356 }; 357 358 /// NumDiagArgs - This contains the number of entries in Arguments. 359 signed char NumDiagArgs; 360 /// NumRanges - This is the number of ranges in the DiagRanges array. 361 unsigned char NumDiagRanges; 362 /// \brief The number of code modifications hints in the 363 /// CodeModificationHints array. 364 unsigned char NumCodeModificationHints; 365 /// \brief The number of post-diagnostic hooks in the 366 /// PostDiagnosticHooks array. 367 unsigned char NumPostDiagnosticHooks; 368 369 /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum 370 /// values, with one for each argument. This specifies whether the argument 371 /// is in DiagArgumentsStr or in DiagArguments. 372 unsigned char DiagArgumentsKind[MaxArguments]; 373 374 /// DiagArgumentsStr - This holds the values of each string argument for the 375 /// current diagnostic. This value is only used when the corresponding 376 /// ArgumentKind is ak_std_string. 377 std::string DiagArgumentsStr[MaxArguments]; 378 379 /// DiagArgumentsVal - The values for the various substitution positions. This 380 /// is used when the argument is not an std::string. The specific value is 381 /// mangled into an intptr_t and the intepretation depends on exactly what 382 /// sort of argument kind it is. 383 intptr_t DiagArgumentsVal[MaxArguments]; 384 385 /// DiagRanges - The list of ranges added to this diagnostic. It currently 386 /// only support 10 ranges, could easily be extended if needed. 387 const SourceRange *DiagRanges[10]; 388 389 enum { MaxCodeModificationHints = 3 }; 390 391 /// CodeModificationHints - If valid, provides a hint with some code 392 /// to insert, remove, or modify at a particular position. 393 CodeModificationHint CodeModificationHints[MaxCodeModificationHints]; 394 395 enum { MaxPostDiagnosticHooks = 10 }; 396 397 /// \brief Functions that will be invoked after the diagnostic has 398 /// been emitted. 399 PostDiagnosticHook PostDiagnosticHooks[MaxPostDiagnosticHooks]; 400 401 /// \brief Whether we're already within a post-diagnostic hook. 402 bool InPostDiagnosticHook; 403 404 /// ProcessDiag - This is the method used to report a diagnostic that is 405 /// finally fully formed. 406 void ProcessDiag(); 407}; 408 409//===----------------------------------------------------------------------===// 410// DiagnosticBuilder 411//===----------------------------------------------------------------------===// 412 413/// DiagnosticBuilder - This is a little helper class used to produce 414/// diagnostics. This is constructed by the Diagnostic::Report method, and 415/// allows insertion of extra information (arguments and source ranges) into the 416/// currently "in flight" diagnostic. When the temporary for the builder is 417/// destroyed, the diagnostic is issued. 418/// 419/// Note that many of these will be created as temporary objects (many call 420/// sites), so we want them to be small and we never want their address taken. 421/// This ensures that compilers with somewhat reasonable optimizers will promote 422/// the common fields to registers, eliminating increments of the NumArgs field, 423/// for example. 424class DiagnosticBuilder { 425 mutable Diagnostic *DiagObj; 426 mutable unsigned NumArgs, NumRanges, NumCodeModificationHints, 427 NumPostDiagnosticHooks; 428 429 void operator=(const DiagnosticBuilder&); // DO NOT IMPLEMENT 430 friend class Diagnostic; 431 explicit DiagnosticBuilder(Diagnostic *diagObj) 432 : DiagObj(diagObj), NumArgs(0), NumRanges(0), 433 NumCodeModificationHints(0), NumPostDiagnosticHooks(0) {} 434 435public: 436 /// Copy constructor. When copied, this "takes" the diagnostic info from the 437 /// input and neuters it. 438 DiagnosticBuilder(const DiagnosticBuilder &D) { 439 DiagObj = D.DiagObj; 440 D.DiagObj = 0; 441 } 442 443 /// Destructor - The dtor emits the diagnostic. 444 ~DiagnosticBuilder() { 445 // If DiagObj is null, then its soul was stolen by the copy ctor. 446 if (DiagObj == 0) return; 447 448 // When destroyed, the ~DiagnosticBuilder sets the final argument count into 449 // the Diagnostic object. 450 DiagObj->NumDiagArgs = NumArgs; 451 DiagObj->NumDiagRanges = NumRanges; 452 DiagObj->NumCodeModificationHints = NumCodeModificationHints; 453 DiagObj->NumPostDiagnosticHooks = NumPostDiagnosticHooks; 454 455 // Process the diagnostic, sending the accumulated information to the 456 // DiagnosticClient. 457 DiagObj->ProcessDiag(); 458 459 // This diagnostic is no longer in flight. 460 DiagObj->CurDiagID = ~0U; 461 } 462 463 /// Operator bool: conversion of DiagnosticBuilder to bool always returns 464 /// true. This allows is to be used in boolean error contexts like: 465 /// return Diag(...); 466 operator bool() const { return true; } 467 468 void AddString(const std::string &S) const { 469 assert(NumArgs < Diagnostic::MaxArguments && 470 "Too many arguments to diagnostic!"); 471 DiagObj->DiagArgumentsKind[NumArgs] = Diagnostic::ak_std_string; 472 DiagObj->DiagArgumentsStr[NumArgs++] = S; 473 } 474 475 void AddTaggedVal(intptr_t V, Diagnostic::ArgumentKind Kind) const { 476 assert(NumArgs < Diagnostic::MaxArguments && 477 "Too many arguments to diagnostic!"); 478 DiagObj->DiagArgumentsKind[NumArgs] = Kind; 479 DiagObj->DiagArgumentsVal[NumArgs++] = V; 480 } 481 482 void AddSourceRange(const SourceRange &R) const { 483 assert(NumRanges < 484 sizeof(DiagObj->DiagRanges)/sizeof(DiagObj->DiagRanges[0]) && 485 "Too many arguments to diagnostic!"); 486 DiagObj->DiagRanges[NumRanges++] = &R; 487 } 488 489 void AddCodeModificationHint(const CodeModificationHint &Hint) const { 490 assert(NumCodeModificationHints < Diagnostic::MaxCodeModificationHints && 491 "Too many code modification hints!"); 492 DiagObj->CodeModificationHints[NumCodeModificationHints++] = Hint; 493 } 494 495 void AddPostDiagnosticHook(const PostDiagnosticHook &Hook) const { 496 assert(!DiagObj->InPostDiagnosticHook && 497 "Can't add a post-diagnostic hook to a diagnostic inside " 498 "a post-diagnostic hook"); 499 assert(NumPostDiagnosticHooks < Diagnostic::MaxPostDiagnosticHooks && 500 "Too many post-diagnostic hooks"); 501 DiagObj->PostDiagnosticHooks[NumPostDiagnosticHooks++] = Hook; 502 } 503}; 504 505inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 506 const std::string &S) { 507 DB.AddString(S); 508 return DB; 509} 510 511inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 512 const char *Str) { 513 DB.AddTaggedVal(reinterpret_cast<intptr_t>(Str), 514 Diagnostic::ak_c_string); 515 return DB; 516} 517 518inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, int I) { 519 DB.AddTaggedVal(I, Diagnostic::ak_sint); 520 return DB; 521} 522 523inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,bool I) { 524 DB.AddTaggedVal(I, Diagnostic::ak_sint); 525 return DB; 526} 527 528inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 529 unsigned I) { 530 DB.AddTaggedVal(I, Diagnostic::ak_uint); 531 return DB; 532} 533 534inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 535 const IdentifierInfo *II) { 536 DB.AddTaggedVal(reinterpret_cast<intptr_t>(II), 537 Diagnostic::ak_identifierinfo); 538 return DB; 539} 540 541inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 542 const SourceRange &R) { 543 DB.AddSourceRange(R); 544 return DB; 545} 546 547inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 548 const CodeModificationHint &Hint) { 549 DB.AddCodeModificationHint(Hint); 550 return DB; 551} 552 553inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 554 const PostDiagnosticHook &Hook) { 555 DB.AddPostDiagnosticHook(Hook); 556 return DB; 557} 558 559 560/// Report - Issue the message to the client. DiagID is a member of the 561/// diag::kind enum. This actually returns a new instance of DiagnosticBuilder 562/// which emits the diagnostics (through ProcessDiag) when it is destroyed. 563inline DiagnosticBuilder Diagnostic::Report(FullSourceLoc Loc, unsigned DiagID){ 564 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!"); 565 CurDiagLoc = Loc; 566 CurDiagID = DiagID; 567 return DiagnosticBuilder(this); 568} 569 570//===----------------------------------------------------------------------===// 571// DiagnosticInfo 572//===----------------------------------------------------------------------===// 573 574/// DiagnosticInfo - This is a little helper class (which is basically a smart 575/// pointer that forward info from Diagnostic) that allows clients to enquire 576/// about the currently in-flight diagnostic. 577class DiagnosticInfo { 578 const Diagnostic *DiagObj; 579public: 580 explicit DiagnosticInfo(const Diagnostic *DO) : DiagObj(DO) {} 581 582 const Diagnostic *getDiags() const { return DiagObj; } 583 unsigned getID() const { return DiagObj->CurDiagID; } 584 const FullSourceLoc &getLocation() const { return DiagObj->CurDiagLoc; } 585 586 unsigned getNumArgs() const { return DiagObj->NumDiagArgs; } 587 588 /// getArgKind - Return the kind of the specified index. Based on the kind 589 /// of argument, the accessors below can be used to get the value. 590 Diagnostic::ArgumentKind getArgKind(unsigned Idx) const { 591 assert(Idx < getNumArgs() && "Argument index out of range!"); 592 return (Diagnostic::ArgumentKind)DiagObj->DiagArgumentsKind[Idx]; 593 } 594 595 /// getArgStdStr - Return the provided argument string specified by Idx. 596 const std::string &getArgStdStr(unsigned Idx) const { 597 assert(getArgKind(Idx) == Diagnostic::ak_std_string && 598 "invalid argument accessor!"); 599 return DiagObj->DiagArgumentsStr[Idx]; 600 } 601 602 /// getArgCStr - Return the specified C string argument. 603 const char *getArgCStr(unsigned Idx) const { 604 assert(getArgKind(Idx) == Diagnostic::ak_c_string && 605 "invalid argument accessor!"); 606 return reinterpret_cast<const char*>(DiagObj->DiagArgumentsVal[Idx]); 607 } 608 609 /// getArgSInt - Return the specified signed integer argument. 610 int getArgSInt(unsigned Idx) const { 611 assert(getArgKind(Idx) == Diagnostic::ak_sint && 612 "invalid argument accessor!"); 613 return (int)DiagObj->DiagArgumentsVal[Idx]; 614 } 615 616 /// getArgUInt - Return the specified unsigned integer argument. 617 unsigned getArgUInt(unsigned Idx) const { 618 assert(getArgKind(Idx) == Diagnostic::ak_uint && 619 "invalid argument accessor!"); 620 return (unsigned)DiagObj->DiagArgumentsVal[Idx]; 621 } 622 623 /// getArgIdentifier - Return the specified IdentifierInfo argument. 624 const IdentifierInfo *getArgIdentifier(unsigned Idx) const { 625 assert(getArgKind(Idx) == Diagnostic::ak_identifierinfo && 626 "invalid argument accessor!"); 627 return reinterpret_cast<IdentifierInfo*>(DiagObj->DiagArgumentsVal[Idx]); 628 } 629 630 /// getRawArg - Return the specified non-string argument in an opaque form. 631 intptr_t getRawArg(unsigned Idx) const { 632 assert(getArgKind(Idx) != Diagnostic::ak_std_string && 633 "invalid argument accessor!"); 634 return DiagObj->DiagArgumentsVal[Idx]; 635 } 636 637 638 /// getNumRanges - Return the number of source ranges associated with this 639 /// diagnostic. 640 unsigned getNumRanges() const { 641 return DiagObj->NumDiagRanges; 642 } 643 644 const SourceRange &getRange(unsigned Idx) const { 645 assert(Idx < DiagObj->NumDiagRanges && "Invalid diagnostic range index!"); 646 return *DiagObj->DiagRanges[Idx]; 647 } 648 649 unsigned getNumCodeModificationHints() const { 650 return DiagObj->NumCodeModificationHints; 651 } 652 653 const CodeModificationHint &getCodeModificationHint(unsigned Idx) const { 654 return DiagObj->CodeModificationHints[Idx]; 655 } 656 657 const CodeModificationHint *getCodeModificationHints() const { 658 return DiagObj->NumCodeModificationHints? 659 &DiagObj->CodeModificationHints[0] : 0; 660 } 661 662 /// FormatDiagnostic - Format this diagnostic into a string, substituting the 663 /// formal arguments into the %0 slots. The result is appended onto the Str 664 /// array. 665 void FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const; 666}; 667 668/// DiagnosticClient - This is an abstract interface implemented by clients of 669/// the front-end, which formats and prints fully processed diagnostics. 670class DiagnosticClient { 671public: 672 virtual ~DiagnosticClient(); 673 674 /// IncludeInDiagnosticCounts - This method (whose default implementation 675 /// returns true) indicates whether the diagnostics handled by this 676 /// DiagnosticClient should be included in the number of diagnostics 677 /// reported by Diagnostic. 678 virtual bool IncludeInDiagnosticCounts() const; 679 680 /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or 681 /// capturing it to a log as needed. 682 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel, 683 const DiagnosticInfo &Info) = 0; 684}; 685 686} // end namespace clang 687 688#endif 689