Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Diagnostic-related interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 15 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | #include <map> |
Chris Lattner | 87cf5ac | 2008-03-10 17:04:53 +0000 | [diff] [blame] | 21 | #include <cstring> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Builtin Diagnostic information |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | /// Flag values for diagnostics. |
| 29 | enum { |
| 30 | // Diagnostic classes. |
| 31 | NOTE = 0x01, |
| 32 | WARNING = 0x02, |
| 33 | EXTENSION = 0x03, |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 34 | EXTWARN = 0x04, |
| 35 | ERROR = 0x05, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | class_mask = 0x07 |
| 37 | }; |
| 38 | |
| 39 | /// DiagnosticFlags - A set of flags, or'd together, that describe the |
| 40 | /// diagnostic. |
| 41 | static 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 | /// |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 49 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
| 50 | assert(DiagID < diag::NUM_BUILTIN_DIAGNOSTICS && |
| 51 | "Diagnostic ID out of range!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | return DiagnosticFlags[DiagID] & class_mask; |
| 53 | } |
| 54 | |
| 55 | /// DiagnosticText - An english message to print for the diagnostic. These |
| 56 | /// should be localized. |
| 57 | static const char * const DiagnosticText[] = { |
| 58 | #define DIAG(ENUM,FLAGS,DESC) DESC, |
| 59 | #include "clang/Basic/DiagnosticKinds.def" |
| 60 | 0 |
| 61 | }; |
| 62 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 63 | //===----------------------------------------------------------------------===// |
| 64 | // Custom Diagnostic information |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | |
| 67 | namespace 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 | |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 90 | unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message, |
| 91 | Diagnostic &Diags) { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 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); |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 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); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 107 | return ID; |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | } // end diag namespace |
| 112 | } // end clang namespace |
| 113 | |
| 114 | |
| 115 | //===----------------------------------------------------------------------===// |
| 116 | // Common Diagnostic implementation |
| 117 | //===----------------------------------------------------------------------===// |
| 118 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 119 | static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT, |
| 120 | const char *Modifier, unsigned ML, |
| 121 | const char *Argument, unsigned ArgLen, |
| 122 | llvm::SmallVectorImpl<char> &Output) { |
| 123 | const char *Str = "<can't format argument>"; |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 124 | Output.append(Str, Str+strlen(Str)); |
| 125 | } |
| 126 | |
| 127 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 128 | Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) { |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 129 | IgnoreAllWarnings = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | WarningsAsErrors = false; |
| 131 | WarnOnExtensions = false; |
| 132 | ErrorOnExtensions = false; |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 133 | SuppressSystemWarnings = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | // Clear all mappings, setting them to MAP_DEFAULT. |
| 135 | memset(DiagMappings, 0, sizeof(DiagMappings)); |
| 136 | |
| 137 | ErrorOccurred = false; |
| 138 | NumDiagnostics = 0; |
| 139 | NumErrors = 0; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 140 | CustomDiagInfo = 0; |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 141 | CurDiagID = ~0U; |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 143 | ArgToStringFn = DummyArgToStringFn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 146 | Diagnostic::~Diagnostic() { |
| 147 | delete CustomDiagInfo; |
| 148 | } |
| 149 | |
| 150 | /// getCustomDiagID - Return an ID for a diagnostic with the specified message |
| 151 | /// and level. If this is the first request for this diagnosic, it is |
| 152 | /// registered and created, otherwise the existing ID is returned. |
| 153 | unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) { |
| 154 | if (CustomDiagInfo == 0) |
| 155 | CustomDiagInfo = new diag::CustomDiagInfo(); |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 156 | return CustomDiagInfo->getOrCreateDiagID(L, Message, *this); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 160 | /// isBuiltinNoteWarningOrExtension - Return true if the unmapped diagnostic |
| 161 | /// level of the specified diagnostic ID is a Note, Warning, or Extension. |
| 162 | /// Note that this only works on builtin diagnostics, not custom ones. |
| 163 | bool Diagnostic::isBuiltinNoteWarningOrExtension(unsigned DiagID) { |
| 164 | return DiagID < diag::NUM_BUILTIN_DIAGNOSTICS && |
| 165 | getBuiltinDiagClass(DiagID) < ERROR; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
| 169 | /// getDescription - Given a diagnostic ID, return a description of the |
| 170 | /// issue. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 171 | const char *Diagnostic::getDescription(unsigned DiagID) const { |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 172 | if (DiagID < diag::NUM_BUILTIN_DIAGNOSTICS) |
| 173 | return DiagnosticText[DiagID]; |
| 174 | else |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 175 | return CustomDiagInfo->getDescription(DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 179 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 180 | /// the DiagnosticClient. |
| 181 | Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 182 | // Handle custom diagnostics, which cannot be mapped. |
| 183 | if (DiagID >= diag::NUM_BUILTIN_DIAGNOSTICS) |
| 184 | return CustomDiagInfo->getLevel(DiagID); |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 185 | |
| 186 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | |
| 188 | // Specific non-error diagnostics may be mapped to various levels from ignored |
| 189 | // to error. |
| 190 | if (DiagClass < ERROR) { |
| 191 | switch (getDiagnosticMapping((diag::kind)DiagID)) { |
| 192 | case diag::MAP_DEFAULT: break; |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 193 | case diag::MAP_IGNORE: return Diagnostic::Ignored; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | case diag::MAP_WARNING: DiagClass = WARNING; break; |
| 195 | case diag::MAP_ERROR: DiagClass = ERROR; break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Map diagnostic classes based on command line argument settings. |
| 200 | if (DiagClass == EXTENSION) { |
| 201 | if (ErrorOnExtensions) |
| 202 | DiagClass = ERROR; |
| 203 | else if (WarnOnExtensions) |
| 204 | DiagClass = WARNING; |
| 205 | else |
| 206 | return Ignored; |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 207 | } else if (DiagClass == EXTWARN) { |
| 208 | DiagClass = ErrorOnExtensions ? ERROR : WARNING; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 211 | // If warnings are globally mapped to ignore or error, do it. |
| 212 | if (DiagClass == WARNING) { |
| 213 | if (IgnoreAllWarnings) |
| 214 | return Diagnostic::Ignored; |
| 215 | if (WarningsAsErrors) |
| 216 | DiagClass = ERROR; |
| 217 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 218 | |
| 219 | switch (DiagClass) { |
| 220 | default: assert(0 && "Unknown diagnostic class!"); |
| 221 | case NOTE: return Diagnostic::Note; |
| 222 | case WARNING: return Diagnostic::Warning; |
| 223 | case ERROR: return Diagnostic::Error; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 227 | /// ProcessDiag - This is the method used to report a diagnostic that is |
| 228 | /// finally fully formed. |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 229 | void Diagnostic::ProcessDiag() { |
| 230 | DiagnosticInfo Info(this); |
| 231 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 232 | // Figure out the diagnostic level of this message. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 233 | Diagnostic::Level DiagLevel = getDiagnosticLevel(Info.getID()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | |
| 235 | // If the client doesn't care about this message, don't issue it. |
| 236 | if (DiagLevel == Diagnostic::Ignored) |
| 237 | return; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 238 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 239 | // If this is not an error and we are in a system header, ignore it. We |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 240 | // have to check on the original Diag ID here, because we also want to |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 241 | // ignore extensions and warnings in -Werror and -pedantic-errors modes, |
| 242 | // which *map* warnings/extensions to errors. |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 243 | if (SuppressSystemWarnings && |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 244 | Info.getID() < diag::NUM_BUILTIN_DIAGNOSTICS && |
| 245 | getBuiltinDiagClass(Info.getID()) != ERROR && |
| 246 | Info.getLocation().isValid() && |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame^] | 247 | Info.getLocation().getSpellingLoc().isInSystemHeader()) |
Chris Lattner | 7097d91 | 2008-02-03 09:00:04 +0000 | [diff] [blame] | 248 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | |
| 250 | if (DiagLevel >= Diagnostic::Error) { |
| 251 | ErrorOccurred = true; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 253 | ++NumErrors; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | // Finally, report it. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 257 | Client->HandleDiagnostic(DiagLevel, Info); |
| 258 | ++NumDiagnostics; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 261 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | DiagnosticClient::~DiagnosticClient() {} |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 263 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 264 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 265 | /// ModifierIs - Return true if the specified modifier matches specified string. |
| 266 | template <std::size_t StrLen> |
| 267 | static bool ModifierIs(const char *Modifier, unsigned ModifierLen, |
| 268 | const char (&Str)[StrLen]) { |
| 269 | return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); |
| 270 | } |
| 271 | |
| 272 | /// HandleSelectModifier - Handle the integer 'select' modifier. This is used |
| 273 | /// like this: %select{foo|bar|baz}2. This means that the integer argument |
| 274 | /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. |
| 275 | /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. |
| 276 | /// This is very useful for certain classes of variant diagnostics. |
| 277 | static void HandleSelectModifier(unsigned ValNo, |
| 278 | const char *Argument, unsigned ArgumentLen, |
| 279 | llvm::SmallVectorImpl<char> &OutStr) { |
| 280 | const char *ArgumentEnd = Argument+ArgumentLen; |
| 281 | |
| 282 | // Skip over 'ValNo' |'s. |
| 283 | while (ValNo) { |
| 284 | const char *NextVal = std::find(Argument, ArgumentEnd, '|'); |
| 285 | assert(NextVal != ArgumentEnd && "Value for integer select modifier was" |
| 286 | " larger than the number of options in the diagnostic string!"); |
| 287 | Argument = NextVal+1; // Skip this string. |
| 288 | --ValNo; |
| 289 | } |
| 290 | |
| 291 | // Get the end of the value. This is either the } or the |. |
| 292 | const char *EndPtr = std::find(Argument, ArgumentEnd, '|'); |
| 293 | // Add the value to the output string. |
| 294 | OutStr.append(Argument, EndPtr); |
| 295 | } |
| 296 | |
| 297 | /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the |
| 298 | /// letter 's' to the string if the value is not 1. This is used in cases like |
| 299 | /// this: "you idiot, you have %4 parameter%s4!". |
| 300 | static void HandleIntegerSModifier(unsigned ValNo, |
| 301 | llvm::SmallVectorImpl<char> &OutStr) { |
| 302 | if (ValNo != 1) |
| 303 | OutStr.push_back('s'); |
| 304 | } |
| 305 | |
| 306 | |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 307 | /// PluralNumber - Parse an unsigned integer and advance Start. |
| 308 | static unsigned PluralNumber(const char *&Start, const char *End) |
| 309 | { |
| 310 | // Programming 101: Parse a decimal number :-) |
| 311 | unsigned Val = 0; |
| 312 | while (Start != End && *Start >= '0' && *Start <= '9') { |
| 313 | Val *= 10; |
| 314 | Val += *Start - '0'; |
| 315 | ++Start; |
| 316 | } |
| 317 | return Val; |
| 318 | } |
| 319 | |
| 320 | /// TestPluralRange - Test if Val is in the parsed range. Modifies Start. |
| 321 | static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) |
| 322 | { |
| 323 | if (*Start != '[') { |
| 324 | unsigned Ref = PluralNumber(Start, End); |
| 325 | return Ref == Val; |
| 326 | } |
| 327 | |
| 328 | ++Start; |
| 329 | unsigned Low = PluralNumber(Start, End); |
| 330 | assert(*Start == ',' && "Bad plural expression syntax: expected ,"); |
| 331 | ++Start; |
| 332 | unsigned High = PluralNumber(Start, End); |
| 333 | assert(*Start == ']' && "Bad plural expression syntax: expected )"); |
| 334 | ++Start; |
| 335 | return Low <= Val && Val <= High; |
| 336 | } |
| 337 | |
| 338 | /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. |
| 339 | static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) |
| 340 | { |
| 341 | // Empty condition? |
| 342 | if (*Start == ':') |
| 343 | return true; |
| 344 | |
| 345 | while (1) { |
| 346 | char C = *Start; |
| 347 | if (C == '%') { |
| 348 | // Modulo expression |
| 349 | ++Start; |
| 350 | unsigned Arg = PluralNumber(Start, End); |
| 351 | assert(*Start == '=' && "Bad plural expression syntax: expected ="); |
| 352 | ++Start; |
| 353 | unsigned ValMod = ValNo % Arg; |
| 354 | if (TestPluralRange(ValMod, Start, End)) |
| 355 | return true; |
| 356 | } else { |
Sebastian Redl | e206532 | 2008-11-27 07:28:14 +0000 | [diff] [blame] | 357 | assert((C == '[' || (C >= '0' && C <= '9')) && |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 358 | "Bad plural expression syntax: unexpected character"); |
| 359 | // Range expression |
| 360 | if (TestPluralRange(ValNo, Start, End)) |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | // Scan for next or-expr part. |
| 365 | Start = std::find(Start, End, ','); |
| 366 | if(Start == End) |
| 367 | break; |
| 368 | ++Start; |
| 369 | } |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used |
| 374 | /// for complex plural forms, or in languages where all plurals are complex. |
| 375 | /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are |
| 376 | /// conditions that are tested in order, the form corresponding to the first |
| 377 | /// that applies being emitted. The empty condition is always true, making the |
| 378 | /// last form a default case. |
| 379 | /// Conditions are simple boolean expressions, where n is the number argument. |
| 380 | /// Here are the rules. |
| 381 | /// condition := expression | empty |
| 382 | /// empty := -> always true |
| 383 | /// expression := numeric [',' expression] -> logical or |
| 384 | /// numeric := range -> true if n in range |
| 385 | /// | '%' number '=' range -> true if n % number in range |
| 386 | /// range := number |
| 387 | /// | '[' number ',' number ']' -> ranges are inclusive both ends |
| 388 | /// |
| 389 | /// Here are some examples from the GNU gettext manual written in this form: |
| 390 | /// English: |
| 391 | /// {1:form0|:form1} |
| 392 | /// Latvian: |
| 393 | /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} |
| 394 | /// Gaeilge: |
| 395 | /// {1:form0|2:form1|:form2} |
| 396 | /// Romanian: |
| 397 | /// {1:form0|0,%100=[1,19]:form1|:form2} |
| 398 | /// Lithuanian: |
| 399 | /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} |
| 400 | /// Russian (requires repeated form): |
| 401 | /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} |
| 402 | /// Slovak |
| 403 | /// {1:form0|[2,4]:form1|:form2} |
| 404 | /// Polish (requires repeated form): |
| 405 | /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} |
| 406 | static void HandlePluralModifier(unsigned ValNo, |
| 407 | const char *Argument, unsigned ArgumentLen, |
| 408 | llvm::SmallVectorImpl<char> &OutStr) |
| 409 | { |
| 410 | const char *ArgumentEnd = Argument + ArgumentLen; |
| 411 | while (1) { |
| 412 | assert(Argument < ArgumentEnd && "Plural expression didn't match."); |
| 413 | const char *ExprEnd = Argument; |
| 414 | while (*ExprEnd != ':') { |
| 415 | assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); |
| 416 | ++ExprEnd; |
| 417 | } |
| 418 | if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { |
| 419 | Argument = ExprEnd + 1; |
| 420 | ExprEnd = std::find(Argument, ArgumentEnd, '|'); |
| 421 | OutStr.append(Argument, ExprEnd); |
| 422 | return; |
| 423 | } |
| 424 | Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 429 | /// FormatDiagnostic - Format this diagnostic into a string, substituting the |
| 430 | /// formal arguments into the %0 slots. The result is appended onto the Str |
| 431 | /// array. |
| 432 | void DiagnosticInfo:: |
| 433 | FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const { |
| 434 | const char *DiagStr = getDiags()->getDescription(getID()); |
| 435 | const char *DiagEnd = DiagStr+strlen(DiagStr); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 436 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 437 | while (DiagStr != DiagEnd) { |
| 438 | if (DiagStr[0] != '%') { |
| 439 | // Append non-%0 substrings to Str if we have one. |
| 440 | const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); |
| 441 | OutStr.append(DiagStr, StrEnd); |
| 442 | DiagStr = StrEnd; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 443 | continue; |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 444 | } else if (DiagStr[1] == '%') { |
| 445 | OutStr.push_back('%'); // %% -> %. |
| 446 | DiagStr += 2; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 447 | continue; |
| 448 | } |
| 449 | |
| 450 | // Skip the %. |
| 451 | ++DiagStr; |
| 452 | |
| 453 | // This must be a placeholder for a diagnostic argument. The format for a |
| 454 | // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". |
| 455 | // The digit is a number from 0-9 indicating which argument this comes from. |
| 456 | // The modifier is a string of digits from the set [-a-z]+, arguments is a |
| 457 | // brace enclosed string. |
| 458 | const char *Modifier = 0, *Argument = 0; |
| 459 | unsigned ModifierLen = 0, ArgumentLen = 0; |
| 460 | |
| 461 | // Check to see if we have a modifier. If so eat it. |
| 462 | if (!isdigit(DiagStr[0])) { |
| 463 | Modifier = DiagStr; |
| 464 | while (DiagStr[0] == '-' || |
| 465 | (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) |
| 466 | ++DiagStr; |
| 467 | ModifierLen = DiagStr-Modifier; |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 468 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 469 | // If we have an argument, get it next. |
| 470 | if (DiagStr[0] == '{') { |
| 471 | ++DiagStr; // Skip {. |
| 472 | Argument = DiagStr; |
| 473 | |
| 474 | for (; DiagStr[0] != '}'; ++DiagStr) |
| 475 | assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!"); |
| 476 | ArgumentLen = DiagStr-Argument; |
| 477 | ++DiagStr; // Skip }. |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 478 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic"); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 482 | unsigned ArgNo = *DiagStr++ - '0'; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 484 | switch (getArgKind(ArgNo)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 485 | // ---- STRINGS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 486 | case Diagnostic::ak_std_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 487 | const std::string &S = getArgStdStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 488 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 489 | OutStr.append(S.begin(), S.end()); |
| 490 | break; |
| 491 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 492 | case Diagnostic::ak_c_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 493 | const char *S = getArgCStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 494 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 495 | OutStr.append(S, S + strlen(S)); |
| 496 | break; |
| 497 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 498 | // ---- INTEGERS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 499 | case Diagnostic::ak_sint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 500 | int Val = getArgSInt(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 501 | |
| 502 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
| 503 | HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
| 504 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 505 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 506 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
| 507 | HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 508 | } else { |
| 509 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 510 | // FIXME: Optimize |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 511 | std::string S = llvm::itostr(Val); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 512 | OutStr.append(S.begin(), S.end()); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 513 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 514 | break; |
| 515 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 516 | case Diagnostic::ak_uint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 517 | unsigned Val = getArgUInt(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 518 | |
| 519 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
| 520 | HandleSelectModifier(Val, Argument, ArgumentLen, OutStr); |
| 521 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 522 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 523 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
| 524 | HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 525 | } else { |
| 526 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
| 527 | |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 528 | // FIXME: Optimize |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 529 | std::string S = llvm::utostr_32(Val); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 530 | OutStr.append(S.begin(), S.end()); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 531 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 532 | break; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 533 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 534 | // ---- NAMES and TYPES ---- |
| 535 | case Diagnostic::ak_identifierinfo: { |
| 536 | OutStr.push_back('\''); |
| 537 | const IdentifierInfo *II = getArgIdentifier(ArgNo); |
| 538 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 539 | OutStr.append(II->getName(), II->getName() + II->getLength()); |
| 540 | OutStr.push_back('\''); |
| 541 | break; |
| 542 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 543 | case Diagnostic::ak_qualtype: |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 544 | case Diagnostic::ak_declarationname: |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 545 | OutStr.push_back('\''); |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 546 | getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo), |
| 547 | Modifier, ModifierLen, |
| 548 | Argument, ArgumentLen, OutStr); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 549 | OutStr.push_back('\''); |
| 550 | break; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 553 | } |