blob: 738c27ccae761425a7b502d885518ec956d9f17f [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Diagnostic.h"
Douglas Gregor89336232010-03-29 23:34:08 +000015#include "clang/Basic/PartialDiagnostic.h"
Chris Lattnere007de32009-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 Gregorac0605e2010-01-28 06:00:51 +000025#include "clang/Basic/FileManager.h"
Chris Lattnerb91fd172008-11-19 07:32:16 +000026#include "clang/Basic/IdentifierTable.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000027#include "clang/Basic/SourceLocation.h"
Douglas Gregorac0605e2010-01-28 06:00:51 +000028#include "clang/Basic/SourceManager.h"
Chris Lattner23be0672008-11-19 06:51:40 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattner91aea712008-11-19 07:22:31 +000030#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3633792009-10-17 18:12:14 +000031#include "llvm/Support/raw_ostream.h"
Chris Lattnere6535cf2007-12-02 01:09:57 +000032#include <vector>
33#include <map>
Chris Lattner0d799d32008-03-10 17:04:53 +000034#include <cstring>
Chris Lattner22eb9722006-06-18 05:43:12 +000035using namespace clang;
36
Chris Lattnere6535cf2007-12-02 01:09:57 +000037//===----------------------------------------------------------------------===//
38// Builtin Diagnostic information
39//===----------------------------------------------------------------------===//
40
Chris Lattner6c440322009-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 Lattnere007de32009-04-15 07:01:18 +000048
Chris Lattner6a64cc62009-04-16 06:00:24 +000049struct StaticDiagInfoRec {
Chris Lattner6c440322009-04-16 06:07:15 +000050 unsigned short DiagID;
51 unsigned Mapping : 3;
52 unsigned Class : 3;
Douglas Gregor33834512009-06-14 07:33:30 +000053 bool SFINAE : 1;
Chris Lattner6c440322009-04-16 06:07:15 +000054 const char *Description;
Chris Lattner6a64cc62009-04-16 06:00:24 +000055 const char *OptionGroup;
Mike Stump11289f42009-09-09 15:08:12 +000056
Chris Lattner2d49eed2009-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 Lattnere007de32009-04-15 07:01:18 +000063};
64
Chris Lattner6a64cc62009-04-16 06:00:24 +000065static const StaticDiagInfoRec StaticDiagInfo[] = {
Douglas Gregor33834512009-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 Lattnere007de32009-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 Gregor33834512009-06-14 07:33:30 +000076 { 0, 0, 0, 0, 0, 0}
Chris Lattnere007de32009-04-15 07:01:18 +000077};
Chris Lattnere6c831d2009-04-15 16:56:26 +000078#undef DIAG
Chris Lattnere007de32009-04-15 07:01:18 +000079
Chris Lattner2d49eed2009-04-16 06:13:46 +000080/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
81/// or null if the ID is invalid.
Chris Lattner6a64cc62009-04-16 06:00:24 +000082static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner2d49eed2009-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 Lattnercb4e68c2009-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 Lattner2d49eed2009-04-16 06:13:46 +000094 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
95 "Improperly sorted diag info");
Chris Lattnercb4e68c2009-10-16 02:34:51 +000096 }
Chris Lattner2d49eed2009-04-16 06:13:46 +000097 IsFirst = false;
98 }
99#endif
Mike Stump11289f42009-09-09 15:08:12 +0000100
Chris Lattner2d49eed2009-04-16 06:13:46 +0000101 // Search the diagnostic table with a binary search.
Douglas Gregor33834512009-06-14 07:33:30 +0000102 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0 };
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattner2d49eed2009-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 Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattner2d49eed2009-04-16 06:13:46 +0000110 return Found;
Chris Lattner6a64cc62009-04-16 06:00:24 +0000111}
112
113static unsigned GetDefaultDiagMapping(unsigned DiagID) {
114 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Chris Lattner6c440322009-04-16 06:07:15 +0000115 return Info->Mapping;
Chris Lattner411c0ff2009-04-16 04:12:40 +0000116 return diag::MAP_FATAL;
117}
118
Chris Lattner22cb8182009-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 Lattner6a64cc62009-04-16 06:00:24 +0000123 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
124 return Info->OptionGroup;
125 return 0;
Chris Lattner22cb8182009-04-16 05:44:38 +0000126}
127
Douglas Gregor210b5902010-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 Gregor33834512009-06-14 07:33:30 +0000142}
143
Chris Lattner22eb9722006-06-18 05:43:12 +0000144/// getDiagClass - Return the class field of the diagnostic.
145///
Chris Lattner4431a1b2007-11-30 22:53:43 +0000146static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner6c440322009-04-16 06:07:15 +0000147 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
148 return Info->Class;
149 return ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +0000150}
151
Chris Lattnere6535cf2007-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 Stump11289f42009-09-09 15:08:12 +0000163
Chris Lattnere6535cf2007-12-02 01:09:57 +0000164 /// getDescription - Return the description of the specified custom
165 /// diagnostic.
166 const char *getDescription(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000167 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000168 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000169 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattnere6535cf2007-12-02 01:09:57 +0000170 }
Mike Stump11289f42009-09-09 15:08:12 +0000171
Chris Lattnere6535cf2007-12-02 01:09:57 +0000172 /// getLevel - Return the level of the specified custom diagnostic.
173 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000174 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000175 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000176 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000177 }
Mike Stump11289f42009-09-09 15:08:12 +0000178
Daniel Dunbar4886c812009-12-01 17:42:06 +0000179 unsigned getOrCreateDiagID(Diagnostic::Level L, llvm::StringRef Message,
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000180 Diagnostic &Diags) {
Chris Lattnere6535cf2007-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 Stump11289f42009-09-09 15:08:12 +0000186
Chris Lattnere6535cf2007-12-02 01:09:57 +0000187 // If not, assign a new ID.
Chris Lattner36790cf2009-01-29 06:55:46 +0000188 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000189 DiagIDs.insert(std::make_pair(D, ID));
190 DiagInfo.push_back(D);
191 return ID;
192 }
193 };
Mike Stump11289f42009-09-09 15:08:12 +0000194
195 } // end diag namespace
196} // end clang namespace
Chris Lattnere6535cf2007-12-02 01:09:57 +0000197
198
199//===----------------------------------------------------------------------===//
200// Common Diagnostic implementation
201//===----------------------------------------------------------------------===//
202
Chris Lattner63ecc502008-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 Lattnerc243f292009-10-20 05:25:22 +0000206 const Diagnostic::ArgumentValue *PrevArgs,
207 unsigned NumPrevArgs,
Chris Lattnercf868c42009-02-19 23:53:20 +0000208 llvm::SmallVectorImpl<char> &Output,
209 void *Cookie) {
Chris Lattner63ecc502008-11-23 09:21:17 +0000210 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000211 Output.append(Str, Str+strlen(Str));
212}
213
214
Ted Kremenek31691ae2008-08-07 17:49:57 +0000215Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattnere007de32009-04-15 07:01:18 +0000216 AllExtensionsSilenced = 0;
Chris Lattner8c800702008-05-29 15:36:45 +0000217 IgnoreAllWarnings = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000218 WarningsAsErrors = false;
Chris Lattner801fda82009-12-22 23:12:53 +0000219 ErrorsAsFatal = false;
Daniel Dunbar84b70f72008-09-12 18:10:20 +0000220 SuppressSystemWarnings = false;
Douglas Gregor2436e712009-09-17 21:32:03 +0000221 SuppressAllDiagnostics = false;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000222 ExtBehavior = Ext_Ignore;
Mike Stump11289f42009-09-09 15:08:12 +0000223
Chris Lattnerc49b9052007-05-28 00:46:44 +0000224 ErrorOccurred = false;
Chris Lattner9e031192009-02-06 04:16:02 +0000225 FatalErrorOccurred = false;
Chris Lattnerdec49e72010-04-07 20:37:06 +0000226 ErrorLimit = 0;
Steve Naroff4fb3d9f2009-12-05 02:14:08 +0000227
Chris Lattner198cb4d2010-04-07 18:47:42 +0000228 NumWarnings = 0;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000229 NumErrors = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000230 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000231 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000232 LastDiagLevel = Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000233
Chris Lattner63ecc502008-11-23 09:21:17 +0000234 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000235 ArgToStringCookie = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000236
Douglas Gregor85795312010-03-22 15:10:57 +0000237 DelayedDiagID = 0;
238
Chris Lattner411c0ff2009-04-16 04:12:40 +0000239 // Set all mappings to 'unset'.
Chris Lattnerfb42a182009-07-12 21:18:45 +0000240 DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0);
241 DiagMappingsStack.push_back(BlankDiags);
Chris Lattnerae411572006-07-05 00:55:08 +0000242}
243
Chris Lattnere6535cf2007-12-02 01:09:57 +0000244Diagnostic::~Diagnostic() {
245 delete CustomDiagInfo;
246}
247
Chris Lattnerfb42a182009-07-12 21:18:45 +0000248
249void Diagnostic::pushMappings() {
John Thompsond73d7ad2009-10-23 02:21:17 +0000250 // Avoids undefined behavior when the stack has to resize.
251 DiagMappingsStack.reserve(DiagMappingsStack.size() + 1);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000252 DiagMappingsStack.push_back(DiagMappingsStack.back());
253}
254
255bool Diagnostic::popMappings() {
256 if (DiagMappingsStack.size() == 1)
257 return false;
258
259 DiagMappingsStack.pop_back();
260 return true;
261}
262
Chris Lattnere6535cf2007-12-02 01:09:57 +0000263/// getCustomDiagID - Return an ID for a diagnostic with the specified message
264/// and level. If this is the first request for this diagnosic, it is
265/// registered and created, otherwise the existing ID is returned.
Daniel Dunbar4886c812009-12-01 17:42:06 +0000266unsigned Diagnostic::getCustomDiagID(Level L, llvm::StringRef Message) {
Mike Stump11289f42009-09-09 15:08:12 +0000267 if (CustomDiagInfo == 0)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000268 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000269 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000270}
271
272
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000273/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
274/// level of the specified diagnostic ID is a Warning or Extension.
275/// This only works on builtin diagnostics, not custom ones, and is not legal to
276/// call on NOTEs.
277bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000278 return DiagID < diag::DIAG_UPPER_LIMIT &&
279 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000280}
281
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000282/// \brief Determine whether the given built-in diagnostic ID is a
283/// Note.
284bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000285 return DiagID < diag::DIAG_UPPER_LIMIT &&
286 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000287}
288
Chris Lattnere007de32009-04-15 07:01:18 +0000289/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
290/// ID is for an extension of some sort.
291///
292bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000293 return DiagID < diag::DIAG_UPPER_LIMIT &&
294 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattnere007de32009-04-15 07:01:18 +0000295}
296
Chris Lattner22eb9722006-06-18 05:43:12 +0000297
298/// getDescription - Given a diagnostic ID, return a description of the
299/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000300const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner6c440322009-04-16 06:07:15 +0000301 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
302 return Info->Description;
Chris Lattner7368d582009-01-27 18:30:58 +0000303 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000304}
305
Douglas Gregor85795312010-03-22 15:10:57 +0000306void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
307 llvm::StringRef Arg2) {
308 if (DelayedDiagID)
309 return;
310
311 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000312 DelayedDiagArg1 = Arg1.str();
313 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000314}
315
316void Diagnostic::ReportDelayed() {
317 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
318 DelayedDiagID = 0;
319 DelayedDiagArg1.clear();
320 DelayedDiagArg2.clear();
321}
322
Chris Lattner22eb9722006-06-18 05:43:12 +0000323/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
324/// object, classify the specified diagnostic ID into a Level, consumable by
325/// the DiagnosticClient.
326Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000327 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000328 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000329 return CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000330
Chris Lattner4431a1b2007-11-30 22:53:43 +0000331 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000332 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000333 return getDiagnosticLevel(DiagID, DiagClass);
334}
335
336/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
337/// object, classify the specified diagnostic ID into a Level, consumable by
338/// the DiagnosticClient.
339Diagnostic::Level
340Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattnerae411572006-07-05 00:55:08 +0000341 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000342 // to error. Errors can only be mapped to fatal.
Chris Lattnere007de32009-04-15 07:01:18 +0000343 Diagnostic::Level Result = Diagnostic::Fatal;
Mike Stump11289f42009-09-09 15:08:12 +0000344
Chris Lattner411c0ff2009-04-16 04:12:40 +0000345 // Get the mapping information, if unset, compute it lazily.
346 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
347 if (MappingInfo == 0) {
348 MappingInfo = GetDefaultDiagMapping(DiagID);
349 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattner411c0ff2009-04-16 04:12:40 +0000352 switch (MappingInfo & 7) {
353 default: assert(0 && "Unknown mapping!");
Chris Lattnere007de32009-04-15 07:01:18 +0000354 case diag::MAP_IGNORE:
Chris Lattnerb8e73152009-04-16 05:04:32 +0000355 // Ignore this, unless this is an extension diagnostic and we're mapping
356 // them onto warnings or errors.
357 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
358 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
359 (MappingInfo & 8) != 0) // User explicitly mapped it.
360 return Diagnostic::Ignored;
361 Result = Diagnostic::Warning;
362 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000363 if (Result == Diagnostic::Error && ErrorsAsFatal)
364 Result = Diagnostic::Fatal;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000365 break;
Chris Lattnere007de32009-04-15 07:01:18 +0000366 case diag::MAP_ERROR:
367 Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000368 if (ErrorsAsFatal)
369 Result = Diagnostic::Fatal;
Chris Lattnere007de32009-04-15 07:01:18 +0000370 break;
371 case diag::MAP_FATAL:
372 Result = Diagnostic::Fatal;
373 break;
374 case diag::MAP_WARNING:
375 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner8c800702008-05-29 15:36:45 +0000376 if (IgnoreAllWarnings)
377 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000378
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000379 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000380
Chris Lattnerb8e73152009-04-16 05:04:32 +0000381 // If this is an extension diagnostic and we're in -pedantic-error mode, and
382 // if the user didn't explicitly map it, upgrade to an error.
383 if (ExtBehavior == Ext_Error &&
384 (MappingInfo & 8) == 0 &&
385 isBuiltinExtensionDiag(DiagID))
386 Result = Diagnostic::Error;
Mike Stump11289f42009-09-09 15:08:12 +0000387
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000388 if (WarningsAsErrors)
389 Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000390 if (Result == Diagnostic::Error && ErrorsAsFatal)
391 Result = Diagnostic::Fatal;
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000392 break;
Mike Stump11289f42009-09-09 15:08:12 +0000393
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000394 case diag::MAP_WARNING_NO_WERROR:
395 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
396 // not be adjusted by -Werror or -pedantic-errors.
397 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000399 // If warnings are globally mapped to ignore or error, do it.
400 if (IgnoreAllWarnings)
401 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000402
Chris Lattnere007de32009-04-15 07:01:18 +0000403 break;
Chris Lattner801fda82009-12-22 23:12:53 +0000404
405 case diag::MAP_ERROR_NO_WFATAL:
406 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
407 // unaffected by -Wfatal-errors.
408 Result = Diagnostic::Error;
409 break;
Chris Lattner8c800702008-05-29 15:36:45 +0000410 }
Chris Lattnere007de32009-04-15 07:01:18 +0000411
412 // Okay, we're about to return this as a "diagnostic to emit" one last check:
413 // if this is any sort of extension warning, and if we're in an __extension__
414 // block, silence it.
415 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
416 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattnere007de32009-04-15 07:01:18 +0000418 return Result;
Chris Lattner22eb9722006-06-18 05:43:12 +0000419}
420
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000421struct WarningOption {
422 const char *Name;
423 const short *Members;
424 const char *SubGroups;
425};
426
427#define GET_DIAG_ARRAYS
428#include "clang/Basic/DiagnosticGroups.inc"
429#undef GET_DIAG_ARRAYS
430
431// Second the table of options, sorted by name for fast binary lookup.
432static const WarningOption OptionTable[] = {
433#define GET_DIAG_TABLE
434#include "clang/Basic/DiagnosticGroups.inc"
435#undef GET_DIAG_TABLE
436};
437static const size_t OptionTableSize =
438sizeof(OptionTable) / sizeof(OptionTable[0]);
439
440static bool WarningOptionCompare(const WarningOption &LHS,
441 const WarningOption &RHS) {
442 return strcmp(LHS.Name, RHS.Name) < 0;
443}
444
445static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
446 Diagnostic &Diags) {
447 // Option exists, poke all the members of its diagnostic set.
448 if (const short *Member = Group->Members) {
449 for (; *Member != -1; ++Member)
450 Diags.setDiagnosticMapping(*Member, Mapping);
451 }
Mike Stump11289f42009-09-09 15:08:12 +0000452
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000453 // Enable/disable all subgroups along with this one.
454 if (const char *SubGroups = Group->SubGroups) {
455 for (; *SubGroups != (char)-1; ++SubGroups)
456 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
457 }
458}
459
460/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
461/// "unknown-pragmas" to have the specified mapping. This returns true and
462/// ignores the request if "Group" was unknown, false otherwise.
463bool Diagnostic::setDiagnosticGroupMapping(const char *Group,
464 diag::Mapping Map) {
Mike Stump11289f42009-09-09 15:08:12 +0000465
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000466 WarningOption Key = { Group, 0, 0 };
467 const WarningOption *Found =
468 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
469 WarningOptionCompare);
470 if (Found == OptionTable + OptionTableSize ||
471 strcmp(Found->Name, Group) != 0)
472 return true; // Option not found.
Mike Stump11289f42009-09-09 15:08:12 +0000473
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000474 MapGroupMembers(Found, Map, *this);
475 return false;
476}
477
478
Chris Lattner8488c822008-11-18 07:04:44 +0000479/// ProcessDiag - This is the method used to report a diagnostic that is
480/// finally fully formed.
Douglas Gregor33834512009-06-14 07:33:30 +0000481bool Diagnostic::ProcessDiag() {
Chris Lattner427c9c12008-11-22 00:59:29 +0000482 DiagnosticInfo Info(this);
Mike Stump11289f42009-09-09 15:08:12 +0000483
Douglas Gregor2436e712009-09-17 21:32:03 +0000484 if (SuppressAllDiagnostics)
485 return false;
486
Chris Lattner22eb9722006-06-18 05:43:12 +0000487 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000488 Diagnostic::Level DiagLevel;
489 unsigned DiagID = Info.getID();
Mike Stump11289f42009-09-09 15:08:12 +0000490
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000491 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
492 // in a system header.
493 bool ShouldEmitInSystemHeader;
Mike Stump11289f42009-09-09 15:08:12 +0000494
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000495 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
496 // Handle custom diagnostics, which cannot be mapped.
497 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000498
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000499 // Custom diagnostics always are emitted in system headers.
500 ShouldEmitInSystemHeader = true;
501 } else {
502 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
503 // the diagnostic level was for the previous diagnostic so that it is
504 // filtered the same as the previous diagnostic.
505 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000506 if (DiagClass == CLASS_NOTE) {
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000507 DiagLevel = Diagnostic::Note;
508 ShouldEmitInSystemHeader = false; // extra consideration is needed
509 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000510 // If this is not an error and we are in a system header, we ignore it.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000511 // Check the original Diag ID here, because we also want to ignore
512 // extensions and warnings in -Werror and -pedantic-errors modes, which
513 // *map* warnings/extensions to errors.
Chris Lattnere6c831d2009-04-15 16:56:26 +0000514 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Mike Stump11289f42009-09-09 15:08:12 +0000515
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000516 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
517 }
518 }
519
Douglas Gregor19367f52009-03-19 18:55:06 +0000520 if (DiagLevel != Diagnostic::Note) {
521 // Record that a fatal error occurred only when we see a second
522 // non-note diagnostic. This allows notes to be attached to the
523 // fatal error, but suppresses any diagnostics that follow those
524 // notes.
525 if (LastDiagLevel == Diagnostic::Fatal)
526 FatalErrorOccurred = true;
527
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000528 LastDiagLevel = DiagLevel;
Mike Stump11289f42009-09-09 15:08:12 +0000529 }
Douglas Gregor19367f52009-03-19 18:55:06 +0000530
531 // If a fatal error has already been emitted, silence all subsequent
532 // diagnostics.
533 if (FatalErrorOccurred)
Douglas Gregor33834512009-06-14 07:33:30 +0000534 return false;
Douglas Gregor19367f52009-03-19 18:55:06 +0000535
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000536 // If the client doesn't care about this message, don't issue it. If this is
537 // a note and the last real diagnostic was ignored, ignore it too.
538 if (DiagLevel == Diagnostic::Ignored ||
539 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Douglas Gregor33834512009-06-14 07:33:30 +0000540 return false;
Nico Weber4c311642008-08-10 19:59:06 +0000541
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000542 // If this diagnostic is in a system header and is not a clang error, suppress
543 // it.
544 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner8488c822008-11-18 07:04:44 +0000545 Info.getLocation().isValid() &&
John McCallbe089fa2010-02-11 10:04:29 +0000546 Info.getLocation().getInstantiationLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000547 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
548 LastDiagLevel = Diagnostic::Ignored;
Douglas Gregor33834512009-06-14 07:33:30 +0000549 return false;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000550 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000551
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000552 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000553 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000554 ++NumErrors;
Chris Lattner75a03932010-04-07 20:21:58 +0000555
556 // If we've emitted a lot of errors, emit a fatal error after it to stop a
557 // flood of bogus errors.
Chris Lattnerdec49e72010-04-07 20:37:06 +0000558 if (ErrorLimit && NumErrors >= ErrorLimit &&
Chris Lattner75a03932010-04-07 20:21:58 +0000559 DiagLevel == Diagnostic::Error)
560 SetDelayedDiagnostic(diag::fatal_too_many_errors);
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000561 }
Mike Stump11289f42009-09-09 15:08:12 +0000562
Chris Lattner22eb9722006-06-18 05:43:12 +0000563 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000564 Client->HandleDiagnostic(DiagLevel, Info);
Chris Lattner198cb4d2010-04-07 18:47:42 +0000565 if (Client->IncludeInDiagnosticCounts()) {
566 if (DiagLevel == Diagnostic::Warning)
567 ++NumWarnings;
568 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000569
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000570 CurDiagID = ~0U;
Douglas Gregor33834512009-06-14 07:33:30 +0000571
572 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000573}
574
Douglas Gregor85795312010-03-22 15:10:57 +0000575bool DiagnosticBuilder::Emit() {
576 // If DiagObj is null, then its soul was stolen by the copy ctor
577 // or the user called Emit().
578 if (DiagObj == 0) return false;
579
580 // When emitting diagnostics, we set the final argument count into
581 // the Diagnostic object.
582 DiagObj->NumDiagArgs = NumArgs;
583 DiagObj->NumDiagRanges = NumRanges;
Douglas Gregora771f462010-03-31 17:46:05 +0000584 DiagObj->NumFixItHints = NumFixItHints;
Douglas Gregor85795312010-03-22 15:10:57 +0000585
586 // Process the diagnostic, sending the accumulated information to the
587 // DiagnosticClient.
588 bool Emitted = DiagObj->ProcessDiag();
589
590 // Clear out the current diagnostic object.
Douglas Gregor96380982010-03-22 15:47:45 +0000591 unsigned DiagID = DiagObj->CurDiagID;
Douglas Gregor85795312010-03-22 15:10:57 +0000592 DiagObj->Clear();
593
594 // If there was a delayed diagnostic, emit it now.
Douglas Gregor96380982010-03-22 15:47:45 +0000595 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
Douglas Gregor85795312010-03-22 15:10:57 +0000596 DiagObj->ReportDelayed();
597
598 // This diagnostic is dead.
599 DiagObj = 0;
600
601 return Emitted;
602}
603
Nico Weber4c311642008-08-10 19:59:06 +0000604
Chris Lattner22eb9722006-06-18 05:43:12 +0000605DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000606
Chris Lattner23be0672008-11-19 06:51:40 +0000607
Chris Lattner2b786902008-11-21 07:50:02 +0000608/// ModifierIs - Return true if the specified modifier matches specified string.
609template <std::size_t StrLen>
610static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
611 const char (&Str)[StrLen]) {
612 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
613}
614
John McCall8cb7a8a32010-01-14 20:11:39 +0000615/// ScanForward - Scans forward, looking for the given character, skipping
616/// nested clauses and escaped characters.
617static const char *ScanFormat(const char *I, const char *E, char Target) {
618 unsigned Depth = 0;
619
620 for ( ; I != E; ++I) {
621 if (Depth == 0 && *I == Target) return I;
622 if (Depth != 0 && *I == '}') Depth--;
623
624 if (*I == '%') {
625 I++;
626 if (I == E) break;
627
628 // Escaped characters get implicitly skipped here.
629
630 // Format specifier.
631 if (!isdigit(*I) && !ispunct(*I)) {
632 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
633 if (I == E) break;
634 if (*I == '{')
635 Depth++;
636 }
637 }
638 }
639 return E;
640}
641
Chris Lattner2b786902008-11-21 07:50:02 +0000642/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
643/// like this: %select{foo|bar|baz}2. This means that the integer argument
644/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
645/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
646/// This is very useful for certain classes of variant diagnostics.
John McCalle4d54322010-01-13 23:58:20 +0000647static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000648 const char *Argument, unsigned ArgumentLen,
649 llvm::SmallVectorImpl<char> &OutStr) {
650 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000651
Chris Lattner2b786902008-11-21 07:50:02 +0000652 // Skip over 'ValNo' |'s.
653 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000654 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000655 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
656 " larger than the number of options in the diagnostic string!");
657 Argument = NextVal+1; // Skip this string.
658 --ValNo;
659 }
Mike Stump11289f42009-09-09 15:08:12 +0000660
Chris Lattner2b786902008-11-21 07:50:02 +0000661 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000662 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000663
664 // Recursively format the result of the select clause into the output string.
665 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000666}
667
668/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
669/// letter 's' to the string if the value is not 1. This is used in cases like
670/// this: "you idiot, you have %4 parameter%s4!".
671static void HandleIntegerSModifier(unsigned ValNo,
672 llvm::SmallVectorImpl<char> &OutStr) {
673 if (ValNo != 1)
674 OutStr.push_back('s');
675}
676
John McCall9015cde2010-01-14 00:50:32 +0000677/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
678/// prints the ordinal form of the given integer, with 1 corresponding
679/// to the first ordinal. Currently this is hard-coded to use the
680/// English form.
681static void HandleOrdinalModifier(unsigned ValNo,
682 llvm::SmallVectorImpl<char> &OutStr) {
683 assert(ValNo != 0 && "ValNo must be strictly positive!");
684
685 llvm::raw_svector_ostream Out(OutStr);
686
687 // We could use text forms for the first N ordinals, but the numeric
688 // forms are actually nicer in diagnostics because they stand out.
689 Out << ValNo;
690
691 // It is critically important that we do this perfectly for
692 // user-written sequences with over 100 elements.
693 switch (ValNo % 100) {
694 case 11:
695 case 12:
696 case 13:
697 Out << "th"; return;
698 default:
699 switch (ValNo % 10) {
700 case 1: Out << "st"; return;
701 case 2: Out << "nd"; return;
702 case 3: Out << "rd"; return;
703 default: Out << "th"; return;
704 }
705 }
706}
707
Chris Lattner2b786902008-11-21 07:50:02 +0000708
Sebastian Redl15b02d22008-11-22 13:44:36 +0000709/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000710static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000711 // Programming 101: Parse a decimal number :-)
712 unsigned Val = 0;
713 while (Start != End && *Start >= '0' && *Start <= '9') {
714 Val *= 10;
715 Val += *Start - '0';
716 ++Start;
717 }
718 return Val;
719}
720
721/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000722static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000723 if (*Start != '[') {
724 unsigned Ref = PluralNumber(Start, End);
725 return Ref == Val;
726 }
727
728 ++Start;
729 unsigned Low = PluralNumber(Start, End);
730 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
731 ++Start;
732 unsigned High = PluralNumber(Start, End);
733 assert(*Start == ']' && "Bad plural expression syntax: expected )");
734 ++Start;
735 return Low <= Val && Val <= High;
736}
737
738/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000739static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000740 // Empty condition?
741 if (*Start == ':')
742 return true;
743
744 while (1) {
745 char C = *Start;
746 if (C == '%') {
747 // Modulo expression
748 ++Start;
749 unsigned Arg = PluralNumber(Start, End);
750 assert(*Start == '=' && "Bad plural expression syntax: expected =");
751 ++Start;
752 unsigned ValMod = ValNo % Arg;
753 if (TestPluralRange(ValMod, Start, End))
754 return true;
755 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000756 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000757 "Bad plural expression syntax: unexpected character");
758 // Range expression
759 if (TestPluralRange(ValNo, Start, End))
760 return true;
761 }
762
763 // Scan for next or-expr part.
764 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000765 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000766 break;
767 ++Start;
768 }
769 return false;
770}
771
772/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
773/// for complex plural forms, or in languages where all plurals are complex.
774/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
775/// conditions that are tested in order, the form corresponding to the first
776/// that applies being emitted. The empty condition is always true, making the
777/// last form a default case.
778/// Conditions are simple boolean expressions, where n is the number argument.
779/// Here are the rules.
780/// condition := expression | empty
781/// empty := -> always true
782/// expression := numeric [',' expression] -> logical or
783/// numeric := range -> true if n in range
784/// | '%' number '=' range -> true if n % number in range
785/// range := number
786/// | '[' number ',' number ']' -> ranges are inclusive both ends
787///
788/// Here are some examples from the GNU gettext manual written in this form:
789/// English:
790/// {1:form0|:form1}
791/// Latvian:
792/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
793/// Gaeilge:
794/// {1:form0|2:form1|:form2}
795/// Romanian:
796/// {1:form0|0,%100=[1,19]:form1|:form2}
797/// Lithuanian:
798/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
799/// Russian (requires repeated form):
800/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
801/// Slovak
802/// {1:form0|[2,4]:form1|:form2}
803/// Polish (requires repeated form):
804/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
805static void HandlePluralModifier(unsigned ValNo,
806 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb8e73152009-04-16 05:04:32 +0000807 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000808 const char *ArgumentEnd = Argument + ArgumentLen;
809 while (1) {
810 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
811 const char *ExprEnd = Argument;
812 while (*ExprEnd != ':') {
813 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
814 ++ExprEnd;
815 }
816 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
817 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000818 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
Sebastian Redl15b02d22008-11-22 13:44:36 +0000819 OutStr.append(Argument, ExprEnd);
820 return;
821 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000822 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000823 }
824}
825
826
Chris Lattner23be0672008-11-19 06:51:40 +0000827/// FormatDiagnostic - Format this diagnostic into a string, substituting the
828/// formal arguments into the %0 slots. The result is appended onto the Str
829/// array.
830void DiagnosticInfo::
831FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
832 const char *DiagStr = getDiags()->getDescription(getID());
833 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump11289f42009-09-09 15:08:12 +0000834
John McCalle4d54322010-01-13 23:58:20 +0000835 FormatDiagnostic(DiagStr, DiagEnd, OutStr);
836}
837
838void DiagnosticInfo::
839FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
840 llvm::SmallVectorImpl<char> &OutStr) const {
841
Chris Lattnerc243f292009-10-20 05:25:22 +0000842 /// FormattedArgs - Keep track of all of the arguments formatted by
843 /// ConvertArgToString and pass them into subsequent calls to
844 /// ConvertArgToString, allowing the implementation to avoid redundancies in
845 /// obvious cases.
846 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
847
Chris Lattner23be0672008-11-19 06:51:40 +0000848 while (DiagStr != DiagEnd) {
849 if (DiagStr[0] != '%') {
850 // Append non-%0 substrings to Str if we have one.
851 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
852 OutStr.append(DiagStr, StrEnd);
853 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000854 continue;
John McCall8cb7a8a32010-01-14 20:11:39 +0000855 } else if (ispunct(DiagStr[1])) {
856 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000857 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000858 continue;
859 }
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattner2b786902008-11-21 07:50:02 +0000861 // Skip the %.
862 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000863
Chris Lattner2b786902008-11-21 07:50:02 +0000864 // This must be a placeholder for a diagnostic argument. The format for a
865 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
866 // The digit is a number from 0-9 indicating which argument this comes from.
867 // The modifier is a string of digits from the set [-a-z]+, arguments is a
868 // brace enclosed string.
869 const char *Modifier = 0, *Argument = 0;
870 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000871
Chris Lattner2b786902008-11-21 07:50:02 +0000872 // Check to see if we have a modifier. If so eat it.
873 if (!isdigit(DiagStr[0])) {
874 Modifier = DiagStr;
875 while (DiagStr[0] == '-' ||
876 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
877 ++DiagStr;
878 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000879
Chris Lattner2b786902008-11-21 07:50:02 +0000880 // If we have an argument, get it next.
881 if (DiagStr[0] == '{') {
882 ++DiagStr; // Skip {.
883 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000884
John McCall8cb7a8a32010-01-14 20:11:39 +0000885 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
886 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000887 ArgumentLen = DiagStr-Argument;
888 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000889 }
Chris Lattner2b786902008-11-21 07:50:02 +0000890 }
Mike Stump11289f42009-09-09 15:08:12 +0000891
Chris Lattner2b786902008-11-21 07:50:02 +0000892 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000893 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000894
Chris Lattnerc243f292009-10-20 05:25:22 +0000895 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
896
897 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000898 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000899 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000900 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000901 assert(ModifierLen == 0 && "No modifiers for strings yet");
902 OutStr.append(S.begin(), S.end());
903 break;
904 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000905 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000906 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000907 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000908
909 // Don't crash if get passed a null pointer by accident.
910 if (!S)
911 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000912
Chris Lattner2b786902008-11-21 07:50:02 +0000913 OutStr.append(S, S + strlen(S));
914 break;
915 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000916 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000917 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000918 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000919
Chris Lattner2b786902008-11-21 07:50:02 +0000920 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000921 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000922 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
923 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000924 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
925 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000926 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
927 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000928 } else {
929 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000930 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000931 }
Chris Lattner2b786902008-11-21 07:50:02 +0000932 break;
933 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000934 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000935 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000936
Chris Lattner2b786902008-11-21 07:50:02 +0000937 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000938 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000939 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
940 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000941 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
942 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000943 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
944 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000945 } else {
946 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000947 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000948 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000949 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000950 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000951 // ---- NAMES and TYPES ----
952 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000953 const IdentifierInfo *II = getArgIdentifier(ArgNo);
954 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000955
956 // Don't crash if get passed a null pointer by accident.
957 if (!II) {
958 const char *S = "(null)";
959 OutStr.append(S, S + strlen(S));
960 continue;
961 }
962
Daniel Dunbar07d07852009-10-18 21:17:35 +0000963 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000964 break;
965 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000966 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000967 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000968 case Diagnostic::ak_nameddecl:
Douglas Gregor053f6912009-08-26 00:04:55 +0000969 case Diagnostic::ak_nestednamespec:
Douglas Gregore40876a2009-10-13 21:16:44 +0000970 case Diagnostic::ak_declcontext:
Chris Lattnerc243f292009-10-20 05:25:22 +0000971 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000972 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000973 Argument, ArgumentLen,
974 FormattedArgs.data(), FormattedArgs.size(),
975 OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000976 break;
Nico Weber4c311642008-08-10 19:59:06 +0000977 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000978
979 // Remember this argument info for subsequent formatting operations. Turn
980 // std::strings into a null terminated string to make it be the same case as
981 // all the other ones.
982 if (Kind != Diagnostic::ak_std_string)
983 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
984 else
985 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
986 (intptr_t)getArgStdStr(ArgNo).c_str()));
987
Nico Weber4c311642008-08-10 19:59:06 +0000988 }
Nico Weber4c311642008-08-10 19:59:06 +0000989}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000990
Douglas Gregor33cdd812010-02-18 18:08:43 +0000991StoredDiagnostic::StoredDiagnostic() { }
992
993StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
994 llvm::StringRef Message)
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000995 : Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000996
997StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
998 const DiagnosticInfo &Info)
999 : Level(Level), Loc(Info.getLocation())
1000{
1001 llvm::SmallString<64> Message;
1002 Info.FormatDiagnostic(Message);
1003 this->Message.assign(Message.begin(), Message.end());
1004
1005 Ranges.reserve(Info.getNumRanges());
1006 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
1007 Ranges.push_back(Info.getRange(I));
1008
Douglas Gregora771f462010-03-31 17:46:05 +00001009 FixIts.reserve(Info.getNumFixItHints());
1010 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
1011 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +00001012}
1013
1014StoredDiagnostic::~StoredDiagnostic() { }
1015
Douglas Gregorac0605e2010-01-28 06:00:51 +00001016static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
1017 OS.write((const char *)&Value, sizeof(unsigned));
1018}
1019
1020static void WriteString(llvm::raw_ostream &OS, llvm::StringRef String) {
1021 WriteUnsigned(OS, String.size());
1022 OS.write(String.data(), String.size());
1023}
1024
1025static void WriteSourceLocation(llvm::raw_ostream &OS,
1026 SourceManager *SM,
1027 SourceLocation Location) {
1028 if (!SM || Location.isInvalid()) {
1029 // If we don't have a source manager or this location is invalid,
1030 // just write an invalid location.
1031 WriteUnsigned(OS, 0);
1032 WriteUnsigned(OS, 0);
1033 WriteUnsigned(OS, 0);
1034 return;
1035 }
1036
1037 Location = SM->getInstantiationLoc(Location);
1038 std::pair<FileID, unsigned> Decomposed = SM->getDecomposedLoc(Location);
1039
1040 WriteString(OS, SM->getFileEntryForID(Decomposed.first)->getName());
1041 WriteUnsigned(OS, SM->getLineNumber(Decomposed.first, Decomposed.second));
1042 WriteUnsigned(OS, SM->getColumnNumber(Decomposed.first, Decomposed.second));
1043}
1044
Douglas Gregor33cdd812010-02-18 18:08:43 +00001045void StoredDiagnostic::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001046 SourceManager *SM = 0;
1047 if (getLocation().isValid())
1048 SM = &const_cast<SourceManager &>(getLocation().getManager());
1049
Douglas Gregor70127c12010-02-19 00:40:40 +00001050 // Write a short header to help identify diagnostics.
1051 OS << (char)0x06 << (char)0x07;
1052
Douglas Gregorac0605e2010-01-28 06:00:51 +00001053 // Write the diagnostic level and location.
Douglas Gregor33cdd812010-02-18 18:08:43 +00001054 WriteUnsigned(OS, (unsigned)Level);
Douglas Gregorac0605e2010-01-28 06:00:51 +00001055 WriteSourceLocation(OS, SM, getLocation());
1056
1057 // Write the diagnostic message.
1058 llvm::SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001059 WriteString(OS, getMessage());
Douglas Gregorac0605e2010-01-28 06:00:51 +00001060
1061 // Count the number of ranges that don't point into macros, since
1062 // only simple file ranges serialize well.
1063 unsigned NumNonMacroRanges = 0;
Douglas Gregor33cdd812010-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 Gregorac0605e2010-01-28 06:00:51 +00001066 continue;
1067
1068 ++NumNonMacroRanges;
1069 }
1070
1071 // Write the ranges.
1072 WriteUnsigned(OS, NumNonMacroRanges);
1073 if (NumNonMacroRanges) {
Douglas Gregor33cdd812010-02-18 18:08:43 +00001074 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1075 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregorac0605e2010-01-28 06:00:51 +00001076 continue;
1077
Douglas Gregor33cdd812010-02-18 18:08:43 +00001078 WriteSourceLocation(OS, SM, R->getBegin());
1079 WriteSourceLocation(OS, SM, R->getEnd());
Douglas Gregorac0605e2010-01-28 06:00:51 +00001080 }
1081 }
1082
1083 // Determine if all of the fix-its involve rewrites with simple file
1084 // locations (not in macro instantiations). If so, we can write
1085 // fix-it information.
Douglas Gregor33cdd812010-02-18 18:08:43 +00001086 unsigned NumFixIts = 0;
1087 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1088 if (F->RemoveRange.isValid() &&
1089 (F->RemoveRange.getBegin().isMacroID() ||
1090 F->RemoveRange.getEnd().isMacroID())) {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001091 NumFixIts = 0;
1092 break;
1093 }
1094
Douglas Gregor33cdd812010-02-18 18:08:43 +00001095 if (F->InsertionLoc.isValid() && F->InsertionLoc.isMacroID()) {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001096 NumFixIts = 0;
1097 break;
1098 }
Douglas Gregor33cdd812010-02-18 18:08:43 +00001099
1100 ++NumFixIts;
Douglas Gregorac0605e2010-01-28 06:00:51 +00001101 }
1102
1103 // Write the fix-its.
1104 WriteUnsigned(OS, NumFixIts);
Douglas Gregor33cdd812010-02-18 18:08:43 +00001105 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1106 WriteSourceLocation(OS, SM, F->RemoveRange.getBegin());
1107 WriteSourceLocation(OS, SM, F->RemoveRange.getEnd());
1108 WriteSourceLocation(OS, SM, F->InsertionLoc);
1109 WriteString(OS, F->CodeToInsert);
Douglas Gregorac0605e2010-01-28 06:00:51 +00001110 }
1111}
1112
Douglas Gregor33cdd812010-02-18 18:08:43 +00001113static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
1114 unsigned &Value) {
1115 if (Memory + sizeof(unsigned) > MemoryEnd)
1116 return true;
1117
1118 memmove(&Value, Memory, sizeof(unsigned));
1119 Memory += sizeof(unsigned);
1120 return false;
1121}
1122
1123static bool ReadSourceLocation(FileManager &FM, SourceManager &SM,
1124 const char *&Memory, const char *MemoryEnd,
1125 SourceLocation &Location) {
1126 // Read the filename.
1127 unsigned FileNameLen = 0;
1128 if (ReadUnsigned(Memory, MemoryEnd, FileNameLen) ||
1129 Memory + FileNameLen > MemoryEnd)
1130 return true;
1131
1132 llvm::StringRef FileName(Memory, FileNameLen);
1133 Memory += FileNameLen;
1134
1135 // Read the line, column.
1136 unsigned Line = 0, Column = 0;
1137 if (ReadUnsigned(Memory, MemoryEnd, Line) ||
1138 ReadUnsigned(Memory, MemoryEnd, Column))
1139 return true;
1140
1141 if (FileName.empty()) {
1142 Location = SourceLocation();
1143 return false;
1144 }
1145
1146 const FileEntry *File = FM.getFile(FileName);
1147 if (!File)
1148 return true;
1149
1150 // Make sure that this file has an entry in the source manager.
1151 if (!SM.hasFileInfo(File))
1152 SM.createFileID(File, SourceLocation(), SrcMgr::C_User);
1153
1154 Location = SM.getLocation(File, Line, Column);
1155 return false;
1156}
1157
1158StoredDiagnostic
1159StoredDiagnostic::Deserialize(FileManager &FM, SourceManager &SM,
1160 const char *&Memory, const char *MemoryEnd) {
Douglas Gregor70127c12010-02-19 00:40:40 +00001161 while (true) {
1162 if (Memory == MemoryEnd)
1163 return StoredDiagnostic();
1164
1165 if (*Memory != 0x06) {
1166 ++Memory;
1167 continue;
1168 }
1169
1170 ++Memory;
1171 if (Memory == MemoryEnd)
1172 return StoredDiagnostic();
1173
1174 if (*Memory != 0x07) {
1175 ++Memory;
1176 continue;
1177 }
1178
1179 // We found the header. We're done.
1180 ++Memory;
1181 break;
1182 }
1183
Douglas Gregor33cdd812010-02-18 18:08:43 +00001184 // Read the severity level.
1185 unsigned Level = 0;
1186 if (ReadUnsigned(Memory, MemoryEnd, Level) || Level > Diagnostic::Fatal)
1187 return StoredDiagnostic();
1188
1189 // Read the source location.
1190 SourceLocation Location;
1191 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Location))
1192 return StoredDiagnostic();
1193
1194 // Read the diagnostic text.
1195 if (Memory == MemoryEnd)
1196 return StoredDiagnostic();
1197
1198 unsigned MessageLen = 0;
1199 if (ReadUnsigned(Memory, MemoryEnd, MessageLen) ||
1200 Memory + MessageLen > MemoryEnd)
1201 return StoredDiagnostic();
1202
1203 llvm::StringRef Message(Memory, MessageLen);
1204 Memory += MessageLen;
1205
1206
1207 // At this point, we have enough information to form a diagnostic. Do so.
1208 StoredDiagnostic Diag;
1209 Diag.Level = (Diagnostic::Level)Level;
1210 Diag.Loc = FullSourceLoc(Location, SM);
1211 Diag.Message = Message;
1212 if (Memory == MemoryEnd)
1213 return Diag;
1214
1215 // Read the source ranges.
1216 unsigned NumSourceRanges = 0;
1217 if (ReadUnsigned(Memory, MemoryEnd, NumSourceRanges))
1218 return Diag;
1219 for (unsigned I = 0; I != NumSourceRanges; ++I) {
1220 SourceLocation Begin, End;
1221 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Begin) ||
1222 ReadSourceLocation(FM, SM, Memory, MemoryEnd, End))
1223 return Diag;
1224
1225 Diag.Ranges.push_back(SourceRange(Begin, End));
1226 }
1227
1228 // Read the fix-it hints.
1229 unsigned NumFixIts = 0;
1230 if (ReadUnsigned(Memory, MemoryEnd, NumFixIts))
1231 return Diag;
1232 for (unsigned I = 0; I != NumFixIts; ++I) {
1233 SourceLocation RemoveBegin, RemoveEnd, InsertionLoc;
1234 unsigned InsertLen = 0;
1235 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveBegin) ||
1236 ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveEnd) ||
1237 ReadSourceLocation(FM, SM, Memory, MemoryEnd, InsertionLoc) ||
1238 ReadUnsigned(Memory, MemoryEnd, InsertLen) ||
1239 Memory + InsertLen > MemoryEnd) {
1240 Diag.FixIts.clear();
1241 return Diag;
1242 }
1243
Douglas Gregora771f462010-03-31 17:46:05 +00001244 FixItHint Hint;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001245 Hint.RemoveRange = SourceRange(RemoveBegin, RemoveEnd);
1246 Hint.InsertionLoc = InsertionLoc;
1247 Hint.CodeToInsert.assign(Memory, Memory + InsertLen);
1248 Memory += InsertLen;
1249 Diag.FixIts.push_back(Hint);
1250 }
1251
1252 return Diag;
1253}
1254
Ted Kremenekea06ec12009-01-23 20:28:53 +00001255/// IncludeInDiagnosticCounts - This method (whose default implementation
1256/// returns true) indicates whether the diagnostics handled by this
1257/// DiagnosticClient should be included in the number of diagnostics
1258/// reported by Diagnostic.
1259bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +00001260
1261PartialDiagnostic::StorageAllocator::StorageAllocator() {
1262 for (unsigned I = 0; I != NumCached; ++I)
1263 FreeList[I] = Cached + I;
1264 NumFreeListEntries = NumCached;
1265}
1266
1267PartialDiagnostic::StorageAllocator::~StorageAllocator() {
1268 assert(NumFreeListEntries == NumCached && "A partial is on the lamb");
1269}