blob: 97952a04f23ed3c06c0cfac556e6c720bbb9afce [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Diagnostic.h"
Chris Lattnerb91fd172008-11-19 07:32:16 +000015#include "clang/Basic/IdentifierTable.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000016#include "clang/Basic/SourceLocation.h"
Chris Lattner23be0672008-11-19 06:51:40 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattner91aea712008-11-19 07:22:31 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattnere6535cf2007-12-02 01:09:57 +000019#include <vector>
20#include <map>
Chris Lattner0d799d32008-03-10 17:04:53 +000021#include <cstring>
Chris Lattner22eb9722006-06-18 05:43:12 +000022using namespace clang;
23
Chris Lattnere6535cf2007-12-02 01:09:57 +000024//===----------------------------------------------------------------------===//
25// Builtin Diagnostic information
26//===----------------------------------------------------------------------===//
27
Chris Lattner22eb9722006-06-18 05:43:12 +000028/// Flag values for diagnostics.
29enum {
30 // Diagnostic classes.
31 NOTE = 0x01,
32 WARNING = 0x02,
33 EXTENSION = 0x03,
Daniel Dunbar81f7f292008-08-05 00:07:51 +000034 EXTWARN = 0x04,
35 ERROR = 0x05,
Chris Lattnerfbe22722009-02-05 22:47:05 +000036 FATAL = 0x06,
Chris Lattner22eb9722006-06-18 05:43:12 +000037 class_mask = 0x07
38};
39
40/// DiagnosticFlags - A set of flags, or'd together, that describe the
41/// diagnostic.
Chris Lattner22eb9722006-06-18 05:43:12 +000042#define DIAG(ENUM,FLAGS,DESC) FLAGS,
Chris Lattner7368d582009-01-27 18:30:58 +000043static unsigned char DiagnosticFlagsCommon[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000044#include "clang/Basic/DiagnosticCommonKinds.inc"
Chris Lattner22eb9722006-06-18 05:43:12 +000045 0
46};
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000047static unsigned char DiagnosticFlagsDriver[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000048#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000049 0
50};
Daniel Dunbar4f495982009-03-12 10:14:16 +000051static unsigned char DiagnosticFlagsFrontend[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000052#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar4f495982009-03-12 10:14:16 +000053 0
54};
Chris Lattner7368d582009-01-27 18:30:58 +000055static unsigned char DiagnosticFlagsLex[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000056#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000057 0
58};
59static unsigned char DiagnosticFlagsParse[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000060#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000061 0
62};
63static unsigned char DiagnosticFlagsAST[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000064#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000065 0
66};
67static unsigned char DiagnosticFlagsSema[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000068#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000069 0
70};
71static unsigned char DiagnosticFlagsAnalysis[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000072#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000073 0
74};
75#undef DIAG
Chris Lattner22eb9722006-06-18 05:43:12 +000076
77/// getDiagClass - Return the class field of the diagnostic.
78///
Chris Lattner4431a1b2007-11-30 22:53:43 +000079static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner4b6713e2009-01-29 17:46:13 +000080 assert(DiagID < diag::DIAG_UPPER_LIMIT &&
Chris Lattner4431a1b2007-11-30 22:53:43 +000081 "Diagnostic ID out of range!");
Chris Lattner7368d582009-01-27 18:30:58 +000082 unsigned res;
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000083 if (DiagID < diag::DIAG_START_DRIVER)
Chris Lattner7368d582009-01-27 18:30:58 +000084 res = DiagnosticFlagsCommon[DiagID];
Daniel Dunbar4f495982009-03-12 10:14:16 +000085 else if (DiagID < diag::DIAG_START_FRONTEND)
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000086 res = DiagnosticFlagsDriver[DiagID - diag::DIAG_START_DRIVER - 1];
Daniel Dunbar4f495982009-03-12 10:14:16 +000087 else if (DiagID < diag::DIAG_START_LEX)
88 res = DiagnosticFlagsFrontend[DiagID - diag::DIAG_START_FRONTEND - 1];
Chris Lattner4b6713e2009-01-29 17:46:13 +000089 else if (DiagID < diag::DIAG_START_PARSE)
90 res = DiagnosticFlagsLex[DiagID - diag::DIAG_START_LEX - 1];
91 else if (DiagID < diag::DIAG_START_AST)
92 res = DiagnosticFlagsParse[DiagID - diag::DIAG_START_PARSE - 1];
93 else if (DiagID < diag::DIAG_START_SEMA)
94 res = DiagnosticFlagsAST[DiagID - diag::DIAG_START_AST - 1];
95 else if (DiagID < diag::DIAG_START_ANALYSIS)
96 res = DiagnosticFlagsSema[DiagID - diag::DIAG_START_SEMA - 1];
Chris Lattner7368d582009-01-27 18:30:58 +000097 else
Chris Lattner4b6713e2009-01-29 17:46:13 +000098 res = DiagnosticFlagsAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
Chris Lattner7368d582009-01-27 18:30:58 +000099 return res & class_mask;
Chris Lattner22eb9722006-06-18 05:43:12 +0000100}
101
102/// DiagnosticText - An english message to print for the diagnostic. These
103/// should be localized.
Chris Lattner22eb9722006-06-18 05:43:12 +0000104#define DIAG(ENUM,FLAGS,DESC) DESC,
Chris Lattner7368d582009-01-27 18:30:58 +0000105static const char * const DiagnosticTextCommon[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000106#include "clang/Basic/DiagnosticCommonKinds.inc"
Chris Lattner22eb9722006-06-18 05:43:12 +0000107 0
108};
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000109static const char * const DiagnosticTextDriver[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000110#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000111 0
112};
Daniel Dunbar4f495982009-03-12 10:14:16 +0000113static const char * const DiagnosticTextFrontend[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000114#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar4f495982009-03-12 10:14:16 +0000115 0
116};
Chris Lattner7368d582009-01-27 18:30:58 +0000117static const char * const DiagnosticTextLex[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000118#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000119 0
120};
121static const char * const DiagnosticTextParse[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000122#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000123 0
124};
125static const char * const DiagnosticTextAST[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000126#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000127 0
128};
129static const char * const DiagnosticTextSema[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000130#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000131 0
132};
133static const char * const DiagnosticTextAnalysis[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000134#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000135 0
136};
137#undef DIAG
Chris Lattner22eb9722006-06-18 05:43:12 +0000138
Chris Lattnere6535cf2007-12-02 01:09:57 +0000139//===----------------------------------------------------------------------===//
140// Custom Diagnostic information
141//===----------------------------------------------------------------------===//
142
143namespace clang {
144 namespace diag {
145 class CustomDiagInfo {
146 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
147 std::vector<DiagDesc> DiagInfo;
148 std::map<DiagDesc, unsigned> DiagIDs;
149 public:
150
151 /// getDescription - Return the description of the specified custom
152 /// diagnostic.
153 const char *getDescription(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000154 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000155 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000156 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattnere6535cf2007-12-02 01:09:57 +0000157 }
158
159 /// getLevel - Return the level of the specified custom diagnostic.
160 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000161 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000162 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000163 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000164 }
165
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000166 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
167 Diagnostic &Diags) {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000168 DiagDesc D(L, Message);
169 // Check to see if it already exists.
170 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
171 if (I != DiagIDs.end() && I->first == D)
172 return I->second;
173
174 // If not, assign a new ID.
Chris Lattner36790cf2009-01-29 06:55:46 +0000175 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000176 DiagIDs.insert(std::make_pair(D, ID));
177 DiagInfo.push_back(D);
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000178
179 // If this is a warning, and all warnings are supposed to map to errors,
180 // insert the mapping now.
181 if (L == Diagnostic::Warning && Diags.getWarningsAsErrors())
182 Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000183 return ID;
184 }
185 };
186
187 } // end diag namespace
188} // end clang namespace
189
190
191//===----------------------------------------------------------------------===//
192// Common Diagnostic implementation
193//===----------------------------------------------------------------------===//
194
Chris Lattner63ecc502008-11-23 09:21:17 +0000195static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
196 const char *Modifier, unsigned ML,
197 const char *Argument, unsigned ArgLen,
Chris Lattnercf868c42009-02-19 23:53:20 +0000198 llvm::SmallVectorImpl<char> &Output,
199 void *Cookie) {
Chris Lattner63ecc502008-11-23 09:21:17 +0000200 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000201 Output.append(Str, Str+strlen(Str));
202}
203
204
Ted Kremenek31691ae2008-08-07 17:49:57 +0000205Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattner8c800702008-05-29 15:36:45 +0000206 IgnoreAllWarnings = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000207 WarningsAsErrors = false;
208 WarnOnExtensions = false;
209 ErrorOnExtensions = false;
Daniel Dunbar84b70f72008-09-12 18:10:20 +0000210 SuppressSystemWarnings = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000211 // Clear all mappings, setting them to MAP_DEFAULT.
212 memset(DiagMappings, 0, sizeof(DiagMappings));
Chris Lattnerc49b9052007-05-28 00:46:44 +0000213
214 ErrorOccurred = false;
Chris Lattner9e031192009-02-06 04:16:02 +0000215 FatalErrorOccurred = false;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000216 NumDiagnostics = 0;
217 NumErrors = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000218 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000219 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000220 LastDiagLevel = Ignored;
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000221
Chris Lattner63ecc502008-11-23 09:21:17 +0000222 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000223 ArgToStringCookie = 0;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000224
225 InPostDiagnosticHook = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000226}
227
Chris Lattnere6535cf2007-12-02 01:09:57 +0000228Diagnostic::~Diagnostic() {
229 delete CustomDiagInfo;
230}
231
232/// getCustomDiagID - Return an ID for a diagnostic with the specified message
233/// and level. If this is the first request for this diagnosic, it is
234/// registered and created, otherwise the existing ID is returned.
235unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
236 if (CustomDiagInfo == 0)
237 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000238 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000239}
240
241
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000242/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
243/// level of the specified diagnostic ID is a Warning or Extension.
244/// This only works on builtin diagnostics, not custom ones, and is not legal to
245/// call on NOTEs.
246bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattner4b6713e2009-01-29 17:46:13 +0000247 return DiagID < diag::DIAG_UPPER_LIMIT && getBuiltinDiagClass(DiagID) < ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000248}
249
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000250/// \brief Determine whether the given built-in diagnostic ID is a
251/// Note.
252bool Diagnostic::isBuiltinNote(unsigned DiagID) {
253 return DiagID < diag::DIAG_UPPER_LIMIT && getBuiltinDiagClass(DiagID) == NOTE;
254}
255
Chris Lattner22eb9722006-06-18 05:43:12 +0000256
257/// getDescription - Given a diagnostic ID, return a description of the
258/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000259const char *Diagnostic::getDescription(unsigned DiagID) const {
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000260 if (DiagID < diag::DIAG_START_DRIVER)
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000261 return DiagnosticTextCommon[DiagID];
Daniel Dunbar4f495982009-03-12 10:14:16 +0000262 else if (DiagID < diag::DIAG_START_FRONTEND)
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000263 return DiagnosticTextDriver[DiagID - diag::DIAG_START_DRIVER - 1];
Daniel Dunbar4f495982009-03-12 10:14:16 +0000264 else if (DiagID < diag::DIAG_START_LEX)
265 return DiagnosticTextFrontend[DiagID - diag::DIAG_START_FRONTEND - 1];
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000266 else if (DiagID < diag::DIAG_START_PARSE)
267 return DiagnosticTextLex[DiagID - diag::DIAG_START_LEX - 1];
268 else if (DiagID < diag::DIAG_START_AST)
269 return DiagnosticTextParse[DiagID - diag::DIAG_START_PARSE - 1];
270 else if (DiagID < diag::DIAG_START_SEMA)
271 return DiagnosticTextAST[DiagID - diag::DIAG_START_AST - 1];
272 else if (DiagID < diag::DIAG_START_ANALYSIS)
273 return DiagnosticTextSema[DiagID - diag::DIAG_START_SEMA - 1];
274 else if (DiagID < diag::DIAG_UPPER_LIMIT)
275 return DiagnosticTextAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
Chris Lattner7368d582009-01-27 18:30:58 +0000276 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000277}
278
279/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
280/// object, classify the specified diagnostic ID into a Level, consumable by
281/// the DiagnosticClient.
282Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000283 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000284 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000285 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner4431a1b2007-11-30 22:53:43 +0000286
287 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000288 assert(DiagClass != NOTE && "Cannot get the diagnostic level of a note!");
289 return getDiagnosticLevel(DiagID, DiagClass);
290}
291
292/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
293/// object, classify the specified diagnostic ID into a Level, consumable by
294/// the DiagnosticClient.
295Diagnostic::Level
296Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattnerae411572006-07-05 00:55:08 +0000297 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000298 // to error. Errors can only be mapped to fatal.
299 switch (getDiagnosticMapping((diag::kind)DiagID)) {
300 case diag::MAP_DEFAULT: break;
301 case diag::MAP_IGNORE: return Diagnostic::Ignored;
302 case diag::MAP_WARNING: DiagClass = WARNING; break;
303 case diag::MAP_ERROR: DiagClass = ERROR; break;
304 case diag::MAP_FATAL: DiagClass = FATAL; break;
Chris Lattnerae411572006-07-05 00:55:08 +0000305 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000306
307 // Map diagnostic classes based on command line argument settings.
308 if (DiagClass == EXTENSION) {
309 if (ErrorOnExtensions)
310 DiagClass = ERROR;
311 else if (WarnOnExtensions)
312 DiagClass = WARNING;
313 else
314 return Ignored;
Daniel Dunbar81f7f292008-08-05 00:07:51 +0000315 } else if (DiagClass == EXTWARN) {
316 DiagClass = ErrorOnExtensions ? ERROR : WARNING;
Chris Lattner22eb9722006-06-18 05:43:12 +0000317 }
318
Chris Lattner8c800702008-05-29 15:36:45 +0000319 // If warnings are globally mapped to ignore or error, do it.
320 if (DiagClass == WARNING) {
321 if (IgnoreAllWarnings)
322 return Diagnostic::Ignored;
323 if (WarningsAsErrors)
324 DiagClass = ERROR;
325 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000326
327 switch (DiagClass) {
328 default: assert(0 && "Unknown diagnostic class!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000329 case WARNING: return Diagnostic::Warning;
330 case ERROR: return Diagnostic::Error;
Chris Lattnerfbe22722009-02-05 22:47:05 +0000331 case FATAL: return Diagnostic::Fatal;
Chris Lattner22eb9722006-06-18 05:43:12 +0000332 }
333}
334
Chris Lattner8488c822008-11-18 07:04:44 +0000335/// ProcessDiag - This is the method used to report a diagnostic that is
336/// finally fully formed.
Chris Lattner427c9c12008-11-22 00:59:29 +0000337void Diagnostic::ProcessDiag() {
338 DiagnosticInfo Info(this);
Douglas Gregor19367f52009-03-19 18:55:06 +0000339
Chris Lattner22eb9722006-06-18 05:43:12 +0000340 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000341 Diagnostic::Level DiagLevel;
342 unsigned DiagID = Info.getID();
Chris Lattner22eb9722006-06-18 05:43:12 +0000343
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000344 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
345 // in a system header.
346 bool ShouldEmitInSystemHeader;
347
348 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
349 // Handle custom diagnostics, which cannot be mapped.
350 DiagLevel = CustomDiagInfo->getLevel(DiagID);
351
352 // Custom diagnostics always are emitted in system headers.
353 ShouldEmitInSystemHeader = true;
354 } else {
355 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
356 // the diagnostic level was for the previous diagnostic so that it is
357 // filtered the same as the previous diagnostic.
358 unsigned DiagClass = getBuiltinDiagClass(DiagID);
359 if (DiagClass == NOTE) {
360 DiagLevel = Diagnostic::Note;
361 ShouldEmitInSystemHeader = false; // extra consideration is needed
362 } else {
363 // If this is not an error and we are in a system header, we ignore it.
364 // Check the original Diag ID here, because we also want to ignore
365 // extensions and warnings in -Werror and -pedantic-errors modes, which
366 // *map* warnings/extensions to errors.
367 ShouldEmitInSystemHeader = DiagClass == ERROR;
368
369 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
370 }
371 }
372
Douglas Gregor19367f52009-03-19 18:55:06 +0000373 if (DiagLevel != Diagnostic::Note) {
374 // Record that a fatal error occurred only when we see a second
375 // non-note diagnostic. This allows notes to be attached to the
376 // fatal error, but suppresses any diagnostics that follow those
377 // notes.
378 if (LastDiagLevel == Diagnostic::Fatal)
379 FatalErrorOccurred = true;
380
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000381 LastDiagLevel = DiagLevel;
Douglas Gregor19367f52009-03-19 18:55:06 +0000382 }
383
384 // If a fatal error has already been emitted, silence all subsequent
385 // diagnostics.
386 if (FatalErrorOccurred)
387 return;
388
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000389 // If the client doesn't care about this message, don't issue it. If this is
390 // a note and the last real diagnostic was ignored, ignore it too.
391 if (DiagLevel == Diagnostic::Ignored ||
392 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Chris Lattnercb283342006-06-18 06:48:37 +0000393 return;
Nico Weber4c311642008-08-10 19:59:06 +0000394
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000395 // If this diagnostic is in a system header and is not a clang error, suppress
396 // it.
397 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner8488c822008-11-18 07:04:44 +0000398 Info.getLocation().isValid() &&
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000399 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000400 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
401 LastDiagLevel = Diagnostic::Ignored;
Chris Lattner3ac96992008-02-03 09:00:04 +0000402 return;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000403 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000404
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000405 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000406 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000407 ++NumErrors;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000408 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000409
Chris Lattner22eb9722006-06-18 05:43:12 +0000410 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000411 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekea06ec12009-01-23 20:28:53 +0000412 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000413
414 // Invoke any post-diagnostic hooks.
415 unsigned LastDiag = CurDiagID;
416 CurDiagID = ~0U;
417
418 InPostDiagnosticHook = true;
419 for (unsigned Hook = 0; Hook < NumPostDiagnosticHooks; ++Hook)
420 PostDiagnosticHooks[Hook].Hook(LastDiag,
421 PostDiagnosticHooks[Hook].Cookie);
422 InPostDiagnosticHook = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000423}
424
Nico Weber4c311642008-08-10 19:59:06 +0000425
Chris Lattner22eb9722006-06-18 05:43:12 +0000426DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000427
Chris Lattner23be0672008-11-19 06:51:40 +0000428
Chris Lattner2b786902008-11-21 07:50:02 +0000429/// ModifierIs - Return true if the specified modifier matches specified string.
430template <std::size_t StrLen>
431static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
432 const char (&Str)[StrLen]) {
433 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
434}
435
436/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
437/// like this: %select{foo|bar|baz}2. This means that the integer argument
438/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
439/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
440/// This is very useful for certain classes of variant diagnostics.
441static void HandleSelectModifier(unsigned ValNo,
442 const char *Argument, unsigned ArgumentLen,
443 llvm::SmallVectorImpl<char> &OutStr) {
444 const char *ArgumentEnd = Argument+ArgumentLen;
445
446 // Skip over 'ValNo' |'s.
447 while (ValNo) {
448 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
449 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
450 " larger than the number of options in the diagnostic string!");
451 Argument = NextVal+1; // Skip this string.
452 --ValNo;
453 }
454
455 // Get the end of the value. This is either the } or the |.
456 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
457 // Add the value to the output string.
458 OutStr.append(Argument, EndPtr);
459}
460
461/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
462/// letter 's' to the string if the value is not 1. This is used in cases like
463/// this: "you idiot, you have %4 parameter%s4!".
464static void HandleIntegerSModifier(unsigned ValNo,
465 llvm::SmallVectorImpl<char> &OutStr) {
466 if (ValNo != 1)
467 OutStr.push_back('s');
468}
469
470
Sebastian Redl15b02d22008-11-22 13:44:36 +0000471/// PluralNumber - Parse an unsigned integer and advance Start.
472static unsigned PluralNumber(const char *&Start, const char *End)
473{
474 // Programming 101: Parse a decimal number :-)
475 unsigned Val = 0;
476 while (Start != End && *Start >= '0' && *Start <= '9') {
477 Val *= 10;
478 Val += *Start - '0';
479 ++Start;
480 }
481 return Val;
482}
483
484/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
485static bool TestPluralRange(unsigned Val, const char *&Start, const char *End)
486{
487 if (*Start != '[') {
488 unsigned Ref = PluralNumber(Start, End);
489 return Ref == Val;
490 }
491
492 ++Start;
493 unsigned Low = PluralNumber(Start, End);
494 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
495 ++Start;
496 unsigned High = PluralNumber(Start, End);
497 assert(*Start == ']' && "Bad plural expression syntax: expected )");
498 ++Start;
499 return Low <= Val && Val <= High;
500}
501
502/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
503static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End)
504{
505 // Empty condition?
506 if (*Start == ':')
507 return true;
508
509 while (1) {
510 char C = *Start;
511 if (C == '%') {
512 // Modulo expression
513 ++Start;
514 unsigned Arg = PluralNumber(Start, End);
515 assert(*Start == '=' && "Bad plural expression syntax: expected =");
516 ++Start;
517 unsigned ValMod = ValNo % Arg;
518 if (TestPluralRange(ValMod, Start, End))
519 return true;
520 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000521 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000522 "Bad plural expression syntax: unexpected character");
523 // Range expression
524 if (TestPluralRange(ValNo, Start, End))
525 return true;
526 }
527
528 // Scan for next or-expr part.
529 Start = std::find(Start, End, ',');
530 if(Start == End)
531 break;
532 ++Start;
533 }
534 return false;
535}
536
537/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
538/// for complex plural forms, or in languages where all plurals are complex.
539/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
540/// conditions that are tested in order, the form corresponding to the first
541/// that applies being emitted. The empty condition is always true, making the
542/// last form a default case.
543/// Conditions are simple boolean expressions, where n is the number argument.
544/// Here are the rules.
545/// condition := expression | empty
546/// empty := -> always true
547/// expression := numeric [',' expression] -> logical or
548/// numeric := range -> true if n in range
549/// | '%' number '=' range -> true if n % number in range
550/// range := number
551/// | '[' number ',' number ']' -> ranges are inclusive both ends
552///
553/// Here are some examples from the GNU gettext manual written in this form:
554/// English:
555/// {1:form0|:form1}
556/// Latvian:
557/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
558/// Gaeilge:
559/// {1:form0|2:form1|:form2}
560/// Romanian:
561/// {1:form0|0,%100=[1,19]:form1|:form2}
562/// Lithuanian:
563/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
564/// Russian (requires repeated form):
565/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
566/// Slovak
567/// {1:form0|[2,4]:form1|:form2}
568/// Polish (requires repeated form):
569/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
570static void HandlePluralModifier(unsigned ValNo,
571 const char *Argument, unsigned ArgumentLen,
572 llvm::SmallVectorImpl<char> &OutStr)
573{
574 const char *ArgumentEnd = Argument + ArgumentLen;
575 while (1) {
576 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
577 const char *ExprEnd = Argument;
578 while (*ExprEnd != ':') {
579 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
580 ++ExprEnd;
581 }
582 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
583 Argument = ExprEnd + 1;
584 ExprEnd = std::find(Argument, ArgumentEnd, '|');
585 OutStr.append(Argument, ExprEnd);
586 return;
587 }
588 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
589 }
590}
591
592
Chris Lattner23be0672008-11-19 06:51:40 +0000593/// FormatDiagnostic - Format this diagnostic into a string, substituting the
594/// formal arguments into the %0 slots. The result is appended onto the Str
595/// array.
596void DiagnosticInfo::
597FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
598 const char *DiagStr = getDiags()->getDescription(getID());
599 const char *DiagEnd = DiagStr+strlen(DiagStr);
Nico Weber4c311642008-08-10 19:59:06 +0000600
Chris Lattner23be0672008-11-19 06:51:40 +0000601 while (DiagStr != DiagEnd) {
602 if (DiagStr[0] != '%') {
603 // Append non-%0 substrings to Str if we have one.
604 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
605 OutStr.append(DiagStr, StrEnd);
606 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000607 continue;
Chris Lattner23be0672008-11-19 06:51:40 +0000608 } else if (DiagStr[1] == '%') {
609 OutStr.push_back('%'); // %% -> %.
610 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000611 continue;
612 }
613
614 // Skip the %.
615 ++DiagStr;
616
617 // This must be a placeholder for a diagnostic argument. The format for a
618 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
619 // The digit is a number from 0-9 indicating which argument this comes from.
620 // The modifier is a string of digits from the set [-a-z]+, arguments is a
621 // brace enclosed string.
622 const char *Modifier = 0, *Argument = 0;
623 unsigned ModifierLen = 0, ArgumentLen = 0;
624
625 // Check to see if we have a modifier. If so eat it.
626 if (!isdigit(DiagStr[0])) {
627 Modifier = DiagStr;
628 while (DiagStr[0] == '-' ||
629 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
630 ++DiagStr;
631 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000632
Chris Lattner2b786902008-11-21 07:50:02 +0000633 // If we have an argument, get it next.
634 if (DiagStr[0] == '{') {
635 ++DiagStr; // Skip {.
636 Argument = DiagStr;
637
638 for (; DiagStr[0] != '}'; ++DiagStr)
639 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
640 ArgumentLen = DiagStr-Argument;
641 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000642 }
Chris Lattner2b786902008-11-21 07:50:02 +0000643 }
644
645 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000646 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000647
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000648 switch (getArgKind(ArgNo)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000649 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000650 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000651 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000652 assert(ModifierLen == 0 && "No modifiers for strings yet");
653 OutStr.append(S.begin(), S.end());
654 break;
655 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000656 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000657 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000658 assert(ModifierLen == 0 && "No modifiers for strings yet");
659 OutStr.append(S, S + strlen(S));
660 break;
661 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000662 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000663 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000664 int Val = getArgSInt(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000665
666 if (ModifierIs(Modifier, ModifierLen, "select")) {
667 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
668 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
669 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000670 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
671 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000672 } else {
673 assert(ModifierLen == 0 && "Unknown integer modifier");
Chris Lattner91aea712008-11-19 07:22:31 +0000674 // FIXME: Optimize
Chris Lattner2b786902008-11-21 07:50:02 +0000675 std::string S = llvm::itostr(Val);
Chris Lattner91aea712008-11-19 07:22:31 +0000676 OutStr.append(S.begin(), S.end());
Chris Lattner91aea712008-11-19 07:22:31 +0000677 }
Chris Lattner2b786902008-11-21 07:50:02 +0000678 break;
679 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000680 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000681 unsigned Val = getArgUInt(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000682
683 if (ModifierIs(Modifier, ModifierLen, "select")) {
684 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
685 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
686 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000687 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
688 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000689 } else {
690 assert(ModifierLen == 0 && "Unknown integer modifier");
691
Chris Lattner91aea712008-11-19 07:22:31 +0000692 // FIXME: Optimize
Chris Lattner2b786902008-11-21 07:50:02 +0000693 std::string S = llvm::utostr_32(Val);
Chris Lattner91aea712008-11-19 07:22:31 +0000694 OutStr.append(S.begin(), S.end());
Chris Lattner91aea712008-11-19 07:22:31 +0000695 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000696 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000697 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000698 // ---- NAMES and TYPES ----
699 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000700 const IdentifierInfo *II = getArgIdentifier(ArgNo);
701 assert(ModifierLen == 0 && "No modifiers for strings yet");
Chris Lattner810d3302009-02-19 23:45:49 +0000702 OutStr.push_back('\'');
Chris Lattnere3d20d92008-11-23 21:45:46 +0000703 OutStr.append(II->getName(), II->getName() + II->getLength());
704 OutStr.push_back('\'');
705 break;
706 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000707 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000708 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000709 case Diagnostic::ak_nameddecl:
Chris Lattner63ecc502008-11-23 09:21:17 +0000710 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
711 Modifier, ModifierLen,
712 Argument, ArgumentLen, OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000713 break;
Nico Weber4c311642008-08-10 19:59:06 +0000714 }
715 }
Nico Weber4c311642008-08-10 19:59:06 +0000716}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000717
718/// IncludeInDiagnosticCounts - This method (whose default implementation
719/// returns true) indicates whether the diagnostics handled by this
720/// DiagnosticClient should be included in the number of diagnostics
721/// reported by Diagnostic.
722bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }