forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClangDiagnosticConsumer.cpp
232 lines (202 loc) · 9.05 KB
/
ClangDiagnosticConsumer.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//===--- ClangDiagnosticConsumer.cpp - Handle Clang diagnostics -----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "ClangDiagnosticConsumer.h"
#include "ImporterImpl.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsClangImporter.h"
#include "clang/AST/ASTContext.h"
#include "clang/Frontend/DiagnosticRenderer.h"
#include "clang/Lex/LexDiagnostic.h"
#include "llvm/ADT/STLExtras.h"
using namespace swift;
namespace {
class ClangDiagRenderer final : public clang::DiagnosticNoteRenderer {
const llvm::function_ref<void(clang::FullSourceLoc,
clang::DiagnosticsEngine::Level,
StringRef)> callback;
public:
ClangDiagRenderer(const clang::ASTContext &ctx, decltype(callback) fn)
: DiagnosticNoteRenderer(ctx.getLangOpts(),
&ctx.getDiagnostics().getDiagnosticOptions()),
callback(fn) {}
private:
void emitDiagnosticMessage(clang::SourceLocation Loc,
clang::PresumedLoc PLoc,
clang::DiagnosticsEngine::Level Level,
StringRef Message,
ArrayRef<clang::CharSourceRange> Ranges,
const clang::SourceManager *SM,
clang::DiagOrStoredDiag Info) override {
StringRef bufName = StringRef(SM->getBufferName(Loc));
if (bufName == ClangImporter::Implementation::moduleImportBufferName ||
bufName == ClangImporter::Implementation::bridgingHeaderBufferName) {
return;
}
callback(clang::FullSourceLoc(Loc, *SM), Level, Message);
}
void emitDiagnosticLoc(clang::SourceLocation Loc, clang::PresumedLoc PLoc,
clang::DiagnosticsEngine::Level Level,
ArrayRef<clang::CharSourceRange> Ranges,
const clang::SourceManager &SM) override {}
void emitCodeContext(clang::SourceLocation Loc,
clang::DiagnosticsEngine::Level Level,
SmallVectorImpl<clang::CharSourceRange>& Ranges,
ArrayRef<clang::FixItHint> Hints,
const clang::SourceManager &SM) override {}
void emitNote(clang::SourceLocation Loc, StringRef Message,
const clang::SourceManager *SM) override {
// We get invalid note locations when trying to describe where a module
// is imported and the actual location is in Swift.
if (Loc.isInvalid())
return;
emitDiagnosticMessage(Loc, {}, clang::DiagnosticsEngine::Note, Message,
{}, SM, {});
}
};
} // end anonymous namespace
ClangDiagnosticConsumer::ClangDiagnosticConsumer(
ClangImporter::Implementation &impl,
clang::DiagnosticOptions &clangDiagOptions,
bool dumpToStderr)
: TextDiagnosticPrinter(llvm::errs(), &clangDiagOptions),
ImporterImpl(impl), DumpToStderr(dumpToStderr) {}
static SourceLoc findEndOfLine(SourceManager &SM, SourceLoc loc,
unsigned bufferID) {
CharSourceRange entireBuffer = SM.getRangeForBuffer(bufferID);
CharSourceRange rangeFromLoc{SM, loc, entireBuffer.getEnd()};
StringRef textFromLoc = SM.extractText(rangeFromLoc);
size_t newlineOffset = textFromLoc.find_first_of({"\r\n\0", 3});
if (newlineOffset == StringRef::npos)
return entireBuffer.getEnd();
return loc.getAdvancedLoc(newlineOffset);
}
SourceLoc ClangDiagnosticConsumer::resolveSourceLocation(
const clang::SourceManager &clangSrcMgr,
clang::SourceLocation clangLoc) {
SourceManager &swiftSrcMgr = ImporterImpl.SwiftContext.SourceMgr;
SourceLoc loc;
clangLoc = clangSrcMgr.getFileLoc(clangLoc);
auto decomposedLoc = clangSrcMgr.getDecomposedLoc(clangLoc);
if (decomposedLoc.first.isInvalid())
return loc;
auto buffer = clangSrcMgr.getBuffer(decomposedLoc.first);
unsigned mirrorID;
auto mirrorIter = mirroredBuffers.find(buffer);
if (mirrorIter != mirroredBuffers.end()) {
mirrorID = mirrorIter->second;
} else {
std::unique_ptr<llvm::MemoryBuffer> mirrorBuffer{
llvm::MemoryBuffer::getMemBuffer(buffer->getBuffer(),
buffer->getBufferIdentifier(),
/*RequiresNullTerminator=*/true)
};
mirrorID = swiftSrcMgr.addNewSourceBuffer(std::move(mirrorBuffer));
mirroredBuffers[buffer] = mirrorID;
}
loc = swiftSrcMgr.getLocForOffset(mirrorID, decomposedLoc.second);
auto presumedLoc = clangSrcMgr.getPresumedLoc(clangLoc);
if (!presumedLoc.getFilename())
return loc;
if (presumedLoc.getLine() == 0)
return SourceLoc();
unsigned bufferLineNumber =
clangSrcMgr.getLineNumber(decomposedLoc.first, decomposedLoc.second);
StringRef presumedFile = presumedLoc.getFilename();
SourceLoc startOfLine = loc.getAdvancedLoc(-presumedLoc.getColumn() + 1);
bool isNewVirtualFile =
swiftSrcMgr.openVirtualFile(startOfLine, presumedFile,
presumedLoc.getLine() - bufferLineNumber);
if (isNewVirtualFile) {
SourceLoc endOfLine = findEndOfLine(swiftSrcMgr, loc, mirrorID);
swiftSrcMgr.closeVirtualFile(endOfLine);
}
using SourceManagerRef = llvm::IntrusiveRefCntPtr<const clang::SourceManager>;
auto iter = std::lower_bound(sourceManagersWithDiagnostics.begin(),
sourceManagersWithDiagnostics.end(),
&clangSrcMgr,
[](const SourceManagerRef &inArray,
const clang::SourceManager *toInsert) {
return std::less<const clang::SourceManager *>()(inArray.get(), toInsert);
});
if (iter->get() != &clangSrcMgr)
sourceManagersWithDiagnostics.insert(iter, &clangSrcMgr);
return loc;
}
void ClangDiagnosticConsumer::HandleDiagnostic(
clang::DiagnosticsEngine::Level clangDiagLevel,
const clang::Diagnostic &clangDiag) {
// Handle the module-not-found diagnostic specially if it's a top-level module
// we're looking for.
if (clangDiag.getID() == clang::diag::err_module_not_found &&
CurrentImport && clangDiag.getArgStdStr(0) == CurrentImport->getName()) {
return;
}
const ASTContext &ctx = ImporterImpl.SwiftContext;
if (clangDiag.getID() == clang::diag::err_module_not_built &&
CurrentImport && clangDiag.getArgStdStr(0) == CurrentImport->getName()) {
SourceLoc loc = DiagLoc;
if (clangDiag.getLocation().isValid())
loc = resolveSourceLocation(clangDiag.getSourceManager(),
clangDiag.getLocation());
ctx.Diags.diagnose(loc, diag::clang_cannot_build_module,
ctx.LangOpts.EnableObjCInterop,
CurrentImport->getName());
return;
}
// Satisfy the default implementation (bookkeeping).
if (DumpToStderr)
TextDiagnosticPrinter::HandleDiagnostic(clangDiagLevel, clangDiag);
else
DiagnosticConsumer::HandleDiagnostic(clangDiagLevel, clangDiag);
// FIXME: Map over source ranges in the diagnostic.
auto emitDiag = [&ctx, this](clang::FullSourceLoc clangNoteLoc,
clang::DiagnosticsEngine::Level clangDiagLevel,
StringRef message) {
decltype(diag::error_from_clang) diagKind;
switch (clangDiagLevel) {
case clang::DiagnosticsEngine::Ignored:
return;
case clang::DiagnosticsEngine::Note:
diagKind = diag::note_from_clang;
break;
case clang::DiagnosticsEngine::Remark:
// FIXME: We don't handle remarks yet.
return;
case clang::DiagnosticsEngine::Warning:
diagKind = diag::warning_from_clang;
break;
case clang::DiagnosticsEngine::Error:
case clang::DiagnosticsEngine::Fatal:
// FIXME: What happens after a fatal error in the importer?
diagKind = diag::error_from_clang;
break;
}
SourceLoc noteLoc;
if (clangNoteLoc.isValid())
noteLoc = resolveSourceLocation(clangNoteLoc.getManager(),
clangNoteLoc);
ctx.Diags.diagnose(noteLoc, diagKind, message);
};
llvm::SmallString<128> message;
clangDiag.FormatDiagnostic(message);
if (clangDiag.getLocation().isInvalid()) {
// Diagnostic about the compiler arguments.
emitDiag(clang::FullSourceLoc(), clangDiagLevel, message);
} else {
assert(clangDiag.hasSourceManager());
ClangDiagRenderer renderer(ImporterImpl.getClangASTContext(), emitDiag);
renderer.emitDiagnostic(clangDiag.getLocation(), clangDiagLevel, message,
clangDiag.getRanges(), clangDiag.getFixItHints(),
&clangDiag.getSourceManager());
}
}