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