blob: 4dcd7c3608797fedc8a7bd0192f2b27c348008c9 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Diagnostic.h"
Chris Lattner27ceb9d2009-04-15 07:01:18 +000015
16#include "clang/Lex/LexDiagnostic.h"
17#include "clang/Parse/ParseDiagnostic.h"
18#include "clang/AST/ASTDiagnostic.h"
19#include "clang/Sema/SemaDiagnostic.h"
20#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Analysis/AnalysisDiagnostic.h"
22#include "clang/Driver/DriverDiagnostic.h"
23
Chris Lattner43b628c2008-11-19 07:32:16 +000024#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "clang/Basic/SourceLocation.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner30bc9652008-11-19 07:22:31 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner182745a2007-12-02 01:09:57 +000028#include <vector>
29#include <map>
Chris Lattner87cf5ac2008-03-10 17:04:53 +000030#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000031using namespace clang;
32
Chris Lattner182745a2007-12-02 01:09:57 +000033//===----------------------------------------------------------------------===//
34// Builtin Diagnostic information
35//===----------------------------------------------------------------------===//
36
Chris Lattner27ceb9d2009-04-15 07:01:18 +000037// DefaultDiagnosticMappings - This specifies the default mapping for each diag,
38// based on its kind. Yay for macros?
39
40struct DefaultMappingInfo {
41 unsigned DiagID : 14;
42 unsigned Mapping : 2;
43};
44
45#define NOTE diag::MAP_IGNORE
46#define WARNING diag::MAP_WARNING
47#define EXTENSION diag::MAP_IGNORE
48#define EXTWARN diag::MAP_WARNING
49#define ERROR diag::MAP_ERROR
50#define FATAL diag::MAP_FATAL
51
52static const DefaultMappingInfo DefaultMappings[] = {
53#define DIAG(ENUM,CLASS,DESC) { diag::ENUM, CLASS },
54#include "clang/Basic/DiagnosticCommonKinds.inc"
55#include "clang/Basic/DiagnosticDriverKinds.inc"
56#include "clang/Basic/DiagnosticFrontendKinds.inc"
57#include "clang/Basic/DiagnosticLexKinds.inc"
58#include "clang/Basic/DiagnosticParseKinds.inc"
59#include "clang/Basic/DiagnosticASTKinds.inc"
60#include "clang/Basic/DiagnosticSemaKinds.inc"
61#include "clang/Basic/DiagnosticAnalysisKinds.inc"
62{ 0, 0 }
63};
64
65#undef DIAG
66#undef NOTE
67#undef WARNING
68#undef EXTENSION
69#undef EXTWARN
70#undef ERROR
71#undef FATAL
72
73
74
75// Diagnostic classes.
Reid Spencer5f016e22007-07-11 17:01:13 +000076enum {
Reid Spencer5f016e22007-07-11 17:01:13 +000077 NOTE = 0x01,
78 WARNING = 0x02,
79 EXTENSION = 0x03,
Daniel Dunbar4489fe12008-08-05 00:07:51 +000080 EXTWARN = 0x04,
81 ERROR = 0x05,
Chris Lattner27ceb9d2009-04-15 07:01:18 +000082 FATAL = 0x06
Reid Spencer5f016e22007-07-11 17:01:13 +000083};
84
Chris Lattner27ceb9d2009-04-15 07:01:18 +000085/// DiagnosticClasses - The class for each diagnostic.
86#define DIAG(ENUM,CLASS,DESC) CLASS,
87static unsigned char DiagnosticClassesCommon[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000088#include "clang/Basic/DiagnosticCommonKinds.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +000089 0
90};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000091static unsigned char DiagnosticClassesDriver[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000092#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000093 0
94};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000095static unsigned char DiagnosticClassesFrontend[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000096#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar50f4f462009-03-12 10:14:16 +000097 0
98};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000099static unsigned char DiagnosticClassesLex[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000100#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000101 0
102};
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000103static unsigned char DiagnosticClassesParse[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000104#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000105 0
106};
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000107static unsigned char DiagnosticClassesAST[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000108#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000109 0
110};
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000111static unsigned char DiagnosticClassesSema[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000112#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000113 0
114};
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000115static unsigned char DiagnosticClassesAnalysis[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000116#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000117 0
118};
119#undef DIAG
Reid Spencer5f016e22007-07-11 17:01:13 +0000120
121/// getDiagClass - Return the class field of the diagnostic.
122///
Chris Lattner07506182007-11-30 22:53:43 +0000123static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000124 assert(DiagID < diag::DIAG_UPPER_LIMIT &&
Chris Lattner07506182007-11-30 22:53:43 +0000125 "Diagnostic ID out of range!");
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000126 unsigned res;
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000127 if (DiagID < diag::DIAG_START_DRIVER)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000128 res = DiagnosticClassesCommon[DiagID];
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000129 else if (DiagID < diag::DIAG_START_FRONTEND)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000130 res = DiagnosticClassesDriver[DiagID - diag::DIAG_START_DRIVER - 1];
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000131 else if (DiagID < diag::DIAG_START_LEX)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000132 res = DiagnosticClassesFrontend[DiagID - diag::DIAG_START_FRONTEND - 1];
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000133 else if (DiagID < diag::DIAG_START_PARSE)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000134 res = DiagnosticClassesLex[DiagID - diag::DIAG_START_LEX - 1];
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000135 else if (DiagID < diag::DIAG_START_AST)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000136 res = DiagnosticClassesParse[DiagID - diag::DIAG_START_PARSE - 1];
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000137 else if (DiagID < diag::DIAG_START_SEMA)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000138 res = DiagnosticClassesAST[DiagID - diag::DIAG_START_AST - 1];
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000139 else if (DiagID < diag::DIAG_START_ANALYSIS)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000140 res = DiagnosticClassesSema[DiagID - diag::DIAG_START_SEMA - 1];
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000141 else
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000142 res = DiagnosticClassesAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
143 return res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
146/// DiagnosticText - An english message to print for the diagnostic. These
147/// should be localized.
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000148#define DIAG(ENUM,CLASS,DESC) DESC,
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000149static const char * const DiagnosticTextCommon[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000150#include "clang/Basic/DiagnosticCommonKinds.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 0
152};
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000153static const char * const DiagnosticTextDriver[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000154#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000155 0
156};
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000157static const char * const DiagnosticTextFrontend[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000158#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000159 0
160};
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000161static const char * const DiagnosticTextLex[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000162#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000163 0
164};
165static const char * const DiagnosticTextParse[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000166#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000167 0
168};
169static const char * const DiagnosticTextAST[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000170#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000171 0
172};
173static const char * const DiagnosticTextSema[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000174#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000175 0
176};
177static const char * const DiagnosticTextAnalysis[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000178#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000179 0
180};
181#undef DIAG
Reid Spencer5f016e22007-07-11 17:01:13 +0000182
Chris Lattner182745a2007-12-02 01:09:57 +0000183//===----------------------------------------------------------------------===//
184// Custom Diagnostic information
185//===----------------------------------------------------------------------===//
186
187namespace clang {
188 namespace diag {
189 class CustomDiagInfo {
190 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
191 std::vector<DiagDesc> DiagInfo;
192 std::map<DiagDesc, unsigned> DiagIDs;
193 public:
194
195 /// getDescription - Return the description of the specified custom
196 /// diagnostic.
197 const char *getDescription(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000198 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000199 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000200 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattner182745a2007-12-02 01:09:57 +0000201 }
202
203 /// getLevel - Return the level of the specified custom diagnostic.
204 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000205 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000206 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000207 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattner182745a2007-12-02 01:09:57 +0000208 }
209
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000210 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
211 Diagnostic &Diags) {
Chris Lattner182745a2007-12-02 01:09:57 +0000212 DiagDesc D(L, Message);
213 // Check to see if it already exists.
214 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
215 if (I != DiagIDs.end() && I->first == D)
216 return I->second;
217
218 // If not, assign a new ID.
Chris Lattner88eccaf2009-01-29 06:55:46 +0000219 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattner182745a2007-12-02 01:09:57 +0000220 DiagIDs.insert(std::make_pair(D, ID));
221 DiagInfo.push_back(D);
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000222
223 // If this is a warning, and all warnings are supposed to map to errors,
224 // insert the mapping now.
225 if (L == Diagnostic::Warning && Diags.getWarningsAsErrors())
226 Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR);
Chris Lattner182745a2007-12-02 01:09:57 +0000227 return ID;
228 }
229 };
230
231 } // end diag namespace
232} // end clang namespace
233
234
235//===----------------------------------------------------------------------===//
236// Common Diagnostic implementation
237//===----------------------------------------------------------------------===//
238
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000239static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
240 const char *Modifier, unsigned ML,
241 const char *Argument, unsigned ArgLen,
Chris Lattner92dd3862009-02-19 23:53:20 +0000242 llvm::SmallVectorImpl<char> &Output,
243 void *Cookie) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000244 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +0000245 Output.append(Str, Str+strlen(Str));
246}
247
248
Ted Kremenekb4398aa2008-08-07 17:49:57 +0000249Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000250 AllExtensionsSilenced = 0;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000251 IgnoreAllWarnings = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 WarningsAsErrors = false;
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000253 SuppressSystemWarnings = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000254
255 ErrorOccurred = false;
Chris Lattner15221422009-02-06 04:16:02 +0000256 FatalErrorOccurred = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 NumDiagnostics = 0;
258 NumErrors = 0;
Chris Lattner182745a2007-12-02 01:09:57 +0000259 CustomDiagInfo = 0;
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000260 CurDiagID = ~0U;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000261 LastDiagLevel = Ignored;
Chris Lattner22caddc2008-11-23 09:13:29 +0000262
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000263 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +0000264 ArgToStringCookie = 0;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000265
266 // Set all mappings to their default.
267 for (unsigned i = 0, e = sizeof(DefaultMappings)/sizeof(DefaultMappings[0]);
268 i != e; ++i)
269 setDiagnosticMappingInternal(DefaultMappings[i].DiagID,
270 DefaultMappings[i].Mapping);
Reid Spencer5f016e22007-07-11 17:01:13 +0000271}
272
Chris Lattner182745a2007-12-02 01:09:57 +0000273Diagnostic::~Diagnostic() {
274 delete CustomDiagInfo;
275}
276
277/// getCustomDiagID - Return an ID for a diagnostic with the specified message
278/// and level. If this is the first request for this diagnosic, it is
279/// registered and created, otherwise the existing ID is returned.
280unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
281 if (CustomDiagInfo == 0)
282 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000283 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattner182745a2007-12-02 01:09:57 +0000284}
285
286
Chris Lattnerf5d23282009-02-17 06:49:55 +0000287/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
288/// level of the specified diagnostic ID is a Warning or Extension.
289/// This only works on builtin diagnostics, not custom ones, and is not legal to
290/// call on NOTEs.
291bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000292 return DiagID < diag::DIAG_UPPER_LIMIT && getBuiltinDiagClass(DiagID) < ERROR;
Reid Spencer5f016e22007-07-11 17:01:13 +0000293}
294
Douglas Gregoree1828a2009-03-10 18:03:33 +0000295/// \brief Determine whether the given built-in diagnostic ID is a
296/// Note.
297bool Diagnostic::isBuiltinNote(unsigned DiagID) {
298 return DiagID < diag::DIAG_UPPER_LIMIT && getBuiltinDiagClass(DiagID) == NOTE;
299}
300
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000301/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
302/// ID is for an extension of some sort.
303///
304bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
305 if (DiagID < diag::DIAG_UPPER_LIMIT) {
306 unsigned Class = getBuiltinDiagClass(DiagID);
307 return Class == EXTENSION || Class == EXTWARN;
308 }
309 return false;
310}
311
Reid Spencer5f016e22007-07-11 17:01:13 +0000312
313/// getDescription - Given a diagnostic ID, return a description of the
314/// issue.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000315const char *Diagnostic::getDescription(unsigned DiagID) const {
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000316 if (DiagID < diag::DIAG_START_DRIVER)
Chris Lattnerf5d23282009-02-17 06:49:55 +0000317 return DiagnosticTextCommon[DiagID];
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000318 else if (DiagID < diag::DIAG_START_FRONTEND)
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000319 return DiagnosticTextDriver[DiagID - diag::DIAG_START_DRIVER - 1];
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000320 else if (DiagID < diag::DIAG_START_LEX)
321 return DiagnosticTextFrontend[DiagID - diag::DIAG_START_FRONTEND - 1];
Chris Lattnerf5d23282009-02-17 06:49:55 +0000322 else if (DiagID < diag::DIAG_START_PARSE)
323 return DiagnosticTextLex[DiagID - diag::DIAG_START_LEX - 1];
324 else if (DiagID < diag::DIAG_START_AST)
325 return DiagnosticTextParse[DiagID - diag::DIAG_START_PARSE - 1];
326 else if (DiagID < diag::DIAG_START_SEMA)
327 return DiagnosticTextAST[DiagID - diag::DIAG_START_AST - 1];
328 else if (DiagID < diag::DIAG_START_ANALYSIS)
329 return DiagnosticTextSema[DiagID - diag::DIAG_START_SEMA - 1];
330 else if (DiagID < diag::DIAG_UPPER_LIMIT)
331 return DiagnosticTextAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000332 return CustomDiagInfo->getDescription(DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000333}
334
335/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
336/// object, classify the specified diagnostic ID into a Level, consumable by
337/// the DiagnosticClient.
338Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattner182745a2007-12-02 01:09:57 +0000339 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000340 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattner182745a2007-12-02 01:09:57 +0000341 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner07506182007-11-30 22:53:43 +0000342
343 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnerf5d23282009-02-17 06:49:55 +0000344 assert(DiagClass != NOTE && "Cannot get the diagnostic level of a note!");
345 return getDiagnosticLevel(DiagID, DiagClass);
346}
347
348/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
349/// object, classify the specified diagnostic ID into a Level, consumable by
350/// the DiagnosticClient.
351Diagnostic::Level
352Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerf5d23282009-02-17 06:49:55 +0000354 // to error. Errors can only be mapped to fatal.
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000355 Diagnostic::Level Result = Diagnostic::Fatal;
Chris Lattnerf5d23282009-02-17 06:49:55 +0000356 switch (getDiagnosticMapping((diag::kind)DiagID)) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000357 case diag::MAP_IGNORE:
358 return Diagnostic::Ignored;
359 case diag::MAP_ERROR:
360 Result = Diagnostic::Error;
361 break;
362 case diag::MAP_FATAL:
363 Result = Diagnostic::Fatal;
364 break;
365 case diag::MAP_WARNING:
366 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner5b4681c2008-05-29 15:36:45 +0000367 if (IgnoreAllWarnings)
368 return Diagnostic::Ignored;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000369 Result = WarningsAsErrors ? Diagnostic::Error : Diagnostic::Warning;
370 break;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000371 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000372
373 // Okay, we're about to return this as a "diagnostic to emit" one last check:
374 // if this is any sort of extension warning, and if we're in an __extension__
375 // block, silence it.
376 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
377 return Diagnostic::Ignored;
Reid Spencer5f016e22007-07-11 17:01:13 +0000378
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000379 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000380}
381
Chris Lattner0a14eee2008-11-18 07:04:44 +0000382/// ProcessDiag - This is the method used to report a diagnostic that is
383/// finally fully formed.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000384void Diagnostic::ProcessDiag() {
385 DiagnosticInfo Info(this);
Douglas Gregor525c4b02009-03-19 18:55:06 +0000386
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 // Figure out the diagnostic level of this message.
Chris Lattnerf5d23282009-02-17 06:49:55 +0000388 Diagnostic::Level DiagLevel;
389 unsigned DiagID = Info.getID();
Reid Spencer5f016e22007-07-11 17:01:13 +0000390
Chris Lattnerf5d23282009-02-17 06:49:55 +0000391 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
392 // in a system header.
393 bool ShouldEmitInSystemHeader;
394
395 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
396 // Handle custom diagnostics, which cannot be mapped.
397 DiagLevel = CustomDiagInfo->getLevel(DiagID);
398
399 // Custom diagnostics always are emitted in system headers.
400 ShouldEmitInSystemHeader = true;
401 } else {
402 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
403 // the diagnostic level was for the previous diagnostic so that it is
404 // filtered the same as the previous diagnostic.
405 unsigned DiagClass = getBuiltinDiagClass(DiagID);
406 if (DiagClass == NOTE) {
407 DiagLevel = Diagnostic::Note;
408 ShouldEmitInSystemHeader = false; // extra consideration is needed
409 } else {
410 // If this is not an error and we are in a system header, we ignore it.
411 // Check the original Diag ID here, because we also want to ignore
412 // extensions and warnings in -Werror and -pedantic-errors modes, which
413 // *map* warnings/extensions to errors.
414 ShouldEmitInSystemHeader = DiagClass == ERROR;
415
416 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
417 }
418 }
419
Douglas Gregor525c4b02009-03-19 18:55:06 +0000420 if (DiagLevel != Diagnostic::Note) {
421 // Record that a fatal error occurred only when we see a second
422 // non-note diagnostic. This allows notes to be attached to the
423 // fatal error, but suppresses any diagnostics that follow those
424 // notes.
425 if (LastDiagLevel == Diagnostic::Fatal)
426 FatalErrorOccurred = true;
427
Chris Lattnerf5d23282009-02-17 06:49:55 +0000428 LastDiagLevel = DiagLevel;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000429 }
430
431 // If a fatal error has already been emitted, silence all subsequent
432 // diagnostics.
433 if (FatalErrorOccurred)
434 return;
435
Chris Lattnerf5d23282009-02-17 06:49:55 +0000436 // If the client doesn't care about this message, don't issue it. If this is
437 // a note and the last real diagnostic was ignored, ignore it too.
438 if (DiagLevel == Diagnostic::Ignored ||
439 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Reid Spencer5f016e22007-07-11 17:01:13 +0000440 return;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000441
Chris Lattnerf5d23282009-02-17 06:49:55 +0000442 // If this diagnostic is in a system header and is not a clang error, suppress
443 // it.
444 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner0a14eee2008-11-18 07:04:44 +0000445 Info.getLocation().isValid() &&
Chris Lattnerf5d23282009-02-17 06:49:55 +0000446 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner336f26b2009-02-17 06:52:20 +0000447 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
448 LastDiagLevel = Diagnostic::Ignored;
Chris Lattner7097d912008-02-03 09:00:04 +0000449 return;
Chris Lattner336f26b2009-02-17 06:52:20 +0000450 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 if (DiagLevel >= Diagnostic::Error) {
453 ErrorOccurred = true;
Chris Lattner0a14eee2008-11-18 07:04:44 +0000454 ++NumErrors;
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000456
Reid Spencer5f016e22007-07-11 17:01:13 +0000457 // Finally, report it.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000458 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekcabe6682009-01-23 20:28:53 +0000459 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000460
Douglas Gregoree1828a2009-03-10 18:03:33 +0000461 CurDiagID = ~0U;
Reid Spencer5f016e22007-07-11 17:01:13 +0000462}
463
Nico Weber7bfaaae2008-08-10 19:59:06 +0000464
Reid Spencer5f016e22007-07-11 17:01:13 +0000465DiagnosticClient::~DiagnosticClient() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000466
Chris Lattnerf4c83962008-11-19 06:51:40 +0000467
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000468/// ModifierIs - Return true if the specified modifier matches specified string.
469template <std::size_t StrLen>
470static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
471 const char (&Str)[StrLen]) {
472 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
473}
474
475/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
476/// like this: %select{foo|bar|baz}2. This means that the integer argument
477/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
478/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
479/// This is very useful for certain classes of variant diagnostics.
480static void HandleSelectModifier(unsigned ValNo,
481 const char *Argument, unsigned ArgumentLen,
482 llvm::SmallVectorImpl<char> &OutStr) {
483 const char *ArgumentEnd = Argument+ArgumentLen;
484
485 // Skip over 'ValNo' |'s.
486 while (ValNo) {
487 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
488 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
489 " larger than the number of options in the diagnostic string!");
490 Argument = NextVal+1; // Skip this string.
491 --ValNo;
492 }
493
494 // Get the end of the value. This is either the } or the |.
495 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
496 // Add the value to the output string.
497 OutStr.append(Argument, EndPtr);
498}
499
500/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
501/// letter 's' to the string if the value is not 1. This is used in cases like
502/// this: "you idiot, you have %4 parameter%s4!".
503static void HandleIntegerSModifier(unsigned ValNo,
504 llvm::SmallVectorImpl<char> &OutStr) {
505 if (ValNo != 1)
506 OutStr.push_back('s');
507}
508
509
Sebastian Redle4c452c2008-11-22 13:44:36 +0000510/// PluralNumber - Parse an unsigned integer and advance Start.
511static unsigned PluralNumber(const char *&Start, const char *End)
512{
513 // Programming 101: Parse a decimal number :-)
514 unsigned Val = 0;
515 while (Start != End && *Start >= '0' && *Start <= '9') {
516 Val *= 10;
517 Val += *Start - '0';
518 ++Start;
519 }
520 return Val;
521}
522
523/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
524static bool TestPluralRange(unsigned Val, const char *&Start, const char *End)
525{
526 if (*Start != '[') {
527 unsigned Ref = PluralNumber(Start, End);
528 return Ref == Val;
529 }
530
531 ++Start;
532 unsigned Low = PluralNumber(Start, End);
533 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
534 ++Start;
535 unsigned High = PluralNumber(Start, End);
536 assert(*Start == ']' && "Bad plural expression syntax: expected )");
537 ++Start;
538 return Low <= Val && Val <= High;
539}
540
541/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
542static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End)
543{
544 // Empty condition?
545 if (*Start == ':')
546 return true;
547
548 while (1) {
549 char C = *Start;
550 if (C == '%') {
551 // Modulo expression
552 ++Start;
553 unsigned Arg = PluralNumber(Start, End);
554 assert(*Start == '=' && "Bad plural expression syntax: expected =");
555 ++Start;
556 unsigned ValMod = ValNo % Arg;
557 if (TestPluralRange(ValMod, Start, End))
558 return true;
559 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000560 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000561 "Bad plural expression syntax: unexpected character");
562 // Range expression
563 if (TestPluralRange(ValNo, Start, End))
564 return true;
565 }
566
567 // Scan for next or-expr part.
568 Start = std::find(Start, End, ',');
569 if(Start == End)
570 break;
571 ++Start;
572 }
573 return false;
574}
575
576/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
577/// for complex plural forms, or in languages where all plurals are complex.
578/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
579/// conditions that are tested in order, the form corresponding to the first
580/// that applies being emitted. The empty condition is always true, making the
581/// last form a default case.
582/// Conditions are simple boolean expressions, where n is the number argument.
583/// Here are the rules.
584/// condition := expression | empty
585/// empty := -> always true
586/// expression := numeric [',' expression] -> logical or
587/// numeric := range -> true if n in range
588/// | '%' number '=' range -> true if n % number in range
589/// range := number
590/// | '[' number ',' number ']' -> ranges are inclusive both ends
591///
592/// Here are some examples from the GNU gettext manual written in this form:
593/// English:
594/// {1:form0|:form1}
595/// Latvian:
596/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
597/// Gaeilge:
598/// {1:form0|2:form1|:form2}
599/// Romanian:
600/// {1:form0|0,%100=[1,19]:form1|:form2}
601/// Lithuanian:
602/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
603/// Russian (requires repeated form):
604/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
605/// Slovak
606/// {1:form0|[2,4]:form1|:form2}
607/// Polish (requires repeated form):
608/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
609static void HandlePluralModifier(unsigned ValNo,
610 const char *Argument, unsigned ArgumentLen,
611 llvm::SmallVectorImpl<char> &OutStr)
612{
613 const char *ArgumentEnd = Argument + ArgumentLen;
614 while (1) {
615 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
616 const char *ExprEnd = Argument;
617 while (*ExprEnd != ':') {
618 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
619 ++ExprEnd;
620 }
621 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
622 Argument = ExprEnd + 1;
623 ExprEnd = std::find(Argument, ArgumentEnd, '|');
624 OutStr.append(Argument, ExprEnd);
625 return;
626 }
627 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
628 }
629}
630
631
Chris Lattnerf4c83962008-11-19 06:51:40 +0000632/// FormatDiagnostic - Format this diagnostic into a string, substituting the
633/// formal arguments into the %0 slots. The result is appended onto the Str
634/// array.
635void DiagnosticInfo::
636FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
637 const char *DiagStr = getDiags()->getDescription(getID());
638 const char *DiagEnd = DiagStr+strlen(DiagStr);
Nico Weber7bfaaae2008-08-10 19:59:06 +0000639
Chris Lattnerf4c83962008-11-19 06:51:40 +0000640 while (DiagStr != DiagEnd) {
641 if (DiagStr[0] != '%') {
642 // Append non-%0 substrings to Str if we have one.
643 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
644 OutStr.append(DiagStr, StrEnd);
645 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000646 continue;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000647 } else if (DiagStr[1] == '%') {
648 OutStr.push_back('%'); // %% -> %.
649 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000650 continue;
651 }
652
653 // Skip the %.
654 ++DiagStr;
655
656 // This must be a placeholder for a diagnostic argument. The format for a
657 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
658 // The digit is a number from 0-9 indicating which argument this comes from.
659 // The modifier is a string of digits from the set [-a-z]+, arguments is a
660 // brace enclosed string.
661 const char *Modifier = 0, *Argument = 0;
662 unsigned ModifierLen = 0, ArgumentLen = 0;
663
664 // Check to see if we have a modifier. If so eat it.
665 if (!isdigit(DiagStr[0])) {
666 Modifier = DiagStr;
667 while (DiagStr[0] == '-' ||
668 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
669 ++DiagStr;
670 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000671
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000672 // If we have an argument, get it next.
673 if (DiagStr[0] == '{') {
674 ++DiagStr; // Skip {.
675 Argument = DiagStr;
676
677 for (; DiagStr[0] != '}'; ++DiagStr)
678 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
679 ArgumentLen = DiagStr-Argument;
680 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000681 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000682 }
683
684 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000685 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000686
Chris Lattner22caddc2008-11-23 09:13:29 +0000687 switch (getArgKind(ArgNo)) {
Chris Lattner08631c52008-11-23 21:45:46 +0000688 // ---- STRINGS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000689 case Diagnostic::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000690 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000691 assert(ModifierLen == 0 && "No modifiers for strings yet");
692 OutStr.append(S.begin(), S.end());
693 break;
694 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000695 case Diagnostic::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000696 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000697 assert(ModifierLen == 0 && "No modifiers for strings yet");
698 OutStr.append(S, S + strlen(S));
699 break;
700 }
Chris Lattner08631c52008-11-23 21:45:46 +0000701 // ---- INTEGERS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000702 case Diagnostic::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000703 int Val = getArgSInt(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000704
705 if (ModifierIs(Modifier, ModifierLen, "select")) {
706 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
707 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
708 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000709 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
710 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000711 } else {
712 assert(ModifierLen == 0 && "Unknown integer modifier");
Chris Lattner30bc9652008-11-19 07:22:31 +0000713 // FIXME: Optimize
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000714 std::string S = llvm::itostr(Val);
Chris Lattner30bc9652008-11-19 07:22:31 +0000715 OutStr.append(S.begin(), S.end());
Chris Lattner30bc9652008-11-19 07:22:31 +0000716 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000717 break;
718 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000719 case Diagnostic::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000720 unsigned Val = getArgUInt(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000721
722 if (ModifierIs(Modifier, ModifierLen, "select")) {
723 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
724 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
725 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000726 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
727 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000728 } else {
729 assert(ModifierLen == 0 && "Unknown integer modifier");
730
Chris Lattner30bc9652008-11-19 07:22:31 +0000731 // FIXME: Optimize
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000732 std::string S = llvm::utostr_32(Val);
Chris Lattner30bc9652008-11-19 07:22:31 +0000733 OutStr.append(S.begin(), S.end());
Chris Lattner30bc9652008-11-19 07:22:31 +0000734 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000735 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000736 }
Chris Lattner08631c52008-11-23 21:45:46 +0000737 // ---- NAMES and TYPES ----
738 case Diagnostic::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000739 const IdentifierInfo *II = getArgIdentifier(ArgNo);
740 assert(ModifierLen == 0 && "No modifiers for strings yet");
Chris Lattnerd0344a42009-02-19 23:45:49 +0000741 OutStr.push_back('\'');
Chris Lattner08631c52008-11-23 21:45:46 +0000742 OutStr.append(II->getName(), II->getName() + II->getLength());
743 OutStr.push_back('\'');
744 break;
745 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000746 case Diagnostic::ak_qualtype:
Chris Lattner011bb4e2008-11-23 20:28:15 +0000747 case Diagnostic::ak_declarationname:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000748 case Diagnostic::ak_nameddecl:
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000749 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
750 Modifier, ModifierLen,
751 Argument, ArgumentLen, OutStr);
Chris Lattner22caddc2008-11-23 09:13:29 +0000752 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000753 }
754 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000755}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000756
757/// IncludeInDiagnosticCounts - This method (whose default implementation
758/// returns true) indicates whether the diagnostics handled by this
759/// DiagnosticClient should be included in the number of diagnostics
760/// reported by Diagnostic.
761bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }