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