Skip to content

Commit 14fd89d

Browse files
committed
Swift: generalize output redirection code
1 parent 45c0c7f commit 14fd89d

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

swift/extractor/main.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,35 @@ static void processFrontendOptions(swift::FrontendOptions& options) {
3737
case Action::EmitModuleOnly:
3838
case Action::MergeModules:
3939
case Action::CompileModuleFromInterface:
40-
// for module emission actions, we redirect the output to our internal artifact storage
41-
{
42-
swift::SupplementaryOutputPaths paths;
43-
paths.ModuleOutputPath =
44-
codeql::redirect(options.InputsAndOutputs.getSingleOutputFilename()).string();
45-
options.InputsAndOutputs.setMainAndSupplementaryOutputs(std::vector{paths.ModuleOutputPath},
46-
std::vector{paths});
47-
return;
40+
case Action::EmitObject: {
41+
auto& inOuts = options.InputsAndOutputs;
42+
std::vector<swift::InputFile> inputs;
43+
inOuts.forEachInput([&](const swift::InputFile& input) {
44+
swift::PrimarySpecificPaths psp{};
45+
if (std::filesystem::path output = input.getPrimarySpecificPaths().OutputFilename;
46+
!output.empty()) {
47+
if (output.extension() == ".swiftmodule") {
48+
psp.OutputFilename = codeql::redirect(output);
49+
} else {
50+
psp.OutputFilename = "/dev/null";
51+
}
52+
}
53+
if (std::filesystem::path module =
54+
input.getPrimarySpecificPaths().SupplementaryOutputs.ModuleOutputPath;
55+
!module.empty()) {
56+
psp.SupplementaryOutputs.ModuleOutputPath = codeql::redirect(module);
57+
}
58+
auto inputCopy = input;
59+
inputCopy.setPrimarySpecificPaths(std::move(psp));
60+
inputs.push_back(std::move(inputCopy));
61+
return false;
62+
});
63+
inOuts.clearInputs();
64+
for (const auto& i : inputs) {
65+
inOuts.addInput(i);
4866
}
49-
case Action::EmitObject:
50-
// for object emission, we do a type check pass instead, muting output but getting the sema
51-
// phase to run in order to extract everything
52-
options.RequestedAction = Action::Typecheck;
5367
return;
68+
}
5469
case Action::PrintVersion:
5570
case Action::DumpAST:
5671
case Action::PrintAST:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
| F1.swift:0:0:0:0 | F1.swift |
77
| F2.swift:0:0:0:0 | F2.swift |
88
| G.swift:0:0:0:0 | G.swift |
9+
| H1.swift:0:0:0:0 | H1.swift |
10+
| H2.swift:0:0:0:0 | H2.swift |
911
| file://:0:0:0:0 | |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
func h1() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
func h2() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
func h3() {}

swift/integration-tests/posix-only/frontend-invocations/build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ $FRONTEND -frontend -c -primary-file D.swift -o D.o $SDK
1515
$FRONTEND -frontend -c -primary-file E.swift Esup.swift -o E.o $SDK
1616
$FRONTEND -frontend -emit-module -primary-file F1.swift F2.swift -module-name F -o F1.swiftmodule $SDK
1717
$FRONTEND -frontend -emit-module F1.swift -primary-file F2.swift -module-name F -o F2.swiftmodule $SDK
18+
$FRONTEND -frontend -emit-module F1.swift F2.swift -o Fs.swiftmodule $SDK
1819
$FRONTEND -frontend -merge-modules F1.swiftmodule F2.swiftmodule -o F.swiftmodule $SDK
1920
( cd dir; $FRONTEND -frontend -c ../G.swift $SDK )
21+
$FRONTEND -frontend -c -primary-file H1.swift -primary-file H2.swift H3.swift -emit-module-path H1.swiftmodule -emit-module-path H2.swiftmodule -o H1.o -o H2.o $SDK

0 commit comments

Comments
 (0)