blob: caf30fac633d2a8f858dc175a303f0912af8d87b [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 Lattnere007de32009-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 Lattnerb91fd172008-11-19 07:32:16 +000024#include "clang/Basic/IdentifierTable.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000025#include "clang/Basic/SourceLocation.h"
Chris Lattner23be0672008-11-19 06:51:40 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner91aea712008-11-19 07:22:31 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattnere6535cf2007-12-02 01:09:57 +000028#include <vector>
29#include <map>
Chris Lattner0d799d32008-03-10 17:04:53 +000030#include <cstring>
Chris Lattner22eb9722006-06-18 05:43:12 +000031using namespace clang;
32
Chris Lattnere6535cf2007-12-02 01:09:57 +000033//===----------------------------------------------------------------------===//
34// Builtin Diagnostic information
35//===----------------------------------------------------------------------===//
36
Chris Lattnere007de32009-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
Chris Lattnere007de32009-04-15 07:01:18 +000045static const DefaultMappingInfo DefaultMappings[] = {
Chris Lattner1b595622009-04-15 16:44:12 +000046#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) { diag::ENUM, DEFAULT_MAPPING },
Chris Lattnere007de32009-04-15 07:01:18 +000047#include "clang/Basic/DiagnosticCommonKinds.inc"
48#include "clang/Basic/DiagnosticDriverKinds.inc"
49#include "clang/Basic/DiagnosticFrontendKinds.inc"
50#include "clang/Basic/DiagnosticLexKinds.inc"
51#include "clang/Basic/DiagnosticParseKinds.inc"
52#include "clang/Basic/DiagnosticASTKinds.inc"
53#include "clang/Basic/DiagnosticSemaKinds.inc"
54#include "clang/Basic/DiagnosticAnalysisKinds.inc"
55{ 0, 0 }
56};
Chris Lattnere6c831d2009-04-15 16:56:26 +000057#undef DIAG
Chris Lattnere007de32009-04-15 07:01:18 +000058
Chris Lattnere007de32009-04-15 07:01:18 +000059// Diagnostic classes.
Chris Lattner22eb9722006-06-18 05:43:12 +000060enum {
Chris Lattnere6c831d2009-04-15 16:56:26 +000061 CLASS_NOTE = 0x01,
62 CLASS_WARNING = 0x02,
63 CLASS_EXTENSION = 0x03,
64 CLASS_ERROR = 0x04
Chris Lattner22eb9722006-06-18 05:43:12 +000065};
66
Chris Lattnere007de32009-04-15 07:01:18 +000067/// DiagnosticClasses - The class for each diagnostic.
Chris Lattner1b595622009-04-15 16:44:12 +000068#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) CLASS,
Chris Lattnere007de32009-04-15 07:01:18 +000069static unsigned char DiagnosticClassesCommon[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000070#include "clang/Basic/DiagnosticCommonKinds.inc"
Chris Lattner22eb9722006-06-18 05:43:12 +000071 0
72};
Chris Lattnere007de32009-04-15 07:01:18 +000073static unsigned char DiagnosticClassesDriver[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000074#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000075 0
76};
Chris Lattnere007de32009-04-15 07:01:18 +000077static unsigned char DiagnosticClassesFrontend[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000078#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar4f495982009-03-12 10:14:16 +000079 0
80};
Chris Lattnere007de32009-04-15 07:01:18 +000081static unsigned char DiagnosticClassesLex[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000082#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000083 0
84};
Chris Lattnere007de32009-04-15 07:01:18 +000085static unsigned char DiagnosticClassesParse[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000086#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000087 0
88};
Chris Lattnere007de32009-04-15 07:01:18 +000089static unsigned char DiagnosticClassesAST[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000090#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000091 0
92};
Chris Lattnere007de32009-04-15 07:01:18 +000093static unsigned char DiagnosticClassesSema[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000094#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000095 0
96};
Chris Lattnere007de32009-04-15 07:01:18 +000097static unsigned char DiagnosticClassesAnalysis[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000098#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000099 0
100};
101#undef DIAG
Chris Lattner22eb9722006-06-18 05:43:12 +0000102
103/// getDiagClass - Return the class field of the diagnostic.
104///
Chris Lattner4431a1b2007-11-30 22:53:43 +0000105static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner4b6713e2009-01-29 17:46:13 +0000106 assert(DiagID < diag::DIAG_UPPER_LIMIT &&
Chris Lattner4431a1b2007-11-30 22:53:43 +0000107 "Diagnostic ID out of range!");
Chris Lattnerd9ecb8e2009-04-15 18:48:23 +0000108 unsigned char *Arr;
109 unsigned ArrSize;
110 if (DiagID <= diag::DIAG_START_DRIVER) {
111 DiagID -= 0;
112 Arr = DiagnosticClassesCommon;
113 ArrSize = sizeof(DiagnosticClassesCommon);
114 } else if (DiagID <= diag::DIAG_START_FRONTEND) {
115 DiagID -= diag::DIAG_START_DRIVER + 1;
116 Arr = DiagnosticClassesDriver;
117 ArrSize = sizeof(DiagnosticClassesDriver);
118 } else if (DiagID <= diag::DIAG_START_LEX) {
119 DiagID -= diag::DIAG_START_FRONTEND + 1;
120 Arr = DiagnosticClassesFrontend;
121 ArrSize = sizeof(DiagnosticClassesFrontend);
122 } else if (DiagID <= diag::DIAG_START_PARSE) {
123 DiagID -= diag::DIAG_START_LEX + 1;
124 Arr = DiagnosticClassesLex;
125 ArrSize = sizeof(DiagnosticClassesLex);
126 } else if (DiagID <= diag::DIAG_START_AST) {
127 DiagID -= diag::DIAG_START_PARSE + 1;
128 Arr = DiagnosticClassesParse;
129 ArrSize = sizeof(DiagnosticClassesParse);
130 } else if (DiagID <= diag::DIAG_START_SEMA) {
131 DiagID -= diag::DIAG_START_AST + 1;
132 Arr = DiagnosticClassesAST;
133 ArrSize = sizeof(DiagnosticClassesAST);
134
135 } else if (DiagID <= diag::DIAG_START_ANALYSIS) {
136 DiagID -= diag::DIAG_START_SEMA + 1;
137 Arr = DiagnosticClassesSema;
138 ArrSize = sizeof(DiagnosticClassesSema);
139 } else {
140 DiagID -= diag::DIAG_START_ANALYSIS + 1;
141 Arr = DiagnosticClassesAnalysis;
142 ArrSize = sizeof(DiagnosticClassesAnalysis);
143 }
144 return DiagID < ArrSize ? Arr[DiagID] : ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +0000145}
146
147/// DiagnosticText - An english message to print for the diagnostic. These
148/// should be localized.
Chris Lattner1b595622009-04-15 16:44:12 +0000149#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) DESC,
Chris Lattner7368d582009-01-27 18:30:58 +0000150static const char * const DiagnosticTextCommon[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000151#include "clang/Basic/DiagnosticCommonKinds.inc"
Chris Lattner22eb9722006-06-18 05:43:12 +0000152 0
153};
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000154static const char * const DiagnosticTextDriver[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000155#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000156 0
157};
Daniel Dunbar4f495982009-03-12 10:14:16 +0000158static const char * const DiagnosticTextFrontend[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000159#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar4f495982009-03-12 10:14:16 +0000160 0
161};
Chris Lattner7368d582009-01-27 18:30:58 +0000162static const char * const DiagnosticTextLex[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000163#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000164 0
165};
166static const char * const DiagnosticTextParse[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000167#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000168 0
169};
170static const char * const DiagnosticTextAST[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000171#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000172 0
173};
174static const char * const DiagnosticTextSema[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000175#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000176 0
177};
178static const char * const DiagnosticTextAnalysis[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000179#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000180 0
181};
182#undef DIAG
Chris Lattner22eb9722006-06-18 05:43:12 +0000183
Chris Lattnere6535cf2007-12-02 01:09:57 +0000184//===----------------------------------------------------------------------===//
185// Custom Diagnostic information
186//===----------------------------------------------------------------------===//
187
188namespace clang {
189 namespace diag {
190 class CustomDiagInfo {
191 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
192 std::vector<DiagDesc> DiagInfo;
193 std::map<DiagDesc, unsigned> DiagIDs;
194 public:
195
196 /// getDescription - Return the description of the specified custom
197 /// diagnostic.
198 const char *getDescription(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000199 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000200 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000201 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattnere6535cf2007-12-02 01:09:57 +0000202 }
203
204 /// getLevel - Return the level of the specified custom diagnostic.
205 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000206 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000207 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000208 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000209 }
210
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000211 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
212 Diagnostic &Diags) {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000213 DiagDesc D(L, Message);
214 // Check to see if it already exists.
215 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
216 if (I != DiagIDs.end() && I->first == D)
217 return I->second;
218
219 // If not, assign a new ID.
Chris Lattner36790cf2009-01-29 06:55:46 +0000220 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000221 DiagIDs.insert(std::make_pair(D, ID));
222 DiagInfo.push_back(D);
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000223
224 // If this is a warning, and all warnings are supposed to map to errors,
225 // insert the mapping now.
226 if (L == Diagnostic::Warning && Diags.getWarningsAsErrors())
227 Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000228 return ID;
229 }
230 };
231
232 } // end diag namespace
233} // end clang namespace
234
235
236//===----------------------------------------------------------------------===//
237// Common Diagnostic implementation
238//===----------------------------------------------------------------------===//
239
Chris Lattner63ecc502008-11-23 09:21:17 +0000240static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
241 const char *Modifier, unsigned ML,
242 const char *Argument, unsigned ArgLen,
Chris Lattnercf868c42009-02-19 23:53:20 +0000243 llvm::SmallVectorImpl<char> &Output,
244 void *Cookie) {
Chris Lattner63ecc502008-11-23 09:21:17 +0000245 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000246 Output.append(Str, Str+strlen(Str));
247}
248
249
Ted Kremenek31691ae2008-08-07 17:49:57 +0000250Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattnere007de32009-04-15 07:01:18 +0000251 AllExtensionsSilenced = 0;
Chris Lattner8c800702008-05-29 15:36:45 +0000252 IgnoreAllWarnings = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000253 WarningsAsErrors = false;
Daniel Dunbar84b70f72008-09-12 18:10:20 +0000254 SuppressSystemWarnings = false;
Chris Lattnerc49b9052007-05-28 00:46:44 +0000255
256 ErrorOccurred = false;
Chris Lattner9e031192009-02-06 04:16:02 +0000257 FatalErrorOccurred = false;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000258 NumDiagnostics = 0;
259 NumErrors = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000260 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000261 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000262 LastDiagLevel = Ignored;
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000263
Chris Lattner63ecc502008-11-23 09:21:17 +0000264 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000265 ArgToStringCookie = 0;
Chris Lattnere007de32009-04-15 07:01:18 +0000266
267 // Set all mappings to their default.
268 for (unsigned i = 0, e = sizeof(DefaultMappings)/sizeof(DefaultMappings[0]);
269 i != e; ++i)
270 setDiagnosticMappingInternal(DefaultMappings[i].DiagID,
271 DefaultMappings[i].Mapping);
Chris Lattnerae411572006-07-05 00:55:08 +0000272}
273
Chris Lattnere6535cf2007-12-02 01:09:57 +0000274Diagnostic::~Diagnostic() {
275 delete CustomDiagInfo;
276}
277
278/// getCustomDiagID - Return an ID for a diagnostic with the specified message
279/// and level. If this is the first request for this diagnosic, it is
280/// registered and created, otherwise the existing ID is returned.
281unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
282 if (CustomDiagInfo == 0)
283 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000284 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000285}
286
287
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000288/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
289/// level of the specified diagnostic ID is a Warning or Extension.
290/// This only works on builtin diagnostics, not custom ones, and is not legal to
291/// call on NOTEs.
292bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000293 return DiagID < diag::DIAG_UPPER_LIMIT &&
294 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000295}
296
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000297/// \brief Determine whether the given built-in diagnostic ID is a
298/// Note.
299bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000300 return DiagID < diag::DIAG_UPPER_LIMIT &&
301 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000302}
303
Chris Lattnere007de32009-04-15 07:01:18 +0000304/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
305/// ID is for an extension of some sort.
306///
307bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000308 return DiagID < diag::DIAG_UPPER_LIMIT &&
309 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattnere007de32009-04-15 07:01:18 +0000310}
311
Chris Lattner22eb9722006-06-18 05:43:12 +0000312
313/// getDescription - Given a diagnostic ID, return a description of the
314/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000315const char *Diagnostic::getDescription(unsigned DiagID) const {
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000316 if (DiagID < diag::DIAG_START_DRIVER)
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000317 return DiagnosticTextCommon[DiagID];
Daniel Dunbar4f495982009-03-12 10:14:16 +0000318 else if (DiagID < diag::DIAG_START_FRONTEND)
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000319 return DiagnosticTextDriver[DiagID - diag::DIAG_START_DRIVER - 1];
Daniel Dunbar4f495982009-03-12 10:14:16 +0000320 else if (DiagID < diag::DIAG_START_LEX)
321 return DiagnosticTextFrontend[DiagID - diag::DIAG_START_FRONTEND - 1];
Chris Lattnerd2a2c132009-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 Lattner7368d582009-01-27 18:30:58 +0000332 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +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 Lattnere6535cf2007-12-02 01:09:57 +0000339 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000340 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000341 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner4431a1b2007-11-30 22:53:43 +0000342
343 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000344 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000345 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 {
Chris Lattnerae411572006-07-05 00:55:08 +0000353 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000354 // to error. Errors can only be mapped to fatal.
Chris Lattnere007de32009-04-15 07:01:18 +0000355 Diagnostic::Level Result = Diagnostic::Fatal;
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000356 switch (getDiagnosticMapping((diag::kind)DiagID)) {
Chris Lattnere007de32009-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 Lattner8c800702008-05-29 15:36:45 +0000367 if (IgnoreAllWarnings)
368 return Diagnostic::Ignored;
Chris Lattnere007de32009-04-15 07:01:18 +0000369 Result = WarningsAsErrors ? Diagnostic::Error : Diagnostic::Warning;
370 break;
Chris Lattner8c800702008-05-29 15:36:45 +0000371 }
Chris Lattnere007de32009-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;
Chris Lattner22eb9722006-06-18 05:43:12 +0000378
Chris Lattnere007de32009-04-15 07:01:18 +0000379 return Result;
Chris Lattner22eb9722006-06-18 05:43:12 +0000380}
381
Chris Lattner8488c822008-11-18 07:04:44 +0000382/// ProcessDiag - This is the method used to report a diagnostic that is
383/// finally fully formed.
Chris Lattner427c9c12008-11-22 00:59:29 +0000384void Diagnostic::ProcessDiag() {
385 DiagnosticInfo Info(this);
Douglas Gregor19367f52009-03-19 18:55:06 +0000386
Chris Lattner22eb9722006-06-18 05:43:12 +0000387 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000388 Diagnostic::Level DiagLevel;
389 unsigned DiagID = Info.getID();
Chris Lattner22eb9722006-06-18 05:43:12 +0000390
Chris Lattnerd2a2c132009-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);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000406 if (DiagClass == CLASS_NOTE) {
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000407 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.
Chris Lattnere6c831d2009-04-15 16:56:26 +0000414 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000415
416 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
417 }
418 }
419
Douglas Gregor19367f52009-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 Lattnerd2a2c132009-02-17 06:49:55 +0000428 LastDiagLevel = DiagLevel;
Douglas Gregor19367f52009-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 Lattnerd2a2c132009-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))
Chris Lattnercb283342006-06-18 06:48:37 +0000440 return;
Nico Weber4c311642008-08-10 19:59:06 +0000441
Chris Lattnerd2a2c132009-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 Lattner8488c822008-11-18 07:04:44 +0000445 Info.getLocation().isValid() &&
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000446 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000447 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
448 LastDiagLevel = Diagnostic::Ignored;
Chris Lattner3ac96992008-02-03 09:00:04 +0000449 return;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000450 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000451
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000452 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000453 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000454 ++NumErrors;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000455 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000456
Chris Lattner22eb9722006-06-18 05:43:12 +0000457 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000458 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekea06ec12009-01-23 20:28:53 +0000459 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000460
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000461 CurDiagID = ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +0000462}
463
Nico Weber4c311642008-08-10 19:59:06 +0000464
Chris Lattner22eb9722006-06-18 05:43:12 +0000465DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000466
Chris Lattner23be0672008-11-19 06:51:40 +0000467
Chris Lattner2b786902008-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 Redl15b02d22008-11-22 13:44:36 +0000510/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000511static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000512 // Programming 101: Parse a decimal number :-)
513 unsigned Val = 0;
514 while (Start != End && *Start >= '0' && *Start <= '9') {
515 Val *= 10;
516 Val += *Start - '0';
517 ++Start;
518 }
519 return Val;
520}
521
522/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000523static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000524 if (*Start != '[') {
525 unsigned Ref = PluralNumber(Start, End);
526 return Ref == Val;
527 }
528
529 ++Start;
530 unsigned Low = PluralNumber(Start, End);
531 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
532 ++Start;
533 unsigned High = PluralNumber(Start, End);
534 assert(*Start == ']' && "Bad plural expression syntax: expected )");
535 ++Start;
536 return Low <= Val && Val <= High;
537}
538
539/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000540static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000541 // Empty condition?
542 if (*Start == ':')
543 return true;
544
545 while (1) {
546 char C = *Start;
547 if (C == '%') {
548 // Modulo expression
549 ++Start;
550 unsigned Arg = PluralNumber(Start, End);
551 assert(*Start == '=' && "Bad plural expression syntax: expected =");
552 ++Start;
553 unsigned ValMod = ValNo % Arg;
554 if (TestPluralRange(ValMod, Start, End))
555 return true;
556 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000557 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000558 "Bad plural expression syntax: unexpected character");
559 // Range expression
560 if (TestPluralRange(ValNo, Start, End))
561 return true;
562 }
563
564 // Scan for next or-expr part.
565 Start = std::find(Start, End, ',');
566 if(Start == End)
567 break;
568 ++Start;
569 }
570 return false;
571}
572
573/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
574/// for complex plural forms, or in languages where all plurals are complex.
575/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
576/// conditions that are tested in order, the form corresponding to the first
577/// that applies being emitted. The empty condition is always true, making the
578/// last form a default case.
579/// Conditions are simple boolean expressions, where n is the number argument.
580/// Here are the rules.
581/// condition := expression | empty
582/// empty := -> always true
583/// expression := numeric [',' expression] -> logical or
584/// numeric := range -> true if n in range
585/// | '%' number '=' range -> true if n % number in range
586/// range := number
587/// | '[' number ',' number ']' -> ranges are inclusive both ends
588///
589/// Here are some examples from the GNU gettext manual written in this form:
590/// English:
591/// {1:form0|:form1}
592/// Latvian:
593/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
594/// Gaeilge:
595/// {1:form0|2:form1|:form2}
596/// Romanian:
597/// {1:form0|0,%100=[1,19]:form1|:form2}
598/// Lithuanian:
599/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
600/// Russian (requires repeated form):
601/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
602/// Slovak
603/// {1:form0|[2,4]:form1|:form2}
604/// Polish (requires repeated form):
605/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
606static void HandlePluralModifier(unsigned ValNo,
607 const char *Argument, unsigned ArgumentLen,
608 llvm::SmallVectorImpl<char> &OutStr)
609{
610 const char *ArgumentEnd = Argument + ArgumentLen;
611 while (1) {
612 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
613 const char *ExprEnd = Argument;
614 while (*ExprEnd != ':') {
615 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
616 ++ExprEnd;
617 }
618 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
619 Argument = ExprEnd + 1;
620 ExprEnd = std::find(Argument, ArgumentEnd, '|');
621 OutStr.append(Argument, ExprEnd);
622 return;
623 }
624 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
625 }
626}
627
628
Chris Lattner23be0672008-11-19 06:51:40 +0000629/// FormatDiagnostic - Format this diagnostic into a string, substituting the
630/// formal arguments into the %0 slots. The result is appended onto the Str
631/// array.
632void DiagnosticInfo::
633FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
634 const char *DiagStr = getDiags()->getDescription(getID());
635 const char *DiagEnd = DiagStr+strlen(DiagStr);
Nico Weber4c311642008-08-10 19:59:06 +0000636
Chris Lattner23be0672008-11-19 06:51:40 +0000637 while (DiagStr != DiagEnd) {
638 if (DiagStr[0] != '%') {
639 // Append non-%0 substrings to Str if we have one.
640 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
641 OutStr.append(DiagStr, StrEnd);
642 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000643 continue;
Chris Lattner23be0672008-11-19 06:51:40 +0000644 } else if (DiagStr[1] == '%') {
645 OutStr.push_back('%'); // %% -> %.
646 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000647 continue;
648 }
649
650 // Skip the %.
651 ++DiagStr;
652
653 // This must be a placeholder for a diagnostic argument. The format for a
654 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
655 // The digit is a number from 0-9 indicating which argument this comes from.
656 // The modifier is a string of digits from the set [-a-z]+, arguments is a
657 // brace enclosed string.
658 const char *Modifier = 0, *Argument = 0;
659 unsigned ModifierLen = 0, ArgumentLen = 0;
660
661 // Check to see if we have a modifier. If so eat it.
662 if (!isdigit(DiagStr[0])) {
663 Modifier = DiagStr;
664 while (DiagStr[0] == '-' ||
665 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
666 ++DiagStr;
667 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000668
Chris Lattner2b786902008-11-21 07:50:02 +0000669 // If we have an argument, get it next.
670 if (DiagStr[0] == '{') {
671 ++DiagStr; // Skip {.
672 Argument = DiagStr;
673
674 for (; DiagStr[0] != '}'; ++DiagStr)
675 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
676 ArgumentLen = DiagStr-Argument;
677 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000678 }
Chris Lattner2b786902008-11-21 07:50:02 +0000679 }
680
681 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000682 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000683
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000684 switch (getArgKind(ArgNo)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000685 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000686 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000687 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000688 assert(ModifierLen == 0 && "No modifiers for strings yet");
689 OutStr.append(S.begin(), S.end());
690 break;
691 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000692 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000693 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000694 assert(ModifierLen == 0 && "No modifiers for strings yet");
695 OutStr.append(S, S + strlen(S));
696 break;
697 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000698 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000699 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000700 int Val = getArgSInt(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000701
702 if (ModifierIs(Modifier, ModifierLen, "select")) {
703 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
704 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
705 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000706 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
707 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000708 } else {
709 assert(ModifierLen == 0 && "Unknown integer modifier");
Chris Lattner91aea712008-11-19 07:22:31 +0000710 // FIXME: Optimize
Chris Lattner2b786902008-11-21 07:50:02 +0000711 std::string S = llvm::itostr(Val);
Chris Lattner91aea712008-11-19 07:22:31 +0000712 OutStr.append(S.begin(), S.end());
Chris Lattner91aea712008-11-19 07:22:31 +0000713 }
Chris Lattner2b786902008-11-21 07:50:02 +0000714 break;
715 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000716 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000717 unsigned Val = getArgUInt(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000718
719 if (ModifierIs(Modifier, ModifierLen, "select")) {
720 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
721 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
722 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000723 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
724 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000725 } else {
726 assert(ModifierLen == 0 && "Unknown integer modifier");
727
Chris Lattner91aea712008-11-19 07:22:31 +0000728 // FIXME: Optimize
Chris Lattner2b786902008-11-21 07:50:02 +0000729 std::string S = llvm::utostr_32(Val);
Chris Lattner91aea712008-11-19 07:22:31 +0000730 OutStr.append(S.begin(), S.end());
Chris Lattner91aea712008-11-19 07:22:31 +0000731 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000732 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000733 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000734 // ---- NAMES and TYPES ----
735 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000736 const IdentifierInfo *II = getArgIdentifier(ArgNo);
737 assert(ModifierLen == 0 && "No modifiers for strings yet");
Chris Lattner810d3302009-02-19 23:45:49 +0000738 OutStr.push_back('\'');
Chris Lattnere3d20d92008-11-23 21:45:46 +0000739 OutStr.append(II->getName(), II->getName() + II->getLength());
740 OutStr.push_back('\'');
741 break;
742 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000743 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000744 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000745 case Diagnostic::ak_nameddecl:
Chris Lattner63ecc502008-11-23 09:21:17 +0000746 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
747 Modifier, ModifierLen,
748 Argument, ArgumentLen, OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000749 break;
Nico Weber4c311642008-08-10 19:59:06 +0000750 }
751 }
Nico Weber4c311642008-08-10 19:59:06 +0000752}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000753
754/// IncludeInDiagnosticCounts - This method (whose default implementation
755/// returns true) indicates whether the diagnostics handled by this
756/// DiagnosticClient should be included in the number of diagnostics
757/// reported by Diagnostic.
758bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }