@@ -151,8 +151,7 @@ static void extractDeclarations(const SwiftExtractorConfiguration& config,
151151 }
152152}
153153
154- void codeql::extractSwiftFiles (const SwiftExtractorConfiguration& config,
155- swift::CompilerInstance& compiler) {
154+ static std::unordered_set<std::string> collectInputFilenames (swift::CompilerInstance& compiler) {
156155 // The frontend can be called in many different ways.
157156 // At each invocation we only extract system and builtin modules and any input source files that
158157 // have an output associated with them.
@@ -163,7 +162,10 @@ void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config,
163162 sourceFiles.insert (input.getFileName ());
164163 }
165164 }
165+ return sourceFiles;
166+ }
166167
168+ static std::unordered_set<swift::ModuleDecl*> collectModules (swift::CompilerInstance& compiler) {
167169 // getASTContext().getLoadedModules() does not provide all the modules available within the
168170 // program.
169171 // We need to iterate over all the imported modules (recursively) to see the whole "universe."
@@ -187,8 +189,15 @@ void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config,
187189 }
188190 }
189191 }
192+ return allModules;
193+ }
194+
195+ void codeql::extractSwiftFiles (const SwiftExtractorConfiguration& config,
196+ swift::CompilerInstance& compiler) {
197+ auto inputFiles = collectInputFilenames (compiler);
198+ auto modules = collectModules (compiler);
190199
191- for (auto & module : allModules ) {
200+ for (auto & module : modules ) {
192201 // We only extract system and builtin modules here as the other "user" modules can be built
193202 // during the build process and then re-used at a later stage. In this case, we extract the
194203 // user code twice: once during the module build in a form of a source file, and then as
@@ -201,7 +210,7 @@ void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config,
201210 } else {
202211 for (auto file : module ->getFiles ()) {
203212 auto sourceFile = llvm::dyn_cast<swift::SourceFile>(file);
204- if (!sourceFile || sourceFiles .count (sourceFile->getFilename ().str ()) == 0 ) {
213+ if (!sourceFile || inputFiles .count (sourceFile->getFilename ().str ()) == 0 ) {
205214 continue ;
206215 }
207216 archiveFile (config, *sourceFile);
0 commit comments