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