blob: 68548da0cb18b34fd03bd797ac174676112af08b [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
Chris Lattner27ceb9d2009-04-15 07:01:18 +000014#include "clang/AST/ASTDiagnostic.h"
Chris Lattner27ceb9d2009-04-15 07:01:18 +000015#include "clang/Analysis/AnalysisDiagnostic.h"
Ted Kremenekec55c942010-04-12 19:54:17 +000016#include "clang/Basic/Diagnostic.h"
Douglas Gregord93256e2010-01-28 06:00:51 +000017#include "clang/Basic/FileManager.h"
Chris Lattner43b628c2008-11-19 07:32:16 +000018#include "clang/Basic/IdentifierTable.h"
Ted Kremenekec55c942010-04-12 19:54:17 +000019#include "clang/Basic/PartialDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/SourceLocation.h"
Douglas Gregord93256e2010-01-28 06:00:51 +000021#include "clang/Basic/SourceManager.h"
Ted Kremenekec55c942010-04-12 19:54:17 +000022#include "clang/Driver/DriverDiagnostic.h"
23#include "clang/Frontend/FrontendDiagnostic.h"
24#include "clang/Lex/LexDiagnostic.h"
25#include "clang/Parse/ParseDiagnostic.h"
26#include "clang/Sema/SemaDiagnostic.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000027#include "llvm/ADT/SmallVector.h"
Chris Lattner30bc9652008-11-19 07:22:31 +000028#include "llvm/ADT/StringExtras.h"
Ted Kremenekec55c942010-04-12 19:54:17 +000029#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar23e47c62009-10-17 18:12:14 +000030#include "llvm/Support/raw_ostream.h"
Ted Kremenekec55c942010-04-12 19:54:17 +000031
Chris Lattner182745a2007-12-02 01:09:57 +000032#include <vector>
33#include <map>
Chris Lattner87cf5ac2008-03-10 17:04:53 +000034#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000035using namespace clang;
36
Chris Lattner182745a2007-12-02 01:09:57 +000037//===----------------------------------------------------------------------===//
38// Builtin Diagnostic information
39//===----------------------------------------------------------------------===//
40
Chris Lattner121f60c2009-04-16 06:07:15 +000041// Diagnostic classes.
42enum {
43 CLASS_NOTE = 0x01,
44 CLASS_WARNING = 0x02,
45 CLASS_EXTENSION = 0x03,
46 CLASS_ERROR = 0x04
47};
Chris Lattner27ceb9d2009-04-15 07:01:18 +000048
Chris Lattner33dd2822009-04-16 06:00:24 +000049struct StaticDiagInfoRec {
Chris Lattner121f60c2009-04-16 06:07:15 +000050 unsigned short DiagID;
51 unsigned Mapping : 3;
52 unsigned Class : 3;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000053 bool SFINAE : 1;
Chris Lattner27b0f512010-05-04 20:44:26 +000054 unsigned Category : 5;
55
Chris Lattner121f60c2009-04-16 06:07:15 +000056 const char *Description;
Chris Lattner33dd2822009-04-16 06:00:24 +000057 const char *OptionGroup;
Mike Stump1eb44332009-09-09 15:08:12 +000058
Chris Lattner87d854e2009-04-16 06:13:46 +000059 bool operator<(const StaticDiagInfoRec &RHS) const {
60 return DiagID < RHS.DiagID;
61 }
62 bool operator>(const StaticDiagInfoRec &RHS) const {
63 return DiagID > RHS.DiagID;
64 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +000065};
66
Chris Lattner33dd2822009-04-16 06:00:24 +000067static const StaticDiagInfoRec StaticDiagInfo[] = {
Chris Lattner27b0f512010-05-04 20:44:26 +000068#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,SFINAE, CATEGORY) \
69 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, CATEGORY, DESC, GROUP },
Chris Lattner27ceb9d2009-04-15 07:01:18 +000070#include "clang/Basic/DiagnosticCommonKinds.inc"
71#include "clang/Basic/DiagnosticDriverKinds.inc"
72#include "clang/Basic/DiagnosticFrontendKinds.inc"
73#include "clang/Basic/DiagnosticLexKinds.inc"
74#include "clang/Basic/DiagnosticParseKinds.inc"
75#include "clang/Basic/DiagnosticASTKinds.inc"
76#include "clang/Basic/DiagnosticSemaKinds.inc"
77#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Chris Lattner27b0f512010-05-04 20:44:26 +000078 { 0, 0, 0, 0, 0, 0, 0}
Chris Lattner27ceb9d2009-04-15 07:01:18 +000079};
Chris Lattner8a941e02009-04-15 16:56:26 +000080#undef DIAG
Chris Lattner27ceb9d2009-04-15 07:01:18 +000081
Chris Lattner87d854e2009-04-16 06:13:46 +000082/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
83/// or null if the ID is invalid.
Chris Lattner33dd2822009-04-16 06:00:24 +000084static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner87d854e2009-04-16 06:13:46 +000085 unsigned NumDiagEntries = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
86
87 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
88#ifndef NDEBUG
89 static bool IsFirst = true;
90 if (IsFirst) {
Chris Lattner5a3ce9b2009-10-16 02:34:51 +000091 for (unsigned i = 1; i != NumDiagEntries; ++i) {
92 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
93 "Diag ID conflict, the enums at the start of clang::diag (in "
94 "Diagnostic.h) probably need to be increased");
95
Chris Lattner87d854e2009-04-16 06:13:46 +000096 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
97 "Improperly sorted diag info");
Chris Lattner5a3ce9b2009-10-16 02:34:51 +000098 }
Chris Lattner87d854e2009-04-16 06:13:46 +000099 IsFirst = false;
100 }
101#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner87d854e2009-04-16 06:13:46 +0000103 // Search the diagnostic table with a binary search.
Chris Lattner27b0f512010-05-04 20:44:26 +0000104 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0, 0 };
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Chris Lattner87d854e2009-04-16 06:13:46 +0000106 const StaticDiagInfoRec *Found =
107 std::lower_bound(StaticDiagInfo, StaticDiagInfo + NumDiagEntries, Find);
108 if (Found == StaticDiagInfo + NumDiagEntries ||
109 Found->DiagID != DiagID)
110 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner87d854e2009-04-16 06:13:46 +0000112 return Found;
Chris Lattner33dd2822009-04-16 06:00:24 +0000113}
114
115static unsigned GetDefaultDiagMapping(unsigned DiagID) {
116 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Chris Lattner121f60c2009-04-16 06:07:15 +0000117 return Info->Mapping;
Chris Lattner691f1ae2009-04-16 04:12:40 +0000118 return diag::MAP_FATAL;
119}
120
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000121/// getWarningOptionForDiag - Return the lowest-level warning option that
122/// enables the specified diagnostic. If there is no -Wfoo flag that controls
123/// the diagnostic, this returns null.
124const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) {
Chris Lattner33dd2822009-04-16 06:00:24 +0000125 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
126 return Info->OptionGroup;
127 return 0;
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000128}
129
Chris Lattnerc9b88902010-05-04 21:13:21 +0000130/// getWarningOptionForDiag - Return the category number that a specified
131/// DiagID belongs to, or 0 if no category.
132unsigned Diagnostic::getCategoryNumberForDiag(unsigned DiagID) {
133 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
134 return Info->Category;
135 return 0;
136}
137
138/// getCategoryNameFromID - Given a category ID, return the name of the
139/// category, an empty string if CategoryID is zero, or null if CategoryID is
140/// invalid.
141const char *Diagnostic::getCategoryNameFromID(unsigned CategoryID) {
142 // Second the table of options, sorted by name for fast binary lookup.
143 static const char *CategoryNameTable[] = {
144#define GET_CATEGORY_TABLE
145#define CATEGORY(X) X,
146#include "clang/Basic/DiagnosticGroups.inc"
147#undef GET_CATEGORY_TABLE
148 "<<END>>"
149 };
150 static const size_t CategoryNameTableSize =
151 sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
152
153 if (CategoryID >= CategoryNameTableSize) return 0;
154 return CategoryNameTable[CategoryID];
155}
156
157
158
Douglas Gregoreab5d1e2010-03-25 22:17:48 +0000159Diagnostic::SFINAEResponse
160Diagnostic::getDiagnosticSFINAEResponse(unsigned DiagID) {
161 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
162 if (!Info->SFINAE)
163 return SFINAE_Report;
164
165 if (Info->Class == CLASS_ERROR)
166 return SFINAE_SubstitutionFailure;
167
168 // Suppress notes, warnings, and extensions;
169 return SFINAE_Suppress;
170 }
171
172 return SFINAE_Report;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000173}
174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175/// getDiagClass - Return the class field of the diagnostic.
176///
Chris Lattner07506182007-11-30 22:53:43 +0000177static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner121f60c2009-04-16 06:07:15 +0000178 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
179 return Info->Class;
180 return ~0U;
Reid Spencer5f016e22007-07-11 17:01:13 +0000181}
182
Chris Lattner182745a2007-12-02 01:09:57 +0000183//===----------------------------------------------------------------------===//
184// Custom Diagnostic information
185//===----------------------------------------------------------------------===//
186
187namespace clang {
188 namespace diag {
189 class CustomDiagInfo {
190 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
191 std::vector<DiagDesc> DiagInfo;
192 std::map<DiagDesc, unsigned> DiagIDs;
193 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Chris Lattner182745a2007-12-02 01:09:57 +0000195 /// getDescription - Return the description of the specified custom
196 /// diagnostic.
197 const char *getDescription(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000198 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000199 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000200 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattner182745a2007-12-02 01:09:57 +0000201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Chris Lattner182745a2007-12-02 01:09:57 +0000203 /// getLevel - Return the level of the specified custom diagnostic.
204 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000205 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000206 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000207 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattner182745a2007-12-02 01:09:57 +0000208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Daniel Dunbar32d4d802009-12-01 17:42:06 +0000210 unsigned getOrCreateDiagID(Diagnostic::Level L, llvm::StringRef Message,
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000211 Diagnostic &Diags) {
Chris Lattner182745a2007-12-02 01:09:57 +0000212 DiagDesc D(L, Message);
213 // Check to see if it already exists.
214 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
215 if (I != DiagIDs.end() && I->first == D)
216 return I->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Chris Lattner182745a2007-12-02 01:09:57 +0000218 // If not, assign a new ID.
Chris Lattner88eccaf2009-01-29 06:55:46 +0000219 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattner182745a2007-12-02 01:09:57 +0000220 DiagIDs.insert(std::make_pair(D, ID));
221 DiagInfo.push_back(D);
222 return ID;
223 }
224 };
Mike Stump1eb44332009-09-09 15:08:12 +0000225
226 } // end diag namespace
227} // end clang namespace
Chris Lattner182745a2007-12-02 01:09:57 +0000228
229
230//===----------------------------------------------------------------------===//
231// Common Diagnostic implementation
232//===----------------------------------------------------------------------===//
233
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000234static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
235 const char *Modifier, unsigned ML,
236 const char *Argument, unsigned ArgLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000237 const Diagnostic::ArgumentValue *PrevArgs,
238 unsigned NumPrevArgs,
Chris Lattner92dd3862009-02-19 23:53:20 +0000239 llvm::SmallVectorImpl<char> &Output,
240 void *Cookie) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000241 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +0000242 Output.append(Str, Str+strlen(Str));
243}
244
245
Ted Kremenekb4398aa2008-08-07 17:49:57 +0000246Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000247 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +0000248 ArgToStringCookie = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Douglas Gregorabc563f2010-07-19 21:46:24 +0000250 Reset();
Reid Spencer5f016e22007-07-11 17:01:13 +0000251}
252
Chris Lattner182745a2007-12-02 01:09:57 +0000253Diagnostic::~Diagnostic() {
254 delete CustomDiagInfo;
255}
256
Chris Lattner04ae2df2009-07-12 21:18:45 +0000257
258void Diagnostic::pushMappings() {
John Thompsonca2c3e22009-10-23 02:21:17 +0000259 // Avoids undefined behavior when the stack has to resize.
260 DiagMappingsStack.reserve(DiagMappingsStack.size() + 1);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000261 DiagMappingsStack.push_back(DiagMappingsStack.back());
262}
263
264bool Diagnostic::popMappings() {
265 if (DiagMappingsStack.size() == 1)
266 return false;
267
268 DiagMappingsStack.pop_back();
269 return true;
270}
271
Chris Lattner182745a2007-12-02 01:09:57 +0000272/// getCustomDiagID - Return an ID for a diagnostic with the specified message
273/// and level. If this is the first request for this diagnosic, it is
274/// registered and created, otherwise the existing ID is returned.
Daniel Dunbar32d4d802009-12-01 17:42:06 +0000275unsigned Diagnostic::getCustomDiagID(Level L, llvm::StringRef Message) {
Mike Stump1eb44332009-09-09 15:08:12 +0000276 if (CustomDiagInfo == 0)
Chris Lattner182745a2007-12-02 01:09:57 +0000277 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000278 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattner182745a2007-12-02 01:09:57 +0000279}
280
281
Chris Lattnerf5d23282009-02-17 06:49:55 +0000282/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
283/// level of the specified diagnostic ID is a Warning or Extension.
284/// This only works on builtin diagnostics, not custom ones, and is not legal to
285/// call on NOTEs.
286bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000287 return DiagID < diag::DIAG_UPPER_LIMIT &&
288 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Reid Spencer5f016e22007-07-11 17:01:13 +0000289}
290
Douglas Gregoree1828a2009-03-10 18:03:33 +0000291/// \brief Determine whether the given built-in diagnostic ID is a
292/// Note.
293bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000294 return DiagID < diag::DIAG_UPPER_LIMIT &&
295 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000296}
297
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000298/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
Chris Lattner04e44272010-04-12 21:53:11 +0000299/// ID is for an extension of some sort. This also returns EnabledByDefault,
300/// which is set to indicate whether the diagnostic is ignored by default (in
301/// which case -pedantic enables it) or treated as a warning/error by default.
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000302///
Chris Lattner04e44272010-04-12 21:53:11 +0000303bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID,
304 bool &EnabledByDefault) {
305 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
306 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
307 return false;
308
309 EnabledByDefault = StaticDiagInfo[DiagID].Mapping != diag::MAP_IGNORE;
310 return true;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000311}
312
Douglas Gregorabc563f2010-07-19 21:46:24 +0000313void Diagnostic::Reset() {
314 AllExtensionsSilenced = 0;
315 IgnoreAllWarnings = false;
316 WarningsAsErrors = false;
317 ErrorsAsFatal = false;
318 SuppressSystemWarnings = false;
319 SuppressAllDiagnostics = false;
320 ShowOverloads = Ovl_All;
321 ExtBehavior = Ext_Ignore;
322
323 ErrorOccurred = false;
324 FatalErrorOccurred = false;
325 ErrorLimit = 0;
326 TemplateBacktraceLimit = 0;
327
328 NumWarnings = 0;
329 NumErrors = 0;
330 NumErrorsSuppressed = 0;
331 CustomDiagInfo = 0;
332 CurDiagID = ~0U;
333 LastDiagLevel = Ignored;
334 DelayedDiagID = 0;
335
336 // Set all mappings to 'unset'.
337 while (!DiagMappingsStack.empty())
338 DiagMappingsStack.pop_back();
339
340 DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0);
341 DiagMappingsStack.push_back(BlankDiags);
342}
Reid Spencer5f016e22007-07-11 17:01:13 +0000343
344/// getDescription - Given a diagnostic ID, return a description of the
345/// issue.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000346const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner121f60c2009-04-16 06:07:15 +0000347 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
348 return Info->Description;
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000349 return CustomDiagInfo->getDescription(DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000350}
351
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000352void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
353 llvm::StringRef Arg2) {
354 if (DelayedDiagID)
355 return;
356
357 DelayedDiagID = DiagID;
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000358 DelayedDiagArg1 = Arg1.str();
359 DelayedDiagArg2 = Arg2.str();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000360}
361
362void Diagnostic::ReportDelayed() {
363 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
364 DelayedDiagID = 0;
365 DelayedDiagArg1.clear();
366 DelayedDiagArg2.clear();
367}
368
Reid Spencer5f016e22007-07-11 17:01:13 +0000369/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
370/// object, classify the specified diagnostic ID into a Level, consumable by
371/// the DiagnosticClient.
372Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattner182745a2007-12-02 01:09:57 +0000373 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000374 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattner182745a2007-12-02 01:09:57 +0000375 return CustomDiagInfo->getLevel(DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattner07506182007-11-30 22:53:43 +0000377 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000378 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerf5d23282009-02-17 06:49:55 +0000379 return getDiagnosticLevel(DiagID, DiagClass);
380}
381
382/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
383/// object, classify the specified diagnostic ID into a Level, consumable by
384/// the DiagnosticClient.
385Diagnostic::Level
386Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerf5d23282009-02-17 06:49:55 +0000388 // to error. Errors can only be mapped to fatal.
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000389 Diagnostic::Level Result = Diagnostic::Fatal;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattner691f1ae2009-04-16 04:12:40 +0000391 // Get the mapping information, if unset, compute it lazily.
392 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
393 if (MappingInfo == 0) {
394 MappingInfo = GetDefaultDiagMapping(DiagID);
395 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Chris Lattner691f1ae2009-04-16 04:12:40 +0000398 switch (MappingInfo & 7) {
399 default: assert(0 && "Unknown mapping!");
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000400 case diag::MAP_IGNORE:
Chris Lattnerb54b2762009-04-16 05:04:32 +0000401 // Ignore this, unless this is an extension diagnostic and we're mapping
402 // them onto warnings or errors.
403 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
404 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
405 (MappingInfo & 8) != 0) // User explicitly mapped it.
406 return Diagnostic::Ignored;
407 Result = Diagnostic::Warning;
408 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
Chris Lattnere663c722009-12-22 23:12:53 +0000409 if (Result == Diagnostic::Error && ErrorsAsFatal)
410 Result = Diagnostic::Fatal;
Chris Lattnerb54b2762009-04-16 05:04:32 +0000411 break;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000412 case diag::MAP_ERROR:
413 Result = Diagnostic::Error;
Chris Lattnere663c722009-12-22 23:12:53 +0000414 if (ErrorsAsFatal)
415 Result = Diagnostic::Fatal;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000416 break;
417 case diag::MAP_FATAL:
418 Result = Diagnostic::Fatal;
419 break;
420 case diag::MAP_WARNING:
421 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner5b4681c2008-05-29 15:36:45 +0000422 if (IgnoreAllWarnings)
423 return Diagnostic::Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000425 Result = Diagnostic::Warning;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Chris Lattnerb54b2762009-04-16 05:04:32 +0000427 // If this is an extension diagnostic and we're in -pedantic-error mode, and
428 // if the user didn't explicitly map it, upgrade to an error.
429 if (ExtBehavior == Ext_Error &&
430 (MappingInfo & 8) == 0 &&
431 isBuiltinExtensionDiag(DiagID))
432 Result = Diagnostic::Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000434 if (WarningsAsErrors)
435 Result = Diagnostic::Error;
Chris Lattnere663c722009-12-22 23:12:53 +0000436 if (Result == Diagnostic::Error && ErrorsAsFatal)
437 Result = Diagnostic::Fatal;
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000438 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000440 case diag::MAP_WARNING_NO_WERROR:
441 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
442 // not be adjusted by -Werror or -pedantic-errors.
443 Result = Diagnostic::Warning;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000445 // If warnings are globally mapped to ignore or error, do it.
446 if (IgnoreAllWarnings)
447 return Diagnostic::Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000449 break;
Chris Lattnere663c722009-12-22 23:12:53 +0000450
451 case diag::MAP_ERROR_NO_WFATAL:
452 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
453 // unaffected by -Wfatal-errors.
454 Result = Diagnostic::Error;
455 break;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000456 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000457
458 // Okay, we're about to return this as a "diagnostic to emit" one last check:
459 // if this is any sort of extension warning, and if we're in an __extension__
460 // block, silence it.
461 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
462 return Diagnostic::Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000464 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000465}
466
Chris Lattner3bc172b2009-04-19 22:34:23 +0000467struct WarningOption {
468 const char *Name;
469 const short *Members;
Chandler Carruth5ef12b32010-05-13 07:43:05 +0000470 const short *SubGroups;
Chris Lattner3bc172b2009-04-19 22:34:23 +0000471};
472
473#define GET_DIAG_ARRAYS
474#include "clang/Basic/DiagnosticGroups.inc"
475#undef GET_DIAG_ARRAYS
476
477// Second the table of options, sorted by name for fast binary lookup.
478static const WarningOption OptionTable[] = {
479#define GET_DIAG_TABLE
480#include "clang/Basic/DiagnosticGroups.inc"
481#undef GET_DIAG_TABLE
482};
483static const size_t OptionTableSize =
484sizeof(OptionTable) / sizeof(OptionTable[0]);
485
486static bool WarningOptionCompare(const WarningOption &LHS,
487 const WarningOption &RHS) {
488 return strcmp(LHS.Name, RHS.Name) < 0;
489}
490
491static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
492 Diagnostic &Diags) {
493 // Option exists, poke all the members of its diagnostic set.
494 if (const short *Member = Group->Members) {
495 for (; *Member != -1; ++Member)
496 Diags.setDiagnosticMapping(*Member, Mapping);
497 }
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattner3bc172b2009-04-19 22:34:23 +0000499 // Enable/disable all subgroups along with this one.
Chandler Carruth5ef12b32010-05-13 07:43:05 +0000500 if (const short *SubGroups = Group->SubGroups) {
501 for (; *SubGroups != (short)-1; ++SubGroups)
502 MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Diags);
Chris Lattner3bc172b2009-04-19 22:34:23 +0000503 }
504}
505
506/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
507/// "unknown-pragmas" to have the specified mapping. This returns true and
508/// ignores the request if "Group" was unknown, false otherwise.
509bool Diagnostic::setDiagnosticGroupMapping(const char *Group,
510 diag::Mapping Map) {
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Chris Lattner3bc172b2009-04-19 22:34:23 +0000512 WarningOption Key = { Group, 0, 0 };
513 const WarningOption *Found =
514 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
515 WarningOptionCompare);
516 if (Found == OptionTable + OptionTableSize ||
517 strcmp(Found->Name, Group) != 0)
518 return true; // Option not found.
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Chris Lattner3bc172b2009-04-19 22:34:23 +0000520 MapGroupMembers(Found, Map, *this);
521 return false;
522}
523
524
Chris Lattner0a14eee2008-11-18 07:04:44 +0000525/// ProcessDiag - This is the method used to report a diagnostic that is
526/// finally fully formed.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000527bool Diagnostic::ProcessDiag() {
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000528 DiagnosticInfo Info(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Douglas Gregor81b747b2009-09-17 21:32:03 +0000530 if (SuppressAllDiagnostics)
531 return false;
532
Reid Spencer5f016e22007-07-11 17:01:13 +0000533 // Figure out the diagnostic level of this message.
Chris Lattnerf5d23282009-02-17 06:49:55 +0000534 Diagnostic::Level DiagLevel;
535 unsigned DiagID = Info.getID();
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattnerf5d23282009-02-17 06:49:55 +0000537 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
538 // in a system header.
539 bool ShouldEmitInSystemHeader;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattnerf5d23282009-02-17 06:49:55 +0000541 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
542 // Handle custom diagnostics, which cannot be mapped.
543 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattnerf5d23282009-02-17 06:49:55 +0000545 // Custom diagnostics always are emitted in system headers.
546 ShouldEmitInSystemHeader = true;
547 } else {
548 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
549 // the diagnostic level was for the previous diagnostic so that it is
550 // filtered the same as the previous diagnostic.
551 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000552 if (DiagClass == CLASS_NOTE) {
Chris Lattnerf5d23282009-02-17 06:49:55 +0000553 DiagLevel = Diagnostic::Note;
554 ShouldEmitInSystemHeader = false; // extra consideration is needed
555 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000556 // If this is not an error and we are in a system header, we ignore it.
Chris Lattnerf5d23282009-02-17 06:49:55 +0000557 // Check the original Diag ID here, because we also want to ignore
558 // extensions and warnings in -Werror and -pedantic-errors modes, which
559 // *map* warnings/extensions to errors.
Chris Lattner8a941e02009-04-15 16:56:26 +0000560 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Chris Lattnerf5d23282009-02-17 06:49:55 +0000562 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
563 }
564 }
565
Douglas Gregor525c4b02009-03-19 18:55:06 +0000566 if (DiagLevel != Diagnostic::Note) {
567 // Record that a fatal error occurred only when we see a second
568 // non-note diagnostic. This allows notes to be attached to the
569 // fatal error, but suppresses any diagnostics that follow those
570 // notes.
571 if (LastDiagLevel == Diagnostic::Fatal)
572 FatalErrorOccurred = true;
573
Chris Lattnerf5d23282009-02-17 06:49:55 +0000574 LastDiagLevel = DiagLevel;
Mike Stump1eb44332009-09-09 15:08:12 +0000575 }
Douglas Gregor525c4b02009-03-19 18:55:06 +0000576
577 // If a fatal error has already been emitted, silence all subsequent
578 // diagnostics.
Douglas Gregor1864f2e2010-04-14 22:19:45 +0000579 if (FatalErrorOccurred) {
580 if (DiagLevel >= Diagnostic::Error) {
581 ++NumErrors;
582 ++NumErrorsSuppressed;
583 }
584
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000585 return false;
Douglas Gregor1864f2e2010-04-14 22:19:45 +0000586 }
Douglas Gregor525c4b02009-03-19 18:55:06 +0000587
Chris Lattnerf5d23282009-02-17 06:49:55 +0000588 // If the client doesn't care about this message, don't issue it. If this is
589 // a note and the last real diagnostic was ignored, ignore it too.
590 if (DiagLevel == Diagnostic::Ignored ||
591 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000592 return false;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000593
Chris Lattnerf5d23282009-02-17 06:49:55 +0000594 // If this diagnostic is in a system header and is not a clang error, suppress
595 // it.
596 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner0a14eee2008-11-18 07:04:44 +0000597 Info.getLocation().isValid() &&
John McCall779cf422010-02-11 10:04:29 +0000598 Info.getLocation().getInstantiationLoc().isInSystemHeader() &&
Chris Lattner336f26b2009-02-17 06:52:20 +0000599 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
600 LastDiagLevel = Diagnostic::Ignored;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000601 return false;
Chris Lattner336f26b2009-02-17 06:52:20 +0000602 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 if (DiagLevel >= Diagnostic::Error) {
605 ErrorOccurred = true;
Chris Lattner0a14eee2008-11-18 07:04:44 +0000606 ++NumErrors;
Chris Lattnerb205ac92010-04-07 20:21:58 +0000607
608 // If we've emitted a lot of errors, emit a fatal error after it to stop a
609 // flood of bogus errors.
Chris Lattnerc1002142010-04-07 20:37:06 +0000610 if (ErrorLimit && NumErrors >= ErrorLimit &&
Chris Lattnerb205ac92010-04-07 20:21:58 +0000611 DiagLevel == Diagnostic::Error)
612 SetDelayedDiagnostic(diag::fatal_too_many_errors);
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 // Finally, report it.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000616 Client->HandleDiagnostic(DiagLevel, Info);
Chris Lattner53eee7b2010-04-07 18:47:42 +0000617 if (Client->IncludeInDiagnosticCounts()) {
618 if (DiagLevel == Diagnostic::Warning)
619 ++NumWarnings;
620 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000621
Douglas Gregoree1828a2009-03-10 18:03:33 +0000622 CurDiagID = ~0U;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000623
624 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000625}
626
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000627bool DiagnosticBuilder::Emit() {
628 // If DiagObj is null, then its soul was stolen by the copy ctor
629 // or the user called Emit().
630 if (DiagObj == 0) return false;
631
632 // When emitting diagnostics, we set the final argument count into
633 // the Diagnostic object.
634 DiagObj->NumDiagArgs = NumArgs;
635 DiagObj->NumDiagRanges = NumRanges;
Douglas Gregor849b2432010-03-31 17:46:05 +0000636 DiagObj->NumFixItHints = NumFixItHints;
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000637
638 // Process the diagnostic, sending the accumulated information to the
639 // DiagnosticClient.
640 bool Emitted = DiagObj->ProcessDiag();
641
642 // Clear out the current diagnostic object.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000643 unsigned DiagID = DiagObj->CurDiagID;
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000644 DiagObj->Clear();
645
646 // If there was a delayed diagnostic, emit it now.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000647 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000648 DiagObj->ReportDelayed();
649
650 // This diagnostic is dead.
651 DiagObj = 0;
652
653 return Emitted;
654}
655
Nico Weber7bfaaae2008-08-10 19:59:06 +0000656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657DiagnosticClient::~DiagnosticClient() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000658
Chris Lattnerf4c83962008-11-19 06:51:40 +0000659
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000660/// ModifierIs - Return true if the specified modifier matches specified string.
661template <std::size_t StrLen>
662static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
663 const char (&Str)[StrLen]) {
664 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
665}
666
John McCall909c1822010-01-14 20:11:39 +0000667/// ScanForward - Scans forward, looking for the given character, skipping
668/// nested clauses and escaped characters.
669static const char *ScanFormat(const char *I, const char *E, char Target) {
670 unsigned Depth = 0;
671
672 for ( ; I != E; ++I) {
673 if (Depth == 0 && *I == Target) return I;
674 if (Depth != 0 && *I == '}') Depth--;
675
676 if (*I == '%') {
677 I++;
678 if (I == E) break;
679
680 // Escaped characters get implicitly skipped here.
681
682 // Format specifier.
683 if (!isdigit(*I) && !ispunct(*I)) {
684 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
685 if (I == E) break;
686 if (*I == '{')
687 Depth++;
688 }
689 }
690 }
691 return E;
692}
693
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000694/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
695/// like this: %select{foo|bar|baz}2. This means that the integer argument
696/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
697/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
698/// This is very useful for certain classes of variant diagnostics.
John McCall9f286142010-01-13 23:58:20 +0000699static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000700 const char *Argument, unsigned ArgumentLen,
701 llvm::SmallVectorImpl<char> &OutStr) {
702 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000704 // Skip over 'ValNo' |'s.
705 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000706 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000707 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
708 " larger than the number of options in the diagnostic string!");
709 Argument = NextVal+1; // Skip this string.
710 --ValNo;
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000713 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000714 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000715
716 // Recursively format the result of the select clause into the output string.
717 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000718}
719
720/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
721/// letter 's' to the string if the value is not 1. This is used in cases like
722/// this: "you idiot, you have %4 parameter%s4!".
723static void HandleIntegerSModifier(unsigned ValNo,
724 llvm::SmallVectorImpl<char> &OutStr) {
725 if (ValNo != 1)
726 OutStr.push_back('s');
727}
728
John McCall3be16b72010-01-14 00:50:32 +0000729/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
730/// prints the ordinal form of the given integer, with 1 corresponding
731/// to the first ordinal. Currently this is hard-coded to use the
732/// English form.
733static void HandleOrdinalModifier(unsigned ValNo,
734 llvm::SmallVectorImpl<char> &OutStr) {
735 assert(ValNo != 0 && "ValNo must be strictly positive!");
736
737 llvm::raw_svector_ostream Out(OutStr);
738
739 // We could use text forms for the first N ordinals, but the numeric
740 // forms are actually nicer in diagnostics because they stand out.
741 Out << ValNo;
742
743 // It is critically important that we do this perfectly for
744 // user-written sequences with over 100 elements.
745 switch (ValNo % 100) {
746 case 11:
747 case 12:
748 case 13:
749 Out << "th"; return;
750 default:
751 switch (ValNo % 10) {
752 case 1: Out << "st"; return;
753 case 2: Out << "nd"; return;
754 case 3: Out << "rd"; return;
755 default: Out << "th"; return;
756 }
757 }
758}
759
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000760
Sebastian Redle4c452c2008-11-22 13:44:36 +0000761/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000762static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000763 // Programming 101: Parse a decimal number :-)
764 unsigned Val = 0;
765 while (Start != End && *Start >= '0' && *Start <= '9') {
766 Val *= 10;
767 Val += *Start - '0';
768 ++Start;
769 }
770 return Val;
771}
772
773/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000774static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000775 if (*Start != '[') {
776 unsigned Ref = PluralNumber(Start, End);
777 return Ref == Val;
778 }
779
780 ++Start;
781 unsigned Low = PluralNumber(Start, End);
782 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
783 ++Start;
784 unsigned High = PluralNumber(Start, End);
785 assert(*Start == ']' && "Bad plural expression syntax: expected )");
786 ++Start;
787 return Low <= Val && Val <= High;
788}
789
790/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000791static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000792 // Empty condition?
793 if (*Start == ':')
794 return true;
795
796 while (1) {
797 char C = *Start;
798 if (C == '%') {
799 // Modulo expression
800 ++Start;
801 unsigned Arg = PluralNumber(Start, End);
802 assert(*Start == '=' && "Bad plural expression syntax: expected =");
803 ++Start;
804 unsigned ValMod = ValNo % Arg;
805 if (TestPluralRange(ValMod, Start, End))
806 return true;
807 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000808 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000809 "Bad plural expression syntax: unexpected character");
810 // Range expression
811 if (TestPluralRange(ValNo, Start, End))
812 return true;
813 }
814
815 // Scan for next or-expr part.
816 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000817 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000818 break;
819 ++Start;
820 }
821 return false;
822}
823
824/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
825/// for complex plural forms, or in languages where all plurals are complex.
826/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
827/// conditions that are tested in order, the form corresponding to the first
828/// that applies being emitted. The empty condition is always true, making the
829/// last form a default case.
830/// Conditions are simple boolean expressions, where n is the number argument.
831/// Here are the rules.
832/// condition := expression | empty
833/// empty := -> always true
834/// expression := numeric [',' expression] -> logical or
835/// numeric := range -> true if n in range
836/// | '%' number '=' range -> true if n % number in range
837/// range := number
838/// | '[' number ',' number ']' -> ranges are inclusive both ends
839///
840/// Here are some examples from the GNU gettext manual written in this form:
841/// English:
842/// {1:form0|:form1}
843/// Latvian:
844/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
845/// Gaeilge:
846/// {1:form0|2:form1|:form2}
847/// Romanian:
848/// {1:form0|0,%100=[1,19]:form1|:form2}
849/// Lithuanian:
850/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
851/// Russian (requires repeated form):
852/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
853/// Slovak
854/// {1:form0|[2,4]:form1|:form2}
855/// Polish (requires repeated form):
856/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
857static void HandlePluralModifier(unsigned ValNo,
858 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb54b2762009-04-16 05:04:32 +0000859 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000860 const char *ArgumentEnd = Argument + ArgumentLen;
861 while (1) {
862 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
863 const char *ExprEnd = Argument;
864 while (*ExprEnd != ':') {
865 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
866 ++ExprEnd;
867 }
868 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
869 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000870 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
Sebastian Redle4c452c2008-11-22 13:44:36 +0000871 OutStr.append(Argument, ExprEnd);
872 return;
873 }
John McCall909c1822010-01-14 20:11:39 +0000874 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000875 }
876}
877
878
Chris Lattnerf4c83962008-11-19 06:51:40 +0000879/// FormatDiagnostic - Format this diagnostic into a string, substituting the
880/// formal arguments into the %0 slots. The result is appended onto the Str
881/// array.
882void DiagnosticInfo::
883FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
884 const char *DiagStr = getDiags()->getDescription(getID());
885 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000886
John McCall9f286142010-01-13 23:58:20 +0000887 FormatDiagnostic(DiagStr, DiagEnd, OutStr);
888}
889
890void DiagnosticInfo::
891FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
892 llvm::SmallVectorImpl<char> &OutStr) const {
893
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000894 /// FormattedArgs - Keep track of all of the arguments formatted by
895 /// ConvertArgToString and pass them into subsequent calls to
896 /// ConvertArgToString, allowing the implementation to avoid redundancies in
897 /// obvious cases.
898 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
899
Chris Lattnerf4c83962008-11-19 06:51:40 +0000900 while (DiagStr != DiagEnd) {
901 if (DiagStr[0] != '%') {
902 // Append non-%0 substrings to Str if we have one.
903 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
904 OutStr.append(DiagStr, StrEnd);
905 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000906 continue;
John McCall909c1822010-01-14 20:11:39 +0000907 } else if (ispunct(DiagStr[1])) {
908 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000909 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000910 continue;
911 }
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000913 // Skip the %.
914 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000916 // This must be a placeholder for a diagnostic argument. The format for a
917 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
918 // The digit is a number from 0-9 indicating which argument this comes from.
919 // The modifier is a string of digits from the set [-a-z]+, arguments is a
920 // brace enclosed string.
921 const char *Modifier = 0, *Argument = 0;
922 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000924 // Check to see if we have a modifier. If so eat it.
925 if (!isdigit(DiagStr[0])) {
926 Modifier = DiagStr;
927 while (DiagStr[0] == '-' ||
928 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
929 ++DiagStr;
930 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000931
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000932 // If we have an argument, get it next.
933 if (DiagStr[0] == '{') {
934 ++DiagStr; // Skip {.
935 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000936
John McCall909c1822010-01-14 20:11:39 +0000937 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
938 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000939 ArgumentLen = DiagStr-Argument;
940 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000941 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000944 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000945 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000946
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000947 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
948
949 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000950 // ---- STRINGS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000951 case Diagnostic::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000952 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000953 assert(ModifierLen == 0 && "No modifiers for strings yet");
954 OutStr.append(S.begin(), S.end());
955 break;
956 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000957 case Diagnostic::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000958 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000959 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000960
961 // Don't crash if get passed a null pointer by accident.
962 if (!S)
963 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000965 OutStr.append(S, S + strlen(S));
966 break;
967 }
Chris Lattner08631c52008-11-23 21:45:46 +0000968 // ---- INTEGERS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000969 case Diagnostic::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000970 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000972 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000973 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000974 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
975 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000976 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
977 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000978 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
979 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000980 } else {
981 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000982 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000983 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000984 break;
985 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000986 case Diagnostic::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000987 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000989 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000990 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000991 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
992 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000993 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
994 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000995 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
996 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000997 } else {
998 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000999 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +00001000 }
Chris Lattner22caddc2008-11-23 09:13:29 +00001001 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +00001002 }
Chris Lattner08631c52008-11-23 21:45:46 +00001003 // ---- NAMES and TYPES ----
1004 case Diagnostic::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +00001005 const IdentifierInfo *II = getArgIdentifier(ArgNo);
1006 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +00001007
1008 // Don't crash if get passed a null pointer by accident.
1009 if (!II) {
1010 const char *S = "(null)";
1011 OutStr.append(S, S + strlen(S));
1012 continue;
1013 }
1014
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001015 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +00001016 break;
1017 }
Chris Lattner22caddc2008-11-23 09:13:29 +00001018 case Diagnostic::ak_qualtype:
Chris Lattner011bb4e2008-11-23 20:28:15 +00001019 case Diagnostic::ak_declarationname:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001020 case Diagnostic::ak_nameddecl:
Douglas Gregordacd4342009-08-26 00:04:55 +00001021 case Diagnostic::ak_nestednamespec:
Douglas Gregor3f093272009-10-13 21:16:44 +00001022 case Diagnostic::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +00001023 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +00001024 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +00001025 Argument, ArgumentLen,
1026 FormattedArgs.data(), FormattedArgs.size(),
1027 OutStr);
Chris Lattner22caddc2008-11-23 09:13:29 +00001028 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +00001029 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +00001030
1031 // Remember this argument info for subsequent formatting operations. Turn
1032 // std::strings into a null terminated string to make it be the same case as
1033 // all the other ones.
1034 if (Kind != Diagnostic::ak_std_string)
1035 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
1036 else
1037 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
1038 (intptr_t)getArgStdStr(ArgNo).c_str()));
1039
Nico Weber7bfaaae2008-08-10 19:59:06 +00001040 }
Nico Weber7bfaaae2008-08-10 19:59:06 +00001041}
Ted Kremenekcabe6682009-01-23 20:28:53 +00001042
Douglas Gregora88084b2010-02-18 18:08:43 +00001043StoredDiagnostic::StoredDiagnostic() { }
1044
1045StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
1046 llvm::StringRef Message)
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001047 : Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +00001048
1049StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
1050 const DiagnosticInfo &Info)
Chris Lattner0a76aae2010-06-18 22:45:06 +00001051 : Level(Level), Loc(Info.getLocation()) {
Douglas Gregora88084b2010-02-18 18:08:43 +00001052 llvm::SmallString<64> Message;
1053 Info.FormatDiagnostic(Message);
1054 this->Message.assign(Message.begin(), Message.end());
1055
1056 Ranges.reserve(Info.getNumRanges());
1057 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
1058 Ranges.push_back(Info.getRange(I));
1059
Douglas Gregor849b2432010-03-31 17:46:05 +00001060 FixIts.reserve(Info.getNumFixItHints());
1061 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
1062 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +00001063}
1064
1065StoredDiagnostic::~StoredDiagnostic() { }
1066
Douglas Gregord93256e2010-01-28 06:00:51 +00001067static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
1068 OS.write((const char *)&Value, sizeof(unsigned));
1069}
1070
1071static void WriteString(llvm::raw_ostream &OS, llvm::StringRef String) {
1072 WriteUnsigned(OS, String.size());
1073 OS.write(String.data(), String.size());
1074}
1075
1076static void WriteSourceLocation(llvm::raw_ostream &OS,
1077 SourceManager *SM,
1078 SourceLocation Location) {
1079 if (!SM || Location.isInvalid()) {
1080 // If we don't have a source manager or this location is invalid,
1081 // just write an invalid location.
1082 WriteUnsigned(OS, 0);
1083 WriteUnsigned(OS, 0);
1084 WriteUnsigned(OS, 0);
1085 return;
1086 }
1087
1088 Location = SM->getInstantiationLoc(Location);
1089 std::pair<FileID, unsigned> Decomposed = SM->getDecomposedLoc(Location);
Ted Kremenekec55c942010-04-12 19:54:17 +00001090
1091 const FileEntry *FE = SM->getFileEntryForID(Decomposed.first);
1092 if (FE)
1093 WriteString(OS, FE->getName());
1094 else {
1095 // Fallback to using the buffer name when there is no entry.
1096 WriteString(OS, SM->getBuffer(Decomposed.first)->getBufferIdentifier());
1097 }
1098
Douglas Gregord93256e2010-01-28 06:00:51 +00001099 WriteUnsigned(OS, SM->getLineNumber(Decomposed.first, Decomposed.second));
1100 WriteUnsigned(OS, SM->getColumnNumber(Decomposed.first, Decomposed.second));
1101}
1102
Douglas Gregora88084b2010-02-18 18:08:43 +00001103void StoredDiagnostic::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregord93256e2010-01-28 06:00:51 +00001104 SourceManager *SM = 0;
1105 if (getLocation().isValid())
1106 SM = &const_cast<SourceManager &>(getLocation().getManager());
1107
Douglas Gregorb9c903b2010-02-19 00:40:40 +00001108 // Write a short header to help identify diagnostics.
1109 OS << (char)0x06 << (char)0x07;
1110
Douglas Gregord93256e2010-01-28 06:00:51 +00001111 // Write the diagnostic level and location.
Douglas Gregora88084b2010-02-18 18:08:43 +00001112 WriteUnsigned(OS, (unsigned)Level);
Douglas Gregord93256e2010-01-28 06:00:51 +00001113 WriteSourceLocation(OS, SM, getLocation());
1114
1115 // Write the diagnostic message.
1116 llvm::SmallString<64> Message;
Douglas Gregora88084b2010-02-18 18:08:43 +00001117 WriteString(OS, getMessage());
Douglas Gregord93256e2010-01-28 06:00:51 +00001118
1119 // Count the number of ranges that don't point into macros, since
1120 // only simple file ranges serialize well.
1121 unsigned NumNonMacroRanges = 0;
Douglas Gregora88084b2010-02-18 18:08:43 +00001122 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1123 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregord93256e2010-01-28 06:00:51 +00001124 continue;
1125
1126 ++NumNonMacroRanges;
1127 }
1128
1129 // Write the ranges.
1130 WriteUnsigned(OS, NumNonMacroRanges);
1131 if (NumNonMacroRanges) {
Douglas Gregora88084b2010-02-18 18:08:43 +00001132 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1133 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregord93256e2010-01-28 06:00:51 +00001134 continue;
1135
Douglas Gregora88084b2010-02-18 18:08:43 +00001136 WriteSourceLocation(OS, SM, R->getBegin());
1137 WriteSourceLocation(OS, SM, R->getEnd());
Chris Lattner0a76aae2010-06-18 22:45:06 +00001138 WriteUnsigned(OS, R->isTokenRange());
Douglas Gregord93256e2010-01-28 06:00:51 +00001139 }
1140 }
1141
1142 // Determine if all of the fix-its involve rewrites with simple file
1143 // locations (not in macro instantiations). If so, we can write
1144 // fix-it information.
Douglas Gregora88084b2010-02-18 18:08:43 +00001145 unsigned NumFixIts = 0;
1146 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1147 if (F->RemoveRange.isValid() &&
1148 (F->RemoveRange.getBegin().isMacroID() ||
1149 F->RemoveRange.getEnd().isMacroID())) {
Douglas Gregord93256e2010-01-28 06:00:51 +00001150 NumFixIts = 0;
1151 break;
1152 }
1153
Douglas Gregora88084b2010-02-18 18:08:43 +00001154 if (F->InsertionLoc.isValid() && F->InsertionLoc.isMacroID()) {
Douglas Gregord93256e2010-01-28 06:00:51 +00001155 NumFixIts = 0;
1156 break;
1157 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001158
1159 ++NumFixIts;
Douglas Gregord93256e2010-01-28 06:00:51 +00001160 }
1161
1162 // Write the fix-its.
1163 WriteUnsigned(OS, NumFixIts);
Douglas Gregora88084b2010-02-18 18:08:43 +00001164 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1165 WriteSourceLocation(OS, SM, F->RemoveRange.getBegin());
1166 WriteSourceLocation(OS, SM, F->RemoveRange.getEnd());
Chris Lattner0a76aae2010-06-18 22:45:06 +00001167 WriteUnsigned(OS, F->RemoveRange.isTokenRange());
Douglas Gregora88084b2010-02-18 18:08:43 +00001168 WriteSourceLocation(OS, SM, F->InsertionLoc);
1169 WriteString(OS, F->CodeToInsert);
Douglas Gregord93256e2010-01-28 06:00:51 +00001170 }
1171}
1172
Douglas Gregora88084b2010-02-18 18:08:43 +00001173static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
1174 unsigned &Value) {
1175 if (Memory + sizeof(unsigned) > MemoryEnd)
1176 return true;
1177
1178 memmove(&Value, Memory, sizeof(unsigned));
1179 Memory += sizeof(unsigned);
1180 return false;
1181}
1182
1183static bool ReadSourceLocation(FileManager &FM, SourceManager &SM,
1184 const char *&Memory, const char *MemoryEnd,
1185 SourceLocation &Location) {
1186 // Read the filename.
1187 unsigned FileNameLen = 0;
1188 if (ReadUnsigned(Memory, MemoryEnd, FileNameLen) ||
1189 Memory + FileNameLen > MemoryEnd)
1190 return true;
1191
1192 llvm::StringRef FileName(Memory, FileNameLen);
1193 Memory += FileNameLen;
1194
1195 // Read the line, column.
1196 unsigned Line = 0, Column = 0;
1197 if (ReadUnsigned(Memory, MemoryEnd, Line) ||
1198 ReadUnsigned(Memory, MemoryEnd, Column))
1199 return true;
1200
1201 if (FileName.empty()) {
1202 Location = SourceLocation();
1203 return false;
1204 }
1205
1206 const FileEntry *File = FM.getFile(FileName);
1207 if (!File)
1208 return true;
1209
1210 // Make sure that this file has an entry in the source manager.
1211 if (!SM.hasFileInfo(File))
1212 SM.createFileID(File, SourceLocation(), SrcMgr::C_User);
1213
1214 Location = SM.getLocation(File, Line, Column);
1215 return false;
1216}
1217
1218StoredDiagnostic
1219StoredDiagnostic::Deserialize(FileManager &FM, SourceManager &SM,
1220 const char *&Memory, const char *MemoryEnd) {
Douglas Gregorb9c903b2010-02-19 00:40:40 +00001221 while (true) {
1222 if (Memory == MemoryEnd)
1223 return StoredDiagnostic();
1224
1225 if (*Memory != 0x06) {
1226 ++Memory;
1227 continue;
1228 }
1229
1230 ++Memory;
1231 if (Memory == MemoryEnd)
1232 return StoredDiagnostic();
1233
1234 if (*Memory != 0x07) {
1235 ++Memory;
1236 continue;
1237 }
1238
1239 // We found the header. We're done.
1240 ++Memory;
1241 break;
1242 }
1243
Douglas Gregora88084b2010-02-18 18:08:43 +00001244 // Read the severity level.
1245 unsigned Level = 0;
1246 if (ReadUnsigned(Memory, MemoryEnd, Level) || Level > Diagnostic::Fatal)
1247 return StoredDiagnostic();
1248
1249 // Read the source location.
1250 SourceLocation Location;
1251 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Location))
1252 return StoredDiagnostic();
1253
1254 // Read the diagnostic text.
1255 if (Memory == MemoryEnd)
1256 return StoredDiagnostic();
1257
1258 unsigned MessageLen = 0;
1259 if (ReadUnsigned(Memory, MemoryEnd, MessageLen) ||
1260 Memory + MessageLen > MemoryEnd)
1261 return StoredDiagnostic();
1262
1263 llvm::StringRef Message(Memory, MessageLen);
1264 Memory += MessageLen;
1265
1266
1267 // At this point, we have enough information to form a diagnostic. Do so.
1268 StoredDiagnostic Diag;
1269 Diag.Level = (Diagnostic::Level)Level;
1270 Diag.Loc = FullSourceLoc(Location, SM);
1271 Diag.Message = Message;
1272 if (Memory == MemoryEnd)
1273 return Diag;
1274
1275 // Read the source ranges.
1276 unsigned NumSourceRanges = 0;
1277 if (ReadUnsigned(Memory, MemoryEnd, NumSourceRanges))
1278 return Diag;
1279 for (unsigned I = 0; I != NumSourceRanges; ++I) {
1280 SourceLocation Begin, End;
Chris Lattner0a76aae2010-06-18 22:45:06 +00001281 unsigned IsTokenRange;
Douglas Gregora88084b2010-02-18 18:08:43 +00001282 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Begin) ||
Chris Lattner0a76aae2010-06-18 22:45:06 +00001283 ReadSourceLocation(FM, SM, Memory, MemoryEnd, End) ||
1284 ReadUnsigned(Memory, MemoryEnd, IsTokenRange))
Douglas Gregora88084b2010-02-18 18:08:43 +00001285 return Diag;
1286
Chris Lattner0a76aae2010-06-18 22:45:06 +00001287 Diag.Ranges.push_back(CharSourceRange(SourceRange(Begin, End),
1288 IsTokenRange));
Douglas Gregora88084b2010-02-18 18:08:43 +00001289 }
1290
1291 // Read the fix-it hints.
1292 unsigned NumFixIts = 0;
1293 if (ReadUnsigned(Memory, MemoryEnd, NumFixIts))
1294 return Diag;
1295 for (unsigned I = 0; I != NumFixIts; ++I) {
1296 SourceLocation RemoveBegin, RemoveEnd, InsertionLoc;
Chris Lattner0a76aae2010-06-18 22:45:06 +00001297 unsigned InsertLen = 0, RemoveIsTokenRange;
Douglas Gregora88084b2010-02-18 18:08:43 +00001298 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveBegin) ||
1299 ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveEnd) ||
Chris Lattner0a76aae2010-06-18 22:45:06 +00001300 ReadUnsigned(Memory, MemoryEnd, RemoveIsTokenRange) ||
Douglas Gregora88084b2010-02-18 18:08:43 +00001301 ReadSourceLocation(FM, SM, Memory, MemoryEnd, InsertionLoc) ||
1302 ReadUnsigned(Memory, MemoryEnd, InsertLen) ||
1303 Memory + InsertLen > MemoryEnd) {
1304 Diag.FixIts.clear();
1305 return Diag;
1306 }
1307
Douglas Gregor849b2432010-03-31 17:46:05 +00001308 FixItHint Hint;
Chris Lattner0a76aae2010-06-18 22:45:06 +00001309 Hint.RemoveRange = CharSourceRange(SourceRange(RemoveBegin, RemoveEnd),
1310 RemoveIsTokenRange);
Douglas Gregora88084b2010-02-18 18:08:43 +00001311 Hint.InsertionLoc = InsertionLoc;
1312 Hint.CodeToInsert.assign(Memory, Memory + InsertLen);
1313 Memory += InsertLen;
1314 Diag.FixIts.push_back(Hint);
1315 }
1316
1317 return Diag;
1318}
1319
Ted Kremenekcabe6682009-01-23 20:28:53 +00001320/// IncludeInDiagnosticCounts - This method (whose default implementation
1321/// returns true) indicates whether the diagnostics handled by this
1322/// DiagnosticClient should be included in the number of diagnostics
1323/// reported by Diagnostic.
1324bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001325
1326PartialDiagnostic::StorageAllocator::StorageAllocator() {
1327 for (unsigned I = 0; I != NumCached; ++I)
1328 FreeList[I] = Cached + I;
1329 NumFreeListEntries = NumCached;
1330}
1331
1332PartialDiagnostic::StorageAllocator::~StorageAllocator() {
1333 assert(NumFreeListEntries == NumCached && "A partial is on the lamb");
1334}