Skip to content

Commit 7f389b9

Browse files
committed
Swift: introduce TrapType
1 parent 353536b commit 7f389b9

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <swift/AST/SourceFile.h>
99
#include <swift/AST/Builtins.h>
1010

11-
#include "swift/extractor/trap/TrapDomain.h"
1211
#include "swift/extractor/translators/SwiftVisitor.h"
1312
#include "swift/extractor/TargetTrapDomain.h"
1413
#include "swift/extractor/SwiftBuiltinSymbols.h"
@@ -121,7 +120,8 @@ static std::unordered_set<swift::ModuleDecl*> extractDeclarations(
121120
// The extractor can be called several times from different processes with
122121
// the same input file(s). Using `TargetFile` the first process will win, and the following
123122
// will just skip the work
124-
auto trap = createTargetTrapDomain(state, filename);
123+
const auto trapType = primaryFile ? TrapType::source : TrapType::module;
124+
auto trap = createTargetTrapDomain(state, filename, trapType);
125125
if (!trap) {
126126
// another process arrived first, nothing to do for us
127127
return {};

swift/extractor/TargetTrapDomain.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
#include "swift/extractor/TargetTrapDomain.h"
22
#include <iomanip>
33
namespace codeql {
4+
static const char* typeToStr(TrapType type) {
5+
switch (type) {
6+
case TrapType::source:
7+
return "sources";
8+
case TrapType::module:
9+
return "modules";
10+
case TrapType::invocation:
11+
return "invocations";
12+
default:
13+
return "";
14+
}
15+
}
16+
17+
static std::filesystem::path getRelativeTrapPath(const std::filesystem::path& target,
18+
TrapType type,
19+
const char* extension = ".trap") {
20+
auto trap = typeToStr(type) / target.relative_path();
21+
trap += extension;
22+
return trap;
23+
}
24+
425
std::optional<TrapDomain> createTargetTrapDomain(SwiftExtractorState& state,
5-
const std::filesystem::path& target) {
6-
auto trap = target;
7-
trap += ".trap";
8-
state.traps.push_back(trap.relative_path());
9-
if (auto ret = TargetFile::create(trap, state.configuration.trapDir,
10-
state.configuration.getTempTrapDir())) {
26+
const std::filesystem::path& target,
27+
TrapType type) {
28+
if (target.empty()) {
29+
return std::nullopt;
30+
}
31+
auto trap = getRelativeTrapPath(target, type);
32+
auto ret =
33+
TargetFile::create(trap, state.configuration.trapDir, state.configuration.getTempTrapDir());
34+
state.traps.push_back(std::move(trap));
35+
if (ret) {
1136
*ret << "/* extractor-args:\n";
1237
for (const auto& opt : state.configuration.frontendOptions) {
1338
*ret << " " << std::quoted(opt) << " \\\n";

swift/extractor/TargetTrapDomain.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55

66
namespace codeql {
77

8+
enum class TrapType {
9+
source,
10+
module,
11+
invocation,
12+
};
13+
814
std::optional<TrapDomain> createTargetTrapDomain(SwiftExtractorState& state,
9-
const std::filesystem::path& target);
15+
const std::filesystem::path& target,
16+
TrapType type);
1017

1118
} // namespace codeql

swift/extractor/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ static void lockOutputSwiftModuleTraps(codeql::SwiftExtractorState& state,
2626
for (const auto& input : options.InputsAndOutputs.getAllInputs()) {
2727
if (const auto& module = input.getPrimarySpecificPaths().SupplementaryOutputs.ModuleOutputPath;
2828
!module.empty()) {
29-
if (auto target = codeql::createTargetTrapDomain(state, codeql::resolvePath(module))) {
29+
if (auto target = codeql::createTargetTrapDomain(state, codeql::resolvePath(module),
30+
codeql::TrapType::module)) {
3031
target->emit("// trap file deliberately empty\n"
3132
"// this swiftmodule was created during the build, so its entities must have"
3233
" been extracted directly from source files");
@@ -152,7 +153,7 @@ codeql::TrapDomain invocationTrapDomain(codeql::SwiftExtractorState& state) {
152153
auto timestamp = std::chrono::system_clock::now().time_since_epoch().count();
153154
auto filename = std::to_string(timestamp) + '-' + std::to_string(getpid());
154155
auto target = std::filesystem::path("invocations") / std::filesystem::path(filename);
155-
auto maybeDomain = codeql::createTargetTrapDomain(state, target);
156+
auto maybeDomain = codeql::createTargetTrapDomain(state, target, codeql::TrapType::invocation);
156157
if (!maybeDomain) {
157158
std::cerr << "Cannot create invocation trap file: " << target << "\n";
158159
abort();

0 commit comments

Comments
 (0)