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