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