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