blob: d665e49126d3bb1f1f8baeb2a5dbbf29faf0a6f5 [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"
Daniel Dunbar23e47c62009-10-17 18:12:14 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattner182745a2007-12-02 01:09:57 +000029#include <vector>
30#include <map>
Chris Lattner87cf5ac2008-03-10 17:04:53 +000031#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000032using namespace clang;
33
Chris Lattner182745a2007-12-02 01:09:57 +000034//===----------------------------------------------------------------------===//
35// Builtin Diagnostic information
36//===----------------------------------------------------------------------===//
37
Chris Lattner121f60c2009-04-16 06:07:15 +000038// Diagnostic classes.
39enum {
40 CLASS_NOTE = 0x01,
41 CLASS_WARNING = 0x02,
42 CLASS_EXTENSION = 0x03,
43 CLASS_ERROR = 0x04
44};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000045
Chris Lattner33dd2822009-04-16 06:00:24 +000046struct StaticDiagInfoRec {
Chris Lattner121f60c2009-04-16 06:07:15 +000047 unsigned short DiagID;
48 unsigned Mapping : 3;
49 unsigned Class : 3;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000050 bool SFINAE : 1;
Chris Lattner121f60c2009-04-16 06:07:15 +000051 const char *Description;
Chris Lattner33dd2822009-04-16 06:00:24 +000052 const char *OptionGroup;
Mike Stump1eb44332009-09-09 15:08:12 +000053
Chris Lattner87d854e2009-04-16 06:13:46 +000054 bool operator<(const StaticDiagInfoRec &RHS) const {
55 return DiagID < RHS.DiagID;
56 }
57 bool operator>(const StaticDiagInfoRec &RHS) const {
58 return DiagID > RHS.DiagID;
59 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +000060};
61
Chris Lattner33dd2822009-04-16 06:00:24 +000062static const StaticDiagInfoRec StaticDiagInfo[] = {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000063#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,SFINAE) \
64 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, DESC, GROUP },
Chris Lattner27ceb9d2009-04-15 07:01:18 +000065#include "clang/Basic/DiagnosticCommonKinds.inc"
66#include "clang/Basic/DiagnosticDriverKinds.inc"
67#include "clang/Basic/DiagnosticFrontendKinds.inc"
68#include "clang/Basic/DiagnosticLexKinds.inc"
69#include "clang/Basic/DiagnosticParseKinds.inc"
70#include "clang/Basic/DiagnosticASTKinds.inc"
71#include "clang/Basic/DiagnosticSemaKinds.inc"
72#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000073 { 0, 0, 0, 0, 0, 0}
Chris Lattner27ceb9d2009-04-15 07:01:18 +000074};
Chris Lattner8a941e02009-04-15 16:56:26 +000075#undef DIAG
Chris Lattner27ceb9d2009-04-15 07:01:18 +000076
Chris Lattner87d854e2009-04-16 06:13:46 +000077/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
78/// or null if the ID is invalid.
Chris Lattner33dd2822009-04-16 06:00:24 +000079static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner87d854e2009-04-16 06:13:46 +000080 unsigned NumDiagEntries = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
81
82 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
83#ifndef NDEBUG
84 static bool IsFirst = true;
85 if (IsFirst) {
Chris Lattner5a3ce9b2009-10-16 02:34:51 +000086 for (unsigned i = 1; i != NumDiagEntries; ++i) {
87 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
88 "Diag ID conflict, the enums at the start of clang::diag (in "
89 "Diagnostic.h) probably need to be increased");
90
Chris Lattner87d854e2009-04-16 06:13:46 +000091 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
92 "Improperly sorted diag info");
Chris Lattner5a3ce9b2009-10-16 02:34:51 +000093 }
Chris Lattner87d854e2009-04-16 06:13:46 +000094 IsFirst = false;
95 }
96#endif
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner87d854e2009-04-16 06:13:46 +000098 // Search the diagnostic table with a binary search.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000099 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0 };
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattner87d854e2009-04-16 06:13:46 +0000101 const StaticDiagInfoRec *Found =
102 std::lower_bound(StaticDiagInfo, StaticDiagInfo + NumDiagEntries, Find);
103 if (Found == StaticDiagInfo + NumDiagEntries ||
104 Found->DiagID != DiagID)
105 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattner87d854e2009-04-16 06:13:46 +0000107 return Found;
Chris Lattner33dd2822009-04-16 06:00:24 +0000108}
109
110static unsigned GetDefaultDiagMapping(unsigned DiagID) {
111 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Chris Lattner121f60c2009-04-16 06:07:15 +0000112 return Info->Mapping;
Chris Lattner691f1ae2009-04-16 04:12:40 +0000113 return diag::MAP_FATAL;
114}
115
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000116/// getWarningOptionForDiag - Return the lowest-level warning option that
117/// enables the specified diagnostic. If there is no -Wfoo flag that controls
118/// the diagnostic, this returns null.
119const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) {
Chris Lattner33dd2822009-04-16 06:00:24 +0000120 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
121 return Info->OptionGroup;
122 return 0;
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000123}
124
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000125bool Diagnostic::isBuiltinSFINAEDiag(unsigned DiagID) {
126 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Douglas Gregor8439fac2009-06-15 16:52:15 +0000127 return Info->SFINAE && Info->Class == CLASS_ERROR;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000128 return false;
129}
130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131/// getDiagClass - Return the class field of the diagnostic.
132///
Chris Lattner07506182007-11-30 22:53:43 +0000133static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner121f60c2009-04-16 06:07:15 +0000134 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
135 return Info->Class;
136 return ~0U;
Reid Spencer5f016e22007-07-11 17:01:13 +0000137}
138
Chris Lattner182745a2007-12-02 01:09:57 +0000139//===----------------------------------------------------------------------===//
140// Custom Diagnostic information
141//===----------------------------------------------------------------------===//
142
143namespace clang {
144 namespace diag {
145 class CustomDiagInfo {
146 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
147 std::vector<DiagDesc> DiagInfo;
148 std::map<DiagDesc, unsigned> DiagIDs;
149 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Chris Lattner182745a2007-12-02 01:09:57 +0000151 /// getDescription - Return the description of the specified custom
152 /// diagnostic.
153 const char *getDescription(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000154 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000155 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000156 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattner182745a2007-12-02 01:09:57 +0000157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattner182745a2007-12-02 01:09:57 +0000159 /// getLevel - Return the level of the specified custom diagnostic.
160 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000161 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000162 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000163 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattner182745a2007-12-02 01:09:57 +0000164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000166 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
167 Diagnostic &Diags) {
Chris Lattner182745a2007-12-02 01:09:57 +0000168 DiagDesc D(L, Message);
169 // Check to see if it already exists.
170 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
171 if (I != DiagIDs.end() && I->first == D)
172 return I->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Chris Lattner182745a2007-12-02 01:09:57 +0000174 // If not, assign a new ID.
Chris Lattner88eccaf2009-01-29 06:55:46 +0000175 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattner182745a2007-12-02 01:09:57 +0000176 DiagIDs.insert(std::make_pair(D, ID));
177 DiagInfo.push_back(D);
178 return ID;
179 }
180 };
Mike Stump1eb44332009-09-09 15:08:12 +0000181
182 } // end diag namespace
183} // end clang namespace
Chris Lattner182745a2007-12-02 01:09:57 +0000184
185
186//===----------------------------------------------------------------------===//
187// Common Diagnostic implementation
188//===----------------------------------------------------------------------===//
189
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000190static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
191 const char *Modifier, unsigned ML,
192 const char *Argument, unsigned ArgLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000193 const Diagnostic::ArgumentValue *PrevArgs,
194 unsigned NumPrevArgs,
Chris Lattner92dd3862009-02-19 23:53:20 +0000195 llvm::SmallVectorImpl<char> &Output,
196 void *Cookie) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000197 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +0000198 Output.append(Str, Str+strlen(Str));
199}
200
201
Ted Kremenekb4398aa2008-08-07 17:49:57 +0000202Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000203 AllExtensionsSilenced = 0;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000204 IgnoreAllWarnings = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 WarningsAsErrors = false;
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000206 SuppressSystemWarnings = false;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000207 SuppressAllDiagnostics = false;
Chris Lattnerb54b2762009-04-16 05:04:32 +0000208 ExtBehavior = Ext_Ignore;
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 ErrorOccurred = false;
Chris Lattner15221422009-02-06 04:16:02 +0000211 FatalErrorOccurred = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 NumDiagnostics = 0;
Chris Lattner04ae2df2009-07-12 21:18:45 +0000213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 NumErrors = 0;
Chris Lattner182745a2007-12-02 01:09:57 +0000215 CustomDiagInfo = 0;
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000216 CurDiagID = ~0U;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000217 LastDiagLevel = Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000219 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +0000220 ArgToStringCookie = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Chris Lattner691f1ae2009-04-16 04:12:40 +0000222 // Set all mappings to 'unset'.
Chris Lattner04ae2df2009-07-12 21:18:45 +0000223 DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0);
224 DiagMappingsStack.push_back(BlankDiags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000225}
226
Chris Lattner182745a2007-12-02 01:09:57 +0000227Diagnostic::~Diagnostic() {
228 delete CustomDiagInfo;
229}
230
Chris Lattner04ae2df2009-07-12 21:18:45 +0000231
232void Diagnostic::pushMappings() {
233 DiagMappingsStack.push_back(DiagMappingsStack.back());
234}
235
236bool Diagnostic::popMappings() {
237 if (DiagMappingsStack.size() == 1)
238 return false;
239
240 DiagMappingsStack.pop_back();
241 return true;
242}
243
Chris Lattner182745a2007-12-02 01:09:57 +0000244/// getCustomDiagID - Return an ID for a diagnostic with the specified message
245/// and level. If this is the first request for this diagnosic, it is
246/// registered and created, otherwise the existing ID is returned.
247unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
Mike Stump1eb44332009-09-09 15:08:12 +0000248 if (CustomDiagInfo == 0)
Chris Lattner182745a2007-12-02 01:09:57 +0000249 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000250 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattner182745a2007-12-02 01:09:57 +0000251}
252
253
Chris Lattnerf5d23282009-02-17 06:49:55 +0000254/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
255/// level of the specified diagnostic ID is a Warning or Extension.
256/// This only works on builtin diagnostics, not custom ones, and is not legal to
257/// call on NOTEs.
258bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000259 return DiagID < diag::DIAG_UPPER_LIMIT &&
260 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Reid Spencer5f016e22007-07-11 17:01:13 +0000261}
262
Douglas Gregoree1828a2009-03-10 18:03:33 +0000263/// \brief Determine whether the given built-in diagnostic ID is a
264/// Note.
265bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000266 return DiagID < diag::DIAG_UPPER_LIMIT &&
267 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000268}
269
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000270/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
271/// ID is for an extension of some sort.
272///
273bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000274 return DiagID < diag::DIAG_UPPER_LIMIT &&
275 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000276}
277
Reid Spencer5f016e22007-07-11 17:01:13 +0000278
279/// getDescription - Given a diagnostic ID, return a description of the
280/// issue.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000281const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner121f60c2009-04-16 06:07:15 +0000282 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
283 return Info->Description;
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000284 return CustomDiagInfo->getDescription(DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000285}
286
287/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
288/// object, classify the specified diagnostic ID into a Level, consumable by
289/// the DiagnosticClient.
290Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattner182745a2007-12-02 01:09:57 +0000291 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000292 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattner182745a2007-12-02 01:09:57 +0000293 return CustomDiagInfo->getLevel(DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Chris Lattner07506182007-11-30 22:53:43 +0000295 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000296 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerf5d23282009-02-17 06:49:55 +0000297 return getDiagnosticLevel(DiagID, DiagClass);
298}
299
300/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
301/// object, classify the specified diagnostic ID into a Level, consumable by
302/// the DiagnosticClient.
303Diagnostic::Level
304Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerf5d23282009-02-17 06:49:55 +0000306 // to error. Errors can only be mapped to fatal.
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000307 Diagnostic::Level Result = Diagnostic::Fatal;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattner691f1ae2009-04-16 04:12:40 +0000309 // Get the mapping information, if unset, compute it lazily.
310 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
311 if (MappingInfo == 0) {
312 MappingInfo = GetDefaultDiagMapping(DiagID);
313 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Chris Lattner691f1ae2009-04-16 04:12:40 +0000316 switch (MappingInfo & 7) {
317 default: assert(0 && "Unknown mapping!");
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000318 case diag::MAP_IGNORE:
Chris Lattnerb54b2762009-04-16 05:04:32 +0000319 // Ignore this, unless this is an extension diagnostic and we're mapping
320 // them onto warnings or errors.
321 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
322 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
323 (MappingInfo & 8) != 0) // User explicitly mapped it.
324 return Diagnostic::Ignored;
325 Result = Diagnostic::Warning;
326 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
327 break;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000328 case diag::MAP_ERROR:
329 Result = Diagnostic::Error;
330 break;
331 case diag::MAP_FATAL:
332 Result = Diagnostic::Fatal;
333 break;
334 case diag::MAP_WARNING:
335 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner5b4681c2008-05-29 15:36:45 +0000336 if (IgnoreAllWarnings)
337 return Diagnostic::Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000339 Result = Diagnostic::Warning;
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Chris Lattnerb54b2762009-04-16 05:04:32 +0000341 // If this is an extension diagnostic and we're in -pedantic-error mode, and
342 // if the user didn't explicitly map it, upgrade to an error.
343 if (ExtBehavior == Ext_Error &&
344 (MappingInfo & 8) == 0 &&
345 isBuiltinExtensionDiag(DiagID))
346 Result = Diagnostic::Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000348 if (WarningsAsErrors)
349 Result = Diagnostic::Error;
350 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000352 case diag::MAP_WARNING_NO_WERROR:
353 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
354 // not be adjusted by -Werror or -pedantic-errors.
355 Result = Diagnostic::Warning;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000357 // If warnings are globally mapped to ignore or error, do it.
358 if (IgnoreAllWarnings)
359 return Diagnostic::Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000361 break;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000362 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000363
364 // Okay, we're about to return this as a "diagnostic to emit" one last check:
365 // if this is any sort of extension warning, and if we're in an __extension__
366 // block, silence it.
367 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
368 return Diagnostic::Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000370 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000371}
372
Chris Lattner3bc172b2009-04-19 22:34:23 +0000373struct WarningOption {
374 const char *Name;
375 const short *Members;
376 const char *SubGroups;
377};
378
379#define GET_DIAG_ARRAYS
380#include "clang/Basic/DiagnosticGroups.inc"
381#undef GET_DIAG_ARRAYS
382
383// Second the table of options, sorted by name for fast binary lookup.
384static const WarningOption OptionTable[] = {
385#define GET_DIAG_TABLE
386#include "clang/Basic/DiagnosticGroups.inc"
387#undef GET_DIAG_TABLE
388};
389static const size_t OptionTableSize =
390sizeof(OptionTable) / sizeof(OptionTable[0]);
391
392static bool WarningOptionCompare(const WarningOption &LHS,
393 const WarningOption &RHS) {
394 return strcmp(LHS.Name, RHS.Name) < 0;
395}
396
397static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
398 Diagnostic &Diags) {
399 // Option exists, poke all the members of its diagnostic set.
400 if (const short *Member = Group->Members) {
401 for (; *Member != -1; ++Member)
402 Diags.setDiagnosticMapping(*Member, Mapping);
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattner3bc172b2009-04-19 22:34:23 +0000405 // Enable/disable all subgroups along with this one.
406 if (const char *SubGroups = Group->SubGroups) {
407 for (; *SubGroups != (char)-1; ++SubGroups)
408 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
409 }
410}
411
412/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
413/// "unknown-pragmas" to have the specified mapping. This returns true and
414/// ignores the request if "Group" was unknown, false otherwise.
415bool Diagnostic::setDiagnosticGroupMapping(const char *Group,
416 diag::Mapping Map) {
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Chris Lattner3bc172b2009-04-19 22:34:23 +0000418 WarningOption Key = { Group, 0, 0 };
419 const WarningOption *Found =
420 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
421 WarningOptionCompare);
422 if (Found == OptionTable + OptionTableSize ||
423 strcmp(Found->Name, Group) != 0)
424 return true; // Option not found.
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner3bc172b2009-04-19 22:34:23 +0000426 MapGroupMembers(Found, Map, *this);
427 return false;
428}
429
430
Chris Lattner0a14eee2008-11-18 07:04:44 +0000431/// ProcessDiag - This is the method used to report a diagnostic that is
432/// finally fully formed.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000433bool Diagnostic::ProcessDiag() {
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000434 DiagnosticInfo Info(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Douglas Gregor81b747b2009-09-17 21:32:03 +0000436 if (SuppressAllDiagnostics)
437 return false;
438
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 // Figure out the diagnostic level of this message.
Chris Lattnerf5d23282009-02-17 06:49:55 +0000440 Diagnostic::Level DiagLevel;
441 unsigned DiagID = Info.getID();
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Chris Lattnerf5d23282009-02-17 06:49:55 +0000443 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
444 // in a system header.
445 bool ShouldEmitInSystemHeader;
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattnerf5d23282009-02-17 06:49:55 +0000447 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
448 // Handle custom diagnostics, which cannot be mapped.
449 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattnerf5d23282009-02-17 06:49:55 +0000451 // Custom diagnostics always are emitted in system headers.
452 ShouldEmitInSystemHeader = true;
453 } else {
454 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
455 // the diagnostic level was for the previous diagnostic so that it is
456 // filtered the same as the previous diagnostic.
457 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000458 if (DiagClass == CLASS_NOTE) {
Chris Lattnerf5d23282009-02-17 06:49:55 +0000459 DiagLevel = Diagnostic::Note;
460 ShouldEmitInSystemHeader = false; // extra consideration is needed
461 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000462 // If this is not an error and we are in a system header, we ignore it.
Chris Lattnerf5d23282009-02-17 06:49:55 +0000463 // Check the original Diag ID here, because we also want to ignore
464 // extensions and warnings in -Werror and -pedantic-errors modes, which
465 // *map* warnings/extensions to errors.
Chris Lattner8a941e02009-04-15 16:56:26 +0000466 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattnerf5d23282009-02-17 06:49:55 +0000468 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
469 }
470 }
471
Douglas Gregor525c4b02009-03-19 18:55:06 +0000472 if (DiagLevel != Diagnostic::Note) {
473 // Record that a fatal error occurred only when we see a second
474 // non-note diagnostic. This allows notes to be attached to the
475 // fatal error, but suppresses any diagnostics that follow those
476 // notes.
477 if (LastDiagLevel == Diagnostic::Fatal)
478 FatalErrorOccurred = true;
479
Chris Lattnerf5d23282009-02-17 06:49:55 +0000480 LastDiagLevel = DiagLevel;
Mike Stump1eb44332009-09-09 15:08:12 +0000481 }
Douglas Gregor525c4b02009-03-19 18:55:06 +0000482
483 // If a fatal error has already been emitted, silence all subsequent
484 // diagnostics.
485 if (FatalErrorOccurred)
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000486 return false;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000487
Chris Lattnerf5d23282009-02-17 06:49:55 +0000488 // If the client doesn't care about this message, don't issue it. If this is
489 // a note and the last real diagnostic was ignored, ignore it too.
490 if (DiagLevel == Diagnostic::Ignored ||
491 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000492 return false;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000493
Chris Lattnerf5d23282009-02-17 06:49:55 +0000494 // If this diagnostic is in a system header and is not a clang error, suppress
495 // it.
496 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner0a14eee2008-11-18 07:04:44 +0000497 Info.getLocation().isValid() &&
Chris Lattnerf5d23282009-02-17 06:49:55 +0000498 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner336f26b2009-02-17 06:52:20 +0000499 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
500 LastDiagLevel = Diagnostic::Ignored;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000501 return false;
Chris Lattner336f26b2009-02-17 06:52:20 +0000502 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000503
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 if (DiagLevel >= Diagnostic::Error) {
505 ErrorOccurred = true;
Chris Lattner0a14eee2008-11-18 07:04:44 +0000506 ++NumErrors;
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 // Finally, report it.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000510 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekcabe6682009-01-23 20:28:53 +0000511 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000512
Douglas Gregoree1828a2009-03-10 18:03:33 +0000513 CurDiagID = ~0U;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000514
515 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000516}
517
Nico Weber7bfaaae2008-08-10 19:59:06 +0000518
Reid Spencer5f016e22007-07-11 17:01:13 +0000519DiagnosticClient::~DiagnosticClient() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000520
Chris Lattnerf4c83962008-11-19 06:51:40 +0000521
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000522/// ModifierIs - Return true if the specified modifier matches specified string.
523template <std::size_t StrLen>
524static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
525 const char (&Str)[StrLen]) {
526 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
527}
528
529/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
530/// like this: %select{foo|bar|baz}2. This means that the integer argument
531/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
532/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
533/// This is very useful for certain classes of variant diagnostics.
534static void HandleSelectModifier(unsigned ValNo,
535 const char *Argument, unsigned ArgumentLen,
536 llvm::SmallVectorImpl<char> &OutStr) {
537 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000539 // Skip over 'ValNo' |'s.
540 while (ValNo) {
541 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
542 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
543 " larger than the number of options in the diagnostic string!");
544 Argument = NextVal+1; // Skip this string.
545 --ValNo;
546 }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000548 // Get the end of the value. This is either the } or the |.
549 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
550 // Add the value to the output string.
551 OutStr.append(Argument, EndPtr);
552}
553
554/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
555/// letter 's' to the string if the value is not 1. This is used in cases like
556/// this: "you idiot, you have %4 parameter%s4!".
557static void HandleIntegerSModifier(unsigned ValNo,
558 llvm::SmallVectorImpl<char> &OutStr) {
559 if (ValNo != 1)
560 OutStr.push_back('s');
561}
562
563
Sebastian Redle4c452c2008-11-22 13:44:36 +0000564/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000565static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000566 // Programming 101: Parse a decimal number :-)
567 unsigned Val = 0;
568 while (Start != End && *Start >= '0' && *Start <= '9') {
569 Val *= 10;
570 Val += *Start - '0';
571 ++Start;
572 }
573 return Val;
574}
575
576/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000577static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000578 if (*Start != '[') {
579 unsigned Ref = PluralNumber(Start, End);
580 return Ref == Val;
581 }
582
583 ++Start;
584 unsigned Low = PluralNumber(Start, End);
585 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
586 ++Start;
587 unsigned High = PluralNumber(Start, End);
588 assert(*Start == ']' && "Bad plural expression syntax: expected )");
589 ++Start;
590 return Low <= Val && Val <= High;
591}
592
593/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000594static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000595 // Empty condition?
596 if (*Start == ':')
597 return true;
598
599 while (1) {
600 char C = *Start;
601 if (C == '%') {
602 // Modulo expression
603 ++Start;
604 unsigned Arg = PluralNumber(Start, End);
605 assert(*Start == '=' && "Bad plural expression syntax: expected =");
606 ++Start;
607 unsigned ValMod = ValNo % Arg;
608 if (TestPluralRange(ValMod, Start, End))
609 return true;
610 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000611 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000612 "Bad plural expression syntax: unexpected character");
613 // Range expression
614 if (TestPluralRange(ValNo, Start, End))
615 return true;
616 }
617
618 // Scan for next or-expr part.
619 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000620 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000621 break;
622 ++Start;
623 }
624 return false;
625}
626
627/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
628/// for complex plural forms, or in languages where all plurals are complex.
629/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
630/// conditions that are tested in order, the form corresponding to the first
631/// that applies being emitted. The empty condition is always true, making the
632/// last form a default case.
633/// Conditions are simple boolean expressions, where n is the number argument.
634/// Here are the rules.
635/// condition := expression | empty
636/// empty := -> always true
637/// expression := numeric [',' expression] -> logical or
638/// numeric := range -> true if n in range
639/// | '%' number '=' range -> true if n % number in range
640/// range := number
641/// | '[' number ',' number ']' -> ranges are inclusive both ends
642///
643/// Here are some examples from the GNU gettext manual written in this form:
644/// English:
645/// {1:form0|:form1}
646/// Latvian:
647/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
648/// Gaeilge:
649/// {1:form0|2:form1|:form2}
650/// Romanian:
651/// {1:form0|0,%100=[1,19]:form1|:form2}
652/// Lithuanian:
653/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
654/// Russian (requires repeated form):
655/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
656/// Slovak
657/// {1:form0|[2,4]:form1|:form2}
658/// Polish (requires repeated form):
659/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
660static void HandlePluralModifier(unsigned ValNo,
661 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb54b2762009-04-16 05:04:32 +0000662 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000663 const char *ArgumentEnd = Argument + ArgumentLen;
664 while (1) {
665 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
666 const char *ExprEnd = Argument;
667 while (*ExprEnd != ':') {
668 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
669 ++ExprEnd;
670 }
671 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
672 Argument = ExprEnd + 1;
673 ExprEnd = std::find(Argument, ArgumentEnd, '|');
674 OutStr.append(Argument, ExprEnd);
675 return;
676 }
677 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
678 }
679}
680
681
Chris Lattnerf4c83962008-11-19 06:51:40 +0000682/// FormatDiagnostic - Format this diagnostic into a string, substituting the
683/// formal arguments into the %0 slots. The result is appended onto the Str
684/// array.
685void DiagnosticInfo::
686FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
687 const char *DiagStr = getDiags()->getDescription(getID());
688 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000690 /// FormattedArgs - Keep track of all of the arguments formatted by
691 /// ConvertArgToString and pass them into subsequent calls to
692 /// ConvertArgToString, allowing the implementation to avoid redundancies in
693 /// obvious cases.
694 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
695
Chris Lattnerf4c83962008-11-19 06:51:40 +0000696 while (DiagStr != DiagEnd) {
697 if (DiagStr[0] != '%') {
698 // Append non-%0 substrings to Str if we have one.
699 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
700 OutStr.append(DiagStr, StrEnd);
701 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000702 continue;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000703 } else if (DiagStr[1] == '%') {
704 OutStr.push_back('%'); // %% -> %.
705 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000706 continue;
707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000709 // Skip the %.
710 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000712 // This must be a placeholder for a diagnostic argument. The format for a
713 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
714 // The digit is a number from 0-9 indicating which argument this comes from.
715 // The modifier is a string of digits from the set [-a-z]+, arguments is a
716 // brace enclosed string.
717 const char *Modifier = 0, *Argument = 0;
718 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000720 // Check to see if we have a modifier. If so eat it.
721 if (!isdigit(DiagStr[0])) {
722 Modifier = DiagStr;
723 while (DiagStr[0] == '-' ||
724 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
725 ++DiagStr;
726 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000727
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000728 // If we have an argument, get it next.
729 if (DiagStr[0] == '{') {
730 ++DiagStr; // Skip {.
731 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000733 for (; DiagStr[0] != '}'; ++DiagStr)
734 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
735 ArgumentLen = DiagStr-Argument;
736 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000737 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000740 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000741 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000742
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000743 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
744
745 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000746 // ---- STRINGS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000747 case Diagnostic::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000748 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000749 assert(ModifierLen == 0 && "No modifiers for strings yet");
750 OutStr.append(S.begin(), S.end());
751 break;
752 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000753 case Diagnostic::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000754 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000755 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000756
757 // Don't crash if get passed a null pointer by accident.
758 if (!S)
759 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000761 OutStr.append(S, S + strlen(S));
762 break;
763 }
Chris Lattner08631c52008-11-23 21:45:46 +0000764 // ---- INTEGERS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000765 case Diagnostic::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000766 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000768 if (ModifierIs(Modifier, ModifierLen, "select")) {
769 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
770 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
771 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000772 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
773 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000774 } else {
775 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000776 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000777 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000778 break;
779 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000780 case Diagnostic::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000781 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000783 if (ModifierIs(Modifier, ModifierLen, "select")) {
784 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
785 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
786 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000787 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
788 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000789 } else {
790 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000791 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000792 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000793 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000794 }
Chris Lattner08631c52008-11-23 21:45:46 +0000795 // ---- NAMES and TYPES ----
796 case Diagnostic::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000797 const IdentifierInfo *II = getArgIdentifier(ArgNo);
798 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000799
800 // Don't crash if get passed a null pointer by accident.
801 if (!II) {
802 const char *S = "(null)";
803 OutStr.append(S, S + strlen(S));
804 continue;
805 }
806
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000807 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000808 break;
809 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000810 case Diagnostic::ak_qualtype:
Chris Lattner011bb4e2008-11-23 20:28:15 +0000811 case Diagnostic::ak_declarationname:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000812 case Diagnostic::ak_nameddecl:
Douglas Gregordacd4342009-08-26 00:04:55 +0000813 case Diagnostic::ak_nestednamespec:
Douglas Gregor3f093272009-10-13 21:16:44 +0000814 case Diagnostic::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000815 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000816 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000817 Argument, ArgumentLen,
818 FormattedArgs.data(), FormattedArgs.size(),
819 OutStr);
Chris Lattner22caddc2008-11-23 09:13:29 +0000820 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000821 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000822
823 // Remember this argument info for subsequent formatting operations. Turn
824 // std::strings into a null terminated string to make it be the same case as
825 // all the other ones.
826 if (Kind != Diagnostic::ak_std_string)
827 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
828 else
829 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
830 (intptr_t)getArgStdStr(ArgNo).c_str()));
831
Nico Weber7bfaaae2008-08-10 19:59:06 +0000832 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000833}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000834
835/// IncludeInDiagnosticCounts - This method (whose default implementation
836/// returns true) indicates whether the diagnostics handled by this
837/// DiagnosticClient should be included in the number of diagnostics
838/// reported by Diagnostic.
839bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }