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