2121using namespace std ::string_literals;
2222
2323// must be called before processFrontendOptions modifies output paths
24- static void lockOutputSwiftModuleTraps (const codeql::SwiftExtractorConfiguration& config ,
24+ static void lockOutputSwiftModuleTraps (codeql::SwiftExtractorState& state ,
2525 const swift::FrontendOptions& options) {
2626 for (const auto & input : options.InputsAndOutputs .getAllInputs ()) {
2727 if (const auto & module = input.getPrimarySpecificPaths ().SupplementaryOutputs .ModuleOutputPath ;
2828 !module .empty ()) {
29- if (auto target = codeql::createTargetTrapDomain (config , codeql::resolvePath (module ))) {
29+ if (auto target = codeql::createTargetTrapDomain (state , codeql::resolvePath (module ))) {
3030 target->emit (" // trap file deliberately empty\n "
3131 " // this swiftmodule was created during the build, so its entities must have"
3232 " been extracted directly from source files" );
@@ -65,18 +65,18 @@ static void processFrontendOptions(swift::FrontendOptions& options) {
6565 }
6666}
6767
68+ codeql::TrapDomain invocationTrapDomain (codeql::SwiftExtractorState& state);
69+
6870// This is part of the swiftFrontendTool interface, we hook into the
6971// compilation pipeline and extract files after the Swift frontend performed
7072// semantic analysis
7173class Observer : public swift ::FrontendObserver {
7274 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} {}
7676
7777 void parsedArgs (swift::CompilerInvocation& invocation) override {
7878 auto & options = invocation.getFrontendOptions ();
79- lockOutputSwiftModuleTraps (config , options);
79+ lockOutputSwiftModuleTraps (state , options);
8080 processFrontendOptions (options);
8181 }
8282
@@ -85,12 +85,13 @@ class Observer : public swift::FrontendObserver {
8585 }
8686
8787 void performedSemanticAnalysis (swift::CompilerInstance& compiler) override {
88- codeql::extractSwiftFiles (config , compiler);
88+ codeql::extractSwiftFiles (state , compiler);
8989 }
9090
9191 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};
9495};
9596
9697static std::string getenv_or (const char * envvar, const std::string& def) {
@@ -145,11 +146,11 @@ static void checkWhetherToRunUnderTool(int argc, char* const* argv) {
145146
146147// Creates a target file that should store per-invocation info, e.g. compilation args,
147148// compilations, diagnostics, etc.
148- codeql::TrapDomain invocationTrapDomain (const codeql::SwiftExtractorConfiguration& configuration ) {
149+ codeql::TrapDomain invocationTrapDomain (codeql::SwiftExtractorState& state ) {
149150 auto timestamp = std::chrono::system_clock::now ().time_since_epoch ().count ();
150151 auto filename = std::to_string (timestamp) + ' -' + std::to_string (getpid ());
151152 auto target = std::filesystem::path (" invocations" ) / std::filesystem::path (filename);
152- auto maybeDomain = codeql::createTargetTrapDomain (configuration , target);
153+ auto maybeDomain = codeql::createTargetTrapDomain (state , target);
153154 if (!maybeDomain) {
154155 std::cerr << " Cannot create invocation trap file: " << target << " \n " ;
155156 abort ();
@@ -183,9 +184,7 @@ int main(int argc, char** argv) {
183184
184185 auto openInterception = codeql::setupFileInterception (configuration);
185186
186- auto invocationDomain = invocationTrapDomain (configuration);
187- codeql::SwiftDiagnosticsConsumer diagConsumer (invocationDomain);
188- Observer observer (configuration, diagConsumer);
187+ Observer observer (configuration);
189188 int frontend_rc = swift::performFrontend (configuration.frontendOptions , " swift-extractor" ,
190189 (void *)main, &observer);
191190
0 commit comments