Diagnostic.h revision b7fc3b87d065041a10eaa0603d738df21ff7af3a
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 DiagnosticInfo; 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 a new instance of DiagnosticInfo 174 /// which emits the diagnostics (through ProcessDiag) when it is destroyed. 175 inline DiagnosticInfo Report(FullSourceLoc Pos, unsigned DiagID); 176 177private: 178 // This is private state used by DiagnosticInfo. We put it here instead of 179 // in DiagnosticInfo in order to keep DiagnosticInfo a small light-weight 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 DiagnosticInfo; 185 186 /// CurDiagLoc - This is the location of the current diagnostic that is in 187 /// flight. 188 FullSourceLoc CurDiagLoc; 189 190 /// CurDiagID - This is the ID of the current diagnostic that is in flight. 191 unsigned CurDiagID; 192 193 enum { 194 /// MaxArguments - The maximum number of arguments we can hold. We currently 195 /// only support up to 10 arguments (%0-%9). A single diagnostic with more 196 /// than that almost certainly has to be simplified anyway. 197 MaxArguments = 10 198 }; 199 200 /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum 201 /// values, with one for each argument. This specifies whether the argument 202 /// is in DiagArgumentsStr or in DiagArguments. 203 unsigned char DiagArgumentsKind[MaxArguments]; 204 205 /// DiagArgumentsStr - This holds the values of each string argument for the 206 /// current diagnostic. This value is only used when the corresponding 207 /// ArgumentKind is ak_std_string. 208 std::string DiagArgumentsStr[MaxArguments]; 209 210 /// DiagArgumentsVal - The values for the various substitution positions. This 211 /// is used when the argument is not an std::string. The specific value is 212 /// mangled into an intptr_t and the intepretation depends on exactly what 213 /// sort of argument kind it is. 214 intptr_t DiagArgumentsVal[MaxArguments]; 215 216 /// DiagRanges - The list of ranges added to this diagnostic. It currently 217 /// only support 10 ranges, could easily be extended if needed. 218 const SourceRange *DiagRanges[10]; 219 220 /// NumDiagArgs - This is set to -1 when no diag is in flight. Otherwise it 221 /// is 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 /// ProcessDiag - This is the method used to report a diagnostic that is 227 /// finally fully formed. 228 void ProcessDiag(const DiagnosticInfo &Info); 229}; 230 231/// DiagnosticInfo - This is a little helper class used to produce diagnostics. 232/// This is constructed with an ID and location, and then has some number of 233/// arguments (for %0 substitution) and SourceRanges added to it with the 234/// overloaded operator<<. Once it is destroyed, it emits the diagnostic with 235/// the accumulated information. 236/// 237/// Note that many of these will be created as temporary objects (many call 238/// sites), so we want them to be small to reduce stack space usage etc. For 239/// this reason, we stick state in the Diagnostic class, see the comment there 240/// for more info. 241class DiagnosticInfo { 242 mutable Diagnostic *DiagObj; 243 void operator=(const DiagnosticInfo&); // DO NOT IMPLEMENT 244public: 245 enum ArgumentKind { 246 ak_std_string, // std::string 247 ak_c_string, // const char * 248 ak_sint, // int 249 ak_uint, // unsigned 250 ak_identifierinfo // IdentifierInfo 251 }; 252 253 254 DiagnosticInfo(Diagnostic *diagObj, FullSourceLoc Loc, unsigned DiagID) : 255 DiagObj(diagObj) { 256 if (DiagObj == 0) return; 257 assert(DiagObj->NumDiagArgs == -1 && 258 "Multiple diagnostics in flight at once!"); 259 DiagObj->NumDiagArgs = DiagObj->NumDiagRanges = 0; 260 DiagObj->CurDiagLoc = Loc; 261 DiagObj->CurDiagID = DiagID; 262 } 263 264 /// Copy constructor. When copied, this "takes" the diagnostic info from the 265 /// input and neuters it. 266 DiagnosticInfo(const DiagnosticInfo &D) { 267 DiagObj = D.DiagObj; 268 D.DiagObj = 0; 269 } 270 271 /// Destructor - The dtor emits the diagnostic. 272 ~DiagnosticInfo() { 273 // If DiagObj is null, then its soul was stolen by the copy ctor. 274 if (!DiagObj) return; 275 276 DiagObj->ProcessDiag(*this); 277 278 // This diagnostic is no longer in flight. 279 DiagObj->NumDiagArgs = -1; 280 } 281 282 const Diagnostic *getDiags() const { return DiagObj; } 283 unsigned getID() const { return DiagObj->CurDiagID; } 284 const FullSourceLoc &getLocation() const { return DiagObj->CurDiagLoc; } 285 286 /// Operator bool: conversion of DiagnosticInfo to bool always returns true. 287 /// This allows is to be used in boolean error contexts like: 288 /// return Diag(...); 289 operator bool() const { return true; } 290 291 unsigned getNumArgs() const { return DiagObj->NumDiagArgs; } 292 293 294 /// getArgKind - Return the kind of the specified index. Based on the kind 295 /// of argument, the accessors below can be used to get the value. 296 ArgumentKind getArgKind(unsigned Idx) const { 297 assert((signed char)Idx < DiagObj->NumDiagArgs && 298 "Argument index out of range!"); 299 return (ArgumentKind)DiagObj->DiagArgumentsKind[Idx]; 300 } 301 302 /// getArgStdStr - Return the provided argument string specified by Idx. 303 const std::string &getArgStdStr(unsigned Idx) const { 304 assert(getArgKind(Idx) == ak_std_string && "invalid argument accessor!"); 305 return DiagObj->DiagArgumentsStr[Idx]; 306 } 307 308 /// getArgCStr - Return the specified C string argument. 309 const char *getArgCStr(unsigned Idx) const { 310 assert(getArgKind(Idx) == ak_c_string && "invalid argument accessor!"); 311 return reinterpret_cast<const char*>(DiagObj->DiagArgumentsVal[Idx]); 312 } 313 314 /// getArgSInt - Return the specified signed integer argument. 315 int getArgSInt(unsigned Idx) const { 316 assert(getArgKind(Idx) == ak_sint && "invalid argument accessor!"); 317 return (int)DiagObj->DiagArgumentsVal[Idx]; 318 } 319 320 /// getArgUInt - Return the specified unsigned integer argument. 321 unsigned getArgUInt(unsigned Idx) const { 322 assert(getArgKind(Idx) == ak_uint && "invalid argument accessor!"); 323 return (unsigned)DiagObj->DiagArgumentsVal[Idx]; 324 } 325 326 /// getArgIdentifier - Return the specified IdentifierInfo argument. 327 const IdentifierInfo *getArgIdentifier(unsigned Idx) const { 328 assert(getArgKind(Idx) == ak_identifierinfo &&"invalid argument accessor!"); 329 return reinterpret_cast<const IdentifierInfo*>( 330 DiagObj->DiagArgumentsVal[Idx]); 331 } 332 333 /// getNumRanges - Return the number of source ranges associated with this 334 /// diagnostic. 335 unsigned getNumRanges() const { 336 return DiagObj->NumDiagRanges; 337 } 338 339 const SourceRange &getRange(unsigned Idx) const { 340 assert(Idx < DiagObj->NumDiagRanges && "Invalid diagnostic range index!"); 341 return *DiagObj->DiagRanges[Idx]; 342 } 343 344 void AddString(const std::string &S) const { 345 assert((unsigned)DiagObj->NumDiagArgs < Diagnostic::MaxArguments && 346 "Too many arguments to diagnostic!"); 347 DiagObj->DiagArgumentsKind[DiagObj->NumDiagArgs] = ak_std_string; 348 DiagObj->DiagArgumentsStr[DiagObj->NumDiagArgs++] = S; 349 } 350 351 void AddTaggedVal(intptr_t V, ArgumentKind Kind) const { 352 assert((unsigned)DiagObj->NumDiagArgs < Diagnostic::MaxArguments && 353 "Too many arguments to diagnostic!"); 354 DiagObj->DiagArgumentsKind[DiagObj->NumDiagArgs] = Kind; 355 DiagObj->DiagArgumentsVal[DiagObj->NumDiagArgs++] = V; 356 } 357 358 void AddSourceRange(const SourceRange &R) const { 359 assert((unsigned)DiagObj->NumDiagArgs < 360 sizeof(DiagObj->DiagRanges)/sizeof(DiagObj->DiagRanges[0]) && 361 "Too many arguments to diagnostic!"); 362 DiagObj->DiagRanges[DiagObj->NumDiagRanges++] = &R; 363 } 364 365 /// FormatDiagnostic - Format this diagnostic into a string, substituting the 366 /// formal arguments into the %0 slots. The result is appended onto the Str 367 /// array. 368 void FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const; 369}; 370 371inline const DiagnosticInfo &operator<<(const DiagnosticInfo &DI, 372 const std::string &S) { 373 DI.AddString(S); 374 return DI; 375} 376 377inline const DiagnosticInfo &operator<<(const DiagnosticInfo &DI, 378 const char *Str) { 379 DI.AddTaggedVal(reinterpret_cast<intptr_t>(Str), DiagnosticInfo::ak_c_string); 380 return DI; 381} 382 383inline const DiagnosticInfo &operator<<(const DiagnosticInfo &DI, int I) { 384 DI.AddTaggedVal(I, DiagnosticInfo::ak_sint); 385 return DI; 386} 387 388inline const DiagnosticInfo &operator<<(const DiagnosticInfo &DI, unsigned I) { 389 DI.AddTaggedVal(I, DiagnosticInfo::ak_uint); 390 return DI; 391} 392 393inline const DiagnosticInfo &operator<<(const DiagnosticInfo &DI, 394 const IdentifierInfo *II){ 395 DI.AddTaggedVal(reinterpret_cast<intptr_t>(II), 396 DiagnosticInfo::ak_identifierinfo); 397 return DI; 398} 399 400inline const DiagnosticInfo &operator<<(const DiagnosticInfo &DI, 401 const SourceRange &R) { 402 DI.AddSourceRange(R); 403 return DI; 404} 405 406 407/// Report - Issue the message to the client. DiagID is a member of the 408/// diag::kind enum. This actually returns a new instance of DiagnosticInfo 409/// which emits the diagnostics (through ProcessDiag) when it is destroyed. 410inline DiagnosticInfo Diagnostic::Report(FullSourceLoc Pos, unsigned DiagID) { 411 return DiagnosticInfo(this, Pos, DiagID); 412} 413 414 415/// DiagnosticClient - This is an abstract interface implemented by clients of 416/// the front-end, which formats and prints fully processed diagnostics. 417class DiagnosticClient { 418public: 419 virtual ~DiagnosticClient(); 420 421 /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or 422 /// capturing it to a log as needed. 423 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel, 424 const DiagnosticInfo &Info) = 0; 425}; 426 427} // end namespace clang 428 429#endif 430