Swift: introduce TrapType

This commit is contained in:
Paolo Tranquilli
2023-01-18 09:48:44 +01:00
parent 353536b826
commit 7f389b9f9a
4 changed files with 44 additions and 11 deletions

View File

@@ -8,7 +8,6 @@
#include <swift/AST/SourceFile.h>
#include <swift/AST/Builtins.h>
#include "swift/extractor/trap/TrapDomain.h"
#include "swift/extractor/translators/SwiftVisitor.h"
#include "swift/extractor/TargetTrapDomain.h"
#include "swift/extractor/SwiftBuiltinSymbols.h"
@@ -121,7 +120,8 @@ 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 trap = createTargetTrapDomain(state, filename);
const auto trapType = primaryFile ? TrapType::source : TrapType::module;
auto trap = createTargetTrapDomain(state, filename, trapType);
if (!trap) {
// another process arrived first, nothing to do for us
return {};

View File

@@ -1,13 +1,38 @@
#include "swift/extractor/TargetTrapDomain.h"
#include <iomanip>
namespace codeql {
static const char* typeToStr(TrapType type) {
switch (type) {
case TrapType::source:
return "sources";
case TrapType::module:
return "modules";
case TrapType::invocation:
return "invocations";
default:
return "";
}
}
static std::filesystem::path getRelativeTrapPath(const std::filesystem::path& target,
TrapType type,
const char* extension = ".trap") {
auto trap = typeToStr(type) / target.relative_path();
trap += extension;
return trap;
}
std::optional<TrapDomain> createTargetTrapDomain(SwiftExtractorState& state,
const std::filesystem::path& target) {
auto trap = target;
trap += ".trap";
state.traps.push_back(trap.relative_path());
if (auto ret = TargetFile::create(trap, state.configuration.trapDir,
state.configuration.getTempTrapDir())) {
const std::filesystem::path& target,
TrapType type) {
if (target.empty()) {
return std::nullopt;
}
auto trap = getRelativeTrapPath(target, type);
auto ret =
TargetFile::create(trap, state.configuration.trapDir, state.configuration.getTempTrapDir());
state.traps.push_back(std::move(trap));
if (ret) {
*ret << "/* extractor-args:\n";
for (const auto& opt : state.configuration.frontendOptions) {
*ret << " " << std::quoted(opt) << " \\\n";

View File

@@ -5,7 +5,14 @@
namespace codeql {
enum class TrapType {
source,
module,
invocation,
};
std::optional<TrapDomain> createTargetTrapDomain(SwiftExtractorState& state,
const std::filesystem::path& target);
const std::filesystem::path& target,
TrapType type);
} // namespace codeql

View File

@@ -26,7 +26,8 @@ static void lockOutputSwiftModuleTraps(codeql::SwiftExtractorState& state,
for (const auto& input : options.InputsAndOutputs.getAllInputs()) {
if (const auto& module = input.getPrimarySpecificPaths().SupplementaryOutputs.ModuleOutputPath;
!module.empty()) {
if (auto target = codeql::createTargetTrapDomain(state, codeql::resolvePath(module))) {
if (auto target = codeql::createTargetTrapDomain(state, codeql::resolvePath(module),
codeql::TrapType::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");
@@ -152,7 +153,7 @@ codeql::TrapDomain invocationTrapDomain(codeql::SwiftExtractorState& state) {
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 maybeDomain = codeql::createTargetTrapDomain(state, target);
auto maybeDomain = codeql::createTargetTrapDomain(state, target, codeql::TrapType::invocation);
if (!maybeDomain) {
std::cerr << "Cannot create invocation trap file: " << target << "\n";
abort();