blob: d3bb9d5496cb8dd50a37f25520ac925bfce06189 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Diagnostic.h"
Chris Lattnere007de32009-04-15 07:01:18 +000015
16#include "clang/Lex/LexDiagnostic.h"
17#include "clang/Parse/ParseDiagnostic.h"
18#include "clang/AST/ASTDiagnostic.h"
19#include "clang/Sema/SemaDiagnostic.h"
20#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Analysis/AnalysisDiagnostic.h"
22#include "clang/Driver/DriverDiagnostic.h"
23
Chris Lattnerb91fd172008-11-19 07:32:16 +000024#include "clang/Basic/IdentifierTable.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000025#include "clang/Basic/SourceLocation.h"
Chris Lattner23be0672008-11-19 06:51:40 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner91aea712008-11-19 07:22:31 +000027#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3633792009-10-17 18:12:14 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattnere6535cf2007-12-02 01:09:57 +000029#include <vector>
30#include <map>
Chris Lattner0d799d32008-03-10 17:04:53 +000031#include <cstring>
Chris Lattner22eb9722006-06-18 05:43:12 +000032using namespace clang;
33
Chris Lattnere6535cf2007-12-02 01:09:57 +000034//===----------------------------------------------------------------------===//
35// Builtin Diagnostic information
36//===----------------------------------------------------------------------===//
37
Chris Lattner6c440322009-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 Lattnere007de32009-04-15 07:01:18 +000045
Chris Lattner6a64cc62009-04-16 06:00:24 +000046struct StaticDiagInfoRec {
Chris Lattner6c440322009-04-16 06:07:15 +000047 unsigned short DiagID;
48 unsigned Mapping : 3;
49 unsigned Class : 3;
Douglas Gregor33834512009-06-14 07:33:30 +000050 bool SFINAE : 1;
Chris Lattner6c440322009-04-16 06:07:15 +000051 const char *Description;
Chris Lattner6a64cc62009-04-16 06:00:24 +000052 const char *OptionGroup;
Mike Stump11289f42009-09-09 15:08:12 +000053
Chris Lattner2d49eed2009-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 Lattnere007de32009-04-15 07:01:18 +000060};
61
Chris Lattner6a64cc62009-04-16 06:00:24 +000062static const StaticDiagInfoRec StaticDiagInfo[] = {
Douglas Gregor33834512009-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 Lattnere007de32009-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 Gregor33834512009-06-14 07:33:30 +000073 { 0, 0, 0, 0, 0, 0}
Chris Lattnere007de32009-04-15 07:01:18 +000074};
Chris Lattnere6c831d2009-04-15 16:56:26 +000075#undef DIAG
Chris Lattnere007de32009-04-15 07:01:18 +000076
Chris Lattner2d49eed2009-04-16 06:13:46 +000077/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
78/// or null if the ID is invalid.
Chris Lattner6a64cc62009-04-16 06:00:24 +000079static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner2d49eed2009-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 Lattnercb4e68c2009-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 Lattner2d49eed2009-04-16 06:13:46 +000091 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
92 "Improperly sorted diag info");
Chris Lattnercb4e68c2009-10-16 02:34:51 +000093 }
Chris Lattner2d49eed2009-04-16 06:13:46 +000094 IsFirst = false;
95 }
96#endif
Mike Stump11289f42009-09-09 15:08:12 +000097
Chris Lattner2d49eed2009-04-16 06:13:46 +000098 // Search the diagnostic table with a binary search.
Douglas Gregor33834512009-06-14 07:33:30 +000099 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0 };
Mike Stump11289f42009-09-09 15:08:12 +0000100
Chris Lattner2d49eed2009-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 Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattner2d49eed2009-04-16 06:13:46 +0000107 return Found;
Chris Lattner6a64cc62009-04-16 06:00:24 +0000108}
109
110static unsigned GetDefaultDiagMapping(unsigned DiagID) {
111 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Chris Lattner6c440322009-04-16 06:07:15 +0000112 return Info->Mapping;
Chris Lattner411c0ff2009-04-16 04:12:40 +0000113 return diag::MAP_FATAL;
114}
115
Chris Lattner22cb8182009-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 Lattner6a64cc62009-04-16 06:00:24 +0000120 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
121 return Info->OptionGroup;
122 return 0;
Chris Lattner22cb8182009-04-16 05:44:38 +0000123}
124
Douglas Gregor33834512009-06-14 07:33:30 +0000125bool Diagnostic::isBuiltinSFINAEDiag(unsigned DiagID) {
126 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Douglas Gregor15e08d82009-06-15 16:52:15 +0000127 return Info->SFINAE && Info->Class == CLASS_ERROR;
Douglas Gregor33834512009-06-14 07:33:30 +0000128 return false;
129}
130
Chris Lattner22eb9722006-06-18 05:43:12 +0000131/// getDiagClass - Return the class field of the diagnostic.
132///
Chris Lattner4431a1b2007-11-30 22:53:43 +0000133static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner6c440322009-04-16 06:07:15 +0000134 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
135 return Info->Class;
136 return ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +0000137}
138
Chris Lattnere6535cf2007-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 Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattnere6535cf2007-12-02 01:09:57 +0000151 /// getDescription - Return the description of the specified custom
152 /// diagnostic.
153 const char *getDescription(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000154 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000155 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000156 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattnere6535cf2007-12-02 01:09:57 +0000157 }
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattnere6535cf2007-12-02 01:09:57 +0000159 /// getLevel - Return the level of the specified custom diagnostic.
160 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000161 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000162 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000163 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000164 }
Mike Stump11289f42009-09-09 15:08:12 +0000165
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000166 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
167 Diagnostic &Diags) {
Chris Lattnere6535cf2007-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 Stump11289f42009-09-09 15:08:12 +0000173
Chris Lattnere6535cf2007-12-02 01:09:57 +0000174 // If not, assign a new ID.
Chris Lattner36790cf2009-01-29 06:55:46 +0000175 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000176 DiagIDs.insert(std::make_pair(D, ID));
177 DiagInfo.push_back(D);
178 return ID;
179 }
180 };
Mike Stump11289f42009-09-09 15:08:12 +0000181
182 } // end diag namespace
183} // end clang namespace
Chris Lattnere6535cf2007-12-02 01:09:57 +0000184
185
186//===----------------------------------------------------------------------===//
187// Common Diagnostic implementation
188//===----------------------------------------------------------------------===//
189
Chris Lattner63ecc502008-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 Lattnercf868c42009-02-19 23:53:20 +0000193 llvm::SmallVectorImpl<char> &Output,
194 void *Cookie) {
Chris Lattner63ecc502008-11-23 09:21:17 +0000195 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000196 Output.append(Str, Str+strlen(Str));
197}
198
199
Ted Kremenek31691ae2008-08-07 17:49:57 +0000200Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattnere007de32009-04-15 07:01:18 +0000201 AllExtensionsSilenced = 0;
Chris Lattner8c800702008-05-29 15:36:45 +0000202 IgnoreAllWarnings = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000203 WarningsAsErrors = false;
Daniel Dunbar84b70f72008-09-12 18:10:20 +0000204 SuppressSystemWarnings = false;
Douglas Gregor2436e712009-09-17 21:32:03 +0000205 SuppressAllDiagnostics = false;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000206 ExtBehavior = Ext_Ignore;
Mike Stump11289f42009-09-09 15:08:12 +0000207
Chris Lattnerc49b9052007-05-28 00:46:44 +0000208 ErrorOccurred = false;
Chris Lattner9e031192009-02-06 04:16:02 +0000209 FatalErrorOccurred = false;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000210 NumDiagnostics = 0;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000211
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000212 NumErrors = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000213 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000214 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000215 LastDiagLevel = Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000216
Chris Lattner63ecc502008-11-23 09:21:17 +0000217 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000218 ArgToStringCookie = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000219
Chris Lattner411c0ff2009-04-16 04:12:40 +0000220 // Set all mappings to 'unset'.
Chris Lattnerfb42a182009-07-12 21:18:45 +0000221 DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0);
222 DiagMappingsStack.push_back(BlankDiags);
Chris Lattnerae411572006-07-05 00:55:08 +0000223}
224
Chris Lattnere6535cf2007-12-02 01:09:57 +0000225Diagnostic::~Diagnostic() {
226 delete CustomDiagInfo;
227}
228
Chris Lattnerfb42a182009-07-12 21:18:45 +0000229
230void Diagnostic::pushMappings() {
231 DiagMappingsStack.push_back(DiagMappingsStack.back());
232}
233
234bool Diagnostic::popMappings() {
235 if (DiagMappingsStack.size() == 1)
236 return false;
237
238 DiagMappingsStack.pop_back();
239 return true;
240}
241
Chris Lattnere6535cf2007-12-02 01:09:57 +0000242/// getCustomDiagID - Return an ID for a diagnostic with the specified message
243/// and level. If this is the first request for this diagnosic, it is
244/// registered and created, otherwise the existing ID is returned.
245unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
Mike Stump11289f42009-09-09 15:08:12 +0000246 if (CustomDiagInfo == 0)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000247 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000248 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000249}
250
251
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000252/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
253/// level of the specified diagnostic ID is a Warning or Extension.
254/// This only works on builtin diagnostics, not custom ones, and is not legal to
255/// call on NOTEs.
256bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000257 return DiagID < diag::DIAG_UPPER_LIMIT &&
258 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000259}
260
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000261/// \brief Determine whether the given built-in diagnostic ID is a
262/// Note.
263bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000264 return DiagID < diag::DIAG_UPPER_LIMIT &&
265 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000266}
267
Chris Lattnere007de32009-04-15 07:01:18 +0000268/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
269/// ID is for an extension of some sort.
270///
271bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000272 return DiagID < diag::DIAG_UPPER_LIMIT &&
273 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattnere007de32009-04-15 07:01:18 +0000274}
275
Chris Lattner22eb9722006-06-18 05:43:12 +0000276
277/// getDescription - Given a diagnostic ID, return a description of the
278/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000279const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner6c440322009-04-16 06:07:15 +0000280 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
281 return Info->Description;
Chris Lattner7368d582009-01-27 18:30:58 +0000282 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000283}
284
285/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
286/// object, classify the specified diagnostic ID into a Level, consumable by
287/// the DiagnosticClient.
288Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000289 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000290 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000291 return CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000292
Chris Lattner4431a1b2007-11-30 22:53:43 +0000293 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000294 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000295 return getDiagnosticLevel(DiagID, DiagClass);
296}
297
298/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
299/// object, classify the specified diagnostic ID into a Level, consumable by
300/// the DiagnosticClient.
301Diagnostic::Level
302Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattnerae411572006-07-05 00:55:08 +0000303 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000304 // to error. Errors can only be mapped to fatal.
Chris Lattnere007de32009-04-15 07:01:18 +0000305 Diagnostic::Level Result = Diagnostic::Fatal;
Mike Stump11289f42009-09-09 15:08:12 +0000306
Chris Lattner411c0ff2009-04-16 04:12:40 +0000307 // Get the mapping information, if unset, compute it lazily.
308 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
309 if (MappingInfo == 0) {
310 MappingInfo = GetDefaultDiagMapping(DiagID);
311 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
312 }
Mike Stump11289f42009-09-09 15:08:12 +0000313
Chris Lattner411c0ff2009-04-16 04:12:40 +0000314 switch (MappingInfo & 7) {
315 default: assert(0 && "Unknown mapping!");
Chris Lattnere007de32009-04-15 07:01:18 +0000316 case diag::MAP_IGNORE:
Chris Lattnerb8e73152009-04-16 05:04:32 +0000317 // Ignore this, unless this is an extension diagnostic and we're mapping
318 // them onto warnings or errors.
319 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
320 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
321 (MappingInfo & 8) != 0) // User explicitly mapped it.
322 return Diagnostic::Ignored;
323 Result = Diagnostic::Warning;
324 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
325 break;
Chris Lattnere007de32009-04-15 07:01:18 +0000326 case diag::MAP_ERROR:
327 Result = Diagnostic::Error;
328 break;
329 case diag::MAP_FATAL:
330 Result = Diagnostic::Fatal;
331 break;
332 case diag::MAP_WARNING:
333 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner8c800702008-05-29 15:36:45 +0000334 if (IgnoreAllWarnings)
335 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000336
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000337 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000338
Chris Lattnerb8e73152009-04-16 05:04:32 +0000339 // If this is an extension diagnostic and we're in -pedantic-error mode, and
340 // if the user didn't explicitly map it, upgrade to an error.
341 if (ExtBehavior == Ext_Error &&
342 (MappingInfo & 8) == 0 &&
343 isBuiltinExtensionDiag(DiagID))
344 Result = Diagnostic::Error;
Mike Stump11289f42009-09-09 15:08:12 +0000345
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000346 if (WarningsAsErrors)
347 Result = Diagnostic::Error;
348 break;
Mike Stump11289f42009-09-09 15:08:12 +0000349
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000350 case diag::MAP_WARNING_NO_WERROR:
351 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
352 // not be adjusted by -Werror or -pedantic-errors.
353 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000355 // If warnings are globally mapped to ignore or error, do it.
356 if (IgnoreAllWarnings)
357 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000358
Chris Lattnere007de32009-04-15 07:01:18 +0000359 break;
Chris Lattner8c800702008-05-29 15:36:45 +0000360 }
Chris Lattnere007de32009-04-15 07:01:18 +0000361
362 // Okay, we're about to return this as a "diagnostic to emit" one last check:
363 // if this is any sort of extension warning, and if we're in an __extension__
364 // block, silence it.
365 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
366 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattnere007de32009-04-15 07:01:18 +0000368 return Result;
Chris Lattner22eb9722006-06-18 05:43:12 +0000369}
370
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000371struct WarningOption {
372 const char *Name;
373 const short *Members;
374 const char *SubGroups;
375};
376
377#define GET_DIAG_ARRAYS
378#include "clang/Basic/DiagnosticGroups.inc"
379#undef GET_DIAG_ARRAYS
380
381// Second the table of options, sorted by name for fast binary lookup.
382static const WarningOption OptionTable[] = {
383#define GET_DIAG_TABLE
384#include "clang/Basic/DiagnosticGroups.inc"
385#undef GET_DIAG_TABLE
386};
387static const size_t OptionTableSize =
388sizeof(OptionTable) / sizeof(OptionTable[0]);
389
390static bool WarningOptionCompare(const WarningOption &LHS,
391 const WarningOption &RHS) {
392 return strcmp(LHS.Name, RHS.Name) < 0;
393}
394
395static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
396 Diagnostic &Diags) {
397 // Option exists, poke all the members of its diagnostic set.
398 if (const short *Member = Group->Members) {
399 for (; *Member != -1; ++Member)
400 Diags.setDiagnosticMapping(*Member, Mapping);
401 }
Mike Stump11289f42009-09-09 15:08:12 +0000402
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000403 // Enable/disable all subgroups along with this one.
404 if (const char *SubGroups = Group->SubGroups) {
405 for (; *SubGroups != (char)-1; ++SubGroups)
406 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
407 }
408}
409
410/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
411/// "unknown-pragmas" to have the specified mapping. This returns true and
412/// ignores the request if "Group" was unknown, false otherwise.
413bool Diagnostic::setDiagnosticGroupMapping(const char *Group,
414 diag::Mapping Map) {
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000416 WarningOption Key = { Group, 0, 0 };
417 const WarningOption *Found =
418 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
419 WarningOptionCompare);
420 if (Found == OptionTable + OptionTableSize ||
421 strcmp(Found->Name, Group) != 0)
422 return true; // Option not found.
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000424 MapGroupMembers(Found, Map, *this);
425 return false;
426}
427
428
Chris Lattner8488c822008-11-18 07:04:44 +0000429/// ProcessDiag - This is the method used to report a diagnostic that is
430/// finally fully formed.
Douglas Gregor33834512009-06-14 07:33:30 +0000431bool Diagnostic::ProcessDiag() {
Chris Lattner427c9c12008-11-22 00:59:29 +0000432 DiagnosticInfo Info(this);
Mike Stump11289f42009-09-09 15:08:12 +0000433
Douglas Gregor2436e712009-09-17 21:32:03 +0000434 if (SuppressAllDiagnostics)
435 return false;
436
Chris Lattner22eb9722006-06-18 05:43:12 +0000437 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000438 Diagnostic::Level DiagLevel;
439 unsigned DiagID = Info.getID();
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000441 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
442 // in a system header.
443 bool ShouldEmitInSystemHeader;
Mike Stump11289f42009-09-09 15:08:12 +0000444
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000445 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
446 // Handle custom diagnostics, which cannot be mapped.
447 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000448
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000449 // Custom diagnostics always are emitted in system headers.
450 ShouldEmitInSystemHeader = true;
451 } else {
452 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
453 // the diagnostic level was for the previous diagnostic so that it is
454 // filtered the same as the previous diagnostic.
455 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000456 if (DiagClass == CLASS_NOTE) {
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000457 DiagLevel = Diagnostic::Note;
458 ShouldEmitInSystemHeader = false; // extra consideration is needed
459 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000460 // If this is not an error and we are in a system header, we ignore it.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000461 // Check the original Diag ID here, because we also want to ignore
462 // extensions and warnings in -Werror and -pedantic-errors modes, which
463 // *map* warnings/extensions to errors.
Chris Lattnere6c831d2009-04-15 16:56:26 +0000464 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Mike Stump11289f42009-09-09 15:08:12 +0000465
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000466 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
467 }
468 }
469
Douglas Gregor19367f52009-03-19 18:55:06 +0000470 if (DiagLevel != Diagnostic::Note) {
471 // Record that a fatal error occurred only when we see a second
472 // non-note diagnostic. This allows notes to be attached to the
473 // fatal error, but suppresses any diagnostics that follow those
474 // notes.
475 if (LastDiagLevel == Diagnostic::Fatal)
476 FatalErrorOccurred = true;
477
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000478 LastDiagLevel = DiagLevel;
Mike Stump11289f42009-09-09 15:08:12 +0000479 }
Douglas Gregor19367f52009-03-19 18:55:06 +0000480
481 // If a fatal error has already been emitted, silence all subsequent
482 // diagnostics.
483 if (FatalErrorOccurred)
Douglas Gregor33834512009-06-14 07:33:30 +0000484 return false;
Douglas Gregor19367f52009-03-19 18:55:06 +0000485
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000486 // If the client doesn't care about this message, don't issue it. If this is
487 // a note and the last real diagnostic was ignored, ignore it too.
488 if (DiagLevel == Diagnostic::Ignored ||
489 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Douglas Gregor33834512009-06-14 07:33:30 +0000490 return false;
Nico Weber4c311642008-08-10 19:59:06 +0000491
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000492 // If this diagnostic is in a system header and is not a clang error, suppress
493 // it.
494 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner8488c822008-11-18 07:04:44 +0000495 Info.getLocation().isValid() &&
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000496 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000497 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
498 LastDiagLevel = Diagnostic::Ignored;
Douglas Gregor33834512009-06-14 07:33:30 +0000499 return false;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000500 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000501
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000502 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000503 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000504 ++NumErrors;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Chris Lattner22eb9722006-06-18 05:43:12 +0000507 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000508 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekea06ec12009-01-23 20:28:53 +0000509 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000510
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000511 CurDiagID = ~0U;
Douglas Gregor33834512009-06-14 07:33:30 +0000512
513 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000514}
515
Nico Weber4c311642008-08-10 19:59:06 +0000516
Chris Lattner22eb9722006-06-18 05:43:12 +0000517DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000518
Chris Lattner23be0672008-11-19 06:51:40 +0000519
Chris Lattner2b786902008-11-21 07:50:02 +0000520/// ModifierIs - Return true if the specified modifier matches specified string.
521template <std::size_t StrLen>
522static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
523 const char (&Str)[StrLen]) {
524 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
525}
526
527/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
528/// like this: %select{foo|bar|baz}2. This means that the integer argument
529/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
530/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
531/// This is very useful for certain classes of variant diagnostics.
532static void HandleSelectModifier(unsigned ValNo,
533 const char *Argument, unsigned ArgumentLen,
534 llvm::SmallVectorImpl<char> &OutStr) {
535 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Chris Lattner2b786902008-11-21 07:50:02 +0000537 // Skip over 'ValNo' |'s.
538 while (ValNo) {
539 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
540 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
541 " larger than the number of options in the diagnostic string!");
542 Argument = NextVal+1; // Skip this string.
543 --ValNo;
544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Chris Lattner2b786902008-11-21 07:50:02 +0000546 // Get the end of the value. This is either the } or the |.
547 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
548 // Add the value to the output string.
549 OutStr.append(Argument, EndPtr);
550}
551
552/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
553/// letter 's' to the string if the value is not 1. This is used in cases like
554/// this: "you idiot, you have %4 parameter%s4!".
555static void HandleIntegerSModifier(unsigned ValNo,
556 llvm::SmallVectorImpl<char> &OutStr) {
557 if (ValNo != 1)
558 OutStr.push_back('s');
559}
560
561
Sebastian Redl15b02d22008-11-22 13:44:36 +0000562/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000563static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000564 // Programming 101: Parse a decimal number :-)
565 unsigned Val = 0;
566 while (Start != End && *Start >= '0' && *Start <= '9') {
567 Val *= 10;
568 Val += *Start - '0';
569 ++Start;
570 }
571 return Val;
572}
573
574/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000575static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000576 if (*Start != '[') {
577 unsigned Ref = PluralNumber(Start, End);
578 return Ref == Val;
579 }
580
581 ++Start;
582 unsigned Low = PluralNumber(Start, End);
583 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
584 ++Start;
585 unsigned High = PluralNumber(Start, End);
586 assert(*Start == ']' && "Bad plural expression syntax: expected )");
587 ++Start;
588 return Low <= Val && Val <= High;
589}
590
591/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000592static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000593 // Empty condition?
594 if (*Start == ':')
595 return true;
596
597 while (1) {
598 char C = *Start;
599 if (C == '%') {
600 // Modulo expression
601 ++Start;
602 unsigned Arg = PluralNumber(Start, End);
603 assert(*Start == '=' && "Bad plural expression syntax: expected =");
604 ++Start;
605 unsigned ValMod = ValNo % Arg;
606 if (TestPluralRange(ValMod, Start, End))
607 return true;
608 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000609 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000610 "Bad plural expression syntax: unexpected character");
611 // Range expression
612 if (TestPluralRange(ValNo, Start, End))
613 return true;
614 }
615
616 // Scan for next or-expr part.
617 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000618 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000619 break;
620 ++Start;
621 }
622 return false;
623}
624
625/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
626/// for complex plural forms, or in languages where all plurals are complex.
627/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
628/// conditions that are tested in order, the form corresponding to the first
629/// that applies being emitted. The empty condition is always true, making the
630/// last form a default case.
631/// Conditions are simple boolean expressions, where n is the number argument.
632/// Here are the rules.
633/// condition := expression | empty
634/// empty := -> always true
635/// expression := numeric [',' expression] -> logical or
636/// numeric := range -> true if n in range
637/// | '%' number '=' range -> true if n % number in range
638/// range := number
639/// | '[' number ',' number ']' -> ranges are inclusive both ends
640///
641/// Here are some examples from the GNU gettext manual written in this form:
642/// English:
643/// {1:form0|:form1}
644/// Latvian:
645/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
646/// Gaeilge:
647/// {1:form0|2:form1|:form2}
648/// Romanian:
649/// {1:form0|0,%100=[1,19]:form1|:form2}
650/// Lithuanian:
651/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
652/// Russian (requires repeated form):
653/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
654/// Slovak
655/// {1:form0|[2,4]:form1|:form2}
656/// Polish (requires repeated form):
657/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
658static void HandlePluralModifier(unsigned ValNo,
659 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb8e73152009-04-16 05:04:32 +0000660 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000661 const char *ArgumentEnd = Argument + ArgumentLen;
662 while (1) {
663 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
664 const char *ExprEnd = Argument;
665 while (*ExprEnd != ':') {
666 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
667 ++ExprEnd;
668 }
669 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
670 Argument = ExprEnd + 1;
671 ExprEnd = std::find(Argument, ArgumentEnd, '|');
672 OutStr.append(Argument, ExprEnd);
673 return;
674 }
675 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
676 }
677}
678
679
Chris Lattner23be0672008-11-19 06:51:40 +0000680/// FormatDiagnostic - Format this diagnostic into a string, substituting the
681/// formal arguments into the %0 slots. The result is appended onto the Str
682/// array.
683void DiagnosticInfo::
684FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
685 const char *DiagStr = getDiags()->getDescription(getID());
686 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump11289f42009-09-09 15:08:12 +0000687
Chris Lattner23be0672008-11-19 06:51:40 +0000688 while (DiagStr != DiagEnd) {
689 if (DiagStr[0] != '%') {
690 // Append non-%0 substrings to Str if we have one.
691 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
692 OutStr.append(DiagStr, StrEnd);
693 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000694 continue;
Chris Lattner23be0672008-11-19 06:51:40 +0000695 } else if (DiagStr[1] == '%') {
696 OutStr.push_back('%'); // %% -> %.
697 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000698 continue;
699 }
Mike Stump11289f42009-09-09 15:08:12 +0000700
Chris Lattner2b786902008-11-21 07:50:02 +0000701 // Skip the %.
702 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000703
Chris Lattner2b786902008-11-21 07:50:02 +0000704 // This must be a placeholder for a diagnostic argument. The format for a
705 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
706 // The digit is a number from 0-9 indicating which argument this comes from.
707 // The modifier is a string of digits from the set [-a-z]+, arguments is a
708 // brace enclosed string.
709 const char *Modifier = 0, *Argument = 0;
710 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000711
Chris Lattner2b786902008-11-21 07:50:02 +0000712 // Check to see if we have a modifier. If so eat it.
713 if (!isdigit(DiagStr[0])) {
714 Modifier = DiagStr;
715 while (DiagStr[0] == '-' ||
716 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
717 ++DiagStr;
718 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000719
Chris Lattner2b786902008-11-21 07:50:02 +0000720 // If we have an argument, get it next.
721 if (DiagStr[0] == '{') {
722 ++DiagStr; // Skip {.
723 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000724
Chris Lattner2b786902008-11-21 07:50:02 +0000725 for (; DiagStr[0] != '}'; ++DiagStr)
726 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
727 ArgumentLen = DiagStr-Argument;
728 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000729 }
Chris Lattner2b786902008-11-21 07:50:02 +0000730 }
Mike Stump11289f42009-09-09 15:08:12 +0000731
Chris Lattner2b786902008-11-21 07:50:02 +0000732 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000733 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000734
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000735 switch (getArgKind(ArgNo)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000736 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000737 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000738 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000739 assert(ModifierLen == 0 && "No modifiers for strings yet");
740 OutStr.append(S.begin(), S.end());
741 break;
742 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000743 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000744 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000745 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000746
747 // Don't crash if get passed a null pointer by accident.
748 if (!S)
749 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattner2b786902008-11-21 07:50:02 +0000751 OutStr.append(S, S + strlen(S));
752 break;
753 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000754 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000755 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000756 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000757
Chris Lattner2b786902008-11-21 07:50:02 +0000758 if (ModifierIs(Modifier, ModifierLen, "select")) {
759 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
760 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
761 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000762 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
763 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000764 } else {
765 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000766 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000767 }
Chris Lattner2b786902008-11-21 07:50:02 +0000768 break;
769 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000770 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000771 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000772
Chris Lattner2b786902008-11-21 07:50:02 +0000773 if (ModifierIs(Modifier, ModifierLen, "select")) {
774 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
775 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
776 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000777 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
778 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000779 } else {
780 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000781 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000782 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000783 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000784 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000785 // ---- NAMES and TYPES ----
786 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000787 const IdentifierInfo *II = getArgIdentifier(ArgNo);
788 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000789
790 // Don't crash if get passed a null pointer by accident.
791 if (!II) {
792 const char *S = "(null)";
793 OutStr.append(S, S + strlen(S));
794 continue;
795 }
796
Daniel Dunbar07d07852009-10-18 21:17:35 +0000797 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000798 break;
799 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000800 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000801 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000802 case Diagnostic::ak_nameddecl:
Douglas Gregor053f6912009-08-26 00:04:55 +0000803 case Diagnostic::ak_nestednamespec:
Douglas Gregore40876a2009-10-13 21:16:44 +0000804 case Diagnostic::ak_declcontext:
Chris Lattner63ecc502008-11-23 09:21:17 +0000805 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
806 Modifier, ModifierLen,
807 Argument, ArgumentLen, OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000808 break;
Nico Weber4c311642008-08-10 19:59:06 +0000809 }
810 }
Nico Weber4c311642008-08-10 19:59:06 +0000811}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000812
813/// IncludeInDiagnosticCounts - This method (whose default implementation
814/// returns true) indicates whether the diagnostics handled by this
815/// DiagnosticClient should be included in the number of diagnostics
816/// reported by Diagnostic.
817bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }