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