Diagnostic.cpp revision 3cbfe2c4159e0a219ae660d50625c013aa4afbd0
1//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===// 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 implements the Diagnostic-related interfaces. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Basic/Diagnostic.h" 15#include "clang/Basic/IdentifierTable.h" 16#include "clang/Basic/SourceLocation.h" 17#include "llvm/ADT/SmallVector.h" 18#include "llvm/ADT/StringExtras.h" 19#include <vector> 20#include <map> 21#include <cstring> 22using namespace clang; 23 24//===----------------------------------------------------------------------===// 25// Builtin Diagnostic information 26//===----------------------------------------------------------------------===// 27 28/// Flag values for diagnostics. 29enum { 30 // Diagnostic classes. 31 NOTE = 0x01, 32 WARNING = 0x02, 33 EXTENSION = 0x03, 34 EXTWARN = 0x04, 35 ERROR = 0x05, 36 class_mask = 0x07 37}; 38 39/// DiagnosticFlags - A set of flags, or'd together, that describe the 40/// diagnostic. 41static unsigned char DiagnosticFlags[] = { 42#define DIAG(ENUM,FLAGS,DESC) FLAGS, 43#include "clang/Basic/DiagnosticKinds.def" 44 0 45}; 46 47/// getDiagClass - Return the class field of the diagnostic. 48/// 49static unsigned getBuiltinDiagClass(unsigned DiagID) { 50 assert(DiagID < diag::NUM_BUILTIN_DIAGNOSTICS && 51 "Diagnostic ID out of range!"); 52 return DiagnosticFlags[DiagID] & class_mask; 53} 54 55/// DiagnosticText - An english message to print for the diagnostic. These 56/// should be localized. 57static const char * const DiagnosticText[] = { 58#define DIAG(ENUM,FLAGS,DESC) DESC, 59#include "clang/Basic/DiagnosticKinds.def" 60 0 61}; 62 63//===----------------------------------------------------------------------===// 64// Custom Diagnostic information 65//===----------------------------------------------------------------------===// 66 67namespace clang { 68 namespace diag { 69 class CustomDiagInfo { 70 typedef std::pair<Diagnostic::Level, std::string> DiagDesc; 71 std::vector<DiagDesc> DiagInfo; 72 std::map<DiagDesc, unsigned> DiagIDs; 73 public: 74 75 /// getDescription - Return the description of the specified custom 76 /// diagnostic. 77 const char *getDescription(unsigned DiagID) const { 78 assert(this && DiagID-diag::NUM_BUILTIN_DIAGNOSTICS < DiagInfo.size() && 79 "Invalid diagnosic ID"); 80 return DiagInfo[DiagID-diag::NUM_BUILTIN_DIAGNOSTICS].second.c_str(); 81 } 82 83 /// getLevel - Return the level of the specified custom diagnostic. 84 Diagnostic::Level getLevel(unsigned DiagID) const { 85 assert(this && DiagID-diag::NUM_BUILTIN_DIAGNOSTICS < DiagInfo.size() && 86 "Invalid diagnosic ID"); 87 return DiagInfo[DiagID-diag::NUM_BUILTIN_DIAGNOSTICS].first; 88 } 89 90 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message, 91 Diagnostic &Diags) { 92 DiagDesc D(L, Message); 93 // Check to see if it already exists. 94 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); 95 if (I != DiagIDs.end() && I->first == D) 96 return I->second; 97 98 // If not, assign a new ID. 99 unsigned ID = DiagInfo.size()+diag::NUM_BUILTIN_DIAGNOSTICS; 100 DiagIDs.insert(std::make_pair(D, ID)); 101 DiagInfo.push_back(D); 102 103 // If this is a warning, and all warnings are supposed to map to errors, 104 // insert the mapping now. 105 if (L == Diagnostic::Warning && Diags.getWarningsAsErrors()) 106 Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR); 107 return ID; 108 } 109 }; 110 111 } // end diag namespace 112} // end clang namespace 113 114 115//===----------------------------------------------------------------------===// 116// Common Diagnostic implementation 117//===----------------------------------------------------------------------===// 118 119Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) { 120 IgnoreAllWarnings = false; 121 WarningsAsErrors = false; 122 WarnOnExtensions = false; 123 ErrorOnExtensions = false; 124 SuppressSystemWarnings = false; 125 // Clear all mappings, setting them to MAP_DEFAULT. 126 memset(DiagMappings, 0, sizeof(DiagMappings)); 127 128 ErrorOccurred = false; 129 NumDiagnostics = 0; 130 NumErrors = 0; 131 CustomDiagInfo = 0; 132 CurDiagID = ~0U; 133} 134 135Diagnostic::~Diagnostic() { 136 delete CustomDiagInfo; 137} 138 139/// getCustomDiagID - Return an ID for a diagnostic with the specified message 140/// and level. If this is the first request for this diagnosic, it is 141/// registered and created, otherwise the existing ID is returned. 142unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) { 143 if (CustomDiagInfo == 0) 144 CustomDiagInfo = new diag::CustomDiagInfo(); 145 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this); 146} 147 148 149/// isBuiltinNoteWarningOrExtension - Return true if the unmapped diagnostic 150/// level of the specified diagnostic ID is a Note, Warning, or Extension. 151/// Note that this only works on builtin diagnostics, not custom ones. 152bool Diagnostic::isBuiltinNoteWarningOrExtension(unsigned DiagID) { 153 return DiagID < diag::NUM_BUILTIN_DIAGNOSTICS && 154 getBuiltinDiagClass(DiagID) < ERROR; 155} 156 157 158/// getDescription - Given a diagnostic ID, return a description of the 159/// issue. 160const char *Diagnostic::getDescription(unsigned DiagID) const { 161 if (DiagID < diag::NUM_BUILTIN_DIAGNOSTICS) 162 return DiagnosticText[DiagID]; 163 else 164 return CustomDiagInfo->getDescription(DiagID); 165} 166 167/// getDiagnosticLevel - Based on the way the client configured the Diagnostic 168/// object, classify the specified diagnostic ID into a Level, consumable by 169/// the DiagnosticClient. 170Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const { 171 // Handle custom diagnostics, which cannot be mapped. 172 if (DiagID >= diag::NUM_BUILTIN_DIAGNOSTICS) 173 return CustomDiagInfo->getLevel(DiagID); 174 175 unsigned DiagClass = getBuiltinDiagClass(DiagID); 176 177 // Specific non-error diagnostics may be mapped to various levels from ignored 178 // to error. 179 if (DiagClass < ERROR) { 180 switch (getDiagnosticMapping((diag::kind)DiagID)) { 181 case diag::MAP_DEFAULT: break; 182 case diag::MAP_IGNORE: return Diagnostic::Ignored; 183 case diag::MAP_WARNING: DiagClass = WARNING; break; 184 case diag::MAP_ERROR: DiagClass = ERROR; break; 185 } 186 } 187 188 // Map diagnostic classes based on command line argument settings. 189 if (DiagClass == EXTENSION) { 190 if (ErrorOnExtensions) 191 DiagClass = ERROR; 192 else if (WarnOnExtensions) 193 DiagClass = WARNING; 194 else 195 return Ignored; 196 } else if (DiagClass == EXTWARN) { 197 DiagClass = ErrorOnExtensions ? ERROR : WARNING; 198 } 199 200 // If warnings are globally mapped to ignore or error, do it. 201 if (DiagClass == WARNING) { 202 if (IgnoreAllWarnings) 203 return Diagnostic::Ignored; 204 if (WarningsAsErrors) 205 DiagClass = ERROR; 206 } 207 208 switch (DiagClass) { 209 default: assert(0 && "Unknown diagnostic class!"); 210 case NOTE: return Diagnostic::Note; 211 case WARNING: return Diagnostic::Warning; 212 case ERROR: return Diagnostic::Error; 213 } 214} 215 216/// ProcessDiag - This is the method used to report a diagnostic that is 217/// finally fully formed. 218void Diagnostic::ProcessDiag() { 219 DiagnosticInfo Info(this); 220 221 // Figure out the diagnostic level of this message. 222 Diagnostic::Level DiagLevel = getDiagnosticLevel(Info.getID()); 223 224 // If the client doesn't care about this message, don't issue it. 225 if (DiagLevel == Diagnostic::Ignored) 226 return; 227 228 // If this is not an error and we are in a system header, ignore it. We 229 // have to check on the original Diag ID here, because we also want to 230 // ignore extensions and warnings in -Werror and -pedantic-errors modes, 231 // which *map* warnings/extensions to errors. 232 if (SuppressSystemWarnings && 233 Info.getID() < diag::NUM_BUILTIN_DIAGNOSTICS && 234 getBuiltinDiagClass(Info.getID()) != ERROR && 235 Info.getLocation().isValid() && 236 Info.getLocation().getPhysicalLoc().isInSystemHeader()) 237 return; 238 239 if (DiagLevel >= Diagnostic::Error) { 240 ErrorOccurred = true; 241 242 ++NumErrors; 243 } 244 245 // Finally, report it. 246 Client->HandleDiagnostic(DiagLevel, Info); 247 ++NumDiagnostics; 248} 249 250 251DiagnosticClient::~DiagnosticClient() {} 252 253 254/// ModifierIs - Return true if the specified modifier matches specified string. 255template <std::size_t StrLen> 256static bool ModifierIs(const char *Modifier, unsigned ModifierLen, 257 const char (&Str)[StrLen]) { 258 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); 259} 260 261/// HandleSelectModifier - Handle the integer 'select' modifier. This is used 262/// like this: %select{foo|bar|baz}2. This means that the integer argument 263/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. 264/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. 265/// This is very useful for certain classes of variant diagnostics. 266static void HandleSelectModifier(unsigned ValNo, 267 const char *Argument, unsigned ArgumentLen, 268 llvm::SmallVectorImpl<char> &OutStr) { 269 const char *ArgumentEnd = Argument+ArgumentLen; 270 271 // Skip over 'ValNo' |'s. 272 while (ValNo) { 273 const char *NextVal = std::find(Argument, ArgumentEnd, '|'); 274 assert(NextVal != ArgumentEnd && "Value for integer select modifier was" 275 " larger than the number of options in the diagnostic string!"); 276 Argument = NextVal+1; // Skip this string. 277 --ValNo; 278 } 279 280 // Get the end of the value. This is either the } or the |. 281 const char *EndPtr = std::find(Argument, ArgumentEnd, '|'); 282 // Add the value to the output string. 283 OutStr.append(Argument, EndPtr); 284} 285 286/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the 287/// letter 's' to the string if the value is not 1. This is used in cases like 288/// this: "you idiot, you have %4 parameter%s4!". 289static void HandleIntegerSModifier(unsigned ValNo, 290 llvm::SmallVectorImpl<char> &OutStr) { 291 if (ValNo != 1) 292 OutStr.push_back('s'); 293} 294 295 296/// FormatDiagnostic - Format this diagnostic into a string, substituting the 297/// formal arguments into the %0 slots. The result is appended onto the Str 298/// array. 299void DiagnosticInfo:: 300FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const { 301 const char *DiagStr = getDiags()->getDescription(getID()); 302 const char *DiagEnd = DiagStr+strlen(DiagStr); 303 304 while (DiagStr != DiagEnd) { 305 if (DiagStr[0] != '%') { 306 // Append non-%0 substrings to Str if we have one. 307 const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); 308 OutStr.append(DiagStr, StrEnd); 309 DiagStr = StrEnd; 310 continue; 311 } else if (DiagStr[1] == '%') { 312 OutStr.push_back('%'); // %% -> %. 313 DiagStr += 2; 314 continue; 315 } 316 317 // Skip the %. 318 ++DiagStr; 319 320 // This must be a placeholder for a diagnostic argument. The format for a 321 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". 322 // The digit is a number from 0-9 indicating which argument this comes from. 323 // The modifier is a string of digits from the set [-a-z]+, arguments is a 324 // brace enclosed string. 325 const char *Modifier = 0, *Argument = 0; 326 unsigned ModifierLen = 0, ArgumentLen = 0; 327 328 // Check to see if we have a modifier. If so eat it. 329 if (!isdigit(DiagStr[0])) { 330 Modifier = DiagStr; 331 while (DiagStr[0] == '-' || 332 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) 333 ++DiagStr; 334 ModifierLen = DiagStr-Modifier; 335 336 // If we have an argument, get it next. 337 if (DiagStr[0] == '{') { 338 ++DiagStr; // Skip {. 339 Argument = DiagStr; 340 341 for (; DiagStr[0] != '}'; ++DiagStr) 342 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!"); 343 ArgumentLen = DiagStr-Argument; 344 ++DiagStr; // Skip }. 345 } 346 } 347 348 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic"); 349 unsigned StrNo = *DiagStr++ - '0'; 350 351 switch (getArgKind(StrNo)) { 352 case Diagnostic::ak_std_string: { 353 const std::string &S = getArgStdStr(StrNo); 354 assert(ModifierLen == 0 && "No modifiers for strings yet"); 355 OutStr.append(S.begin(), S.end()); 356 break; 357 } 358 case Diagnostic::ak_c_string: { 359 const char *S = getArgCStr(StrNo); 360 assert(ModifierLen == 0 && "No modifiers for strings yet"); 361 OutStr.append(S, S + strlen(S)); 362 break; 363 } 364 case Diagnostic::ak_identifierinfo: { 365 const IdentifierInfo *II = getArgIdentifier(StrNo); 366 assert(ModifierLen == 0 && "No modifiers for strings yet"); 367 OutStr.append(II->getName(), II->getName() + II->getLength()); 368 break; 369 } 370 case Diagnostic::ak_sint: { 371 int Val = getArgSInt(StrNo); 372 373 if (ModifierIs(Modifier, ModifierLen, "select")) { 374 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr); 375 } else if (ModifierIs(Modifier, ModifierLen, "s")) { 376 HandleIntegerSModifier(Val, OutStr); 377 } else { 378 assert(ModifierLen == 0 && "Unknown integer modifier"); 379 // FIXME: Optimize 380 std::string S = llvm::itostr(Val); 381 OutStr.append(S.begin(), S.end()); 382 } 383 break; 384 } 385 case Diagnostic::ak_uint: { 386 unsigned Val = getArgUInt(StrNo); 387 388 if (ModifierIs(Modifier, ModifierLen, "select")) { 389 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr); 390 } else if (ModifierIs(Modifier, ModifierLen, "s")) { 391 HandleIntegerSModifier(Val, OutStr); 392 } else { 393 assert(ModifierLen == 0 && "Unknown integer modifier"); 394 395 // FIXME: Optimize 396 std::string S = llvm::utostr_32(Val); 397 OutStr.append(S.begin(), S.end()); 398 break; 399 } 400 } 401 } 402 } 403} 404