forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswift-remoteast-test.cpp
159 lines (131 loc) · 4.96 KB
/
swift-remoteast-test.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
//===--- swift-remoteast-test.cpp - RemoteAST testing application ---------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// This file supports performing target-specific remote reflection tests
// on live swift executables.
//===----------------------------------------------------------------------===//
#include "swift/RemoteAST/RemoteAST.h"
#include "swift/Remote/InProcessMemoryReader.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Frontend/Frontend.h"
#include "swift/FrontendTool/FrontendTool.h"
#include "swift/Basic/LLVM.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
using namespace swift;
using namespace swift::remote;
using namespace swift::remoteAST;
/// The context for the code we're running. Set by the observer.
static ASTContext *Context = nullptr;
// FIXME: swiftcall
/// func printType(forMetadata: Any.Type)
LLVM_ATTRIBUTE_USED
extern "C" void printMetadataType(const Metadata *typeMetadata) {
assert(Context && "context was not set");
std::shared_ptr<MemoryReader> reader(new InProcessMemoryReader());
RemoteASTContext remoteAST(*Context, std::move(reader));
auto &out = llvm::outs();
auto result =
remoteAST.getTypeForRemoteTypeMetadata(RemoteAddress(typeMetadata));
if (result) {
out << "found type: ";
result.getValue().print(out);
out << '\n';
} else {
out << result.getFailure().render() << '\n';
}
}
// FIXME: swiftcall
/// func printDynamicType(_: AnyObject)
LLVM_ATTRIBUTE_USED
extern "C" void printHeapMetadataType(void *object) {
assert(Context && "context was not set");
std::shared_ptr<MemoryReader> reader(new InProcessMemoryReader());
RemoteASTContext remoteAST(*Context, std::move(reader));
auto &out = llvm::outs();
auto metadataResult =
remoteAST.getHeapMetadataForObject(RemoteAddress(object));
if (!metadataResult) {
out << metadataResult.getFailure().render() << '\n';
return;
}
auto metadata = metadataResult.getValue();
auto result =
remoteAST.getTypeForRemoteTypeMetadata(metadata, /*skipArtificial*/ true);
if (result) {
out << "found type: ";
result.getValue().print(out);
out << '\n';
} else {
out << result.getFailure().render() << '\n';
}
}
static void printMemberOffset(const Metadata *typeMetadata,
StringRef memberName, bool passMetadata) {
assert(Context && "context was not set");
std::shared_ptr<MemoryReader> reader(new InProcessMemoryReader());
RemoteASTContext remoteAST(*Context, std::move(reader));
auto &out = llvm::outs();
// The first thing we have to do is get the type.
auto typeResult =
remoteAST.getTypeForRemoteTypeMetadata(RemoteAddress(typeMetadata));
if (!typeResult) {
out << "failed to find type: " << typeResult.getFailure().render() << '\n';
return;
}
Type type = typeResult.getValue();
RemoteAddress address =
(passMetadata ? RemoteAddress(typeMetadata) : RemoteAddress(nullptr));
auto offsetResult =
remoteAST.getOffsetOfMember(type, address, memberName);
if (!offsetResult) {
out << "failed to find offset: "
<< offsetResult.getFailure().render() << '\n';
return;
}
out << "found offset: " << offsetResult.getValue() << '\n';
}
// FIXME: swiftcall
/// func printTypeMemberOffset(forType: Any.Type, memberName: StaticString)
LLVM_ATTRIBUTE_USED
extern "C" void printTypeMemberOffset(const Metadata *typeMetadata,
const char *memberName) {
printMemberOffset(typeMetadata, memberName, /*pass metadata*/ false);
}
// FIXME: swiftcall
/// func printTypeMetadataMemberOffset(forType: Any.Type,
/// memberName: StaticString)
LLVM_ATTRIBUTE_USED
extern "C" void printTypeMetadataMemberOffset(const Metadata *typeMetadata,
const char *memberName) {
printMemberOffset(typeMetadata, memberName, /*pass metadata*/ true);
}
namespace {
struct Observer : public FrontendObserver {
void aboutToRunImmediately(CompilerInstance &instance) override {
Context = &instance.getASTContext();
}
};
} // end anonymous namespace
int main(int argc, const char *argv[]) {
unsigned numForwardedArgs = argc
- 1 // we drop argv[0]
+ 1; // -interpret
SmallVector<const char *, 8> forwardedArgs;
forwardedArgs.reserve(numForwardedArgs);
forwardedArgs.append(&argv[1], &argv[argc]);
forwardedArgs.push_back("-interpret");
assert(forwardedArgs.size() == numForwardedArgs);
Observer observer;
return performFrontend(forwardedArgs, argv[0], (void*) &printMetadataType,
&observer);
}