blob: 1870195ded036cdc57ea76f58bd20f6ccdc437a5 [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;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000227 TemplateBacktraceLimit = 0;
228
Chris Lattner198cb4d2010-04-07 18:47:42 +0000229 NumWarnings = 0;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000230 NumErrors = 0;
Douglas Gregor2d2d9072010-04-14 22:19:45 +0000231 NumErrorsSuppressed = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000232 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000233 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000234 LastDiagLevel = Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000235
Chris Lattner63ecc502008-11-23 09:21:17 +0000236 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000237 ArgToStringCookie = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000238
Douglas Gregor85795312010-03-22 15:10:57 +0000239 DelayedDiagID = 0;
240
Chris Lattner411c0ff2009-04-16 04:12:40 +0000241 // Set all mappings to 'unset'.
Chris Lattnerfb42a182009-07-12 21:18:45 +0000242 DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0);
243 DiagMappingsStack.push_back(BlankDiags);
Chris Lattnerae411572006-07-05 00:55:08 +0000244}
245
Chris Lattnere6535cf2007-12-02 01:09:57 +0000246Diagnostic::~Diagnostic() {
247 delete CustomDiagInfo;
248}
249
Chris Lattnerfb42a182009-07-12 21:18:45 +0000250
251void Diagnostic::pushMappings() {
John Thompsond73d7ad2009-10-23 02:21:17 +0000252 // Avoids undefined behavior when the stack has to resize.
253 DiagMappingsStack.reserve(DiagMappingsStack.size() + 1);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000254 DiagMappingsStack.push_back(DiagMappingsStack.back());
255}
256
257bool Diagnostic::popMappings() {
258 if (DiagMappingsStack.size() == 1)
259 return false;
260
261 DiagMappingsStack.pop_back();
262 return true;
263}
264
Chris Lattnere6535cf2007-12-02 01:09:57 +0000265/// getCustomDiagID - Return an ID for a diagnostic with the specified message
266/// and level. If this is the first request for this diagnosic, it is
267/// registered and created, otherwise the existing ID is returned.
Daniel Dunbar4886c812009-12-01 17:42:06 +0000268unsigned Diagnostic::getCustomDiagID(Level L, llvm::StringRef Message) {
Mike Stump11289f42009-09-09 15:08:12 +0000269 if (CustomDiagInfo == 0)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000270 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000271 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000272}
273
274
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000275/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
276/// level of the specified diagnostic ID is a Warning or Extension.
277/// This only works on builtin diagnostics, not custom ones, and is not legal to
278/// call on NOTEs.
279bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000280 return DiagID < diag::DIAG_UPPER_LIMIT &&
281 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000282}
283
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000284/// \brief Determine whether the given built-in diagnostic ID is a
285/// Note.
286bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000287 return DiagID < diag::DIAG_UPPER_LIMIT &&
288 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000289}
290
Chris Lattnere007de32009-04-15 07:01:18 +0000291/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
Chris Lattner97a8e432010-04-12 21:53:11 +0000292/// ID is for an extension of some sort. This also returns EnabledByDefault,
293/// which is set to indicate whether the diagnostic is ignored by default (in
294/// which case -pedantic enables it) or treated as a warning/error by default.
Chris Lattnere007de32009-04-15 07:01:18 +0000295///
Chris Lattner97a8e432010-04-12 21:53:11 +0000296bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID,
297 bool &EnabledByDefault) {
298 if (DiagID >= diag::DIAG_UPPER_LIMIT ||
299 getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
300 return false;
301
302 EnabledByDefault = StaticDiagInfo[DiagID].Mapping != diag::MAP_IGNORE;
303 return true;
Chris Lattnere007de32009-04-15 07:01:18 +0000304}
305
Chris Lattner22eb9722006-06-18 05:43:12 +0000306
307/// getDescription - Given a diagnostic ID, return a description of the
308/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000309const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner6c440322009-04-16 06:07:15 +0000310 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
311 return Info->Description;
Chris Lattner7368d582009-01-27 18:30:58 +0000312 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000313}
314
Douglas Gregor85795312010-03-22 15:10:57 +0000315void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
316 llvm::StringRef Arg2) {
317 if (DelayedDiagID)
318 return;
319
320 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000321 DelayedDiagArg1 = Arg1.str();
322 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000323}
324
325void Diagnostic::ReportDelayed() {
326 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
327 DelayedDiagID = 0;
328 DelayedDiagArg1.clear();
329 DelayedDiagArg2.clear();
330}
331
Chris Lattner22eb9722006-06-18 05:43:12 +0000332/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
333/// object, classify the specified diagnostic ID into a Level, consumable by
334/// the DiagnosticClient.
335Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000336 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000337 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000338 return CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000339
Chris Lattner4431a1b2007-11-30 22:53:43 +0000340 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000341 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000342 return getDiagnosticLevel(DiagID, DiagClass);
343}
344
345/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
346/// object, classify the specified diagnostic ID into a Level, consumable by
347/// the DiagnosticClient.
348Diagnostic::Level
349Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattnerae411572006-07-05 00:55:08 +0000350 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000351 // to error. Errors can only be mapped to fatal.
Chris Lattnere007de32009-04-15 07:01:18 +0000352 Diagnostic::Level Result = Diagnostic::Fatal;
Mike Stump11289f42009-09-09 15:08:12 +0000353
Chris Lattner411c0ff2009-04-16 04:12:40 +0000354 // Get the mapping information, if unset, compute it lazily.
355 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
356 if (MappingInfo == 0) {
357 MappingInfo = GetDefaultDiagMapping(DiagID);
358 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattner411c0ff2009-04-16 04:12:40 +0000361 switch (MappingInfo & 7) {
362 default: assert(0 && "Unknown mapping!");
Chris Lattnere007de32009-04-15 07:01:18 +0000363 case diag::MAP_IGNORE:
Chris Lattnerb8e73152009-04-16 05:04:32 +0000364 // Ignore this, unless this is an extension diagnostic and we're mapping
365 // them onto warnings or errors.
366 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
367 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
368 (MappingInfo & 8) != 0) // User explicitly mapped it.
369 return Diagnostic::Ignored;
370 Result = Diagnostic::Warning;
371 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000372 if (Result == Diagnostic::Error && ErrorsAsFatal)
373 Result = Diagnostic::Fatal;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000374 break;
Chris Lattnere007de32009-04-15 07:01:18 +0000375 case diag::MAP_ERROR:
376 Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000377 if (ErrorsAsFatal)
378 Result = Diagnostic::Fatal;
Chris Lattnere007de32009-04-15 07:01:18 +0000379 break;
380 case diag::MAP_FATAL:
381 Result = Diagnostic::Fatal;
382 break;
383 case diag::MAP_WARNING:
384 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner8c800702008-05-29 15:36:45 +0000385 if (IgnoreAllWarnings)
386 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000387
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000388 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000389
Chris Lattnerb8e73152009-04-16 05:04:32 +0000390 // If this is an extension diagnostic and we're in -pedantic-error mode, and
391 // if the user didn't explicitly map it, upgrade to an error.
392 if (ExtBehavior == Ext_Error &&
393 (MappingInfo & 8) == 0 &&
394 isBuiltinExtensionDiag(DiagID))
395 Result = Diagnostic::Error;
Mike Stump11289f42009-09-09 15:08:12 +0000396
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000397 if (WarningsAsErrors)
398 Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000399 if (Result == Diagnostic::Error && ErrorsAsFatal)
400 Result = Diagnostic::Fatal;
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000401 break;
Mike Stump11289f42009-09-09 15:08:12 +0000402
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000403 case diag::MAP_WARNING_NO_WERROR:
404 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
405 // not be adjusted by -Werror or -pedantic-errors.
406 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000408 // If warnings are globally mapped to ignore or error, do it.
409 if (IgnoreAllWarnings)
410 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattnere007de32009-04-15 07:01:18 +0000412 break;
Chris Lattner801fda82009-12-22 23:12:53 +0000413
414 case diag::MAP_ERROR_NO_WFATAL:
415 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
416 // unaffected by -Wfatal-errors.
417 Result = Diagnostic::Error;
418 break;
Chris Lattner8c800702008-05-29 15:36:45 +0000419 }
Chris Lattnere007de32009-04-15 07:01:18 +0000420
421 // Okay, we're about to return this as a "diagnostic to emit" one last check:
422 // if this is any sort of extension warning, and if we're in an __extension__
423 // block, silence it.
424 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
425 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattnere007de32009-04-15 07:01:18 +0000427 return Result;
Chris Lattner22eb9722006-06-18 05:43:12 +0000428}
429
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000430struct WarningOption {
431 const char *Name;
432 const short *Members;
433 const char *SubGroups;
434};
435
436#define GET_DIAG_ARRAYS
437#include "clang/Basic/DiagnosticGroups.inc"
438#undef GET_DIAG_ARRAYS
439
440// Second the table of options, sorted by name for fast binary lookup.
441static const WarningOption OptionTable[] = {
442#define GET_DIAG_TABLE
443#include "clang/Basic/DiagnosticGroups.inc"
444#undef GET_DIAG_TABLE
445};
446static const size_t OptionTableSize =
447sizeof(OptionTable) / sizeof(OptionTable[0]);
448
449static bool WarningOptionCompare(const WarningOption &LHS,
450 const WarningOption &RHS) {
451 return strcmp(LHS.Name, RHS.Name) < 0;
452}
453
454static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
455 Diagnostic &Diags) {
456 // Option exists, poke all the members of its diagnostic set.
457 if (const short *Member = Group->Members) {
458 for (; *Member != -1; ++Member)
459 Diags.setDiagnosticMapping(*Member, Mapping);
460 }
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000462 // Enable/disable all subgroups along with this one.
463 if (const char *SubGroups = Group->SubGroups) {
464 for (; *SubGroups != (char)-1; ++SubGroups)
465 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
466 }
467}
468
469/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
470/// "unknown-pragmas" to have the specified mapping. This returns true and
471/// ignores the request if "Group" was unknown, false otherwise.
472bool Diagnostic::setDiagnosticGroupMapping(const char *Group,
473 diag::Mapping Map) {
Mike Stump11289f42009-09-09 15:08:12 +0000474
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000475 WarningOption Key = { Group, 0, 0 };
476 const WarningOption *Found =
477 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
478 WarningOptionCompare);
479 if (Found == OptionTable + OptionTableSize ||
480 strcmp(Found->Name, Group) != 0)
481 return true; // Option not found.
Mike Stump11289f42009-09-09 15:08:12 +0000482
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000483 MapGroupMembers(Found, Map, *this);
484 return false;
485}
486
487
Chris Lattner8488c822008-11-18 07:04:44 +0000488/// ProcessDiag - This is the method used to report a diagnostic that is
489/// finally fully formed.
Douglas Gregor33834512009-06-14 07:33:30 +0000490bool Diagnostic::ProcessDiag() {
Chris Lattner427c9c12008-11-22 00:59:29 +0000491 DiagnosticInfo Info(this);
Mike Stump11289f42009-09-09 15:08:12 +0000492
Douglas Gregor2436e712009-09-17 21:32:03 +0000493 if (SuppressAllDiagnostics)
494 return false;
495
Chris Lattner22eb9722006-06-18 05:43:12 +0000496 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000497 Diagnostic::Level DiagLevel;
498 unsigned DiagID = Info.getID();
Mike Stump11289f42009-09-09 15:08:12 +0000499
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000500 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
501 // in a system header.
502 bool ShouldEmitInSystemHeader;
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000504 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
505 // Handle custom diagnostics, which cannot be mapped.
506 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000508 // Custom diagnostics always are emitted in system headers.
509 ShouldEmitInSystemHeader = true;
510 } else {
511 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
512 // the diagnostic level was for the previous diagnostic so that it is
513 // filtered the same as the previous diagnostic.
514 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000515 if (DiagClass == CLASS_NOTE) {
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000516 DiagLevel = Diagnostic::Note;
517 ShouldEmitInSystemHeader = false; // extra consideration is needed
518 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000519 // If this is not an error and we are in a system header, we ignore it.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000520 // Check the original Diag ID here, because we also want to ignore
521 // extensions and warnings in -Werror and -pedantic-errors modes, which
522 // *map* warnings/extensions to errors.
Chris Lattnere6c831d2009-04-15 16:56:26 +0000523 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Mike Stump11289f42009-09-09 15:08:12 +0000524
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000525 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
526 }
527 }
528
Douglas Gregor19367f52009-03-19 18:55:06 +0000529 if (DiagLevel != Diagnostic::Note) {
530 // Record that a fatal error occurred only when we see a second
531 // non-note diagnostic. This allows notes to be attached to the
532 // fatal error, but suppresses any diagnostics that follow those
533 // notes.
534 if (LastDiagLevel == Diagnostic::Fatal)
535 FatalErrorOccurred = true;
536
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000537 LastDiagLevel = DiagLevel;
Mike Stump11289f42009-09-09 15:08:12 +0000538 }
Douglas Gregor19367f52009-03-19 18:55:06 +0000539
540 // If a fatal error has already been emitted, silence all subsequent
541 // diagnostics.
Douglas Gregor2d2d9072010-04-14 22:19:45 +0000542 if (FatalErrorOccurred) {
543 if (DiagLevel >= Diagnostic::Error) {
544 ++NumErrors;
545 ++NumErrorsSuppressed;
546 }
547
Douglas Gregor33834512009-06-14 07:33:30 +0000548 return false;
Douglas Gregor2d2d9072010-04-14 22:19:45 +0000549 }
Douglas Gregor19367f52009-03-19 18:55:06 +0000550
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000551 // If the client doesn't care about this message, don't issue it. If this is
552 // a note and the last real diagnostic was ignored, ignore it too.
553 if (DiagLevel == Diagnostic::Ignored ||
554 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Douglas Gregor33834512009-06-14 07:33:30 +0000555 return false;
Nico Weber4c311642008-08-10 19:59:06 +0000556
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000557 // If this diagnostic is in a system header and is not a clang error, suppress
558 // it.
559 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner8488c822008-11-18 07:04:44 +0000560 Info.getLocation().isValid() &&
John McCallbe089fa2010-02-11 10:04:29 +0000561 Info.getLocation().getInstantiationLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000562 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
563 LastDiagLevel = Diagnostic::Ignored;
Douglas Gregor33834512009-06-14 07:33:30 +0000564 return false;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000565 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000566
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000567 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000568 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000569 ++NumErrors;
Chris Lattner75a03932010-04-07 20:21:58 +0000570
571 // If we've emitted a lot of errors, emit a fatal error after it to stop a
572 // flood of bogus errors.
Chris Lattnerdec49e72010-04-07 20:37:06 +0000573 if (ErrorLimit && NumErrors >= ErrorLimit &&
Chris Lattner75a03932010-04-07 20:21:58 +0000574 DiagLevel == Diagnostic::Error)
575 SetDelayedDiagnostic(diag::fatal_too_many_errors);
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner22eb9722006-06-18 05:43:12 +0000578 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000579 Client->HandleDiagnostic(DiagLevel, Info);
Chris Lattner198cb4d2010-04-07 18:47:42 +0000580 if (Client->IncludeInDiagnosticCounts()) {
581 if (DiagLevel == Diagnostic::Warning)
582 ++NumWarnings;
583 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000584
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000585 CurDiagID = ~0U;
Douglas Gregor33834512009-06-14 07:33:30 +0000586
587 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000588}
589
Douglas Gregor85795312010-03-22 15:10:57 +0000590bool DiagnosticBuilder::Emit() {
591 // If DiagObj is null, then its soul was stolen by the copy ctor
592 // or the user called Emit().
593 if (DiagObj == 0) return false;
594
595 // When emitting diagnostics, we set the final argument count into
596 // the Diagnostic object.
597 DiagObj->NumDiagArgs = NumArgs;
598 DiagObj->NumDiagRanges = NumRanges;
Douglas Gregora771f462010-03-31 17:46:05 +0000599 DiagObj->NumFixItHints = NumFixItHints;
Douglas Gregor85795312010-03-22 15:10:57 +0000600
601 // Process the diagnostic, sending the accumulated information to the
602 // DiagnosticClient.
603 bool Emitted = DiagObj->ProcessDiag();
604
605 // Clear out the current diagnostic object.
Douglas Gregor96380982010-03-22 15:47:45 +0000606 unsigned DiagID = DiagObj->CurDiagID;
Douglas Gregor85795312010-03-22 15:10:57 +0000607 DiagObj->Clear();
608
609 // If there was a delayed diagnostic, emit it now.
Douglas Gregor96380982010-03-22 15:47:45 +0000610 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
Douglas Gregor85795312010-03-22 15:10:57 +0000611 DiagObj->ReportDelayed();
612
613 // This diagnostic is dead.
614 DiagObj = 0;
615
616 return Emitted;
617}
618
Nico Weber4c311642008-08-10 19:59:06 +0000619
Chris Lattner22eb9722006-06-18 05:43:12 +0000620DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000621
Chris Lattner23be0672008-11-19 06:51:40 +0000622
Chris Lattner2b786902008-11-21 07:50:02 +0000623/// ModifierIs - Return true if the specified modifier matches specified string.
624template <std::size_t StrLen>
625static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
626 const char (&Str)[StrLen]) {
627 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
628}
629
John McCall8cb7a8a32010-01-14 20:11:39 +0000630/// ScanForward - Scans forward, looking for the given character, skipping
631/// nested clauses and escaped characters.
632static const char *ScanFormat(const char *I, const char *E, char Target) {
633 unsigned Depth = 0;
634
635 for ( ; I != E; ++I) {
636 if (Depth == 0 && *I == Target) return I;
637 if (Depth != 0 && *I == '}') Depth--;
638
639 if (*I == '%') {
640 I++;
641 if (I == E) break;
642
643 // Escaped characters get implicitly skipped here.
644
645 // Format specifier.
646 if (!isdigit(*I) && !ispunct(*I)) {
647 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
648 if (I == E) break;
649 if (*I == '{')
650 Depth++;
651 }
652 }
653 }
654 return E;
655}
656
Chris Lattner2b786902008-11-21 07:50:02 +0000657/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
658/// like this: %select{foo|bar|baz}2. This means that the integer argument
659/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
660/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
661/// This is very useful for certain classes of variant diagnostics.
John McCalle4d54322010-01-13 23:58:20 +0000662static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000663 const char *Argument, unsigned ArgumentLen,
664 llvm::SmallVectorImpl<char> &OutStr) {
665 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000666
Chris Lattner2b786902008-11-21 07:50:02 +0000667 // Skip over 'ValNo' |'s.
668 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000669 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000670 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
671 " larger than the number of options in the diagnostic string!");
672 Argument = NextVal+1; // Skip this string.
673 --ValNo;
674 }
Mike Stump11289f42009-09-09 15:08:12 +0000675
Chris Lattner2b786902008-11-21 07:50:02 +0000676 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000677 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000678
679 // Recursively format the result of the select clause into the output string.
680 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000681}
682
683/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
684/// letter 's' to the string if the value is not 1. This is used in cases like
685/// this: "you idiot, you have %4 parameter%s4!".
686static void HandleIntegerSModifier(unsigned ValNo,
687 llvm::SmallVectorImpl<char> &OutStr) {
688 if (ValNo != 1)
689 OutStr.push_back('s');
690}
691
John McCall9015cde2010-01-14 00:50:32 +0000692/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
693/// prints the ordinal form of the given integer, with 1 corresponding
694/// to the first ordinal. Currently this is hard-coded to use the
695/// English form.
696static void HandleOrdinalModifier(unsigned ValNo,
697 llvm::SmallVectorImpl<char> &OutStr) {
698 assert(ValNo != 0 && "ValNo must be strictly positive!");
699
700 llvm::raw_svector_ostream Out(OutStr);
701
702 // We could use text forms for the first N ordinals, but the numeric
703 // forms are actually nicer in diagnostics because they stand out.
704 Out << ValNo;
705
706 // It is critically important that we do this perfectly for
707 // user-written sequences with over 100 elements.
708 switch (ValNo % 100) {
709 case 11:
710 case 12:
711 case 13:
712 Out << "th"; return;
713 default:
714 switch (ValNo % 10) {
715 case 1: Out << "st"; return;
716 case 2: Out << "nd"; return;
717 case 3: Out << "rd"; return;
718 default: Out << "th"; return;
719 }
720 }
721}
722
Chris Lattner2b786902008-11-21 07:50:02 +0000723
Sebastian Redl15b02d22008-11-22 13:44:36 +0000724/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000725static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000726 // Programming 101: Parse a decimal number :-)
727 unsigned Val = 0;
728 while (Start != End && *Start >= '0' && *Start <= '9') {
729 Val *= 10;
730 Val += *Start - '0';
731 ++Start;
732 }
733 return Val;
734}
735
736/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000737static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000738 if (*Start != '[') {
739 unsigned Ref = PluralNumber(Start, End);
740 return Ref == Val;
741 }
742
743 ++Start;
744 unsigned Low = PluralNumber(Start, End);
745 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
746 ++Start;
747 unsigned High = PluralNumber(Start, End);
748 assert(*Start == ']' && "Bad plural expression syntax: expected )");
749 ++Start;
750 return Low <= Val && Val <= High;
751}
752
753/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000754static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000755 // Empty condition?
756 if (*Start == ':')
757 return true;
758
759 while (1) {
760 char C = *Start;
761 if (C == '%') {
762 // Modulo expression
763 ++Start;
764 unsigned Arg = PluralNumber(Start, End);
765 assert(*Start == '=' && "Bad plural expression syntax: expected =");
766 ++Start;
767 unsigned ValMod = ValNo % Arg;
768 if (TestPluralRange(ValMod, Start, End))
769 return true;
770 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000771 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000772 "Bad plural expression syntax: unexpected character");
773 // Range expression
774 if (TestPluralRange(ValNo, Start, End))
775 return true;
776 }
777
778 // Scan for next or-expr part.
779 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000780 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000781 break;
782 ++Start;
783 }
784 return false;
785}
786
787/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
788/// for complex plural forms, or in languages where all plurals are complex.
789/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
790/// conditions that are tested in order, the form corresponding to the first
791/// that applies being emitted. The empty condition is always true, making the
792/// last form a default case.
793/// Conditions are simple boolean expressions, where n is the number argument.
794/// Here are the rules.
795/// condition := expression | empty
796/// empty := -> always true
797/// expression := numeric [',' expression] -> logical or
798/// numeric := range -> true if n in range
799/// | '%' number '=' range -> true if n % number in range
800/// range := number
801/// | '[' number ',' number ']' -> ranges are inclusive both ends
802///
803/// Here are some examples from the GNU gettext manual written in this form:
804/// English:
805/// {1:form0|:form1}
806/// Latvian:
807/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
808/// Gaeilge:
809/// {1:form0|2:form1|:form2}
810/// Romanian:
811/// {1:form0|0,%100=[1,19]:form1|:form2}
812/// Lithuanian:
813/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
814/// Russian (requires repeated form):
815/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
816/// Slovak
817/// {1:form0|[2,4]:form1|:form2}
818/// Polish (requires repeated form):
819/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
820static void HandlePluralModifier(unsigned ValNo,
821 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb8e73152009-04-16 05:04:32 +0000822 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000823 const char *ArgumentEnd = Argument + ArgumentLen;
824 while (1) {
825 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
826 const char *ExprEnd = Argument;
827 while (*ExprEnd != ':') {
828 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
829 ++ExprEnd;
830 }
831 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
832 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000833 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
Sebastian Redl15b02d22008-11-22 13:44:36 +0000834 OutStr.append(Argument, ExprEnd);
835 return;
836 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000837 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000838 }
839}
840
841
Chris Lattner23be0672008-11-19 06:51:40 +0000842/// FormatDiagnostic - Format this diagnostic into a string, substituting the
843/// formal arguments into the %0 slots. The result is appended onto the Str
844/// array.
845void DiagnosticInfo::
846FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
847 const char *DiagStr = getDiags()->getDescription(getID());
848 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump11289f42009-09-09 15:08:12 +0000849
John McCalle4d54322010-01-13 23:58:20 +0000850 FormatDiagnostic(DiagStr, DiagEnd, OutStr);
851}
852
853void DiagnosticInfo::
854FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
855 llvm::SmallVectorImpl<char> &OutStr) const {
856
Chris Lattnerc243f292009-10-20 05:25:22 +0000857 /// FormattedArgs - Keep track of all of the arguments formatted by
858 /// ConvertArgToString and pass them into subsequent calls to
859 /// ConvertArgToString, allowing the implementation to avoid redundancies in
860 /// obvious cases.
861 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
862
Chris Lattner23be0672008-11-19 06:51:40 +0000863 while (DiagStr != DiagEnd) {
864 if (DiagStr[0] != '%') {
865 // Append non-%0 substrings to Str if we have one.
866 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
867 OutStr.append(DiagStr, StrEnd);
868 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000869 continue;
John McCall8cb7a8a32010-01-14 20:11:39 +0000870 } else if (ispunct(DiagStr[1])) {
871 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000872 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000873 continue;
874 }
Mike Stump11289f42009-09-09 15:08:12 +0000875
Chris Lattner2b786902008-11-21 07:50:02 +0000876 // Skip the %.
877 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000878
Chris Lattner2b786902008-11-21 07:50:02 +0000879 // This must be a placeholder for a diagnostic argument. The format for a
880 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
881 // The digit is a number from 0-9 indicating which argument this comes from.
882 // The modifier is a string of digits from the set [-a-z]+, arguments is a
883 // brace enclosed string.
884 const char *Modifier = 0, *Argument = 0;
885 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000886
Chris Lattner2b786902008-11-21 07:50:02 +0000887 // Check to see if we have a modifier. If so eat it.
888 if (!isdigit(DiagStr[0])) {
889 Modifier = DiagStr;
890 while (DiagStr[0] == '-' ||
891 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
892 ++DiagStr;
893 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000894
Chris Lattner2b786902008-11-21 07:50:02 +0000895 // If we have an argument, get it next.
896 if (DiagStr[0] == '{') {
897 ++DiagStr; // Skip {.
898 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000899
John McCall8cb7a8a32010-01-14 20:11:39 +0000900 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
901 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000902 ArgumentLen = DiagStr-Argument;
903 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000904 }
Chris Lattner2b786902008-11-21 07:50:02 +0000905 }
Mike Stump11289f42009-09-09 15:08:12 +0000906
Chris Lattner2b786902008-11-21 07:50:02 +0000907 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000908 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000909
Chris Lattnerc243f292009-10-20 05:25:22 +0000910 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
911
912 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000913 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000914 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000915 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000916 assert(ModifierLen == 0 && "No modifiers for strings yet");
917 OutStr.append(S.begin(), S.end());
918 break;
919 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000920 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000921 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000922 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000923
924 // Don't crash if get passed a null pointer by accident.
925 if (!S)
926 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000927
Chris Lattner2b786902008-11-21 07:50:02 +0000928 OutStr.append(S, S + strlen(S));
929 break;
930 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000931 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000932 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000933 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000934
Chris Lattner2b786902008-11-21 07:50:02 +0000935 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000936 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000937 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
938 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000939 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
940 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000941 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
942 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000943 } else {
944 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000945 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000946 }
Chris Lattner2b786902008-11-21 07:50:02 +0000947 break;
948 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000949 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000950 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000951
Chris Lattner2b786902008-11-21 07:50:02 +0000952 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000953 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000954 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
955 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000956 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
957 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000958 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
959 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000960 } else {
961 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000962 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000963 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000964 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000965 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000966 // ---- NAMES and TYPES ----
967 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000968 const IdentifierInfo *II = getArgIdentifier(ArgNo);
969 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000970
971 // Don't crash if get passed a null pointer by accident.
972 if (!II) {
973 const char *S = "(null)";
974 OutStr.append(S, S + strlen(S));
975 continue;
976 }
977
Daniel Dunbar07d07852009-10-18 21:17:35 +0000978 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000979 break;
980 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000981 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000982 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000983 case Diagnostic::ak_nameddecl:
Douglas Gregor053f6912009-08-26 00:04:55 +0000984 case Diagnostic::ak_nestednamespec:
Douglas Gregore40876a2009-10-13 21:16:44 +0000985 case Diagnostic::ak_declcontext:
Chris Lattnerc243f292009-10-20 05:25:22 +0000986 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000987 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000988 Argument, ArgumentLen,
989 FormattedArgs.data(), FormattedArgs.size(),
990 OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000991 break;
Nico Weber4c311642008-08-10 19:59:06 +0000992 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000993
994 // Remember this argument info for subsequent formatting operations. Turn
995 // std::strings into a null terminated string to make it be the same case as
996 // all the other ones.
997 if (Kind != Diagnostic::ak_std_string)
998 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
999 else
1000 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
1001 (intptr_t)getArgStdStr(ArgNo).c_str()));
1002
Nico Weber4c311642008-08-10 19:59:06 +00001003 }
Nico Weber4c311642008-08-10 19:59:06 +00001004}
Ted Kremenekea06ec12009-01-23 20:28:53 +00001005
Douglas Gregor33cdd812010-02-18 18:08:43 +00001006StoredDiagnostic::StoredDiagnostic() { }
1007
1008StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
1009 llvm::StringRef Message)
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001010 : Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +00001011
1012StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
1013 const DiagnosticInfo &Info)
1014 : Level(Level), Loc(Info.getLocation())
1015{
1016 llvm::SmallString<64> Message;
1017 Info.FormatDiagnostic(Message);
1018 this->Message.assign(Message.begin(), Message.end());
1019
1020 Ranges.reserve(Info.getNumRanges());
1021 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
1022 Ranges.push_back(Info.getRange(I));
1023
Douglas Gregora771f462010-03-31 17:46:05 +00001024 FixIts.reserve(Info.getNumFixItHints());
1025 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
1026 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +00001027}
1028
1029StoredDiagnostic::~StoredDiagnostic() { }
1030
Douglas Gregorac0605e2010-01-28 06:00:51 +00001031static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
1032 OS.write((const char *)&Value, sizeof(unsigned));
1033}
1034
1035static void WriteString(llvm::raw_ostream &OS, llvm::StringRef String) {
1036 WriteUnsigned(OS, String.size());
1037 OS.write(String.data(), String.size());
1038}
1039
1040static void WriteSourceLocation(llvm::raw_ostream &OS,
1041 SourceManager *SM,
1042 SourceLocation Location) {
1043 if (!SM || Location.isInvalid()) {
1044 // If we don't have a source manager or this location is invalid,
1045 // just write an invalid location.
1046 WriteUnsigned(OS, 0);
1047 WriteUnsigned(OS, 0);
1048 WriteUnsigned(OS, 0);
1049 return;
1050 }
1051
1052 Location = SM->getInstantiationLoc(Location);
1053 std::pair<FileID, unsigned> Decomposed = SM->getDecomposedLoc(Location);
Ted Kremenek39a76652010-04-12 19:54:17 +00001054
1055 const FileEntry *FE = SM->getFileEntryForID(Decomposed.first);
1056 if (FE)
1057 WriteString(OS, FE->getName());
1058 else {
1059 // Fallback to using the buffer name when there is no entry.
1060 WriteString(OS, SM->getBuffer(Decomposed.first)->getBufferIdentifier());
1061 }
1062
Douglas Gregorac0605e2010-01-28 06:00:51 +00001063 WriteUnsigned(OS, SM->getLineNumber(Decomposed.first, Decomposed.second));
1064 WriteUnsigned(OS, SM->getColumnNumber(Decomposed.first, Decomposed.second));
1065}
1066
Douglas Gregor33cdd812010-02-18 18:08:43 +00001067void StoredDiagnostic::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001068 SourceManager *SM = 0;
1069 if (getLocation().isValid())
1070 SM = &const_cast<SourceManager &>(getLocation().getManager());
1071
Douglas Gregor70127c12010-02-19 00:40:40 +00001072 // Write a short header to help identify diagnostics.
1073 OS << (char)0x06 << (char)0x07;
1074
Douglas Gregorac0605e2010-01-28 06:00:51 +00001075 // Write the diagnostic level and location.
Douglas Gregor33cdd812010-02-18 18:08:43 +00001076 WriteUnsigned(OS, (unsigned)Level);
Douglas Gregorac0605e2010-01-28 06:00:51 +00001077 WriteSourceLocation(OS, SM, getLocation());
1078
1079 // Write the diagnostic message.
1080 llvm::SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001081 WriteString(OS, getMessage());
Douglas Gregorac0605e2010-01-28 06:00:51 +00001082
1083 // Count the number of ranges that don't point into macros, since
1084 // only simple file ranges serialize well.
1085 unsigned NumNonMacroRanges = 0;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001086 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1087 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregorac0605e2010-01-28 06:00:51 +00001088 continue;
1089
1090 ++NumNonMacroRanges;
1091 }
1092
1093 // Write the ranges.
1094 WriteUnsigned(OS, NumNonMacroRanges);
1095 if (NumNonMacroRanges) {
Douglas Gregor33cdd812010-02-18 18:08:43 +00001096 for (range_iterator R = range_begin(), REnd = range_end(); R != REnd; ++R) {
1097 if (R->getBegin().isMacroID() || R->getEnd().isMacroID())
Douglas Gregorac0605e2010-01-28 06:00:51 +00001098 continue;
1099
Douglas Gregor33cdd812010-02-18 18:08:43 +00001100 WriteSourceLocation(OS, SM, R->getBegin());
1101 WriteSourceLocation(OS, SM, R->getEnd());
Douglas Gregorac0605e2010-01-28 06:00:51 +00001102 }
1103 }
1104
1105 // Determine if all of the fix-its involve rewrites with simple file
1106 // locations (not in macro instantiations). If so, we can write
1107 // fix-it information.
Douglas Gregor33cdd812010-02-18 18:08:43 +00001108 unsigned NumFixIts = 0;
1109 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1110 if (F->RemoveRange.isValid() &&
1111 (F->RemoveRange.getBegin().isMacroID() ||
1112 F->RemoveRange.getEnd().isMacroID())) {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001113 NumFixIts = 0;
1114 break;
1115 }
1116
Douglas Gregor33cdd812010-02-18 18:08:43 +00001117 if (F->InsertionLoc.isValid() && F->InsertionLoc.isMacroID()) {
Douglas Gregorac0605e2010-01-28 06:00:51 +00001118 NumFixIts = 0;
1119 break;
1120 }
Douglas Gregor33cdd812010-02-18 18:08:43 +00001121
1122 ++NumFixIts;
Douglas Gregorac0605e2010-01-28 06:00:51 +00001123 }
1124
1125 // Write the fix-its.
1126 WriteUnsigned(OS, NumFixIts);
Douglas Gregor33cdd812010-02-18 18:08:43 +00001127 for (fixit_iterator F = fixit_begin(), FEnd = fixit_end(); F != FEnd; ++F) {
1128 WriteSourceLocation(OS, SM, F->RemoveRange.getBegin());
1129 WriteSourceLocation(OS, SM, F->RemoveRange.getEnd());
1130 WriteSourceLocation(OS, SM, F->InsertionLoc);
1131 WriteString(OS, F->CodeToInsert);
Douglas Gregorac0605e2010-01-28 06:00:51 +00001132 }
1133}
1134
Douglas Gregor33cdd812010-02-18 18:08:43 +00001135static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
1136 unsigned &Value) {
1137 if (Memory + sizeof(unsigned) > MemoryEnd)
1138 return true;
1139
1140 memmove(&Value, Memory, sizeof(unsigned));
1141 Memory += sizeof(unsigned);
1142 return false;
1143}
1144
1145static bool ReadSourceLocation(FileManager &FM, SourceManager &SM,
1146 const char *&Memory, const char *MemoryEnd,
1147 SourceLocation &Location) {
1148 // Read the filename.
1149 unsigned FileNameLen = 0;
1150 if (ReadUnsigned(Memory, MemoryEnd, FileNameLen) ||
1151 Memory + FileNameLen > MemoryEnd)
1152 return true;
1153
1154 llvm::StringRef FileName(Memory, FileNameLen);
1155 Memory += FileNameLen;
1156
1157 // Read the line, column.
1158 unsigned Line = 0, Column = 0;
1159 if (ReadUnsigned(Memory, MemoryEnd, Line) ||
1160 ReadUnsigned(Memory, MemoryEnd, Column))
1161 return true;
1162
1163 if (FileName.empty()) {
1164 Location = SourceLocation();
1165 return false;
1166 }
1167
1168 const FileEntry *File = FM.getFile(FileName);
1169 if (!File)
1170 return true;
1171
1172 // Make sure that this file has an entry in the source manager.
1173 if (!SM.hasFileInfo(File))
1174 SM.createFileID(File, SourceLocation(), SrcMgr::C_User);
1175
1176 Location = SM.getLocation(File, Line, Column);
1177 return false;
1178}
1179
1180StoredDiagnostic
1181StoredDiagnostic::Deserialize(FileManager &FM, SourceManager &SM,
1182 const char *&Memory, const char *MemoryEnd) {
Douglas Gregor70127c12010-02-19 00:40:40 +00001183 while (true) {
1184 if (Memory == MemoryEnd)
1185 return StoredDiagnostic();
1186
1187 if (*Memory != 0x06) {
1188 ++Memory;
1189 continue;
1190 }
1191
1192 ++Memory;
1193 if (Memory == MemoryEnd)
1194 return StoredDiagnostic();
1195
1196 if (*Memory != 0x07) {
1197 ++Memory;
1198 continue;
1199 }
1200
1201 // We found the header. We're done.
1202 ++Memory;
1203 break;
1204 }
1205
Douglas Gregor33cdd812010-02-18 18:08:43 +00001206 // Read the severity level.
1207 unsigned Level = 0;
1208 if (ReadUnsigned(Memory, MemoryEnd, Level) || Level > Diagnostic::Fatal)
1209 return StoredDiagnostic();
1210
1211 // Read the source location.
1212 SourceLocation Location;
1213 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Location))
1214 return StoredDiagnostic();
1215
1216 // Read the diagnostic text.
1217 if (Memory == MemoryEnd)
1218 return StoredDiagnostic();
1219
1220 unsigned MessageLen = 0;
1221 if (ReadUnsigned(Memory, MemoryEnd, MessageLen) ||
1222 Memory + MessageLen > MemoryEnd)
1223 return StoredDiagnostic();
1224
1225 llvm::StringRef Message(Memory, MessageLen);
1226 Memory += MessageLen;
1227
1228
1229 // At this point, we have enough information to form a diagnostic. Do so.
1230 StoredDiagnostic Diag;
1231 Diag.Level = (Diagnostic::Level)Level;
1232 Diag.Loc = FullSourceLoc(Location, SM);
1233 Diag.Message = Message;
1234 if (Memory == MemoryEnd)
1235 return Diag;
1236
1237 // Read the source ranges.
1238 unsigned NumSourceRanges = 0;
1239 if (ReadUnsigned(Memory, MemoryEnd, NumSourceRanges))
1240 return Diag;
1241 for (unsigned I = 0; I != NumSourceRanges; ++I) {
1242 SourceLocation Begin, End;
1243 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, Begin) ||
1244 ReadSourceLocation(FM, SM, Memory, MemoryEnd, End))
1245 return Diag;
1246
1247 Diag.Ranges.push_back(SourceRange(Begin, End));
1248 }
1249
1250 // Read the fix-it hints.
1251 unsigned NumFixIts = 0;
1252 if (ReadUnsigned(Memory, MemoryEnd, NumFixIts))
1253 return Diag;
1254 for (unsigned I = 0; I != NumFixIts; ++I) {
1255 SourceLocation RemoveBegin, RemoveEnd, InsertionLoc;
1256 unsigned InsertLen = 0;
1257 if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveBegin) ||
1258 ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveEnd) ||
1259 ReadSourceLocation(FM, SM, Memory, MemoryEnd, InsertionLoc) ||
1260 ReadUnsigned(Memory, MemoryEnd, InsertLen) ||
1261 Memory + InsertLen > MemoryEnd) {
1262 Diag.FixIts.clear();
1263 return Diag;
1264 }
1265
Douglas Gregora771f462010-03-31 17:46:05 +00001266 FixItHint Hint;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001267 Hint.RemoveRange = SourceRange(RemoveBegin, RemoveEnd);
1268 Hint.InsertionLoc = InsertionLoc;
1269 Hint.CodeToInsert.assign(Memory, Memory + InsertLen);
1270 Memory += InsertLen;
1271 Diag.FixIts.push_back(Hint);
1272 }
1273
1274 return Diag;
1275}
1276
Ted Kremenekea06ec12009-01-23 20:28:53 +00001277/// IncludeInDiagnosticCounts - This method (whose default implementation
1278/// returns true) indicates whether the diagnostics handled by this
1279/// DiagnosticClient should be included in the number of diagnostics
1280/// reported by Diagnostic.
1281bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +00001282
1283PartialDiagnostic::StorageAllocator::StorageAllocator() {
1284 for (unsigned I = 0; I != NumCached; ++I)
1285 FreeList[I] = Cached + I;
1286 NumFreeListEntries = NumCached;
1287}
1288
1289PartialDiagnostic::StorageAllocator::~StorageAllocator() {
1290 assert(NumFreeListEntries == NumCached && "A partial is on the lamb");
1291}