-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathASTBridging.cpp
74 lines (63 loc) · 2.55 KB
/
ASTBridging.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
//===--- ASTBridging.cpp - AST bridging functions -------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 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/Basic/BridgingUtils.h"
using namespace swift;
namespace {
/// BridgedDiagnosticEngine -> DiagnosticEngine *.
DiagnosticEngine *getDiagnosticEngine(const BridgedDiagnosticEngine &bridged) {
return static_cast<DiagnosticEngine *>(bridged.object);
}
/// BridgedDiagnosticArgument -> DiagnosticArgument
DiagnosticArgument
getDiagnosticArgument(const BridgedDiagnosticArgument &bridged) {
switch (bridged.kind) {
case BridgedDiagnosticArgumentKind_StringRef:
return {getStringRef(bridged.value.stringRefValue)};
case BridgedDiagnosticArgumentKind_Int:
return {(int)bridged.value.intValue};
}
llvm_unreachable("unhandled enum value");
}
} // namespace
void DiagnosticEngine_diagnose(
BridgedDiagnosticEngine bridgedEngine, SourceLoc loc,
BridgedDiagID bridgedDiagID,
BridgedArrayRef /*BridgedDiagnosticArgument*/ bridgedArguments,
BridgedCharSourceRange bridgedHighlight,
BridgedArrayRef /*BridgedDiagnosticFixIt*/ bridgedFixIts) {
auto *D = getDiagnosticEngine(bridgedEngine);
auto diagID = static_cast<DiagID>(bridgedDiagID);
SmallVector<DiagnosticArgument, 2> arguments;
for (auto bridgedArg :
getArrayRef<BridgedDiagnosticArgument>(bridgedArguments)) {
arguments.push_back(getDiagnosticArgument(bridgedArg));
}
auto inflight = D->diagnose(loc, diagID, arguments);
// Add highlight.
auto highlight = getCharSourceRange(bridgedHighlight);
if (highlight.isValid()) {
inflight.highlightChars(highlight.getStart(), highlight.getEnd());
}
// Add fix-its.
for (auto bridgedFixIt : getArrayRef<BridgedDiagnosticFixIt>(bridgedFixIts)) {
auto range = CharSourceRange(bridgedFixIt.start,
bridgedFixIt.byteLength);
auto text = getStringRef(bridgedFixIt.text);
inflight.fixItReplaceChars(range.getStart(), range.getEnd(), text);
}
}
bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine bridgedEngine) {
auto *D = getDiagnosticEngine(bridgedEngine);
return D->hadAnyError();
}