Skip to content

Commit c31c515

Browse files
committed
Swift: move TargetFile as managed inside TrapDomain
1 parent 20eaa34 commit c31c515

File tree

7 files changed

+45
-47
lines changed

7 files changed

+45
-47
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include "swift/extractor/trap/TrapDomain.h"
1212
#include "swift/extractor/translators/SwiftVisitor.h"
13-
#include "swift/extractor/TargetTrapFile.h"
13+
#include "swift/extractor/TargetTrapDomain.h"
1414
#include "swift/extractor/SwiftBuiltinSymbols.h"
1515
#include "swift/extractor/infra/file/Path.h"
1616

@@ -118,12 +118,11 @@ static std::unordered_set<swift::ModuleDecl*> extractDeclarations(
118118
// The extractor can be called several times from different processes with
119119
// the same input file(s). Using `TargetFile` the first process will win, and the following
120120
// will just skip the work
121-
auto trapTarget = createTargetTrapFile(config, filename);
122-
if (!trapTarget) {
121+
auto trap = createTargetTrapDomain(config, filename);
122+
if (!trap) {
123123
// another process arrived first, nothing to do for us
124124
return {};
125125
}
126-
TrapDomain trap{*trapTarget};
127126

128127
std::vector<swift::Token> comments;
129128
if (primaryFile && primaryFile->getBufferID().hasValue()) {
@@ -137,7 +136,7 @@ static std::unordered_set<swift::ModuleDecl*> extractDeclarations(
137136
}
138137
}
139138

140-
SwiftVisitor visitor(compiler.getSourceMgr(), trap, module, primaryFile);
139+
SwiftVisitor visitor(compiler.getSourceMgr(), *trap, module, primaryFile);
141140
auto topLevelDecls = getTopLevelDecls(module, primaryFile);
142141
for (auto decl : topLevelDecls) {
143142
visitor.extract(decl);

swift/extractor/TargetTrapDomain.cpp

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

swift/extractor/TargetTrapDomain.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/trap/TrapDomain.h"
4+
#include "swift/extractor/config/SwiftExtractorConfiguration.h"
5+
6+
namespace codeql {
7+
8+
std::optional<TrapDomain> createTargetTrapDomain(const SwiftExtractorConfiguration& configuration,
9+
const std::filesystem::path& target);
10+
11+
} // namespace codeql

swift/extractor/TargetTrapFile.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

swift/extractor/TargetTrapFile.h

Lines changed: 0 additions & 11 deletions
This file was deleted.

swift/extractor/main.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <swift/FrontendTool/FrontendTool.h>
1212

1313
#include "swift/extractor/SwiftExtractor.h"
14-
#include "swift/extractor/TargetTrapFile.h"
14+
#include "swift/extractor/TargetTrapDomain.h"
1515
#include "swift/extractor/remapping/SwiftFileInterception.h"
1616
#include "swift/extractor/invocation/SwiftDiagnosticsConsumer.h"
1717
#include "swift/extractor/trap/TrapDomain.h"
@@ -26,10 +26,10 @@ static void lockOutputSwiftModuleTraps(const codeql::SwiftExtractorConfiguration
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::createTargetTrapFile(config, codeql::resolvePath(module))) {
30-
*target << "// trap file deliberately empty\n"
31-
"// this swiftmodule was created during the build, so its entities must have"
32-
" been extracted directly from source files";
29+
if (auto target = codeql::createTargetTrapDomain(config, codeql::resolvePath(module))) {
30+
target->emit("// trap file deliberately empty\n"
31+
"// this swiftmodule was created during the build, so its entities must have"
32+
" been extracted directly from source files");
3333
}
3434
}
3535
}
@@ -145,16 +145,16 @@ static void checkWhetherToRunUnderTool(int argc, char* const* argv) {
145145

146146
// Creates a target file that should store per-invocation info, e.g. compilation args,
147147
// compilations, diagnostics, etc.
148-
codeql::TargetFile invocationTargetFile(const codeql::SwiftExtractorConfiguration& configuration) {
148+
codeql::TrapDomain invocationTrapDomain(const codeql::SwiftExtractorConfiguration& configuration) {
149149
auto timestamp = std::chrono::system_clock::now().time_since_epoch().count();
150150
auto filename = std::to_string(timestamp) + '-' + std::to_string(getpid());
151151
auto target = std::filesystem::path("invocations") / std::filesystem::path(filename);
152-
auto maybeFile = codeql::createTargetTrapFile(configuration, target);
153-
if (!maybeFile) {
152+
auto maybeDomain = codeql::createTargetTrapDomain(configuration, target);
153+
if (!maybeDomain) {
154154
std::cerr << "Cannot create invocation trap file: " << target << "\n";
155155
abort();
156156
}
157-
return std::move(maybeFile.value());
157+
return std::move(maybeDomain.value());
158158
}
159159

160160
codeql::SwiftExtractorConfiguration configure(int argc, char** argv) {
@@ -183,8 +183,7 @@ int main(int argc, char** argv) {
183183

184184
auto openInterception = codeql::setupFileInterception(configuration);
185185

186-
auto invocationTrapFile = invocationTargetFile(configuration);
187-
codeql::TrapDomain invocationDomain(invocationTrapFile);
186+
auto invocationDomain = invocationTrapDomain(configuration);
188187
codeql::SwiftDiagnosticsConsumer diagConsumer(invocationDomain);
189188
Observer observer(configuration, diagConsumer);
190189
int frontend_rc = swift::performFrontend(configuration.frontendOptions, "swift-extractor",

swift/extractor/trap/TrapDomain.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ namespace codeql {
1010

1111
// Abstracts a given trap output file, with its own universe of trap labels
1212
class TrapDomain {
13-
TargetFile& out_;
13+
TargetFile out_;
1414

1515
public:
16-
explicit TrapDomain(TargetFile& out) : out_{out} {}
16+
explicit TrapDomain(TargetFile&& out) : out_{std::move(out)} {}
1717

1818
template <typename Entry>
1919
void emit(const Entry& e) {

0 commit comments

Comments
 (0)