Skip to content

Commit f3f1779

Browse files
committed
Swift: extract all source files in WMO mode
WMO stands for whole module optimization. It's a compilation mode where all sources of a module are compiled together, e.g. ``` swift-frontend -emit-module A.swift B.swift -o Module.swiftmodule ``` This is opposed to incremental mode, where one would do something like ``` swift-frontend -emit-module -primary-file A.swift B.swift -module-name Module -o Module~A.swiftmodule swift-frontend -emit-module A.swift -primary-file B.swift -module-name Module -o Module~B.swiftmodule swift-frontend -merge-modules Module~A.swiftmodule Module~B.swiftmodule -o Module.swiftmodule ``` In WMO mode we were skipping extraction of all files after the first one, because we were filtering in only files with an associated output, and internally swift only assigns the output to the first input file in WMO mode (which is just an implementation detail). This patch refines that filter, by getting all input source files in case there are no primary inputs.
1 parent e393188 commit f3f1779

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,12 @@ static std::unordered_set<swift::ModuleDecl*> extractDeclarations(
161161
static std::unordered_set<std::string> collectInputFilenames(swift::CompilerInstance& compiler) {
162162
// The frontend can be called in many different ways.
163163
// At each invocation we only extract system and builtin modules and any input source files that
164-
// have an output associated with them.
164+
// are primary inputs, or all of them if there are no primary inputs (whole module optimization)
165165
std::unordered_set<std::string> sourceFiles;
166-
auto inputFiles = compiler.getInvocation().getFrontendOptions().InputsAndOutputs.getAllInputs();
167-
for (auto& input : inputFiles) {
168-
if (input.getType() == swift::file_types::TY_Swift && !input.outputFilename().empty()) {
166+
const auto& inOuts = compiler.getInvocation().getFrontendOptions().InputsAndOutputs;
167+
for (auto& input : inOuts.getAllInputs()) {
168+
if (input.getType() == swift::file_types::TY_Swift &&
169+
(!inOuts.hasPrimaryInputs() || input.isPrimary())) {
169170
sourceFiles.insert(input.getFileName());
170171
}
171172
}
@@ -191,11 +192,15 @@ void codeql::extractSwiftFiles(SwiftExtractorState& state, swift::CompilerInstan
191192
todo.pop_back();
192193
bool isFromSourceFile = false;
193194
std::unordered_set<swift::ModuleDecl*> encounteredModules;
195+
llvm::errs() << "MODULE: " << module->getRealName() << '\n';
194196
for (auto file : module->getFiles()) {
197+
llvm::errs() << " FILE: kind=" << static_cast<int>(file->getKind());
195198
auto sourceFile = llvm::dyn_cast<swift::SourceFile>(file);
196199
if (!sourceFile) {
200+
llvm::errs() << '\n';
197201
continue;
198202
}
203+
llvm::errs() << " name=" << sourceFile->getFilename() << '\n';
199204
isFromSourceFile = true;
200205
if (inputFiles.count(sourceFile->getFilename().str()) == 0) {
201206
continue;

swift/integration-tests/posix-only/frontend-invocations/Files.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
| F1.swift:0:0:0:0 | F1.swift |
77
| F2.swift:0:0:0:0 | F2.swift |
88
| F3.swift:0:0:0:0 | F3.swift |
9+
| F4.swift:0:0:0:0 | F4.swift |
910
| F5.swift:0:0:0:0 | F5.swift |
1011
| G.swift:0:0:0:0 | G.swift |
1112
| H1.swift:0:0:0:0 | H1.swift |

0 commit comments

Comments
 (0)