blob: 2b7fcd07f9d0c431cdd00f081878c632c11f5c74 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Diagnostic.h"
Douglas Gregorfe6b2d42010-03-29 23:34:08 +000015#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner27ceb9d2009-04-15 07:01:18 +000016
17#include "clang/Lex/LexDiagnostic.h"
18#include "clang/Parse/ParseDiagnostic.h"
19#include "clang/AST/ASTDiagnostic.h"
20#include "clang/Sema/SemaDiagnostic.h"
21#include "clang/Frontend/FrontendDiagnostic.h"
22#include "clang/Analysis/AnalysisDiagnostic.h"
23#include "clang/Driver/DriverDiagnostic.h"
24
Douglas Gregord93256e2010-01-28 06:00:51 +000025#include "clang/Basic/FileManager.h"
Chris Lattner43b628c2008-11-19 07:32:16 +000026#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "clang/Basic/SourceLocation.h"
Douglas Gregord93256e2010-01-28 06:00:51 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattner30bc9652008-11-19 07:22:31 +000030#include "llvm/ADT/StringExtras.h"
Daniel Dunbar23e47c62009-10-17 18:12:14 +000031#include "llvm/Support/raw_ostream.h"
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 Lattner121f60c2009-04-16 06:07:15 +000054 const char *Description;
Chris Lattner33dd2822009-04-16 06:00:24 +000055 const char *OptionGroup;
Mike Stump1eb44332009-09-09 15:08:12 +000056
Chris Lattner87d854e2009-04-16 06:13:46 +000057 bool operator<(const StaticDiagInfoRec &RHS) const {
58 return DiagID < RHS.DiagID;
59 }
60 bool operator>(const StaticDiagInfoRec &RHS) const {
61 return DiagID > RHS.DiagID;
62 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +000063};
64
Chris Lattner33dd2822009-04-16 06:00:24 +000065static const StaticDiagInfoRec StaticDiagInfo[] = {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000066#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,SFINAE) \
67 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, DESC, GROUP },
Chris Lattner27ceb9d2009-04-15 07:01:18 +000068#include "clang/Basic/DiagnosticCommonKinds.inc"
69#include "clang/Basic/DiagnosticDriverKinds.inc"
70#include "clang/Basic/DiagnosticFrontendKinds.inc"
71#include "clang/Basic/DiagnosticLexKinds.inc"
72#include "clang/Basic/DiagnosticParseKinds.inc"
73#include "clang/Basic/DiagnosticASTKinds.inc"
74#include "clang/Basic/DiagnosticSemaKinds.inc"
75#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Douglas Gregor5e9f35c2009-06-14 07:33:30 +000076 { 0, 0, 0, 0, 0, 0}
Chris Lattner27ceb9d2009-04-15 07:01:18 +000077};
Chris Lattner8a941e02009-04-15 16:56:26 +000078#undef DIAG
Chris Lattner27ceb9d2009-04-15 07:01:18 +000079
Chris Lattner87d854e2009-04-16 06:13:46 +000080/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
81/// or null if the ID is invalid.
Chris Lattner33dd2822009-04-16 06:00:24 +000082static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner87d854e2009-04-16 06:13:46 +000083 unsigned NumDiagEntries = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
84
85 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
86#ifndef NDEBUG
87 static bool IsFirst = true;
88 if (IsFirst) {
Chris Lattner5a3ce9b2009-10-16 02:34:51 +000089 for (unsigned i = 1; i != NumDiagEntries; ++i) {
90 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
91 "Diag ID conflict, the enums at the start of clang::diag (in "
92 "Diagnostic.h) probably need to be increased");
93
Chris Lattner87d854e2009-04-16 06:13:46 +000094 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
95 "Improperly sorted diag info");
Chris Lattner5a3ce9b2009-10-16 02:34:51 +000096 }
Chris Lattner87d854e2009-04-16 06:13:46 +000097 IsFirst = false;
98 }
99#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattner87d854e2009-04-16 06:13:46 +0000101 // Search the diagnostic table with a binary search.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000102 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0 };
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattner87d854e2009-04-16 06:13:46 +0000104 const StaticDiagInfoRec *Found =
105 std::lower_bound(StaticDiagInfo, StaticDiagInfo + NumDiagEntries, Find);
106 if (Found == StaticDiagInfo + NumDiagEntries ||
107 Found->DiagID != DiagID)
108 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Chris Lattner87d854e2009-04-16 06:13:46 +0000110 return Found;
Chris Lattner33dd2822009-04-16 06:00:24 +0000111}
112
113static unsigned GetDefaultDiagMapping(unsigned DiagID) {
114 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Chris Lattner121f60c2009-04-16 06:07:15 +0000115 return Info->Mapping;
Chris Lattner691f1ae2009-04-16 04:12:40 +0000116 return diag::MAP_FATAL;
117}
118
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000119/// getWarningOptionForDiag - Return the lowest-level warning option that
120/// enables the specified diagnostic. If there is no -Wfoo flag that controls
121/// the diagnostic, this returns null.
122const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) {
Chris Lattner33dd2822009-04-16 06:00:24 +0000123 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
124 return Info->OptionGroup;
125 return 0;
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000126}
127
Douglas Gregoreab5d1e2010-03-25 22:17:48 +0000128Diagnostic::SFINAEResponse
129Diagnostic::getDiagnosticSFINAEResponse(unsigned DiagID) {
130 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
131 if (!Info->SFINAE)
132 return SFINAE_Report;
133
134 if (Info->Class == CLASS_ERROR)
135 return SFINAE_SubstitutionFailure;
136
137 // Suppress notes, warnings, and extensions;
138 return SFINAE_Suppress;
139 }
140
141 return SFINAE_Report;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000142}
143
Reid Spencer5f016e22007-07-11 17:01:13 +0000144/// getDiagClass - Return the class field of the diagnostic.
145///
Chris Lattner07506182007-11-30 22:53:43 +0000146static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner121f60c2009-04-16 06:07:15 +0000147 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
148 return Info->Class;
149 return ~0U;
Reid Spencer5f016e22007-07-11 17:01:13 +0000150}
151
Chris Lattner182745a2007-12-02 01:09:57 +0000152//===----------------------------------------------------------------------===//
153// Custom Diagnostic information
154//===----------------------------------------------------------------------===//
155
156namespace clang {
157 namespace diag {
158 class CustomDiagInfo {
159 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
160 std::vector<DiagDesc> DiagInfo;
161 std::map<DiagDesc, unsigned> DiagIDs;
162 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattner182745a2007-12-02 01:09:57 +0000164 /// getDescription - Return the description of the specified custom
165 /// diagnostic.
166 const char *getDescription(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000167 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000168 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000169 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattner182745a2007-12-02 01:09:57 +0000170 }
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Chris Lattner182745a2007-12-02 01:09:57 +0000172 /// getLevel - Return the level of the specified custom diagnostic.
173 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner88eccaf2009-01-29 06:55:46 +0000174 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattner182745a2007-12-02 01:09:57 +0000175 "Invalid diagnosic ID");
Chris Lattner88eccaf2009-01-29 06:55:46 +0000176 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattner182745a2007-12-02 01:09:57 +0000177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Daniel Dunbar32d4d802009-12-01 17:42:06 +0000179 unsigned getOrCreateDiagID(Diagnostic::Level L, llvm::StringRef Message,
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000180 Diagnostic &Diags) {
Chris Lattner182745a2007-12-02 01:09:57 +0000181 DiagDesc D(L, Message);
182 // Check to see if it already exists.
183 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
184 if (I != DiagIDs.end() && I->first == D)
185 return I->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattner182745a2007-12-02 01:09:57 +0000187 // If not, assign a new ID.
Chris Lattner88eccaf2009-01-29 06:55:46 +0000188 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattner182745a2007-12-02 01:09:57 +0000189 DiagIDs.insert(std::make_pair(D, ID));
190 DiagInfo.push_back(D);
191 return ID;
192 }
193 };
Mike Stump1eb44332009-09-09 15:08:12 +0000194
195 } // end diag namespace
196} // end clang namespace
Chris Lattner182745a2007-12-02 01:09:57 +0000197
198
199//===----------------------------------------------------------------------===//
200// Common Diagnostic implementation
201//===----------------------------------------------------------------------===//
202
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000203static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
204 const char *Modifier, unsigned ML,
205 const char *Argument, unsigned ArgLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000206 const Diagnostic::ArgumentValue *PrevArgs,
207 unsigned NumPrevArgs,
Chris Lattner92dd3862009-02-19 23:53:20 +0000208 llvm::SmallVectorImpl<char> &Output,
209 void *Cookie) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000210 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +0000211 Output.append(Str, Str+strlen(Str));
212}
213
214
Ted Kremenekb4398aa2008-08-07 17:49:57 +0000215Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000216 AllExtensionsSilenced = 0;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000217 IgnoreAllWarnings = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 WarningsAsErrors = false;
Chris Lattnere663c722009-12-22 23:12:53 +0000219 ErrorsAsFatal = false;
Daniel Dunbar2fe09972008-09-12 18:10:20 +0000220 SuppressSystemWarnings = false;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000221 SuppressAllDiagnostics = false;
Chris Lattnerb54b2762009-04-16 05:04:32 +0000222 ExtBehavior = Ext_Ignore;
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 ErrorOccurred = false;
Chris Lattner15221422009-02-06 04:16:02 +0000225 FatalErrorOccurred = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 NumDiagnostics = 0;
Steve Naroffe0c4d892009-12-05 02:14:08 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 NumErrors = 0;
Chris Lattner182745a2007-12-02 01:09:57 +0000229 CustomDiagInfo = 0;
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000230 CurDiagID = ~0U;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000231 LastDiagLevel = Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000233 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +0000234 ArgToStringCookie = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000236 DelayedDiagID = 0;
237
Chris Lattner691f1ae2009-04-16 04:12:40 +0000238 // Set all mappings to 'unset'.
Chris Lattner04ae2df2009-07-12 21:18:45 +0000239 DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0);
240 DiagMappingsStack.push_back(BlankDiags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000241}
242
Chris Lattner182745a2007-12-02 01:09:57 +0000243Diagnostic::~Diagnostic() {
244 delete CustomDiagInfo;
245}
246
Chris Lattner04ae2df2009-07-12 21:18:45 +0000247
248void Diagnostic::pushMappings() {
John Thompsonca2c3e22009-10-23 02:21:17 +0000249 // Avoids undefined behavior when the stack has to resize.
250 DiagMappingsStack.reserve(DiagMappingsStack.size() + 1);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000251 DiagMappingsStack.push_back(DiagMappingsStack.back());
252}
253
254bool Diagnostic::popMappings() {
255 if (DiagMappingsStack.size() == 1)
256 return false;
257
258 DiagMappingsStack.pop_back();
259 return true;
260}
261
Chris Lattner182745a2007-12-02 01:09:57 +0000262/// getCustomDiagID - Return an ID for a diagnostic with the specified message
263/// and level. If this is the first request for this diagnosic, it is
264/// registered and created, otherwise the existing ID is returned.
Daniel Dunbar32d4d802009-12-01 17:42:06 +0000265unsigned Diagnostic::getCustomDiagID(Level L, llvm::StringRef Message) {
Mike Stump1eb44332009-09-09 15:08:12 +0000266 if (CustomDiagInfo == 0)
Chris Lattner182745a2007-12-02 01:09:57 +0000267 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnera1f23cc2008-10-17 21:24:47 +0000268 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattner182745a2007-12-02 01:09:57 +0000269}
270
271
Chris Lattnerf5d23282009-02-17 06:49:55 +0000272/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
273/// level of the specified diagnostic ID is a Warning or Extension.
274/// This only works on builtin diagnostics, not custom ones, and is not legal to
275/// call on NOTEs.
276bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000277 return DiagID < diag::DIAG_UPPER_LIMIT &&
278 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Reid Spencer5f016e22007-07-11 17:01:13 +0000279}
280
Douglas Gregoree1828a2009-03-10 18:03:33 +0000281/// \brief Determine whether the given built-in diagnostic ID is a
282/// Note.
283bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000284 return DiagID < diag::DIAG_UPPER_LIMIT &&
285 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000286}
287
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000288/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
289/// ID is for an extension of some sort.
290///
291bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattner8a941e02009-04-15 16:56:26 +0000292 return DiagID < diag::DIAG_UPPER_LIMIT &&
293 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000294}
295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296
297/// getDescription - Given a diagnostic ID, return a description of the
298/// issue.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000299const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner121f60c2009-04-16 06:07:15 +0000300 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
301 return Info->Description;
Chris Lattner20c6b3b2009-01-27 18:30:58 +0000302 return CustomDiagInfo->getDescription(DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000303}
304
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000305void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
306 llvm::StringRef Arg2) {
307 if (DelayedDiagID)
308 return;
309
310 DelayedDiagID = DiagID;
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000311 DelayedDiagArg1 = Arg1.str();
312 DelayedDiagArg2 = Arg2.str();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000313}
314
315void Diagnostic::ReportDelayed() {
316 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
317 DelayedDiagID = 0;
318 DelayedDiagArg1.clear();
319 DelayedDiagArg2.clear();
320}
321
Reid Spencer5f016e22007-07-11 17:01:13 +0000322/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
323/// object, classify the specified diagnostic ID into a Level, consumable by
324/// the DiagnosticClient.
325Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattner182745a2007-12-02 01:09:57 +0000326 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner19e8e2c2009-01-29 17:46:13 +0000327 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattner182745a2007-12-02 01:09:57 +0000328 return CustomDiagInfo->getLevel(DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner07506182007-11-30 22:53:43 +0000330 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000331 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerf5d23282009-02-17 06:49:55 +0000332 return getDiagnosticLevel(DiagID, DiagClass);
333}
334
335/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
336/// object, classify the specified diagnostic ID into a Level, consumable by
337/// the DiagnosticClient.
338Diagnostic::Level
339Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerf5d23282009-02-17 06:49:55 +0000341 // to error. Errors can only be mapped to fatal.
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000342 Diagnostic::Level Result = Diagnostic::Fatal;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Chris Lattner691f1ae2009-04-16 04:12:40 +0000344 // Get the mapping information, if unset, compute it lazily.
345 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
346 if (MappingInfo == 0) {
347 MappingInfo = GetDefaultDiagMapping(DiagID);
348 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattner691f1ae2009-04-16 04:12:40 +0000351 switch (MappingInfo & 7) {
352 default: assert(0 && "Unknown mapping!");
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000353 case diag::MAP_IGNORE:
Chris Lattnerb54b2762009-04-16 05:04:32 +0000354 // Ignore this, unless this is an extension diagnostic and we're mapping
355 // them onto warnings or errors.
356 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
357 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
358 (MappingInfo & 8) != 0) // User explicitly mapped it.
359 return Diagnostic::Ignored;
360 Result = Diagnostic::Warning;
361 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
Chris Lattnere663c722009-12-22 23:12:53 +0000362 if (Result == Diagnostic::Error && ErrorsAsFatal)
363 Result = Diagnostic::Fatal;
Chris Lattnerb54b2762009-04-16 05:04:32 +0000364 break;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000365 case diag::MAP_ERROR:
366 Result = Diagnostic::Error;
Chris Lattnere663c722009-12-22 23:12:53 +0000367 if (ErrorsAsFatal)
368 Result = Diagnostic::Fatal;
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000369 break;
370 case diag::MAP_FATAL:
371 Result = Diagnostic::Fatal;
372 break;
373 case diag::MAP_WARNING:
374 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner5b4681c2008-05-29 15:36:45 +0000375 if (IgnoreAllWarnings)
376 return Diagnostic::Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000378 Result = Diagnostic::Warning;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattnerb54b2762009-04-16 05:04:32 +0000380 // If this is an extension diagnostic and we're in -pedantic-error mode, and
381 // if the user didn't explicitly map it, upgrade to an error.
382 if (ExtBehavior == Ext_Error &&
383 (MappingInfo & 8) == 0 &&
384 isBuiltinExtensionDiag(DiagID))
385 Result = Diagnostic::Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000387 if (WarningsAsErrors)
388 Result = Diagnostic::Error;
Chris Lattnere663c722009-12-22 23:12:53 +0000389 if (Result == Diagnostic::Error && ErrorsAsFatal)
390 Result = Diagnostic::Fatal;
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000391 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000393 case diag::MAP_WARNING_NO_WERROR:
394 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
395 // not be adjusted by -Werror or -pedantic-errors.
396 Result = Diagnostic::Warning;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000398 // If warnings are globally mapped to ignore or error, do it.
399 if (IgnoreAllWarnings)
400 return Diagnostic::Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000402 break;
Chris Lattnere663c722009-12-22 23:12:53 +0000403
404 case diag::MAP_ERROR_NO_WFATAL:
405 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
406 // unaffected by -Wfatal-errors.
407 Result = Diagnostic::Error;
408 break;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000409 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000410
411 // Okay, we're about to return this as a "diagnostic to emit" one last check:
412 // if this is any sort of extension warning, and if we're in an __extension__
413 // block, silence it.
414 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
415 return Diagnostic::Ignored;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000417 return Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000418}
419
Chris Lattner3bc172b2009-04-19 22:34:23 +0000420struct WarningOption {
421 const char *Name;
422 const short *Members;
423 const char *SubGroups;
424};
425
426#define GET_DIAG_ARRAYS
427#include "clang/Basic/DiagnosticGroups.inc"
428#undef GET_DIAG_ARRAYS
429
430// Second the table of options, sorted by name for fast binary lookup.
431static const WarningOption OptionTable[] = {
432#define GET_DIAG_TABLE
433#include "clang/Basic/DiagnosticGroups.inc"
434#undef GET_DIAG_TABLE
435};
436static const size_t OptionTableSize =
437sizeof(OptionTable) / sizeof(OptionTable[0]);
438
439static bool WarningOptionCompare(const WarningOption &LHS,
440 const WarningOption &RHS) {
441 return strcmp(LHS.Name, RHS.Name) < 0;
442}
443
444static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
445 Diagnostic &Diags) {
446 // Option exists, poke all the members of its diagnostic set.
447 if (const short *Member = Group->Members) {
448 for (; *Member != -1; ++Member)
449 Diags.setDiagnosticMapping(*Member, Mapping);
450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Chris Lattner3bc172b2009-04-19 22:34:23 +0000452 // Enable/disable all subgroups along with this one.
453 if (const char *SubGroups = Group->SubGroups) {
454 for (; *SubGroups != (char)-1; ++SubGroups)
455 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
456 }
457}
458
459/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
460/// "unknown-pragmas" to have the specified mapping. This returns true and
461/// ignores the request if "Group" was unknown, false otherwise.
462bool Diagnostic::setDiagnosticGroupMapping(const char *Group,
463 diag::Mapping Map) {
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Chris Lattner3bc172b2009-04-19 22:34:23 +0000465 WarningOption Key = { Group, 0, 0 };
466 const WarningOption *Found =
467 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
468 WarningOptionCompare);
469 if (Found == OptionTable + OptionTableSize ||
470 strcmp(Found->Name, Group) != 0)
471 return true; // Option not found.
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattner3bc172b2009-04-19 22:34:23 +0000473 MapGroupMembers(Found, Map, *this);
474 return false;
475}
476
477
Chris Lattner0a14eee2008-11-18 07:04:44 +0000478/// ProcessDiag - This is the method used to report a diagnostic that is
479/// finally fully formed.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000480bool Diagnostic::ProcessDiag() {
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000481 DiagnosticInfo Info(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Douglas Gregor81b747b2009-09-17 21:32:03 +0000483 if (SuppressAllDiagnostics)
484 return false;
485
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 // Figure out the diagnostic level of this message.
Chris Lattnerf5d23282009-02-17 06:49:55 +0000487 Diagnostic::Level DiagLevel;
488 unsigned DiagID = Info.getID();
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Chris Lattnerf5d23282009-02-17 06:49:55 +0000490 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
491 // in a system header.
492 bool ShouldEmitInSystemHeader;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattnerf5d23282009-02-17 06:49:55 +0000494 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
495 // Handle custom diagnostics, which cannot be mapped.
496 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Chris Lattnerf5d23282009-02-17 06:49:55 +0000498 // Custom diagnostics always are emitted in system headers.
499 ShouldEmitInSystemHeader = true;
500 } else {
501 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
502 // the diagnostic level was for the previous diagnostic so that it is
503 // filtered the same as the previous diagnostic.
504 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattner8a941e02009-04-15 16:56:26 +0000505 if (DiagClass == CLASS_NOTE) {
Chris Lattnerf5d23282009-02-17 06:49:55 +0000506 DiagLevel = Diagnostic::Note;
507 ShouldEmitInSystemHeader = false; // extra consideration is needed
508 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000509 // If this is not an error and we are in a system header, we ignore it.
Chris Lattnerf5d23282009-02-17 06:49:55 +0000510 // Check the original Diag ID here, because we also want to ignore
511 // extensions and warnings in -Werror and -pedantic-errors modes, which
512 // *map* warnings/extensions to errors.
Chris Lattner8a941e02009-04-15 16:56:26 +0000513 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattnerf5d23282009-02-17 06:49:55 +0000515 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
516 }
517 }
518
Douglas Gregor525c4b02009-03-19 18:55:06 +0000519 if (DiagLevel != Diagnostic::Note) {
520 // Record that a fatal error occurred only when we see a second
521 // non-note diagnostic. This allows notes to be attached to the
522 // fatal error, but suppresses any diagnostics that follow those
523 // notes.
524 if (LastDiagLevel == Diagnostic::Fatal)
525 FatalErrorOccurred = true;
526
Chris Lattnerf5d23282009-02-17 06:49:55 +0000527 LastDiagLevel = DiagLevel;
Mike Stump1eb44332009-09-09 15:08:12 +0000528 }
Douglas Gregor525c4b02009-03-19 18:55:06 +0000529
530 // If a fatal error has already been emitted, silence all subsequent
531 // diagnostics.
532 if (FatalErrorOccurred)
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000533 return false;
Douglas Gregor525c4b02009-03-19 18:55:06 +0000534
Chris Lattnerf5d23282009-02-17 06:49:55 +0000535 // If the client doesn't care about this message, don't issue it. If this is
536 // a note and the last real diagnostic was ignored, ignore it too.
537 if (DiagLevel == Diagnostic::Ignored ||
538 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000539 return false;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000540
Chris Lattnerf5d23282009-02-17 06:49:55 +0000541 // If this diagnostic is in a system header and is not a clang error, suppress
542 // it.
543 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner0a14eee2008-11-18 07:04:44 +0000544 Info.getLocation().isValid() &&
John McCall779cf422010-02-11 10:04:29 +0000545 Info.getLocation().getInstantiationLoc().isInSystemHeader() &&
Chris Lattner336f26b2009-02-17 06:52:20 +0000546 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
547 LastDiagLevel = Diagnostic::Ignored;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000548 return false;
Chris Lattner336f26b2009-02-17 06:52:20 +0000549 }
Chris Lattnerf5d23282009-02-17 06:49:55 +0000550
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 if (DiagLevel >= Diagnostic::Error) {
552 ErrorOccurred = true;
Chris Lattner0a14eee2008-11-18 07:04:44 +0000553 ++NumErrors;
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 }
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 // Finally, report it.
Chris Lattner0a14eee2008-11-18 07:04:44 +0000557 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekcabe6682009-01-23 20:28:53 +0000558 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000559
Douglas Gregoree1828a2009-03-10 18:03:33 +0000560 CurDiagID = ~0U;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000561
562 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000563}
564
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000565bool DiagnosticBuilder::Emit() {
566 // If DiagObj is null, then its soul was stolen by the copy ctor
567 // or the user called Emit().
568 if (DiagObj == 0) return false;
569
570 // When emitting diagnostics, we set the final argument count into
571 // the Diagnostic object.
572 DiagObj->NumDiagArgs = NumArgs;
573 DiagObj->NumDiagRanges = NumRanges;
Douglas Gregor849b2432010-03-31 17:46:05 +0000574 DiagObj->NumFixItHints = NumFixItHints;
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000575
576 // Process the diagnostic, sending the accumulated information to the
577 // DiagnosticClient.
578 bool Emitted = DiagObj->ProcessDiag();
579
580 // Clear out the current diagnostic object.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000581 unsigned DiagID = DiagObj->CurDiagID;
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000582 DiagObj->Clear();
583
584 // If there was a delayed diagnostic, emit it now.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000585 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000586 DiagObj->ReportDelayed();
587
588 // This diagnostic is dead.
589 DiagObj = 0;
590
591 return Emitted;
592}
593
Nico Weber7bfaaae2008-08-10 19:59:06 +0000594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595DiagnosticClient::~DiagnosticClient() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000596
Chris Lattnerf4c83962008-11-19 06:51:40 +0000597
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000598/// ModifierIs - Return true if the specified modifier matches specified string.
599template <std::size_t StrLen>
600static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
601 const char (&Str)[StrLen]) {
602 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
603}
604
John McCall909c1822010-01-14 20:11:39 +0000605/// ScanForward - Scans forward, looking for the given character, skipping
606/// nested clauses and escaped characters.
607static const char *ScanFormat(const char *I, const char *E, char Target) {
608 unsigned Depth = 0;
609
610 for ( ; I != E; ++I) {
611 if (Depth == 0 && *I == Target) return I;
612 if (Depth != 0 && *I == '}') Depth--;
613
614 if (*I == '%') {
615 I++;
616 if (I == E) break;
617
618 // Escaped characters get implicitly skipped here.
619
620 // Format specifier.
621 if (!isdigit(*I) && !ispunct(*I)) {
622 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
623 if (I == E) break;
624 if (*I == '{')
625 Depth++;
626 }
627 }
628 }
629 return E;
630}
631
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000632/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
633/// like this: %select{foo|bar|baz}2. This means that the integer argument
634/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
635/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
636/// This is very useful for certain classes of variant diagnostics.
John McCall9f286142010-01-13 23:58:20 +0000637static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000638 const char *Argument, unsigned ArgumentLen,
639 llvm::SmallVectorImpl<char> &OutStr) {
640 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000642 // Skip over 'ValNo' |'s.
643 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000644 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000645 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
646 " larger than the number of options in the diagnostic string!");
647 Argument = NextVal+1; // Skip this string.
648 --ValNo;
649 }
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000651 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000652 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000653
654 // Recursively format the result of the select clause into the output string.
655 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000656}
657
658/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
659/// letter 's' to the string if the value is not 1. This is used in cases like
660/// this: "you idiot, you have %4 parameter%s4!".
661static void HandleIntegerSModifier(unsigned ValNo,
662 llvm::SmallVectorImpl<char> &OutStr) {
663 if (ValNo != 1)
664 OutStr.push_back('s');
665}
666
John McCall3be16b72010-01-14 00:50:32 +0000667/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
668/// prints the ordinal form of the given integer, with 1 corresponding
669/// to the first ordinal. Currently this is hard-coded to use the
670/// English form.
671static void HandleOrdinalModifier(unsigned ValNo,
672 llvm::SmallVectorImpl<char> &OutStr) {
673 assert(ValNo != 0 && "ValNo must be strictly positive!");
674
675 llvm::raw_svector_ostream Out(OutStr);
676
677 // We could use text forms for the first N ordinals, but the numeric
678 // forms are actually nicer in diagnostics because they stand out.
679 Out << ValNo;
680
681 // It is critically important that we do this perfectly for
682 // user-written sequences with over 100 elements.
683 switch (ValNo % 100) {
684 case 11:
685 case 12:
686 case 13:
687 Out << "th"; return;
688 default:
689 switch (ValNo % 10) {
690 case 1: Out << "st"; return;
691 case 2: Out << "nd"; return;
692 case 3: Out << "rd"; return;
693 default: Out << "th"; return;
694 }
695 }
696}
697
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000698
Sebastian Redle4c452c2008-11-22 13:44:36 +0000699/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000700static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000701 // Programming 101: Parse a decimal number :-)
702 unsigned Val = 0;
703 while (Start != End && *Start >= '0' && *Start <= '9') {
704 Val *= 10;
705 Val += *Start - '0';
706 ++Start;
707 }
708 return Val;
709}
710
711/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000712static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000713 if (*Start != '[') {
714 unsigned Ref = PluralNumber(Start, End);
715 return Ref == Val;
716 }
717
718 ++Start;
719 unsigned Low = PluralNumber(Start, End);
720 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
721 ++Start;
722 unsigned High = PluralNumber(Start, End);
723 assert(*Start == ']' && "Bad plural expression syntax: expected )");
724 ++Start;
725 return Low <= Val && Val <= High;
726}
727
728/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000729static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000730 // Empty condition?
731 if (*Start == ':')
732 return true;
733
734 while (1) {
735 char C = *Start;
736 if (C == '%') {
737 // Modulo expression
738 ++Start;
739 unsigned Arg = PluralNumber(Start, End);
740 assert(*Start == '=' && "Bad plural expression syntax: expected =");
741 ++Start;
742 unsigned ValMod = ValNo % Arg;
743 if (TestPluralRange(ValMod, Start, End))
744 return true;
745 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000746 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000747 "Bad plural expression syntax: unexpected character");
748 // Range expression
749 if (TestPluralRange(ValNo, Start, End))
750 return true;
751 }
752
753 // Scan for next or-expr part.
754 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000755 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000756 break;
757 ++Start;
758 }
759 return false;
760}
761
762/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
763/// for complex plural forms, or in languages where all plurals are complex.
764/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
765/// conditions that are tested in order, the form corresponding to the first
766/// that applies being emitted. The empty condition is always true, making the
767/// last form a default case.
768/// Conditions are simple boolean expressions, where n is the number argument.
769/// Here are the rules.
770/// condition := expression | empty
771/// empty := -> always true
772/// expression := numeric [',' expression] -> logical or
773/// numeric := range -> true if n in range
774/// | '%' number '=' range -> true if n % number in range
775/// range := number
776/// | '[' number ',' number ']' -> ranges are inclusive both ends
777///
778/// Here are some examples from the GNU gettext manual written in this form:
779/// English:
780/// {1:form0|:form1}
781/// Latvian:
782/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
783/// Gaeilge:
784/// {1:form0|2:form1|:form2}
785/// Romanian:
786/// {1:form0|0,%100=[1,19]:form1|:form2}
787/// Lithuanian:
788/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
789/// Russian (requires repeated form):
790/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
791/// Slovak
792/// {1:form0|[2,4]:form1|:form2}
793/// Polish (requires repeated form):
794/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
795static void HandlePluralModifier(unsigned ValNo,
796 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb54b2762009-04-16 05:04:32 +0000797 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000798 const char *ArgumentEnd = Argument + ArgumentLen;
799 while (1) {
800 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
801 const char *ExprEnd = Argument;
802 while (*ExprEnd != ':') {
803 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
804 ++ExprEnd;
805 }
806 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
807 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000808 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
Sebastian Redle4c452c2008-11-22 13:44:36 +0000809 OutStr.append(Argument, ExprEnd);
810 return;
811 }
John McCall909c1822010-01-14 20:11:39 +0000812 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000813 }
814}
815
816
Chris Lattnerf4c83962008-11-19 06:51:40 +0000817/// FormatDiagnostic - Format this diagnostic into a string, substituting the
818/// formal arguments into the %0 slots. The result is appended onto the Str
819/// array.
820void DiagnosticInfo::
821FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
822 const char *DiagStr = getDiags()->getDescription(getID());
823 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000824
John McCall9f286142010-01-13 23:58:20 +0000825 FormatDiagnostic(DiagStr, DiagEnd, OutStr);
826}
827
828void DiagnosticInfo::
829FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
830 llvm::SmallVectorImpl<char> &OutStr) const {
831
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000832 /// FormattedArgs - Keep track of all of the arguments formatted by
833 /// ConvertArgToString and pass them into subsequent calls to
834 /// ConvertArgToString, allowing the implementation to avoid redundancies in
835 /// obvious cases.
836 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
837
Chris Lattnerf4c83962008-11-19 06:51:40 +0000838 while (DiagStr != DiagEnd) {
839 if (DiagStr[0] != '%') {
840 // Append non-%0 substrings to Str if we have one.
841 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
842 OutStr.append(DiagStr, StrEnd);
843 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000844 continue;
John McCall909c1822010-01-14 20:11:39 +0000845 } else if (ispunct(DiagStr[1])) {
846 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000847 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000848 continue;
849 }
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000851 // Skip the %.
852 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000854 // This must be a placeholder for a diagnostic argument. The format for a
855 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
856 // The digit is a number from 0-9 indicating which argument this comes from.
857 // The modifier is a string of digits from the set [-a-z]+, arguments is a
858 // brace enclosed string.
859 const char *Modifier = 0, *Argument = 0;
860 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000862 // Check to see if we have a modifier. If so eat it.
863 if (!isdigit(DiagStr[0])) {
864 Modifier = DiagStr;
865 while (DiagStr[0] == '-' ||
866 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
867 ++DiagStr;
868 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000869
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000870 // If we have an argument, get it next.
871 if (DiagStr[0] == '{') {
872 ++DiagStr; // Skip {.
873 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000874
John McCall909c1822010-01-14 20:11:39 +0000875 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
876 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000877 ArgumentLen = DiagStr-Argument;
878 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000879 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000882 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000883 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000884
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000885 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
886
887 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000888 // ---- STRINGS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000889 case Diagnostic::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000890 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000891 assert(ModifierLen == 0 && "No modifiers for strings yet");
892 OutStr.append(S.begin(), S.end());
893 break;
894 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000895 case Diagnostic::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000896 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000897 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000898
899 // Don't crash if get passed a null pointer by accident.
900 if (!S)
901 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000903 OutStr.append(S, S + strlen(S));
904 break;
905 }
Chris Lattner08631c52008-11-23 21:45:46 +0000906 // ---- INTEGERS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000907 case Diagnostic::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000908 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000910 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000911 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000912 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
913 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000914 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
915 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000916 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
917 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000918 } else {
919 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000920 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000921 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000922 break;
923 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000924 case Diagnostic::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000925 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000927 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000928 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000929 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
930 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000931 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
932 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000933 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
934 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000935 } else {
936 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000937 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000938 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000939 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000940 }
Chris Lattner08631c52008-11-23 21:45:46 +0000941 // ---- NAMES and TYPES ----
942 case Diagnostic::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000943 const IdentifierInfo *II = getArgIdentifier(ArgNo);
944 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000945
946 // Don't crash if get passed a null pointer by accident.
947 if (!II) {
948 const char *S = "(null)";
949 OutStr.append(S, S + strlen(S));
950 continue;
951 }
952
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000953 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000954 break;
955 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000956 case Diagnostic::ak_qualtype:
Chris Lattner011bb4e2008-11-23 20:28:15 +0000957 case Diagnostic::ak_declarationname:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000958 case Diagnostic::ak_nameddecl:
Douglas Gregordacd4342009-08-26 00:04:55 +0000959 case Diagnostic::ak_nestednamespec:
Douglas Gregor3f093272009-10-13 21:16:44 +0000960 case Diagnostic::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000961 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000962 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000963 Argument, ArgumentLen,
964 FormattedArgs.data(), FormattedArgs.size(),
965 OutStr);
Chris Lattner22caddc2008-11-23 09:13:29 +0000966 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000967 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000968
969 // Remember this argument info for subsequent formatting operations. Turn
970 // std::strings into a null terminated string to make it be the same case as
971 // all the other ones.
972 if (Kind != Diagnostic::ak_std_string)
973 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
974 else
975 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
976 (intptr_t)getArgStdStr(ArgNo).c_str()));
977
Nico Weber7bfaaae2008-08-10 19:59:06 +0000978 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000979}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000980
Douglas Gregora88084b2010-02-18 18:08:43 +0000981StoredDiagnostic::StoredDiagnostic() { }
982
983StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
984 llvm::StringRef Message)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000985 : Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000986
987StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
988 const DiagnosticInfo &Info)
989 : Level(Level), Loc(Info.getLocation())
990{
991 llvm::SmallString<64> Message;
992 Info.FormatDiagnostic(Message);
993 this->Message.assign(Message.begin(), Message.end());
994
995 Ranges.reserve(Info.getNumRanges());
996 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
997 Ranges.push_back(Info.getRange(I));
998
Douglas Gregor849b2432010-03-31 17:46:05 +0000999 FixIts.reserve(Info.getNumFixItHints());
1000 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
1001 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +00001002}
1003
1004StoredDiagnostic::~StoredDiagnostic() { }
1005
Douglas Gregord93256e2010-01-28 06:00:51 +00001006static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
1007 OS.write((const char *)&Value, sizeof(unsigned));
1008}
1009
1010static void WriteString(llvm::raw_ostream &OS, llvm::StringRef String) {
1011 WriteUnsigned(OS, String.size());
1012 OS.write(String.data(), String.size());
1013}
1014
1015static void WriteSourceLocation(llvm::raw_ostream &OS,
1016 SourceManager *SM,
1017 SourceLocation Location) {
1018 if (!SM || Location.isInvalid()) {
1019 // If we don't have a source manager or this location is invalid,
1020 // just write an invalid location.
1021 WriteUnsigned(OS, 0);
1022 WriteUnsigned(OS, 0);
1023 WriteUnsigned(OS, 0);
1024 return;
1025 }
1026
1027 Location = SM->getInstantiationLoc(Location);
1028 std::pair<FileID, unsigned> Decomposed = SM->getDecomposedLoc(Location);
1029
1030 WriteString(OS, SM->getFileEntryForID(Decomposed.first)->getName());
1031 WriteUnsigned(OS, SM->getLineNumber(Decomposed.first, Decomposed.second));
1032 WriteUnsigned(OS, SM->getColumnNumber(Decomposed.first, Decomposed.second));
1033}
1034
Douglas Gregora88084b2010-02-18 18:08:43 +00001035void StoredDiagnostic::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregord93256e2010-01-28 06:00:51 +00001036 SourceManager *SM = 0;
1037 if (getLocation().isValid())
1038 SM = &const_cast<SourceManager &>(getLocation().getManager());
1039
Douglas Gregorb9c903b2010-02-19 00:40:40 +00001040 // Write a short header to help identify diagnostics.
1041 OS << (char)0x06 << (char)0x07;
1042
Douglas Gregord93256e2010-01-28 06:00:51 +00001043 // Write the diagnostic level and location.
Douglas Gregora88084b2010-02-18 18:08:43 +00001044 WriteUnsigned(OS, (unsigned)Level);
Douglas Gregord93256e2010-01-28 06:00:51 +00001045 WriteSourceLocation(OS, SM, getLocation());
1046
1047 // Write the diagnostic message.
1048 llvm::SmallString<64> Message;
Douglas Gregora88084b2010-02-18 18:08:43 +00001049 WriteString(OS, getMessage());
Douglas Gregord93256e2010-01-28 06:00:51 +00001050
1051 // Count the number of ranges that don't point into macros, since
1052 // only simple file ranges serialize well.
1053 unsigned NumNonMacroRanges = 0;
Douglas Gregora88084b2010-02-18 18:08:43 +00001054 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1055 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregord93256e2010-01-28 06:00:51 +00001056 continue;
1057
1058 ++NumNonMacroRanges;
1059 }
1060
1061 // Write the ranges.
1062 WriteUnsigned(OS, NumNonMacroRanges);
1063 if (NumNonMacroRanges) {
Douglas Gregora88084b2010-02-18 18:08:43 +00001064 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1065 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregord93256e2010-01-28 06:00:51 +00001066 continue;
1067
Douglas Gregora88084b2010-02-18 18:08:43 +00001068 WriteSourceLocation(OS, SM, R->getBegin());
1069 WriteSourceLocation(OS, SM, R->getEnd());
Douglas Gregord93256e2010-01-28 06:00:51 +00001070 }
1071 }
1072
1073 // Determine if all of the fix-its involve rewrites with simple file
1074 // locations (not in macro instantiations). If so, we can write
1075 // fix-it information.
Douglas Gregora88084b2010-02-18 18:08:43 +00001076 unsigned NumFixIts = 0;
1077 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1078 if (F->RemoveRange.isValid() &&
1079 (F->RemoveRange.getBegin().isMacroID() ||
1080 F->RemoveRange.getEnd().isMacroID())) {
Douglas Gregord93256e2010-01-28 06:00:51 +00001081 NumFixIts = 0;
1082 break;
1083 }
1084
Douglas Gregora88084b2010-02-18 18:08:43 +00001085 if (F->InsertionLoc.isValid() && F->InsertionLoc.isMacroID()) {
Douglas Gregord93256e2010-01-28 06:00:51 +00001086 NumFixIts = 0;
1087 break;
1088 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001089
1090 ++NumFixIts;
Douglas Gregord93256e2010-01-28 06:00:51 +00001091 }
1092
1093 // Write the fix-its.
1094 WriteUnsigned(OS, NumFixIts);
Douglas Gregora88084b2010-02-18 18:08:43 +00001095 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1096 WriteSourceLocation(OS, SM, F->RemoveRange.getBegin());
1097 WriteSourceLocation(OS, SM, F->RemoveRange.getEnd());
1098 WriteSourceLocation(OS, SM, F->InsertionLoc);
1099 WriteString(OS, F->CodeToInsert);
Douglas Gregord93256e2010-01-28 06:00:51 +00001100 }
1101}
1102
Douglas Gregora88084b2010-02-18 18:08:43 +00001103static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
1104 unsigned &Value) {
1105 if (Memory + sizeof(unsigned) > MemoryEnd)
1106 return true;
1107
1108 memmove(&Value, Memory, sizeof(unsigned));
1109 Memory += sizeof(unsigned);
1110 return false;
1111}
1112
1113static bool ReadSourceLocation(FileManager &FM, SourceManager &SM,
1114 const char *&Memory, const char *MemoryEnd,
1115 SourceLocation &Location) {
1116 // Read the filename.
1117 unsigned FileNameLen = 0;
1118 if (ReadUnsigned(Memory, MemoryEnd, FileNameLen) ||
1119 Memory + FileNameLen > MemoryEnd)
1120 return true;
1121
1122 llvm::StringRef FileName(Memory, FileNameLen);
1123 Memory += FileNameLen;
1124
1125 // Read the line, column.
1126 unsigned Line = 0, Column = 0;
1127 if (ReadUnsigned(Memory, MemoryEnd, Line) ||
1128 ReadUnsigned(Memory, MemoryEnd, Column))
1129 return true;
1130
1131 if (FileName.empty()) {
1132 Location = SourceLocation();
1133 return false;
1134 }
1135
1136 const FileEntry *File = FM.getFile(FileName);
1137 if (!File)
1138 return true;
1139
1140 // Make sure that this file has an entry in the source manager.
1141 if (!SM.hasFileInfo(File))
1142 SM.createFileID(File, SourceLocation(), SrcMgr::C_User);
1143
1144 Location = SM.getLocation(File, Line, Column);
1145 return false;
1146}
1147
1148StoredDiagnostic
1149StoredDiagnostic::Deserialize(FileManager &FM, SourceManager &SM,
1150 const char *&Memory, const char *MemoryEnd) {
Douglas Gregorb9c903b2010-02-19 00:40:40 +00001151 while (true) {
1152 if (Memory == MemoryEnd)
1153 return StoredDiagnostic();
1154
1155 if (*Memory != 0x06) {
1156 ++Memory;
1157 continue;
1158 }
1159
1160 ++Memory;
1161 if (Memory == MemoryEnd)
1162 return StoredDiagnostic();
1163
1164 if (*Memory != 0x07) {
1165 ++Memory;
1166 continue;
1167 }
1168
1169 // We found the header. We're done.
1170 ++Memory;
1171 break;
1172 }
1173
Douglas Gregora88084b2010-02-18 18:08:43 +00001174 // Read the severity level.
1175 unsigned Level = 0;
1176 if (ReadUnsigned(Memory, MemoryEnd, Level) || Level > Diagnostic::Fatal)
1177 return StoredDiagnostic();
1178
1179 // Read the source location.
1180 SourceLocation Location;
1181 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Location))
1182 return StoredDiagnostic();
1183
1184 // Read the diagnostic text.
1185 if (Memory == MemoryEnd)
1186 return StoredDiagnostic();
1187
1188 unsigned MessageLen = 0;
1189 if (ReadUnsigned(Memory, MemoryEnd, MessageLen) ||
1190 Memory + MessageLen > MemoryEnd)
1191 return StoredDiagnostic();
1192
1193 llvm::StringRef Message(Memory, MessageLen);
1194 Memory += MessageLen;
1195
1196
1197 // At this point, we have enough information to form a diagnostic. Do so.
1198 StoredDiagnostic Diag;
1199 Diag.Level = (Diagnostic::Level)Level;
1200 Diag.Loc = FullSourceLoc(Location, SM);
1201 Diag.Message = Message;
1202 if (Memory == MemoryEnd)
1203 return Diag;
1204
1205 // Read the source ranges.
1206 unsigned NumSourceRanges = 0;
1207 if (ReadUnsigned(Memory, MemoryEnd, NumSourceRanges))
1208 return Diag;
1209 for (unsigned I = 0; I != NumSourceRanges; ++I) {
1210 SourceLocation Begin, End;
1211 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Begin) ||
1212 ReadSourceLocation(FM, SM, Memory, MemoryEnd, End))
1213 return Diag;
1214
1215 Diag.Ranges.push_back(SourceRange(Begin, End));
1216 }
1217
1218 // Read the fix-it hints.
1219 unsigned NumFixIts = 0;
1220 if (ReadUnsigned(Memory, MemoryEnd, NumFixIts))
1221 return Diag;
1222 for (unsigned I = 0; I != NumFixIts; ++I) {
1223 SourceLocation RemoveBegin, RemoveEnd, InsertionLoc;
1224 unsigned InsertLen = 0;
1225 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveBegin) ||
1226 ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveEnd) ||
1227 ReadSourceLocation(FM, SM, Memory, MemoryEnd, InsertionLoc) ||
1228 ReadUnsigned(Memory, MemoryEnd, InsertLen) ||
1229 Memory + InsertLen > MemoryEnd) {
1230 Diag.FixIts.clear();
1231 return Diag;
1232 }
1233
Douglas Gregor849b2432010-03-31 17:46:05 +00001234 FixItHint Hint;
Douglas Gregora88084b2010-02-18 18:08:43 +00001235 Hint.RemoveRange = SourceRange(RemoveBegin, RemoveEnd);
1236 Hint.InsertionLoc = InsertionLoc;
1237 Hint.CodeToInsert.assign(Memory, Memory + InsertLen);
1238 Memory += InsertLen;
1239 Diag.FixIts.push_back(Hint);
1240 }
1241
1242 return Diag;
1243}
1244
Ted Kremenekcabe6682009-01-23 20:28:53 +00001245/// IncludeInDiagnosticCounts - This method (whose default implementation
1246/// returns true) indicates whether the diagnostics handled by this
1247/// DiagnosticClient should be included in the number of diagnostics
1248/// reported by Diagnostic.
1249bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001250
1251PartialDiagnostic::StorageAllocator::StorageAllocator() {
1252 for (unsigned I = 0; I != NumCached; ++I)
1253 FreeList[I] = Cached + I;
1254 NumFreeListEntries = NumCached;
1255}
1256
1257PartialDiagnostic::StorageAllocator::~StorageAllocator() {
1258 assert(NumFreeListEntries == NumCached && "A partial is on the lamb");
1259}