Swift: generalize output redirection code

This commit is contained in:
Paolo Tranquilli
2022-12-14 18:26:48 +01:00
parent 45c0c7fe6c
commit 14fd89d482
6 changed files with 34 additions and 12 deletions

View File

@@ -37,20 +37,35 @@ static void processFrontendOptions(swift::FrontendOptions& options) {
case Action::EmitModuleOnly:
case Action::MergeModules:
case Action::CompileModuleFromInterface:
// for module emission actions, we redirect the output to our internal artifact storage
{
swift::SupplementaryOutputPaths paths;
paths.ModuleOutputPath =
codeql::redirect(options.InputsAndOutputs.getSingleOutputFilename()).string();
options.InputsAndOutputs.setMainAndSupplementaryOutputs(std::vector{paths.ModuleOutputPath},
std::vector{paths});
return;
case Action::EmitObject: {
auto& inOuts = options.InputsAndOutputs;
std::vector<swift::InputFile> inputs;
inOuts.forEachInput([&](const swift::InputFile& input) {
swift::PrimarySpecificPaths psp{};
if (std::filesystem::path output = input.getPrimarySpecificPaths().OutputFilename;
!output.empty()) {
if (output.extension() == ".swiftmodule") {
psp.OutputFilename = codeql::redirect(output);
} else {
psp.OutputFilename = "/dev/null";
}
}
if (std::filesystem::path module =
input.getPrimarySpecificPaths().SupplementaryOutputs.ModuleOutputPath;
!module.empty()) {
psp.SupplementaryOutputs.ModuleOutputPath = codeql::redirect(module);
}
auto inputCopy = input;
inputCopy.setPrimarySpecificPaths(std::move(psp));
inputs.push_back(std::move(inputCopy));
return false;
});
inOuts.clearInputs();
for (const auto& i : inputs) {
inOuts.addInput(i);
}
case Action::EmitObject:
// for object emission, we do a type check pass instead, muting output but getting the sema
// phase to run in order to extract everything
options.RequestedAction = Action::Typecheck;
return;
}
case Action::PrintVersion:
case Action::DumpAST:
case Action::PrintAST:

View File

@@ -6,4 +6,6 @@
| F1.swift:0:0:0:0 | F1.swift |
| F2.swift:0:0:0:0 | F2.swift |
| G.swift:0:0:0:0 | G.swift |
| H1.swift:0:0:0:0 | H1.swift |
| H2.swift:0:0:0:0 | H2.swift |
| file://:0:0:0:0 | |

View File

@@ -0,0 +1 @@
func h1() {}

View File

@@ -0,0 +1 @@
func h2() {}

View File

@@ -0,0 +1 @@
func h3() {}

View File

@@ -15,5 +15,7 @@ $FRONTEND -frontend -c -primary-file D.swift -o D.o $SDK
$FRONTEND -frontend -c -primary-file E.swift Esup.swift -o E.o $SDK
$FRONTEND -frontend -emit-module -primary-file F1.swift F2.swift -module-name F -o F1.swiftmodule $SDK
$FRONTEND -frontend -emit-module F1.swift -primary-file F2.swift -module-name F -o F2.swiftmodule $SDK
$FRONTEND -frontend -emit-module F1.swift F2.swift -o Fs.swiftmodule $SDK
$FRONTEND -frontend -merge-modules F1.swiftmodule F2.swiftmodule -o F.swiftmodule $SDK
( cd dir; $FRONTEND -frontend -c ../G.swift $SDK )
$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