blob: 21a8aeae429ffdd06019b6307a2764cbae9f4112 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Diagnostic.h"
Chris Lattnere007de32009-04-15 07:01:18 +000015
16#include "clang/Lex/LexDiagnostic.h"
17#include "clang/Parse/ParseDiagnostic.h"
18#include "clang/AST/ASTDiagnostic.h"
19#include "clang/Sema/SemaDiagnostic.h"
20#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Analysis/AnalysisDiagnostic.h"
22#include "clang/Driver/DriverDiagnostic.h"
23
Douglas Gregorac0605e2010-01-28 06:00:51 +000024#include "clang/Basic/FileManager.h"
Chris Lattnerb91fd172008-11-19 07:32:16 +000025#include "clang/Basic/IdentifierTable.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000026#include "clang/Basic/SourceLocation.h"
Douglas Gregorac0605e2010-01-28 06:00:51 +000027#include "clang/Basic/SourceManager.h"
Chris Lattner23be0672008-11-19 06:51:40 +000028#include "llvm/ADT/SmallVector.h"
Chris Lattner91aea712008-11-19 07:22:31 +000029#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3633792009-10-17 18:12:14 +000030#include "llvm/Support/raw_ostream.h"
Chris Lattnere6535cf2007-12-02 01:09:57 +000031#include <vector>
32#include <map>
Chris Lattner0d799d32008-03-10 17:04:53 +000033#include <cstring>
Chris Lattner22eb9722006-06-18 05:43:12 +000034using namespace clang;
35
Chris Lattnere6535cf2007-12-02 01:09:57 +000036//===----------------------------------------------------------------------===//
37// Builtin Diagnostic information
38//===----------------------------------------------------------------------===//
39
Chris Lattner6c440322009-04-16 06:07:15 +000040// Diagnostic classes.
41enum {
42 CLASS_NOTE = 0x01,
43 CLASS_WARNING = 0x02,
44 CLASS_EXTENSION = 0x03,
45 CLASS_ERROR = 0x04
46};
Chris Lattnere007de32009-04-15 07:01:18 +000047
Chris Lattner6a64cc62009-04-16 06:00:24 +000048struct StaticDiagInfoRec {
Chris Lattner6c440322009-04-16 06:07:15 +000049 unsigned short DiagID;
50 unsigned Mapping : 3;
51 unsigned Class : 3;
Douglas Gregor33834512009-06-14 07:33:30 +000052 bool SFINAE : 1;
Chris Lattner6c440322009-04-16 06:07:15 +000053 const char *Description;
Chris Lattner6a64cc62009-04-16 06:00:24 +000054 const char *OptionGroup;
Mike Stump11289f42009-09-09 15:08:12 +000055
Chris Lattner2d49eed2009-04-16 06:13:46 +000056 bool operator<(const StaticDiagInfoRec &RHS) const {
57 return DiagID < RHS.DiagID;
58 }
59 bool operator>(const StaticDiagInfoRec &RHS) const {
60 return DiagID > RHS.DiagID;
61 }
Chris Lattnere007de32009-04-15 07:01:18 +000062};
63
Chris Lattner6a64cc62009-04-16 06:00:24 +000064static const StaticDiagInfoRec StaticDiagInfo[] = {
Douglas Gregor33834512009-06-14 07:33:30 +000065#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,SFINAE) \
66 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, DESC, GROUP },
Chris Lattnere007de32009-04-15 07:01:18 +000067#include "clang/Basic/DiagnosticCommonKinds.inc"
68#include "clang/Basic/DiagnosticDriverKinds.inc"
69#include "clang/Basic/DiagnosticFrontendKinds.inc"
70#include "clang/Basic/DiagnosticLexKinds.inc"
71#include "clang/Basic/DiagnosticParseKinds.inc"
72#include "clang/Basic/DiagnosticASTKinds.inc"
73#include "clang/Basic/DiagnosticSemaKinds.inc"
74#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Douglas Gregor33834512009-06-14 07:33:30 +000075 { 0, 0, 0, 0, 0, 0}
Chris Lattnere007de32009-04-15 07:01:18 +000076};
Chris Lattnere6c831d2009-04-15 16:56:26 +000077#undef DIAG
Chris Lattnere007de32009-04-15 07:01:18 +000078
Chris Lattner2d49eed2009-04-16 06:13:46 +000079/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
80/// or null if the ID is invalid.
Chris Lattner6a64cc62009-04-16 06:00:24 +000081static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner2d49eed2009-04-16 06:13:46 +000082 unsigned NumDiagEntries = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
83
84 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
85#ifndef NDEBUG
86 static bool IsFirst = true;
87 if (IsFirst) {
Chris Lattnercb4e68c2009-10-16 02:34:51 +000088 for (unsigned i = 1; i != NumDiagEntries; ++i) {
89 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
90 "Diag ID conflict, the enums at the start of clang::diag (in "
91 "Diagnostic.h) probably need to be increased");
92
Chris Lattner2d49eed2009-04-16 06:13:46 +000093 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
94 "Improperly sorted diag info");
Chris Lattnercb4e68c2009-10-16 02:34:51 +000095 }
Chris Lattner2d49eed2009-04-16 06:13:46 +000096 IsFirst = false;
97 }
98#endif
Mike Stump11289f42009-09-09 15:08:12 +000099
Chris Lattner2d49eed2009-04-16 06:13:46 +0000100 // Search the diagnostic table with a binary search.
Douglas Gregor33834512009-06-14 07:33:30 +0000101 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0 };
Mike Stump11289f42009-09-09 15:08:12 +0000102
Chris Lattner2d49eed2009-04-16 06:13:46 +0000103 const StaticDiagInfoRec *Found =
104 std::lower_bound(StaticDiagInfo, StaticDiagInfo + NumDiagEntries, Find);
105 if (Found == StaticDiagInfo + NumDiagEntries ||
106 Found->DiagID != DiagID)
107 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000108
Chris Lattner2d49eed2009-04-16 06:13:46 +0000109 return Found;
Chris Lattner6a64cc62009-04-16 06:00:24 +0000110}
111
112static unsigned GetDefaultDiagMapping(unsigned DiagID) {
113 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Chris Lattner6c440322009-04-16 06:07:15 +0000114 return Info->Mapping;
Chris Lattner411c0ff2009-04-16 04:12:40 +0000115 return diag::MAP_FATAL;
116}
117
Chris Lattner22cb8182009-04-16 05:44:38 +0000118/// getWarningOptionForDiag - Return the lowest-level warning option that
119/// enables the specified diagnostic. If there is no -Wfoo flag that controls
120/// the diagnostic, this returns null.
121const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) {
Chris Lattner6a64cc62009-04-16 06:00:24 +0000122 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
123 return Info->OptionGroup;
124 return 0;
Chris Lattner22cb8182009-04-16 05:44:38 +0000125}
126
Douglas Gregor210b5902010-03-25 22:17:48 +0000127Diagnostic::SFINAEResponse
128Diagnostic::getDiagnosticSFINAEResponse(unsigned DiagID) {
129 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) {
130 if (!Info->SFINAE)
131 return SFINAE_Report;
132
133 if (Info->Class == CLASS_ERROR)
134 return SFINAE_SubstitutionFailure;
135
136 // Suppress notes, warnings, and extensions;
137 return SFINAE_Suppress;
138 }
139
140 return SFINAE_Report;
Douglas Gregor33834512009-06-14 07:33:30 +0000141}
142
Chris Lattner22eb9722006-06-18 05:43:12 +0000143/// getDiagClass - Return the class field of the diagnostic.
144///
Chris Lattner4431a1b2007-11-30 22:53:43 +0000145static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner6c440322009-04-16 06:07:15 +0000146 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
147 return Info->Class;
148 return ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +0000149}
150
Chris Lattnere6535cf2007-12-02 01:09:57 +0000151//===----------------------------------------------------------------------===//
152// Custom Diagnostic information
153//===----------------------------------------------------------------------===//
154
155namespace clang {
156 namespace diag {
157 class CustomDiagInfo {
158 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
159 std::vector<DiagDesc> DiagInfo;
160 std::map<DiagDesc, unsigned> DiagIDs;
161 public:
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattnere6535cf2007-12-02 01:09:57 +0000163 /// getDescription - Return the description of the specified custom
164 /// diagnostic.
165 const char *getDescription(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000166 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000167 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000168 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattnere6535cf2007-12-02 01:09:57 +0000169 }
Mike Stump11289f42009-09-09 15:08:12 +0000170
Chris Lattnere6535cf2007-12-02 01:09:57 +0000171 /// getLevel - Return the level of the specified custom diagnostic.
172 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000173 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000174 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000175 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000176 }
Mike Stump11289f42009-09-09 15:08:12 +0000177
Daniel Dunbar4886c812009-12-01 17:42:06 +0000178 unsigned getOrCreateDiagID(Diagnostic::Level L, llvm::StringRef Message,
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000179 Diagnostic &Diags) {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000180 DiagDesc D(L, Message);
181 // Check to see if it already exists.
182 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
183 if (I != DiagIDs.end() && I->first == D)
184 return I->second;
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattnere6535cf2007-12-02 01:09:57 +0000186 // If not, assign a new ID.
Chris Lattner36790cf2009-01-29 06:55:46 +0000187 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000188 DiagIDs.insert(std::make_pair(D, ID));
189 DiagInfo.push_back(D);
190 return ID;
191 }
192 };
Mike Stump11289f42009-09-09 15:08:12 +0000193
194 } // end diag namespace
195} // end clang namespace
Chris Lattnere6535cf2007-12-02 01:09:57 +0000196
197
198//===----------------------------------------------------------------------===//
199// Common Diagnostic implementation
200//===----------------------------------------------------------------------===//
201
Chris Lattner63ecc502008-11-23 09:21:17 +0000202static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
203 const char *Modifier, unsigned ML,
204 const char *Argument, unsigned ArgLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000205 const Diagnostic::ArgumentValue *PrevArgs,
206 unsigned NumPrevArgs,
Chris Lattnercf868c42009-02-19 23:53:20 +0000207 llvm::SmallVectorImpl<char> &Output,
208 void *Cookie) {
Chris Lattner63ecc502008-11-23 09:21:17 +0000209 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000210 Output.append(Str, Str+strlen(Str));
211}
212
213
Ted Kremenek31691ae2008-08-07 17:49:57 +0000214Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattnere007de32009-04-15 07:01:18 +0000215 AllExtensionsSilenced = 0;
Chris Lattner8c800702008-05-29 15:36:45 +0000216 IgnoreAllWarnings = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000217 WarningsAsErrors = false;
Chris Lattner801fda82009-12-22 23:12:53 +0000218 ErrorsAsFatal = false;
Daniel Dunbar84b70f72008-09-12 18:10:20 +0000219 SuppressSystemWarnings = false;
Douglas Gregor2436e712009-09-17 21:32:03 +0000220 SuppressAllDiagnostics = false;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000221 ExtBehavior = Ext_Ignore;
Mike Stump11289f42009-09-09 15:08:12 +0000222
Chris Lattnerc49b9052007-05-28 00:46:44 +0000223 ErrorOccurred = false;
Chris Lattner9e031192009-02-06 04:16:02 +0000224 FatalErrorOccurred = false;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000225 NumDiagnostics = 0;
Steve Naroff4fb3d9f2009-12-05 02:14:08 +0000226
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000227 NumErrors = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000228 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000229 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000230 LastDiagLevel = Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000231
Chris Lattner63ecc502008-11-23 09:21:17 +0000232 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000233 ArgToStringCookie = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000234
Douglas Gregor85795312010-03-22 15:10:57 +0000235 DelayedDiagID = 0;
236
Chris Lattner411c0ff2009-04-16 04:12:40 +0000237 // Set all mappings to 'unset'.
Chris Lattnerfb42a182009-07-12 21:18:45 +0000238 DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0);
239 DiagMappingsStack.push_back(BlankDiags);
Chris Lattnerae411572006-07-05 00:55:08 +0000240}
241
Chris Lattnere6535cf2007-12-02 01:09:57 +0000242Diagnostic::~Diagnostic() {
243 delete CustomDiagInfo;
244}
245
Chris Lattnerfb42a182009-07-12 21:18:45 +0000246
247void Diagnostic::pushMappings() {
John Thompsond73d7ad2009-10-23 02:21:17 +0000248 // Avoids undefined behavior when the stack has to resize.
249 DiagMappingsStack.reserve(DiagMappingsStack.size() + 1);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000250 DiagMappingsStack.push_back(DiagMappingsStack.back());
251}
252
253bool Diagnostic::popMappings() {
254 if (DiagMappingsStack.size() == 1)
255 return false;
256
257 DiagMappingsStack.pop_back();
258 return true;
259}
260
Chris Lattnere6535cf2007-12-02 01:09:57 +0000261/// getCustomDiagID - Return an ID for a diagnostic with the specified message
262/// and level. If this is the first request for this diagnosic, it is
263/// registered and created, otherwise the existing ID is returned.
Daniel Dunbar4886c812009-12-01 17:42:06 +0000264unsigned Diagnostic::getCustomDiagID(Level L, llvm::StringRef Message) {
Mike Stump11289f42009-09-09 15:08:12 +0000265 if (CustomDiagInfo == 0)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000266 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000267 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000268}
269
270
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000271/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
272/// level of the specified diagnostic ID is a Warning or Extension.
273/// This only works on builtin diagnostics, not custom ones, and is not legal to
274/// call on NOTEs.
275bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000276 return DiagID < diag::DIAG_UPPER_LIMIT &&
277 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000278}
279
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000280/// \brief Determine whether the given built-in diagnostic ID is a
281/// Note.
282bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000283 return DiagID < diag::DIAG_UPPER_LIMIT &&
284 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000285}
286
Chris Lattnere007de32009-04-15 07:01:18 +0000287/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
288/// ID is for an extension of some sort.
289///
290bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000291 return DiagID < diag::DIAG_UPPER_LIMIT &&
292 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattnere007de32009-04-15 07:01:18 +0000293}
294
Chris Lattner22eb9722006-06-18 05:43:12 +0000295
296/// getDescription - Given a diagnostic ID, return a description of the
297/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000298const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner6c440322009-04-16 06:07:15 +0000299 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
300 return Info->Description;
Chris Lattner7368d582009-01-27 18:30:58 +0000301 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000302}
303
Douglas Gregor85795312010-03-22 15:10:57 +0000304void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
305 llvm::StringRef Arg2) {
306 if (DelayedDiagID)
307 return;
308
309 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000310 DelayedDiagArg1 = Arg1.str();
311 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000312}
313
314void Diagnostic::ReportDelayed() {
315 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
316 DelayedDiagID = 0;
317 DelayedDiagArg1.clear();
318 DelayedDiagArg2.clear();
319}
320
Chris Lattner22eb9722006-06-18 05:43:12 +0000321/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
322/// object, classify the specified diagnostic ID into a Level, consumable by
323/// the DiagnosticClient.
324Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000325 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000326 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000327 return CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000328
Chris Lattner4431a1b2007-11-30 22:53:43 +0000329 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000330 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000331 return getDiagnosticLevel(DiagID, DiagClass);
332}
333
334/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
335/// object, classify the specified diagnostic ID into a Level, consumable by
336/// the DiagnosticClient.
337Diagnostic::Level
338Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattnerae411572006-07-05 00:55:08 +0000339 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000340 // to error. Errors can only be mapped to fatal.
Chris Lattnere007de32009-04-15 07:01:18 +0000341 Diagnostic::Level Result = Diagnostic::Fatal;
Mike Stump11289f42009-09-09 15:08:12 +0000342
Chris Lattner411c0ff2009-04-16 04:12:40 +0000343 // Get the mapping information, if unset, compute it lazily.
344 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
345 if (MappingInfo == 0) {
346 MappingInfo = GetDefaultDiagMapping(DiagID);
347 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
348 }
Mike Stump11289f42009-09-09 15:08:12 +0000349
Chris Lattner411c0ff2009-04-16 04:12:40 +0000350 switch (MappingInfo & 7) {
351 default: assert(0 && "Unknown mapping!");
Chris Lattnere007de32009-04-15 07:01:18 +0000352 case diag::MAP_IGNORE:
Chris Lattnerb8e73152009-04-16 05:04:32 +0000353 // Ignore this, unless this is an extension diagnostic and we're mapping
354 // them onto warnings or errors.
355 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
356 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
357 (MappingInfo & 8) != 0) // User explicitly mapped it.
358 return Diagnostic::Ignored;
359 Result = Diagnostic::Warning;
360 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000361 if (Result == Diagnostic::Error && ErrorsAsFatal)
362 Result = Diagnostic::Fatal;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000363 break;
Chris Lattnere007de32009-04-15 07:01:18 +0000364 case diag::MAP_ERROR:
365 Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000366 if (ErrorsAsFatal)
367 Result = Diagnostic::Fatal;
Chris Lattnere007de32009-04-15 07:01:18 +0000368 break;
369 case diag::MAP_FATAL:
370 Result = Diagnostic::Fatal;
371 break;
372 case diag::MAP_WARNING:
373 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner8c800702008-05-29 15:36:45 +0000374 if (IgnoreAllWarnings)
375 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000377 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000378
Chris Lattnerb8e73152009-04-16 05:04:32 +0000379 // If this is an extension diagnostic and we're in -pedantic-error mode, and
380 // if the user didn't explicitly map it, upgrade to an error.
381 if (ExtBehavior == Ext_Error &&
382 (MappingInfo & 8) == 0 &&
383 isBuiltinExtensionDiag(DiagID))
384 Result = Diagnostic::Error;
Mike Stump11289f42009-09-09 15:08:12 +0000385
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000386 if (WarningsAsErrors)
387 Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000388 if (Result == Diagnostic::Error && ErrorsAsFatal)
389 Result = Diagnostic::Fatal;
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000390 break;
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000392 case diag::MAP_WARNING_NO_WERROR:
393 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
394 // not be adjusted by -Werror or -pedantic-errors.
395 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000396
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000397 // If warnings are globally mapped to ignore or error, do it.
398 if (IgnoreAllWarnings)
399 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattnere007de32009-04-15 07:01:18 +0000401 break;
Chris Lattner801fda82009-12-22 23:12:53 +0000402
403 case diag::MAP_ERROR_NO_WFATAL:
404 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
405 // unaffected by -Wfatal-errors.
406 Result = Diagnostic::Error;
407 break;
Chris Lattner8c800702008-05-29 15:36:45 +0000408 }
Chris Lattnere007de32009-04-15 07:01:18 +0000409
410 // Okay, we're about to return this as a "diagnostic to emit" one last check:
411 // if this is any sort of extension warning, and if we're in an __extension__
412 // block, silence it.
413 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
414 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattnere007de32009-04-15 07:01:18 +0000416 return Result;
Chris Lattner22eb9722006-06-18 05:43:12 +0000417}
418
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000419struct WarningOption {
420 const char *Name;
421 const short *Members;
422 const char *SubGroups;
423};
424
425#define GET_DIAG_ARRAYS
426#include "clang/Basic/DiagnosticGroups.inc"
427#undef GET_DIAG_ARRAYS
428
429// Second the table of options, sorted by name for fast binary lookup.
430static const WarningOption OptionTable[] = {
431#define GET_DIAG_TABLE
432#include "clang/Basic/DiagnosticGroups.inc"
433#undef GET_DIAG_TABLE
434};
435static const size_t OptionTableSize =
436sizeof(OptionTable) / sizeof(OptionTable[0]);
437
438static bool WarningOptionCompare(const WarningOption &LHS,
439 const WarningOption &RHS) {
440 return strcmp(LHS.Name, RHS.Name) < 0;
441}
442
443static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
444 Diagnostic &Diags) {
445 // Option exists, poke all the members of its diagnostic set.
446 if (const short *Member = Group->Members) {
447 for (; *Member != -1; ++Member)
448 Diags.setDiagnosticMapping(*Member, Mapping);
449 }
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000451 // Enable/disable all subgroups along with this one.
452 if (const char *SubGroups = Group->SubGroups) {
453 for (; *SubGroups != (char)-1; ++SubGroups)
454 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
455 }
456}
457
458/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
459/// "unknown-pragmas" to have the specified mapping. This returns true and
460/// ignores the request if "Group" was unknown, false otherwise.
461bool Diagnostic::setDiagnosticGroupMapping(const char *Group,
462 diag::Mapping Map) {
Mike Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000464 WarningOption Key = { Group, 0, 0 };
465 const WarningOption *Found =
466 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
467 WarningOptionCompare);
468 if (Found == OptionTable + OptionTableSize ||
469 strcmp(Found->Name, Group) != 0)
470 return true; // Option not found.
Mike Stump11289f42009-09-09 15:08:12 +0000471
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000472 MapGroupMembers(Found, Map, *this);
473 return false;
474}
475
476
Chris Lattner8488c822008-11-18 07:04:44 +0000477/// ProcessDiag - This is the method used to report a diagnostic that is
478/// finally fully formed.
Douglas Gregor33834512009-06-14 07:33:30 +0000479bool Diagnostic::ProcessDiag() {
Chris Lattner427c9c12008-11-22 00:59:29 +0000480 DiagnosticInfo Info(this);
Mike Stump11289f42009-09-09 15:08:12 +0000481
Douglas Gregor2436e712009-09-17 21:32:03 +0000482 if (SuppressAllDiagnostics)
483 return false;
484
Chris Lattner22eb9722006-06-18 05:43:12 +0000485 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000486 Diagnostic::Level DiagLevel;
487 unsigned DiagID = Info.getID();
Mike Stump11289f42009-09-09 15:08:12 +0000488
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000489 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
490 // in a system header.
491 bool ShouldEmitInSystemHeader;
Mike Stump11289f42009-09-09 15:08:12 +0000492
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000493 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
494 // Handle custom diagnostics, which cannot be mapped.
495 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000496
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000497 // Custom diagnostics always are emitted in system headers.
498 ShouldEmitInSystemHeader = true;
499 } else {
500 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
501 // the diagnostic level was for the previous diagnostic so that it is
502 // filtered the same as the previous diagnostic.
503 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000504 if (DiagClass == CLASS_NOTE) {
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000505 DiagLevel = Diagnostic::Note;
506 ShouldEmitInSystemHeader = false; // extra consideration is needed
507 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000508 // If this is not an error and we are in a system header, we ignore it.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000509 // Check the original Diag ID here, because we also want to ignore
510 // extensions and warnings in -Werror and -pedantic-errors modes, which
511 // *map* warnings/extensions to errors.
Chris Lattnere6c831d2009-04-15 16:56:26 +0000512 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Mike Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000514 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
515 }
516 }
517
Douglas Gregor19367f52009-03-19 18:55:06 +0000518 if (DiagLevel != Diagnostic::Note) {
519 // Record that a fatal error occurred only when we see a second
520 // non-note diagnostic. This allows notes to be attached to the
521 // fatal error, but suppresses any diagnostics that follow those
522 // notes.
523 if (LastDiagLevel == Diagnostic::Fatal)
524 FatalErrorOccurred = true;
525
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000526 LastDiagLevel = DiagLevel;
Mike Stump11289f42009-09-09 15:08:12 +0000527 }
Douglas Gregor19367f52009-03-19 18:55:06 +0000528
529 // If a fatal error has already been emitted, silence all subsequent
530 // diagnostics.
531 if (FatalErrorOccurred)
Douglas Gregor33834512009-06-14 07:33:30 +0000532 return false;
Douglas Gregor19367f52009-03-19 18:55:06 +0000533
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000534 // If the client doesn't care about this message, don't issue it. If this is
535 // a note and the last real diagnostic was ignored, ignore it too.
536 if (DiagLevel == Diagnostic::Ignored ||
537 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Douglas Gregor33834512009-06-14 07:33:30 +0000538 return false;
Nico Weber4c311642008-08-10 19:59:06 +0000539
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000540 // If this diagnostic is in a system header and is not a clang error, suppress
541 // it.
542 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner8488c822008-11-18 07:04:44 +0000543 Info.getLocation().isValid() &&
John McCallbe089fa2010-02-11 10:04:29 +0000544 Info.getLocation().getInstantiationLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000545 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
546 LastDiagLevel = Diagnostic::Ignored;
Douglas Gregor33834512009-06-14 07:33:30 +0000547 return false;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000548 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000549
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000550 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000551 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000552 ++NumErrors;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000553 }
Mike Stump11289f42009-09-09 15:08:12 +0000554
Chris Lattner22eb9722006-06-18 05:43:12 +0000555 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000556 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekea06ec12009-01-23 20:28:53 +0000557 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000558
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000559 CurDiagID = ~0U;
Douglas Gregor33834512009-06-14 07:33:30 +0000560
561 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000562}
563
Douglas Gregor85795312010-03-22 15:10:57 +0000564bool DiagnosticBuilder::Emit() {
565 // If DiagObj is null, then its soul was stolen by the copy ctor
566 // or the user called Emit().
567 if (DiagObj == 0) return false;
568
569 // When emitting diagnostics, we set the final argument count into
570 // the Diagnostic object.
571 DiagObj->NumDiagArgs = NumArgs;
572 DiagObj->NumDiagRanges = NumRanges;
573 DiagObj->NumCodeModificationHints = NumCodeModificationHints;
574
575 // Process the diagnostic, sending the accumulated information to the
576 // DiagnosticClient.
577 bool Emitted = DiagObj->ProcessDiag();
578
579 // Clear out the current diagnostic object.
Douglas Gregor96380982010-03-22 15:47:45 +0000580 unsigned DiagID = DiagObj->CurDiagID;
Douglas Gregor85795312010-03-22 15:10:57 +0000581 DiagObj->Clear();
582
583 // If there was a delayed diagnostic, emit it now.
Douglas Gregor96380982010-03-22 15:47:45 +0000584 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
Douglas Gregor85795312010-03-22 15:10:57 +0000585 DiagObj->ReportDelayed();
586
587 // This diagnostic is dead.
588 DiagObj = 0;
589
590 return Emitted;
591}
592
Nico Weber4c311642008-08-10 19:59:06 +0000593
Chris Lattner22eb9722006-06-18 05:43:12 +0000594DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000595
Chris Lattner23be0672008-11-19 06:51:40 +0000596
Chris Lattner2b786902008-11-21 07:50:02 +0000597/// ModifierIs - Return true if the specified modifier matches specified string.
598template <std::size_t StrLen>
599static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
600 const char (&Str)[StrLen]) {
601 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
602}
603
John McCall8cb7a8a32010-01-14 20:11:39 +0000604/// ScanForward - Scans forward, looking for the given character, skipping
605/// nested clauses and escaped characters.
606static const char *ScanFormat(const char *I, const char *E, char Target) {
607 unsigned Depth = 0;
608
609 for ( ; I != E; ++I) {
610 if (Depth == 0 && *I == Target) return I;
611 if (Depth != 0 && *I == '}') Depth--;
612
613 if (*I == '%') {
614 I++;
615 if (I == E) break;
616
617 // Escaped characters get implicitly skipped here.
618
619 // Format specifier.
620 if (!isdigit(*I) && !ispunct(*I)) {
621 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
622 if (I == E) break;
623 if (*I == '{')
624 Depth++;
625 }
626 }
627 }
628 return E;
629}
630
Chris Lattner2b786902008-11-21 07:50:02 +0000631/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
632/// like this: %select{foo|bar|baz}2. This means that the integer argument
633/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
634/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
635/// This is very useful for certain classes of variant diagnostics.
John McCalle4d54322010-01-13 23:58:20 +0000636static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000637 const char *Argument, unsigned ArgumentLen,
638 llvm::SmallVectorImpl<char> &OutStr) {
639 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000640
Chris Lattner2b786902008-11-21 07:50:02 +0000641 // Skip over 'ValNo' |'s.
642 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000643 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000644 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
645 " larger than the number of options in the diagnostic string!");
646 Argument = NextVal+1; // Skip this string.
647 --ValNo;
648 }
Mike Stump11289f42009-09-09 15:08:12 +0000649
Chris Lattner2b786902008-11-21 07:50:02 +0000650 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000651 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000652
653 // Recursively format the result of the select clause into the output string.
654 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000655}
656
657/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
658/// letter 's' to the string if the value is not 1. This is used in cases like
659/// this: "you idiot, you have %4 parameter%s4!".
660static void HandleIntegerSModifier(unsigned ValNo,
661 llvm::SmallVectorImpl<char> &OutStr) {
662 if (ValNo != 1)
663 OutStr.push_back('s');
664}
665
John McCall9015cde2010-01-14 00:50:32 +0000666/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
667/// prints the ordinal form of the given integer, with 1 corresponding
668/// to the first ordinal. Currently this is hard-coded to use the
669/// English form.
670static void HandleOrdinalModifier(unsigned ValNo,
671 llvm::SmallVectorImpl<char> &OutStr) {
672 assert(ValNo != 0 && "ValNo must be strictly positive!");
673
674 llvm::raw_svector_ostream Out(OutStr);
675
676 // We could use text forms for the first N ordinals, but the numeric
677 // forms are actually nicer in diagnostics because they stand out.
678 Out << ValNo;
679
680 // It is critically important that we do this perfectly for
681 // user-written sequences with over 100 elements.
682 switch (ValNo % 100) {
683 case 11:
684 case 12:
685 case 13:
686 Out << "th"; return;
687 default:
688 switch (ValNo % 10) {
689 case 1: Out << "st"; return;
690 case 2: Out << "nd"; return;
691 case 3: Out << "rd"; return;
692 default: Out << "th"; return;
693 }
694 }
695}
696
Chris Lattner2b786902008-11-21 07:50:02 +0000697
Sebastian Redl15b02d22008-11-22 13:44:36 +0000698/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000699static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000700 // Programming 101: Parse a decimal number :-)
701 unsigned Val = 0;
702 while (Start != End && *Start >= '0' && *Start <= '9') {
703 Val *= 10;
704 Val += *Start - '0';
705 ++Start;
706 }
707 return Val;
708}
709
710/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000711static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000712 if (*Start != '[') {
713 unsigned Ref = PluralNumber(Start, End);
714 return Ref == Val;
715 }
716
717 ++Start;
718 unsigned Low = PluralNumber(Start, End);
719 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
720 ++Start;
721 unsigned High = PluralNumber(Start, End);
722 assert(*Start == ']' && "Bad plural expression syntax: expected )");
723 ++Start;
724 return Low <= Val && Val <= High;
725}
726
727/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000728static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000729 // Empty condition?
730 if (*Start == ':')
731 return true;
732
733 while (1) {
734 char C = *Start;
735 if (C == '%') {
736 // Modulo expression
737 ++Start;
738 unsigned Arg = PluralNumber(Start, End);
739 assert(*Start == '=' && "Bad plural expression syntax: expected =");
740 ++Start;
741 unsigned ValMod = ValNo % Arg;
742 if (TestPluralRange(ValMod, Start, End))
743 return true;
744 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000745 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000746 "Bad plural expression syntax: unexpected character");
747 // Range expression
748 if (TestPluralRange(ValNo, Start, End))
749 return true;
750 }
751
752 // Scan for next or-expr part.
753 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000754 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000755 break;
756 ++Start;
757 }
758 return false;
759}
760
761/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
762/// for complex plural forms, or in languages where all plurals are complex.
763/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
764/// conditions that are tested in order, the form corresponding to the first
765/// that applies being emitted. The empty condition is always true, making the
766/// last form a default case.
767/// Conditions are simple boolean expressions, where n is the number argument.
768/// Here are the rules.
769/// condition := expression | empty
770/// empty := -> always true
771/// expression := numeric [',' expression] -> logical or
772/// numeric := range -> true if n in range
773/// | '%' number '=' range -> true if n % number in range
774/// range := number
775/// | '[' number ',' number ']' -> ranges are inclusive both ends
776///
777/// Here are some examples from the GNU gettext manual written in this form:
778/// English:
779/// {1:form0|:form1}
780/// Latvian:
781/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
782/// Gaeilge:
783/// {1:form0|2:form1|:form2}
784/// Romanian:
785/// {1:form0|0,%100=[1,19]:form1|:form2}
786/// Lithuanian:
787/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
788/// Russian (requires repeated form):
789/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
790/// Slovak
791/// {1:form0|[2,4]:form1|:form2}
792/// Polish (requires repeated form):
793/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
794static void HandlePluralModifier(unsigned ValNo,
795 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb8e73152009-04-16 05:04:32 +0000796 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000797 const char *ArgumentEnd = Argument + ArgumentLen;
798 while (1) {
799 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
800 const char *ExprEnd = Argument;
801 while (*ExprEnd != ':') {
802 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
803 ++ExprEnd;
804 }
805 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
806 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000807 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
Sebastian Redl15b02d22008-11-22 13:44:36 +0000808 OutStr.append(Argument, ExprEnd);
809 return;
810 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000811 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000812 }
813}
814
815
Chris Lattner23be0672008-11-19 06:51:40 +0000816/// FormatDiagnostic - Format this diagnostic into a string, substituting the
817/// formal arguments into the %0 slots. The result is appended onto the Str
818/// array.
819void DiagnosticInfo::
820FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
821 const char *DiagStr = getDiags()->getDescription(getID());
822 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump11289f42009-09-09 15:08:12 +0000823
John McCalle4d54322010-01-13 23:58:20 +0000824 FormatDiagnostic(DiagStr, DiagEnd, OutStr);
825}
826
827void DiagnosticInfo::
828FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
829 llvm::SmallVectorImpl<char> &OutStr) const {
830
Chris Lattnerc243f292009-10-20 05:25:22 +0000831 /// FormattedArgs - Keep track of all of the arguments formatted by
832 /// ConvertArgToString and pass them into subsequent calls to
833 /// ConvertArgToString, allowing the implementation to avoid redundancies in
834 /// obvious cases.
835 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
836
Chris Lattner23be0672008-11-19 06:51:40 +0000837 while (DiagStr != DiagEnd) {
838 if (DiagStr[0] != '%') {
839 // Append non-%0 substrings to Str if we have one.
840 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
841 OutStr.append(DiagStr, StrEnd);
842 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000843 continue;
John McCall8cb7a8a32010-01-14 20:11:39 +0000844 } else if (ispunct(DiagStr[1])) {
845 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000846 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000847 continue;
848 }
Mike Stump11289f42009-09-09 15:08:12 +0000849
Chris Lattner2b786902008-11-21 07:50:02 +0000850 // Skip the %.
851 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattner2b786902008-11-21 07:50:02 +0000853 // This must be a placeholder for a diagnostic argument. The format for a
854 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
855 // The digit is a number from 0-9 indicating which argument this comes from.
856 // The modifier is a string of digits from the set [-a-z]+, arguments is a
857 // brace enclosed string.
858 const char *Modifier = 0, *Argument = 0;
859 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattner2b786902008-11-21 07:50:02 +0000861 // Check to see if we have a modifier. If so eat it.
862 if (!isdigit(DiagStr[0])) {
863 Modifier = DiagStr;
864 while (DiagStr[0] == '-' ||
865 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
866 ++DiagStr;
867 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000868
Chris Lattner2b786902008-11-21 07:50:02 +0000869 // If we have an argument, get it next.
870 if (DiagStr[0] == '{') {
871 ++DiagStr; // Skip {.
872 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000873
John McCall8cb7a8a32010-01-14 20:11:39 +0000874 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
875 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000876 ArgumentLen = DiagStr-Argument;
877 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000878 }
Chris Lattner2b786902008-11-21 07:50:02 +0000879 }
Mike Stump11289f42009-09-09 15:08:12 +0000880
Chris Lattner2b786902008-11-21 07:50:02 +0000881 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000882 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000883
Chris Lattnerc243f292009-10-20 05:25:22 +0000884 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
885
886 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000887 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000888 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000889 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000890 assert(ModifierLen == 0 && "No modifiers for strings yet");
891 OutStr.append(S.begin(), S.end());
892 break;
893 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000894 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000895 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000896 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000897
898 // Don't crash if get passed a null pointer by accident.
899 if (!S)
900 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000901
Chris Lattner2b786902008-11-21 07:50:02 +0000902 OutStr.append(S, S + strlen(S));
903 break;
904 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000905 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000906 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000907 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000908
Chris Lattner2b786902008-11-21 07:50:02 +0000909 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000910 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000911 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
912 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000913 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
914 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000915 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
916 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000917 } else {
918 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000919 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000920 }
Chris Lattner2b786902008-11-21 07:50:02 +0000921 break;
922 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000923 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000924 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000925
Chris Lattner2b786902008-11-21 07:50:02 +0000926 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000927 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000928 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
929 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000930 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
931 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000932 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
933 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000934 } else {
935 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000936 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000937 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000938 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000939 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000940 // ---- NAMES and TYPES ----
941 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000942 const IdentifierInfo *II = getArgIdentifier(ArgNo);
943 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000944
945 // Don't crash if get passed a null pointer by accident.
946 if (!II) {
947 const char *S = "(null)";
948 OutStr.append(S, S + strlen(S));
949 continue;
950 }
951
Daniel Dunbar07d07852009-10-18 21:17:35 +0000952 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000953 break;
954 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000955 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000956 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000957 case Diagnostic::ak_nameddecl:
Douglas Gregor053f6912009-08-26 00:04:55 +0000958 case Diagnostic::ak_nestednamespec:
Douglas Gregore40876a2009-10-13 21:16:44 +0000959 case Diagnostic::ak_declcontext:
Chris Lattnerc243f292009-10-20 05:25:22 +0000960 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000961 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000962 Argument, ArgumentLen,
963 FormattedArgs.data(), FormattedArgs.size(),
964 OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000965 break;
Nico Weber4c311642008-08-10 19:59:06 +0000966 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000967
968 // Remember this argument info for subsequent formatting operations. Turn
969 // std::strings into a null terminated string to make it be the same case as
970 // all the other ones.
971 if (Kind != Diagnostic::ak_std_string)
972 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
973 else
974 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
975 (intptr_t)getArgStdStr(ArgNo).c_str()));
976
Nico Weber4c311642008-08-10 19:59:06 +0000977 }
Nico Weber4c311642008-08-10 19:59:06 +0000978}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000979
Douglas Gregor33cdd812010-02-18 18:08:43 +0000980StoredDiagnostic::StoredDiagnostic() { }
981
982StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
983 llvm::StringRef Message)
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000984 : Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000985
986StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
987 const DiagnosticInfo &Info)
988 : Level(Level), Loc(Info.getLocation())
989{
990 llvm::SmallString<64> Message;
991 Info.FormatDiagnostic(Message);
992 this->Message.assign(Message.begin(), Message.end());
993
994 Ranges.reserve(Info.getNumRanges());
995 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
996 Ranges.push_back(Info.getRange(I));
997
998 FixIts.reserve(Info.getNumCodeModificationHints());
999 for (unsigned I = 0, N = Info.getNumCodeModificationHints(); I != N; ++I)
1000 FixIts.push_back(Info.getCodeModificationHint(I));
1001}
1002
1003StoredDiagnostic::~StoredDiagnostic() { }
1004
Douglas Gregorac0605e2010-01-28 06:00:51 +00001005static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
1006 OS.write((const char *)&Value, sizeof(unsigned));
1007}
1008
1009static void WriteString(llvm::raw_ostream &OS, llvm::StringRef String) {
1010 WriteUnsigned(OS, String.size());
1011 OS.write(String.data(), String.size());
1012}
1013
1014static void WriteSourceLocation(llvm::raw_ostream &OS,
1015 SourceManager *SM,
1016 SourceLocation Location) {
1017 if (!SM || Location.isInvalid()) {
1018 // If we don't have a source manager or this location is invalid,
1019 // just write an invalid location.
1020 WriteUnsigned(OS, 0);
1021 WriteUnsigned(OS, 0);
1022 WriteUnsigned(OS, 0);
1023 return;
1024 }
1025
1026 Location = SM->getInstantiationLoc(Location);
1027 std::pair<FileID, unsigned> Decomposed = SM->getDecomposedLoc(Location);
1028
1029 WriteString(OS, SM->getFileEntryForID(Decomposed.first)->getName());
1030 WriteUnsigned(OS, SM->getLineNumber(Decomposed.first, Decomposed.second));
1031 WriteUnsigned(OS, SM->getColumnNumber(Decomposed.first, Decomposed.second));
1032}
1033
Douglas Gregor33cdd812010-02-18 18:08:43 +00001034void StoredDiagnostic::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001035 SourceManager *SM = 0;
1036 if (getLocation().isValid())
1037 SM = &const_cast<SourceManager &>(getLocation().getManager());
1038
Douglas Gregor70127c12010-02-19 00:40:40 +00001039 // Write a short header to help identify diagnostics.
1040 OS << (char)0x06 << (char)0x07;
1041
Douglas Gregorac0605e2010-01-28 06:00:51 +00001042 // Write the diagnostic level and location.
Douglas Gregor33cdd812010-02-18 18:08:43 +00001043 WriteUnsigned(OS, (unsigned)Level);
Douglas Gregorac0605e2010-01-28 06:00:51 +00001044 WriteSourceLocation(OS, SM, getLocation());
1045
1046 // Write the diagnostic message.
1047 llvm::SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001048 WriteString(OS, getMessage());
Douglas Gregorac0605e2010-01-28 06:00:51 +00001049
1050 // Count the number of ranges that don't point into macros, since
1051 // only simple file ranges serialize well.
1052 unsigned NumNonMacroRanges = 0;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001053 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1054 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregorac0605e2010-01-28 06:00:51 +00001055 continue;
1056
1057 ++NumNonMacroRanges;
1058 }
1059
1060 // Write the ranges.
1061 WriteUnsigned(OS, NumNonMacroRanges);
1062 if (NumNonMacroRanges) {
Douglas Gregor33cdd812010-02-18 18:08:43 +00001063 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1064 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregorac0605e2010-01-28 06:00:51 +00001065 continue;
1066
Douglas Gregor33cdd812010-02-18 18:08:43 +00001067 WriteSourceLocation(OS, SM, R->getBegin());
1068 WriteSourceLocation(OS, SM, R->getEnd());
Douglas Gregorac0605e2010-01-28 06:00:51 +00001069 }
1070 }
1071
1072 // Determine if all of the fix-its involve rewrites with simple file
1073 // locations (not in macro instantiations). If so, we can write
1074 // fix-it information.
Douglas Gregor33cdd812010-02-18 18:08:43 +00001075 unsigned NumFixIts = 0;
1076 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1077 if (F->RemoveRange.isValid() &&
1078 (F->RemoveRange.getBegin().isMacroID() ||
1079 F->RemoveRange.getEnd().isMacroID())) {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001080 NumFixIts = 0;
1081 break;
1082 }
1083
Douglas Gregor33cdd812010-02-18 18:08:43 +00001084 if (F->InsertionLoc.isValid() && F->InsertionLoc.isMacroID()) {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001085 NumFixIts = 0;
1086 break;
1087 }
Douglas Gregor33cdd812010-02-18 18:08:43 +00001088
1089 ++NumFixIts;
Douglas Gregorac0605e2010-01-28 06:00:51 +00001090 }
1091
1092 // Write the fix-its.
1093 WriteUnsigned(OS, NumFixIts);
Douglas Gregor33cdd812010-02-18 18:08:43 +00001094 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1095 WriteSourceLocation(OS, SM, F->RemoveRange.getBegin());
1096 WriteSourceLocation(OS, SM, F->RemoveRange.getEnd());
1097 WriteSourceLocation(OS, SM, F->InsertionLoc);
1098 WriteString(OS, F->CodeToInsert);
Douglas Gregorac0605e2010-01-28 06:00:51 +00001099 }
1100}
1101
Douglas Gregor33cdd812010-02-18 18:08:43 +00001102static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
1103 unsigned &Value) {
1104 if (Memory + sizeof(unsigned) > MemoryEnd)
1105 return true;
1106
1107 memmove(&Value, Memory, sizeof(unsigned));
1108 Memory += sizeof(unsigned);
1109 return false;
1110}
1111
1112static bool ReadSourceLocation(FileManager &FM, SourceManager &SM,
1113 const char *&Memory, const char *MemoryEnd,
1114 SourceLocation &Location) {
1115 // Read the filename.
1116 unsigned FileNameLen = 0;
1117 if (ReadUnsigned(Memory, MemoryEnd, FileNameLen) ||
1118 Memory + FileNameLen > MemoryEnd)
1119 return true;
1120
1121 llvm::StringRef FileName(Memory, FileNameLen);
1122 Memory += FileNameLen;
1123
1124 // Read the line, column.
1125 unsigned Line = 0, Column = 0;
1126 if (ReadUnsigned(Memory, MemoryEnd, Line) ||
1127 ReadUnsigned(Memory, MemoryEnd, Column))
1128 return true;
1129
1130 if (FileName.empty()) {
1131 Location = SourceLocation();
1132 return false;
1133 }
1134
1135 const FileEntry *File = FM.getFile(FileName);
1136 if (!File)
1137 return true;
1138
1139 // Make sure that this file has an entry in the source manager.
1140 if (!SM.hasFileInfo(File))
1141 SM.createFileID(File, SourceLocation(), SrcMgr::C_User);
1142
1143 Location = SM.getLocation(File, Line, Column);
1144 return false;
1145}
1146
1147StoredDiagnostic
1148StoredDiagnostic::Deserialize(FileManager &FM, SourceManager &SM,
1149 const char *&Memory, const char *MemoryEnd) {
Douglas Gregor70127c12010-02-19 00:40:40 +00001150 while (true) {
1151 if (Memory == MemoryEnd)
1152 return StoredDiagnostic();
1153
1154 if (*Memory != 0x06) {
1155 ++Memory;
1156 continue;
1157 }
1158
1159 ++Memory;
1160 if (Memory == MemoryEnd)
1161 return StoredDiagnostic();
1162
1163 if (*Memory != 0x07) {
1164 ++Memory;
1165 continue;
1166 }
1167
1168 // We found the header. We're done.
1169 ++Memory;
1170 break;
1171 }
1172
Douglas Gregor33cdd812010-02-18 18:08:43 +00001173 // Read the severity level.
1174 unsigned Level = 0;
1175 if (ReadUnsigned(Memory, MemoryEnd, Level) || Level > Diagnostic::Fatal)
1176 return StoredDiagnostic();
1177
1178 // Read the source location.
1179 SourceLocation Location;
1180 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Location))
1181 return StoredDiagnostic();
1182
1183 // Read the diagnostic text.
1184 if (Memory == MemoryEnd)
1185 return StoredDiagnostic();
1186
1187 unsigned MessageLen = 0;
1188 if (ReadUnsigned(Memory, MemoryEnd, MessageLen) ||
1189 Memory + MessageLen > MemoryEnd)
1190 return StoredDiagnostic();
1191
1192 llvm::StringRef Message(Memory, MessageLen);
1193 Memory += MessageLen;
1194
1195
1196 // At this point, we have enough information to form a diagnostic. Do so.
1197 StoredDiagnostic Diag;
1198 Diag.Level = (Diagnostic::Level)Level;
1199 Diag.Loc = FullSourceLoc(Location, SM);
1200 Diag.Message = Message;
1201 if (Memory == MemoryEnd)
1202 return Diag;
1203
1204 // Read the source ranges.
1205 unsigned NumSourceRanges = 0;
1206 if (ReadUnsigned(Memory, MemoryEnd, NumSourceRanges))
1207 return Diag;
1208 for (unsigned I = 0; I != NumSourceRanges; ++I) {
1209 SourceLocation Begin, End;
1210 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Begin) ||
1211 ReadSourceLocation(FM, SM, Memory, MemoryEnd, End))
1212 return Diag;
1213
1214 Diag.Ranges.push_back(SourceRange(Begin, End));
1215 }
1216
1217 // Read the fix-it hints.
1218 unsigned NumFixIts = 0;
1219 if (ReadUnsigned(Memory, MemoryEnd, NumFixIts))
1220 return Diag;
1221 for (unsigned I = 0; I != NumFixIts; ++I) {
1222 SourceLocation RemoveBegin, RemoveEnd, InsertionLoc;
1223 unsigned InsertLen = 0;
1224 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveBegin) ||
1225 ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveEnd) ||
1226 ReadSourceLocation(FM, SM, Memory, MemoryEnd, InsertionLoc) ||
1227 ReadUnsigned(Memory, MemoryEnd, InsertLen) ||
1228 Memory + InsertLen > MemoryEnd) {
1229 Diag.FixIts.clear();
1230 return Diag;
1231 }
1232
1233 CodeModificationHint Hint;
1234 Hint.RemoveRange = SourceRange(RemoveBegin, RemoveEnd);
1235 Hint.InsertionLoc = InsertionLoc;
1236 Hint.CodeToInsert.assign(Memory, Memory + InsertLen);
1237 Memory += InsertLen;
1238 Diag.FixIts.push_back(Hint);
1239 }
1240
1241 return Diag;
1242}
1243
Ted Kremenekea06ec12009-01-23 20:28:53 +00001244/// IncludeInDiagnosticCounts - This method (whose default implementation
1245/// returns true) indicates whether the diagnostics handled by this
1246/// DiagnosticClient should be included in the number of diagnostics
1247/// reported by Diagnostic.
1248bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }