blob: 755fbed66f3bea4639d83d5c5a62e9840ceb636a [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
Chris Lattnere007de32009-04-15 07:01:18 +000014#include "clang/AST/ASTDiagnostic.h"
Chris Lattnere007de32009-04-15 07:01:18 +000015#include "clang/Analysis/AnalysisDiagnostic.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000016#include "clang/Basic/Diagnostic.h"
Douglas Gregorac0605e2010-01-28 06:00:51 +000017#include "clang/Basic/FileManager.h"
Chris Lattnerb91fd172008-11-19 07:32:16 +000018#include "clang/Basic/IdentifierTable.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000019#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000020#include "clang/Basic/SourceLocation.h"
Douglas Gregorac0605e2010-01-28 06:00:51 +000021#include "clang/Basic/SourceManager.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000022#include "clang/Driver/DriverDiagnostic.h"
23#include "clang/Frontend/FrontendDiagnostic.h"
24#include "clang/Lex/LexDiagnostic.h"
25#include "clang/Parse/ParseDiagnostic.h"
26#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner23be0672008-11-19 06:51:40 +000027#include "llvm/ADT/SmallVector.h"
Chris Lattner91aea712008-11-19 07:22:31 +000028#include "llvm/ADT/StringExtras.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000029#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbare3633792009-10-17 18:12:14 +000030#include "llvm/Support/raw_ostream.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000031
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;
Douglas Gregor2d2d9072010-04-14 22:19:45 +0000230 NumErrorsSuppressed = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000231 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000232 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000233 LastDiagLevel = Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattner63ecc502008-11-23 09:21:17 +0000235 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000236 ArgToStringCookie = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000237
Douglas Gregor85795312010-03-22 15:10:57 +0000238 DelayedDiagID = 0;
239
Chris Lattner411c0ff2009-04-16 04:12:40 +0000240 // Set all mappings to 'unset'.
Chris Lattnerfb42a182009-07-12 21:18:45 +0000241 DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0);
242 DiagMappingsStack.push_back(BlankDiags);
Chris Lattnerae411572006-07-05 00:55:08 +0000243}
244
Chris Lattnere6535cf2007-12-02 01:09:57 +0000245Diagnostic::~Diagnostic() {
246 delete CustomDiagInfo;
247}
248
Chris Lattnerfb42a182009-07-12 21:18:45 +0000249
250void Diagnostic::pushMappings() {
John Thompsond73d7ad2009-10-23 02:21:17 +0000251 // Avoids undefined behavior when the stack has to resize.
252 DiagMappingsStack.reserve(DiagMappingsStack.size() + 1);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000253 DiagMappingsStack.push_back(DiagMappingsStack.back());
254}
255
256bool Diagnostic::popMappings() {
257 if (DiagMappingsStack.size() == 1)
258 return false;
259
260 DiagMappingsStack.pop_back();
261 return true;
262}
263
Chris Lattnere6535cf2007-12-02 01:09:57 +0000264/// getCustomDiagID - Return an ID for a diagnostic with the specified message
265/// and level. If this is the first request for this diagnosic, it is
266/// registered and created, otherwise the existing ID is returned.
Daniel Dunbar4886c812009-12-01 17:42:06 +0000267unsigned Diagnostic::getCustomDiagID(Level L, llvm::StringRef Message) {
Mike Stump11289f42009-09-09 15:08:12 +0000268 if (CustomDiagInfo == 0)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000269 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000270 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000271}
272
273
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000274/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
275/// level of the specified diagnostic ID is a Warning or Extension.
276/// This only works on builtin diagnostics, not custom ones, and is not legal to
277/// call on NOTEs.
278bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000279 return DiagID < diag::DIAG_UPPER_LIMIT &&
280 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000281}
282
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000283/// \brief Determine whether the given built-in diagnostic ID is a
284/// Note.
285bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000286 return DiagID < diag::DIAG_UPPER_LIMIT &&
287 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000288}
289
Chris Lattnere007de32009-04-15 07:01:18 +0000290/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
Chris Lattner97a8e432010-04-12 21:53:11 +0000291/// ID is for an extension of some sort. This also returns EnabledByDefault,
292/// which is set to indicate whether the diagnostic is ignored by default (in
293/// which case -pedantic enables it) or treated as a warning/error by default.
Chris Lattnere007de32009-04-15 07:01:18 +0000294///
Chris Lattner97a8e432010-04-12 21:53:11 +0000295bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID,
296 bool &EnabledByDefault) {
297 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
298 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
299 return false;
300
301 EnabledByDefault = StaticDiagInfo[DiagID].Mapping != diag::MAP_IGNORE;
302 return true;
Chris Lattnere007de32009-04-15 07:01:18 +0000303}
304
Chris Lattner22eb9722006-06-18 05:43:12 +0000305
306/// getDescription - Given a diagnostic ID, return a description of the
307/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000308const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner6c440322009-04-16 06:07:15 +0000309 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
310 return Info->Description;
Chris Lattner7368d582009-01-27 18:30:58 +0000311 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000312}
313
Douglas Gregor85795312010-03-22 15:10:57 +0000314void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
315 llvm::StringRef Arg2) {
316 if (DelayedDiagID)
317 return;
318
319 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000320 DelayedDiagArg1 = Arg1.str();
321 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000322}
323
324void Diagnostic::ReportDelayed() {
325 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
326 DelayedDiagID = 0;
327 DelayedDiagArg1.clear();
328 DelayedDiagArg2.clear();
329}
330
Chris Lattner22eb9722006-06-18 05:43:12 +0000331/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
332/// object, classify the specified diagnostic ID into a Level, consumable by
333/// the DiagnosticClient.
334Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000335 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000336 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000337 return CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000338
Chris Lattner4431a1b2007-11-30 22:53:43 +0000339 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000340 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000341 return getDiagnosticLevel(DiagID, DiagClass);
342}
343
344/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
345/// object, classify the specified diagnostic ID into a Level, consumable by
346/// the DiagnosticClient.
347Diagnostic::Level
348Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattnerae411572006-07-05 00:55:08 +0000349 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000350 // to error. Errors can only be mapped to fatal.
Chris Lattnere007de32009-04-15 07:01:18 +0000351 Diagnostic::Level Result = Diagnostic::Fatal;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner411c0ff2009-04-16 04:12:40 +0000353 // Get the mapping information, if unset, compute it lazily.
354 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
355 if (MappingInfo == 0) {
356 MappingInfo = GetDefaultDiagMapping(DiagID);
357 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
358 }
Mike Stump11289f42009-09-09 15:08:12 +0000359
Chris Lattner411c0ff2009-04-16 04:12:40 +0000360 switch (MappingInfo & 7) {
361 default: assert(0 && "Unknown mapping!");
Chris Lattnere007de32009-04-15 07:01:18 +0000362 case diag::MAP_IGNORE:
Chris Lattnerb8e73152009-04-16 05:04:32 +0000363 // Ignore this, unless this is an extension diagnostic and we're mapping
364 // them onto warnings or errors.
365 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
366 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
367 (MappingInfo & 8) != 0) // User explicitly mapped it.
368 return Diagnostic::Ignored;
369 Result = Diagnostic::Warning;
370 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000371 if (Result == Diagnostic::Error && ErrorsAsFatal)
372 Result = Diagnostic::Fatal;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000373 break;
Chris Lattnere007de32009-04-15 07:01:18 +0000374 case diag::MAP_ERROR:
375 Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000376 if (ErrorsAsFatal)
377 Result = Diagnostic::Fatal;
Chris Lattnere007de32009-04-15 07:01:18 +0000378 break;
379 case diag::MAP_FATAL:
380 Result = Diagnostic::Fatal;
381 break;
382 case diag::MAP_WARNING:
383 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner8c800702008-05-29 15:36:45 +0000384 if (IgnoreAllWarnings)
385 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000386
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000387 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000388
Chris Lattnerb8e73152009-04-16 05:04:32 +0000389 // If this is an extension diagnostic and we're in -pedantic-error mode, and
390 // if the user didn't explicitly map it, upgrade to an error.
391 if (ExtBehavior == Ext_Error &&
392 (MappingInfo & 8) == 0 &&
393 isBuiltinExtensionDiag(DiagID))
394 Result = Diagnostic::Error;
Mike Stump11289f42009-09-09 15:08:12 +0000395
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000396 if (WarningsAsErrors)
397 Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000398 if (Result == Diagnostic::Error && ErrorsAsFatal)
399 Result = Diagnostic::Fatal;
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000400 break;
Mike Stump11289f42009-09-09 15:08:12 +0000401
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000402 case diag::MAP_WARNING_NO_WERROR:
403 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
404 // not be adjusted by -Werror or -pedantic-errors.
405 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000406
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000407 // If warnings are globally mapped to ignore or error, do it.
408 if (IgnoreAllWarnings)
409 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000410
Chris Lattnere007de32009-04-15 07:01:18 +0000411 break;
Chris Lattner801fda82009-12-22 23:12:53 +0000412
413 case diag::MAP_ERROR_NO_WFATAL:
414 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
415 // unaffected by -Wfatal-errors.
416 Result = Diagnostic::Error;
417 break;
Chris Lattner8c800702008-05-29 15:36:45 +0000418 }
Chris Lattnere007de32009-04-15 07:01:18 +0000419
420 // Okay, we're about to return this as a "diagnostic to emit" one last check:
421 // if this is any sort of extension warning, and if we're in an __extension__
422 // block, silence it.
423 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
424 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattnere007de32009-04-15 07:01:18 +0000426 return Result;
Chris Lattner22eb9722006-06-18 05:43:12 +0000427}
428
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000429struct WarningOption {
430 const char *Name;
431 const short *Members;
432 const char *SubGroups;
433};
434
435#define GET_DIAG_ARRAYS
436#include "clang/Basic/DiagnosticGroups.inc"
437#undef GET_DIAG_ARRAYS
438
439// Second the table of options, sorted by name for fast binary lookup.
440static const WarningOption OptionTable[] = {
441#define GET_DIAG_TABLE
442#include "clang/Basic/DiagnosticGroups.inc"
443#undef GET_DIAG_TABLE
444};
445static const size_t OptionTableSize =
446sizeof(OptionTable) / sizeof(OptionTable[0]);
447
448static bool WarningOptionCompare(const WarningOption &LHS,
449 const WarningOption &RHS) {
450 return strcmp(LHS.Name, RHS.Name) < 0;
451}
452
453static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
454 Diagnostic &Diags) {
455 // Option exists, poke all the members of its diagnostic set.
456 if (const short *Member = Group->Members) {
457 for (; *Member != -1; ++Member)
458 Diags.setDiagnosticMapping(*Member, Mapping);
459 }
Mike Stump11289f42009-09-09 15:08:12 +0000460
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000461 // Enable/disable all subgroups along with this one.
462 if (const char *SubGroups = Group->SubGroups) {
463 for (; *SubGroups != (char)-1; ++SubGroups)
464 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
465 }
466}
467
468/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
469/// "unknown-pragmas" to have the specified mapping. This returns true and
470/// ignores the request if "Group" was unknown, false otherwise.
471bool Diagnostic::setDiagnosticGroupMapping(const char *Group,
472 diag::Mapping Map) {
Mike Stump11289f42009-09-09 15:08:12 +0000473
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000474 WarningOption Key = { Group, 0, 0 };
475 const WarningOption *Found =
476 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
477 WarningOptionCompare);
478 if (Found == OptionTable + OptionTableSize ||
479 strcmp(Found->Name, Group) != 0)
480 return true; // Option not found.
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000482 MapGroupMembers(Found, Map, *this);
483 return false;
484}
485
486
Chris Lattner8488c822008-11-18 07:04:44 +0000487/// ProcessDiag - This is the method used to report a diagnostic that is
488/// finally fully formed.
Douglas Gregor33834512009-06-14 07:33:30 +0000489bool Diagnostic::ProcessDiag() {
Chris Lattner427c9c12008-11-22 00:59:29 +0000490 DiagnosticInfo Info(this);
Mike Stump11289f42009-09-09 15:08:12 +0000491
Douglas Gregor2436e712009-09-17 21:32:03 +0000492 if (SuppressAllDiagnostics)
493 return false;
494
Chris Lattner22eb9722006-06-18 05:43:12 +0000495 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000496 Diagnostic::Level DiagLevel;
497 unsigned DiagID = Info.getID();
Mike Stump11289f42009-09-09 15:08:12 +0000498
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000499 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
500 // in a system header.
501 bool ShouldEmitInSystemHeader;
Mike Stump11289f42009-09-09 15:08:12 +0000502
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000503 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
504 // Handle custom diagnostics, which cannot be mapped.
505 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000506
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000507 // Custom diagnostics always are emitted in system headers.
508 ShouldEmitInSystemHeader = true;
509 } else {
510 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
511 // the diagnostic level was for the previous diagnostic so that it is
512 // filtered the same as the previous diagnostic.
513 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000514 if (DiagClass == CLASS_NOTE) {
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000515 DiagLevel = Diagnostic::Note;
516 ShouldEmitInSystemHeader = false; // extra consideration is needed
517 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000518 // If this is not an error and we are in a system header, we ignore it.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000519 // Check the original Diag ID here, because we also want to ignore
520 // extensions and warnings in -Werror and -pedantic-errors modes, which
521 // *map* warnings/extensions to errors.
Chris Lattnere6c831d2009-04-15 16:56:26 +0000522 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Mike Stump11289f42009-09-09 15:08:12 +0000523
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000524 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
525 }
526 }
527
Douglas Gregor19367f52009-03-19 18:55:06 +0000528 if (DiagLevel != Diagnostic::Note) {
529 // Record that a fatal error occurred only when we see a second
530 // non-note diagnostic. This allows notes to be attached to the
531 // fatal error, but suppresses any diagnostics that follow those
532 // notes.
533 if (LastDiagLevel == Diagnostic::Fatal)
534 FatalErrorOccurred = true;
535
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000536 LastDiagLevel = DiagLevel;
Mike Stump11289f42009-09-09 15:08:12 +0000537 }
Douglas Gregor19367f52009-03-19 18:55:06 +0000538
539 // If a fatal error has already been emitted, silence all subsequent
540 // diagnostics.
Douglas Gregor2d2d9072010-04-14 22:19:45 +0000541 if (FatalErrorOccurred) {
542 if (DiagLevel >= Diagnostic::Error) {
543 ++NumErrors;
544 ++NumErrorsSuppressed;
545 }
546
Douglas Gregor33834512009-06-14 07:33:30 +0000547 return false;
Douglas Gregor2d2d9072010-04-14 22:19:45 +0000548 }
Douglas Gregor19367f52009-03-19 18:55:06 +0000549
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000550 // If the client doesn't care about this message, don't issue it. If this is
551 // a note and the last real diagnostic was ignored, ignore it too.
552 if (DiagLevel == Diagnostic::Ignored ||
553 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Douglas Gregor33834512009-06-14 07:33:30 +0000554 return false;
Nico Weber4c311642008-08-10 19:59:06 +0000555
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000556 // If this diagnostic is in a system header and is not a clang error, suppress
557 // it.
558 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner8488c822008-11-18 07:04:44 +0000559 Info.getLocation().isValid() &&
John McCallbe089fa2010-02-11 10:04:29 +0000560 Info.getLocation().getInstantiationLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000561 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
562 LastDiagLevel = Diagnostic::Ignored;
Douglas Gregor33834512009-06-14 07:33:30 +0000563 return false;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000564 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000565
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000566 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000567 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000568 ++NumErrors;
Chris Lattner75a03932010-04-07 20:21:58 +0000569
570 // If we've emitted a lot of errors, emit a fatal error after it to stop a
571 // flood of bogus errors.
Chris Lattnerdec49e72010-04-07 20:37:06 +0000572 if (ErrorLimit && NumErrors >= ErrorLimit &&
Chris Lattner75a03932010-04-07 20:21:58 +0000573 DiagLevel == Diagnostic::Error)
574 SetDelayedDiagnostic(diag::fatal_too_many_errors);
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000575 }
Mike Stump11289f42009-09-09 15:08:12 +0000576
Chris Lattner22eb9722006-06-18 05:43:12 +0000577 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000578 Client->HandleDiagnostic(DiagLevel, Info);
Chris Lattner198cb4d2010-04-07 18:47:42 +0000579 if (Client->IncludeInDiagnosticCounts()) {
580 if (DiagLevel == Diagnostic::Warning)
581 ++NumWarnings;
582 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000583
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000584 CurDiagID = ~0U;
Douglas Gregor33834512009-06-14 07:33:30 +0000585
586 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000587}
588
Douglas Gregor85795312010-03-22 15:10:57 +0000589bool DiagnosticBuilder::Emit() {
590 // If DiagObj is null, then its soul was stolen by the copy ctor
591 // or the user called Emit().
592 if (DiagObj == 0) return false;
593
594 // When emitting diagnostics, we set the final argument count into
595 // the Diagnostic object.
596 DiagObj->NumDiagArgs = NumArgs;
597 DiagObj->NumDiagRanges = NumRanges;
Douglas Gregora771f462010-03-31 17:46:05 +0000598 DiagObj->NumFixItHints = NumFixItHints;
Douglas Gregor85795312010-03-22 15:10:57 +0000599
600 // Process the diagnostic, sending the accumulated information to the
601 // DiagnosticClient.
602 bool Emitted = DiagObj->ProcessDiag();
603
604 // Clear out the current diagnostic object.
Douglas Gregor96380982010-03-22 15:47:45 +0000605 unsigned DiagID = DiagObj->CurDiagID;
Douglas Gregor85795312010-03-22 15:10:57 +0000606 DiagObj->Clear();
607
608 // If there was a delayed diagnostic, emit it now.
Douglas Gregor96380982010-03-22 15:47:45 +0000609 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
Douglas Gregor85795312010-03-22 15:10:57 +0000610 DiagObj->ReportDelayed();
611
612 // This diagnostic is dead.
613 DiagObj = 0;
614
615 return Emitted;
616}
617
Nico Weber4c311642008-08-10 19:59:06 +0000618
Chris Lattner22eb9722006-06-18 05:43:12 +0000619DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000620
Chris Lattner23be0672008-11-19 06:51:40 +0000621
Chris Lattner2b786902008-11-21 07:50:02 +0000622/// ModifierIs - Return true if the specified modifier matches specified string.
623template <std::size_t StrLen>
624static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
625 const char (&Str)[StrLen]) {
626 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
627}
628
John McCall8cb7a8a32010-01-14 20:11:39 +0000629/// ScanForward - Scans forward, looking for the given character, skipping
630/// nested clauses and escaped characters.
631static const char *ScanFormat(const char *I, const char *E, char Target) {
632 unsigned Depth = 0;
633
634 for ( ; I != E; ++I) {
635 if (Depth == 0 && *I == Target) return I;
636 if (Depth != 0 && *I == '}') Depth--;
637
638 if (*I == '%') {
639 I++;
640 if (I == E) break;
641
642 // Escaped characters get implicitly skipped here.
643
644 // Format specifier.
645 if (!isdigit(*I) && !ispunct(*I)) {
646 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
647 if (I == E) break;
648 if (*I == '{')
649 Depth++;
650 }
651 }
652 }
653 return E;
654}
655
Chris Lattner2b786902008-11-21 07:50:02 +0000656/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
657/// like this: %select{foo|bar|baz}2. This means that the integer argument
658/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
659/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
660/// This is very useful for certain classes of variant diagnostics.
John McCalle4d54322010-01-13 23:58:20 +0000661static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000662 const char *Argument, unsigned ArgumentLen,
663 llvm::SmallVectorImpl<char> &OutStr) {
664 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000665
Chris Lattner2b786902008-11-21 07:50:02 +0000666 // Skip over 'ValNo' |'s.
667 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000668 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000669 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
670 " larger than the number of options in the diagnostic string!");
671 Argument = NextVal+1; // Skip this string.
672 --ValNo;
673 }
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner2b786902008-11-21 07:50:02 +0000675 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000676 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000677
678 // Recursively format the result of the select clause into the output string.
679 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000680}
681
682/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
683/// letter 's' to the string if the value is not 1. This is used in cases like
684/// this: "you idiot, you have %4 parameter%s4!".
685static void HandleIntegerSModifier(unsigned ValNo,
686 llvm::SmallVectorImpl<char> &OutStr) {
687 if (ValNo != 1)
688 OutStr.push_back('s');
689}
690
John McCall9015cde2010-01-14 00:50:32 +0000691/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
692/// prints the ordinal form of the given integer, with 1 corresponding
693/// to the first ordinal. Currently this is hard-coded to use the
694/// English form.
695static void HandleOrdinalModifier(unsigned ValNo,
696 llvm::SmallVectorImpl<char> &OutStr) {
697 assert(ValNo != 0 && "ValNo must be strictly positive!");
698
699 llvm::raw_svector_ostream Out(OutStr);
700
701 // We could use text forms for the first N ordinals, but the numeric
702 // forms are actually nicer in diagnostics because they stand out.
703 Out << ValNo;
704
705 // It is critically important that we do this perfectly for
706 // user-written sequences with over 100 elements.
707 switch (ValNo % 100) {
708 case 11:
709 case 12:
710 case 13:
711 Out << "th"; return;
712 default:
713 switch (ValNo % 10) {
714 case 1: Out << "st"; return;
715 case 2: Out << "nd"; return;
716 case 3: Out << "rd"; return;
717 default: Out << "th"; return;
718 }
719 }
720}
721
Chris Lattner2b786902008-11-21 07:50:02 +0000722
Sebastian Redl15b02d22008-11-22 13:44:36 +0000723/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000724static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000725 // Programming 101: Parse a decimal number :-)
726 unsigned Val = 0;
727 while (Start != End && *Start >= '0' && *Start <= '9') {
728 Val *= 10;
729 Val += *Start - '0';
730 ++Start;
731 }
732 return Val;
733}
734
735/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000736static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000737 if (*Start != '[') {
738 unsigned Ref = PluralNumber(Start, End);
739 return Ref == Val;
740 }
741
742 ++Start;
743 unsigned Low = PluralNumber(Start, End);
744 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
745 ++Start;
746 unsigned High = PluralNumber(Start, End);
747 assert(*Start == ']' && "Bad plural expression syntax: expected )");
748 ++Start;
749 return Low <= Val && Val <= High;
750}
751
752/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000753static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000754 // Empty condition?
755 if (*Start == ':')
756 return true;
757
758 while (1) {
759 char C = *Start;
760 if (C == '%') {
761 // Modulo expression
762 ++Start;
763 unsigned Arg = PluralNumber(Start, End);
764 assert(*Start == '=' && "Bad plural expression syntax: expected =");
765 ++Start;
766 unsigned ValMod = ValNo % Arg;
767 if (TestPluralRange(ValMod, Start, End))
768 return true;
769 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000770 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000771 "Bad plural expression syntax: unexpected character");
772 // Range expression
773 if (TestPluralRange(ValNo, Start, End))
774 return true;
775 }
776
777 // Scan for next or-expr part.
778 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000779 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000780 break;
781 ++Start;
782 }
783 return false;
784}
785
786/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
787/// for complex plural forms, or in languages where all plurals are complex.
788/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
789/// conditions that are tested in order, the form corresponding to the first
790/// that applies being emitted. The empty condition is always true, making the
791/// last form a default case.
792/// Conditions are simple boolean expressions, where n is the number argument.
793/// Here are the rules.
794/// condition := expression | empty
795/// empty := -> always true
796/// expression := numeric [',' expression] -> logical or
797/// numeric := range -> true if n in range
798/// | '%' number '=' range -> true if n % number in range
799/// range := number
800/// | '[' number ',' number ']' -> ranges are inclusive both ends
801///
802/// Here are some examples from the GNU gettext manual written in this form:
803/// English:
804/// {1:form0|:form1}
805/// Latvian:
806/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
807/// Gaeilge:
808/// {1:form0|2:form1|:form2}
809/// Romanian:
810/// {1:form0|0,%100=[1,19]:form1|:form2}
811/// Lithuanian:
812/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
813/// Russian (requires repeated form):
814/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
815/// Slovak
816/// {1:form0|[2,4]:form1|:form2}
817/// Polish (requires repeated form):
818/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
819static void HandlePluralModifier(unsigned ValNo,
820 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb8e73152009-04-16 05:04:32 +0000821 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000822 const char *ArgumentEnd = Argument + ArgumentLen;
823 while (1) {
824 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
825 const char *ExprEnd = Argument;
826 while (*ExprEnd != ':') {
827 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
828 ++ExprEnd;
829 }
830 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
831 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000832 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
Sebastian Redl15b02d22008-11-22 13:44:36 +0000833 OutStr.append(Argument, ExprEnd);
834 return;
835 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000836 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000837 }
838}
839
840
Chris Lattner23be0672008-11-19 06:51:40 +0000841/// FormatDiagnostic - Format this diagnostic into a string, substituting the
842/// formal arguments into the %0 slots. The result is appended onto the Str
843/// array.
844void DiagnosticInfo::
845FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
846 const char *DiagStr = getDiags()->getDescription(getID());
847 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump11289f42009-09-09 15:08:12 +0000848
John McCalle4d54322010-01-13 23:58:20 +0000849 FormatDiagnostic(DiagStr, DiagEnd, OutStr);
850}
851
852void DiagnosticInfo::
853FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
854 llvm::SmallVectorImpl<char> &OutStr) const {
855
Chris Lattnerc243f292009-10-20 05:25:22 +0000856 /// FormattedArgs - Keep track of all of the arguments formatted by
857 /// ConvertArgToString and pass them into subsequent calls to
858 /// ConvertArgToString, allowing the implementation to avoid redundancies in
859 /// obvious cases.
860 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
861
Chris Lattner23be0672008-11-19 06:51:40 +0000862 while (DiagStr != DiagEnd) {
863 if (DiagStr[0] != '%') {
864 // Append non-%0 substrings to Str if we have one.
865 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
866 OutStr.append(DiagStr, StrEnd);
867 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000868 continue;
John McCall8cb7a8a32010-01-14 20:11:39 +0000869 } else if (ispunct(DiagStr[1])) {
870 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000871 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000872 continue;
873 }
Mike Stump11289f42009-09-09 15:08:12 +0000874
Chris Lattner2b786902008-11-21 07:50:02 +0000875 // Skip the %.
876 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000877
Chris Lattner2b786902008-11-21 07:50:02 +0000878 // This must be a placeholder for a diagnostic argument. The format for a
879 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
880 // The digit is a number from 0-9 indicating which argument this comes from.
881 // The modifier is a string of digits from the set [-a-z]+, arguments is a
882 // brace enclosed string.
883 const char *Modifier = 0, *Argument = 0;
884 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000885
Chris Lattner2b786902008-11-21 07:50:02 +0000886 // Check to see if we have a modifier. If so eat it.
887 if (!isdigit(DiagStr[0])) {
888 Modifier = DiagStr;
889 while (DiagStr[0] == '-' ||
890 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
891 ++DiagStr;
892 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000893
Chris Lattner2b786902008-11-21 07:50:02 +0000894 // If we have an argument, get it next.
895 if (DiagStr[0] == '{') {
896 ++DiagStr; // Skip {.
897 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000898
John McCall8cb7a8a32010-01-14 20:11:39 +0000899 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
900 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000901 ArgumentLen = DiagStr-Argument;
902 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000903 }
Chris Lattner2b786902008-11-21 07:50:02 +0000904 }
Mike Stump11289f42009-09-09 15:08:12 +0000905
Chris Lattner2b786902008-11-21 07:50:02 +0000906 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000907 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000908
Chris Lattnerc243f292009-10-20 05:25:22 +0000909 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
910
911 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000912 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000913 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000914 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000915 assert(ModifierLen == 0 && "No modifiers for strings yet");
916 OutStr.append(S.begin(), S.end());
917 break;
918 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000919 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000920 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000921 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000922
923 // Don't crash if get passed a null pointer by accident.
924 if (!S)
925 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000926
Chris Lattner2b786902008-11-21 07:50:02 +0000927 OutStr.append(S, S + strlen(S));
928 break;
929 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000930 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000931 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000932 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000933
Chris Lattner2b786902008-11-21 07:50:02 +0000934 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000935 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000936 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
937 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000938 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
939 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000940 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
941 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000942 } else {
943 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000944 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000945 }
Chris Lattner2b786902008-11-21 07:50:02 +0000946 break;
947 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000948 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000949 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000950
Chris Lattner2b786902008-11-21 07:50:02 +0000951 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000952 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000953 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
954 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000955 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
956 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000957 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
958 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000959 } else {
960 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000961 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000962 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000963 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000964 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000965 // ---- NAMES and TYPES ----
966 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000967 const IdentifierInfo *II = getArgIdentifier(ArgNo);
968 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000969
970 // Don't crash if get passed a null pointer by accident.
971 if (!II) {
972 const char *S = "(null)";
973 OutStr.append(S, S + strlen(S));
974 continue;
975 }
976
Daniel Dunbar07d07852009-10-18 21:17:35 +0000977 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000978 break;
979 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000980 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000981 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000982 case Diagnostic::ak_nameddecl:
Douglas Gregor053f6912009-08-26 00:04:55 +0000983 case Diagnostic::ak_nestednamespec:
Douglas Gregore40876a2009-10-13 21:16:44 +0000984 case Diagnostic::ak_declcontext:
Chris Lattnerc243f292009-10-20 05:25:22 +0000985 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000986 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000987 Argument, ArgumentLen,
988 FormattedArgs.data(), FormattedArgs.size(),
989 OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000990 break;
Nico Weber4c311642008-08-10 19:59:06 +0000991 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000992
993 // Remember this argument info for subsequent formatting operations. Turn
994 // std::strings into a null terminated string to make it be the same case as
995 // all the other ones.
996 if (Kind != Diagnostic::ak_std_string)
997 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
998 else
999 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
1000 (intptr_t)getArgStdStr(ArgNo).c_str()));
1001
Nico Weber4c311642008-08-10 19:59:06 +00001002 }
Nico Weber4c311642008-08-10 19:59:06 +00001003}
Ted Kremenekea06ec12009-01-23 20:28:53 +00001004
Douglas Gregor33cdd812010-02-18 18:08:43 +00001005StoredDiagnostic::StoredDiagnostic() { }
1006
1007StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
1008 llvm::StringRef Message)
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001009 : Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +00001010
1011StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
1012 const DiagnosticInfo &Info)
1013 : Level(Level), Loc(Info.getLocation())
1014{
1015 llvm::SmallString<64> Message;
1016 Info.FormatDiagnostic(Message);
1017 this->Message.assign(Message.begin(), Message.end());
1018
1019 Ranges.reserve(Info.getNumRanges());
1020 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
1021 Ranges.push_back(Info.getRange(I));
1022
Douglas Gregora771f462010-03-31 17:46:05 +00001023 FixIts.reserve(Info.getNumFixItHints());
1024 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
1025 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +00001026}
1027
1028StoredDiagnostic::~StoredDiagnostic() { }
1029
Douglas Gregorac0605e2010-01-28 06:00:51 +00001030static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
1031 OS.write((const char *)&Value, sizeof(unsigned));
1032}
1033
1034static void WriteString(llvm::raw_ostream &OS, llvm::StringRef String) {
1035 WriteUnsigned(OS, String.size());
1036 OS.write(String.data(), String.size());
1037}
1038
1039static void WriteSourceLocation(llvm::raw_ostream &OS,
1040 SourceManager *SM,
1041 SourceLocation Location) {
1042 if (!SM || Location.isInvalid()) {
1043 // If we don't have a source manager or this location is invalid,
1044 // just write an invalid location.
1045 WriteUnsigned(OS, 0);
1046 WriteUnsigned(OS, 0);
1047 WriteUnsigned(OS, 0);
1048 return;
1049 }
1050
1051 Location = SM->getInstantiationLoc(Location);
1052 std::pair<FileID, unsigned> Decomposed = SM->getDecomposedLoc(Location);
Ted Kremenek39a76652010-04-12 19:54:17 +00001053
1054 const FileEntry *FE = SM->getFileEntryForID(Decomposed.first);
1055 if (FE)
1056 WriteString(OS, FE->getName());
1057 else {
1058 // Fallback to using the buffer name when there is no entry.
1059 WriteString(OS, SM->getBuffer(Decomposed.first)->getBufferIdentifier());
1060 }
1061
Douglas Gregorac0605e2010-01-28 06:00:51 +00001062 WriteUnsigned(OS, SM->getLineNumber(Decomposed.first, Decomposed.second));
1063 WriteUnsigned(OS, SM->getColumnNumber(Decomposed.first, Decomposed.second));
1064}
1065
Douglas Gregor33cdd812010-02-18 18:08:43 +00001066void StoredDiagnostic::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001067 SourceManager *SM = 0;
1068 if (getLocation().isValid())
1069 SM = &const_cast<SourceManager &>(getLocation().getManager());
1070
Douglas Gregor70127c12010-02-19 00:40:40 +00001071 // Write a short header to help identify diagnostics.
1072 OS << (char)0x06 << (char)0x07;
1073
Douglas Gregorac0605e2010-01-28 06:00:51 +00001074 // Write the diagnostic level and location.
Douglas Gregor33cdd812010-02-18 18:08:43 +00001075 WriteUnsigned(OS, (unsigned)Level);
Douglas Gregorac0605e2010-01-28 06:00:51 +00001076 WriteSourceLocation(OS, SM, getLocation());
1077
1078 // Write the diagnostic message.
1079 llvm::SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001080 WriteString(OS, getMessage());
Douglas Gregorac0605e2010-01-28 06:00:51 +00001081
1082 // Count the number of ranges that don't point into macros, since
1083 // only simple file ranges serialize well.
1084 unsigned NumNonMacroRanges = 0;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001085 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1086 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregorac0605e2010-01-28 06:00:51 +00001087 continue;
1088
1089 ++NumNonMacroRanges;
1090 }
1091
1092 // Write the ranges.
1093 WriteUnsigned(OS, NumNonMacroRanges);
1094 if (NumNonMacroRanges) {
Douglas Gregor33cdd812010-02-18 18:08:43 +00001095 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1096 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregorac0605e2010-01-28 06:00:51 +00001097 continue;
1098
Douglas Gregor33cdd812010-02-18 18:08:43 +00001099 WriteSourceLocation(OS, SM, R->getBegin());
1100 WriteSourceLocation(OS, SM, R->getEnd());
Douglas Gregorac0605e2010-01-28 06:00:51 +00001101 }
1102 }
1103
1104 // Determine if all of the fix-its involve rewrites with simple file
1105 // locations (not in macro instantiations). If so, we can write
1106 // fix-it information.
Douglas Gregor33cdd812010-02-18 18:08:43 +00001107 unsigned NumFixIts = 0;
1108 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1109 if (F->RemoveRange.isValid() &&
1110 (F->RemoveRange.getBegin().isMacroID() ||
1111 F->RemoveRange.getEnd().isMacroID())) {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001112 NumFixIts = 0;
1113 break;
1114 }
1115
Douglas Gregor33cdd812010-02-18 18:08:43 +00001116 if (F->InsertionLoc.isValid() && F->InsertionLoc.isMacroID()) {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001117 NumFixIts = 0;
1118 break;
1119 }
Douglas Gregor33cdd812010-02-18 18:08:43 +00001120
1121 ++NumFixIts;
Douglas Gregorac0605e2010-01-28 06:00:51 +00001122 }
1123
1124 // Write the fix-its.
1125 WriteUnsigned(OS, NumFixIts);
Douglas Gregor33cdd812010-02-18 18:08:43 +00001126 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1127 WriteSourceLocation(OS, SM, F->RemoveRange.getBegin());
1128 WriteSourceLocation(OS, SM, F->RemoveRange.getEnd());
1129 WriteSourceLocation(OS, SM, F->InsertionLoc);
1130 WriteString(OS, F->CodeToInsert);
Douglas Gregorac0605e2010-01-28 06:00:51 +00001131 }
1132}
1133
Douglas Gregor33cdd812010-02-18 18:08:43 +00001134static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
1135 unsigned &Value) {
1136 if (Memory + sizeof(unsigned) > MemoryEnd)
1137 return true;
1138
1139 memmove(&Value, Memory, sizeof(unsigned));
1140 Memory += sizeof(unsigned);
1141 return false;
1142}
1143
1144static bool ReadSourceLocation(FileManager &FM, SourceManager &SM,
1145 const char *&Memory, const char *MemoryEnd,
1146 SourceLocation &Location) {
1147 // Read the filename.
1148 unsigned FileNameLen = 0;
1149 if (ReadUnsigned(Memory, MemoryEnd, FileNameLen) ||
1150 Memory + FileNameLen > MemoryEnd)
1151 return true;
1152
1153 llvm::StringRef FileName(Memory, FileNameLen);
1154 Memory += FileNameLen;
1155
1156 // Read the line, column.
1157 unsigned Line = 0, Column = 0;
1158 if (ReadUnsigned(Memory, MemoryEnd, Line) ||
1159 ReadUnsigned(Memory, MemoryEnd, Column))
1160 return true;
1161
1162 if (FileName.empty()) {
1163 Location = SourceLocation();
1164 return false;
1165 }
1166
1167 const FileEntry *File = FM.getFile(FileName);
1168 if (!File)
1169 return true;
1170
1171 // Make sure that this file has an entry in the source manager.
1172 if (!SM.hasFileInfo(File))
1173 SM.createFileID(File, SourceLocation(), SrcMgr::C_User);
1174
1175 Location = SM.getLocation(File, Line, Column);
1176 return false;
1177}
1178
1179StoredDiagnostic
1180StoredDiagnostic::Deserialize(FileManager &FM, SourceManager &SM,
1181 const char *&Memory, const char *MemoryEnd) {
Douglas Gregor70127c12010-02-19 00:40:40 +00001182 while (true) {
1183 if (Memory == MemoryEnd)
1184 return StoredDiagnostic();
1185
1186 if (*Memory != 0x06) {
1187 ++Memory;
1188 continue;
1189 }
1190
1191 ++Memory;
1192 if (Memory == MemoryEnd)
1193 return StoredDiagnostic();
1194
1195 if (*Memory != 0x07) {
1196 ++Memory;
1197 continue;
1198 }
1199
1200 // We found the header. We're done.
1201 ++Memory;
1202 break;
1203 }
1204
Douglas Gregor33cdd812010-02-18 18:08:43 +00001205 // Read the severity level.
1206 unsigned Level = 0;
1207 if (ReadUnsigned(Memory, MemoryEnd, Level) || Level > Diagnostic::Fatal)
1208 return StoredDiagnostic();
1209
1210 // Read the source location.
1211 SourceLocation Location;
1212 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Location))
1213 return StoredDiagnostic();
1214
1215 // Read the diagnostic text.
1216 if (Memory == MemoryEnd)
1217 return StoredDiagnostic();
1218
1219 unsigned MessageLen = 0;
1220 if (ReadUnsigned(Memory, MemoryEnd, MessageLen) ||
1221 Memory + MessageLen > MemoryEnd)
1222 return StoredDiagnostic();
1223
1224 llvm::StringRef Message(Memory, MessageLen);
1225 Memory += MessageLen;
1226
1227
1228 // At this point, we have enough information to form a diagnostic. Do so.
1229 StoredDiagnostic Diag;
1230 Diag.Level = (Diagnostic::Level)Level;
1231 Diag.Loc = FullSourceLoc(Location, SM);
1232 Diag.Message = Message;
1233 if (Memory == MemoryEnd)
1234 return Diag;
1235
1236 // Read the source ranges.
1237 unsigned NumSourceRanges = 0;
1238 if (ReadUnsigned(Memory, MemoryEnd, NumSourceRanges))
1239 return Diag;
1240 for (unsigned I = 0; I != NumSourceRanges; ++I) {
1241 SourceLocation Begin, End;
1242 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Begin) ||
1243 ReadSourceLocation(FM, SM, Memory, MemoryEnd, End))
1244 return Diag;
1245
1246 Diag.Ranges.push_back(SourceRange(Begin, End));
1247 }
1248
1249 // Read the fix-it hints.
1250 unsigned NumFixIts = 0;
1251 if (ReadUnsigned(Memory, MemoryEnd, NumFixIts))
1252 return Diag;
1253 for (unsigned I = 0; I != NumFixIts; ++I) {
1254 SourceLocation RemoveBegin, RemoveEnd, InsertionLoc;
1255 unsigned InsertLen = 0;
1256 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveBegin) ||
1257 ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveEnd) ||
1258 ReadSourceLocation(FM, SM, Memory, MemoryEnd, InsertionLoc) ||
1259 ReadUnsigned(Memory, MemoryEnd, InsertLen) ||
1260 Memory + InsertLen > MemoryEnd) {
1261 Diag.FixIts.clear();
1262 return Diag;
1263 }
1264
Douglas Gregora771f462010-03-31 17:46:05 +00001265 FixItHint Hint;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001266 Hint.RemoveRange = SourceRange(RemoveBegin, RemoveEnd);
1267 Hint.InsertionLoc = InsertionLoc;
1268 Hint.CodeToInsert.assign(Memory, Memory + InsertLen);
1269 Memory += InsertLen;
1270 Diag.FixIts.push_back(Hint);
1271 }
1272
1273 return Diag;
1274}
1275
Ted Kremenekea06ec12009-01-23 20:28:53 +00001276/// IncludeInDiagnosticCounts - This method (whose default implementation
1277/// returns true) indicates whether the diagnostics handled by this
1278/// DiagnosticClient should be included in the number of diagnostics
1279/// reported by Diagnostic.
1280bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +00001281
1282PartialDiagnostic::StorageAllocator::StorageAllocator() {
1283 for (unsigned I = 0; I != NumCached; ++I)
1284 FreeList[I] = Cached + I;
1285 NumFreeListEntries = NumCached;
1286}
1287
1288PartialDiagnostic::StorageAllocator::~StorageAllocator() {
1289 assert(NumFreeListEntries == NumCached && "A partial is on the lamb");
1290}