blob: bc0324e8d33409bccda71fb504a69f505291435a [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};
57
Chris Lattnere007de32009-04-15 07:01:18 +000058// Diagnostic classes.
Chris Lattner22eb9722006-06-18 05:43:12 +000059enum {
Chris Lattner22eb9722006-06-18 05:43:12 +000060 NOTE = 0x01,
61 WARNING = 0x02,
62 EXTENSION = 0x03,
Daniel Dunbar81f7f292008-08-05 00:07:51 +000063 EXTWARN = 0x04,
64 ERROR = 0x05,
Chris Lattnere007de32009-04-15 07:01:18 +000065 FATAL = 0x06
Chris Lattner22eb9722006-06-18 05:43:12 +000066};
67
Chris Lattnere007de32009-04-15 07:01:18 +000068/// DiagnosticClasses - The class for each diagnostic.
Chris Lattner1b595622009-04-15 16:44:12 +000069#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) CLASS,
Chris Lattnere007de32009-04-15 07:01:18 +000070static unsigned char DiagnosticClassesCommon[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000071#include "clang/Basic/DiagnosticCommonKinds.inc"
Chris Lattner22eb9722006-06-18 05:43:12 +000072 0
73};
Chris Lattnere007de32009-04-15 07:01:18 +000074static unsigned char DiagnosticClassesDriver[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000075#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000076 0
77};
Chris Lattnere007de32009-04-15 07:01:18 +000078static unsigned char DiagnosticClassesFrontend[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000079#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar4f495982009-03-12 10:14:16 +000080 0
81};
Chris Lattnere007de32009-04-15 07:01:18 +000082static unsigned char DiagnosticClassesLex[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000083#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000084 0
85};
Chris Lattnere007de32009-04-15 07:01:18 +000086static unsigned char DiagnosticClassesParse[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000087#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000088 0
89};
Chris Lattnere007de32009-04-15 07:01:18 +000090static unsigned char DiagnosticClassesAST[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000091#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000092 0
93};
Chris Lattnere007de32009-04-15 07:01:18 +000094static unsigned char DiagnosticClassesSema[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000095#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +000096 0
97};
Chris Lattnere007de32009-04-15 07:01:18 +000098static unsigned char DiagnosticClassesAnalysis[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000099#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000100 0
101};
102#undef DIAG
Chris Lattner22eb9722006-06-18 05:43:12 +0000103
104/// getDiagClass - Return the class field of the diagnostic.
105///
Chris Lattner4431a1b2007-11-30 22:53:43 +0000106static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner4b6713e2009-01-29 17:46:13 +0000107 assert(DiagID < diag::DIAG_UPPER_LIMIT &&
Chris Lattner4431a1b2007-11-30 22:53:43 +0000108 "Diagnostic ID out of range!");
Chris Lattner7368d582009-01-27 18:30:58 +0000109 unsigned res;
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000110 if (DiagID < diag::DIAG_START_DRIVER)
Chris Lattnere007de32009-04-15 07:01:18 +0000111 res = DiagnosticClassesCommon[DiagID];
Daniel Dunbar4f495982009-03-12 10:14:16 +0000112 else if (DiagID < diag::DIAG_START_FRONTEND)
Chris Lattnere007de32009-04-15 07:01:18 +0000113 res = DiagnosticClassesDriver[DiagID - diag::DIAG_START_DRIVER - 1];
Daniel Dunbar4f495982009-03-12 10:14:16 +0000114 else if (DiagID < diag::DIAG_START_LEX)
Chris Lattnere007de32009-04-15 07:01:18 +0000115 res = DiagnosticClassesFrontend[DiagID - diag::DIAG_START_FRONTEND - 1];
Chris Lattner4b6713e2009-01-29 17:46:13 +0000116 else if (DiagID < diag::DIAG_START_PARSE)
Chris Lattnere007de32009-04-15 07:01:18 +0000117 res = DiagnosticClassesLex[DiagID - diag::DIAG_START_LEX - 1];
Chris Lattner4b6713e2009-01-29 17:46:13 +0000118 else if (DiagID < diag::DIAG_START_AST)
Chris Lattnere007de32009-04-15 07:01:18 +0000119 res = DiagnosticClassesParse[DiagID - diag::DIAG_START_PARSE - 1];
Chris Lattner4b6713e2009-01-29 17:46:13 +0000120 else if (DiagID < diag::DIAG_START_SEMA)
Chris Lattnere007de32009-04-15 07:01:18 +0000121 res = DiagnosticClassesAST[DiagID - diag::DIAG_START_AST - 1];
Chris Lattner4b6713e2009-01-29 17:46:13 +0000122 else if (DiagID < diag::DIAG_START_ANALYSIS)
Chris Lattnere007de32009-04-15 07:01:18 +0000123 res = DiagnosticClassesSema[DiagID - diag::DIAG_START_SEMA - 1];
Chris Lattner7368d582009-01-27 18:30:58 +0000124 else
Chris Lattnere007de32009-04-15 07:01:18 +0000125 res = DiagnosticClassesAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
126 return res;
Chris Lattner22eb9722006-06-18 05:43:12 +0000127}
128
129/// DiagnosticText - An english message to print for the diagnostic. These
130/// should be localized.
Chris Lattner1b595622009-04-15 16:44:12 +0000131#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) DESC,
Chris Lattner7368d582009-01-27 18:30:58 +0000132static const char * const DiagnosticTextCommon[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000133#include "clang/Basic/DiagnosticCommonKinds.inc"
Chris Lattner22eb9722006-06-18 05:43:12 +0000134 0
135};
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000136static const char * const DiagnosticTextDriver[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000137#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000138 0
139};
Daniel Dunbar4f495982009-03-12 10:14:16 +0000140static const char * const DiagnosticTextFrontend[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000141#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar4f495982009-03-12 10:14:16 +0000142 0
143};
Chris Lattner7368d582009-01-27 18:30:58 +0000144static const char * const DiagnosticTextLex[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000145#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000146 0
147};
148static const char * const DiagnosticTextParse[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000149#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000150 0
151};
152static const char * const DiagnosticTextAST[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000153#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000154 0
155};
156static const char * const DiagnosticTextSema[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000157#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000158 0
159};
160static const char * const DiagnosticTextAnalysis[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000161#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000162 0
163};
164#undef DIAG
Chris Lattner22eb9722006-06-18 05:43:12 +0000165
Chris Lattnere6535cf2007-12-02 01:09:57 +0000166//===----------------------------------------------------------------------===//
167// Custom Diagnostic information
168//===----------------------------------------------------------------------===//
169
170namespace clang {
171 namespace diag {
172 class CustomDiagInfo {
173 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
174 std::vector<DiagDesc> DiagInfo;
175 std::map<DiagDesc, unsigned> DiagIDs;
176 public:
177
178 /// getDescription - Return the description of the specified custom
179 /// diagnostic.
180 const char *getDescription(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000181 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000182 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000183 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattnere6535cf2007-12-02 01:09:57 +0000184 }
185
186 /// getLevel - Return the level of the specified custom diagnostic.
187 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000188 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000189 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000190 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000191 }
192
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000193 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
194 Diagnostic &Diags) {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000195 DiagDesc D(L, Message);
196 // Check to see if it already exists.
197 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
198 if (I != DiagIDs.end() && I->first == D)
199 return I->second;
200
201 // If not, assign a new ID.
Chris Lattner36790cf2009-01-29 06:55:46 +0000202 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000203 DiagIDs.insert(std::make_pair(D, ID));
204 DiagInfo.push_back(D);
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000205
206 // If this is a warning, and all warnings are supposed to map to errors,
207 // insert the mapping now.
208 if (L == Diagnostic::Warning && Diags.getWarningsAsErrors())
209 Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000210 return ID;
211 }
212 };
213
214 } // end diag namespace
215} // end clang namespace
216
217
218//===----------------------------------------------------------------------===//
219// Common Diagnostic implementation
220//===----------------------------------------------------------------------===//
221
Chris Lattner63ecc502008-11-23 09:21:17 +0000222static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
223 const char *Modifier, unsigned ML,
224 const char *Argument, unsigned ArgLen,
Chris Lattnercf868c42009-02-19 23:53:20 +0000225 llvm::SmallVectorImpl<char> &Output,
226 void *Cookie) {
Chris Lattner63ecc502008-11-23 09:21:17 +0000227 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000228 Output.append(Str, Str+strlen(Str));
229}
230
231
Ted Kremenek31691ae2008-08-07 17:49:57 +0000232Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattnere007de32009-04-15 07:01:18 +0000233 AllExtensionsSilenced = 0;
Chris Lattner8c800702008-05-29 15:36:45 +0000234 IgnoreAllWarnings = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000235 WarningsAsErrors = false;
Daniel Dunbar84b70f72008-09-12 18:10:20 +0000236 SuppressSystemWarnings = false;
Chris Lattnerc49b9052007-05-28 00:46:44 +0000237
238 ErrorOccurred = false;
Chris Lattner9e031192009-02-06 04:16:02 +0000239 FatalErrorOccurred = false;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000240 NumDiagnostics = 0;
241 NumErrors = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000242 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000243 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000244 LastDiagLevel = Ignored;
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000245
Chris Lattner63ecc502008-11-23 09:21:17 +0000246 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000247 ArgToStringCookie = 0;
Chris Lattnere007de32009-04-15 07:01:18 +0000248
249 // Set all mappings to their default.
250 for (unsigned i = 0, e = sizeof(DefaultMappings)/sizeof(DefaultMappings[0]);
251 i != e; ++i)
252 setDiagnosticMappingInternal(DefaultMappings[i].DiagID,
253 DefaultMappings[i].Mapping);
Chris Lattnerae411572006-07-05 00:55:08 +0000254}
255
Chris Lattnere6535cf2007-12-02 01:09:57 +0000256Diagnostic::~Diagnostic() {
257 delete CustomDiagInfo;
258}
259
260/// getCustomDiagID - Return an ID for a diagnostic with the specified message
261/// and level. If this is the first request for this diagnosic, it is
262/// registered and created, otherwise the existing ID is returned.
263unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
264 if (CustomDiagInfo == 0)
265 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000266 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000267}
268
269
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000270/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
271/// level of the specified diagnostic ID is a Warning or Extension.
272/// This only works on builtin diagnostics, not custom ones, and is not legal to
273/// call on NOTEs.
274bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattner4b6713e2009-01-29 17:46:13 +0000275 return DiagID < diag::DIAG_UPPER_LIMIT && getBuiltinDiagClass(DiagID) < ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000276}
277
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000278/// \brief Determine whether the given built-in diagnostic ID is a
279/// Note.
280bool Diagnostic::isBuiltinNote(unsigned DiagID) {
281 return DiagID < diag::DIAG_UPPER_LIMIT && getBuiltinDiagClass(DiagID) == NOTE;
282}
283
Chris Lattnere007de32009-04-15 07:01:18 +0000284/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
285/// ID is for an extension of some sort.
286///
287bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
288 if (DiagID < diag::DIAG_UPPER_LIMIT) {
289 unsigned Class = getBuiltinDiagClass(DiagID);
290 return Class == EXTENSION || Class == EXTWARN;
291 }
292 return false;
293}
294
Chris Lattner22eb9722006-06-18 05:43:12 +0000295
296/// getDescription - Given a diagnostic ID, return a description of the
297/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000298const char *Diagnostic::getDescription(unsigned DiagID) const {
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000299 if (DiagID < diag::DIAG_START_DRIVER)
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000300 return DiagnosticTextCommon[DiagID];
Daniel Dunbar4f495982009-03-12 10:14:16 +0000301 else if (DiagID < diag::DIAG_START_FRONTEND)
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000302 return DiagnosticTextDriver[DiagID - diag::DIAG_START_DRIVER - 1];
Daniel Dunbar4f495982009-03-12 10:14:16 +0000303 else if (DiagID < diag::DIAG_START_LEX)
304 return DiagnosticTextFrontend[DiagID - diag::DIAG_START_FRONTEND - 1];
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000305 else if (DiagID < diag::DIAG_START_PARSE)
306 return DiagnosticTextLex[DiagID - diag::DIAG_START_LEX - 1];
307 else if (DiagID < diag::DIAG_START_AST)
308 return DiagnosticTextParse[DiagID - diag::DIAG_START_PARSE - 1];
309 else if (DiagID < diag::DIAG_START_SEMA)
310 return DiagnosticTextAST[DiagID - diag::DIAG_START_AST - 1];
311 else if (DiagID < diag::DIAG_START_ANALYSIS)
312 return DiagnosticTextSema[DiagID - diag::DIAG_START_SEMA - 1];
313 else if (DiagID < diag::DIAG_UPPER_LIMIT)
314 return DiagnosticTextAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
Chris Lattner7368d582009-01-27 18:30:58 +0000315 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000316}
317
318/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
319/// object, classify the specified diagnostic ID into a Level, consumable by
320/// the DiagnosticClient.
321Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000322 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000323 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000324 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner4431a1b2007-11-30 22:53:43 +0000325
326 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000327 assert(DiagClass != NOTE && "Cannot get the diagnostic level of a note!");
328 return getDiagnosticLevel(DiagID, DiagClass);
329}
330
331/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
332/// object, classify the specified diagnostic ID into a Level, consumable by
333/// the DiagnosticClient.
334Diagnostic::Level
335Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattnerae411572006-07-05 00:55:08 +0000336 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000337 // to error. Errors can only be mapped to fatal.
Chris Lattnere007de32009-04-15 07:01:18 +0000338 Diagnostic::Level Result = Diagnostic::Fatal;
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000339 switch (getDiagnosticMapping((diag::kind)DiagID)) {
Chris Lattnere007de32009-04-15 07:01:18 +0000340 case diag::MAP_IGNORE:
341 return Diagnostic::Ignored;
342 case diag::MAP_ERROR:
343 Result = Diagnostic::Error;
344 break;
345 case diag::MAP_FATAL:
346 Result = Diagnostic::Fatal;
347 break;
348 case diag::MAP_WARNING:
349 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner8c800702008-05-29 15:36:45 +0000350 if (IgnoreAllWarnings)
351 return Diagnostic::Ignored;
Chris Lattnere007de32009-04-15 07:01:18 +0000352 Result = WarningsAsErrors ? Diagnostic::Error : Diagnostic::Warning;
353 break;
Chris Lattner8c800702008-05-29 15:36:45 +0000354 }
Chris Lattnere007de32009-04-15 07:01:18 +0000355
356 // Okay, we're about to return this as a "diagnostic to emit" one last check:
357 // if this is any sort of extension warning, and if we're in an __extension__
358 // block, silence it.
359 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
360 return Diagnostic::Ignored;
Chris Lattner22eb9722006-06-18 05:43:12 +0000361
Chris Lattnere007de32009-04-15 07:01:18 +0000362 return Result;
Chris Lattner22eb9722006-06-18 05:43:12 +0000363}
364
Chris Lattner8488c822008-11-18 07:04:44 +0000365/// ProcessDiag - This is the method used to report a diagnostic that is
366/// finally fully formed.
Chris Lattner427c9c12008-11-22 00:59:29 +0000367void Diagnostic::ProcessDiag() {
368 DiagnosticInfo Info(this);
Douglas Gregor19367f52009-03-19 18:55:06 +0000369
Chris Lattner22eb9722006-06-18 05:43:12 +0000370 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000371 Diagnostic::Level DiagLevel;
372 unsigned DiagID = Info.getID();
Chris Lattner22eb9722006-06-18 05:43:12 +0000373
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000374 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
375 // in a system header.
376 bool ShouldEmitInSystemHeader;
377
378 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
379 // Handle custom diagnostics, which cannot be mapped.
380 DiagLevel = CustomDiagInfo->getLevel(DiagID);
381
382 // Custom diagnostics always are emitted in system headers.
383 ShouldEmitInSystemHeader = true;
384 } else {
385 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
386 // the diagnostic level was for the previous diagnostic so that it is
387 // filtered the same as the previous diagnostic.
388 unsigned DiagClass = getBuiltinDiagClass(DiagID);
389 if (DiagClass == NOTE) {
390 DiagLevel = Diagnostic::Note;
391 ShouldEmitInSystemHeader = false; // extra consideration is needed
392 } else {
393 // If this is not an error and we are in a system header, we ignore it.
394 // Check the original Diag ID here, because we also want to ignore
395 // extensions and warnings in -Werror and -pedantic-errors modes, which
396 // *map* warnings/extensions to errors.
397 ShouldEmitInSystemHeader = DiagClass == ERROR;
398
399 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
400 }
401 }
402
Douglas Gregor19367f52009-03-19 18:55:06 +0000403 if (DiagLevel != Diagnostic::Note) {
404 // Record that a fatal error occurred only when we see a second
405 // non-note diagnostic. This allows notes to be attached to the
406 // fatal error, but suppresses any diagnostics that follow those
407 // notes.
408 if (LastDiagLevel == Diagnostic::Fatal)
409 FatalErrorOccurred = true;
410
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000411 LastDiagLevel = DiagLevel;
Douglas Gregor19367f52009-03-19 18:55:06 +0000412 }
413
414 // If a fatal error has already been emitted, silence all subsequent
415 // diagnostics.
416 if (FatalErrorOccurred)
417 return;
418
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000419 // If the client doesn't care about this message, don't issue it. If this is
420 // a note and the last real diagnostic was ignored, ignore it too.
421 if (DiagLevel == Diagnostic::Ignored ||
422 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Chris Lattnercb283342006-06-18 06:48:37 +0000423 return;
Nico Weber4c311642008-08-10 19:59:06 +0000424
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000425 // If this diagnostic is in a system header and is not a clang error, suppress
426 // it.
427 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner8488c822008-11-18 07:04:44 +0000428 Info.getLocation().isValid() &&
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000429 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000430 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
431 LastDiagLevel = Diagnostic::Ignored;
Chris Lattner3ac96992008-02-03 09:00:04 +0000432 return;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000433 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000434
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000435 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000436 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000437 ++NumErrors;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000438 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000439
Chris Lattner22eb9722006-06-18 05:43:12 +0000440 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000441 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekea06ec12009-01-23 20:28:53 +0000442 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000443
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000444 CurDiagID = ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +0000445}
446
Nico Weber4c311642008-08-10 19:59:06 +0000447
Chris Lattner22eb9722006-06-18 05:43:12 +0000448DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000449
Chris Lattner23be0672008-11-19 06:51:40 +0000450
Chris Lattner2b786902008-11-21 07:50:02 +0000451/// ModifierIs - Return true if the specified modifier matches specified string.
452template <std::size_t StrLen>
453static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
454 const char (&Str)[StrLen]) {
455 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
456}
457
458/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
459/// like this: %select{foo|bar|baz}2. This means that the integer argument
460/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
461/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
462/// This is very useful for certain classes of variant diagnostics.
463static void HandleSelectModifier(unsigned ValNo,
464 const char *Argument, unsigned ArgumentLen,
465 llvm::SmallVectorImpl<char> &OutStr) {
466 const char *ArgumentEnd = Argument+ArgumentLen;
467
468 // Skip over 'ValNo' |'s.
469 while (ValNo) {
470 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
471 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
472 " larger than the number of options in the diagnostic string!");
473 Argument = NextVal+1; // Skip this string.
474 --ValNo;
475 }
476
477 // Get the end of the value. This is either the } or the |.
478 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
479 // Add the value to the output string.
480 OutStr.append(Argument, EndPtr);
481}
482
483/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
484/// letter 's' to the string if the value is not 1. This is used in cases like
485/// this: "you idiot, you have %4 parameter%s4!".
486static void HandleIntegerSModifier(unsigned ValNo,
487 llvm::SmallVectorImpl<char> &OutStr) {
488 if (ValNo != 1)
489 OutStr.push_back('s');
490}
491
492
Sebastian Redl15b02d22008-11-22 13:44:36 +0000493/// PluralNumber - Parse an unsigned integer and advance Start.
494static unsigned PluralNumber(const char *&Start, const char *End)
495{
496 // Programming 101: Parse a decimal number :-)
497 unsigned Val = 0;
498 while (Start != End && *Start >= '0' && *Start <= '9') {
499 Val *= 10;
500 Val += *Start - '0';
501 ++Start;
502 }
503 return Val;
504}
505
506/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
507static bool TestPluralRange(unsigned Val, const char *&Start, const char *End)
508{
509 if (*Start != '[') {
510 unsigned Ref = PluralNumber(Start, End);
511 return Ref == Val;
512 }
513
514 ++Start;
515 unsigned Low = PluralNumber(Start, End);
516 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
517 ++Start;
518 unsigned High = PluralNumber(Start, End);
519 assert(*Start == ']' && "Bad plural expression syntax: expected )");
520 ++Start;
521 return Low <= Val && Val <= High;
522}
523
524/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
525static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End)
526{
527 // Empty condition?
528 if (*Start == ':')
529 return true;
530
531 while (1) {
532 char C = *Start;
533 if (C == '%') {
534 // Modulo expression
535 ++Start;
536 unsigned Arg = PluralNumber(Start, End);
537 assert(*Start == '=' && "Bad plural expression syntax: expected =");
538 ++Start;
539 unsigned ValMod = ValNo % Arg;
540 if (TestPluralRange(ValMod, Start, End))
541 return true;
542 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000543 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000544 "Bad plural expression syntax: unexpected character");
545 // Range expression
546 if (TestPluralRange(ValNo, Start, End))
547 return true;
548 }
549
550 // Scan for next or-expr part.
551 Start = std::find(Start, End, ',');
552 if(Start == End)
553 break;
554 ++Start;
555 }
556 return false;
557}
558
559/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
560/// for complex plural forms, or in languages where all plurals are complex.
561/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
562/// conditions that are tested in order, the form corresponding to the first
563/// that applies being emitted. The empty condition is always true, making the
564/// last form a default case.
565/// Conditions are simple boolean expressions, where n is the number argument.
566/// Here are the rules.
567/// condition := expression | empty
568/// empty := -> always true
569/// expression := numeric [',' expression] -> logical or
570/// numeric := range -> true if n in range
571/// | '%' number '=' range -> true if n % number in range
572/// range := number
573/// | '[' number ',' number ']' -> ranges are inclusive both ends
574///
575/// Here are some examples from the GNU gettext manual written in this form:
576/// English:
577/// {1:form0|:form1}
578/// Latvian:
579/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
580/// Gaeilge:
581/// {1:form0|2:form1|:form2}
582/// Romanian:
583/// {1:form0|0,%100=[1,19]:form1|:form2}
584/// Lithuanian:
585/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
586/// Russian (requires repeated form):
587/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
588/// Slovak
589/// {1:form0|[2,4]:form1|:form2}
590/// Polish (requires repeated form):
591/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
592static void HandlePluralModifier(unsigned ValNo,
593 const char *Argument, unsigned ArgumentLen,
594 llvm::SmallVectorImpl<char> &OutStr)
595{
596 const char *ArgumentEnd = Argument + ArgumentLen;
597 while (1) {
598 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
599 const char *ExprEnd = Argument;
600 while (*ExprEnd != ':') {
601 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
602 ++ExprEnd;
603 }
604 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
605 Argument = ExprEnd + 1;
606 ExprEnd = std::find(Argument, ArgumentEnd, '|');
607 OutStr.append(Argument, ExprEnd);
608 return;
609 }
610 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
611 }
612}
613
614
Chris Lattner23be0672008-11-19 06:51:40 +0000615/// FormatDiagnostic - Format this diagnostic into a string, substituting the
616/// formal arguments into the %0 slots. The result is appended onto the Str
617/// array.
618void DiagnosticInfo::
619FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
620 const char *DiagStr = getDiags()->getDescription(getID());
621 const char *DiagEnd = DiagStr+strlen(DiagStr);
Nico Weber4c311642008-08-10 19:59:06 +0000622
Chris Lattner23be0672008-11-19 06:51:40 +0000623 while (DiagStr != DiagEnd) {
624 if (DiagStr[0] != '%') {
625 // Append non-%0 substrings to Str if we have one.
626 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
627 OutStr.append(DiagStr, StrEnd);
628 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000629 continue;
Chris Lattner23be0672008-11-19 06:51:40 +0000630 } else if (DiagStr[1] == '%') {
631 OutStr.push_back('%'); // %% -> %.
632 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000633 continue;
634 }
635
636 // Skip the %.
637 ++DiagStr;
638
639 // This must be a placeholder for a diagnostic argument. The format for a
640 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
641 // The digit is a number from 0-9 indicating which argument this comes from.
642 // The modifier is a string of digits from the set [-a-z]+, arguments is a
643 // brace enclosed string.
644 const char *Modifier = 0, *Argument = 0;
645 unsigned ModifierLen = 0, ArgumentLen = 0;
646
647 // Check to see if we have a modifier. If so eat it.
648 if (!isdigit(DiagStr[0])) {
649 Modifier = DiagStr;
650 while (DiagStr[0] == '-' ||
651 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
652 ++DiagStr;
653 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000654
Chris Lattner2b786902008-11-21 07:50:02 +0000655 // If we have an argument, get it next.
656 if (DiagStr[0] == '{') {
657 ++DiagStr; // Skip {.
658 Argument = DiagStr;
659
660 for (; DiagStr[0] != '}'; ++DiagStr)
661 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
662 ArgumentLen = DiagStr-Argument;
663 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000664 }
Chris Lattner2b786902008-11-21 07:50:02 +0000665 }
666
667 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000668 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000669
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000670 switch (getArgKind(ArgNo)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000671 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000672 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000673 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000674 assert(ModifierLen == 0 && "No modifiers for strings yet");
675 OutStr.append(S.begin(), S.end());
676 break;
677 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000678 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000679 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000680 assert(ModifierLen == 0 && "No modifiers for strings yet");
681 OutStr.append(S, S + strlen(S));
682 break;
683 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000684 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000685 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000686 int Val = getArgSInt(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000687
688 if (ModifierIs(Modifier, ModifierLen, "select")) {
689 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
690 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
691 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000692 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
693 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000694 } else {
695 assert(ModifierLen == 0 && "Unknown integer modifier");
Chris Lattner91aea712008-11-19 07:22:31 +0000696 // FIXME: Optimize
Chris Lattner2b786902008-11-21 07:50:02 +0000697 std::string S = llvm::itostr(Val);
Chris Lattner91aea712008-11-19 07:22:31 +0000698 OutStr.append(S.begin(), S.end());
Chris Lattner91aea712008-11-19 07:22:31 +0000699 }
Chris Lattner2b786902008-11-21 07:50:02 +0000700 break;
701 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000702 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000703 unsigned Val = getArgUInt(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000704
705 if (ModifierIs(Modifier, ModifierLen, "select")) {
706 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
707 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
708 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000709 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
710 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000711 } else {
712 assert(ModifierLen == 0 && "Unknown integer modifier");
713
Chris Lattner91aea712008-11-19 07:22:31 +0000714 // FIXME: Optimize
Chris Lattner2b786902008-11-21 07:50:02 +0000715 std::string S = llvm::utostr_32(Val);
Chris Lattner91aea712008-11-19 07:22:31 +0000716 OutStr.append(S.begin(), S.end());
Chris Lattner91aea712008-11-19 07:22:31 +0000717 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000718 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000719 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000720 // ---- NAMES and TYPES ----
721 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000722 const IdentifierInfo *II = getArgIdentifier(ArgNo);
723 assert(ModifierLen == 0 && "No modifiers for strings yet");
Chris Lattner810d3302009-02-19 23:45:49 +0000724 OutStr.push_back('\'');
Chris Lattnere3d20d92008-11-23 21:45:46 +0000725 OutStr.append(II->getName(), II->getName() + II->getLength());
726 OutStr.push_back('\'');
727 break;
728 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000729 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000730 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000731 case Diagnostic::ak_nameddecl:
Chris Lattner63ecc502008-11-23 09:21:17 +0000732 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
733 Modifier, ModifierLen,
734 Argument, ArgumentLen, OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000735 break;
Nico Weber4c311642008-08-10 19:59:06 +0000736 }
737 }
Nico Weber4c311642008-08-10 19:59:06 +0000738}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000739
740/// IncludeInDiagnosticCounts - This method (whose default implementation
741/// returns true) indicates whether the diagnostics handled by this
742/// DiagnosticClient should be included in the number of diagnostics
743/// reported by Diagnostic.
744bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }