mirror of
https://github.com/github/codeql.git
synced 2026-04-28 10:15:14 +02:00
Swift: move TargetFile as managed inside TrapDomain
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
|
||||
#include "swift/extractor/trap/TrapDomain.h"
|
||||
#include "swift/extractor/translators/SwiftVisitor.h"
|
||||
#include "swift/extractor/TargetTrapFile.h"
|
||||
#include "swift/extractor/TargetTrapDomain.h"
|
||||
#include "swift/extractor/SwiftBuiltinSymbols.h"
|
||||
#include "swift/extractor/infra/file/Path.h"
|
||||
|
||||
@@ -118,12 +118,11 @@ static std::unordered_set<swift::ModuleDecl*> extractDeclarations(
|
||||
// The extractor can be called several times from different processes with
|
||||
// the same input file(s). Using `TargetFile` the first process will win, and the following
|
||||
// will just skip the work
|
||||
auto trapTarget = createTargetTrapFile(config, filename);
|
||||
if (!trapTarget) {
|
||||
auto trap = createTargetTrapDomain(config, filename);
|
||||
if (!trap) {
|
||||
// another process arrived first, nothing to do for us
|
||||
return {};
|
||||
}
|
||||
TrapDomain trap{*trapTarget};
|
||||
|
||||
std::vector<swift::Token> comments;
|
||||
if (primaryFile && primaryFile->getBufferID().hasValue()) {
|
||||
@@ -137,7 +136,7 @@ static std::unordered_set<swift::ModuleDecl*> extractDeclarations(
|
||||
}
|
||||
}
|
||||
|
||||
SwiftVisitor visitor(compiler.getSourceMgr(), trap, module, primaryFile);
|
||||
SwiftVisitor visitor(compiler.getSourceMgr(), *trap, module, primaryFile);
|
||||
auto topLevelDecls = getTopLevelDecls(module, primaryFile);
|
||||
for (auto decl : topLevelDecls) {
|
||||
visitor.extract(decl);
|
||||
|
||||
18
swift/extractor/TargetTrapDomain.cpp
Normal file
18
swift/extractor/TargetTrapDomain.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "swift/extractor/TargetTrapDomain.h"
|
||||
#include <iomanip>
|
||||
namespace codeql {
|
||||
std::optional<TrapDomain> createTargetTrapDomain(const SwiftExtractorConfiguration& configuration,
|
||||
const std::filesystem::path& target) {
|
||||
auto trap = target;
|
||||
trap += ".trap";
|
||||
if (auto ret = TargetFile::create(trap, configuration.trapDir, configuration.getTempTrapDir())) {
|
||||
*ret << "/* extractor-args:\n";
|
||||
for (const auto& opt : configuration.frontendOptions) {
|
||||
*ret << " " << std::quoted(opt) << " \\\n";
|
||||
}
|
||||
*ret << "\n*/\n";
|
||||
return TrapDomain{*std::move(ret)};
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
} // namespace codeql
|
||||
11
swift/extractor/TargetTrapDomain.h
Normal file
11
swift/extractor/TargetTrapDomain.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "swift/extractor/trap/TrapDomain.h"
|
||||
#include "swift/extractor/config/SwiftExtractorConfiguration.h"
|
||||
|
||||
namespace codeql {
|
||||
|
||||
std::optional<TrapDomain> createTargetTrapDomain(const SwiftExtractorConfiguration& configuration,
|
||||
const std::filesystem::path& target);
|
||||
|
||||
} // namespace codeql
|
||||
@@ -1,18 +0,0 @@
|
||||
#include "swift/extractor/TargetTrapFile.h"
|
||||
#include <iomanip>
|
||||
namespace codeql {
|
||||
std::optional<TargetFile> createTargetTrapFile(const SwiftExtractorConfiguration& configuration,
|
||||
const std::filesystem::path& target) {
|
||||
auto trap = target;
|
||||
trap += ".trap";
|
||||
auto ret = TargetFile::create(trap, configuration.trapDir, configuration.getTempTrapDir());
|
||||
if (ret) {
|
||||
*ret << "/* extractor-args:\n";
|
||||
for (const auto& opt : configuration.frontendOptions) {
|
||||
*ret << " " << std::quoted(opt) << " \\\n";
|
||||
}
|
||||
*ret << "\n*/\n";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
} // namespace codeql
|
||||
@@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "swift/extractor/infra/file/TargetFile.h"
|
||||
#include "swift/extractor/config/SwiftExtractorConfiguration.h"
|
||||
|
||||
namespace codeql {
|
||||
|
||||
std::optional<TargetFile> createTargetTrapFile(const SwiftExtractorConfiguration& configuration,
|
||||
const std::filesystem::path& target);
|
||||
|
||||
} // namespace codeql
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <swift/FrontendTool/FrontendTool.h>
|
||||
|
||||
#include "swift/extractor/SwiftExtractor.h"
|
||||
#include "swift/extractor/TargetTrapFile.h"
|
||||
#include "swift/extractor/TargetTrapDomain.h"
|
||||
#include "swift/extractor/remapping/SwiftFileInterception.h"
|
||||
#include "swift/extractor/invocation/SwiftDiagnosticsConsumer.h"
|
||||
#include "swift/extractor/trap/TrapDomain.h"
|
||||
@@ -26,10 +26,10 @@ static void lockOutputSwiftModuleTraps(const codeql::SwiftExtractorConfiguration
|
||||
for (const auto& input : options.InputsAndOutputs.getAllInputs()) {
|
||||
if (const auto& module = input.getPrimarySpecificPaths().SupplementaryOutputs.ModuleOutputPath;
|
||||
!module.empty()) {
|
||||
if (auto target = codeql::createTargetTrapFile(config, codeql::resolvePath(module))) {
|
||||
*target << "// trap file deliberately empty\n"
|
||||
"// this swiftmodule was created during the build, so its entities must have"
|
||||
" been extracted directly from source files";
|
||||
if (auto target = codeql::createTargetTrapDomain(config, codeql::resolvePath(module))) {
|
||||
target->emit("// trap file deliberately empty\n"
|
||||
"// this swiftmodule was created during the build, so its entities must have"
|
||||
" been extracted directly from source files");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,16 +145,16 @@ static void checkWhetherToRunUnderTool(int argc, char* const* argv) {
|
||||
|
||||
// Creates a target file that should store per-invocation info, e.g. compilation args,
|
||||
// compilations, diagnostics, etc.
|
||||
codeql::TargetFile invocationTargetFile(const codeql::SwiftExtractorConfiguration& configuration) {
|
||||
codeql::TrapDomain invocationTrapDomain(const codeql::SwiftExtractorConfiguration& configuration) {
|
||||
auto timestamp = std::chrono::system_clock::now().time_since_epoch().count();
|
||||
auto filename = std::to_string(timestamp) + '-' + std::to_string(getpid());
|
||||
auto target = std::filesystem::path("invocations") / std::filesystem::path(filename);
|
||||
auto maybeFile = codeql::createTargetTrapFile(configuration, target);
|
||||
if (!maybeFile) {
|
||||
auto maybeDomain = codeql::createTargetTrapDomain(configuration, target);
|
||||
if (!maybeDomain) {
|
||||
std::cerr << "Cannot create invocation trap file: " << target << "\n";
|
||||
abort();
|
||||
}
|
||||
return std::move(maybeFile.value());
|
||||
return std::move(maybeDomain.value());
|
||||
}
|
||||
|
||||
codeql::SwiftExtractorConfiguration configure(int argc, char** argv) {
|
||||
@@ -183,8 +183,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
auto openInterception = codeql::setupFileInterception(configuration);
|
||||
|
||||
auto invocationTrapFile = invocationTargetFile(configuration);
|
||||
codeql::TrapDomain invocationDomain(invocationTrapFile);
|
||||
auto invocationDomain = invocationTrapDomain(configuration);
|
||||
codeql::SwiftDiagnosticsConsumer diagConsumer(invocationDomain);
|
||||
Observer observer(configuration, diagConsumer);
|
||||
int frontend_rc = swift::performFrontend(configuration.frontendOptions, "swift-extractor",
|
||||
|
||||
@@ -10,10 +10,10 @@ namespace codeql {
|
||||
|
||||
// Abstracts a given trap output file, with its own universe of trap labels
|
||||
class TrapDomain {
|
||||
TargetFile& out_;
|
||||
TargetFile out_;
|
||||
|
||||
public:
|
||||
explicit TrapDomain(TargetFile& out) : out_{out} {}
|
||||
explicit TrapDomain(TargetFile&& out) : out_{std::move(out)} {}
|
||||
|
||||
template <typename Entry>
|
||||
void emit(const Entry& e) {
|
||||
|
||||
Reference in New Issue
Block a user