blob: fbf118f27578da653a6f7fb2852af37f7e2e7daa [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Diagnostic.h"
15#include "clang/Basic/SourceLocation.h"
16#include <cassert>
Chris Lattner182745a2007-12-02 01:09:57 +000017#include <vector>
18#include <map>
Chris Lattner87cf5ac2008-03-10 17:04:53 +000019#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Chris Lattner182745a2007-12-02 01:09:57 +000022//===----------------------------------------------------------------------===//
23// Builtin Diagnostic information
24//===----------------------------------------------------------------------===//
25
Reid Spencer5f016e22007-07-11 17:01:13 +000026/// Flag values for diagnostics.
27enum {
28 // Diagnostic classes.
29 NOTE = 0x01,
30 WARNING = 0x02,
31 EXTENSION = 0x03,
32 ERROR = 0x04,
Reid Spencer5f016e22007-07-11 17:01:13 +000033 class_mask = 0x07
34};
35
36/// DiagnosticFlags - A set of flags, or'd together, that describe the
37/// diagnostic.
38static unsigned char DiagnosticFlags[] = {
39#define DIAG(ENUM,FLAGS,DESC) FLAGS,
40#include "clang/Basic/DiagnosticKinds.def"
41 0
42};
43
44/// getDiagClass - Return the class field of the diagnostic.
45///
Chris Lattner07506182007-11-30 22:53:43 +000046static unsigned getBuiltinDiagClass(unsigned DiagID) {
47 assert(DiagID < diag::NUM_BUILTIN_DIAGNOSTICS &&
48 "Diagnostic ID out of range!");
Reid Spencer5f016e22007-07-11 17:01:13 +000049 return DiagnosticFlags[DiagID] & class_mask;
50}
51
52/// DiagnosticText - An english message to print for the diagnostic. These
53/// should be localized.
54static const char * const DiagnosticText[] = {
55#define DIAG(ENUM,FLAGS,DESC) DESC,
56#include "clang/Basic/DiagnosticKinds.def"
57 0
58};
59
Chris Lattner182745a2007-12-02 01:09:57 +000060//===----------------------------------------------------------------------===//
61// Custom Diagnostic information
62//===----------------------------------------------------------------------===//
63
64namespace clang {
65 namespace diag {
66 class CustomDiagInfo {
67 typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
68 std::vector<DiagDesc> DiagInfo;
69 std::map<DiagDesc, unsigned> DiagIDs;
70 public:
71
72 /// getDescription - Return the description of the specified custom
73 /// diagnostic.
74 const char *getDescription(unsigned DiagID) const {
75 assert(this && DiagID-diag::NUM_BUILTIN_DIAGNOSTICS < DiagInfo.size() &&
76 "Invalid diagnosic ID");
77 return DiagInfo[DiagID-diag::NUM_BUILTIN_DIAGNOSTICS].second.c_str();
78 }
79
80 /// getLevel - Return the level of the specified custom diagnostic.
81 Diagnostic::Level getLevel(unsigned DiagID) const {
82 assert(this && DiagID-diag::NUM_BUILTIN_DIAGNOSTICS < DiagInfo.size() &&
83 "Invalid diagnosic ID");
84 return DiagInfo[DiagID-diag::NUM_BUILTIN_DIAGNOSTICS].first;
85 }
86
87 unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message) {
88 DiagDesc D(L, Message);
89 // Check to see if it already exists.
90 std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
91 if (I != DiagIDs.end() && I->first == D)
92 return I->second;
93
94 // If not, assign a new ID.
95 unsigned ID = DiagInfo.size()+diag::NUM_BUILTIN_DIAGNOSTICS;
96 DiagIDs.insert(std::make_pair(D, ID));
97 DiagInfo.push_back(D);
98 return ID;
99 }
100 };
101
102 } // end diag namespace
103} // end clang namespace
104
105
106//===----------------------------------------------------------------------===//
107// Common Diagnostic implementation
108//===----------------------------------------------------------------------===//
109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110Diagnostic::Diagnostic(DiagnosticClient &client) : Client(client) {
Chris Lattner5b4681c2008-05-29 15:36:45 +0000111 IgnoreAllWarnings = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 WarningsAsErrors = false;
113 WarnOnExtensions = false;
114 ErrorOnExtensions = false;
115 // Clear all mappings, setting them to MAP_DEFAULT.
116 memset(DiagMappings, 0, sizeof(DiagMappings));
117
118 ErrorOccurred = false;
119 NumDiagnostics = 0;
120 NumErrors = 0;
Chris Lattner182745a2007-12-02 01:09:57 +0000121 CustomDiagInfo = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122}
123
Chris Lattner182745a2007-12-02 01:09:57 +0000124Diagnostic::~Diagnostic() {
125 delete CustomDiagInfo;
126}
127
128/// getCustomDiagID - Return an ID for a diagnostic with the specified message
129/// and level. If this is the first request for this diagnosic, it is
130/// registered and created, otherwise the existing ID is returned.
131unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
132 if (CustomDiagInfo == 0)
133 CustomDiagInfo = new diag::CustomDiagInfo();
134 return CustomDiagInfo->getOrCreateDiagID(L, Message);
135}
136
137
Chris Lattner07506182007-11-30 22:53:43 +0000138/// isBuiltinNoteWarningOrExtension - Return true if the unmapped diagnostic
139/// level of the specified diagnostic ID is a Note, Warning, or Extension.
140/// Note that this only works on builtin diagnostics, not custom ones.
141bool Diagnostic::isBuiltinNoteWarningOrExtension(unsigned DiagID) {
142 return DiagID < diag::NUM_BUILTIN_DIAGNOSTICS &&
143 getBuiltinDiagClass(DiagID) < ERROR;
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
146
147/// getDescription - Given a diagnostic ID, return a description of the
148/// issue.
149const char *Diagnostic::getDescription(unsigned DiagID) {
Chris Lattner07506182007-11-30 22:53:43 +0000150 if (DiagID < diag::NUM_BUILTIN_DIAGNOSTICS)
151 return DiagnosticText[DiagID];
152 else
Chris Lattner182745a2007-12-02 01:09:57 +0000153 return CustomDiagInfo->getDescription(DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000154}
155
156/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
157/// object, classify the specified diagnostic ID into a Level, consumable by
158/// the DiagnosticClient.
159Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
Chris Lattner182745a2007-12-02 01:09:57 +0000160 // Handle custom diagnostics, which cannot be mapped.
161 if (DiagID >= diag::NUM_BUILTIN_DIAGNOSTICS)
162 return CustomDiagInfo->getLevel(DiagID);
Chris Lattner07506182007-11-30 22:53:43 +0000163
164 unsigned DiagClass = getBuiltinDiagClass(DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165
166 // Specific non-error diagnostics may be mapped to various levels from ignored
167 // to error.
168 if (DiagClass < ERROR) {
169 switch (getDiagnosticMapping((diag::kind)DiagID)) {
170 case diag::MAP_DEFAULT: break;
Chris Lattner5b4681c2008-05-29 15:36:45 +0000171 case diag::MAP_IGNORE: return Diagnostic::Ignored;
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 case diag::MAP_WARNING: DiagClass = WARNING; break;
173 case diag::MAP_ERROR: DiagClass = ERROR; break;
174 }
175 }
176
177 // Map diagnostic classes based on command line argument settings.
178 if (DiagClass == EXTENSION) {
179 if (ErrorOnExtensions)
180 DiagClass = ERROR;
181 else if (WarnOnExtensions)
182 DiagClass = WARNING;
183 else
184 return Ignored;
185 }
186
Chris Lattner5b4681c2008-05-29 15:36:45 +0000187 // If warnings are globally mapped to ignore or error, do it.
188 if (DiagClass == WARNING) {
189 if (IgnoreAllWarnings)
190 return Diagnostic::Ignored;
191 if (WarningsAsErrors)
192 DiagClass = ERROR;
193 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000194
195 switch (DiagClass) {
196 default: assert(0 && "Unknown diagnostic class!");
197 case NOTE: return Diagnostic::Note;
198 case WARNING: return Diagnostic::Warning;
199 case ERROR: return Diagnostic::Error;
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 }
201}
202
203/// Report - Issue the message to the client. If the client wants us to stop
204/// compilation, return true, otherwise return false. DiagID is a member of
205/// the diag::kind enum.
Ted Kremenek615f5172008-03-31 18:23:15 +0000206void Diagnostic::Report(DiagnosticClient* C,
207 FullSourceLoc Pos, unsigned DiagID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 const std::string *Strs, unsigned NumStrs,
209 const SourceRange *Ranges, unsigned NumRanges) {
Chris Lattner7097d912008-02-03 09:00:04 +0000210
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 // Figure out the diagnostic level of this message.
212 Diagnostic::Level DiagLevel = getDiagnosticLevel(DiagID);
213
214 // If the client doesn't care about this message, don't issue it.
215 if (DiagLevel == Diagnostic::Ignored)
216 return;
Ted Kremenekd0734e52008-04-14 21:21:38 +0000217
218 // Set the diagnostic client if it isn't set already.
219 if (!C) C = &Client;
Chris Lattner7097d912008-02-03 09:00:04 +0000220
221 // If this is not an error and we are in a system header, ignore it. We have
222 // to check on the original class here, because we also want to ignore
223 // extensions and warnings in -Werror and -pedantic-errors modes, which *map*
224 // warnings/extensions to errors.
225 if (DiagID < diag::NUM_BUILTIN_DIAGNOSTICS &&
226 getBuiltinDiagClass(DiagID) != ERROR &&
227 Client.isInSystemHeader(Pos))
228 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000229
230 if (DiagLevel >= Diagnostic::Error) {
231 ErrorOccurred = true;
Ted Kremenek06bdb3a2008-04-14 19:56:12 +0000232
233 if (C == &Client)
234 ++NumErrors;
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 }
236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 // Finally, report it.
Ted Kremenek615f5172008-03-31 18:23:15 +0000238
Ted Kremenek615f5172008-03-31 18:23:15 +0000239 C->HandleDiagnostic(*this, DiagLevel, Pos, (diag::kind)DiagID,
240 Strs, NumStrs, Ranges, NumRanges);
Ted Kremenek06bdb3a2008-04-14 19:56:12 +0000241
242 if (C == &Client)
243 ++NumDiagnostics;
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
246DiagnosticClient::~DiagnosticClient() {}