Swift: fix extraction of sources from `..`

This commit is contained in:
Paolo Tranquilli 2022-12-08 15:57:57 +01:00
Родитель 280bb6864f
Коммит 1ba8b6d35a
8 изменённых файлов: 25 добавлений и 27 удалений

Просмотреть файл

@ -12,7 +12,7 @@
#include "swift/extractor/translators/SwiftVisitor.h"
#include "swift/extractor/TargetTrapFile.h"
#include "swift/extractor/SwiftBuiltinSymbols.h"
#include "swift/extractor/infra/Path.h"
#include "swift/extractor/infra/file/Path.h"
using namespace codeql;
using namespace std::string_literals;
@ -28,27 +28,23 @@ static void ensureDirectory(const char* label, const fs::path& dir) {
}
static void archiveFile(const SwiftExtractorConfiguration& config, swift::SourceFile& file) {
ensureDirectory("TRAP", config.trapDir);
ensureDirectory("source archive", config.sourceArchiveDir);
auto source = codeql::resolvePath(file.getFilename());
auto destination = config.sourceArchiveDir / source.relative_path();
fs::path srcFilePath = codeql::getCodeQLPath(file.getFilename());
auto dstFilePath = config.sourceArchiveDir;
dstFilePath += srcFilePath;
ensureDirectory("source archive destination", dstFilePath.parent_path());
ensureDirectory("source archive destination", destination.parent_path());
std::error_code ec;
fs::copy(srcFilePath, dstFilePath, fs::copy_options::overwrite_existing, ec);
fs::copy(source, destination, fs::copy_options::overwrite_existing, ec);
if (ec) {
std::cerr << "Cannot archive source file " << srcFilePath << " -> " << dstFilePath << ": "
std::cerr << "Cannot archive source file " << source << " -> " << destination << ": "
<< ec.message() << "\n";
}
}
static fs::path getFilename(swift::ModuleDecl& module, swift::SourceFile* primaryFile) {
if (primaryFile) {
return primaryFile->getFilename().str();
return resolvePath(primaryFile->getFilename());
}
// PCM clang module
if (module.isNonSwiftModule()) {
@ -57,7 +53,7 @@ static fs::path getFilename(swift::ModuleDecl& module, swift::SourceFile* primar
// Moreover, pcm files may come from caches located in different directories, but are
// unambiguously identified by the base file name, so we can discard the absolute directory
fs::path filename = "/pcms";
filename /= getCodeQLPath(module.getModuleFilename()).filename();
filename /= fs::path{std::string_view{module.getModuleFilename()}}.filename();
filename += "-";
filename += module.getName().str();
return filename;
@ -66,13 +62,13 @@ static fs::path getFilename(swift::ModuleDecl& module, swift::SourceFile* primar
// The Builtin module has an empty filename, let's fix that
return "/__Builtin__";
}
auto filename = getCodeQLPath(module.getModuleFilename());
std::string_view filename = module.getModuleFilename();
// there is a special case of a module without an actual filename reporting `<imports>`: in this
// case we want to avoid the `<>` characters, in case a dirty DB is imported on Windows
if (filename == "<imports>") {
return "/__imports__";
}
return filename;
return resolvePath(filename);
}
/* The builtin module is special, as it does not publish any top-level declaration

Просмотреть файл

@ -1,7 +0,0 @@
#pragma once
#include <filesystem>
namespace codeql {
std::filesystem::path getCodeQLPath(std::string_view path);
}

Просмотреть файл

@ -5,7 +5,7 @@
#include "swift/extractor/trap/generated/TrapEntries.h"
#include "swift/extractor/trap/generated/TrapClasses.h"
#include "swift/extractor/infra/SwiftLocationExtractor.h"
#include "swift/extractor/infra/Path.h"
#include "swift/extractor/infra/file/Path.h"
using namespace codeql;
@ -17,7 +17,7 @@ void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceMa
// invalid locations seem to come from entities synthesized by the compiler
return;
}
auto file = getCodeQLPath(sourceManager.getDisplayNameForLoc(start));
auto file = resolvePath(sourceManager.getDisplayNameForLoc(start));
DbLocation entry{{}};
entry.file = fetchFileLabel(file);
std::tie(entry.start_line, entry.start_column) = sourceManager.getLineAndColumnInBuffer(start);
@ -30,7 +30,7 @@ void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceMa
}
void SwiftLocationExtractor::emitFile(llvm::StringRef path) {
fetchFileLabel(getCodeQLPath(path));
fetchFileLabel(resolvePath(path));
}
TrapLabel<FileTag> SwiftLocationExtractor::fetchFileLabel(const std::filesystem::path& file) {

Просмотреть файл

@ -1,4 +1,4 @@
#include "swift/extractor/infra/Path.h"
#include "swift/extractor/infra/file/Path.h"
#include <iostream>
#include <unistd.h>
@ -16,7 +16,7 @@ static bool shouldCanonicalize() {
return true;
}
std::filesystem::path getCodeQLPath(std::string_view path) {
std::filesystem::path resolvePath(std::string_view path) {
std::error_code ec;
std::filesystem::path ret = {};
static const auto canonicalize = shouldCanonicalize();
@ -28,7 +28,7 @@ std::filesystem::path getCodeQLPath(std::string_view path) {
if (ec) {
std::cerr << "Cannot get " << (canonicalize ? "canonical" : "absolute")
<< " path: " << std::quoted(path) << ": " << ec.message() << "\n";
return {};
return path;
}
return ret;
}

Просмотреть файл

@ -0,0 +1,7 @@
#pragma once
#include <filesystem>
namespace codeql {
std::filesystem::path resolvePath(std::string_view path);
}

Просмотреть файл

@ -5,4 +5,5 @@
| E.swift:0:0:0:0 | E.swift |
| F1.swift:0:0:0:0 | F1.swift |
| F2.swift:0:0:0:0 | F2.swift |
| G.swift:0:0:0:0 | G.swift |
| file://:0:0:0:0 | |

Просмотреть файл

Просмотреть файл

@ -16,3 +16,4 @@ $FRONTEND -frontend -c -primary-file E.swift Esup.swift -o E.o $SDK
$FRONTEND -frontend -emit-module -primary-file F1.swift F2.swift -module-name F -o F1.swiftmodule $SDK
$FRONTEND -frontend -emit-module F1.swift -primary-file F2.swift -module-name F -o F2.swiftmodule $SDK
$FRONTEND -merge-modules F1.swiftmodule F2.swiftmodule -o F.swiftmodule $SDK
( cd dir; $FRONTEND -frontend -c ../G.swift $SDK )