-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDiagnosticsBridging.cpp
176 lines (144 loc) · 6.28 KB
/
DiagnosticsBridging.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
//===--- Bridging/DiagnosticsBridging.cpp.cpp -----------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022-2024 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 "swift/AST/ASTBridging.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsCommon.h"
#include "swift/Basic/Assertions.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// MARK: Diagnostics
//===----------------------------------------------------------------------===//
static_assert(sizeof(BridgedDiagnosticArgument) >= sizeof(DiagnosticArgument),
"BridgedDiagnosticArgument has wrong size");
BridgedDiagnosticArgument::BridgedDiagnosticArgument(SwiftInt i)
: BridgedDiagnosticArgument(DiagnosticArgument((int)i)) {}
BridgedDiagnosticArgument::BridgedDiagnosticArgument(BridgedStringRef s)
: BridgedDiagnosticArgument(DiagnosticArgument(s.unbridged())) {}
static_assert(sizeof(BridgedDiagnosticFixIt) >= sizeof(DiagnosticInfo::FixIt),
"BridgedDiagnosticFixIt has wrong size");
const swift::DiagnosticInfo::FixIt &unbridge(const BridgedDiagnosticFixIt &fixit) {
return *reinterpret_cast<const swift::DiagnosticInfo::FixIt *>(&fixit.storage);
}
BridgedDiagnosticFixIt::BridgedDiagnosticFixIt(BridgedSourceLoc start,
uint32_t length,
BridgedStringRef text) {
DiagnosticInfo::FixIt fixit(
CharSourceRange(start.unbridged(), length), text.unbridged(),
llvm::ArrayRef<DiagnosticArgument>());
*reinterpret_cast<swift::DiagnosticInfo::FixIt *>(&storage) = fixit;
}
void BridgedDiagnosticEngine_diagnose(
BridgedDiagnosticEngine bridgedEngine, BridgedSourceLoc loc,
BridgedDiagID bridgedDiagID,
BridgedArrayRef /*BridgedDiagnosticArgument*/ bridgedArguments,
BridgedSourceLoc highlightStart, uint32_t hightlightLength,
BridgedArrayRef /*BridgedDiagnosticFixIt*/ bridgedFixIts) {
auto *D = bridgedEngine.unbridged();
auto diagID = static_cast<DiagID>(bridgedDiagID);
SmallVector<DiagnosticArgument, 2> arguments;
for (auto arg : bridgedArguments.unbridged<BridgedDiagnosticArgument>()) {
arguments.push_back(arg.unbridged());
}
auto inflight = D->diagnose(loc.unbridged(), diagID, arguments);
// Add highlight.
if (highlightStart.unbridged().isValid()) {
CharSourceRange highlight(highlightStart.unbridged(),
(unsigned)hightlightLength);
inflight.highlightChars(highlight.getStart(), highlight.getEnd());
}
// Add fix-its.
for (const BridgedDiagnosticFixIt &fixIt :
bridgedFixIts.unbridged<BridgedDiagnosticFixIt>()) {
auto range = unbridge(fixIt).getRange();
auto text = unbridge(fixIt).getText();
inflight.fixItReplaceChars(range.getStart(), range.getEnd(), text);
}
}
bool BridgedDiagnosticEngine_hadAnyError(
BridgedDiagnosticEngine bridgedEngine) {
return bridgedEngine.unbridged()->hadAnyError();
}
struct BridgedDiagnostic::Impl {
typedef llvm::MallocAllocator Allocator;
InFlightDiagnostic inFlight;
std::vector<StringRef> textBlobs;
Impl(InFlightDiagnostic inFlight, std::vector<StringRef> textBlobs)
: inFlight(std::move(inFlight)), textBlobs(std::move(textBlobs)) {}
Impl(const Impl &) = delete;
Impl(Impl &&) = delete;
Impl &operator=(const Impl &) = delete;
Impl &operator=(Impl &&) = delete;
~Impl() {
inFlight.flush();
Allocator allocator;
for (auto text : textBlobs) {
allocator.Deallocate(text.data(), text.size());
}
}
};
BridgedDiagnostic BridgedDiagnostic_create(BridgedSourceLoc cLoc,
BridgedStringRef cText,
BridgedDiagnosticSeverity severity,
BridgedDiagnosticEngine cDiags) {
StringRef origText = cText.unbridged();
BridgedDiagnostic::Impl::Allocator alloc;
StringRef text = origText.copy(alloc);
SourceLoc loc = cLoc.unbridged();
Diag<StringRef> diagID;
switch (severity) {
case BridgedDiagnosticSeverity::BridgedError:
diagID = diag::bridged_error;
break;
case BridgedDiagnosticSeverity::BridgedFatalError:
diagID = diag::bridged_fatal_error;
break;
case BridgedDiagnosticSeverity::BridgedNote:
diagID = diag::bridged_note;
break;
case BridgedDiagnosticSeverity::BridgedRemark:
diagID = diag::bridged_remark;
break;
case BridgedDiagnosticSeverity::BridgedWarning:
diagID = diag::bridged_warning;
break;
}
DiagnosticEngine &diags = *cDiags.unbridged();
return new BridgedDiagnostic::Impl{diags.diagnose(loc, diagID, text), {text}};
}
/// Highlight a source range as part of the diagnostic.
void BridgedDiagnostic_highlight(BridgedDiagnostic cDiag,
BridgedSourceLoc cStartLoc,
BridgedSourceLoc cEndLoc) {
SourceLoc startLoc = cStartLoc.unbridged();
SourceLoc endLoc = cEndLoc.unbridged();
BridgedDiagnostic::Impl *diag = cDiag.unbridged();
diag->inFlight.highlightChars(startLoc, endLoc);
}
/// Add a Fix-It to replace a source range as part of the diagnostic.
void BridgedDiagnostic_fixItReplace(BridgedDiagnostic cDiag,
BridgedSourceLoc cStartLoc,
BridgedSourceLoc cEndLoc,
BridgedStringRef cReplaceText) {
SourceLoc startLoc = cStartLoc.unbridged();
SourceLoc endLoc = cEndLoc.unbridged();
StringRef origReplaceText = cReplaceText.unbridged();
BridgedDiagnostic::Impl::Allocator alloc;
StringRef replaceText = origReplaceText.copy(alloc);
BridgedDiagnostic::Impl *diag = cDiag.unbridged();
diag->textBlobs.push_back(replaceText);
diag->inFlight.fixItReplaceChars(startLoc, endLoc, replaceText);
}
/// Finish the given diagnostic and emit it.
void BridgedDiagnostic_finish(BridgedDiagnostic cDiag) {
BridgedDiagnostic::Impl *diag = cDiag.unbridged();
delete diag;
}