Skip to content

Commit 107e687

Browse files
committed
Allow plugging in observer
1 parent cf1b1ff commit 107e687

File tree

3 files changed

+154
-5
lines changed

3 files changed

+154
-5
lines changed

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ message("Using Clang_CONFIG: ${LLVM_CONFIG}")
1010
message("Using Swift_CONFIG: ${Clang_CONFIG}")
1111

1212
add_executable(codeql-swift-artifacts empty.cpp)
13-
target_link_libraries(codeql-swift-artifacts PRIVATE LLVMSupport swiftFrontendTool swiftCompilerModules)
13+
target_link_libraries(codeql-swift-artifacts PRIVATE LLVMSupport swiftPluggableObserverDriverTool swiftCompilerModules)
1414

1515
if(APPLE)
1616
execute_process(
@@ -31,4 +31,3 @@ if(APPLE)
3131
target_link_directories(codeql-swift-artifacts PUBLIC ${TOOLCHAIN_LIB_DIR})
3232
target_link_directories(codeql-swift-artifacts PUBLIC ${CODEQL_MACOS_SDK_PATH}/usr/lib/swift)
3333
endif()
34-
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
diff --git a/include/swift/DriverTool/FrontendObserver.h b/include/swift/DriverTool/FrontendObserver.h
2+
new file mode 100644
3+
index 00000000000..4ac9b299a13
4+
--- /dev/null
5+
+++ b/include/swift/DriverTool/FrontendObserver.h
6+
@@ -0,0 +1,10 @@
7+
+#pragma once
8+
+
9+
+#include "llvm/ADT/ArrayRef.h"
10+
+#include "swift/FrontendTool/FrontendTool.h"
11+
+
12+
+namespace swift {
13+
+
14+
+FrontendObserver* getFrontendObserver(llvm::ArrayRef<const char*> argv);
15+
+
16+
+} // namespace swift
17+
diff --git a/include/swift/FrontendTool/FrontendTool.h b/include/swift/FrontendTool/FrontendTool.h
18+
index 184e6196918..ef5c3eafe69 100644
19+
--- a/include/swift/FrontendTool/FrontendTool.h
20+
+++ b/include/swift/FrontendTool/FrontendTool.h
21+
@@ -53,6 +53,9 @@ public:
22+
/// The frontend has executed the SIL optimization and diagnostics pipelines.
23+
virtual void performedSILProcessing(SILModule &module);
24+
25+
+ /// The frontend has finished executing with the given return value
26+
+ virtual void finished(int status);
27+
+
28+
// TODO: maybe enhance this interface to hear about IRGen and LLVM
29+
// progress.
30+
};
31+
diff --git a/lib/DriverTool/CMakeLists.txt b/lib/DriverTool/CMakeLists.txt
32+
index 869c00fece9..8787598e2bf 100644
33+
--- a/lib/DriverTool/CMakeLists.txt
34+
+++ b/lib/DriverTool/CMakeLists.txt
35+
@@ -9,21 +9,34 @@ set(driver_sources_and_options
36+
37+
set(driver_common_libs
38+
swiftAPIDigester
39+
+ swiftCompilerModules
40+
swiftDriver
41+
swiftFrontendTool
42+
swiftSymbolGraphGen
43+
LLVMBitstreamReader)
44+
45+
-add_swift_host_library(swiftDriverTool STATIC
46+
+add_swift_host_library(swiftFrontendObserver STATIC
47+
+ swift_frontend_observer.cpp)
48+
+target_link_libraries(swiftFrontendObserver
49+
+ PUBLIC
50+
+ swiftFrontendTool)
51+
+
52+
+add_swift_host_library(swiftPluggableObserverDriverTool STATIC
53+
${driver_sources_and_options}
54+
)
55+
-target_link_libraries(swiftDriverTool
56+
+target_link_libraries(swiftPluggableObserverDriverTool
57+
PUBLIC
58+
${driver_common_libs})
59+
60+
+add_library(swiftDriverTool INTERFACE)
61+
+target_link_libraries(swiftDriverTool INTERFACE
62+
+ swiftPluggableObserverDriverTool
63+
+ swiftFrontendObserver)
64+
+
65+
# If building as part of clang, make sure the headers are installed.
66+
if(NOT SWIFT_BUILT_STANDALONE)
67+
- add_dependencies(swiftDriverTool clang-resource-headers)
68+
+ add_dependencies(swiftPluggableObserverDriverTool clang-resource-headers)
69+
endif()
70+
71+
-set_swift_llvm_is_available(swiftDriverTool)
72+
+set_swift_llvm_is_available(swiftFrontendObserver)
73+
+set_swift_llvm_is_available(swiftPluggableObserverDriverTool)
74+
diff --git a/lib/DriverTool/driver.cpp b/lib/DriverTool/driver.cpp
75+
index f71e2de9eae..a500e30827f 100644
76+
--- a/lib/DriverTool/driver.cpp
77+
+++ b/lib/DriverTool/driver.cpp
78+
@@ -31,6 +31,7 @@
79+
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
80+
#include "swift/FrontendTool/FrontendTool.h"
81+
#include "swift/DriverTool/DriverTool.h"
82+
+#include "swift/DriverTool/FrontendObserver.h"
83+
#include "llvm/ADT/SmallVector.h"
84+
#include "llvm/Support/CommandLine.h"
85+
#include "llvm/Support/ConvertUTF.h"
86+
@@ -197,7 +198,8 @@ static int run_driver(StringRef ExecName,
87+
if (FirstArg == "-frontend") {
88+
return performFrontend(llvm::makeArrayRef(argv.data()+2,
89+
argv.data()+argv.size()),
90+
- argv[0], (void *)(intptr_t)getExecutablePath);
91+
+ argv[0], (void *)(intptr_t)getExecutablePath,
92+
+ getFrontendObserver(argv));
93+
}
94+
if (FirstArg == "-modulewrap") {
95+
return modulewrap_main(llvm::makeArrayRef(argv.data()+2,
96+
@@ -211,7 +213,8 @@ static int run_driver(StringRef ExecName,
97+
&& ExecName == "swift-frontend") {
98+
return performFrontend(llvm::makeArrayRef(argv.data()+1,
99+
argv.data()+argv.size()),
100+
- argv[0], (void *)(intptr_t)getExecutablePath);
101+
+ argv[0], (void *)(intptr_t)getExecutablePath,
102+
+ getFrontendObserver(argv));
103+
}
104+
105+
if (FirstArg == "repl") {
106+
diff --git a/lib/DriverTool/swift_frontend_observer.cpp b/lib/DriverTool/swift_frontend_observer.cpp
107+
new file mode 100644
108+
index 00000000000..e16b2f970cd
109+
--- /dev/null
110+
+++ b/lib/DriverTool/swift_frontend_observer.cpp
111+
@@ -0,0 +1,9 @@
112+
+#include "swift/DriverTool/FrontendObserver.h"
113+
+
114+
+namespace swift {
115+
+
116+
+FrontendObserver* getFrontendObserver(llvm::ArrayRef<const char*>) {
117+
+ return nullptr;
118+
+}
119+
+
120+
+} // namespace swift
121+
diff --git a/lib/FrontendTool/FrontendTool.cpp b/lib/FrontendTool/FrontendTool.cpp
122+
index 811fb912f8a..afa2034aa71 100644
123+
--- a/lib/FrontendTool/FrontendTool.cpp
124+
+++ b/lib/FrontendTool/FrontendTool.cpp
125+
@@ -1924,7 +1924,7 @@ public:
126+
};
127+
};
128+
129+
-int swift::performFrontend(ArrayRef<const char *> Args,
130+
+static int performFrontendImpl(ArrayRef<const char *> Args,
131+
const char *Argv0, void *MainAddr,
132+
FrontendObserver *observer) {
133+
INITIALIZE_LLVM();
134+
@@ -2263,8 +2263,19 @@ int swift::performFrontend(ArrayRef<const char *> Args,
135+
return r;
136+
}
137+
138+
+int swift::performFrontend(ArrayRef<const char *> Args,
139+
+ const char *Argv0, void *MainAddr,
140+
+ FrontendObserver *observer) {
141+
+ auto ret = performFrontendImpl(Args, Argv0, MainAddr, observer);
142+
+ if (observer) {
143+
+ observer->finished(ret);
144+
+ }
145+
+ return ret;
146+
+}
147+
+
148+
void FrontendObserver::parsedArgs(CompilerInvocation &invocation) {}
149+
void FrontendObserver::configuredCompiler(CompilerInstance &instance) {}
150+
void FrontendObserver::performedSemanticAnalysis(CompilerInstance &instance) {}
151+
void FrontendObserver::performedSILGeneration(SILModule &module) {}
152+
void FrontendObserver::performedSILProcessing(SILModule &module) {}
153+
+void FrontendObserver::finished(int status) {}

pkg_swift_llvm.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ def export_headers(exported_dir, swift_source_tree, llvm_build_tree, swift_build
217217
for h in header_dirs:
218218
copy_includes(h, exported_dir)
219219

220-
221220
def zip_dir(src, tgt):
222221
tgt = get_tgt(tgt, f"swift-prebuilt-{get_platform()}.zip")
223222
print(f"compressing {src.name} to {tgt}")
@@ -238,10 +237,8 @@ def main(opts):
238237
export_libs(exported, libs, opts.swift_build_tree)
239238
export_headers(exported, opts.swift_source_tree, opts.llvm_build_tree, opts.swift_build_tree)
240239
export_sdk(exported / "sdk", opts.swift_source_tree, opts.swift_build_tree)
241-
242240
zip_dir(exported, opts.output)
243241

244242

245243
if __name__ == "__main__":
246244
main(getoptions())
247-

0 commit comments

Comments
 (0)