blob: bf11a13dc2bb2e932d3db5047991f2f925a770f6 [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 Lattner121f60c2009-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 Lattner27ceb9d2009-04-15 07:01:18 +000044
Chris Lattner33dd2822009-04-16 06:00:24 +000045struct StaticDiagInfoRec {
Chris Lattner121f60c2009-04-16 06:07:15 +000046 unsigned short DiagID;
47 unsigned Mapping : 3;
48 unsigned Class : 3;
49 const char *Description;
Chris Lattner33dd2822009-04-16 06:00:24 +000050 const char *OptionGroup;
Chris Lattner87d854e2009-04-16 06:13:46 +000051
52 bool operator<(const StaticDiagInfoRec &RHS) const {
53 return DiagID < RHS.DiagID;
54 }
55 bool operator>(const StaticDiagInfoRec &RHS) const {
56 return DiagID > RHS.DiagID;
57 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +000058};
59
Chris Lattner33dd2822009-04-16 06:00:24 +000060static const StaticDiagInfoRec StaticDiagInfo[] = {
Chris Lattner19cbb442009-04-16 05:52:14 +000061#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP) \
Chris Lattner121f60c2009-04-16 06:07:15 +000062 { diag::ENUM, DEFAULT_MAPPING, CLASS, DESC, GROUP },
Chris Lattner27ceb9d2009-04-15 07:01:18 +000063#include "clang/Basic/DiagnosticCommonKinds.inc"
64#include "clang/Basic/DiagnosticDriverKinds.inc"
65#include "clang/Basic/DiagnosticFrontendKinds.inc"
66#include "clang/Basic/DiagnosticLexKinds.inc"
67#include "clang/Basic/DiagnosticParseKinds.inc"
68#include "clang/Basic/DiagnosticASTKinds.inc"
69#include "clang/Basic/DiagnosticSemaKinds.inc"
70#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner121f60c2009-04-16 06:07:15 +000071{ 0, 0, 0, 0, 0 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +000072};
Chris Lattner8a941e02009-04-15 16:56:26 +000073#undef DIAG
Chris Lattner27ceb9d2009-04-15 07:01:18 +000074
Chris Lattner87d854e2009-04-16 06:13:46 +000075/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
76/// or null if the ID is invalid.
Chris Lattner33dd2822009-04-16 06:00:24 +000077static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner87d854e2009-04-16 06:13:46 +000078 unsigned NumDiagEntries = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
79
80 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
81#ifndef NDEBUG
82 static bool IsFirst = true;
83 if (IsFirst) {
84 for (unsigned i = 1; i != NumDiagEntries; ++i)
85 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
86 "Improperly sorted diag info");
87 IsFirst = false;
88 }
89#endif
90
91 // Search the diagnostic table with a binary search.
92 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0 };
93
94 const StaticDiagInfoRec *Found =
95 std::lower_bound(StaticDiagInfo, StaticDiagInfo + NumDiagEntries, Find);
96 if (Found == StaticDiagInfo + NumDiagEntries ||
97 Found->DiagID != DiagID)
98 return 0;
99
100 return Found;
Chris Lattner33dd2822009-04-16 06:00:24 +0000101}
102
103static unsigned GetDefaultDiagMapping(unsigned DiagID) {
104 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Chris Lattner121f60c2009-04-16 06:07:15 +0000105 return Info->Mapping;
Chris Lattner691f1ae2009-04-16 04:12:40 +0000106 return diag::MAP_FATAL;
107}
108
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000109/// getWarningOptionForDiag - Return the lowest-level warning option that
110/// enables the specified diagnostic. If there is no -Wfoo flag that controls
111/// the diagnostic, this returns null.
112const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) {
Chris Lattner33dd2822009-04-16 06:00:24 +0000113 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
114 return Info->OptionGroup;
115 return 0;
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000116}
117
Reid Spencer5f016e22007-07-11 17:01:13 +0000118/// getDiagClass - Return the class field of the diagnostic.
119///
Chris Lattner07506182007-11-30 22:53:43 +0000120static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner121f60c2009-04-16 06:07:15 +0000121 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
122 return Info->Class;
123 return ~0U;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124}
125
Chris Lattner182745a2007-12-02 01:09:57 +0000126//===----------------------------------------------------------------------===//
127// Custom Diagnostic information
128//===----------------------------------------------------------------------===//
129
130namespace clang {
131 namespace diag {
132 class CustomDiagInfo {
133 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
134 std::vector<DiagDesc> DiagInfo;
135 std::map<DiagDesc, unsigned> DiagIDs;
136 public:
137
138 /// getDescription - Return the description of the specified custom
139 /// diagnostic.
140 const char *getDescription(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000141 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000142 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000143 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattner182745a2007-12-02 01:09:57 +0000144 }
145
146 /// getLevel - Return the level of the specified custom diagnostic.
147 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000148 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000149 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000150 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattner182745a2007-12-02 01:09:57 +0000151 }
152
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000153 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
154 Diagnostic &Diags) {
Chris Lattner182745a2007-12-02 01:09:57 +0000155 DiagDesc D(L, Message);
156 // Check to see if it already exists.
157 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
158 if (I != DiagIDs.end() && I->first == D)
159 return I->second;
160
161 // If not, assign a new ID.
Chris Lattner88eccaf2009-01-29 06:55:46 +0000162 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattner182745a2007-12-02 01:09:57 +0000163 DiagIDs.insert(std::make_pair(D, ID));
164 DiagInfo.push_back(D);
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000165
166 // If this is a warning, and all warnings are supposed to map to errors,
167 // insert the mapping now.
168 if (L == Diagnostic::Warning && Diags.getWarningsAsErrors())
169 Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR);
Chris Lattner182745a2007-12-02 01:09:57 +0000170 return ID;
171 }
172 };
173
174 } // end diag namespace
175} // end clang namespace
176
177
178//===----------------------------------------------------------------------===//
179// Common Diagnostic implementation
180//===----------------------------------------------------------------------===//
181
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000182static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
183 const char *Modifier, unsigned ML,
184 const char *Argument, unsigned ArgLen,
Chris Lattner92dd3862009-02-19 23:53:20 +0000185 llvm::SmallVectorImpl<char> &Output,
186 void *Cookie) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000187 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +0000188 Output.append(Str, Str+strlen(Str));
189}
190
191
Ted Kremenekb4398aa2008-08-07 17:49:57 +0000192Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000193 AllExtensionsSilenced = 0;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000194 IgnoreAllWarnings = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 WarningsAsErrors = false;
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000196 SuppressSystemWarnings = false;
Chris Lattnerb54b2762009-04-16 05:04:32 +0000197 ExtBehavior = Ext_Ignore;
Reid Spencer5f016e22007-07-11 17:01:13 +0000198
199 ErrorOccurred = false;
Chris Lattner15221422009-02-06 04:16:02 +0000200 FatalErrorOccurred = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 NumDiagnostics = 0;
202 NumErrors = 0;
Chris Lattner182745a2007-12-02 01:09:57 +0000203 CustomDiagInfo = 0;
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000204 CurDiagID = ~0U;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000205 LastDiagLevel = Ignored;
Chris Lattner22caddc2008-11-23 09:13:29 +0000206
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000207 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +0000208 ArgToStringCookie = 0;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000209
Chris Lattner691f1ae2009-04-16 04:12:40 +0000210 // Set all mappings to 'unset'.
211 memset(DiagMappings, 0, sizeof(DiagMappings));
Reid Spencer5f016e22007-07-11 17:01:13 +0000212}
213
Chris Lattner182745a2007-12-02 01:09:57 +0000214Diagnostic::~Diagnostic() {
215 delete CustomDiagInfo;
216}
217
218/// getCustomDiagID - Return an ID for a diagnostic with the specified message
219/// and level. If this is the first request for this diagnosic, it is
220/// registered and created, otherwise the existing ID is returned.
221unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
222 if (CustomDiagInfo == 0)
223 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000224 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattner182745a2007-12-02 01:09:57 +0000225}
226
227
Chris Lattnerf5d23282009-02-17 06:49:55 +0000228/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
229/// level of the specified diagnostic ID is a Warning or Extension.
230/// This only works on builtin diagnostics, not custom ones, and is not legal to
231/// call on NOTEs.
232bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000233 return DiagID < diag::DIAG_UPPER_LIMIT &&
234 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Reid Spencer5f016e22007-07-11 17:01:13 +0000235}
236
Douglas Gregoree1828a2009-03-10 18:03:33 +0000237/// \brief Determine whether the given built-in diagnostic ID is a
238/// Note.
239bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000240 return DiagID < diag::DIAG_UPPER_LIMIT &&
241 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000242}
243
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000244/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
245/// ID is for an extension of some sort.
246///
247bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000248 return DiagID < diag::DIAG_UPPER_LIMIT &&
249 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000250}
251
Reid Spencer5f016e22007-07-11 17:01:13 +0000252
253/// getDescription - Given a diagnostic ID, return a description of the
254/// issue.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000255const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner121f60c2009-04-16 06:07:15 +0000256 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
257 return Info->Description;
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000258 return CustomDiagInfo->getDescription(DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000259}
260
261/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
262/// object, classify the specified diagnostic ID into a Level, consumable by
263/// the DiagnosticClient.
264Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattner182745a2007-12-02 01:09:57 +0000265 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000266 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattner182745a2007-12-02 01:09:57 +0000267 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner07506182007-11-30 22:53:43 +0000268
269 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000270 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerf5d23282009-02-17 06:49:55 +0000271 return getDiagnosticLevel(DiagID, DiagClass);
272}
273
274/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
275/// object, classify the specified diagnostic ID into a Level, consumable by
276/// the DiagnosticClient.
277Diagnostic::Level
278Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerf5d23282009-02-17 06:49:55 +0000280 // to error. Errors can only be mapped to fatal.
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000281 Diagnostic::Level Result = Diagnostic::Fatal;
Chris Lattner691f1ae2009-04-16 04:12:40 +0000282
283 // Get the mapping information, if unset, compute it lazily.
284 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
285 if (MappingInfo == 0) {
286 MappingInfo = GetDefaultDiagMapping(DiagID);
287 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
288 }
289
290 switch (MappingInfo & 7) {
291 default: assert(0 && "Unknown mapping!");
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000292 case diag::MAP_IGNORE:
Chris Lattnerb54b2762009-04-16 05:04:32 +0000293 // Ignore this, unless this is an extension diagnostic and we're mapping
294 // them onto warnings or errors.
295 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
296 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
297 (MappingInfo & 8) != 0) // User explicitly mapped it.
298 return Diagnostic::Ignored;
299 Result = Diagnostic::Warning;
300 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
301 break;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000302 case diag::MAP_ERROR:
303 Result = Diagnostic::Error;
304 break;
305 case diag::MAP_FATAL:
306 Result = Diagnostic::Fatal;
307 break;
308 case diag::MAP_WARNING:
309 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner5b4681c2008-05-29 15:36:45 +0000310 if (IgnoreAllWarnings)
311 return Diagnostic::Ignored;
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000312
313 Result = Diagnostic::Warning;
Chris Lattnerb54b2762009-04-16 05:04:32 +0000314
315 // If this is an extension diagnostic and we're in -pedantic-error mode, and
316 // if the user didn't explicitly map it, upgrade to an error.
317 if (ExtBehavior == Ext_Error &&
318 (MappingInfo & 8) == 0 &&
319 isBuiltinExtensionDiag(DiagID))
320 Result = Diagnostic::Error;
321
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000322 if (WarningsAsErrors)
323 Result = Diagnostic::Error;
324 break;
Chris Lattnerb54b2762009-04-16 05:04:32 +0000325
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000326 case diag::MAP_WARNING_NO_WERROR:
327 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
328 // not be adjusted by -Werror or -pedantic-errors.
329 Result = Diagnostic::Warning;
330
331 // If warnings are globally mapped to ignore or error, do it.
332 if (IgnoreAllWarnings)
333 return Diagnostic::Ignored;
334
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000335 break;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000336 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000337
338 // Okay, we're about to return this as a "diagnostic to emit" one last check:
339 // if this is any sort of extension warning, and if we're in an __extension__
340 // block, silence it.
341 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
342 return Diagnostic::Ignored;
Reid Spencer5f016e22007-07-11 17:01:13 +0000343
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000344 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000345}
346
Chris Lattner0a14eee2008-11-18 07:04:44 +0000347/// ProcessDiag - This is the method used to report a diagnostic that is
348/// finally fully formed.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000349void Diagnostic::ProcessDiag() {
350 DiagnosticInfo Info(this);
Douglas Gregor525c4b02009-03-19 18:55:06 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 // Figure out the diagnostic level of this message.
Chris Lattnerf5d23282009-02-17 06:49:55 +0000353 Diagnostic::Level DiagLevel;
354 unsigned DiagID = Info.getID();
Reid Spencer5f016e22007-07-11 17:01:13 +0000355
Chris Lattnerf5d23282009-02-17 06:49:55 +0000356 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
357 // in a system header.
358 bool ShouldEmitInSystemHeader;
359
360 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
361 // Handle custom diagnostics, which cannot be mapped.
362 DiagLevel = CustomDiagInfo->getLevel(DiagID);
363
364 // Custom diagnostics always are emitted in system headers.
365 ShouldEmitInSystemHeader = true;
366 } else {
367 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
368 // the diagnostic level was for the previous diagnostic so that it is
369 // filtered the same as the previous diagnostic.
370 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000371 if (DiagClass == CLASS_NOTE) {
Chris Lattnerf5d23282009-02-17 06:49:55 +0000372 DiagLevel = Diagnostic::Note;
373 ShouldEmitInSystemHeader = false; // extra consideration is needed
374 } else {
375 // If this is not an error and we are in a system header, we ignore it.
376 // Check the original Diag ID here, because we also want to ignore
377 // extensions and warnings in -Werror and -pedantic-errors modes, which
378 // *map* warnings/extensions to errors.
Chris Lattner8a941e02009-04-15 16:56:26 +0000379 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Chris Lattnerf5d23282009-02-17 06:49:55 +0000380
381 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
382 }
383 }
384
Douglas Gregor525c4b02009-03-19 18:55:06 +0000385 if (DiagLevel != Diagnostic::Note) {
386 // Record that a fatal error occurred only when we see a second
387 // non-note diagnostic. This allows notes to be attached to the
388 // fatal error, but suppresses any diagnostics that follow those
389 // notes.
390 if (LastDiagLevel == Diagnostic::Fatal)
391 FatalErrorOccurred = true;
392
Chris Lattnerf5d23282009-02-17 06:49:55 +0000393 LastDiagLevel = DiagLevel;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000394 }
395
396 // If a fatal error has already been emitted, silence all subsequent
397 // diagnostics.
398 if (FatalErrorOccurred)
399 return;
400
Chris Lattnerf5d23282009-02-17 06:49:55 +0000401 // If the client doesn't care about this message, don't issue it. If this is
402 // a note and the last real diagnostic was ignored, ignore it too.
403 if (DiagLevel == Diagnostic::Ignored ||
404 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 return;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000406
Chris Lattnerf5d23282009-02-17 06:49:55 +0000407 // If this diagnostic is in a system header and is not a clang error, suppress
408 // it.
409 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner0a14eee2008-11-18 07:04:44 +0000410 Info.getLocation().isValid() &&
Chris Lattnerf5d23282009-02-17 06:49:55 +0000411 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner336f26b2009-02-17 06:52:20 +0000412 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
413 LastDiagLevel = Diagnostic::Ignored;
Chris Lattner7097d912008-02-03 09:00:04 +0000414 return;
Chris Lattner336f26b2009-02-17 06:52:20 +0000415 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 if (DiagLevel >= Diagnostic::Error) {
418 ErrorOccurred = true;
Chris Lattner0a14eee2008-11-18 07:04:44 +0000419 ++NumErrors;
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000421
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 // Finally, report it.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000423 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekcabe6682009-01-23 20:28:53 +0000424 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000425
Douglas Gregoree1828a2009-03-10 18:03:33 +0000426 CurDiagID = ~0U;
Reid Spencer5f016e22007-07-11 17:01:13 +0000427}
428
Nico Weber7bfaaae2008-08-10 19:59:06 +0000429
Reid Spencer5f016e22007-07-11 17:01:13 +0000430DiagnosticClient::~DiagnosticClient() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000431
Chris Lattnerf4c83962008-11-19 06:51:40 +0000432
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000433/// ModifierIs - Return true if the specified modifier matches specified string.
434template <std::size_t StrLen>
435static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
436 const char (&Str)[StrLen]) {
437 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
438}
439
440/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
441/// like this: %select{foo|bar|baz}2. This means that the integer argument
442/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
443/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
444/// This is very useful for certain classes of variant diagnostics.
445static void HandleSelectModifier(unsigned ValNo,
446 const char *Argument, unsigned ArgumentLen,
447 llvm::SmallVectorImpl<char> &OutStr) {
448 const char *ArgumentEnd = Argument+ArgumentLen;
449
450 // Skip over 'ValNo' |'s.
451 while (ValNo) {
452 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
453 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
454 " larger than the number of options in the diagnostic string!");
455 Argument = NextVal+1; // Skip this string.
456 --ValNo;
457 }
458
459 // Get the end of the value. This is either the } or the |.
460 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
461 // Add the value to the output string.
462 OutStr.append(Argument, EndPtr);
463}
464
465/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
466/// letter 's' to the string if the value is not 1. This is used in cases like
467/// this: "you idiot, you have %4 parameter%s4!".
468static void HandleIntegerSModifier(unsigned ValNo,
469 llvm::SmallVectorImpl<char> &OutStr) {
470 if (ValNo != 1)
471 OutStr.push_back('s');
472}
473
474
Sebastian Redle4c452c2008-11-22 13:44:36 +0000475/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000476static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000477 // Programming 101: Parse a decimal number :-)
478 unsigned Val = 0;
479 while (Start != End && *Start >= '0' && *Start <= '9') {
480 Val *= 10;
481 Val += *Start - '0';
482 ++Start;
483 }
484 return Val;
485}
486
487/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000488static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000489 if (*Start != '[') {
490 unsigned Ref = PluralNumber(Start, End);
491 return Ref == Val;
492 }
493
494 ++Start;
495 unsigned Low = PluralNumber(Start, End);
496 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
497 ++Start;
498 unsigned High = PluralNumber(Start, End);
499 assert(*Start == ']' && "Bad plural expression syntax: expected )");
500 ++Start;
501 return Low <= Val && Val <= High;
502}
503
504/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000505static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000506 // Empty condition?
507 if (*Start == ':')
508 return true;
509
510 while (1) {
511 char C = *Start;
512 if (C == '%') {
513 // Modulo expression
514 ++Start;
515 unsigned Arg = PluralNumber(Start, End);
516 assert(*Start == '=' && "Bad plural expression syntax: expected =");
517 ++Start;
518 unsigned ValMod = ValNo % Arg;
519 if (TestPluralRange(ValMod, Start, End))
520 return true;
521 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000522 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000523 "Bad plural expression syntax: unexpected character");
524 // Range expression
525 if (TestPluralRange(ValNo, Start, End))
526 return true;
527 }
528
529 // Scan for next or-expr part.
530 Start = std::find(Start, End, ',');
531 if(Start == End)
532 break;
533 ++Start;
534 }
535 return false;
536}
537
538/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
539/// for complex plural forms, or in languages where all plurals are complex.
540/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
541/// conditions that are tested in order, the form corresponding to the first
542/// that applies being emitted. The empty condition is always true, making the
543/// last form a default case.
544/// Conditions are simple boolean expressions, where n is the number argument.
545/// Here are the rules.
546/// condition := expression | empty
547/// empty := -> always true
548/// expression := numeric [',' expression] -> logical or
549/// numeric := range -> true if n in range
550/// | '%' number '=' range -> true if n % number in range
551/// range := number
552/// | '[' number ',' number ']' -> ranges are inclusive both ends
553///
554/// Here are some examples from the GNU gettext manual written in this form:
555/// English:
556/// {1:form0|:form1}
557/// Latvian:
558/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
559/// Gaeilge:
560/// {1:form0|2:form1|:form2}
561/// Romanian:
562/// {1:form0|0,%100=[1,19]:form1|:form2}
563/// Lithuanian:
564/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
565/// Russian (requires repeated form):
566/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
567/// Slovak
568/// {1:form0|[2,4]:form1|:form2}
569/// Polish (requires repeated form):
570/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
571static void HandlePluralModifier(unsigned ValNo,
572 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb54b2762009-04-16 05:04:32 +0000573 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000574 const char *ArgumentEnd = Argument + ArgumentLen;
575 while (1) {
576 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
577 const char *ExprEnd = Argument;
578 while (*ExprEnd != ':') {
579 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
580 ++ExprEnd;
581 }
582 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
583 Argument = ExprEnd + 1;
584 ExprEnd = std::find(Argument, ArgumentEnd, '|');
585 OutStr.append(Argument, ExprEnd);
586 return;
587 }
588 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
589 }
590}
591
592
Chris Lattnerf4c83962008-11-19 06:51:40 +0000593/// FormatDiagnostic - Format this diagnostic into a string, substituting the
594/// formal arguments into the %0 slots. The result is appended onto the Str
595/// array.
596void DiagnosticInfo::
597FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
598 const char *DiagStr = getDiags()->getDescription(getID());
599 const char *DiagEnd = DiagStr+strlen(DiagStr);
Nico Weber7bfaaae2008-08-10 19:59:06 +0000600
Chris Lattnerf4c83962008-11-19 06:51:40 +0000601 while (DiagStr != DiagEnd) {
602 if (DiagStr[0] != '%') {
603 // Append non-%0 substrings to Str if we have one.
604 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
605 OutStr.append(DiagStr, StrEnd);
606 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000607 continue;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000608 } else if (DiagStr[1] == '%') {
609 OutStr.push_back('%'); // %% -> %.
610 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000611 continue;
612 }
613
614 // Skip the %.
615 ++DiagStr;
616
617 // This must be a placeholder for a diagnostic argument. The format for a
618 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
619 // The digit is a number from 0-9 indicating which argument this comes from.
620 // The modifier is a string of digits from the set [-a-z]+, arguments is a
621 // brace enclosed string.
622 const char *Modifier = 0, *Argument = 0;
623 unsigned ModifierLen = 0, ArgumentLen = 0;
624
625 // Check to see if we have a modifier. If so eat it.
626 if (!isdigit(DiagStr[0])) {
627 Modifier = DiagStr;
628 while (DiagStr[0] == '-' ||
629 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
630 ++DiagStr;
631 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000632
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000633 // If we have an argument, get it next.
634 if (DiagStr[0] == '{') {
635 ++DiagStr; // Skip {.
636 Argument = DiagStr;
637
638 for (; DiagStr[0] != '}'; ++DiagStr)
639 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
640 ArgumentLen = DiagStr-Argument;
641 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000642 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000643 }
644
645 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000646 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000647
Chris Lattner22caddc2008-11-23 09:13:29 +0000648 switch (getArgKind(ArgNo)) {
Chris Lattner08631c52008-11-23 21:45:46 +0000649 // ---- STRINGS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000650 case Diagnostic::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000651 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000652 assert(ModifierLen == 0 && "No modifiers for strings yet");
653 OutStr.append(S.begin(), S.end());
654 break;
655 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000656 case Diagnostic::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000657 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000658 assert(ModifierLen == 0 && "No modifiers for strings yet");
659 OutStr.append(S, S + strlen(S));
660 break;
661 }
Chris Lattner08631c52008-11-23 21:45:46 +0000662 // ---- INTEGERS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000663 case Diagnostic::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000664 int Val = getArgSInt(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000665
666 if (ModifierIs(Modifier, ModifierLen, "select")) {
667 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
668 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
669 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000670 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
671 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000672 } else {
673 assert(ModifierLen == 0 && "Unknown integer modifier");
Chris Lattner30bc9652008-11-19 07:22:31 +0000674 // FIXME: Optimize
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000675 std::string S = llvm::itostr(Val);
Chris Lattner30bc9652008-11-19 07:22:31 +0000676 OutStr.append(S.begin(), S.end());
Chris Lattner30bc9652008-11-19 07:22:31 +0000677 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000678 break;
679 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000680 case Diagnostic::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000681 unsigned Val = getArgUInt(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000682
683 if (ModifierIs(Modifier, ModifierLen, "select")) {
684 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
685 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
686 HandleIntegerSModifier(Val, OutStr);
Sebastian 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");
691
Chris Lattner30bc9652008-11-19 07:22:31 +0000692 // FIXME: Optimize
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000693 std::string S = llvm::utostr_32(Val);
Chris Lattner30bc9652008-11-19 07:22:31 +0000694 OutStr.append(S.begin(), S.end());
Chris Lattner30bc9652008-11-19 07:22:31 +0000695 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000696 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000697 }
Chris Lattner08631c52008-11-23 21:45:46 +0000698 // ---- NAMES and TYPES ----
699 case Diagnostic::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000700 const IdentifierInfo *II = getArgIdentifier(ArgNo);
701 assert(ModifierLen == 0 && "No modifiers for strings yet");
Chris Lattnerd0344a42009-02-19 23:45:49 +0000702 OutStr.push_back('\'');
Chris Lattner08631c52008-11-23 21:45:46 +0000703 OutStr.append(II->getName(), II->getName() + II->getLength());
704 OutStr.push_back('\'');
705 break;
706 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000707 case Diagnostic::ak_qualtype:
Chris Lattner011bb4e2008-11-23 20:28:15 +0000708 case Diagnostic::ak_declarationname:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000709 case Diagnostic::ak_nameddecl:
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000710 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
711 Modifier, ModifierLen,
712 Argument, ArgumentLen, OutStr);
Chris Lattner22caddc2008-11-23 09:13:29 +0000713 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000714 }
715 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000716}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000717
718/// IncludeInDiagnosticCounts - This method (whose default implementation
719/// returns true) indicates whether the diagnostics handled by this
720/// DiagnosticClient should be included in the number of diagnostics
721/// reported by Diagnostic.
722bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }