21
21
using namespace std ::string_literals;
22
22
23
23
// must be called before processFrontendOptions modifies output paths
24
- static void lockOutputSwiftModuleTraps (const codeql::SwiftExtractorConfiguration& config ,
24
+ static void lockOutputSwiftModuleTraps (codeql::SwiftExtractorState& state ,
25
25
const swift::FrontendOptions& options) {
26
26
for (const auto & input : options.InputsAndOutputs .getAllInputs ()) {
27
27
if (const auto & module = input.getPrimarySpecificPaths ().SupplementaryOutputs .ModuleOutputPath ;
28
28
!module .empty ()) {
29
- if (auto target = codeql::createTargetTrapDomain (config , codeql::resolvePath (module ))) {
29
+ if (auto target = codeql::createTargetTrapDomain (state , codeql::resolvePath (module ))) {
30
30
target->emit (" // trap file deliberately empty\n "
31
31
" // this swiftmodule was created during the build, so its entities must have"
32
32
" been extracted directly from source files" );
@@ -65,18 +65,18 @@ static void processFrontendOptions(swift::FrontendOptions& options) {
65
65
}
66
66
}
67
67
68
+ codeql::TrapDomain invocationTrapDomain (codeql::SwiftExtractorState& state);
69
+
68
70
// This is part of the swiftFrontendTool interface, we hook into the
69
71
// compilation pipeline and extract files after the Swift frontend performed
70
72
// semantic analysis
71
73
class Observer : public swift ::FrontendObserver {
72
74
public:
73
- explicit Observer (const codeql::SwiftExtractorConfiguration& config,
74
- codeql::SwiftDiagnosticsConsumer& diagConsumer)
75
- : config{config}, diagConsumer{diagConsumer} {}
75
+ explicit Observer (const codeql::SwiftExtractorConfiguration& config) : state{config} {}
76
76
77
77
void parsedArgs (swift::CompilerInvocation& invocation) override {
78
78
auto & options = invocation.getFrontendOptions ();
79
- lockOutputSwiftModuleTraps (config , options);
79
+ lockOutputSwiftModuleTraps (state , options);
80
80
processFrontendOptions (options);
81
81
}
82
82
@@ -85,12 +85,13 @@ class Observer : public swift::FrontendObserver {
85
85
}
86
86
87
87
void performedSemanticAnalysis (swift::CompilerInstance& compiler) override {
88
- codeql::extractSwiftFiles (config , compiler);
88
+ codeql::extractSwiftFiles (state , compiler);
89
89
}
90
90
91
91
private:
92
- const codeql::SwiftExtractorConfiguration& config;
93
- codeql::SwiftDiagnosticsConsumer& diagConsumer;
92
+ codeql::SwiftExtractorState state;
93
+ codeql::TrapDomain invocationTrap{invocationTrapDomain (state)};
94
+ codeql::SwiftDiagnosticsConsumer diagConsumer{invocationTrap};
94
95
};
95
96
96
97
static std::string getenv_or (const char * envvar, const std::string& def) {
@@ -145,11 +146,11 @@ static void checkWhetherToRunUnderTool(int argc, char* const* argv) {
145
146
146
147
// Creates a target file that should store per-invocation info, e.g. compilation args,
147
148
// compilations, diagnostics, etc.
148
- codeql::TrapDomain invocationTrapDomain (const codeql::SwiftExtractorConfiguration& configuration ) {
149
+ codeql::TrapDomain invocationTrapDomain (codeql::SwiftExtractorState& state ) {
149
150
auto timestamp = std::chrono::system_clock::now ().time_since_epoch ().count ();
150
151
auto filename = std::to_string (timestamp) + ' -' + std::to_string (getpid ());
151
152
auto target = std::filesystem::path (" invocations" ) / std::filesystem::path (filename);
152
- auto maybeDomain = codeql::createTargetTrapDomain (configuration , target);
153
+ auto maybeDomain = codeql::createTargetTrapDomain (state , target);
153
154
if (!maybeDomain) {
154
155
std::cerr << " Cannot create invocation trap file: " << target << " \n " ;
155
156
abort ();
@@ -183,9 +184,7 @@ int main(int argc, char** argv) {
183
184
184
185
auto openInterception = codeql::setupFileInterception (configuration);
185
186
186
- auto invocationDomain = invocationTrapDomain (configuration);
187
- codeql::SwiftDiagnosticsConsumer diagConsumer (invocationDomain);
188
- Observer observer (configuration, diagConsumer);
187
+ Observer observer (configuration);
189
188
int frontend_rc = swift::performFrontend (configuration.frontendOptions , " swift-extractor" ,
190
189
(void *)main, &observer);
191
190
0 commit comments