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