blob: efe34828c6e5e40fd737f252e398b7b7cd53873f [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Diagnostic.h"
Chris Lattnere007de32009-04-15 07:01:18 +000015
16#include "clang/Lex/LexDiagnostic.h"
17#include "clang/Parse/ParseDiagnostic.h"
18#include "clang/AST/ASTDiagnostic.h"
19#include "clang/Sema/SemaDiagnostic.h"
20#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Analysis/AnalysisDiagnostic.h"
22#include "clang/Driver/DriverDiagnostic.h"
23
Chris Lattnerb91fd172008-11-19 07:32:16 +000024#include "clang/Basic/IdentifierTable.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000025#include "clang/Basic/SourceLocation.h"
Chris Lattner23be0672008-11-19 06:51:40 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner91aea712008-11-19 07:22:31 +000027#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3633792009-10-17 18:12:14 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattnere6535cf2007-12-02 01:09:57 +000029#include <vector>
30#include <map>
Chris Lattner0d799d32008-03-10 17:04:53 +000031#include <cstring>
Chris Lattner22eb9722006-06-18 05:43:12 +000032using namespace clang;
33
Chris Lattnere6535cf2007-12-02 01:09:57 +000034//===----------------------------------------------------------------------===//
35// Builtin Diagnostic information
36//===----------------------------------------------------------------------===//
37
Chris Lattner6c440322009-04-16 06:07:15 +000038// Diagnostic classes.
39enum {
40 CLASS_NOTE = 0x01,
41 CLASS_WARNING = 0x02,
42 CLASS_EXTENSION = 0x03,
43 CLASS_ERROR = 0x04
44};
Chris Lattnere007de32009-04-15 07:01:18 +000045
Chris Lattner6a64cc62009-04-16 06:00:24 +000046struct StaticDiagInfoRec {
Chris Lattner6c440322009-04-16 06:07:15 +000047 unsigned short DiagID;
48 unsigned Mapping : 3;
49 unsigned Class : 3;
Douglas Gregor33834512009-06-14 07:33:30 +000050 bool SFINAE : 1;
Chris Lattner6c440322009-04-16 06:07:15 +000051 const char *Description;
Chris Lattner6a64cc62009-04-16 06:00:24 +000052 const char *OptionGroup;
Mike Stump11289f42009-09-09 15:08:12 +000053
Chris Lattner2d49eed2009-04-16 06:13:46 +000054 bool operator<(const StaticDiagInfoRec &RHS) const {
55 return DiagID < RHS.DiagID;
56 }
57 bool operator>(const StaticDiagInfoRec &RHS) const {
58 return DiagID > RHS.DiagID;
59 }
Chris Lattnere007de32009-04-15 07:01:18 +000060};
61
Chris Lattner6a64cc62009-04-16 06:00:24 +000062static const StaticDiagInfoRec StaticDiagInfo[] = {
Douglas Gregor33834512009-06-14 07:33:30 +000063#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,SFINAE) \
64 { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, DESC, GROUP },
Chris Lattnere007de32009-04-15 07:01:18 +000065#include "clang/Basic/DiagnosticCommonKinds.inc"
66#include "clang/Basic/DiagnosticDriverKinds.inc"
67#include "clang/Basic/DiagnosticFrontendKinds.inc"
68#include "clang/Basic/DiagnosticLexKinds.inc"
69#include "clang/Basic/DiagnosticParseKinds.inc"
70#include "clang/Basic/DiagnosticASTKinds.inc"
71#include "clang/Basic/DiagnosticSemaKinds.inc"
72#include "clang/Basic/DiagnosticAnalysisKinds.inc"
Douglas Gregor33834512009-06-14 07:33:30 +000073 { 0, 0, 0, 0, 0, 0}
Chris Lattnere007de32009-04-15 07:01:18 +000074};
Chris Lattnere6c831d2009-04-15 16:56:26 +000075#undef DIAG
Chris Lattnere007de32009-04-15 07:01:18 +000076
Chris Lattner2d49eed2009-04-16 06:13:46 +000077/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
78/// or null if the ID is invalid.
Chris Lattner6a64cc62009-04-16 06:00:24 +000079static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
Chris Lattner2d49eed2009-04-16 06:13:46 +000080 unsigned NumDiagEntries = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
81
82 // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
83#ifndef NDEBUG
84 static bool IsFirst = true;
85 if (IsFirst) {
Chris Lattnercb4e68c2009-10-16 02:34:51 +000086 for (unsigned i = 1; i != NumDiagEntries; ++i) {
87 assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
88 "Diag ID conflict, the enums at the start of clang::diag (in "
89 "Diagnostic.h) probably need to be increased");
90
Chris Lattner2d49eed2009-04-16 06:13:46 +000091 assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
92 "Improperly sorted diag info");
Chris Lattnercb4e68c2009-10-16 02:34:51 +000093 }
Chris Lattner2d49eed2009-04-16 06:13:46 +000094 IsFirst = false;
95 }
96#endif
Mike Stump11289f42009-09-09 15:08:12 +000097
Chris Lattner2d49eed2009-04-16 06:13:46 +000098 // Search the diagnostic table with a binary search.
Douglas Gregor33834512009-06-14 07:33:30 +000099 StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0 };
Mike Stump11289f42009-09-09 15:08:12 +0000100
Chris Lattner2d49eed2009-04-16 06:13:46 +0000101 const StaticDiagInfoRec *Found =
102 std::lower_bound(StaticDiagInfo, StaticDiagInfo + NumDiagEntries, Find);
103 if (Found == StaticDiagInfo + NumDiagEntries ||
104 Found->DiagID != DiagID)
105 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattner2d49eed2009-04-16 06:13:46 +0000107 return Found;
Chris Lattner6a64cc62009-04-16 06:00:24 +0000108}
109
110static unsigned GetDefaultDiagMapping(unsigned DiagID) {
111 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Chris Lattner6c440322009-04-16 06:07:15 +0000112 return Info->Mapping;
Chris Lattner411c0ff2009-04-16 04:12:40 +0000113 return diag::MAP_FATAL;
114}
115
Chris Lattner22cb8182009-04-16 05:44:38 +0000116/// getWarningOptionForDiag - Return the lowest-level warning option that
117/// enables the specified diagnostic. If there is no -Wfoo flag that controls
118/// the diagnostic, this returns null.
119const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) {
Chris Lattner6a64cc62009-04-16 06:00:24 +0000120 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
121 return Info->OptionGroup;
122 return 0;
Chris Lattner22cb8182009-04-16 05:44:38 +0000123}
124
Douglas Gregor33834512009-06-14 07:33:30 +0000125bool Diagnostic::isBuiltinSFINAEDiag(unsigned DiagID) {
126 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
Douglas Gregor15e08d82009-06-15 16:52:15 +0000127 return Info->SFINAE && Info->Class == CLASS_ERROR;
Douglas Gregor33834512009-06-14 07:33:30 +0000128 return false;
129}
130
Chris Lattner22eb9722006-06-18 05:43:12 +0000131/// getDiagClass - Return the class field of the diagnostic.
132///
Chris Lattner4431a1b2007-11-30 22:53:43 +0000133static unsigned getBuiltinDiagClass(unsigned DiagID) {
Chris Lattner6c440322009-04-16 06:07:15 +0000134 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
135 return Info->Class;
136 return ~0U;
Chris Lattner22eb9722006-06-18 05:43:12 +0000137}
138
Chris Lattnere6535cf2007-12-02 01:09:57 +0000139//===----------------------------------------------------------------------===//
140// Custom Diagnostic information
141//===----------------------------------------------------------------------===//
142
143namespace clang {
144 namespace diag {
145 class CustomDiagInfo {
146 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
147 std::vector<DiagDesc> DiagInfo;
148 std::map<DiagDesc, unsigned> DiagIDs;
149 public:
Mike Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattnere6535cf2007-12-02 01:09:57 +0000151 /// getDescription - Return the description of the specified custom
152 /// diagnostic.
153 const char *getDescription(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000154 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000155 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000156 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
Chris Lattnere6535cf2007-12-02 01:09:57 +0000157 }
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattnere6535cf2007-12-02 01:09:57 +0000159 /// getLevel - Return the level of the specified custom diagnostic.
160 Diagnostic::Level getLevel(unsigned DiagID) const {
Chris Lattner36790cf2009-01-29 06:55:46 +0000161 assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
Chris Lattnere6535cf2007-12-02 01:09:57 +0000162 "Invalid diagnosic ID");
Chris Lattner36790cf2009-01-29 06:55:46 +0000163 return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000164 }
Mike Stump11289f42009-09-09 15:08:12 +0000165
Daniel Dunbar4886c812009-12-01 17:42:06 +0000166 unsigned getOrCreateDiagID(Diagnostic::Level L, llvm::StringRef Message,
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000167 Diagnostic &Diags) {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000168 DiagDesc D(L, Message);
169 // Check to see if it already exists.
170 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
171 if (I != DiagIDs.end() && I->first == D)
172 return I->second;
Mike Stump11289f42009-09-09 15:08:12 +0000173
Chris Lattnere6535cf2007-12-02 01:09:57 +0000174 // If not, assign a new ID.
Chris Lattner36790cf2009-01-29 06:55:46 +0000175 unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000176 DiagIDs.insert(std::make_pair(D, ID));
177 DiagInfo.push_back(D);
178 return ID;
179 }
180 };
Mike Stump11289f42009-09-09 15:08:12 +0000181
182 } // end diag namespace
183} // end clang namespace
Chris Lattnere6535cf2007-12-02 01:09:57 +0000184
185
186//===----------------------------------------------------------------------===//
187// Common Diagnostic implementation
188//===----------------------------------------------------------------------===//
189
Chris Lattner63ecc502008-11-23 09:21:17 +0000190static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
191 const char *Modifier, unsigned ML,
192 const char *Argument, unsigned ArgLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000193 const Diagnostic::ArgumentValue *PrevArgs,
194 unsigned NumPrevArgs,
Chris Lattnercf868c42009-02-19 23:53:20 +0000195 llvm::SmallVectorImpl<char> &Output,
196 void *Cookie) {
Chris Lattner63ecc502008-11-23 09:21:17 +0000197 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000198 Output.append(Str, Str+strlen(Str));
199}
200
201
Ted Kremenek31691ae2008-08-07 17:49:57 +0000202Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
Chris Lattnere007de32009-04-15 07:01:18 +0000203 AllExtensionsSilenced = 0;
Chris Lattner8c800702008-05-29 15:36:45 +0000204 IgnoreAllWarnings = false;
Chris Lattnerae411572006-07-05 00:55:08 +0000205 WarningsAsErrors = false;
Chris Lattner801fda82009-12-22 23:12:53 +0000206 ErrorsAsFatal = false;
Daniel Dunbar84b70f72008-09-12 18:10:20 +0000207 SuppressSystemWarnings = false;
Douglas Gregor2436e712009-09-17 21:32:03 +0000208 SuppressAllDiagnostics = false;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000209 ExtBehavior = Ext_Ignore;
Mike Stump11289f42009-09-09 15:08:12 +0000210
Chris Lattnerc49b9052007-05-28 00:46:44 +0000211 ErrorOccurred = false;
Chris Lattner9e031192009-02-06 04:16:02 +0000212 FatalErrorOccurred = false;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000213 NumDiagnostics = 0;
Steve Naroff4fb3d9f2009-12-05 02:14:08 +0000214
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000215 NumErrors = 0;
Chris Lattnere6535cf2007-12-02 01:09:57 +0000216 CustomDiagInfo = 0;
Chris Lattner427c9c12008-11-22 00:59:29 +0000217 CurDiagID = ~0U;
Douglas Gregor19367f52009-03-19 18:55:06 +0000218 LastDiagLevel = Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000219
Chris Lattner63ecc502008-11-23 09:21:17 +0000220 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +0000221 ArgToStringCookie = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000222
Chris Lattner411c0ff2009-04-16 04:12:40 +0000223 // Set all mappings to 'unset'.
Chris Lattnerfb42a182009-07-12 21:18:45 +0000224 DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0);
225 DiagMappingsStack.push_back(BlankDiags);
Chris Lattnerae411572006-07-05 00:55:08 +0000226}
227
Chris Lattnere6535cf2007-12-02 01:09:57 +0000228Diagnostic::~Diagnostic() {
229 delete CustomDiagInfo;
230}
231
Chris Lattnerfb42a182009-07-12 21:18:45 +0000232
233void Diagnostic::pushMappings() {
John Thompsond73d7ad2009-10-23 02:21:17 +0000234 // Avoids undefined behavior when the stack has to resize.
235 DiagMappingsStack.reserve(DiagMappingsStack.size() + 1);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000236 DiagMappingsStack.push_back(DiagMappingsStack.back());
237}
238
239bool Diagnostic::popMappings() {
240 if (DiagMappingsStack.size() == 1)
241 return false;
242
243 DiagMappingsStack.pop_back();
244 return true;
245}
246
Chris Lattnere6535cf2007-12-02 01:09:57 +0000247/// getCustomDiagID - Return an ID for a diagnostic with the specified message
248/// and level. If this is the first request for this diagnosic, it is
249/// registered and created, otherwise the existing ID is returned.
Daniel Dunbar4886c812009-12-01 17:42:06 +0000250unsigned Diagnostic::getCustomDiagID(Level L, llvm::StringRef Message) {
Mike Stump11289f42009-09-09 15:08:12 +0000251 if (CustomDiagInfo == 0)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000252 CustomDiagInfo = new diag::CustomDiagInfo();
Chris Lattnerf0a5f842008-10-17 21:24:47 +0000253 return CustomDiagInfo->getOrCreateDiagID(L, Message, *this);
Chris Lattnere6535cf2007-12-02 01:09:57 +0000254}
255
256
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000257/// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
258/// level of the specified diagnostic ID is a Warning or Extension.
259/// This only works on builtin diagnostics, not custom ones, and is not legal to
260/// call on NOTEs.
261bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000262 return DiagID < diag::DIAG_UPPER_LIMIT &&
263 getBuiltinDiagClass(DiagID) != CLASS_ERROR;
Chris Lattner22eb9722006-06-18 05:43:12 +0000264}
265
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000266/// \brief Determine whether the given built-in diagnostic ID is a
267/// Note.
268bool Diagnostic::isBuiltinNote(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000269 return DiagID < diag::DIAG_UPPER_LIMIT &&
270 getBuiltinDiagClass(DiagID) == CLASS_NOTE;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000271}
272
Chris Lattnere007de32009-04-15 07:01:18 +0000273/// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
274/// ID is for an extension of some sort.
275///
276bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) {
Chris Lattnere6c831d2009-04-15 16:56:26 +0000277 return DiagID < diag::DIAG_UPPER_LIMIT &&
278 getBuiltinDiagClass(DiagID) == CLASS_EXTENSION;
Chris Lattnere007de32009-04-15 07:01:18 +0000279}
280
Chris Lattner22eb9722006-06-18 05:43:12 +0000281
282/// getDescription - Given a diagnostic ID, return a description of the
283/// issue.
Chris Lattner8488c822008-11-18 07:04:44 +0000284const char *Diagnostic::getDescription(unsigned DiagID) const {
Chris Lattner6c440322009-04-16 06:07:15 +0000285 if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
286 return Info->Description;
Chris Lattner7368d582009-01-27 18:30:58 +0000287 return CustomDiagInfo->getDescription(DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000288}
289
290/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
291/// object, classify the specified diagnostic ID into a Level, consumable by
292/// the DiagnosticClient.
293Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattnere6535cf2007-12-02 01:09:57 +0000294 // Handle custom diagnostics, which cannot be mapped.
Chris Lattner4b6713e2009-01-29 17:46:13 +0000295 if (DiagID >= diag::DIAG_UPPER_LIMIT)
Chris Lattnere6535cf2007-12-02 01:09:57 +0000296 return CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000297
Chris Lattner4431a1b2007-11-30 22:53:43 +0000298 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000299 assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!");
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000300 return getDiagnosticLevel(DiagID, DiagClass);
301}
302
303/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
304/// object, classify the specified diagnostic ID into a Level, consumable by
305/// the DiagnosticClient.
306Diagnostic::Level
307Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const {
Chris Lattnerae411572006-07-05 00:55:08 +0000308 // Specific non-error diagnostics may be mapped to various levels from ignored
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000309 // to error. Errors can only be mapped to fatal.
Chris Lattnere007de32009-04-15 07:01:18 +0000310 Diagnostic::Level Result = Diagnostic::Fatal;
Mike Stump11289f42009-09-09 15:08:12 +0000311
Chris Lattner411c0ff2009-04-16 04:12:40 +0000312 // Get the mapping information, if unset, compute it lazily.
313 unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID);
314 if (MappingInfo == 0) {
315 MappingInfo = GetDefaultDiagMapping(DiagID);
316 setDiagnosticMappingInternal(DiagID, MappingInfo, false);
317 }
Mike Stump11289f42009-09-09 15:08:12 +0000318
Chris Lattner411c0ff2009-04-16 04:12:40 +0000319 switch (MappingInfo & 7) {
320 default: assert(0 && "Unknown mapping!");
Chris Lattnere007de32009-04-15 07:01:18 +0000321 case diag::MAP_IGNORE:
Chris Lattnerb8e73152009-04-16 05:04:32 +0000322 // Ignore this, unless this is an extension diagnostic and we're mapping
323 // them onto warnings or errors.
324 if (!isBuiltinExtensionDiag(DiagID) || // Not an extension
325 ExtBehavior == Ext_Ignore || // Extensions ignored anyway
326 (MappingInfo & 8) != 0) // User explicitly mapped it.
327 return Diagnostic::Ignored;
328 Result = Diagnostic::Warning;
329 if (ExtBehavior == Ext_Error) Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000330 if (Result == Diagnostic::Error && ErrorsAsFatal)
331 Result = Diagnostic::Fatal;
Chris Lattnerb8e73152009-04-16 05:04:32 +0000332 break;
Chris Lattnere007de32009-04-15 07:01:18 +0000333 case diag::MAP_ERROR:
334 Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000335 if (ErrorsAsFatal)
336 Result = Diagnostic::Fatal;
Chris Lattnere007de32009-04-15 07:01:18 +0000337 break;
338 case diag::MAP_FATAL:
339 Result = Diagnostic::Fatal;
340 break;
341 case diag::MAP_WARNING:
342 // If warnings are globally mapped to ignore or error, do it.
Chris Lattner8c800702008-05-29 15:36:45 +0000343 if (IgnoreAllWarnings)
344 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000345
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000346 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000347
Chris Lattnerb8e73152009-04-16 05:04:32 +0000348 // If this is an extension diagnostic and we're in -pedantic-error mode, and
349 // if the user didn't explicitly map it, upgrade to an error.
350 if (ExtBehavior == Ext_Error &&
351 (MappingInfo & 8) == 0 &&
352 isBuiltinExtensionDiag(DiagID))
353 Result = Diagnostic::Error;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000355 if (WarningsAsErrors)
356 Result = Diagnostic::Error;
Chris Lattner801fda82009-12-22 23:12:53 +0000357 if (Result == Diagnostic::Error && ErrorsAsFatal)
358 Result = Diagnostic::Fatal;
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000359 break;
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000361 case diag::MAP_WARNING_NO_WERROR:
362 // Diagnostics specified with -Wno-error=foo should be set to warnings, but
363 // not be adjusted by -Werror or -pedantic-errors.
364 Result = Diagnostic::Warning;
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattnerf9150ba2009-04-16 04:32:54 +0000366 // If warnings are globally mapped to ignore or error, do it.
367 if (IgnoreAllWarnings)
368 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000369
Chris Lattnere007de32009-04-15 07:01:18 +0000370 break;
Chris Lattner801fda82009-12-22 23:12:53 +0000371
372 case diag::MAP_ERROR_NO_WFATAL:
373 // Diagnostics specified as -Wno-fatal-error=foo should be errors, but
374 // unaffected by -Wfatal-errors.
375 Result = Diagnostic::Error;
376 break;
Chris Lattner8c800702008-05-29 15:36:45 +0000377 }
Chris Lattnere007de32009-04-15 07:01:18 +0000378
379 // Okay, we're about to return this as a "diagnostic to emit" one last check:
380 // if this is any sort of extension warning, and if we're in an __extension__
381 // block, silence it.
382 if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID))
383 return Diagnostic::Ignored;
Mike Stump11289f42009-09-09 15:08:12 +0000384
Chris Lattnere007de32009-04-15 07:01:18 +0000385 return Result;
Chris Lattner22eb9722006-06-18 05:43:12 +0000386}
387
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000388struct WarningOption {
389 const char *Name;
390 const short *Members;
391 const char *SubGroups;
392};
393
394#define GET_DIAG_ARRAYS
395#include "clang/Basic/DiagnosticGroups.inc"
396#undef GET_DIAG_ARRAYS
397
398// Second the table of options, sorted by name for fast binary lookup.
399static const WarningOption OptionTable[] = {
400#define GET_DIAG_TABLE
401#include "clang/Basic/DiagnosticGroups.inc"
402#undef GET_DIAG_TABLE
403};
404static const size_t OptionTableSize =
405sizeof(OptionTable) / sizeof(OptionTable[0]);
406
407static bool WarningOptionCompare(const WarningOption &LHS,
408 const WarningOption &RHS) {
409 return strcmp(LHS.Name, RHS.Name) < 0;
410}
411
412static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
413 Diagnostic &Diags) {
414 // Option exists, poke all the members of its diagnostic set.
415 if (const short *Member = Group->Members) {
416 for (; *Member != -1; ++Member)
417 Diags.setDiagnosticMapping(*Member, Mapping);
418 }
Mike Stump11289f42009-09-09 15:08:12 +0000419
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000420 // Enable/disable all subgroups along with this one.
421 if (const char *SubGroups = Group->SubGroups) {
422 for (; *SubGroups != (char)-1; ++SubGroups)
423 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
424 }
425}
426
427/// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
428/// "unknown-pragmas" to have the specified mapping. This returns true and
429/// ignores the request if "Group" was unknown, false otherwise.
430bool Diagnostic::setDiagnosticGroupMapping(const char *Group,
431 diag::Mapping Map) {
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000433 WarningOption Key = { Group, 0, 0 };
434 const WarningOption *Found =
435 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
436 WarningOptionCompare);
437 if (Found == OptionTable + OptionTableSize ||
438 strcmp(Found->Name, Group) != 0)
439 return true; // Option not found.
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000441 MapGroupMembers(Found, Map, *this);
442 return false;
443}
444
445
Chris Lattner8488c822008-11-18 07:04:44 +0000446/// ProcessDiag - This is the method used to report a diagnostic that is
447/// finally fully formed.
Douglas Gregor33834512009-06-14 07:33:30 +0000448bool Diagnostic::ProcessDiag() {
Chris Lattner427c9c12008-11-22 00:59:29 +0000449 DiagnosticInfo Info(this);
Mike Stump11289f42009-09-09 15:08:12 +0000450
Douglas Gregor2436e712009-09-17 21:32:03 +0000451 if (SuppressAllDiagnostics)
452 return false;
453
Chris Lattner22eb9722006-06-18 05:43:12 +0000454 // Figure out the diagnostic level of this message.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000455 Diagnostic::Level DiagLevel;
456 unsigned DiagID = Info.getID();
Mike Stump11289f42009-09-09 15:08:12 +0000457
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000458 // ShouldEmitInSystemHeader - True if this diagnostic should be produced even
459 // in a system header.
460 bool ShouldEmitInSystemHeader;
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000462 if (DiagID >= diag::DIAG_UPPER_LIMIT) {
463 // Handle custom diagnostics, which cannot be mapped.
464 DiagLevel = CustomDiagInfo->getLevel(DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000465
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000466 // Custom diagnostics always are emitted in system headers.
467 ShouldEmitInSystemHeader = true;
468 } else {
469 // Get the class of the diagnostic. If this is a NOTE, map it onto whatever
470 // the diagnostic level was for the previous diagnostic so that it is
471 // filtered the same as the previous diagnostic.
472 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Chris Lattnere6c831d2009-04-15 16:56:26 +0000473 if (DiagClass == CLASS_NOTE) {
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000474 DiagLevel = Diagnostic::Note;
475 ShouldEmitInSystemHeader = false; // extra consideration is needed
476 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000477 // If this is not an error and we are in a system header, we ignore it.
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000478 // Check the original Diag ID here, because we also want to ignore
479 // extensions and warnings in -Werror and -pedantic-errors modes, which
480 // *map* warnings/extensions to errors.
Chris Lattnere6c831d2009-04-15 16:56:26 +0000481 ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR;
Mike Stump11289f42009-09-09 15:08:12 +0000482
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000483 DiagLevel = getDiagnosticLevel(DiagID, DiagClass);
484 }
485 }
486
Douglas Gregor19367f52009-03-19 18:55:06 +0000487 if (DiagLevel != Diagnostic::Note) {
488 // Record that a fatal error occurred only when we see a second
489 // non-note diagnostic. This allows notes to be attached to the
490 // fatal error, but suppresses any diagnostics that follow those
491 // notes.
492 if (LastDiagLevel == Diagnostic::Fatal)
493 FatalErrorOccurred = true;
494
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000495 LastDiagLevel = DiagLevel;
Mike Stump11289f42009-09-09 15:08:12 +0000496 }
Douglas Gregor19367f52009-03-19 18:55:06 +0000497
498 // If a fatal error has already been emitted, silence all subsequent
499 // diagnostics.
500 if (FatalErrorOccurred)
Douglas Gregor33834512009-06-14 07:33:30 +0000501 return false;
Douglas Gregor19367f52009-03-19 18:55:06 +0000502
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000503 // If the client doesn't care about this message, don't issue it. If this is
504 // a note and the last real diagnostic was ignored, ignore it too.
505 if (DiagLevel == Diagnostic::Ignored ||
506 (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored))
Douglas Gregor33834512009-06-14 07:33:30 +0000507 return false;
Nico Weber4c311642008-08-10 19:59:06 +0000508
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000509 // If this diagnostic is in a system header and is not a clang error, suppress
510 // it.
511 if (SuppressSystemWarnings && !ShouldEmitInSystemHeader &&
Chris Lattner8488c822008-11-18 07:04:44 +0000512 Info.getLocation().isValid() &&
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000513 Info.getLocation().getSpellingLoc().isInSystemHeader() &&
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000514 (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) {
515 LastDiagLevel = Diagnostic::Ignored;
Douglas Gregor33834512009-06-14 07:33:30 +0000516 return false;
Chris Lattner9ee10ea2009-02-17 06:52:20 +0000517 }
Chris Lattnerd2a2c132009-02-17 06:49:55 +0000518
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000519 if (DiagLevel >= Diagnostic::Error) {
Chris Lattnerc49b9052007-05-28 00:46:44 +0000520 ErrorOccurred = true;
Chris Lattner8488c822008-11-18 07:04:44 +0000521 ++NumErrors;
Bill Wendlingda0c8a92007-06-08 19:17:38 +0000522 }
Mike Stump11289f42009-09-09 15:08:12 +0000523
Chris Lattner22eb9722006-06-18 05:43:12 +0000524 // Finally, report it.
Chris Lattner8488c822008-11-18 07:04:44 +0000525 Client->HandleDiagnostic(DiagLevel, Info);
Ted Kremenekea06ec12009-01-23 20:28:53 +0000526 if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000527
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000528 CurDiagID = ~0U;
Douglas Gregor33834512009-06-14 07:33:30 +0000529
530 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000531}
532
Nico Weber4c311642008-08-10 19:59:06 +0000533
Chris Lattner22eb9722006-06-18 05:43:12 +0000534DiagnosticClient::~DiagnosticClient() {}
Nico Weber4c311642008-08-10 19:59:06 +0000535
Chris Lattner23be0672008-11-19 06:51:40 +0000536
Chris Lattner2b786902008-11-21 07:50:02 +0000537/// ModifierIs - Return true if the specified modifier matches specified string.
538template <std::size_t StrLen>
539static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
540 const char (&Str)[StrLen]) {
541 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
542}
543
544/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
545/// like this: %select{foo|bar|baz}2. This means that the integer argument
546/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
547/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
548/// This is very useful for certain classes of variant diagnostics.
John McCalle4d54322010-01-13 23:58:20 +0000549static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000550 const char *Argument, unsigned ArgumentLen,
551 llvm::SmallVectorImpl<char> &OutStr) {
552 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattner2b786902008-11-21 07:50:02 +0000554 // Skip over 'ValNo' |'s.
555 while (ValNo) {
556 const char *NextVal = std::find(Argument, ArgumentEnd, '|');
557 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
558 " larger than the number of options in the diagnostic string!");
559 Argument = NextVal+1; // Skip this string.
560 --ValNo;
561 }
Mike Stump11289f42009-09-09 15:08:12 +0000562
Chris Lattner2b786902008-11-21 07:50:02 +0000563 // Get the end of the value. This is either the } or the |.
564 const char *EndPtr = std::find(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000565
566 // Recursively format the result of the select clause into the output string.
567 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000568}
569
570/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
571/// letter 's' to the string if the value is not 1. This is used in cases like
572/// this: "you idiot, you have %4 parameter%s4!".
573static void HandleIntegerSModifier(unsigned ValNo,
574 llvm::SmallVectorImpl<char> &OutStr) {
575 if (ValNo != 1)
576 OutStr.push_back('s');
577}
578
John McCall9015cde2010-01-14 00:50:32 +0000579/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
580/// prints the ordinal form of the given integer, with 1 corresponding
581/// to the first ordinal. Currently this is hard-coded to use the
582/// English form.
583static void HandleOrdinalModifier(unsigned ValNo,
584 llvm::SmallVectorImpl<char> &OutStr) {
585 assert(ValNo != 0 && "ValNo must be strictly positive!");
586
587 llvm::raw_svector_ostream Out(OutStr);
588
589 // We could use text forms for the first N ordinals, but the numeric
590 // forms are actually nicer in diagnostics because they stand out.
591 Out << ValNo;
592
593 // It is critically important that we do this perfectly for
594 // user-written sequences with over 100 elements.
595 switch (ValNo % 100) {
596 case 11:
597 case 12:
598 case 13:
599 Out << "th"; return;
600 default:
601 switch (ValNo % 10) {
602 case 1: Out << "st"; return;
603 case 2: Out << "nd"; return;
604 case 3: Out << "rd"; return;
605 default: Out << "th"; return;
606 }
607 }
608}
609
Chris Lattner2b786902008-11-21 07:50:02 +0000610
Sebastian Redl15b02d22008-11-22 13:44:36 +0000611/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000612static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000613 // Programming 101: Parse a decimal number :-)
614 unsigned Val = 0;
615 while (Start != End && *Start >= '0' && *Start <= '9') {
616 Val *= 10;
617 Val += *Start - '0';
618 ++Start;
619 }
620 return Val;
621}
622
623/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000624static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000625 if (*Start != '[') {
626 unsigned Ref = PluralNumber(Start, End);
627 return Ref == Val;
628 }
629
630 ++Start;
631 unsigned Low = PluralNumber(Start, End);
632 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
633 ++Start;
634 unsigned High = PluralNumber(Start, End);
635 assert(*Start == ']' && "Bad plural expression syntax: expected )");
636 ++Start;
637 return Low <= Val && Val <= High;
638}
639
640/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000641static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000642 // Empty condition?
643 if (*Start == ':')
644 return true;
645
646 while (1) {
647 char C = *Start;
648 if (C == '%') {
649 // Modulo expression
650 ++Start;
651 unsigned Arg = PluralNumber(Start, End);
652 assert(*Start == '=' && "Bad plural expression syntax: expected =");
653 ++Start;
654 unsigned ValMod = ValNo % Arg;
655 if (TestPluralRange(ValMod, Start, End))
656 return true;
657 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000658 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000659 "Bad plural expression syntax: unexpected character");
660 // Range expression
661 if (TestPluralRange(ValNo, Start, End))
662 return true;
663 }
664
665 // Scan for next or-expr part.
666 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000667 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000668 break;
669 ++Start;
670 }
671 return false;
672}
673
674/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
675/// for complex plural forms, or in languages where all plurals are complex.
676/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
677/// conditions that are tested in order, the form corresponding to the first
678/// that applies being emitted. The empty condition is always true, making the
679/// last form a default case.
680/// Conditions are simple boolean expressions, where n is the number argument.
681/// Here are the rules.
682/// condition := expression | empty
683/// empty := -> always true
684/// expression := numeric [',' expression] -> logical or
685/// numeric := range -> true if n in range
686/// | '%' number '=' range -> true if n % number in range
687/// range := number
688/// | '[' number ',' number ']' -> ranges are inclusive both ends
689///
690/// Here are some examples from the GNU gettext manual written in this form:
691/// English:
692/// {1:form0|:form1}
693/// Latvian:
694/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
695/// Gaeilge:
696/// {1:form0|2:form1|:form2}
697/// Romanian:
698/// {1:form0|0,%100=[1,19]:form1|:form2}
699/// Lithuanian:
700/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
701/// Russian (requires repeated form):
702/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
703/// Slovak
704/// {1:form0|[2,4]:form1|:form2}
705/// Polish (requires repeated form):
706/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
707static void HandlePluralModifier(unsigned ValNo,
708 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb8e73152009-04-16 05:04:32 +0000709 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000710 const char *ArgumentEnd = Argument + ArgumentLen;
711 while (1) {
712 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
713 const char *ExprEnd = Argument;
714 while (*ExprEnd != ':') {
715 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
716 ++ExprEnd;
717 }
718 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
719 Argument = ExprEnd + 1;
720 ExprEnd = std::find(Argument, ArgumentEnd, '|');
721 OutStr.append(Argument, ExprEnd);
722 return;
723 }
724 Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1;
725 }
726}
727
728
Chris Lattner23be0672008-11-19 06:51:40 +0000729/// FormatDiagnostic - Format this diagnostic into a string, substituting the
730/// formal arguments into the %0 slots. The result is appended onto the Str
731/// array.
732void DiagnosticInfo::
733FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
734 const char *DiagStr = getDiags()->getDescription(getID());
735 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump11289f42009-09-09 15:08:12 +0000736
John McCalle4d54322010-01-13 23:58:20 +0000737 FormatDiagnostic(DiagStr, DiagEnd, OutStr);
738}
739
740void DiagnosticInfo::
741FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
742 llvm::SmallVectorImpl<char> &OutStr) const {
743
Chris Lattnerc243f292009-10-20 05:25:22 +0000744 /// FormattedArgs - Keep track of all of the arguments formatted by
745 /// ConvertArgToString and pass them into subsequent calls to
746 /// ConvertArgToString, allowing the implementation to avoid redundancies in
747 /// obvious cases.
748 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
749
Chris Lattner23be0672008-11-19 06:51:40 +0000750 while (DiagStr != DiagEnd) {
751 if (DiagStr[0] != '%') {
752 // Append non-%0 substrings to Str if we have one.
753 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
754 OutStr.append(DiagStr, StrEnd);
755 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000756 continue;
Chris Lattner23be0672008-11-19 06:51:40 +0000757 } else if (DiagStr[1] == '%') {
758 OutStr.push_back('%'); // %% -> %.
759 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000760 continue;
761 }
Mike Stump11289f42009-09-09 15:08:12 +0000762
Chris Lattner2b786902008-11-21 07:50:02 +0000763 // Skip the %.
764 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000765
Chris Lattner2b786902008-11-21 07:50:02 +0000766 // This must be a placeholder for a diagnostic argument. The format for a
767 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
768 // The digit is a number from 0-9 indicating which argument this comes from.
769 // The modifier is a string of digits from the set [-a-z]+, arguments is a
770 // brace enclosed string.
771 const char *Modifier = 0, *Argument = 0;
772 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000773
Chris Lattner2b786902008-11-21 07:50:02 +0000774 // Check to see if we have a modifier. If so eat it.
775 if (!isdigit(DiagStr[0])) {
776 Modifier = DiagStr;
777 while (DiagStr[0] == '-' ||
778 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
779 ++DiagStr;
780 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000781
Chris Lattner2b786902008-11-21 07:50:02 +0000782 // If we have an argument, get it next.
783 if (DiagStr[0] == '{') {
784 ++DiagStr; // Skip {.
785 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000786
Chris Lattner2b786902008-11-21 07:50:02 +0000787 for (; DiagStr[0] != '}'; ++DiagStr)
788 assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!");
789 ArgumentLen = DiagStr-Argument;
790 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000791 }
Chris Lattner2b786902008-11-21 07:50:02 +0000792 }
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattner2b786902008-11-21 07:50:02 +0000794 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000795 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000796
Chris Lattnerc243f292009-10-20 05:25:22 +0000797 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
798
799 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000800 // ---- STRINGS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000801 case Diagnostic::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000802 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000803 assert(ModifierLen == 0 && "No modifiers for strings yet");
804 OutStr.append(S.begin(), S.end());
805 break;
806 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000807 case Diagnostic::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000808 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000809 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000810
811 // Don't crash if get passed a null pointer by accident.
812 if (!S)
813 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000814
Chris Lattner2b786902008-11-21 07:50:02 +0000815 OutStr.append(S, S + strlen(S));
816 break;
817 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000818 // ---- INTEGERS ----
Chris Lattner427c9c12008-11-22 00:59:29 +0000819 case Diagnostic::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000820 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000821
Chris Lattner2b786902008-11-21 07:50:02 +0000822 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000823 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000824 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
825 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000826 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
827 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000828 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
829 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000830 } else {
831 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000832 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000833 }
Chris Lattner2b786902008-11-21 07:50:02 +0000834 break;
835 }
Chris Lattner427c9c12008-11-22 00:59:29 +0000836 case Diagnostic::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000837 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000838
Chris Lattner2b786902008-11-21 07:50:02 +0000839 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000840 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000841 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
842 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000843 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
844 HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000845 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
846 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000847 } else {
848 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000849 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000850 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000851 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000852 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000853 // ---- NAMES and TYPES ----
854 case Diagnostic::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000855 const IdentifierInfo *II = getArgIdentifier(ArgNo);
856 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000857
858 // Don't crash if get passed a null pointer by accident.
859 if (!II) {
860 const char *S = "(null)";
861 OutStr.append(S, S + strlen(S));
862 continue;
863 }
864
Daniel Dunbar07d07852009-10-18 21:17:35 +0000865 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000866 break;
867 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000868 case Diagnostic::ak_qualtype:
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000869 case Diagnostic::ak_declarationname:
Douglas Gregor2ada0482009-02-04 17:27:36 +0000870 case Diagnostic::ak_nameddecl:
Douglas Gregor053f6912009-08-26 00:04:55 +0000871 case Diagnostic::ak_nestednamespec:
Douglas Gregore40876a2009-10-13 21:16:44 +0000872 case Diagnostic::ak_declcontext:
Chris Lattnerc243f292009-10-20 05:25:22 +0000873 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000874 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000875 Argument, ArgumentLen,
876 FormattedArgs.data(), FormattedArgs.size(),
877 OutStr);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000878 break;
Nico Weber4c311642008-08-10 19:59:06 +0000879 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000880
881 // Remember this argument info for subsequent formatting operations. Turn
882 // std::strings into a null terminated string to make it be the same case as
883 // all the other ones.
884 if (Kind != Diagnostic::ak_std_string)
885 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
886 else
887 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
888 (intptr_t)getArgStdStr(ArgNo).c_str()));
889
Nico Weber4c311642008-08-10 19:59:06 +0000890 }
Nico Weber4c311642008-08-10 19:59:06 +0000891}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000892
893/// IncludeInDiagnosticCounts - This method (whose default implementation
894/// returns true) indicates whether the diagnostics handled by this
895/// DiagnosticClient should be included in the number of diagnostics
896/// reported by Diagnostic.
897bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }