blob: b541d084f063b1971ddfaaa4d4b5884eaa98ab44 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Diagnostic.h"
Chris Lattnere007de32009-04-15 07:01:18 +000015
16#include "clang/Lex/LexDiagnostic.h"
17#include "clang/Parse/ParseDiagnostic.h"
18#include "clang/AST/ASTDiagnostic.h"
19#include "clang/Sema/SemaDiagnostic.h"
20#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Analysis/AnalysisDiagnostic.h"
22#include "clang/Driver/DriverDiagnostic.h"
23
Chris Lattnerb91fd172008-11-19 07:32:16 +000024#include "clang/Basic/IdentifierTable.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000025#include "clang/Basic/SourceLocation.h"
Chris Lattner23be0672008-11-19 06:51:40 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner91aea712008-11-19 07:22:31 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattnere6535cf2007-12-02 01:09:57 +000028#include <vector>
29#include <map>
Chris Lattner0d799d32008-03-10 17:04:53 +000030#include <cstring>
Chris Lattner22eb9722006-06-18 05:43:12 +000031using namespace clang;
32
Chris Lattnere6535cf2007-12-02 01:09:57 +000033//===----------------------------------------------------------------------===//
34// Builtin Diagnostic information
35//===----------------------------------------------------------------------===//
36
Chris Lattnere007de32009-04-15 07:01:18 +000037// DefaultDiagnosticMappings - This specifies the default mapping for each diag,
Chris Lattner6a64cc62009-04-16 06:00:24 +000038// based on its kind.
Chris Lattnere007de32009-04-15 07:01:18 +000039
Chris Lattner6a64cc62009-04-16 06:00:24 +000040struct StaticDiagInfoRec {
Chris Lattnere007de32009-04-15 07:01:18 +000041 unsigned DiagID : 14;
42 unsigned Mapping : 2;
Chris Lattner6a64cc62009-04-16 06:00:24 +000043 const char *OptionGroup;
Chris Lattnere007de32009-04-15 07:01:18 +000044};
45
Chris Lattner6a64cc62009-04-16 06:00:24 +000046static const StaticDiagInfoRec StaticDiagInfo[] = {
Chris Lattnera5389672009-04-16 05:52:14 +000047#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP) \
Chris Lattner6a64cc62009-04-16 06:00:24 +000048 { diag::ENUM, DEFAULT_MAPPING-1, GROUP },
Chris Lattnere007de32009-04-15 07:01:18 +000049#include "clang/Basic/DiagnosticCommonKinds.inc"
50#include "clang/Basic/DiagnosticDriverKinds.inc"
51#include "clang/Basic/DiagnosticFrontendKinds.inc"
52#include "clang/Basic/DiagnosticLexKinds.inc"
53#include "clang/Basic/DiagnosticParseKinds.inc"
54#include "clang/Basic/DiagnosticASTKinds.inc"
55#include "clang/Basic/DiagnosticSemaKinds.inc"
56#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner6a64cc62009-04-16 06:00:24 +000057{ 0, 0, 0 }
Chris Lattnere007de32009-04-15 07:01:18 +000058};
Chris Lattnere6c831d2009-04-15 16:56:26 +000059#undef DIAG
Chris Lattnere007de32009-04-15 07:01:18 +000060
Chris Lattner6a64cc62009-04-16 06:00:24 +000061static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner411c0ff2009-04-16 04:12:40 +000062 // FIXME: Binary search.
Chris Lattner6a64cc62009-04-16 06:00:24 +000063 for (unsigned i = 0, e = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0]);
Chris Lattner411c0ff2009-04-16 04:12:40 +000064 i != e; ++i)
Chris Lattner6a64cc62009-04-16 06:00:24 +000065 if (StaticDiagInfo[i].DiagID == DiagID)
66 return &StaticDiagInfo[i];
67 return 0;
68}
69
70static unsigned GetDefaultDiagMapping(unsigned DiagID) {
71 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
72 return Info->Mapping+1;
Chris Lattner411c0ff2009-04-16 04:12:40 +000073 return diag::MAP_FATAL;
74}
75
Chris Lattner22cb8182009-04-16 05:44:38 +000076/// getWarningOptionForDiag - Return the lowest-level warning option that
77/// enables the specified diagnostic. If there is no -Wfoo flag that controls
78/// the diagnostic, this returns null.
79const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) {
Chris Lattner6a64cc62009-04-16 06:00:24 +000080 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
81 return Info->OptionGroup;
82 return 0;
Chris Lattner22cb8182009-04-16 05:44:38 +000083}
84
Chris Lattner411c0ff2009-04-16 04:12:40 +000085
Chris Lattnere007de32009-04-15 07:01:18 +000086// Diagnostic classes.
Chris Lattner22eb9722006-06-18 05:43:12 +000087enum {
Chris Lattnere6c831d2009-04-15 16:56:26 +000088 CLASS_NOTE = 0x01,
89 CLASS_WARNING = 0x02,
90 CLASS_EXTENSION = 0x03,
91 CLASS_ERROR = 0x04
Chris Lattner22eb9722006-06-18 05:43:12 +000092};
93
Chris Lattnere007de32009-04-15 07:01:18 +000094/// DiagnosticClasses - The class for each diagnostic.
Chris Lattnera5389672009-04-16 05:52:14 +000095#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP) CLASS,
Chris Lattnere007de32009-04-15 07:01:18 +000096static unsigned char DiagnosticClassesCommon[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +000097#include "clang/Basic/DiagnosticCommonKinds.inc"
Chris Lattner22eb9722006-06-18 05:43:12 +000098 0
99};
Chris Lattnere007de32009-04-15 07:01:18 +0000100static unsigned char DiagnosticClassesDriver[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000101#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000102 0
103};
Chris Lattnere007de32009-04-15 07:01:18 +0000104static unsigned char DiagnosticClassesFrontend[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000105#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar4f495982009-03-12 10:14:16 +0000106 0
107};
Chris Lattnere007de32009-04-15 07:01:18 +0000108static unsigned char DiagnosticClassesLex[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000109#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000110 0
111};
Chris Lattnere007de32009-04-15 07:01:18 +0000112static unsigned char DiagnosticClassesParse[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000113#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000114 0
115};
Chris Lattnere007de32009-04-15 07:01:18 +0000116static unsigned char DiagnosticClassesAST[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000117#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000118 0
119};
Chris Lattnere007de32009-04-15 07:01:18 +0000120static unsigned char DiagnosticClassesSema[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000121#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000122 0
123};
Chris Lattnere007de32009-04-15 07:01:18 +0000124static unsigned char DiagnosticClassesAnalysis[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000125#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000126 0
127};
128#undef DIAG
Chris Lattner22eb9722006-06-18 05:43:12 +0000129
130/// getDiagClass - Return the class field of the diagnostic.
131///
Chris Lattner4431a1b2007-11-30 22:53:43 +0000132static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner4b6713e2009-01-29 17:46:13 +0000133 assert(DiagID < diag::DIAG_UPPER_LIMIT &&
Chris Lattner4431a1b2007-11-30 22:53:43 +0000134 "Diagnostic ID out of range!");
Chris Lattnerd9ecb8e2009-04-15 18:48:23 +0000135 unsigned char *Arr;
136 unsigned ArrSize;
137 if (DiagID <= diag::DIAG_START_DRIVER) {
138 DiagID -= 0;
139 Arr = DiagnosticClassesCommon;
140 ArrSize = sizeof(DiagnosticClassesCommon);
141 } else if (DiagID <= diag::DIAG_START_FRONTEND) {
142 DiagID -= diag::DIAG_START_DRIVER + 1;
143 Arr = DiagnosticClassesDriver;
144 ArrSize = sizeof(DiagnosticClassesDriver);
145 } else if (DiagID <= diag::DIAG_START_LEX) {
146 DiagID -= diag::DIAG_START_FRONTEND + 1;
147 Arr = DiagnosticClassesFrontend;
148 ArrSize = sizeof(DiagnosticClassesFrontend);
149 } else if (DiagID <= diag::DIAG_START_PARSE) {
150 DiagID -= diag::DIAG_START_LEX + 1;
151 Arr = DiagnosticClassesLex;
152 ArrSize = sizeof(DiagnosticClassesLex);
153 } else if (DiagID <= diag::DIAG_START_AST) {
154 DiagID -= diag::DIAG_START_PARSE + 1;
155 Arr = DiagnosticClassesParse;
156 ArrSize = sizeof(DiagnosticClassesParse);
157 } else if (DiagID <= diag::DIAG_START_SEMA) {
158 DiagID -= diag::DIAG_START_AST + 1;
159 Arr = DiagnosticClassesAST;
160 ArrSize = sizeof(DiagnosticClassesAST);
161
162 } else if (DiagID <= diag::DIAG_START_ANALYSIS) {
163 DiagID -= diag::DIAG_START_SEMA + 1;
164 Arr = DiagnosticClassesSema;
165 ArrSize = sizeof(DiagnosticClassesSema);
166 } else {
167 DiagID -= diag::DIAG_START_ANALYSIS + 1;
168 Arr = DiagnosticClassesAnalysis;
169 ArrSize = sizeof(DiagnosticClassesAnalysis);
170 }
171 return DiagID < ArrSize ? Arr[DiagID] : ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +0000172}
173
174/// DiagnosticText - An english message to print for the diagnostic. These
175/// should be localized.
Chris Lattnera5389672009-04-16 05:52:14 +0000176#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP) DESC,
Chris Lattner7368d582009-01-27 18:30:58 +0000177static const char * const DiagnosticTextCommon[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000178#include "clang/Basic/DiagnosticCommonKinds.inc"
Chris Lattner22eb9722006-06-18 05:43:12 +0000179 0
180};
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000181static const char * const DiagnosticTextDriver[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000182#include "clang/Basic/DiagnosticDriverKinds.inc"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000183 0
184};
Daniel Dunbar4f495982009-03-12 10:14:16 +0000185static const char * const DiagnosticTextFrontend[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000186#include "clang/Basic/DiagnosticFrontendKinds.inc"
Daniel Dunbar4f495982009-03-12 10:14:16 +0000187 0
188};
Chris Lattner7368d582009-01-27 18:30:58 +0000189static const char * const DiagnosticTextLex[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000190#include "clang/Basic/DiagnosticLexKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000191 0
192};
193static const char * const DiagnosticTextParse[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000194#include "clang/Basic/DiagnosticParseKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000195 0
196};
197static const char * const DiagnosticTextAST[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000198#include "clang/Basic/DiagnosticASTKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000199 0
200};
201static const char * const DiagnosticTextSema[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000202#include "clang/Basic/DiagnosticSemaKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000203 0
204};
205static const char * const DiagnosticTextAnalysis[] = {
Sebastian Redl90b6edd2009-03-19 23:18:26 +0000206#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner7368d582009-01-27 18:30:58 +0000207 0
208};
209#undef DIAG
Chris Lattner22eb9722006-06-18 05:43:12 +0000210
Chris Lattnere6535cf2007-12-02 01:09:57 +0000211//===----------------------------------------------------------------------===//
212// Custom Diagnostic information
213//===----------------------------------------------------------------------===//
214
215namespace clang {
216 namespace diag {
217 class CustomDiagInfo {
218 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
219 std::vector<DiagDesc> DiagInfo;
220 std::map<DiagDesc, unsigned> DiagIDs;
221 public:
222
223 /// getDescription - Return the description of the specified custom
224 /// diagnostic.
225 const char *getDescription(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000226 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000227 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000228 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattnere6535cf2007-12-02 01:09:57 +0000229 }
230
231 /// getLevel - Return the level of the specified custom diagnostic.
232 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000233 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000234 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000235 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000236 }
237
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000238 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message,
239 Diagnostic &Diags) {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000240 DiagDesc D(L, Message);
241 // Check to see if it already exists.
242 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
243 if (I != DiagIDs.end() && I->first == D)
244 return I->second;
245
246 // If not, assign a new ID.
Chris Lattner36790cf2009-01-29 06:55:46 +0000247 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000248 DiagIDs.insert(std::make_pair(D, ID));
249 DiagInfo.push_back(D);
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000250
251 // If this is a warning, and all warnings are supposed to map to errors,
252 // insert the mapping now.
253 if (L == Diagnostic::Warning && Diags.getWarningsAsErrors())
254 Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000255 return ID;
256 }
257 };
258
259 } // end diag namespace
260} // end clang namespace
261
262
263//===----------------------------------------------------------------------===//
264// Common Diagnostic implementation
265//===----------------------------------------------------------------------===//
266
Chris Lattner63ecc502008-11-23 09:21:17 +0000267static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
268 const char *Modifier, unsigned ML,
269 const char *Argument, unsigned ArgLen,
Chris Lattnercf868c42009-02-19 23:53:20 +0000270 llvm::SmallVectorImpl<char> &Output,
271 void *Cookie) {
Chris Lattner63ecc502008-11-23 09:21:17 +0000272 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000273 Output.append(Str, Str+strlen(Str));
274}
275
276
Ted Kremenek31691ae2008-08-07 17:49:57 +0000277Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattnere007de32009-04-15 07:01:18 +0000278 AllExtensionsSilenced = 0;
Chris Lattner8c800702008-05-29 15:36:45 +0000279 IgnoreAllWarnings = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000280 WarningsAsErrors = false;
Daniel Dunbar84b70f72008-09-12 18:10:20 +0000281 SuppressSystemWarnings = false;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000282 ExtBehavior = Ext_Ignore;
Chris Lattnerc49b9052007-05-28 00:46:44 +0000283
284 ErrorOccurred = false;
Chris Lattner9e031192009-02-06 04:16:02 +0000285 FatalErrorOccurred = false;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000286 NumDiagnostics = 0;
287 NumErrors = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000288 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000289 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000290 LastDiagLevel = Ignored;
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000291
Chris Lattner63ecc502008-11-23 09:21:17 +0000292 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000293 ArgToStringCookie = 0;
Chris Lattnere007de32009-04-15 07:01:18 +0000294
Chris Lattner411c0ff2009-04-16 04:12:40 +0000295 // Set all mappings to 'unset'.
296 memset(DiagMappings, 0, sizeof(DiagMappings));
Chris Lattnerae411572006-07-05 00:55:08 +0000297}
298
Chris Lattnere6535cf2007-12-02 01:09:57 +0000299Diagnostic::~Diagnostic() {
300 delete CustomDiagInfo;
301}
302
303/// getCustomDiagID - Return an ID for a diagnostic with the specified message
304/// and level. If this is the first request for this diagnosic, it is
305/// registered and created, otherwise the existing ID is returned.
306unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
307 if (CustomDiagInfo == 0)
308 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000309 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000310}
311
312
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000313/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
314/// level of the specified diagnostic ID is a Warning or Extension.
315/// This only works on builtin diagnostics, not custom ones, and is not legal to
316/// call on NOTEs.
317bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000318 return DiagID < diag::DIAG_UPPER_LIMIT &&
319 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000320}
321
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000322/// \brief Determine whether the given built-in diagnostic ID is a
323/// Note.
324bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000325 return DiagID < diag::DIAG_UPPER_LIMIT &&
326 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000327}
328
Chris Lattnere007de32009-04-15 07:01:18 +0000329/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
330/// ID is for an extension of some sort.
331///
332bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000333 return DiagID < diag::DIAG_UPPER_LIMIT &&
334 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattnere007de32009-04-15 07:01:18 +0000335}
336
Chris Lattner22eb9722006-06-18 05:43:12 +0000337
338/// getDescription - Given a diagnostic ID, return a description of the
339/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000340const char *Diagnostic::getDescription(unsigned DiagID) const {
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000341 if (DiagID < diag::DIAG_START_DRIVER)
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000342 return DiagnosticTextCommon[DiagID];
Daniel Dunbar4f495982009-03-12 10:14:16 +0000343 else if (DiagID < diag::DIAG_START_FRONTEND)
Daniel Dunbarc0b3e952009-03-12 08:55:43 +0000344 return DiagnosticTextDriver[DiagID - diag::DIAG_START_DRIVER - 1];
Daniel Dunbar4f495982009-03-12 10:14:16 +0000345 else if (DiagID < diag::DIAG_START_LEX)
346 return DiagnosticTextFrontend[DiagID - diag::DIAG_START_FRONTEND - 1];
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000347 else if (DiagID < diag::DIAG_START_PARSE)
348 return DiagnosticTextLex[DiagID - diag::DIAG_START_LEX - 1];
349 else if (DiagID < diag::DIAG_START_AST)
350 return DiagnosticTextParse[DiagID - diag::DIAG_START_PARSE - 1];
351 else if (DiagID < diag::DIAG_START_SEMA)
352 return DiagnosticTextAST[DiagID - diag::DIAG_START_AST - 1];
353 else if (DiagID < diag::DIAG_START_ANALYSIS)
354 return DiagnosticTextSema[DiagID - diag::DIAG_START_SEMA - 1];
355 else if (DiagID < diag::DIAG_UPPER_LIMIT)
356 return DiagnosticTextAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1];
Chris Lattner7368d582009-01-27 18:30:58 +0000357 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000358}
359
360/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
361/// object, classify the specified diagnostic ID into a Level, consumable by
362/// the DiagnosticClient.
363Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000364 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000365 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000366 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner4431a1b2007-11-30 22:53:43 +0000367
368 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000369 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000370 return getDiagnosticLevel(DiagID, DiagClass);
371}
372
373/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
374/// object, classify the specified diagnostic ID into a Level, consumable by
375/// the DiagnosticClient.
376Diagnostic::Level
377Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattnerae411572006-07-05 00:55:08 +0000378 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000379 // to error. Errors can only be mapped to fatal.
Chris Lattnere007de32009-04-15 07:01:18 +0000380 Diagnostic::Level Result = Diagnostic::Fatal;
Chris Lattner411c0ff2009-04-16 04:12:40 +0000381
382 // Get the mapping information, if unset, compute it lazily.
383 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
384 if (MappingInfo == 0) {
385 MappingInfo = GetDefaultDiagMapping(DiagID);
386 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
387 }
388
389 switch (MappingInfo & 7) {
390 default: assert(0 && "Unknown mapping!");
Chris Lattnere007de32009-04-15 07:01:18 +0000391 case diag::MAP_IGNORE:
Chris Lattnerb8e73152009-04-16 05:04:32 +0000392 // Ignore this, unless this is an extension diagnostic and we're mapping
393 // them onto warnings or errors.
394 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
395 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
396 (MappingInfo & 8) != 0) // User explicitly mapped it.
397 return Diagnostic::Ignored;
398 Result = Diagnostic::Warning;
399 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
400 break;
Chris Lattnere007de32009-04-15 07:01:18 +0000401 case diag::MAP_ERROR:
402 Result = Diagnostic::Error;
403 break;
404 case diag::MAP_FATAL:
405 Result = Diagnostic::Fatal;
406 break;
407 case diag::MAP_WARNING:
408 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner8c800702008-05-29 15:36:45 +0000409 if (IgnoreAllWarnings)
410 return Diagnostic::Ignored;
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000411
412 Result = Diagnostic::Warning;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000413
414 // If this is an extension diagnostic and we're in -pedantic-error mode, and
415 // if the user didn't explicitly map it, upgrade to an error.
416 if (ExtBehavior == Ext_Error &&
417 (MappingInfo & 8) == 0 &&
418 isBuiltinExtensionDiag(DiagID))
419 Result = Diagnostic::Error;
420
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000421 if (WarningsAsErrors)
422 Result = Diagnostic::Error;
423 break;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000424
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000425 case diag::MAP_WARNING_NO_WERROR:
426 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
427 // not be adjusted by -Werror or -pedantic-errors.
428 Result = Diagnostic::Warning;
429
430 // If warnings are globally mapped to ignore or error, do it.
431 if (IgnoreAllWarnings)
432 return Diagnostic::Ignored;
433
Chris Lattnere007de32009-04-15 07:01:18 +0000434 break;
Chris Lattner8c800702008-05-29 15:36:45 +0000435 }
Chris Lattnere007de32009-04-15 07:01:18 +0000436
437 // Okay, we're about to return this as a "diagnostic to emit" one last check:
438 // if this is any sort of extension warning, and if we're in an __extension__
439 // block, silence it.
440 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
441 return Diagnostic::Ignored;
Chris Lattner22eb9722006-06-18 05:43:12 +0000442
Chris Lattnere007de32009-04-15 07:01:18 +0000443 return Result;
Chris Lattner22eb9722006-06-18 05:43:12 +0000444}
445
Chris Lattner8488c822008-11-18 07:04:44 +0000446/// ProcessDiag - This is the method used to report a diagnostic that is
447/// finally fully formed.
Chris Lattner427c9c12008-11-22 00:59:29 +0000448void Diagnostic::ProcessDiag() {
449 DiagnosticInfo Info(this);
Douglas Gregor19367f52009-03-19 18:55:06 +0000450
Chris Lattner22eb9722006-06-18 05:43:12 +0000451 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000452 Diagnostic::Level DiagLevel;
453 unsigned DiagID = Info.getID();
Chris Lattner22eb9722006-06-18 05:43:12 +0000454
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000455 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
456 // in a system header.
457 bool ShouldEmitInSystemHeader;
458
459 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
460 // Handle custom diagnostics, which cannot be mapped.
461 DiagLevel = CustomDiagInfo->getLevel(DiagID);
462
463 // Custom diagnostics always are emitted in system headers.
464 ShouldEmitInSystemHeader = true;
465 } else {
466 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
467 // the diagnostic level was for the previous diagnostic so that it is
468 // filtered the same as the previous diagnostic.
469 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000470 if (DiagClass == CLASS_NOTE) {
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000471 DiagLevel = Diagnostic::Note;
472 ShouldEmitInSystemHeader = false; // extra consideration is needed
473 } else {
474 // If this is not an error and we are in a system header, we ignore it.
475 // Check the original Diag ID here, because we also want to ignore
476 // extensions and warnings in -Werror and -pedantic-errors modes, which
477 // *map* warnings/extensions to errors.
Chris Lattnere6c831d2009-04-15 16:56:26 +0000478 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000479
480 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
481 }
482 }
483
Douglas Gregor19367f52009-03-19 18:55:06 +0000484 if (DiagLevel != Diagnostic::Note) {
485 // Record that a fatal error occurred only when we see a second
486 // non-note diagnostic. This allows notes to be attached to the
487 // fatal error, but suppresses any diagnostics that follow those
488 // notes.
489 if (LastDiagLevel == Diagnostic::Fatal)
490 FatalErrorOccurred = true;
491
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000492 LastDiagLevel = DiagLevel;
Douglas Gregor19367f52009-03-19 18:55:06 +0000493 }
494
495 // If a fatal error has already been emitted, silence all subsequent
496 // diagnostics.
497 if (FatalErrorOccurred)
498 return;
499
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000500 // If the client doesn't care about this message, don't issue it. If this is
501 // a note and the last real diagnostic was ignored, ignore it too.
502 if (DiagLevel == Diagnostic::Ignored ||
503 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Chris Lattnercb283342006-06-18 06:48:37 +0000504 return;
Nico Weber4c311642008-08-10 19:59:06 +0000505
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000506 // If this diagnostic is in a system header and is not a clang error, suppress
507 // it.
508 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner8488c822008-11-18 07:04:44 +0000509 Info.getLocation().isValid() &&
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000510 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000511 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
512 LastDiagLevel = Diagnostic::Ignored;
Chris Lattner3ac96992008-02-03 09:00:04 +0000513 return;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000514 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000515
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000516 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000517 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000518 ++NumErrors;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000519 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000520
Chris Lattner22eb9722006-06-18 05:43:12 +0000521 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000522 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekea06ec12009-01-23 20:28:53 +0000523 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000524
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000525 CurDiagID = ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +0000526}
527
Nico Weber4c311642008-08-10 19:59:06 +0000528
Chris Lattner22eb9722006-06-18 05:43:12 +0000529DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000530
Chris Lattner23be0672008-11-19 06:51:40 +0000531
Chris Lattner2b786902008-11-21 07:50:02 +0000532/// ModifierIs - Return true if the specified modifier matches specified string.
533template <std::size_t StrLen>
534static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
535 const char (&Str)[StrLen]) {
536 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
537}
538
539/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
540/// like this: %select{foo|bar|baz}2. This means that the integer argument
541/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
542/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
543/// This is very useful for certain classes of variant diagnostics.
544static void HandleSelectModifier(unsigned ValNo,
545 const char *Argument, unsigned ArgumentLen,
546 llvm::SmallVectorImpl<char> &OutStr) {
547 const char *ArgumentEnd = Argument+ArgumentLen;
548
549 // Skip over 'ValNo' |'s.
550 while (ValNo) {
551 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
552 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
553 " larger than the number of options in the diagnostic string!");
554 Argument = NextVal+1; // Skip this string.
555 --ValNo;
556 }
557
558 // Get the end of the value. This is either the } or the |.
559 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
560 // Add the value to the output string.
561 OutStr.append(Argument, EndPtr);
562}
563
564/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
565/// letter 's' to the string if the value is not 1. This is used in cases like
566/// this: "you idiot, you have %4 parameter%s4!".
567static void HandleIntegerSModifier(unsigned ValNo,
568 llvm::SmallVectorImpl<char> &OutStr) {
569 if (ValNo != 1)
570 OutStr.push_back('s');
571}
572
573
Sebastian Redl15b02d22008-11-22 13:44:36 +0000574/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000575static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000576 // Programming 101: Parse a decimal number :-)
577 unsigned Val = 0;
578 while (Start != End && *Start >= '0' && *Start <= '9') {
579 Val *= 10;
580 Val += *Start - '0';
581 ++Start;
582 }
583 return Val;
584}
585
586/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000587static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000588 if (*Start != '[') {
589 unsigned Ref = PluralNumber(Start, End);
590 return Ref == Val;
591 }
592
593 ++Start;
594 unsigned Low = PluralNumber(Start, End);
595 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
596 ++Start;
597 unsigned High = PluralNumber(Start, End);
598 assert(*Start == ']' && "Bad plural expression syntax: expected )");
599 ++Start;
600 return Low <= Val && Val <= High;
601}
602
603/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000604static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000605 // Empty condition?
606 if (*Start == ':')
607 return true;
608
609 while (1) {
610 char C = *Start;
611 if (C == '%') {
612 // Modulo expression
613 ++Start;
614 unsigned Arg = PluralNumber(Start, End);
615 assert(*Start == '=' && "Bad plural expression syntax: expected =");
616 ++Start;
617 unsigned ValMod = ValNo % Arg;
618 if (TestPluralRange(ValMod, Start, End))
619 return true;
620 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000621 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000622 "Bad plural expression syntax: unexpected character");
623 // Range expression
624 if (TestPluralRange(ValNo, Start, End))
625 return true;
626 }
627
628 // Scan for next or-expr part.
629 Start = std::find(Start, End, ',');
630 if(Start == End)
631 break;
632 ++Start;
633 }
634 return false;
635}
636
637/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
638/// for complex plural forms, or in languages where all plurals are complex.
639/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
640/// conditions that are tested in order, the form corresponding to the first
641/// that applies being emitted. The empty condition is always true, making the
642/// last form a default case.
643/// Conditions are simple boolean expressions, where n is the number argument.
644/// Here are the rules.
645/// condition := expression | empty
646/// empty := -> always true
647/// expression := numeric [',' expression] -> logical or
648/// numeric := range -> true if n in range
649/// | '%' number '=' range -> true if n % number in range
650/// range := number
651/// | '[' number ',' number ']' -> ranges are inclusive both ends
652///
653/// Here are some examples from the GNU gettext manual written in this form:
654/// English:
655/// {1:form0|:form1}
656/// Latvian:
657/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
658/// Gaeilge:
659/// {1:form0|2:form1|:form2}
660/// Romanian:
661/// {1:form0|0,%100=[1,19]:form1|:form2}
662/// Lithuanian:
663/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
664/// Russian (requires repeated form):
665/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
666/// Slovak
667/// {1:form0|[2,4]:form1|:form2}
668/// Polish (requires repeated form):
669/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
670static void HandlePluralModifier(unsigned ValNo,
671 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb8e73152009-04-16 05:04:32 +0000672 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000673 const char *ArgumentEnd = Argument + ArgumentLen;
674 while (1) {
675 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
676 const char *ExprEnd = Argument;
677 while (*ExprEnd != ':') {
678 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
679 ++ExprEnd;
680 }
681 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
682 Argument = ExprEnd + 1;
683 ExprEnd = std::find(Argument, ArgumentEnd, '|');
684 OutStr.append(Argument, ExprEnd);
685 return;
686 }
687 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
688 }
689}
690
691
Chris Lattner23be0672008-11-19 06:51:40 +0000692/// FormatDiagnostic - Format this diagnostic into a string, substituting the
693/// formal arguments into the %0 slots. The result is appended onto the Str
694/// array.
695void DiagnosticInfo::
696FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
697 const char *DiagStr = getDiags()->getDescription(getID());
698 const char *DiagEnd = DiagStr+strlen(DiagStr);
Nico Weber4c311642008-08-10 19:59:06 +0000699
Chris Lattner23be0672008-11-19 06:51:40 +0000700 while (DiagStr != DiagEnd) {
701 if (DiagStr[0] != '%') {
702 // Append non-%0 substrings to Str if we have one.
703 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
704 OutStr.append(DiagStr, StrEnd);
705 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000706 continue;
Chris Lattner23be0672008-11-19 06:51:40 +0000707 } else if (DiagStr[1] == '%') {
708 OutStr.push_back('%'); // %% -> %.
709 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000710 continue;
711 }
712
713 // Skip the %.
714 ++DiagStr;
715
716 // This must be a placeholder for a diagnostic argument. The format for a
717 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
718 // The digit is a number from 0-9 indicating which argument this comes from.
719 // The modifier is a string of digits from the set [-a-z]+, arguments is a
720 // brace enclosed string.
721 const char *Modifier = 0, *Argument = 0;
722 unsigned ModifierLen = 0, ArgumentLen = 0;
723
724 // Check to see if we have a modifier. If so eat it.
725 if (!isdigit(DiagStr[0])) {
726 Modifier = DiagStr;
727 while (DiagStr[0] == '-' ||
728 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
729 ++DiagStr;
730 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000731
Chris Lattner2b786902008-11-21 07:50:02 +0000732 // If we have an argument, get it next.
733 if (DiagStr[0] == '{') {
734 ++DiagStr; // Skip {.
735 Argument = DiagStr;
736
737 for (; DiagStr[0] != '}'; ++DiagStr)
738 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
739 ArgumentLen = DiagStr-Argument;
740 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000741 }
Chris Lattner2b786902008-11-21 07:50:02 +0000742 }
743
744 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000745 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000746
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000747 switch (getArgKind(ArgNo)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000748 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000749 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000750 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000751 assert(ModifierLen == 0 && "No modifiers for strings yet");
752 OutStr.append(S.begin(), S.end());
753 break;
754 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000755 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000756 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000757 assert(ModifierLen == 0 && "No modifiers for strings yet");
758 OutStr.append(S, S + strlen(S));
759 break;
760 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000761 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000762 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000763 int Val = getArgSInt(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000764
765 if (ModifierIs(Modifier, ModifierLen, "select")) {
766 HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
767 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
768 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000769 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
770 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000771 } else {
772 assert(ModifierLen == 0 && "Unknown integer modifier");
Chris Lattner91aea712008-11-19 07:22:31 +0000773 // FIXME: Optimize
Chris Lattner2b786902008-11-21 07:50:02 +0000774 std::string S = llvm::itostr(Val);
Chris Lattner91aea712008-11-19 07:22:31 +0000775 OutStr.append(S.begin(), S.end());
Chris Lattner91aea712008-11-19 07:22:31 +0000776 }
Chris Lattner2b786902008-11-21 07:50:02 +0000777 break;
778 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000779 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000780 unsigned Val = getArgUInt(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000781
782 if (ModifierIs(Modifier, ModifierLen, "select")) {
783 HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
784 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
785 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000786 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
787 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000788 } else {
789 assert(ModifierLen == 0 && "Unknown integer modifier");
790
Chris Lattner91aea712008-11-19 07:22:31 +0000791 // FIXME: Optimize
Chris Lattner2b786902008-11-21 07:50:02 +0000792 std::string S = llvm::utostr_32(Val);
Chris Lattner91aea712008-11-19 07:22:31 +0000793 OutStr.append(S.begin(), S.end());
Chris Lattner91aea712008-11-19 07:22:31 +0000794 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000795 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000796 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000797 // ---- NAMES and TYPES ----
798 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000799 const IdentifierInfo *II = getArgIdentifier(ArgNo);
800 assert(ModifierLen == 0 && "No modifiers for strings yet");
Chris Lattner810d3302009-02-19 23:45:49 +0000801 OutStr.push_back('\'');
Chris Lattnere3d20d92008-11-23 21:45:46 +0000802 OutStr.append(II->getName(), II->getName() + II->getLength());
803 OutStr.push_back('\'');
804 break;
805 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000806 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000807 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000808 case Diagnostic::ak_nameddecl:
Chris Lattner63ecc502008-11-23 09:21:17 +0000809 getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
810 Modifier, ModifierLen,
811 Argument, ArgumentLen, OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000812 break;
Nico Weber4c311642008-08-10 19:59:06 +0000813 }
814 }
Nico Weber4c311642008-08-10 19:59:06 +0000815}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000816
817/// IncludeInDiagnosticCounts - This method (whose default implementation
818/// returns true) indicates whether the diagnostics handled by this
819/// DiagnosticClient should be included in the number of diagnostics
820/// reported by Diagnostic.
821bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }