blob: 922702fdf51118d27ff5aff8c446ec0f360f916f [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;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000049 bool SFINAE : 1;
Chris Lattner121f60c2009-04-16 06:07:15 +000050 const char *Description;
Chris Lattner33dd2822009-04-16 06:00:24 +000051 const char *OptionGroup;
Chris Lattner87d854e2009-04-16 06:13:46 +000052
53 bool operator<(const StaticDiagInfoRec &RHS) const {
54 return DiagID < RHS.DiagID;
55 }
56 bool operator>(const StaticDiagInfoRec &RHS) const {
57 return DiagID > RHS.DiagID;
58 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +000059};
60
Chris Lattner33dd2822009-04-16 06:00:24 +000061static const StaticDiagInfoRec StaticDiagInfo[] = {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000062#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,SFINAE) \
63 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, DESC, GROUP },
Chris Lattner27ceb9d2009-04-15 07:01:18 +000064#include "clang/Basic/DiagnosticCommonKinds.inc"
65#include "clang/Basic/DiagnosticDriverKinds.inc"
66#include "clang/Basic/DiagnosticFrontendKinds.inc"
67#include "clang/Basic/DiagnosticLexKinds.inc"
68#include "clang/Basic/DiagnosticParseKinds.inc"
69#include "clang/Basic/DiagnosticASTKinds.inc"
70#include "clang/Basic/DiagnosticSemaKinds.inc"
71#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000072 { 0, 0, 0, 0, 0, 0}
Chris Lattner27ceb9d2009-04-15 07:01:18 +000073};
Chris Lattner8a941e02009-04-15 16:56:26 +000074#undef DIAG
Chris Lattner27ceb9d2009-04-15 07:01:18 +000075
Chris Lattner87d854e2009-04-16 06:13:46 +000076/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
77/// or null if the ID is invalid.
Chris Lattner33dd2822009-04-16 06:00:24 +000078static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner87d854e2009-04-16 06:13:46 +000079 unsigned NumDiagEntries = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
80
81 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
82#ifndef NDEBUG
83 static bool IsFirst = true;
84 if (IsFirst) {
85 for (unsigned i = 1; i != NumDiagEntries; ++i)
86 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
87 "Improperly sorted diag info");
88 IsFirst = false;
89 }
90#endif
91
92 // Search the diagnostic table with a binary search.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000093 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0 };
Chris Lattner87d854e2009-04-16 06:13:46 +000094
95 const StaticDiagInfoRec *Found =
96 std::lower_bound(StaticDiagInfo, StaticDiagInfo + NumDiagEntries, Find);
97 if (Found == StaticDiagInfo + NumDiagEntries ||
98 Found->DiagID != DiagID)
99 return 0;
100
101 return Found;
Chris Lattner33dd2822009-04-16 06:00:24 +0000102}
103
104static unsigned GetDefaultDiagMapping(unsigned DiagID) {
105 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Chris Lattner121f60c2009-04-16 06:07:15 +0000106 return Info->Mapping;
Chris Lattner691f1ae2009-04-16 04:12:40 +0000107 return diag::MAP_FATAL;
108}
109
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000110/// getWarningOptionForDiag - Return the lowest-level warning option that
111/// enables the specified diagnostic. If there is no -Wfoo flag that controls
112/// the diagnostic, this returns null.
113const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) {
Chris Lattner33dd2822009-04-16 06:00:24 +0000114 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
115 return Info->OptionGroup;
116 return 0;
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000117}
118
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000119bool Diagnostic::isBuiltinSFINAEDiag(unsigned DiagID) {
120 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Douglas Gregor8439fac2009-06-15 16:52:15 +0000121 return Info->SFINAE && Info->Class == CLASS_ERROR;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000122 return false;
123}
124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125/// getDiagClass - Return the class field of the diagnostic.
126///
Chris Lattner07506182007-11-30 22:53:43 +0000127static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner121f60c2009-04-16 06:07:15 +0000128 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
129 return Info->Class;
130 return ~0U;
Reid Spencer5f016e22007-07-11 17:01:13 +0000131}
132
Chris Lattner182745a2007-12-02 01:09:57 +0000133//===----------------------------------------------------------------------===//
134// Custom Diagnostic information
135//===----------------------------------------------------------------------===//
136
137namespace clang {
138 namespace diag {
139 class CustomDiagInfo {
140 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
141 std::vector<DiagDesc> DiagInfo;
142 std::map<DiagDesc, unsigned> DiagIDs;
143 public:
144
145 /// getDescription - Return the description of the specified custom
146 /// diagnostic.
147 const char *getDescription(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].second.c_str();
Chris Lattner182745a2007-12-02 01:09:57 +0000151 }
152
153 /// getLevel - Return the level of the specified custom diagnostic.
154 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000155 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000156 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000157 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattner182745a2007-12-02 01:09:57 +0000158 }
159
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000160 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
161 Diagnostic &Diags) {
Chris Lattner182745a2007-12-02 01:09:57 +0000162 DiagDesc D(L, Message);
163 // Check to see if it already exists.
164 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
165 if (I != DiagIDs.end() && I->first == D)
166 return I->second;
167
168 // If not, assign a new ID.
Chris Lattner88eccaf2009-01-29 06:55:46 +0000169 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattner182745a2007-12-02 01:09:57 +0000170 DiagIDs.insert(std::make_pair(D, ID));
171 DiagInfo.push_back(D);
172 return ID;
173 }
174 };
175
176 } // end diag namespace
177} // end clang namespace
178
179
180//===----------------------------------------------------------------------===//
181// Common Diagnostic implementation
182//===----------------------------------------------------------------------===//
183
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000184static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
185 const char *Modifier, unsigned ML,
186 const char *Argument, unsigned ArgLen,
Chris Lattner92dd3862009-02-19 23:53:20 +0000187 llvm::SmallVectorImpl<char> &Output,
188 void *Cookie) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000189 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +0000190 Output.append(Str, Str+strlen(Str));
191}
192
193
Ted Kremenekb4398aa2008-08-07 17:49:57 +0000194Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000195 AllExtensionsSilenced = 0;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000196 IgnoreAllWarnings = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 WarningsAsErrors = false;
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000198 SuppressSystemWarnings = false;
Chris Lattnerb54b2762009-04-16 05:04:32 +0000199 ExtBehavior = Ext_Ignore;
Reid Spencer5f016e22007-07-11 17:01:13 +0000200
201 ErrorOccurred = false;
Chris Lattner15221422009-02-06 04:16:02 +0000202 FatalErrorOccurred = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 NumDiagnostics = 0;
Chris Lattner04ae2df2009-07-12 21:18:45 +0000204
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 NumErrors = 0;
Chris Lattner182745a2007-12-02 01:09:57 +0000206 CustomDiagInfo = 0;
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000207 CurDiagID = ~0U;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000208 LastDiagLevel = Ignored;
Chris Lattner22caddc2008-11-23 09:13:29 +0000209
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000210 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +0000211 ArgToStringCookie = 0;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000212
Chris Lattner691f1ae2009-04-16 04:12:40 +0000213 // Set all mappings to 'unset'.
Chris Lattner04ae2df2009-07-12 21:18:45 +0000214 DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0);
215 DiagMappingsStack.push_back(BlankDiags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000216}
217
Chris Lattner182745a2007-12-02 01:09:57 +0000218Diagnostic::~Diagnostic() {
219 delete CustomDiagInfo;
220}
221
Chris Lattner04ae2df2009-07-12 21:18:45 +0000222
223void Diagnostic::pushMappings() {
224 DiagMappingsStack.push_back(DiagMappingsStack.back());
225}
226
227bool Diagnostic::popMappings() {
228 if (DiagMappingsStack.size() == 1)
229 return false;
230
231 DiagMappingsStack.pop_back();
232 return true;
233}
234
Chris Lattner182745a2007-12-02 01:09:57 +0000235/// getCustomDiagID - Return an ID for a diagnostic with the specified message
236/// and level. If this is the first request for this diagnosic, it is
237/// registered and created, otherwise the existing ID is returned.
238unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
239 if (CustomDiagInfo == 0)
240 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000241 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattner182745a2007-12-02 01:09:57 +0000242}
243
244
Chris Lattnerf5d23282009-02-17 06:49:55 +0000245/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
246/// level of the specified diagnostic ID is a Warning or Extension.
247/// This only works on builtin diagnostics, not custom ones, and is not legal to
248/// call on NOTEs.
249bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000250 return DiagID < diag::DIAG_UPPER_LIMIT &&
251 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Reid Spencer5f016e22007-07-11 17:01:13 +0000252}
253
Douglas Gregoree1828a2009-03-10 18:03:33 +0000254/// \brief Determine whether the given built-in diagnostic ID is a
255/// Note.
256bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000257 return DiagID < diag::DIAG_UPPER_LIMIT &&
258 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000259}
260
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000261/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
262/// ID is for an extension of some sort.
263///
264bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000265 return DiagID < diag::DIAG_UPPER_LIMIT &&
266 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000267}
268
Reid Spencer5f016e22007-07-11 17:01:13 +0000269
270/// getDescription - Given a diagnostic ID, return a description of the
271/// issue.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000272const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner121f60c2009-04-16 06:07:15 +0000273 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
274 return Info->Description;
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000275 return CustomDiagInfo->getDescription(DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000276}
277
278/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
279/// object, classify the specified diagnostic ID into a Level, consumable by
280/// the DiagnosticClient.
281Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattner182745a2007-12-02 01:09:57 +0000282 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000283 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattner182745a2007-12-02 01:09:57 +0000284 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner07506182007-11-30 22:53:43 +0000285
286 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000287 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerf5d23282009-02-17 06:49:55 +0000288 return getDiagnosticLevel(DiagID, DiagClass);
289}
290
291/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
292/// object, classify the specified diagnostic ID into a Level, consumable by
293/// the DiagnosticClient.
294Diagnostic::Level
295Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerf5d23282009-02-17 06:49:55 +0000297 // to error. Errors can only be mapped to fatal.
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000298 Diagnostic::Level Result = Diagnostic::Fatal;
Chris Lattner691f1ae2009-04-16 04:12:40 +0000299
300 // Get the mapping information, if unset, compute it lazily.
301 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
302 if (MappingInfo == 0) {
303 MappingInfo = GetDefaultDiagMapping(DiagID);
304 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
305 }
306
307 switch (MappingInfo & 7) {
308 default: assert(0 && "Unknown mapping!");
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000309 case diag::MAP_IGNORE:
Chris Lattnerb54b2762009-04-16 05:04:32 +0000310 // Ignore this, unless this is an extension diagnostic and we're mapping
311 // them onto warnings or errors.
312 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
313 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
314 (MappingInfo & 8) != 0) // User explicitly mapped it.
315 return Diagnostic::Ignored;
316 Result = Diagnostic::Warning;
317 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
318 break;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000319 case diag::MAP_ERROR:
320 Result = Diagnostic::Error;
321 break;
322 case diag::MAP_FATAL:
323 Result = Diagnostic::Fatal;
324 break;
325 case diag::MAP_WARNING:
326 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner5b4681c2008-05-29 15:36:45 +0000327 if (IgnoreAllWarnings)
328 return Diagnostic::Ignored;
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000329
330 Result = Diagnostic::Warning;
Chris Lattnerb54b2762009-04-16 05:04:32 +0000331
332 // If this is an extension diagnostic and we're in -pedantic-error mode, and
333 // if the user didn't explicitly map it, upgrade to an error.
334 if (ExtBehavior == Ext_Error &&
335 (MappingInfo & 8) == 0 &&
336 isBuiltinExtensionDiag(DiagID))
337 Result = Diagnostic::Error;
338
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000339 if (WarningsAsErrors)
340 Result = Diagnostic::Error;
341 break;
Chris Lattnerb54b2762009-04-16 05:04:32 +0000342
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000343 case diag::MAP_WARNING_NO_WERROR:
344 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
345 // not be adjusted by -Werror or -pedantic-errors.
346 Result = Diagnostic::Warning;
347
348 // If warnings are globally mapped to ignore or error, do it.
349 if (IgnoreAllWarnings)
350 return Diagnostic::Ignored;
351
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000352 break;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000353 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000354
355 // Okay, we're about to return this as a "diagnostic to emit" one last check:
356 // if this is any sort of extension warning, and if we're in an __extension__
357 // block, silence it.
358 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
359 return Diagnostic::Ignored;
Reid Spencer5f016e22007-07-11 17:01:13 +0000360
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000361 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000362}
363
Chris Lattner3bc172b2009-04-19 22:34:23 +0000364struct WarningOption {
365 const char *Name;
366 const short *Members;
367 const char *SubGroups;
368};
369
370#define GET_DIAG_ARRAYS
371#include "clang/Basic/DiagnosticGroups.inc"
372#undef GET_DIAG_ARRAYS
373
374// Second the table of options, sorted by name for fast binary lookup.
375static const WarningOption OptionTable[] = {
376#define GET_DIAG_TABLE
377#include "clang/Basic/DiagnosticGroups.inc"
378#undef GET_DIAG_TABLE
379};
380static const size_t OptionTableSize =
381sizeof(OptionTable) / sizeof(OptionTable[0]);
382
383static bool WarningOptionCompare(const WarningOption &LHS,
384 const WarningOption &RHS) {
385 return strcmp(LHS.Name, RHS.Name) < 0;
386}
387
388static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
389 Diagnostic &Diags) {
390 // Option exists, poke all the members of its diagnostic set.
391 if (const short *Member = Group->Members) {
392 for (; *Member != -1; ++Member)
393 Diags.setDiagnosticMapping(*Member, Mapping);
394 }
395
396 // Enable/disable all subgroups along with this one.
397 if (const char *SubGroups = Group->SubGroups) {
398 for (; *SubGroups != (char)-1; ++SubGroups)
399 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
400 }
401}
402
403/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
404/// "unknown-pragmas" to have the specified mapping. This returns true and
405/// ignores the request if "Group" was unknown, false otherwise.
406bool Diagnostic::setDiagnosticGroupMapping(const char *Group,
407 diag::Mapping Map) {
408
409 WarningOption Key = { Group, 0, 0 };
410 const WarningOption *Found =
411 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
412 WarningOptionCompare);
413 if (Found == OptionTable + OptionTableSize ||
414 strcmp(Found->Name, Group) != 0)
415 return true; // Option not found.
416
417 MapGroupMembers(Found, Map, *this);
418 return false;
419}
420
421
Chris Lattner0a14eee2008-11-18 07:04:44 +0000422/// ProcessDiag - This is the method used to report a diagnostic that is
423/// finally fully formed.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000424bool Diagnostic::ProcessDiag() {
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000425 DiagnosticInfo Info(this);
Douglas Gregor525c4b02009-03-19 18:55:06 +0000426
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 // Figure out the diagnostic level of this message.
Chris Lattnerf5d23282009-02-17 06:49:55 +0000428 Diagnostic::Level DiagLevel;
429 unsigned DiagID = Info.getID();
Reid Spencer5f016e22007-07-11 17:01:13 +0000430
Chris Lattnerf5d23282009-02-17 06:49:55 +0000431 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
432 // in a system header.
433 bool ShouldEmitInSystemHeader;
434
435 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
436 // Handle custom diagnostics, which cannot be mapped.
437 DiagLevel = CustomDiagInfo->getLevel(DiagID);
438
439 // Custom diagnostics always are emitted in system headers.
440 ShouldEmitInSystemHeader = true;
441 } else {
442 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
443 // the diagnostic level was for the previous diagnostic so that it is
444 // filtered the same as the previous diagnostic.
445 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000446 if (DiagClass == CLASS_NOTE) {
Chris Lattnerf5d23282009-02-17 06:49:55 +0000447 DiagLevel = Diagnostic::Note;
448 ShouldEmitInSystemHeader = false; // extra consideration is needed
449 } else {
450 // If this is not an error and we are in a system header, we ignore it.
451 // Check the original Diag ID here, because we also want to ignore
452 // extensions and warnings in -Werror and -pedantic-errors modes, which
453 // *map* warnings/extensions to errors.
Chris Lattner8a941e02009-04-15 16:56:26 +0000454 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Chris Lattnerf5d23282009-02-17 06:49:55 +0000455
456 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
457 }
458 }
459
Douglas Gregor525c4b02009-03-19 18:55:06 +0000460 if (DiagLevel != Diagnostic::Note) {
461 // Record that a fatal error occurred only when we see a second
462 // non-note diagnostic. This allows notes to be attached to the
463 // fatal error, but suppresses any diagnostics that follow those
464 // notes.
465 if (LastDiagLevel == Diagnostic::Fatal)
466 FatalErrorOccurred = true;
467
Chris Lattnerf5d23282009-02-17 06:49:55 +0000468 LastDiagLevel = DiagLevel;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000469 }
470
471 // If a fatal error has already been emitted, silence all subsequent
472 // diagnostics.
473 if (FatalErrorOccurred)
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000474 return false;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000475
Chris Lattnerf5d23282009-02-17 06:49:55 +0000476 // If the client doesn't care about this message, don't issue it. If this is
477 // a note and the last real diagnostic was ignored, ignore it too.
478 if (DiagLevel == Diagnostic::Ignored ||
479 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000480 return false;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000481
Chris Lattnerf5d23282009-02-17 06:49:55 +0000482 // If this diagnostic is in a system header and is not a clang error, suppress
483 // it.
484 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner0a14eee2008-11-18 07:04:44 +0000485 Info.getLocation().isValid() &&
Chris Lattnerf5d23282009-02-17 06:49:55 +0000486 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner336f26b2009-02-17 06:52:20 +0000487 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
488 LastDiagLevel = Diagnostic::Ignored;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000489 return false;
Chris Lattner336f26b2009-02-17 06:52:20 +0000490 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000491
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 if (DiagLevel >= Diagnostic::Error) {
493 ErrorOccurred = true;
Chris Lattner0a14eee2008-11-18 07:04:44 +0000494 ++NumErrors;
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000496
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 // Finally, report it.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000498 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekcabe6682009-01-23 20:28:53 +0000499 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000500
Douglas Gregoree1828a2009-03-10 18:03:33 +0000501 CurDiagID = ~0U;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000502
503 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000504}
505
Nico Weber7bfaaae2008-08-10 19:59:06 +0000506
Reid Spencer5f016e22007-07-11 17:01:13 +0000507DiagnosticClient::~DiagnosticClient() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000508
Chris Lattnerf4c83962008-11-19 06:51:40 +0000509
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000510/// ModifierIs - Return true if the specified modifier matches specified string.
511template <std::size_t StrLen>
512static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
513 const char (&Str)[StrLen]) {
514 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
515}
516
517/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
518/// like this: %select{foo|bar|baz}2. This means that the integer argument
519/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
520/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
521/// This is very useful for certain classes of variant diagnostics.
522static void HandleSelectModifier(unsigned ValNo,
523 const char *Argument, unsigned ArgumentLen,
524 llvm::SmallVectorImpl<char> &OutStr) {
525 const char *ArgumentEnd = Argument+ArgumentLen;
526
527 // Skip over 'ValNo' |'s.
528 while (ValNo) {
529 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
530 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
531 " larger than the number of options in the diagnostic string!");
532 Argument = NextVal+1; // Skip this string.
533 --ValNo;
534 }
535
536 // Get the end of the value. This is either the } or the |.
537 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
538 // Add the value to the output string.
539 OutStr.append(Argument, EndPtr);
540}
541
542/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
543/// letter 's' to the string if the value is not 1. This is used in cases like
544/// this: "you idiot, you have %4 parameter%s4!".
545static void HandleIntegerSModifier(unsigned ValNo,
546 llvm::SmallVectorImpl<char> &OutStr) {
547 if (ValNo != 1)
548 OutStr.push_back('s');
549}
550
551
Sebastian Redle4c452c2008-11-22 13:44:36 +0000552/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000553static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000554 // Programming 101: Parse a decimal number :-)
555 unsigned Val = 0;
556 while (Start != End && *Start >= '0' && *Start <= '9') {
557 Val *= 10;
558 Val += *Start - '0';
559 ++Start;
560 }
561 return Val;
562}
563
564/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000565static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000566 if (*Start != '[') {
567 unsigned Ref = PluralNumber(Start, End);
568 return Ref == Val;
569 }
570
571 ++Start;
572 unsigned Low = PluralNumber(Start, End);
573 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
574 ++Start;
575 unsigned High = PluralNumber(Start, End);
576 assert(*Start == ']' && "Bad plural expression syntax: expected )");
577 ++Start;
578 return Low <= Val && Val <= High;
579}
580
581/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000582static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000583 // Empty condition?
584 if (*Start == ':')
585 return true;
586
587 while (1) {
588 char C = *Start;
589 if (C == '%') {
590 // Modulo expression
591 ++Start;
592 unsigned Arg = PluralNumber(Start, End);
593 assert(*Start == '=' && "Bad plural expression syntax: expected =");
594 ++Start;
595 unsigned ValMod = ValNo % Arg;
596 if (TestPluralRange(ValMod, Start, End))
597 return true;
598 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000599 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000600 "Bad plural expression syntax: unexpected character");
601 // Range expression
602 if (TestPluralRange(ValNo, Start, End))
603 return true;
604 }
605
606 // Scan for next or-expr part.
607 Start = std::find(Start, End, ',');
608 if(Start == End)
609 break;
610 ++Start;
611 }
612 return false;
613}
614
615/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
616/// for complex plural forms, or in languages where all plurals are complex.
617/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
618/// conditions that are tested in order, the form corresponding to the first
619/// that applies being emitted. The empty condition is always true, making the
620/// last form a default case.
621/// Conditions are simple boolean expressions, where n is the number argument.
622/// Here are the rules.
623/// condition := expression | empty
624/// empty := -> always true
625/// expression := numeric [',' expression] -> logical or
626/// numeric := range -> true if n in range
627/// | '%' number '=' range -> true if n % number in range
628/// range := number
629/// | '[' number ',' number ']' -> ranges are inclusive both ends
630///
631/// Here are some examples from the GNU gettext manual written in this form:
632/// English:
633/// {1:form0|:form1}
634/// Latvian:
635/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
636/// Gaeilge:
637/// {1:form0|2:form1|:form2}
638/// Romanian:
639/// {1:form0|0,%100=[1,19]:form1|:form2}
640/// Lithuanian:
641/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
642/// Russian (requires repeated form):
643/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
644/// Slovak
645/// {1:form0|[2,4]:form1|:form2}
646/// Polish (requires repeated form):
647/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
648static void HandlePluralModifier(unsigned ValNo,
649 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb54b2762009-04-16 05:04:32 +0000650 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000651 const char *ArgumentEnd = Argument + ArgumentLen;
652 while (1) {
653 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
654 const char *ExprEnd = Argument;
655 while (*ExprEnd != ':') {
656 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
657 ++ExprEnd;
658 }
659 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
660 Argument = ExprEnd + 1;
661 ExprEnd = std::find(Argument, ArgumentEnd, '|');
662 OutStr.append(Argument, ExprEnd);
663 return;
664 }
665 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
666 }
667}
668
669
Chris Lattnerf4c83962008-11-19 06:51:40 +0000670/// FormatDiagnostic - Format this diagnostic into a string, substituting the
671/// formal arguments into the %0 slots. The result is appended onto the Str
672/// array.
673void DiagnosticInfo::
674FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
675 const char *DiagStr = getDiags()->getDescription(getID());
676 const char *DiagEnd = DiagStr+strlen(DiagStr);
Nico Weber7bfaaae2008-08-10 19:59:06 +0000677
Chris Lattnerf4c83962008-11-19 06:51:40 +0000678 while (DiagStr != DiagEnd) {
679 if (DiagStr[0] != '%') {
680 // Append non-%0 substrings to Str if we have one.
681 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
682 OutStr.append(DiagStr, StrEnd);
683 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000684 continue;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000685 } else if (DiagStr[1] == '%') {
686 OutStr.push_back('%'); // %% -> %.
687 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000688 continue;
689 }
690
691 // Skip the %.
692 ++DiagStr;
693
694 // This must be a placeholder for a diagnostic argument. The format for a
695 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
696 // The digit is a number from 0-9 indicating which argument this comes from.
697 // The modifier is a string of digits from the set [-a-z]+, arguments is a
698 // brace enclosed string.
699 const char *Modifier = 0, *Argument = 0;
700 unsigned ModifierLen = 0, ArgumentLen = 0;
701
702 // Check to see if we have a modifier. If so eat it.
703 if (!isdigit(DiagStr[0])) {
704 Modifier = DiagStr;
705 while (DiagStr[0] == '-' ||
706 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
707 ++DiagStr;
708 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000709
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000710 // If we have an argument, get it next.
711 if (DiagStr[0] == '{') {
712 ++DiagStr; // Skip {.
713 Argument = DiagStr;
714
715 for (; DiagStr[0] != '}'; ++DiagStr)
716 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
717 ArgumentLen = DiagStr-Argument;
718 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000719 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000720 }
721
722 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000723 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000724
Chris Lattner22caddc2008-11-23 09:13:29 +0000725 switch (getArgKind(ArgNo)) {
Chris Lattner08631c52008-11-23 21:45:46 +0000726 // ---- STRINGS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000727 case Diagnostic::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000728 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000729 assert(ModifierLen == 0 && "No modifiers for strings yet");
730 OutStr.append(S.begin(), S.end());
731 break;
732 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000733 case Diagnostic::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000734 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000735 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000736
737 // Don't crash if get passed a null pointer by accident.
738 if (!S)
739 S = "(null)";
740
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000741 OutStr.append(S, S + strlen(S));
742 break;
743 }
Chris Lattner08631c52008-11-23 21:45:46 +0000744 // ---- INTEGERS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000745 case Diagnostic::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000746 int Val = getArgSInt(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000747
748 if (ModifierIs(Modifier, ModifierLen, "select")) {
749 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
750 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
751 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000752 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
753 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000754 } else {
755 assert(ModifierLen == 0 && "Unknown integer modifier");
Chris Lattner30bc9652008-11-19 07:22:31 +0000756 // FIXME: Optimize
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000757 std::string S = llvm::itostr(Val);
Chris Lattner30bc9652008-11-19 07:22:31 +0000758 OutStr.append(S.begin(), S.end());
Chris Lattner30bc9652008-11-19 07:22:31 +0000759 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000760 break;
761 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000762 case Diagnostic::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000763 unsigned Val = getArgUInt(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000764
765 if (ModifierIs(Modifier, ModifierLen, "select")) {
766 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
767 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
768 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000769 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
770 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000771 } else {
772 assert(ModifierLen == 0 && "Unknown integer modifier");
773
Chris Lattner30bc9652008-11-19 07:22:31 +0000774 // FIXME: Optimize
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000775 std::string S = llvm::utostr_32(Val);
Chris Lattner30bc9652008-11-19 07:22:31 +0000776 OutStr.append(S.begin(), S.end());
Chris Lattner30bc9652008-11-19 07:22:31 +0000777 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000778 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000779 }
Chris Lattner08631c52008-11-23 21:45:46 +0000780 // ---- NAMES and TYPES ----
781 case Diagnostic::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000782 const IdentifierInfo *II = getArgIdentifier(ArgNo);
783 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000784
785 // Don't crash if get passed a null pointer by accident.
786 if (!II) {
787 const char *S = "(null)";
788 OutStr.append(S, S + strlen(S));
789 continue;
790 }
791
Chris Lattnerd0344a42009-02-19 23:45:49 +0000792 OutStr.push_back('\'');
Chris Lattner08631c52008-11-23 21:45:46 +0000793 OutStr.append(II->getName(), II->getName() + II->getLength());
794 OutStr.push_back('\'');
795 break;
796 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000797 case Diagnostic::ak_qualtype:
Chris Lattner011bb4e2008-11-23 20:28:15 +0000798 case Diagnostic::ak_declarationname:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000799 case Diagnostic::ak_nameddecl:
Douglas Gregordacd4342009-08-26 00:04:55 +0000800 case Diagnostic::ak_nestednamespec:
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000801 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
802 Modifier, ModifierLen,
803 Argument, ArgumentLen, OutStr);
Chris Lattner22caddc2008-11-23 09:13:29 +0000804 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000805 }
806 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000807}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000808
809/// IncludeInDiagnosticCounts - This method (whose default implementation
810/// returns true) indicates whether the diagnostics handled by this
811/// DiagnosticClient should be included in the number of diagnostics
812/// reported by Diagnostic.
813bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }