blob: 155b6fca12462aea58dcd409701c2fe3d0eff84e [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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>
17using namespace clang;
18
19/// Flag values for diagnostics.
20enum {
21 // Diagnostic classes.
22 NOTE = 0x01,
23 WARNING = 0x02,
24 EXTENSION = 0x03,
25 ERROR = 0x04,
26 FATAL = 0x05,
27 class_mask = 0x07
28};
29
30/// DiagnosticFlags - A set of flags, or'd together, that describe the
31/// diagnostic.
32static unsigned char DiagnosticFlags[] = {
33#define DIAG(ENUM,FLAGS,DESC) FLAGS,
34#include "clang/Basic/DiagnosticKinds.def"
35 0
36};
37
38/// getDiagClass - Return the class field of the diagnostic.
39///
40static unsigned getDiagClass(unsigned DiagID) {
41 assert(DiagID < diag::NUM_DIAGNOSTICS && "Diagnostic ID out of range!");
42 return DiagnosticFlags[DiagID] & class_mask;
43}
44
45/// DiagnosticText - An english message to print for the diagnostic. These
46/// should be localized.
47static const char * const DiagnosticText[] = {
48#define DIAG(ENUM,FLAGS,DESC) DESC,
49#include "clang/Basic/DiagnosticKinds.def"
50 0
51};
52
53Diagnostic::Diagnostic(DiagnosticClient &client) : Client(client) {
54 WarningsAsErrors = false;
55 WarnOnExtensions = false;
56 ErrorOnExtensions = false;
57 // Clear all mappings, setting them to MAP_DEFAULT.
58 memset(DiagMappings, 0, sizeof(DiagMappings));
59
60 ErrorOccurred = false;
61 NumDiagnostics = 0;
62 NumErrors = 0;
63}
64
65/// isNoteWarningOrExtension - Return true if the unmapped diagnostic level of
66/// the specified diagnostic ID is a Note, Warning, or Extension.
67bool Diagnostic::isNoteWarningOrExtension(unsigned DiagID) {
68 return getDiagClass(DiagID) < ERROR;
69}
70
71
72/// getDescription - Given a diagnostic ID, return a description of the
73/// issue.
74const char *Diagnostic::getDescription(unsigned DiagID) {
75 assert(DiagID < diag::NUM_DIAGNOSTICS && "Diagnostic ID out of range!");
76 return DiagnosticText[DiagID];
77}
78
79/// getDiagnosticLevel - Based on the way the client configured the Diagnostic
80/// object, classify the specified diagnostic ID into a Level, consumable by
81/// the DiagnosticClient.
82Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
83 unsigned DiagClass = getDiagClass(DiagID);
84
85 // Specific non-error diagnostics may be mapped to various levels from ignored
86 // to error.
87 if (DiagClass < ERROR) {
88 switch (getDiagnosticMapping((diag::kind)DiagID)) {
89 case diag::MAP_DEFAULT: break;
90 case diag::MAP_IGNORE: return Ignored;
91 case diag::MAP_WARNING: DiagClass = WARNING; break;
92 case diag::MAP_ERROR: DiagClass = ERROR; break;
93 }
94 }
95
96 // Map diagnostic classes based on command line argument settings.
97 if (DiagClass == EXTENSION) {
98 if (ErrorOnExtensions)
99 DiagClass = ERROR;
100 else if (WarnOnExtensions)
101 DiagClass = WARNING;
102 else
103 return Ignored;
104 }
105
106 // If warnings are to be treated as errors, indicate this as such.
107 if (DiagClass == WARNING && WarningsAsErrors)
108 DiagClass = ERROR;
109
110 switch (DiagClass) {
111 default: assert(0 && "Unknown diagnostic class!");
112 case NOTE: return Diagnostic::Note;
113 case WARNING: return Diagnostic::Warning;
114 case ERROR: return Diagnostic::Error;
115 case FATAL: return Diagnostic::Fatal;
116 }
117}
118
119/// Report - Issue the message to the client. If the client wants us to stop
120/// compilation, return true, otherwise return false. DiagID is a member of
121/// the diag::kind enum.
122void Diagnostic::Report(SourceLocation Pos, unsigned DiagID,
123 const std::string *Strs, unsigned NumStrs,
124 const SourceRange *Ranges, unsigned NumRanges) {
125 // Figure out the diagnostic level of this message.
126 Diagnostic::Level DiagLevel = getDiagnosticLevel(DiagID);
127
128 // If the client doesn't care about this message, don't issue it.
129 if (DiagLevel == Diagnostic::Ignored)
130 return;
131
132 if (DiagLevel >= Diagnostic::Error) {
133 ErrorOccurred = true;
134 ++NumErrors;
135 }
136
137 // Are we going to ignore this diagnosic?
138 if (Client.IgnoreDiagnostic(DiagLevel, Pos))
139 return;
140
141 // Finally, report it.
142 Client.HandleDiagnostic(DiagLevel, Pos, (diag::kind)DiagID, Strs, NumStrs,
143 Ranges, NumRanges);
144 ++NumDiagnostics;
145}
146
147DiagnosticClient::~DiagnosticClient() {}