blob: 7242026b247b5b7bc25a36c8059791d8422abfc0 [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 Lattner6c440322009-04-16 06:07:15 +000037// Diagnostic classes.
38enum {
39 CLASS_NOTE = 0x01,
40 CLASS_WARNING = 0x02,
41 CLASS_EXTENSION = 0x03,
42 CLASS_ERROR = 0x04
43};
Chris Lattnere007de32009-04-15 07:01:18 +000044
Chris Lattner6a64cc62009-04-16 06:00:24 +000045struct StaticDiagInfoRec {
Chris Lattner6c440322009-04-16 06:07:15 +000046 unsigned short DiagID;
47 unsigned Mapping : 3;
48 unsigned Class : 3;
49 const char *Description;
Chris Lattner6a64cc62009-04-16 06:00:24 +000050 const char *OptionGroup;
Chris Lattnere007de32009-04-15 07:01:18 +000051};
52
Chris Lattner6a64cc62009-04-16 06:00:24 +000053static const StaticDiagInfoRec StaticDiagInfo[] = {
Chris Lattnera5389672009-04-16 05:52:14 +000054#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP) \
Chris Lattner6c440322009-04-16 06:07:15 +000055 { diag::ENUM, DEFAULT_MAPPING, CLASS, DESC, GROUP },
Chris Lattnere007de32009-04-15 07:01:18 +000056#include "clang/Basic/DiagnosticCommonKinds.inc"
57#include "clang/Basic/DiagnosticDriverKinds.inc"
58#include "clang/Basic/DiagnosticFrontendKinds.inc"
59#include "clang/Basic/DiagnosticLexKinds.inc"
60#include "clang/Basic/DiagnosticParseKinds.inc"
61#include "clang/Basic/DiagnosticASTKinds.inc"
62#include "clang/Basic/DiagnosticSemaKinds.inc"
63#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner6c440322009-04-16 06:07:15 +000064{ 0, 0, 0, 0, 0 }
Chris Lattnere007de32009-04-15 07:01:18 +000065};
Chris Lattnere6c831d2009-04-15 16:56:26 +000066#undef DIAG
Chris Lattnere007de32009-04-15 07:01:18 +000067
Chris Lattner6a64cc62009-04-16 06:00:24 +000068static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner411c0ff2009-04-16 04:12:40 +000069 // FIXME: Binary search.
Chris Lattner6a64cc62009-04-16 06:00:24 +000070 for (unsigned i = 0, e = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0]);
Chris Lattner411c0ff2009-04-16 04:12:40 +000071 i != e; ++i)
Chris Lattner6a64cc62009-04-16 06:00:24 +000072 if (StaticDiagInfo[i].DiagID == DiagID)
73 return &StaticDiagInfo[i];
74 return 0;
75}
76
77static unsigned GetDefaultDiagMapping(unsigned DiagID) {
78 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Chris Lattner6c440322009-04-16 06:07:15 +000079 return Info->Mapping;
Chris Lattner411c0ff2009-04-16 04:12:40 +000080 return diag::MAP_FATAL;
81}
82
Chris Lattner22cb8182009-04-16 05:44:38 +000083/// getWarningOptionForDiag - Return the lowest-level warning option that
84/// enables the specified diagnostic. If there is no -Wfoo flag that controls
85/// the diagnostic, this returns null.
86const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) {
Chris Lattner6a64cc62009-04-16 06:00:24 +000087 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
88 return Info->OptionGroup;
89 return 0;
Chris Lattner22cb8182009-04-16 05:44:38 +000090}
91
Chris Lattner22eb9722006-06-18 05:43:12 +000092/// getDiagClass - Return the class field of the diagnostic.
93///
Chris Lattner4431a1b2007-11-30 22:53:43 +000094static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner6c440322009-04-16 06:07:15 +000095 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
96 return Info->Class;
97 return ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +000098}
99
Chris Lattnere6535cf2007-12-02 01:09:57 +0000100//===----------------------------------------------------------------------===//
101// Custom Diagnostic information
102//===----------------------------------------------------------------------===//
103
104namespace clang {
105 namespace diag {
106 class CustomDiagInfo {
107 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
108 std::vector<DiagDesc> DiagInfo;
109 std::map<DiagDesc, unsigned> DiagIDs;
110 public:
111
112 /// getDescription - Return the description of the specified custom
113 /// diagnostic.
114 const char *getDescription(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000115 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000116 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000117 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattnere6535cf2007-12-02 01:09:57 +0000118 }
119
120 /// getLevel - Return the level of the specified custom diagnostic.
121 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000122 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000123 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000124 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000125 }
126
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000127 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
128 Diagnostic &Diags) {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000129 DiagDesc D(L, Message);
130 // Check to see if it already exists.
131 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
132 if (I != DiagIDs.end() && I->first == D)
133 return I->second;
134
135 // If not, assign a new ID.
Chris Lattner36790cf2009-01-29 06:55:46 +0000136 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000137 DiagIDs.insert(std::make_pair(D, ID));
138 DiagInfo.push_back(D);
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000139
140 // If this is a warning, and all warnings are supposed to map to errors,
141 // insert the mapping now.
142 if (L == Diagnostic::Warning && Diags.getWarningsAsErrors())
143 Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000144 return ID;
145 }
146 };
147
148 } // end diag namespace
149} // end clang namespace
150
151
152//===----------------------------------------------------------------------===//
153// Common Diagnostic implementation
154//===----------------------------------------------------------------------===//
155
Chris Lattner63ecc502008-11-23 09:21:17 +0000156static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
157 const char *Modifier, unsigned ML,
158 const char *Argument, unsigned ArgLen,
Chris Lattnercf868c42009-02-19 23:53:20 +0000159 llvm::SmallVectorImpl<char> &Output,
160 void *Cookie) {
Chris Lattner63ecc502008-11-23 09:21:17 +0000161 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000162 Output.append(Str, Str+strlen(Str));
163}
164
165
Ted Kremenek31691ae2008-08-07 17:49:57 +0000166Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattnere007de32009-04-15 07:01:18 +0000167 AllExtensionsSilenced = 0;
Chris Lattner8c800702008-05-29 15:36:45 +0000168 IgnoreAllWarnings = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000169 WarningsAsErrors = false;
Daniel Dunbar84b70f72008-09-12 18:10:20 +0000170 SuppressSystemWarnings = false;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000171 ExtBehavior = Ext_Ignore;
Chris Lattnerc49b9052007-05-28 00:46:44 +0000172
173 ErrorOccurred = false;
Chris Lattner9e031192009-02-06 04:16:02 +0000174 FatalErrorOccurred = false;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000175 NumDiagnostics = 0;
176 NumErrors = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000177 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000178 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000179 LastDiagLevel = Ignored;
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000180
Chris Lattner63ecc502008-11-23 09:21:17 +0000181 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000182 ArgToStringCookie = 0;
Chris Lattnere007de32009-04-15 07:01:18 +0000183
Chris Lattner411c0ff2009-04-16 04:12:40 +0000184 // Set all mappings to 'unset'.
185 memset(DiagMappings, 0, sizeof(DiagMappings));
Chris Lattnerae411572006-07-05 00:55:08 +0000186}
187
Chris Lattnere6535cf2007-12-02 01:09:57 +0000188Diagnostic::~Diagnostic() {
189 delete CustomDiagInfo;
190}
191
192/// getCustomDiagID - Return an ID for a diagnostic with the specified message
193/// and level. If this is the first request for this diagnosic, it is
194/// registered and created, otherwise the existing ID is returned.
195unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
196 if (CustomDiagInfo == 0)
197 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000198 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000199}
200
201
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000202/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
203/// level of the specified diagnostic ID is a Warning or Extension.
204/// This only works on builtin diagnostics, not custom ones, and is not legal to
205/// call on NOTEs.
206bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000207 return DiagID < diag::DIAG_UPPER_LIMIT &&
208 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000209}
210
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000211/// \brief Determine whether the given built-in diagnostic ID is a
212/// Note.
213bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000214 return DiagID < diag::DIAG_UPPER_LIMIT &&
215 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000216}
217
Chris Lattnere007de32009-04-15 07:01:18 +0000218/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
219/// ID is for an extension of some sort.
220///
221bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000222 return DiagID < diag::DIAG_UPPER_LIMIT &&
223 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattnere007de32009-04-15 07:01:18 +0000224}
225
Chris Lattner22eb9722006-06-18 05:43:12 +0000226
227/// getDescription - Given a diagnostic ID, return a description of the
228/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000229const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner6c440322009-04-16 06:07:15 +0000230 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
231 return Info->Description;
Chris Lattner7368d582009-01-27 18:30:58 +0000232 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000233}
234
235/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
236/// object, classify the specified diagnostic ID into a Level, consumable by
237/// the DiagnosticClient.
238Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000239 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000240 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000241 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner4431a1b2007-11-30 22:53:43 +0000242
243 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000244 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000245 return getDiagnosticLevel(DiagID, DiagClass);
246}
247
248/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
249/// object, classify the specified diagnostic ID into a Level, consumable by
250/// the DiagnosticClient.
251Diagnostic::Level
252Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattnerae411572006-07-05 00:55:08 +0000253 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000254 // to error. Errors can only be mapped to fatal.
Chris Lattnere007de32009-04-15 07:01:18 +0000255 Diagnostic::Level Result = Diagnostic::Fatal;
Chris Lattner411c0ff2009-04-16 04:12:40 +0000256
257 // Get the mapping information, if unset, compute it lazily.
258 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
259 if (MappingInfo == 0) {
260 MappingInfo = GetDefaultDiagMapping(DiagID);
261 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
262 }
263
264 switch (MappingInfo & 7) {
265 default: assert(0 && "Unknown mapping!");
Chris Lattnere007de32009-04-15 07:01:18 +0000266 case diag::MAP_IGNORE:
Chris Lattnerb8e73152009-04-16 05:04:32 +0000267 // Ignore this, unless this is an extension diagnostic and we're mapping
268 // them onto warnings or errors.
269 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
270 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
271 (MappingInfo & 8) != 0) // User explicitly mapped it.
272 return Diagnostic::Ignored;
273 Result = Diagnostic::Warning;
274 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
275 break;
Chris Lattnere007de32009-04-15 07:01:18 +0000276 case diag::MAP_ERROR:
277 Result = Diagnostic::Error;
278 break;
279 case diag::MAP_FATAL:
280 Result = Diagnostic::Fatal;
281 break;
282 case diag::MAP_WARNING:
283 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner8c800702008-05-29 15:36:45 +0000284 if (IgnoreAllWarnings)
285 return Diagnostic::Ignored;
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000286
287 Result = Diagnostic::Warning;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000288
289 // If this is an extension diagnostic and we're in -pedantic-error mode, and
290 // if the user didn't explicitly map it, upgrade to an error.
291 if (ExtBehavior == Ext_Error &&
292 (MappingInfo & 8) == 0 &&
293 isBuiltinExtensionDiag(DiagID))
294 Result = Diagnostic::Error;
295
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000296 if (WarningsAsErrors)
297 Result = Diagnostic::Error;
298 break;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000299
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000300 case diag::MAP_WARNING_NO_WERROR:
301 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
302 // not be adjusted by -Werror or -pedantic-errors.
303 Result = Diagnostic::Warning;
304
305 // If warnings are globally mapped to ignore or error, do it.
306 if (IgnoreAllWarnings)
307 return Diagnostic::Ignored;
308
Chris Lattnere007de32009-04-15 07:01:18 +0000309 break;
Chris Lattner8c800702008-05-29 15:36:45 +0000310 }
Chris Lattnere007de32009-04-15 07:01:18 +0000311
312 // Okay, we're about to return this as a "diagnostic to emit" one last check:
313 // if this is any sort of extension warning, and if we're in an __extension__
314 // block, silence it.
315 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
316 return Diagnostic::Ignored;
Chris Lattner22eb9722006-06-18 05:43:12 +0000317
Chris Lattnere007de32009-04-15 07:01:18 +0000318 return Result;
Chris Lattner22eb9722006-06-18 05:43:12 +0000319}
320
Chris Lattner8488c822008-11-18 07:04:44 +0000321/// ProcessDiag - This is the method used to report a diagnostic that is
322/// finally fully formed.
Chris Lattner427c9c12008-11-22 00:59:29 +0000323void Diagnostic::ProcessDiag() {
324 DiagnosticInfo Info(this);
Douglas Gregor19367f52009-03-19 18:55:06 +0000325
Chris Lattner22eb9722006-06-18 05:43:12 +0000326 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000327 Diagnostic::Level DiagLevel;
328 unsigned DiagID = Info.getID();
Chris Lattner22eb9722006-06-18 05:43:12 +0000329
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000330 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
331 // in a system header.
332 bool ShouldEmitInSystemHeader;
333
334 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
335 // Handle custom diagnostics, which cannot be mapped.
336 DiagLevel = CustomDiagInfo->getLevel(DiagID);
337
338 // Custom diagnostics always are emitted in system headers.
339 ShouldEmitInSystemHeader = true;
340 } else {
341 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
342 // the diagnostic level was for the previous diagnostic so that it is
343 // filtered the same as the previous diagnostic.
344 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000345 if (DiagClass == CLASS_NOTE) {
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000346 DiagLevel = Diagnostic::Note;
347 ShouldEmitInSystemHeader = false; // extra consideration is needed
348 } else {
349 // If this is not an error and we are in a system header, we ignore it.
350 // Check the original Diag ID here, because we also want to ignore
351 // extensions and warnings in -Werror and -pedantic-errors modes, which
352 // *map* warnings/extensions to errors.
Chris Lattnere6c831d2009-04-15 16:56:26 +0000353 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000354
355 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
356 }
357 }
358
Douglas Gregor19367f52009-03-19 18:55:06 +0000359 if (DiagLevel != Diagnostic::Note) {
360 // Record that a fatal error occurred only when we see a second
361 // non-note diagnostic. This allows notes to be attached to the
362 // fatal error, but suppresses any diagnostics that follow those
363 // notes.
364 if (LastDiagLevel == Diagnostic::Fatal)
365 FatalErrorOccurred = true;
366
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000367 LastDiagLevel = DiagLevel;
Douglas Gregor19367f52009-03-19 18:55:06 +0000368 }
369
370 // If a fatal error has already been emitted, silence all subsequent
371 // diagnostics.
372 if (FatalErrorOccurred)
373 return;
374
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000375 // If the client doesn't care about this message, don't issue it. If this is
376 // a note and the last real diagnostic was ignored, ignore it too.
377 if (DiagLevel == Diagnostic::Ignored ||
378 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Chris Lattnercb283342006-06-18 06:48:37 +0000379 return;
Nico Weber4c311642008-08-10 19:59:06 +0000380
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000381 // If this diagnostic is in a system header and is not a clang error, suppress
382 // it.
383 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner8488c822008-11-18 07:04:44 +0000384 Info.getLocation().isValid() &&
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000385 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000386 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
387 LastDiagLevel = Diagnostic::Ignored;
Chris Lattner3ac96992008-02-03 09:00:04 +0000388 return;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000389 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000390
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000391 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000392 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000393 ++NumErrors;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000394 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000395
Chris Lattner22eb9722006-06-18 05:43:12 +0000396 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000397 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekea06ec12009-01-23 20:28:53 +0000398 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000399
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000400 CurDiagID = ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +0000401}
402
Nico Weber4c311642008-08-10 19:59:06 +0000403
Chris Lattner22eb9722006-06-18 05:43:12 +0000404DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000405
Chris Lattner23be0672008-11-19 06:51:40 +0000406
Chris Lattner2b786902008-11-21 07:50:02 +0000407/// ModifierIs - Return true if the specified modifier matches specified string.
408template <std::size_t StrLen>
409static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
410 const char (&Str)[StrLen]) {
411 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
412}
413
414/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
415/// like this: %select{foo|bar|baz}2. This means that the integer argument
416/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
417/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
418/// This is very useful for certain classes of variant diagnostics.
419static void HandleSelectModifier(unsigned ValNo,
420 const char *Argument, unsigned ArgumentLen,
421 llvm::SmallVectorImpl<char> &OutStr) {
422 const char *ArgumentEnd = Argument+ArgumentLen;
423
424 // Skip over 'ValNo' |'s.
425 while (ValNo) {
426 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
427 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
428 " larger than the number of options in the diagnostic string!");
429 Argument = NextVal+1; // Skip this string.
430 --ValNo;
431 }
432
433 // Get the end of the value. This is either the } or the |.
434 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
435 // Add the value to the output string.
436 OutStr.append(Argument, EndPtr);
437}
438
439/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
440/// letter 's' to the string if the value is not 1. This is used in cases like
441/// this: "you idiot, you have %4 parameter%s4!".
442static void HandleIntegerSModifier(unsigned ValNo,
443 llvm::SmallVectorImpl<char> &OutStr) {
444 if (ValNo != 1)
445 OutStr.push_back('s');
446}
447
448
Sebastian Redl15b02d22008-11-22 13:44:36 +0000449/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000450static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000451 // Programming 101: Parse a decimal number :-)
452 unsigned Val = 0;
453 while (Start != End && *Start >= '0' && *Start <= '9') {
454 Val *= 10;
455 Val += *Start - '0';
456 ++Start;
457 }
458 return Val;
459}
460
461/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000462static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000463 if (*Start != '[') {
464 unsigned Ref = PluralNumber(Start, End);
465 return Ref == Val;
466 }
467
468 ++Start;
469 unsigned Low = PluralNumber(Start, End);
470 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
471 ++Start;
472 unsigned High = PluralNumber(Start, End);
473 assert(*Start == ']' && "Bad plural expression syntax: expected )");
474 ++Start;
475 return Low <= Val && Val <= High;
476}
477
478/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000479static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000480 // Empty condition?
481 if (*Start == ':')
482 return true;
483
484 while (1) {
485 char C = *Start;
486 if (C == '%') {
487 // Modulo expression
488 ++Start;
489 unsigned Arg = PluralNumber(Start, End);
490 assert(*Start == '=' && "Bad plural expression syntax: expected =");
491 ++Start;
492 unsigned ValMod = ValNo % Arg;
493 if (TestPluralRange(ValMod, Start, End))
494 return true;
495 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000496 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000497 "Bad plural expression syntax: unexpected character");
498 // Range expression
499 if (TestPluralRange(ValNo, Start, End))
500 return true;
501 }
502
503 // Scan for next or-expr part.
504 Start = std::find(Start, End, ',');
505 if(Start == End)
506 break;
507 ++Start;
508 }
509 return false;
510}
511
512/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
513/// for complex plural forms, or in languages where all plurals are complex.
514/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
515/// conditions that are tested in order, the form corresponding to the first
516/// that applies being emitted. The empty condition is always true, making the
517/// last form a default case.
518/// Conditions are simple boolean expressions, where n is the number argument.
519/// Here are the rules.
520/// condition := expression | empty
521/// empty := -> always true
522/// expression := numeric [',' expression] -> logical or
523/// numeric := range -> true if n in range
524/// | '%' number '=' range -> true if n % number in range
525/// range := number
526/// | '[' number ',' number ']' -> ranges are inclusive both ends
527///
528/// Here are some examples from the GNU gettext manual written in this form:
529/// English:
530/// {1:form0|:form1}
531/// Latvian:
532/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
533/// Gaeilge:
534/// {1:form0|2:form1|:form2}
535/// Romanian:
536/// {1:form0|0,%100=[1,19]:form1|:form2}
537/// Lithuanian:
538/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
539/// Russian (requires repeated form):
540/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
541/// Slovak
542/// {1:form0|[2,4]:form1|:form2}
543/// Polish (requires repeated form):
544/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
545static void HandlePluralModifier(unsigned ValNo,
546 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb8e73152009-04-16 05:04:32 +0000547 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000548 const char *ArgumentEnd = Argument + ArgumentLen;
549 while (1) {
550 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
551 const char *ExprEnd = Argument;
552 while (*ExprEnd != ':') {
553 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
554 ++ExprEnd;
555 }
556 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
557 Argument = ExprEnd + 1;
558 ExprEnd = std::find(Argument, ArgumentEnd, '|');
559 OutStr.append(Argument, ExprEnd);
560 return;
561 }
562 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
563 }
564}
565
566
Chris Lattner23be0672008-11-19 06:51:40 +0000567/// FormatDiagnostic - Format this diagnostic into a string, substituting the
568/// formal arguments into the %0 slots. The result is appended onto the Str
569/// array.
570void DiagnosticInfo::
571FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
572 const char *DiagStr = getDiags()->getDescription(getID());
573 const char *DiagEnd = DiagStr+strlen(DiagStr);
Nico Weber4c311642008-08-10 19:59:06 +0000574
Chris Lattner23be0672008-11-19 06:51:40 +0000575 while (DiagStr != DiagEnd) {
576 if (DiagStr[0] != '%') {
577 // Append non-%0 substrings to Str if we have one.
578 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
579 OutStr.append(DiagStr, StrEnd);
580 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000581 continue;
Chris Lattner23be0672008-11-19 06:51:40 +0000582 } else if (DiagStr[1] == '%') {
583 OutStr.push_back('%'); // %% -> %.
584 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000585 continue;
586 }
587
588 // Skip the %.
589 ++DiagStr;
590
591 // This must be a placeholder for a diagnostic argument. The format for a
592 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
593 // The digit is a number from 0-9 indicating which argument this comes from.
594 // The modifier is a string of digits from the set [-a-z]+, arguments is a
595 // brace enclosed string.
596 const char *Modifier = 0, *Argument = 0;
597 unsigned ModifierLen = 0, ArgumentLen = 0;
598
599 // Check to see if we have a modifier. If so eat it.
600 if (!isdigit(DiagStr[0])) {
601 Modifier = DiagStr;
602 while (DiagStr[0] == '-' ||
603 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
604 ++DiagStr;
605 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000606
Chris Lattner2b786902008-11-21 07:50:02 +0000607 // If we have an argument, get it next.
608 if (DiagStr[0] == '{') {
609 ++DiagStr; // Skip {.
610 Argument = DiagStr;
611
612 for (; DiagStr[0] != '}'; ++DiagStr)
613 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
614 ArgumentLen = DiagStr-Argument;
615 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000616 }
Chris Lattner2b786902008-11-21 07:50:02 +0000617 }
618
619 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000620 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000621
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000622 switch (getArgKind(ArgNo)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000623 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000624 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000625 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000626 assert(ModifierLen == 0 && "No modifiers for strings yet");
627 OutStr.append(S.begin(), S.end());
628 break;
629 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000630 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000631 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000632 assert(ModifierLen == 0 && "No modifiers for strings yet");
633 OutStr.append(S, S + strlen(S));
634 break;
635 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000636 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000637 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000638 int Val = getArgSInt(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000639
640 if (ModifierIs(Modifier, ModifierLen, "select")) {
641 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
642 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
643 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000644 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
645 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000646 } else {
647 assert(ModifierLen == 0 && "Unknown integer modifier");
Chris Lattner91aea712008-11-19 07:22:31 +0000648 // FIXME: Optimize
Chris Lattner2b786902008-11-21 07:50:02 +0000649 std::string S = llvm::itostr(Val);
Chris Lattner91aea712008-11-19 07:22:31 +0000650 OutStr.append(S.begin(), S.end());
Chris Lattner91aea712008-11-19 07:22:31 +0000651 }
Chris Lattner2b786902008-11-21 07:50:02 +0000652 break;
653 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000654 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000655 unsigned Val = getArgUInt(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000656
657 if (ModifierIs(Modifier, ModifierLen, "select")) {
658 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
659 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
660 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000661 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
662 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000663 } else {
664 assert(ModifierLen == 0 && "Unknown integer modifier");
665
Chris Lattner91aea712008-11-19 07:22:31 +0000666 // FIXME: Optimize
Chris Lattner2b786902008-11-21 07:50:02 +0000667 std::string S = llvm::utostr_32(Val);
Chris Lattner91aea712008-11-19 07:22:31 +0000668 OutStr.append(S.begin(), S.end());
Chris Lattner91aea712008-11-19 07:22:31 +0000669 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000670 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000671 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000672 // ---- NAMES and TYPES ----
673 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000674 const IdentifierInfo *II = getArgIdentifier(ArgNo);
675 assert(ModifierLen == 0 && "No modifiers for strings yet");
Chris Lattner810d3302009-02-19 23:45:49 +0000676 OutStr.push_back('\'');
Chris Lattnere3d20d92008-11-23 21:45:46 +0000677 OutStr.append(II->getName(), II->getName() + II->getLength());
678 OutStr.push_back('\'');
679 break;
680 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000681 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000682 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000683 case Diagnostic::ak_nameddecl:
Chris Lattner63ecc502008-11-23 09:21:17 +0000684 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
685 Modifier, ModifierLen,
686 Argument, ArgumentLen, OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000687 break;
Nico Weber4c311642008-08-10 19:59:06 +0000688 }
689 }
Nico Weber4c311642008-08-10 19:59:06 +0000690}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000691
692/// IncludeInDiagnosticCounts - This method (whose default implementation
693/// returns true) indicates whether the diagnostics handled by this
694/// DiagnosticClient should be included in the number of diagnostics
695/// reported by Diagnostic.
696bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }