Skip to content

Commit daf1fa3

Browse files
committed
Swift: lock built swiftmodule traps in main
This should cover `-merge-modules` mode. Dumping of the configuration to the target files was moved to a separate pair of header/source files, as now it is also done in `SwiftOutputRewrite.cpp`.
1 parent 405d0fc commit daf1fa3

File tree

8 files changed

+56
-46
lines changed

8 files changed

+56
-46
lines changed

swift/extractor/BUILD.bazel

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ load("//swift:rules.bzl", "swift_cc_binary")
22

33
swift_cc_binary(
44
name = "extractor",
5-
srcs = [
6-
"SwiftOutputRewrite.cpp",
7-
"SwiftOutputRewrite.h",
8-
"SwiftExtractor.cpp",
9-
"SwiftExtractor.h",
10-
"SwiftExtractorConfiguration.h",
11-
"main.cpp",
12-
],
5+
srcs = glob([
6+
"*.h",
7+
"*.cpp",
8+
]),
139
visibility = ["//swift:__pkg__"],
1410
deps = [
1511
"//swift/extractor/infra",

swift/extractor/SwiftExtractor.cpp

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "swift/extractor/trap/generated/TrapClasses.h"
1515
#include "swift/extractor/trap/TrapDomain.h"
1616
#include "swift/extractor/visitors/SwiftVisitor.h"
17-
#include "swift/extractor/infra/TargetFile.h"
17+
#include "swift/extractor/TargetTrapFile.h"
1818

1919
using namespace codeql;
2020
using namespace std::string_literals;
@@ -80,20 +80,6 @@ static llvm::SmallVector<swift::Decl*> getTopLevelDecls(swift::ModuleDecl& modul
8080
return ret;
8181
}
8282

83-
static void dumpArgs(TargetFile& out, const SwiftExtractorConfiguration& config) {
84-
out << "/* extractor-args:\n";
85-
for (const auto& opt : config.frontendOptions) {
86-
out << " " << std::quoted(opt) << " \\\n";
87-
}
88-
out << "\n*/\n";
89-
90-
out << "/* swift-frontend-args:\n";
91-
for (const auto& opt : config.patchedFrontendOptions) {
92-
out << " " << std::quoted(opt) << " \\\n";
93-
}
94-
out << "\n*/\n";
95-
}
96-
9783
static void extractDeclarations(const SwiftExtractorConfiguration& config,
9884
swift::CompilerInstance& compiler,
9985
swift::ModuleDecl& module,
@@ -103,12 +89,11 @@ static void extractDeclarations(const SwiftExtractorConfiguration& config,
10389
// The extractor can be called several times from different processes with
10490
// the same input file(s). Using `TargetFile` the first process will win, and the following
10591
// will just skip the work
106-
auto trapTarget = TargetFile::create(filename + ".trap", config.trapDir, config.getTempTrapDir());
92+
auto trapTarget = createTargetTrapFile(config, filename);
10793
if (!trapTarget) {
10894
// another process arrived first, nothing to do for us
10995
return;
11096
}
111-
dumpArgs(*trapTarget, config);
11297
TrapDomain trap{*trapTarget};
11398

11499
// TODO: remove this and recreate it with IPA when we have that
@@ -172,14 +157,6 @@ void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config,
172157
auto inputFiles = collectInputFilenames(compiler);
173158
auto modules = collectModules(compiler);
174159

175-
// we want to make sure any following extractor run will not try to extract things from
176-
// the swiftmodule files we are creating in this run, as those things will already have been
177-
// extracted from source with more information. We do this by creating empty trap files.
178-
// TargetFile semantics will ensure any following run trying to extract that swiftmodule will just
179-
// skip doing it
180-
for (const auto& output : config.outputSwiftModules) {
181-
TargetFile::create(output + ".trap", config.trapDir, config.getTempTrapDir());
182-
}
183160
for (auto& module : modules) {
184161
bool isFromSourceFile = false;
185162
for (auto file : module->getFiles()) {

swift/extractor/SwiftExtractorConfiguration.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <string>
44
#include <vector>
55

6+
#include "swift/extractor/infra/TargetFile.h"
7+
68
namespace codeql {
79
struct SwiftExtractorConfiguration {
810
// The location for storing TRAP files to be imported by CodeQL engine.
@@ -32,9 +34,6 @@ struct SwiftExtractorConfiguration {
3234
// A temporary directory that contains build artifacts generated by the extractor during the
3335
// overall extraction process.
3436
std::string getTempArtifactDir() const { return scratchDir + "/swift-extraction-artifacts"; }
35-
36-
// Output swiftmodule files. This also includes possible locations where XCode internally moves
37-
// modules
38-
std::vector<std::string> outputSwiftModules;
3937
};
38+
4039
} // namespace codeql

swift/extractor/SwiftOutputRewrite.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "SwiftOutputRewrite.h"
22
#include "swift/extractor/SwiftExtractorConfiguration.h"
3+
#include "swift/extractor/TargetTrapFile.h"
34

45
#include <llvm/ADT/SmallString.h>
56
#include <llvm/Support/FileSystem.h>
@@ -323,15 +324,18 @@ std::vector<std::string> collectVFSFiles(const SwiftExtractorConfiguration& conf
323324

324325
return overlays;
325326
}
326-
std::vector<std::string> getOutputSwiftModules(
327-
const std::unordered_map<std::string, std::string>& remapping) {
328-
std::vector<std::string> ret;
327+
328+
void lockOutputSwiftModuleTraps(const SwiftExtractorConfiguration& config,
329+
const std::unordered_map<std::string, std::string>& remapping) {
329330
for (const auto& [oldPath, newPath] : remapping) {
330331
if (llvm::StringRef(oldPath).endswith(".swiftmodule")) {
331-
ret.push_back(oldPath);
332+
if (auto target = createTargetTrapFile(config, oldPath)) {
333+
*target << "// trap file deliberately empty\n"
334+
"// this swiftmodule was created during the build, so its entities must have"
335+
" been extracted directly from source files";
336+
}
332337
}
333338
}
334-
return ret;
335339
}
336340

337341
} // namespace codeql

swift/extractor/SwiftOutputRewrite.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void storeRemappingForVFS(const SwiftExtractorConfiguration& config,
2929
// This is separate from storeRemappingForVFS as we also collect files produced by other processes.
3030
std::vector<std::string> collectVFSFiles(const SwiftExtractorConfiguration& config);
3131

32-
// Returns a list of output remapped swift module files
33-
std::vector<std::string> getOutputSwiftModules(
34-
const std::unordered_map<std::string, std::string>& remapping);
32+
// Creates empty trap files for output swiftmodule files
33+
void lockOutputSwiftModuleTraps(const SwiftExtractorConfiguration& config,
34+
const std::unordered_map<std::string, std::string>& remapping);
3535
} // namespace codeql

swift/extractor/TargetTrapFile.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "swift/extractor/TargetTrapFile.h"
2+
#include <iomanip>
3+
namespace codeql {
4+
std::optional<TargetFile> createTargetTrapFile(const SwiftExtractorConfiguration& configuration,
5+
std::string_view target) {
6+
std::string trap{target};
7+
trap += ".trap";
8+
auto ret = TargetFile::create(trap, configuration.trapDir, configuration.getTempTrapDir());
9+
if (ret) {
10+
*ret << "/* extractor-args:\n";
11+
for (const auto& opt : configuration.frontendOptions) {
12+
*ret << " " << std::quoted(opt) << " \\\n";
13+
}
14+
*ret << "\n*/\n"
15+
"/* swift-frontend-args:\n";
16+
for (const auto& opt : configuration.patchedFrontendOptions) {
17+
*ret << " " << std::quoted(opt) << " \\\n";
18+
}
19+
*ret << "\n*/\n";
20+
}
21+
return ret;
22+
}
23+
} // namespace codeql

swift/extractor/TargetTrapFile.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "swift/extractor/infra/TargetFile.h"
4+
#include "swift/extractor/SwiftExtractorConfiguration.h"
5+
6+
namespace codeql {
7+
8+
std::optional<TargetFile> createTargetTrapFile(const SwiftExtractorConfiguration& configuration,
9+
std::string_view target);
10+
11+
} // namespace codeql

swift/extractor/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ int main(int argc, char** argv) {
6868
codeql::rewriteOutputsInPlace(configuration, configuration.patchedFrontendOptions);
6969
codeql::ensureDirectoriesForNewPathsExist(remapping);
7070
codeql::storeRemappingForVFS(configuration, remapping);
71-
configuration.outputSwiftModules = codeql::getOutputSwiftModules(remapping);
71+
codeql::lockOutputSwiftModuleTraps(configuration, remapping);
7272

7373
std::vector<const char*> args;
7474
for (auto& arg : configuration.patchedFrontendOptions) {

0 commit comments

Comments
 (0)