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