Diagnostic.h revision 011bb4edf731d529da1cbf71c7c2696aaf5a054f
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 class CustomDiagInfo; 35 36 /// diag::kind - All of the diagnostics that can be emitted by the frontend. 37 enum kind { 38#define DIAG(ENUM,FLAGS,DESC) ENUM, 39#include "DiagnosticKinds.def" 40 NUM_BUILTIN_DIAGNOSTICS 41 }; 42 43 /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs 44 /// to either MAP_IGNORE (nothing), MAP_WARNING (emit a warning), MAP_ERROR 45 /// (emit as an error), or MAP_DEFAULT (handle the default way). 46 enum Mapping { 47 MAP_DEFAULT = 0, //< Do not map this diagnostic. 48 MAP_IGNORE = 1, //< Map this diagnostic to nothing, ignore it. 49 MAP_WARNING = 2, //< Map this diagnostic to a warning. 50 MAP_ERROR = 3 //< Map this diagnostic to an error. 51 }; 52 } 53 54/// Diagnostic - This concrete class is used by the front-end to report 55/// problems and issues. It massages the diagnostics (e.g. handling things like 56/// "report warnings as errors" and passes them off to the DiagnosticClient for 57/// reporting to the user. 58class Diagnostic { 59public: 60 /// Level - The level of the diagnostic, after it has been through mapping. 61 enum Level { 62 Ignored, Note, Warning, Error 63 }; 64 65 enum ArgumentKind { 66 ak_std_string, // std::string 67 ak_c_string, // const char * 68 ak_sint, // int 69 ak_uint, // unsigned 70 ak_identifierinfo, // IdentifierInfo 71 ak_qualtype, // QualType 72 ak_declarationname // DeclarationName 73 }; 74 75private: 76 bool IgnoreAllWarnings; // Ignore all warnings: -w 77 bool WarningsAsErrors; // Treat warnings like errors: 78 bool WarnOnExtensions; // Enables warnings for gcc extensions: -pedantic. 79 bool ErrorOnExtensions; // Error on extensions: -pedantic-errors. 80 bool SuppressSystemWarnings;// Suppress warnings in system headers. 81 DiagnosticClient *Client; 82 83 /// DiagMappings - Mapping information for diagnostics. Mapping info is 84 /// packed into two bits per diagnostic. 85 unsigned char DiagMappings[(diag::NUM_BUILTIN_DIAGNOSTICS+3)/4]; 86 87 /// ErrorOccurred - This is set to true when an error is emitted, and is 88 /// sticky. 89 bool ErrorOccurred; 90 91 unsigned NumDiagnostics; // Number of diagnostics reported 92 unsigned NumErrors; // Number of diagnostics that are errors 93 94 /// CustomDiagInfo - Information for uniquing and looking up custom diags. 95 diag::CustomDiagInfo *CustomDiagInfo; 96 97 /// ArgToStringFn - A function pointer that converts an opaque diagnostic 98 /// argument to a strings. This takes the modifiers and argument that was 99 /// present in the diagnostic. 100 /// This is a hack to avoid a layering violation between libbasic and libsema. 101 typedef void (*ArgToStringFnTy)(ArgumentKind Kind, intptr_t Val, 102 const char *Modifier, unsigned ModifierLen, 103 const char *Argument, unsigned ArgumentLen, 104 llvm::SmallVectorImpl<char> &Output); 105 ArgToStringFnTy ArgToStringFn; 106public: 107 explicit Diagnostic(DiagnosticClient *client = 0); 108 ~Diagnostic(); 109 110 //===--------------------------------------------------------------------===// 111 // Diagnostic characterization methods, used by a client to customize how 112 // 113 114 DiagnosticClient *getClient() { return Client; }; 115 const DiagnosticClient *getClient() const { return Client; }; 116 117 void setClient(DiagnosticClient* client) { Client = client; } 118 119 /// setIgnoreAllWarnings - When set to true, any unmapped warnings are 120 /// ignored. If this and WarningsAsErrors are both set, then this one wins. 121 void setIgnoreAllWarnings(bool Val) { IgnoreAllWarnings = Val; } 122 bool getIgnoreAllWarnings() const { return IgnoreAllWarnings; } 123 124 /// setWarningsAsErrors - When set to true, any warnings reported are issued 125 /// as errors. 126 void setWarningsAsErrors(bool Val) { WarningsAsErrors = Val; } 127 bool getWarningsAsErrors() const { return WarningsAsErrors; } 128 129 /// setWarnOnExtensions - When set to true, issue warnings on GCC extensions, 130 /// the equivalent of GCC's -pedantic. 131 void setWarnOnExtensions(bool Val) { WarnOnExtensions = Val; } 132 bool getWarnOnExtensions() const { return WarnOnExtensions; } 133 134 /// setErrorOnExtensions - When set to true issue errors for GCC extensions 135 /// instead of warnings. This is the equivalent to GCC's -pedantic-errors. 136 void setErrorOnExtensions(bool Val) { ErrorOnExtensions = Val; } 137 bool getErrorOnExtensions() const { return ErrorOnExtensions; } 138 139 /// setSuppressSystemWarnings - When set to true mask warnings that 140 /// come from system headers. 141 void setSuppressSystemWarnings(bool Val) { SuppressSystemWarnings = Val; } 142 bool getSuppressSystemWarnings() const { return SuppressSystemWarnings; } 143 144 /// setDiagnosticMapping - This allows the client to specify that certain 145 /// warnings are ignored. Only NOTEs, WARNINGs, and EXTENSIONs can be mapped. 146 void setDiagnosticMapping(diag::kind Diag, diag::Mapping Map) { 147 assert(Diag < diag::NUM_BUILTIN_DIAGNOSTICS && 148 "Can only map builtin diagnostics"); 149 assert(isBuiltinNoteWarningOrExtension(Diag) && "Cannot map errors!"); 150 unsigned char &Slot = DiagMappings[Diag/4]; 151 unsigned Bits = (Diag & 3)*2; 152 Slot &= ~(3 << Bits); 153 Slot |= Map << Bits; 154 } 155 156 /// getDiagnosticMapping - Return the mapping currently set for the specified 157 /// diagnostic. 158 diag::Mapping getDiagnosticMapping(diag::kind Diag) const { 159 return (diag::Mapping)((DiagMappings[Diag/4] >> (Diag & 3)*2) & 3); 160 } 161 162 bool hasErrorOccurred() const { return ErrorOccurred; } 163 164 unsigned getNumErrors() const { return NumErrors; } 165 unsigned getNumDiagnostics() const { return NumDiagnostics; } 166 167 /// getCustomDiagID - Return an ID for a diagnostic with the specified message 168 /// and level. If this is the first request for this diagnosic, it is 169 /// registered and created, otherwise the existing ID is returned. 170 unsigned getCustomDiagID(Level L, const char *Message); 171 172 173 /// ConvertArgToString - This method converts a diagnostic argument (as an 174 /// intptr_t) into the string that represents it. 175 void ConvertArgToString(ArgumentKind Kind, intptr_t Val, 176 const char *Modifier, unsigned ModLen, 177 const char *Argument, unsigned ArgLen, 178 llvm::SmallVectorImpl<char> &Output) const { 179 ArgToStringFn(Kind, Val, Modifier, ModLen, Argument, ArgLen, Output); 180 } 181 182 void SetArgToStringFn(ArgToStringFnTy Fn) { 183 ArgToStringFn = Fn; 184 } 185 186 //===--------------------------------------------------------------------===// 187 // Diagnostic classification and reporting interfaces. 188 // 189 190 /// getDescription - Given a diagnostic ID, return a description of the 191 /// issue. 192 const char *getDescription(unsigned DiagID) const; 193 194 /// isBuiltinNoteWarningOrExtension - Return true if the unmapped diagnostic 195 /// level of the specified diagnostic ID is a Note, Warning, or Extension. 196 /// Note that this only works on builtin diagnostics, not custom ones. 197 static bool isBuiltinNoteWarningOrExtension(unsigned DiagID); 198 199 /// getDiagnosticLevel - Based on the way the client configured the Diagnostic 200 /// object, classify the specified diagnostic ID into a Level, consumable by 201 /// the DiagnosticClient. 202 Level getDiagnosticLevel(unsigned DiagID) const; 203 204 205 /// Report - Issue the message to the client. DiagID is a member of the 206 /// diag::kind enum. This actually returns aninstance of DiagnosticBuilder 207 /// which emits the diagnostics (through ProcessDiag) when it is destroyed. 208 inline DiagnosticBuilder Report(FullSourceLoc Pos, unsigned DiagID); 209 210private: 211 // This is private state used by DiagnosticBuilder. We put it here instead of 212 // in DiagnosticBuilder in order to keep DiagnosticBuilder a small lightweight 213 // object. This implementation choice means that we can only have one 214 // diagnostic "in flight" at a time, but this seems to be a reasonable 215 // tradeoff to keep these objects small. Assertions verify that only one 216 // diagnostic is in flight at a time. 217 friend class DiagnosticBuilder; 218 friend class DiagnosticInfo; 219 220 /// CurDiagLoc - This is the location of the current diagnostic that is in 221 /// flight. 222 FullSourceLoc CurDiagLoc; 223 224 /// CurDiagID - This is the ID of the current diagnostic that is in flight. 225 /// This is set to ~0U when there is no diagnostic in flight. 226 unsigned CurDiagID; 227 228 enum { 229 /// MaxArguments - The maximum number of arguments we can hold. We currently 230 /// only support up to 10 arguments (%0-%9). A single diagnostic with more 231 /// than that almost certainly has to be simplified anyway. 232 MaxArguments = 10 233 }; 234 235 /// NumDiagArgs - This contains the number of entries in Arguments. 236 signed char NumDiagArgs; 237 /// NumRanges - This is the number of ranges in the DiagRanges array. 238 unsigned char NumDiagRanges; 239 240 /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum 241 /// values, with one for each argument. This specifies whether the argument 242 /// is in DiagArgumentsStr or in DiagArguments. 243 unsigned char DiagArgumentsKind[MaxArguments]; 244 245 /// DiagArgumentsStr - This holds the values of each string argument for the 246 /// current diagnostic. This value is only used when the corresponding 247 /// ArgumentKind is ak_std_string. 248 std::string DiagArgumentsStr[MaxArguments]; 249 250 /// DiagArgumentsVal - The values for the various substitution positions. This 251 /// is used when the argument is not an std::string. The specific value is 252 /// mangled into an intptr_t and the intepretation depends on exactly what 253 /// sort of argument kind it is. 254 intptr_t DiagArgumentsVal[MaxArguments]; 255 256 /// DiagRanges - The list of ranges added to this diagnostic. It currently 257 /// only support 10 ranges, could easily be extended if needed. 258 const SourceRange *DiagRanges[10]; 259 260 /// ProcessDiag - This is the method used to report a diagnostic that is 261 /// finally fully formed. 262 void ProcessDiag(); 263}; 264 265//===----------------------------------------------------------------------===// 266// DiagnosticBuilder 267//===----------------------------------------------------------------------===// 268 269/// DiagnosticBuilder - This is a little helper class used to produce 270/// diagnostics. This is constructed by the Diagnostic::Report method, and 271/// allows insertion of extra information (arguments and source ranges) into the 272/// currently "in flight" diagnostic. When the temporary for the builder is 273/// destroyed, the diagnostic is issued. 274/// 275/// Note that many of these will be created as temporary objects (many call 276/// sites), so we want them to be small and we never want their address taken. 277/// This ensures that compilers with somewhat reasonable optimizers will promote 278/// the common fields to registers, eliminating increments of the NumArgs field, 279/// for example. 280class DiagnosticBuilder { 281 mutable Diagnostic *DiagObj; 282 mutable unsigned NumArgs, NumRanges; 283 284 void operator=(const DiagnosticBuilder&); // DO NOT IMPLEMENT 285 friend class Diagnostic; 286 explicit DiagnosticBuilder(Diagnostic *diagObj) 287 : DiagObj(diagObj), NumArgs(0), NumRanges(0) {} 288public: 289 290 /// Copy constructor. When copied, this "takes" the diagnostic info from the 291 /// input and neuters it. 292 DiagnosticBuilder(const DiagnosticBuilder &D) { 293 DiagObj = D.DiagObj; 294 D.DiagObj = 0; 295 } 296 297 /// Destructor - The dtor emits the diagnostic. 298 ~DiagnosticBuilder() { 299 // If DiagObj is null, then its soul was stolen by the copy ctor. 300 if (DiagObj == 0) return; 301 302 // When destroyed, the ~DiagnosticBuilder sets the final argument count into 303 // the Diagnostic object. 304 DiagObj->NumDiagArgs = NumArgs; 305 DiagObj->NumDiagRanges = NumRanges; 306 307 // Process the diagnostic, sending the accumulated information to the 308 // DiagnosticClient. 309 DiagObj->ProcessDiag(); 310 311 // This diagnostic is no longer in flight. 312 DiagObj->CurDiagID = ~0U; 313 } 314 315 /// Operator bool: conversion of DiagnosticBuilder to bool always returns 316 /// true. This allows is to be used in boolean error contexts like: 317 /// return Diag(...); 318 operator bool() const { return true; } 319 320 void AddString(const std::string &S) const { 321 assert(NumArgs < Diagnostic::MaxArguments && 322 "Too many arguments to diagnostic!"); 323 DiagObj->DiagArgumentsKind[NumArgs] = Diagnostic::ak_std_string; 324 DiagObj->DiagArgumentsStr[NumArgs++] = S; 325 } 326 327 void AddTaggedVal(intptr_t V, Diagnostic::ArgumentKind Kind) const { 328 assert(NumArgs < Diagnostic::MaxArguments && 329 "Too many arguments to diagnostic!"); 330 DiagObj->DiagArgumentsKind[NumArgs] = Kind; 331 DiagObj->DiagArgumentsVal[NumArgs++] = V; 332 } 333 334 void AddSourceRange(const SourceRange &R) const { 335 assert(NumRanges < 336 sizeof(DiagObj->DiagRanges)/sizeof(DiagObj->DiagRanges[0]) && 337 "Too many arguments to diagnostic!"); 338 DiagObj->DiagRanges[NumRanges++] = &R; 339 } 340}; 341 342inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 343 const std::string &S) { 344 DB.AddString(S); 345 return DB; 346} 347 348inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 349 const char *Str) { 350 DB.AddTaggedVal(reinterpret_cast<intptr_t>(Str), 351 Diagnostic::ak_c_string); 352 return DB; 353} 354 355inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, int I) { 356 DB.AddTaggedVal(I, Diagnostic::ak_sint); 357 return DB; 358} 359 360inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 361 unsigned I) { 362 DB.AddTaggedVal(I, Diagnostic::ak_uint); 363 return DB; 364} 365 366inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 367 const IdentifierInfo *II) { 368 DB.AddTaggedVal(reinterpret_cast<intptr_t>(II), 369 Diagnostic::ak_identifierinfo); 370 return DB; 371} 372 373inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 374 const SourceRange &R) { 375 DB.AddSourceRange(R); 376 return DB; 377} 378 379 380/// Report - Issue the message to the client. DiagID is a member of the 381/// diag::kind enum. This actually returns a new instance of DiagnosticBuilder 382/// which emits the diagnostics (through ProcessDiag) when it is destroyed. 383inline DiagnosticBuilder Diagnostic::Report(FullSourceLoc Loc, unsigned DiagID){ 384 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!"); 385 CurDiagLoc = Loc; 386 CurDiagID = DiagID; 387 return DiagnosticBuilder(this); 388} 389 390//===----------------------------------------------------------------------===// 391// DiagnosticInfo 392//===----------------------------------------------------------------------===// 393 394/// DiagnosticInfo - This is a little helper class (which is basically a smart 395/// pointer that forward info from Diagnostic) that allows clients ot enquire 396/// about the currently in-flight diagnostic. 397class DiagnosticInfo { 398 const Diagnostic *DiagObj; 399public: 400 explicit DiagnosticInfo(const Diagnostic *DO) : DiagObj(DO) {} 401 402 const Diagnostic *getDiags() const { return DiagObj; } 403 unsigned getID() const { return DiagObj->CurDiagID; } 404 const FullSourceLoc &getLocation() const { return DiagObj->CurDiagLoc; } 405 406 unsigned getNumArgs() const { return DiagObj->NumDiagArgs; } 407 408 /// getArgKind - Return the kind of the specified index. Based on the kind 409 /// of argument, the accessors below can be used to get the value. 410 Diagnostic::ArgumentKind getArgKind(unsigned Idx) const { 411 assert(Idx < getNumArgs() && "Argument index out of range!"); 412 return (Diagnostic::ArgumentKind)DiagObj->DiagArgumentsKind[Idx]; 413 } 414 415 /// getArgStdStr - Return the provided argument string specified by Idx. 416 const std::string &getArgStdStr(unsigned Idx) const { 417 assert(getArgKind(Idx) == Diagnostic::ak_std_string && 418 "invalid argument accessor!"); 419 return DiagObj->DiagArgumentsStr[Idx]; 420 } 421 422 /// getArgCStr - Return the specified C string argument. 423 const char *getArgCStr(unsigned Idx) const { 424 assert(getArgKind(Idx) == Diagnostic::ak_c_string && 425 "invalid argument accessor!"); 426 return reinterpret_cast<const char*>(DiagObj->DiagArgumentsVal[Idx]); 427 } 428 429 /// getArgSInt - Return the specified signed integer argument. 430 int getArgSInt(unsigned Idx) const { 431 assert(getArgKind(Idx) == Diagnostic::ak_sint && 432 "invalid argument accessor!"); 433 return (int)DiagObj->DiagArgumentsVal[Idx]; 434 } 435 436 /// getArgUInt - Return the specified unsigned integer argument. 437 unsigned getArgUInt(unsigned Idx) const { 438 assert(getArgKind(Idx) == Diagnostic::ak_uint && 439 "invalid argument accessor!"); 440 return (unsigned)DiagObj->DiagArgumentsVal[Idx]; 441 } 442 443 /// getArgIdentifier - Return the specified IdentifierInfo argument. 444 const IdentifierInfo *getArgIdentifier(unsigned Idx) const { 445 assert(getArgKind(Idx) == Diagnostic::ak_identifierinfo && 446 "invalid argument accessor!"); 447 return reinterpret_cast<IdentifierInfo*>(DiagObj->DiagArgumentsVal[Idx]); 448 } 449 450 /// getRawArg - Return the specified non-string argument in an opaque form. 451 intptr_t getRawArg(unsigned Idx) const { 452 assert(getArgKind(Idx) != Diagnostic::ak_std_string && 453 "invalid argument accessor!"); 454 return DiagObj->DiagArgumentsVal[Idx]; 455 } 456 457 458 /// getNumRanges - Return the number of source ranges associated with this 459 /// diagnostic. 460 unsigned getNumRanges() const { 461 return DiagObj->NumDiagRanges; 462 } 463 464 const SourceRange &getRange(unsigned Idx) const { 465 assert(Idx < DiagObj->NumDiagRanges && "Invalid diagnostic range index!"); 466 return *DiagObj->DiagRanges[Idx]; 467 } 468 469 470 /// FormatDiagnostic - Format this diagnostic into a string, substituting the 471 /// formal arguments into the %0 slots. The result is appended onto the Str 472 /// array. 473 void FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const; 474}; 475 476 477/// DiagnosticClient - This is an abstract interface implemented by clients of 478/// the front-end, which formats and prints fully processed diagnostics. 479class DiagnosticClient { 480public: 481 virtual ~DiagnosticClient(); 482 483 /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or 484 /// capturing it to a log as needed. 485 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel, 486 const DiagnosticInfo &Info) = 0; 487}; 488 489} // end namespace clang 490 491#endif 492