blob: 893eae5d1a97d6f3338e705a5f7f4a1fddc722fe [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Diagnostic.h"
Chris Lattnere46b8792008-11-19 07:32:16 +000015#include "clang/Basic/IdentifierTable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/SourceLocation.h"
Chris Lattnerbe8e5a42008-11-19 06:51:40 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattner68f621c2008-11-19 07:22:31 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattner217df512007-12-02 01:09:57 +000019#include <vector>
20#include <map>
Chris Lattner8b8720f2008-03-10 17:04:53 +000021#include <cstring>
Chris Lattner4b009652007-07-25 00:24:17 +000022using namespace clang;
23
Chris Lattner217df512007-12-02 01:09:57 +000024//===----------------------------------------------------------------------===//
25// Builtin Diagnostic information
26//===----------------------------------------------------------------------===//
27
Chris Lattner4b009652007-07-25 00:24:17 +000028/// Flag values for diagnostics.
29enum {
30 // Diagnostic classes.
31 NOTE = 0x01,
32 WARNING = 0x02,
33 EXTENSION = 0x03,
Daniel Dunbar103baef2008-08-05 00:07:51 +000034 EXTWARN = 0x04,
35 ERROR = 0x05,
Chris Lattner6d6f11f2009-02-05 22:47:05 +000036 FATAL = 0x06,
Chris Lattner4b009652007-07-25 00:24:17 +000037 class_mask = 0x07
38};
39
40/// DiagnosticFlags - A set of flags, or'd together, that describe the
41/// diagnostic.
Chris Lattner4b009652007-07-25 00:24:17 +000042#define DIAG(ENUM,FLAGS,DESC) FLAGS,
Chris Lattner52a425b2009-01-27 18:30:58 +000043static unsigned char DiagnosticFlagsCommon[] = {
44#include "clang/Basic/DiagnosticCommonKinds.def"
Chris Lattner4b009652007-07-25 00:24:17 +000045 0
46};
Chris Lattner52a425b2009-01-27 18:30:58 +000047static unsigned char DiagnosticFlagsLex[] = {
48#include "clang/Basic/DiagnosticLexKinds.def"
49 0
50};
51static unsigned char DiagnosticFlagsParse[] = {
52#include "clang/Basic/DiagnosticParseKinds.def"
53 0
54};
55static unsigned char DiagnosticFlagsAST[] = {
56#include "clang/Basic/DiagnosticASTKinds.def"
57 0
58};
59static unsigned char DiagnosticFlagsSema[] = {
60#include "clang/Basic/DiagnosticSemaKinds.def"
61 0
62};
63static unsigned char DiagnosticFlagsAnalysis[] = {
64#include "clang/Basic/DiagnosticAnalysisKinds.def"
65 0
66};
67#undef DIAG
Chris Lattner4b009652007-07-25 00:24:17 +000068
69/// getDiagClass - Return the class field of the diagnostic.
70///
Chris Lattner4478db92007-11-30 22:53:43 +000071static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner8d9b1d82009-01-29 17:46:13 +000072 assert(DiagID < diag::DIAG_UPPER_LIMIT &&
Chris Lattner4478db92007-11-30 22:53:43 +000073 "Diagnostic ID out of range!");
Chris Lattner52a425b2009-01-27 18:30:58 +000074 unsigned res;
Chris Lattner8d9b1d82009-01-29 17:46:13 +000075 if (DiagID < diag::DIAG_START_LEX)
Chris Lattner52a425b2009-01-27 18:30:58 +000076 res = DiagnosticFlagsCommon[DiagID];
Chris Lattner8d9b1d82009-01-29 17:46:13 +000077 else if (DiagID < diag::DIAG_START_PARSE)
78 res = DiagnosticFlagsLex[DiagID - diag::DIAG_START_LEX - 1];
79 else if (DiagID < diag::DIAG_START_AST)
80 res = DiagnosticFlagsParse[DiagID - diag::DIAG_START_PARSE - 1];
81 else if (DiagID < diag::DIAG_START_SEMA)
82 res = DiagnosticFlagsAST[DiagID - diag::DIAG_START_AST - 1];
83 else if (DiagID < diag::DIAG_START_ANALYSIS)
84 res = DiagnosticFlagsSema[DiagID - diag::DIAG_START_SEMA - 1];
Chris Lattner52a425b2009-01-27 18:30:58 +000085 else
Chris Lattner8d9b1d82009-01-29 17:46:13 +000086 res = DiagnosticFlagsAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
Chris Lattner52a425b2009-01-27 18:30:58 +000087 return res & class_mask;
Chris Lattner4b009652007-07-25 00:24:17 +000088}
89
90/// DiagnosticText - An english message to print for the diagnostic. These
91/// should be localized.
Chris Lattner4b009652007-07-25 00:24:17 +000092#define DIAG(ENUM,FLAGS,DESC) DESC,
Chris Lattner52a425b2009-01-27 18:30:58 +000093static const char * const DiagnosticTextCommon[] = {
94#include "clang/Basic/DiagnosticCommonKinds.def"
Chris Lattner4b009652007-07-25 00:24:17 +000095 0
96};
Chris Lattner52a425b2009-01-27 18:30:58 +000097static const char * const DiagnosticTextLex[] = {
98#include "clang/Basic/DiagnosticLexKinds.def"
99 0
100};
101static const char * const DiagnosticTextParse[] = {
102#include "clang/Basic/DiagnosticParseKinds.def"
103 0
104};
105static const char * const DiagnosticTextAST[] = {
106#include "clang/Basic/DiagnosticASTKinds.def"
107 0
108};
109static const char * const DiagnosticTextSema[] = {
110#include "clang/Basic/DiagnosticSemaKinds.def"
111 0
112};
113static const char * const DiagnosticTextAnalysis[] = {
114#include "clang/Basic/DiagnosticAnalysisKinds.def"
115 0
116};
117#undef DIAG
Chris Lattner4b009652007-07-25 00:24:17 +0000118
Chris Lattner217df512007-12-02 01:09:57 +0000119//===----------------------------------------------------------------------===//
120// Custom Diagnostic information
121//===----------------------------------------------------------------------===//
122
123namespace clang {
124 namespace diag {
125 class CustomDiagInfo {
126 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
127 std::vector<DiagDesc> DiagInfo;
128 std::map<DiagDesc, unsigned> DiagIDs;
129 public:
130
131 /// getDescription - Return the description of the specified custom
132 /// diagnostic.
133 const char *getDescription(unsigned DiagID) const {
Chris Lattner86a24842009-01-29 06:55:46 +0000134 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner217df512007-12-02 01:09:57 +0000135 "Invalid diagnosic ID");
Chris Lattner86a24842009-01-29 06:55:46 +0000136 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattner217df512007-12-02 01:09:57 +0000137 }
138
139 /// getLevel - Return the level of the specified custom diagnostic.
140 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner86a24842009-01-29 06:55:46 +0000141 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner217df512007-12-02 01:09:57 +0000142 "Invalid diagnosic ID");
Chris Lattner86a24842009-01-29 06:55:46 +0000143 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattner217df512007-12-02 01:09:57 +0000144 }
145
Chris Lattnerb90f3cd2008-10-17 21:24:47 +0000146 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
147 Diagnostic &Diags) {
Chris Lattner217df512007-12-02 01:09:57 +0000148 DiagDesc D(L, Message);
149 // Check to see if it already exists.
150 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
151 if (I != DiagIDs.end() && I->first == D)
152 return I->second;
153
154 // If not, assign a new ID.
Chris Lattner86a24842009-01-29 06:55:46 +0000155 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattner217df512007-12-02 01:09:57 +0000156 DiagIDs.insert(std::make_pair(D, ID));
157 DiagInfo.push_back(D);
Chris Lattnerb90f3cd2008-10-17 21:24:47 +0000158
159 // If this is a warning, and all warnings are supposed to map to errors,
160 // insert the mapping now.
161 if (L == Diagnostic::Warning && Diags.getWarningsAsErrors())
162 Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR);
Chris Lattner217df512007-12-02 01:09:57 +0000163 return ID;
164 }
165 };
166
167 } // end diag namespace
168} // end clang namespace
169
170
171//===----------------------------------------------------------------------===//
172// Common Diagnostic implementation
173//===----------------------------------------------------------------------===//
174
Chris Lattnerf5b269a2008-11-23 09:21:17 +0000175static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
176 const char *Modifier, unsigned ML,
177 const char *Argument, unsigned ArgLen,
Chris Lattner0b53af22009-02-19 23:53:20 +0000178 llvm::SmallVectorImpl<char> &Output,
179 void *Cookie) {
Chris Lattnerf5b269a2008-11-23 09:21:17 +0000180 const char *Str = "<can't format argument>";
Chris Lattnerda5c0872008-11-23 09:13:29 +0000181 Output.append(Str, Str+strlen(Str));
182}
183
184
Ted Kremenek5c341732008-08-07 17:49:57 +0000185Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattner3a22b7c2008-05-29 15:36:45 +0000186 IgnoreAllWarnings = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000187 WarningsAsErrors = false;
188 WarnOnExtensions = false;
189 ErrorOnExtensions = false;
Daniel Dunbar4dbd8572008-09-12 18:10:20 +0000190 SuppressSystemWarnings = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000191 // Clear all mappings, setting them to MAP_DEFAULT.
192 memset(DiagMappings, 0, sizeof(DiagMappings));
193
194 ErrorOccurred = false;
Chris Lattneraab419a2009-02-06 04:16:02 +0000195 FatalErrorOccurred = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000196 NumDiagnostics = 0;
197 NumErrors = 0;
Chris Lattner217df512007-12-02 01:09:57 +0000198 CustomDiagInfo = 0;
Chris Lattner9943e982008-11-22 00:59:29 +0000199 CurDiagID = ~0U;
Chris Lattner44b39832009-02-17 06:49:55 +0000200 LastDiagLevel = Fatal;
Chris Lattnerda5c0872008-11-23 09:13:29 +0000201
Chris Lattnerf5b269a2008-11-23 09:21:17 +0000202 ArgToStringFn = DummyArgToStringFn;
Chris Lattner0b53af22009-02-19 23:53:20 +0000203 ArgToStringCookie = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000204}
205
Chris Lattner217df512007-12-02 01:09:57 +0000206Diagnostic::~Diagnostic() {
207 delete CustomDiagInfo;
208}
209
210/// getCustomDiagID - Return an ID for a diagnostic with the specified message
211/// and level. If this is the first request for this diagnosic, it is
212/// registered and created, otherwise the existing ID is returned.
213unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
214 if (CustomDiagInfo == 0)
215 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerb90f3cd2008-10-17 21:24:47 +0000216 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattner217df512007-12-02 01:09:57 +0000217}
218
219
Chris Lattner44b39832009-02-17 06:49:55 +0000220/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
221/// level of the specified diagnostic ID is a Warning or Extension.
222/// This only works on builtin diagnostics, not custom ones, and is not legal to
223/// call on NOTEs.
224bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattner8d9b1d82009-01-29 17:46:13 +0000225 return DiagID < diag::DIAG_UPPER_LIMIT && getBuiltinDiagClass(DiagID) < ERROR;
Chris Lattner4b009652007-07-25 00:24:17 +0000226}
227
228
229/// getDescription - Given a diagnostic ID, return a description of the
230/// issue.
Chris Lattner6948ae62008-11-18 07:04:44 +0000231const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner44b39832009-02-17 06:49:55 +0000232 if (DiagID < diag::DIAG_START_LEX)
233 return DiagnosticTextCommon[DiagID];
234 else if (DiagID < diag::DIAG_START_PARSE)
235 return DiagnosticTextLex[DiagID - diag::DIAG_START_LEX - 1];
236 else if (DiagID < diag::DIAG_START_AST)
237 return DiagnosticTextParse[DiagID - diag::DIAG_START_PARSE - 1];
238 else if (DiagID < diag::DIAG_START_SEMA)
239 return DiagnosticTextAST[DiagID - diag::DIAG_START_AST - 1];
240 else if (DiagID < diag::DIAG_START_ANALYSIS)
241 return DiagnosticTextSema[DiagID - diag::DIAG_START_SEMA - 1];
242 else if (DiagID < diag::DIAG_UPPER_LIMIT)
243 return DiagnosticTextAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
Chris Lattner52a425b2009-01-27 18:30:58 +0000244 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000245}
246
247/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
248/// object, classify the specified diagnostic ID into a Level, consumable by
249/// the DiagnosticClient.
250Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattner217df512007-12-02 01:09:57 +0000251 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner8d9b1d82009-01-29 17:46:13 +0000252 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattner217df512007-12-02 01:09:57 +0000253 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner4478db92007-11-30 22:53:43 +0000254
255 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner44b39832009-02-17 06:49:55 +0000256 assert(DiagClass != NOTE && "Cannot get the diagnostic level of a note!");
257 return getDiagnosticLevel(DiagID, DiagClass);
258}
259
260/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
261/// object, classify the specified diagnostic ID into a Level, consumable by
262/// the DiagnosticClient.
263Diagnostic::Level
264Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000265 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattner44b39832009-02-17 06:49:55 +0000266 // to error. Errors can only be mapped to fatal.
267 switch (getDiagnosticMapping((diag::kind)DiagID)) {
268 case diag::MAP_DEFAULT: break;
269 case diag::MAP_IGNORE: return Diagnostic::Ignored;
270 case diag::MAP_WARNING: DiagClass = WARNING; break;
271 case diag::MAP_ERROR: DiagClass = ERROR; break;
272 case diag::MAP_FATAL: DiagClass = FATAL; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000273 }
274
275 // Map diagnostic classes based on command line argument settings.
276 if (DiagClass == EXTENSION) {
277 if (ErrorOnExtensions)
278 DiagClass = ERROR;
279 else if (WarnOnExtensions)
280 DiagClass = WARNING;
281 else
282 return Ignored;
Daniel Dunbar103baef2008-08-05 00:07:51 +0000283 } else if (DiagClass == EXTWARN) {
284 DiagClass = ErrorOnExtensions ? ERROR : WARNING;
Chris Lattner4b009652007-07-25 00:24:17 +0000285 }
286
Chris Lattner3a22b7c2008-05-29 15:36:45 +0000287 // If warnings are globally mapped to ignore or error, do it.
288 if (DiagClass == WARNING) {
289 if (IgnoreAllWarnings)
290 return Diagnostic::Ignored;
291 if (WarningsAsErrors)
292 DiagClass = ERROR;
293 }
Chris Lattner4b009652007-07-25 00:24:17 +0000294
295 switch (DiagClass) {
296 default: assert(0 && "Unknown diagnostic class!");
Chris Lattner4b009652007-07-25 00:24:17 +0000297 case WARNING: return Diagnostic::Warning;
298 case ERROR: return Diagnostic::Error;
Chris Lattner6d6f11f2009-02-05 22:47:05 +0000299 case FATAL: return Diagnostic::Fatal;
Chris Lattner4b009652007-07-25 00:24:17 +0000300 }
301}
302
Chris Lattner6948ae62008-11-18 07:04:44 +0000303/// ProcessDiag - This is the method used to report a diagnostic that is
304/// finally fully formed.
Chris Lattner9943e982008-11-22 00:59:29 +0000305void Diagnostic::ProcessDiag() {
306 DiagnosticInfo Info(this);
307
Chris Lattneraab419a2009-02-06 04:16:02 +0000308 // If a fatal error has already been emitted, silence all subsequent
309 // diagnostics.
310 if (FatalErrorOccurred)
311 return;
312
Chris Lattner4b009652007-07-25 00:24:17 +0000313 // Figure out the diagnostic level of this message.
Chris Lattner44b39832009-02-17 06:49:55 +0000314 Diagnostic::Level DiagLevel;
315 unsigned DiagID = Info.getID();
Chris Lattner4b009652007-07-25 00:24:17 +0000316
Chris Lattner44b39832009-02-17 06:49:55 +0000317 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
318 // in a system header.
319 bool ShouldEmitInSystemHeader;
320
321 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
322 // Handle custom diagnostics, which cannot be mapped.
323 DiagLevel = CustomDiagInfo->getLevel(DiagID);
324
325 // Custom diagnostics always are emitted in system headers.
326 ShouldEmitInSystemHeader = true;
327 } else {
328 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
329 // the diagnostic level was for the previous diagnostic so that it is
330 // filtered the same as the previous diagnostic.
331 unsigned DiagClass = getBuiltinDiagClass(DiagID);
332 if (DiagClass == NOTE) {
333 DiagLevel = Diagnostic::Note;
334 ShouldEmitInSystemHeader = false; // extra consideration is needed
335 } else {
336 // If this is not an error and we are in a system header, we ignore it.
337 // Check the original Diag ID here, because we also want to ignore
338 // extensions and warnings in -Werror and -pedantic-errors modes, which
339 // *map* warnings/extensions to errors.
340 ShouldEmitInSystemHeader = DiagClass == ERROR;
341
342 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
343 }
344 }
345
346 if (DiagLevel != Diagnostic::Note)
347 LastDiagLevel = DiagLevel;
348
349 // If the client doesn't care about this message, don't issue it. If this is
350 // a note and the last real diagnostic was ignored, ignore it too.
351 if (DiagLevel == Diagnostic::Ignored ||
352 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Chris Lattner4b009652007-07-25 00:24:17 +0000353 return;
Nico Weberd2a6ac92008-08-10 19:59:06 +0000354
Chris Lattner44b39832009-02-17 06:49:55 +0000355 // If this diagnostic is in a system header and is not a clang error, suppress
356 // it.
357 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner6948ae62008-11-18 07:04:44 +0000358 Info.getLocation().isValid() &&
Chris Lattner44b39832009-02-17 06:49:55 +0000359 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner724231a2009-02-17 06:52:20 +0000360 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
361 LastDiagLevel = Diagnostic::Ignored;
Chris Lattner84602d72008-02-03 09:00:04 +0000362 return;
Chris Lattner724231a2009-02-17 06:52:20 +0000363 }
Chris Lattner44b39832009-02-17 06:49:55 +0000364
Chris Lattner4b009652007-07-25 00:24:17 +0000365 if (DiagLevel >= Diagnostic::Error) {
366 ErrorOccurred = true;
Chris Lattner6948ae62008-11-18 07:04:44 +0000367 ++NumErrors;
Chris Lattneraab419a2009-02-06 04:16:02 +0000368
369 if (DiagLevel == Diagnostic::Fatal)
370 FatalErrorOccurred = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000371 }
Chris Lattner44b39832009-02-17 06:49:55 +0000372
Chris Lattner4b009652007-07-25 00:24:17 +0000373 // Finally, report it.
Chris Lattner6948ae62008-11-18 07:04:44 +0000374 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenek1e1a34b2009-01-23 20:28:53 +0000375 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Chris Lattner4b009652007-07-25 00:24:17 +0000376}
377
Nico Weberd2a6ac92008-08-10 19:59:06 +0000378
Chris Lattner4b009652007-07-25 00:24:17 +0000379DiagnosticClient::~DiagnosticClient() {}
Nico Weberd2a6ac92008-08-10 19:59:06 +0000380
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000381
Chris Lattnera7021ee2008-11-21 07:50:02 +0000382/// ModifierIs - Return true if the specified modifier matches specified string.
383template <std::size_t StrLen>
384static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
385 const char (&Str)[StrLen]) {
386 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
387}
388
389/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
390/// like this: %select{foo|bar|baz}2. This means that the integer argument
391/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
392/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
393/// This is very useful for certain classes of variant diagnostics.
394static void HandleSelectModifier(unsigned ValNo,
395 const char *Argument, unsigned ArgumentLen,
396 llvm::SmallVectorImpl<char> &OutStr) {
397 const char *ArgumentEnd = Argument+ArgumentLen;
398
399 // Skip over 'ValNo' |'s.
400 while (ValNo) {
401 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
402 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
403 " larger than the number of options in the diagnostic string!");
404 Argument = NextVal+1; // Skip this string.
405 --ValNo;
406 }
407
408 // Get the end of the value. This is either the } or the |.
409 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
410 // Add the value to the output string.
411 OutStr.append(Argument, EndPtr);
412}
413
414/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
415/// letter 's' to the string if the value is not 1. This is used in cases like
416/// this: "you idiot, you have %4 parameter%s4!".
417static void HandleIntegerSModifier(unsigned ValNo,
418 llvm::SmallVectorImpl<char> &OutStr) {
419 if (ValNo != 1)
420 OutStr.push_back('s');
421}
422
423
Sebastian Redlfd9f2ac2008-11-22 13:44:36 +0000424/// PluralNumber - Parse an unsigned integer and advance Start.
425static unsigned PluralNumber(const char *&Start, const char *End)
426{
427 // Programming 101: Parse a decimal number :-)
428 unsigned Val = 0;
429 while (Start != End && *Start >= '0' && *Start <= '9') {
430 Val *= 10;
431 Val += *Start - '0';
432 ++Start;
433 }
434 return Val;
435}
436
437/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
438static bool TestPluralRange(unsigned Val, const char *&Start, const char *End)
439{
440 if (*Start != '[') {
441 unsigned Ref = PluralNumber(Start, End);
442 return Ref == Val;
443 }
444
445 ++Start;
446 unsigned Low = PluralNumber(Start, End);
447 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
448 ++Start;
449 unsigned High = PluralNumber(Start, End);
450 assert(*Start == ']' && "Bad plural expression syntax: expected )");
451 ++Start;
452 return Low <= Val && Val <= High;
453}
454
455/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
456static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End)
457{
458 // Empty condition?
459 if (*Start == ':')
460 return true;
461
462 while (1) {
463 char C = *Start;
464 if (C == '%') {
465 // Modulo expression
466 ++Start;
467 unsigned Arg = PluralNumber(Start, End);
468 assert(*Start == '=' && "Bad plural expression syntax: expected =");
469 ++Start;
470 unsigned ValMod = ValNo % Arg;
471 if (TestPluralRange(ValMod, Start, End))
472 return true;
473 } else {
Sebastian Redlc43ec3b2008-11-27 07:28:14 +0000474 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redlfd9f2ac2008-11-22 13:44:36 +0000475 "Bad plural expression syntax: unexpected character");
476 // Range expression
477 if (TestPluralRange(ValNo, Start, End))
478 return true;
479 }
480
481 // Scan for next or-expr part.
482 Start = std::find(Start, End, ',');
483 if(Start == End)
484 break;
485 ++Start;
486 }
487 return false;
488}
489
490/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
491/// for complex plural forms, or in languages where all plurals are complex.
492/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
493/// conditions that are tested in order, the form corresponding to the first
494/// that applies being emitted. The empty condition is always true, making the
495/// last form a default case.
496/// Conditions are simple boolean expressions, where n is the number argument.
497/// Here are the rules.
498/// condition := expression | empty
499/// empty := -> always true
500/// expression := numeric [',' expression] -> logical or
501/// numeric := range -> true if n in range
502/// | '%' number '=' range -> true if n % number in range
503/// range := number
504/// | '[' number ',' number ']' -> ranges are inclusive both ends
505///
506/// Here are some examples from the GNU gettext manual written in this form:
507/// English:
508/// {1:form0|:form1}
509/// Latvian:
510/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
511/// Gaeilge:
512/// {1:form0|2:form1|:form2}
513/// Romanian:
514/// {1:form0|0,%100=[1,19]:form1|:form2}
515/// Lithuanian:
516/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
517/// Russian (requires repeated form):
518/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
519/// Slovak
520/// {1:form0|[2,4]:form1|:form2}
521/// Polish (requires repeated form):
522/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
523static void HandlePluralModifier(unsigned ValNo,
524 const char *Argument, unsigned ArgumentLen,
525 llvm::SmallVectorImpl<char> &OutStr)
526{
527 const char *ArgumentEnd = Argument + ArgumentLen;
528 while (1) {
529 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
530 const char *ExprEnd = Argument;
531 while (*ExprEnd != ':') {
532 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
533 ++ExprEnd;
534 }
535 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
536 Argument = ExprEnd + 1;
537 ExprEnd = std::find(Argument, ArgumentEnd, '|');
538 OutStr.append(Argument, ExprEnd);
539 return;
540 }
541 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
542 }
543}
544
545
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000546/// FormatDiagnostic - Format this diagnostic into a string, substituting the
547/// formal arguments into the %0 slots. The result is appended onto the Str
548/// array.
549void DiagnosticInfo::
550FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
551 const char *DiagStr = getDiags()->getDescription(getID());
552 const char *DiagEnd = DiagStr+strlen(DiagStr);
Nico Weberd2a6ac92008-08-10 19:59:06 +0000553
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000554 while (DiagStr != DiagEnd) {
555 if (DiagStr[0] != '%') {
556 // Append non-%0 substrings to Str if we have one.
557 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
558 OutStr.append(DiagStr, StrEnd);
559 DiagStr = StrEnd;
Chris Lattnera7021ee2008-11-21 07:50:02 +0000560 continue;
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000561 } else if (DiagStr[1] == '%') {
562 OutStr.push_back('%'); // %% -> %.
563 DiagStr += 2;
Chris Lattnera7021ee2008-11-21 07:50:02 +0000564 continue;
565 }
566
567 // Skip the %.
568 ++DiagStr;
569
570 // This must be a placeholder for a diagnostic argument. The format for a
571 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
572 // The digit is a number from 0-9 indicating which argument this comes from.
573 // The modifier is a string of digits from the set [-a-z]+, arguments is a
574 // brace enclosed string.
575 const char *Modifier = 0, *Argument = 0;
576 unsigned ModifierLen = 0, ArgumentLen = 0;
577
578 // Check to see if we have a modifier. If so eat it.
579 if (!isdigit(DiagStr[0])) {
580 Modifier = DiagStr;
581 while (DiagStr[0] == '-' ||
582 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
583 ++DiagStr;
584 ModifierLen = DiagStr-Modifier;
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000585
Chris Lattnera7021ee2008-11-21 07:50:02 +0000586 // If we have an argument, get it next.
587 if (DiagStr[0] == '{') {
588 ++DiagStr; // Skip {.
589 Argument = DiagStr;
590
591 for (; DiagStr[0] != '}'; ++DiagStr)
592 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
593 ArgumentLen = DiagStr-Argument;
594 ++DiagStr; // Skip }.
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000595 }
Chris Lattnera7021ee2008-11-21 07:50:02 +0000596 }
597
598 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattnerda5c0872008-11-23 09:13:29 +0000599 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattnera7021ee2008-11-21 07:50:02 +0000600
Chris Lattnerda5c0872008-11-23 09:13:29 +0000601 switch (getArgKind(ArgNo)) {
Chris Lattnerb1753422008-11-23 21:45:46 +0000602 // ---- STRINGS ----
Chris Lattner9943e982008-11-22 00:59:29 +0000603 case Diagnostic::ak_std_string: {
Chris Lattnerda5c0872008-11-23 09:13:29 +0000604 const std::string &S = getArgStdStr(ArgNo);
Chris Lattnera7021ee2008-11-21 07:50:02 +0000605 assert(ModifierLen == 0 && "No modifiers for strings yet");
606 OutStr.append(S.begin(), S.end());
607 break;
608 }
Chris Lattner9943e982008-11-22 00:59:29 +0000609 case Diagnostic::ak_c_string: {
Chris Lattnerda5c0872008-11-23 09:13:29 +0000610 const char *S = getArgCStr(ArgNo);
Chris Lattnera7021ee2008-11-21 07:50:02 +0000611 assert(ModifierLen == 0 && "No modifiers for strings yet");
612 OutStr.append(S, S + strlen(S));
613 break;
614 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000615 // ---- INTEGERS ----
Chris Lattner9943e982008-11-22 00:59:29 +0000616 case Diagnostic::ak_sint: {
Chris Lattnerda5c0872008-11-23 09:13:29 +0000617 int Val = getArgSInt(ArgNo);
Chris Lattnera7021ee2008-11-21 07:50:02 +0000618
619 if (ModifierIs(Modifier, ModifierLen, "select")) {
620 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
621 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
622 HandleIntegerSModifier(Val, OutStr);
Sebastian Redlfd9f2ac2008-11-22 13:44:36 +0000623 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
624 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattnera7021ee2008-11-21 07:50:02 +0000625 } else {
626 assert(ModifierLen == 0 && "Unknown integer modifier");
Chris Lattner68f621c2008-11-19 07:22:31 +0000627 // FIXME: Optimize
Chris Lattnera7021ee2008-11-21 07:50:02 +0000628 std::string S = llvm::itostr(Val);
Chris Lattner68f621c2008-11-19 07:22:31 +0000629 OutStr.append(S.begin(), S.end());
Chris Lattner68f621c2008-11-19 07:22:31 +0000630 }
Chris Lattnera7021ee2008-11-21 07:50:02 +0000631 break;
632 }
Chris Lattner9943e982008-11-22 00:59:29 +0000633 case Diagnostic::ak_uint: {
Chris Lattnerda5c0872008-11-23 09:13:29 +0000634 unsigned Val = getArgUInt(ArgNo);
Chris Lattnera7021ee2008-11-21 07:50:02 +0000635
636 if (ModifierIs(Modifier, ModifierLen, "select")) {
637 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
638 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
639 HandleIntegerSModifier(Val, OutStr);
Sebastian Redlfd9f2ac2008-11-22 13:44:36 +0000640 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
641 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattnera7021ee2008-11-21 07:50:02 +0000642 } else {
643 assert(ModifierLen == 0 && "Unknown integer modifier");
644
Chris Lattner68f621c2008-11-19 07:22:31 +0000645 // FIXME: Optimize
Chris Lattnera7021ee2008-11-21 07:50:02 +0000646 std::string S = llvm::utostr_32(Val);
Chris Lattner68f621c2008-11-19 07:22:31 +0000647 OutStr.append(S.begin(), S.end());
Chris Lattner68f621c2008-11-19 07:22:31 +0000648 }
Chris Lattnerda5c0872008-11-23 09:13:29 +0000649 break;
Chris Lattnera7021ee2008-11-21 07:50:02 +0000650 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000651 // ---- NAMES and TYPES ----
652 case Diagnostic::ak_identifierinfo: {
Chris Lattnerb1753422008-11-23 21:45:46 +0000653 const IdentifierInfo *II = getArgIdentifier(ArgNo);
654 assert(ModifierLen == 0 && "No modifiers for strings yet");
Chris Lattner2bd4a5a2009-02-19 23:45:49 +0000655 OutStr.push_back('\'');
Chris Lattnerb1753422008-11-23 21:45:46 +0000656 OutStr.append(II->getName(), II->getName() + II->getLength());
657 OutStr.push_back('\'');
658 break;
659 }
Chris Lattnerda5c0872008-11-23 09:13:29 +0000660 case Diagnostic::ak_qualtype:
Chris Lattner254de7d2008-11-23 20:28:15 +0000661 case Diagnostic::ak_declarationname:
Douglas Gregor09be81b2009-02-04 17:27:36 +0000662 case Diagnostic::ak_nameddecl:
Chris Lattnerf5b269a2008-11-23 09:21:17 +0000663 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
664 Modifier, ModifierLen,
665 Argument, ArgumentLen, OutStr);
Chris Lattnerda5c0872008-11-23 09:13:29 +0000666 break;
Nico Weberd2a6ac92008-08-10 19:59:06 +0000667 }
668 }
Nico Weberd2a6ac92008-08-10 19:59:06 +0000669}
Ted Kremenek1e1a34b2009-01-23 20:28:53 +0000670
671/// IncludeInDiagnosticCounts - This method (whose default implementation
672/// returns true) indicates whether the diagnostics handled by this
673/// DiagnosticClient should be included in the number of diagnostics
674/// reported by Diagnostic.
675bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }