blob: 27041354f82d25c06bac6d21183e0df6e7db0f89 [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
Chris Lattner27ceb9d2009-04-15 07:01:18 +000045static const DefaultMappingInfo DefaultMappings[] = {
Chris Lattner4ac072a2009-04-15 16:44:12 +000046#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) { diag::ENUM, DEFAULT_MAPPING },
Chris Lattner27ceb9d2009-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 Lattner8a941e02009-04-15 16:56:26 +000057#undef DIAG
Chris Lattner27ceb9d2009-04-15 07:01:18 +000058
Chris Lattner27ceb9d2009-04-15 07:01:18 +000059// Diagnostic classes.
Reid Spencer5f016e22007-07-11 17:01:13 +000060enum {
Chris Lattner8a941e02009-04-15 16:56:26 +000061 CLASS_NOTE = 0x01,
62 CLASS_WARNING = 0x02,
63 CLASS_EXTENSION = 0x03,
64 CLASS_ERROR = 0x04
Reid Spencer5f016e22007-07-11 17:01:13 +000065};
66
Chris Lattner27ceb9d2009-04-15 07:01:18 +000067/// DiagnosticClasses - The class for each diagnostic.
Chris Lattner4ac072a2009-04-15 16:44:12 +000068#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) CLASS,
Chris Lattner27ceb9d2009-04-15 07:01:18 +000069static unsigned char DiagnosticClassesCommon[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000070#include "clang/Basic/DiagnosticCommonKinds.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +000071 0
72};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000073static unsigned char DiagnosticClassesDriver[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000074#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000075 0
76};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000077static unsigned char DiagnosticClassesFrontend[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000078#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar50f4f462009-03-12 10:14:16 +000079 0
80};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000081static unsigned char DiagnosticClassesLex[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000082#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +000083 0
84};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000085static unsigned char DiagnosticClassesParse[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000086#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +000087 0
88};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000089static unsigned char DiagnosticClassesAST[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000090#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +000091 0
92};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000093static unsigned char DiagnosticClassesSema[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000094#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +000095 0
96};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000097static unsigned char DiagnosticClassesAnalysis[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +000098#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +000099 0
100};
101#undef DIAG
Reid Spencer5f016e22007-07-11 17:01:13 +0000102
103/// getDiagClass - Return the class field of the diagnostic.
104///
Chris Lattner07506182007-11-30 22:53:43 +0000105static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000106 assert(DiagID < diag::DIAG_UPPER_LIMIT &&
Chris Lattner07506182007-11-30 22:53:43 +0000107 "Diagnostic ID out of range!");
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000108 unsigned res;
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000109 if (DiagID < diag::DIAG_START_DRIVER)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000110 res = DiagnosticClassesCommon[DiagID];
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000111 else if (DiagID < diag::DIAG_START_FRONTEND)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000112 res = DiagnosticClassesDriver[DiagID - diag::DIAG_START_DRIVER - 1];
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000113 else if (DiagID < diag::DIAG_START_LEX)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000114 res = DiagnosticClassesFrontend[DiagID - diag::DIAG_START_FRONTEND - 1];
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000115 else if (DiagID < diag::DIAG_START_PARSE)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000116 res = DiagnosticClassesLex[DiagID - diag::DIAG_START_LEX - 1];
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000117 else if (DiagID < diag::DIAG_START_AST)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000118 res = DiagnosticClassesParse[DiagID - diag::DIAG_START_PARSE - 1];
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000119 else if (DiagID < diag::DIAG_START_SEMA)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000120 res = DiagnosticClassesAST[DiagID - diag::DIAG_START_AST - 1];
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000121 else if (DiagID < diag::DIAG_START_ANALYSIS)
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000122 res = DiagnosticClassesSema[DiagID - diag::DIAG_START_SEMA - 1];
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000123 else
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000124 res = DiagnosticClassesAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
125 return res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000126}
127
128/// DiagnosticText - An english message to print for the diagnostic. These
129/// should be localized.
Chris Lattner4ac072a2009-04-15 16:44:12 +0000130#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) DESC,
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000131static const char * const DiagnosticTextCommon[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000132#include "clang/Basic/DiagnosticCommonKinds.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 0
134};
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000135static const char * const DiagnosticTextDriver[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000136#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000137 0
138};
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000139static const char * const DiagnosticTextFrontend[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000140#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000141 0
142};
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000143static const char * const DiagnosticTextLex[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000144#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000145 0
146};
147static const char * const DiagnosticTextParse[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000148#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000149 0
150};
151static const char * const DiagnosticTextAST[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000152#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000153 0
154};
155static const char * const DiagnosticTextSema[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000156#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000157 0
158};
159static const char * const DiagnosticTextAnalysis[] = {
Sebastian Redl4d7a0892009-03-19 23:18:26 +0000160#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000161 0
162};
163#undef DIAG
Reid Spencer5f016e22007-07-11 17:01:13 +0000164
Chris Lattner182745a2007-12-02 01:09:57 +0000165//===----------------------------------------------------------------------===//
166// Custom Diagnostic information
167//===----------------------------------------------------------------------===//
168
169namespace clang {
170 namespace diag {
171 class CustomDiagInfo {
172 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
173 std::vector<DiagDesc> DiagInfo;
174 std::map<DiagDesc, unsigned> DiagIDs;
175 public:
176
177 /// getDescription - Return the description of the specified custom
178 /// diagnostic.
179 const char *getDescription(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000180 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000181 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000182 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattner182745a2007-12-02 01:09:57 +0000183 }
184
185 /// getLevel - Return the level of the specified custom diagnostic.
186 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000187 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000188 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000189 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattner182745a2007-12-02 01:09:57 +0000190 }
191
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000192 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
193 Diagnostic &Diags) {
Chris Lattner182745a2007-12-02 01:09:57 +0000194 DiagDesc D(L, Message);
195 // Check to see if it already exists.
196 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
197 if (I != DiagIDs.end() && I->first == D)
198 return I->second;
199
200 // If not, assign a new ID.
Chris Lattner88eccaf2009-01-29 06:55:46 +0000201 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattner182745a2007-12-02 01:09:57 +0000202 DiagIDs.insert(std::make_pair(D, ID));
203 DiagInfo.push_back(D);
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000204
205 // If this is a warning, and all warnings are supposed to map to errors,
206 // insert the mapping now.
207 if (L == Diagnostic::Warning && Diags.getWarningsAsErrors())
208 Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR);
Chris Lattner182745a2007-12-02 01:09:57 +0000209 return ID;
210 }
211 };
212
213 } // end diag namespace
214} // end clang namespace
215
216
217//===----------------------------------------------------------------------===//
218// Common Diagnostic implementation
219//===----------------------------------------------------------------------===//
220
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000221static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
222 const char *Modifier, unsigned ML,
223 const char *Argument, unsigned ArgLen,
Chris Lattner92dd3862009-02-19 23:53:20 +0000224 llvm::SmallVectorImpl<char> &Output,
225 void *Cookie) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000226 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +0000227 Output.append(Str, Str+strlen(Str));
228}
229
230
Ted Kremenekb4398aa2008-08-07 17:49:57 +0000231Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000232 AllExtensionsSilenced = 0;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000233 IgnoreAllWarnings = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 WarningsAsErrors = false;
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000235 SuppressSystemWarnings = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000236
237 ErrorOccurred = false;
Chris Lattner15221422009-02-06 04:16:02 +0000238 FatalErrorOccurred = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 NumDiagnostics = 0;
240 NumErrors = 0;
Chris Lattner182745a2007-12-02 01:09:57 +0000241 CustomDiagInfo = 0;
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000242 CurDiagID = ~0U;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000243 LastDiagLevel = Ignored;
Chris Lattner22caddc2008-11-23 09:13:29 +0000244
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000245 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +0000246 ArgToStringCookie = 0;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000247
248 // Set all mappings to their default.
249 for (unsigned i = 0, e = sizeof(DefaultMappings)/sizeof(DefaultMappings[0]);
250 i != e; ++i)
251 setDiagnosticMappingInternal(DefaultMappings[i].DiagID,
252 DefaultMappings[i].Mapping);
Reid Spencer5f016e22007-07-11 17:01:13 +0000253}
254
Chris Lattner182745a2007-12-02 01:09:57 +0000255Diagnostic::~Diagnostic() {
256 delete CustomDiagInfo;
257}
258
259/// getCustomDiagID - Return an ID for a diagnostic with the specified message
260/// and level. If this is the first request for this diagnosic, it is
261/// registered and created, otherwise the existing ID is returned.
262unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
263 if (CustomDiagInfo == 0)
264 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000265 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattner182745a2007-12-02 01:09:57 +0000266}
267
268
Chris Lattnerf5d23282009-02-17 06:49:55 +0000269/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
270/// level of the specified diagnostic ID is a Warning or Extension.
271/// This only works on builtin diagnostics, not custom ones, and is not legal to
272/// call on NOTEs.
273bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000274 return DiagID < diag::DIAG_UPPER_LIMIT &&
275 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Reid Spencer5f016e22007-07-11 17:01:13 +0000276}
277
Douglas Gregoree1828a2009-03-10 18:03:33 +0000278/// \brief Determine whether the given built-in diagnostic ID is a
279/// Note.
280bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000281 return DiagID < diag::DIAG_UPPER_LIMIT &&
282 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000283}
284
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000285/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
286/// ID is for an extension of some sort.
287///
288bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000289 return DiagID < diag::DIAG_UPPER_LIMIT &&
290 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000291}
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293
294/// getDescription - Given a diagnostic ID, return a description of the
295/// issue.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000296const char *Diagnostic::getDescription(unsigned DiagID) const {
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000297 if (DiagID < diag::DIAG_START_DRIVER)
Chris Lattnerf5d23282009-02-17 06:49:55 +0000298 return DiagnosticTextCommon[DiagID];
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000299 else if (DiagID < diag::DIAG_START_FRONTEND)
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000300 return DiagnosticTextDriver[DiagID - diag::DIAG_START_DRIVER - 1];
Daniel Dunbar50f4f462009-03-12 10:14:16 +0000301 else if (DiagID < diag::DIAG_START_LEX)
302 return DiagnosticTextFrontend[DiagID - diag::DIAG_START_FRONTEND - 1];
Chris Lattnerf5d23282009-02-17 06:49:55 +0000303 else if (DiagID < diag::DIAG_START_PARSE)
304 return DiagnosticTextLex[DiagID - diag::DIAG_START_LEX - 1];
305 else if (DiagID < diag::DIAG_START_AST)
306 return DiagnosticTextParse[DiagID - diag::DIAG_START_PARSE - 1];
307 else if (DiagID < diag::DIAG_START_SEMA)
308 return DiagnosticTextAST[DiagID - diag::DIAG_START_AST - 1];
309 else if (DiagID < diag::DIAG_START_ANALYSIS)
310 return DiagnosticTextSema[DiagID - diag::DIAG_START_SEMA - 1];
311 else if (DiagID < diag::DIAG_UPPER_LIMIT)
312 return DiagnosticTextAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000313 return CustomDiagInfo->getDescription(DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000314}
315
316/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
317/// object, classify the specified diagnostic ID into a Level, consumable by
318/// the DiagnosticClient.
319Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattner182745a2007-12-02 01:09:57 +0000320 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000321 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattner182745a2007-12-02 01:09:57 +0000322 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner07506182007-11-30 22:53:43 +0000323
324 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000325 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerf5d23282009-02-17 06:49:55 +0000326 return getDiagnosticLevel(DiagID, DiagClass);
327}
328
329/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
330/// object, classify the specified diagnostic ID into a Level, consumable by
331/// the DiagnosticClient.
332Diagnostic::Level
333Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerf5d23282009-02-17 06:49:55 +0000335 // to error. Errors can only be mapped to fatal.
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000336 Diagnostic::Level Result = Diagnostic::Fatal;
Chris Lattnerf5d23282009-02-17 06:49:55 +0000337 switch (getDiagnosticMapping((diag::kind)DiagID)) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000338 case diag::MAP_IGNORE:
339 return Diagnostic::Ignored;
340 case diag::MAP_ERROR:
341 Result = Diagnostic::Error;
342 break;
343 case diag::MAP_FATAL:
344 Result = Diagnostic::Fatal;
345 break;
346 case diag::MAP_WARNING:
347 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner5b4681c2008-05-29 15:36:45 +0000348 if (IgnoreAllWarnings)
349 return Diagnostic::Ignored;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000350 Result = WarningsAsErrors ? Diagnostic::Error : Diagnostic::Warning;
351 break;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000352 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000353
354 // Okay, we're about to return this as a "diagnostic to emit" one last check:
355 // if this is any sort of extension warning, and if we're in an __extension__
356 // block, silence it.
357 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
358 return Diagnostic::Ignored;
Reid Spencer5f016e22007-07-11 17:01:13 +0000359
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000360 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000361}
362
Chris Lattner0a14eee2008-11-18 07:04:44 +0000363/// ProcessDiag - This is the method used to report a diagnostic that is
364/// finally fully formed.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000365void Diagnostic::ProcessDiag() {
366 DiagnosticInfo Info(this);
Douglas Gregor525c4b02009-03-19 18:55:06 +0000367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 // Figure out the diagnostic level of this message.
Chris Lattnerf5d23282009-02-17 06:49:55 +0000369 Diagnostic::Level DiagLevel;
370 unsigned DiagID = Info.getID();
Reid Spencer5f016e22007-07-11 17:01:13 +0000371
Chris Lattnerf5d23282009-02-17 06:49:55 +0000372 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
373 // in a system header.
374 bool ShouldEmitInSystemHeader;
375
376 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
377 // Handle custom diagnostics, which cannot be mapped.
378 DiagLevel = CustomDiagInfo->getLevel(DiagID);
379
380 // Custom diagnostics always are emitted in system headers.
381 ShouldEmitInSystemHeader = true;
382 } else {
383 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
384 // the diagnostic level was for the previous diagnostic so that it is
385 // filtered the same as the previous diagnostic.
386 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000387 if (DiagClass == CLASS_NOTE) {
Chris Lattnerf5d23282009-02-17 06:49:55 +0000388 DiagLevel = Diagnostic::Note;
389 ShouldEmitInSystemHeader = false; // extra consideration is needed
390 } else {
391 // If this is not an error and we are in a system header, we ignore it.
392 // Check the original Diag ID here, because we also want to ignore
393 // extensions and warnings in -Werror and -pedantic-errors modes, which
394 // *map* warnings/extensions to errors.
Chris Lattner8a941e02009-04-15 16:56:26 +0000395 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Chris Lattnerf5d23282009-02-17 06:49:55 +0000396
397 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
398 }
399 }
400
Douglas Gregor525c4b02009-03-19 18:55:06 +0000401 if (DiagLevel != Diagnostic::Note) {
402 // Record that a fatal error occurred only when we see a second
403 // non-note diagnostic. This allows notes to be attached to the
404 // fatal error, but suppresses any diagnostics that follow those
405 // notes.
406 if (LastDiagLevel == Diagnostic::Fatal)
407 FatalErrorOccurred = true;
408
Chris Lattnerf5d23282009-02-17 06:49:55 +0000409 LastDiagLevel = DiagLevel;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000410 }
411
412 // If a fatal error has already been emitted, silence all subsequent
413 // diagnostics.
414 if (FatalErrorOccurred)
415 return;
416
Chris Lattnerf5d23282009-02-17 06:49:55 +0000417 // If the client doesn't care about this message, don't issue it. If this is
418 // a note and the last real diagnostic was ignored, ignore it too.
419 if (DiagLevel == Diagnostic::Ignored ||
420 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 return;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000422
Chris Lattnerf5d23282009-02-17 06:49:55 +0000423 // If this diagnostic is in a system header and is not a clang error, suppress
424 // it.
425 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner0a14eee2008-11-18 07:04:44 +0000426 Info.getLocation().isValid() &&
Chris Lattnerf5d23282009-02-17 06:49:55 +0000427 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner336f26b2009-02-17 06:52:20 +0000428 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
429 LastDiagLevel = Diagnostic::Ignored;
Chris Lattner7097d912008-02-03 09:00:04 +0000430 return;
Chris Lattner336f26b2009-02-17 06:52:20 +0000431 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000432
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 if (DiagLevel >= Diagnostic::Error) {
434 ErrorOccurred = true;
Chris Lattner0a14eee2008-11-18 07:04:44 +0000435 ++NumErrors;
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000437
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 // Finally, report it.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000439 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekcabe6682009-01-23 20:28:53 +0000440 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000441
Douglas Gregoree1828a2009-03-10 18:03:33 +0000442 CurDiagID = ~0U;
Reid Spencer5f016e22007-07-11 17:01:13 +0000443}
444
Nico Weber7bfaaae2008-08-10 19:59:06 +0000445
Reid Spencer5f016e22007-07-11 17:01:13 +0000446DiagnosticClient::~DiagnosticClient() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000447
Chris Lattnerf4c83962008-11-19 06:51:40 +0000448
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000449/// ModifierIs - Return true if the specified modifier matches specified string.
450template <std::size_t StrLen>
451static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
452 const char (&Str)[StrLen]) {
453 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
454}
455
456/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
457/// like this: %select{foo|bar|baz}2. This means that the integer argument
458/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
459/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
460/// This is very useful for certain classes of variant diagnostics.
461static void HandleSelectModifier(unsigned ValNo,
462 const char *Argument, unsigned ArgumentLen,
463 llvm::SmallVectorImpl<char> &OutStr) {
464 const char *ArgumentEnd = Argument+ArgumentLen;
465
466 // Skip over 'ValNo' |'s.
467 while (ValNo) {
468 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
469 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
470 " larger than the number of options in the diagnostic string!");
471 Argument = NextVal+1; // Skip this string.
472 --ValNo;
473 }
474
475 // Get the end of the value. This is either the } or the |.
476 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
477 // Add the value to the output string.
478 OutStr.append(Argument, EndPtr);
479}
480
481/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
482/// letter 's' to the string if the value is not 1. This is used in cases like
483/// this: "you idiot, you have %4 parameter%s4!".
484static void HandleIntegerSModifier(unsigned ValNo,
485 llvm::SmallVectorImpl<char> &OutStr) {
486 if (ValNo != 1)
487 OutStr.push_back('s');
488}
489
490
Sebastian Redle4c452c2008-11-22 13:44:36 +0000491/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000492static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000493 // Programming 101: Parse a decimal number :-)
494 unsigned Val = 0;
495 while (Start != End && *Start >= '0' && *Start <= '9') {
496 Val *= 10;
497 Val += *Start - '0';
498 ++Start;
499 }
500 return Val;
501}
502
503/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000504static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000505 if (*Start != '[') {
506 unsigned Ref = PluralNumber(Start, End);
507 return Ref == Val;
508 }
509
510 ++Start;
511 unsigned Low = PluralNumber(Start, End);
512 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
513 ++Start;
514 unsigned High = PluralNumber(Start, End);
515 assert(*Start == ']' && "Bad plural expression syntax: expected )");
516 ++Start;
517 return Low <= Val && Val <= High;
518}
519
520/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000521static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000522 // Empty condition?
523 if (*Start == ':')
524 return true;
525
526 while (1) {
527 char C = *Start;
528 if (C == '%') {
529 // Modulo expression
530 ++Start;
531 unsigned Arg = PluralNumber(Start, End);
532 assert(*Start == '=' && "Bad plural expression syntax: expected =");
533 ++Start;
534 unsigned ValMod = ValNo % Arg;
535 if (TestPluralRange(ValMod, Start, End))
536 return true;
537 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000538 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000539 "Bad plural expression syntax: unexpected character");
540 // Range expression
541 if (TestPluralRange(ValNo, Start, End))
542 return true;
543 }
544
545 // Scan for next or-expr part.
546 Start = std::find(Start, End, ',');
547 if(Start == End)
548 break;
549 ++Start;
550 }
551 return false;
552}
553
554/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
555/// for complex plural forms, or in languages where all plurals are complex.
556/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
557/// conditions that are tested in order, the form corresponding to the first
558/// that applies being emitted. The empty condition is always true, making the
559/// last form a default case.
560/// Conditions are simple boolean expressions, where n is the number argument.
561/// Here are the rules.
562/// condition := expression | empty
563/// empty := -> always true
564/// expression := numeric [',' expression] -> logical or
565/// numeric := range -> true if n in range
566/// | '%' number '=' range -> true if n % number in range
567/// range := number
568/// | '[' number ',' number ']' -> ranges are inclusive both ends
569///
570/// Here are some examples from the GNU gettext manual written in this form:
571/// English:
572/// {1:form0|:form1}
573/// Latvian:
574/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
575/// Gaeilge:
576/// {1:form0|2:form1|:form2}
577/// Romanian:
578/// {1:form0|0,%100=[1,19]:form1|:form2}
579/// Lithuanian:
580/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
581/// Russian (requires repeated form):
582/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
583/// Slovak
584/// {1:form0|[2,4]:form1|:form2}
585/// Polish (requires repeated form):
586/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
587static void HandlePluralModifier(unsigned ValNo,
588 const char *Argument, unsigned ArgumentLen,
589 llvm::SmallVectorImpl<char> &OutStr)
590{
591 const char *ArgumentEnd = Argument + ArgumentLen;
592 while (1) {
593 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
594 const char *ExprEnd = Argument;
595 while (*ExprEnd != ':') {
596 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
597 ++ExprEnd;
598 }
599 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
600 Argument = ExprEnd + 1;
601 ExprEnd = std::find(Argument, ArgumentEnd, '|');
602 OutStr.append(Argument, ExprEnd);
603 return;
604 }
605 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
606 }
607}
608
609
Chris Lattnerf4c83962008-11-19 06:51:40 +0000610/// FormatDiagnostic - Format this diagnostic into a string, substituting the
611/// formal arguments into the %0 slots. The result is appended onto the Str
612/// array.
613void DiagnosticInfo::
614FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
615 const char *DiagStr = getDiags()->getDescription(getID());
616 const char *DiagEnd = DiagStr+strlen(DiagStr);
Nico Weber7bfaaae2008-08-10 19:59:06 +0000617
Chris Lattnerf4c83962008-11-19 06:51:40 +0000618 while (DiagStr != DiagEnd) {
619 if (DiagStr[0] != '%') {
620 // Append non-%0 substrings to Str if we have one.
621 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
622 OutStr.append(DiagStr, StrEnd);
623 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000624 continue;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000625 } else if (DiagStr[1] == '%') {
626 OutStr.push_back('%'); // %% -> %.
627 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000628 continue;
629 }
630
631 // Skip the %.
632 ++DiagStr;
633
634 // This must be a placeholder for a diagnostic argument. The format for a
635 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
636 // The digit is a number from 0-9 indicating which argument this comes from.
637 // The modifier is a string of digits from the set [-a-z]+, arguments is a
638 // brace enclosed string.
639 const char *Modifier = 0, *Argument = 0;
640 unsigned ModifierLen = 0, ArgumentLen = 0;
641
642 // Check to see if we have a modifier. If so eat it.
643 if (!isdigit(DiagStr[0])) {
644 Modifier = DiagStr;
645 while (DiagStr[0] == '-' ||
646 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
647 ++DiagStr;
648 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000649
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000650 // If we have an argument, get it next.
651 if (DiagStr[0] == '{') {
652 ++DiagStr; // Skip {.
653 Argument = DiagStr;
654
655 for (; DiagStr[0] != '}'; ++DiagStr)
656 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
657 ArgumentLen = DiagStr-Argument;
658 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000659 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000660 }
661
662 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000663 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000664
Chris Lattner22caddc2008-11-23 09:13:29 +0000665 switch (getArgKind(ArgNo)) {
Chris Lattner08631c52008-11-23 21:45:46 +0000666 // ---- STRINGS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000667 case Diagnostic::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000668 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000669 assert(ModifierLen == 0 && "No modifiers for strings yet");
670 OutStr.append(S.begin(), S.end());
671 break;
672 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000673 case Diagnostic::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000674 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000675 assert(ModifierLen == 0 && "No modifiers for strings yet");
676 OutStr.append(S, S + strlen(S));
677 break;
678 }
Chris Lattner08631c52008-11-23 21:45:46 +0000679 // ---- INTEGERS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000680 case Diagnostic::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000681 int Val = getArgSInt(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000682
683 if (ModifierIs(Modifier, ModifierLen, "select")) {
684 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
685 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
686 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000687 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
688 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000689 } else {
690 assert(ModifierLen == 0 && "Unknown integer modifier");
Chris Lattner30bc9652008-11-19 07:22:31 +0000691 // FIXME: Optimize
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000692 std::string S = llvm::itostr(Val);
Chris Lattner30bc9652008-11-19 07:22:31 +0000693 OutStr.append(S.begin(), S.end());
Chris Lattner30bc9652008-11-19 07:22:31 +0000694 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000695 break;
696 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000697 case Diagnostic::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000698 unsigned Val = getArgUInt(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000699
700 if (ModifierIs(Modifier, ModifierLen, "select")) {
701 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
702 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
703 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000704 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
705 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000706 } else {
707 assert(ModifierLen == 0 && "Unknown integer modifier");
708
Chris Lattner30bc9652008-11-19 07:22:31 +0000709 // FIXME: Optimize
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000710 std::string S = llvm::utostr_32(Val);
Chris Lattner30bc9652008-11-19 07:22:31 +0000711 OutStr.append(S.begin(), S.end());
Chris Lattner30bc9652008-11-19 07:22:31 +0000712 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000713 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000714 }
Chris Lattner08631c52008-11-23 21:45:46 +0000715 // ---- NAMES and TYPES ----
716 case Diagnostic::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000717 const IdentifierInfo *II = getArgIdentifier(ArgNo);
718 assert(ModifierLen == 0 && "No modifiers for strings yet");
Chris Lattnerd0344a42009-02-19 23:45:49 +0000719 OutStr.push_back('\'');
Chris Lattner08631c52008-11-23 21:45:46 +0000720 OutStr.append(II->getName(), II->getName() + II->getLength());
721 OutStr.push_back('\'');
722 break;
723 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000724 case Diagnostic::ak_qualtype:
Chris Lattner011bb4e2008-11-23 20:28:15 +0000725 case Diagnostic::ak_declarationname:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000726 case Diagnostic::ak_nameddecl:
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000727 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
728 Modifier, ModifierLen,
729 Argument, ArgumentLen, OutStr);
Chris Lattner22caddc2008-11-23 09:13:29 +0000730 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000731 }
732 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000733}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000734
735/// IncludeInDiagnosticCounts - This method (whose default implementation
736/// returns true) indicates whether the diagnostics handled by this
737/// DiagnosticClient should be included in the number of diagnostics
738/// reported by Diagnostic.
739bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }