-
Notifications
You must be signed in to change notification settings - Fork 10
/
Diagnostics.cpp
156 lines (128 loc) · 4.05 KB
/
Diagnostics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// Utilities related to diagnostics reporting for ccons.
//
// Part of ccons, the interactive console for the C programming language.
//
// Copyright (c) 2009 Alexei Svitkine. This file is distributed under the
// terms of MIT Open Source License. See file LICENSE for details.
//
#include "Diagnostics.h"
#include <clang/Basic/SourceManager.h>
#include <clang/Lex/LexDiagnostic.h>
#include <clang/Sema/SemaDiagnostic.h>
namespace ccons {
//
// DiagnosticsProvider
//
namespace {
class DiagnosticsProviderConsumer : public clang::DiagnosticConsumer {
public:
DiagnosticsProviderConsumer(DiagnosticsProvider *dp)
: _dp(dp)
{
}
void BeginSourceFile(const clang::LangOptions& opts,
const clang::Preprocessor *pp)
{
_dp->BeginSourceFile(opts, pp);
}
void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic& Info)
{
_dp->HandleDiagnostic(DiagLevel, Info);
}
clang::DiagnosticConsumer *clone(clang::DiagnosticsEngine& engine) const
{
return new DiagnosticsProviderConsumer(_dp);
}
private:
DiagnosticsProvider *_dp;
};
}
DiagnosticsProvider::DiagnosticsProvider(llvm::raw_os_ostream& out)
: _dop(new clang::DiagnosticOptions)
, _tdp(out, &*_dop)
, _diagIDs(new clang::DiagnosticIDs())
, _engine(_diagIDs, &*_dop, new DiagnosticsProviderConsumer(this))
{
_dop->ShowColumn = 0;
_dop->ShowSourceRanges = 1;
_engine.setDiagnosticMapping((clang::diag::kind)clang::diag::ext_implicit_function_decl,
clang::diag::MAP_ERROR, clang::SourceLocation());
_engine.setDiagnosticMapping((clang::diag::kind)clang::diag::warn_unused_expr,
clang::diag::MAP_IGNORE, clang::SourceLocation());
_engine.setDiagnosticMapping((clang::diag::kind)clang::diag::warn_missing_prototype,
clang::diag::MAP_IGNORE, clang::SourceLocation());
_engine.setDiagnosticMapping((clang::diag::kind)clang::diag::pp_macro_not_used,
clang::diag::MAP_IGNORE, clang::SourceLocation());
_engine.setSuppressSystemWarnings(true);
}
void DiagnosticsProvider::BeginSourceFile(const clang::LangOptions& opts, const clang::Preprocessor *pp)
{
_tdp.BeginSourceFile(opts, pp);
}
void DiagnosticsProvider::HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic& Info)
{
std::pair<clang::diag::kind, unsigned> record = std::make_pair(Info.getID(),
Info.getSourceManager().getFileOffset(Info.getLocation()) - _offs);
if (_memory.insert(record).second) {
_tdp.HandleDiagnostic(DiagLevel, Info);
}
}
void DiagnosticsProvider::setOffset(unsigned offset)
{
_offs = offset;
}
clang::DiagnosticsEngine * DiagnosticsProvider::getDiagnosticsEngine()
{
return &_engine;
}
//
// ProxyDiagnosticConsumer
//
ProxyDiagnosticConsumer::ProxyDiagnosticConsumer(clang::DiagnosticConsumer *DC)
: _DC(DC)
{
}
void ProxyDiagnosticConsumer::HandleDiagnostic(
clang::DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic& Info)
{
if (DiagLevel == clang::DiagnosticsEngine::Error)
_errors.insert(std::make_pair(Info.getID(), Info));
if (_DC)
_DC->HandleDiagnostic(DiagLevel, Info);
}
bool ProxyDiagnosticConsumer::hadError(clang::diag::kind Kind) const
{
return _errors.find(Kind) != _errors.end();
}
bool ProxyDiagnosticConsumer::hadErrors() const
{
return !_errors.empty();
}
clang::DiagnosticConsumer * ProxyDiagnosticConsumer::clone(clang::DiagnosticsEngine& engine) const
{
ProxyDiagnosticConsumer *clone = new ProxyDiagnosticConsumer(_DC);
clone->_errors = _errors;
return clone;
}
//
// NullDiagnosticProvider
//
NullDiagnosticProvider::NullDiagnosticProvider()
: _dop(new clang::DiagnosticOptions)
, _diagnosticIDs(new clang::DiagnosticIDs())
, _engine(_diagnosticIDs, &*_dop, _pdc = new ProxyDiagnosticConsumer(NULL))
{
}
clang::DiagnosticsEngine * NullDiagnosticProvider::getDiagnosticsEngine()
{
return &_engine;
}
ProxyDiagnosticConsumer * NullDiagnosticProvider::getProxyDiagnosticConsumer()
{
return _pdc;
}
} // namespace ccons