Merge pull request #11622 from github/redsun82/swift-fix-parent-paths

Swift: fix extraction of sources from `..`
This commit is contained in:
AlexDenisov 2022-12-09 08:37:46 +01:00 коммит произвёл GitHub
Родитель 89cd4790d5 7645d4d928
Коммит 6f631b4daf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
11 изменённых файлов: 28 добавлений и 34 удалений

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

@ -12,7 +12,7 @@
#include "swift/extractor/translators/SwiftVisitor.h" #include "swift/extractor/translators/SwiftVisitor.h"
#include "swift/extractor/TargetTrapFile.h" #include "swift/extractor/TargetTrapFile.h"
#include "swift/extractor/SwiftBuiltinSymbols.h" #include "swift/extractor/SwiftBuiltinSymbols.h"
#include "swift/extractor/infra/Path.h" #include "swift/extractor/infra/file/Path.h"
using namespace codeql; using namespace codeql;
using namespace std::string_literals; 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) { static void archiveFile(const SwiftExtractorConfiguration& config, swift::SourceFile& file) {
ensureDirectory("TRAP", config.trapDir); auto source = codeql::resolvePath(file.getFilename());
ensureDirectory("source archive", config.sourceArchiveDir); auto destination = config.sourceArchiveDir / source.relative_path();
fs::path srcFilePath = codeql::getCodeQLPath(file.getFilename()); ensureDirectory("source archive destination", destination.parent_path());
auto dstFilePath = config.sourceArchiveDir;
dstFilePath += srcFilePath;
ensureDirectory("source archive destination", dstFilePath.parent_path());
std::error_code ec; 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) { if (ec) {
std::cerr << "Cannot archive source file " << srcFilePath << " -> " << dstFilePath << ": " std::cerr << "Cannot archive source file " << source << " -> " << destination << ": "
<< ec.message() << "\n"; << ec.message() << "\n";
} }
} }
static fs::path getFilename(swift::ModuleDecl& module, swift::SourceFile* primaryFile) { static fs::path getFilename(swift::ModuleDecl& module, swift::SourceFile* primaryFile) {
if (primaryFile) { if (primaryFile) {
return primaryFile->getFilename().str(); return resolvePath(primaryFile->getFilename());
} }
// PCM clang module // PCM clang module
if (module.isNonSwiftModule()) { 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 // 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 // unambiguously identified by the base file name, so we can discard the absolute directory
fs::path filename = "/pcms"; fs::path filename = "/pcms";
filename /= getCodeQLPath(module.getModuleFilename()).filename(); filename /= fs::path{std::string_view{module.getModuleFilename()}}.filename();
filename += "-"; filename += "-";
filename += module.getName().str(); filename += module.getName().str();
return filename; 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 // The Builtin module has an empty filename, let's fix that
return "/__Builtin__"; 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 // 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 // case we want to avoid the `<>` characters, in case a dirty DB is imported on Windows
if (filename == "<imports>") { if (filename == "<imports>") {
return "/__imports__"; return "/__imports__";
} }
return filename; return resolvePath(filename);
} }
/* The builtin module is special, as it does not publish any top-level declaration /* 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/TrapEntries.h"
#include "swift/extractor/trap/generated/TrapClasses.h" #include "swift/extractor/trap/generated/TrapClasses.h"
#include "swift/extractor/infra/SwiftLocationExtractor.h" #include "swift/extractor/infra/SwiftLocationExtractor.h"
#include "swift/extractor/infra/Path.h" #include "swift/extractor/infra/file/Path.h"
using namespace codeql; 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 // invalid locations seem to come from entities synthesized by the compiler
return; return;
} }
auto file = getCodeQLPath(sourceManager.getDisplayNameForLoc(start)); auto file = resolvePath(sourceManager.getDisplayNameForLoc(start));
DbLocation entry{{}}; DbLocation entry{{}};
entry.file = fetchFileLabel(file); entry.file = fetchFileLabel(file);
std::tie(entry.start_line, entry.start_column) = sourceManager.getLineAndColumnInBuffer(start); 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) { void SwiftLocationExtractor::emitFile(llvm::StringRef path) {
fetchFileLabel(getCodeQLPath(path)); fetchFileLabel(resolvePath(path));
} }
TrapLabel<FileTag> SwiftLocationExtractor::fetchFileLabel(const std::filesystem::path& file) { 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 <iostream>
#include <unistd.h> #include <unistd.h>
@ -16,7 +16,7 @@ static bool shouldCanonicalize() {
return true; return true;
} }
std::filesystem::path getCodeQLPath(std::string_view path) { std::filesystem::path resolvePath(std::string_view path) {
std::error_code ec; std::error_code ec;
std::filesystem::path ret = {}; std::filesystem::path ret = {};
static const auto canonicalize = shouldCanonicalize(); static const auto canonicalize = shouldCanonicalize();
@ -28,7 +28,7 @@ std::filesystem::path getCodeQLPath(std::string_view path) {
if (ec) { if (ec) {
std::cerr << "Cannot get " << (canonicalize ? "canonical" : "absolute") std::cerr << "Cannot get " << (canonicalize ? "canonical" : "absolute")
<< " path: " << std::quoted(path) << ": " << ec.message() << "\n"; << " path: " << std::quoted(path) << ": " << ec.message() << "\n";
return {}; return path;
} }
return ret; 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 | | E.swift:0:0:0:0 | E.swift |
| F1.swift:0:0:0:0 | F1.swift | | F1.swift:0:0:0:0 | F1.swift |
| F2.swift:0:0:0:0 | F2.swift | | F2.swift:0:0:0:0 | F2.swift |
| G.swift:0:0:0:0 | G.swift |
| file://:0:0:0:0 | | | 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 -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 -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 $FRONTEND -merge-modules F1.swiftmodule F2.swiftmodule -o F.swiftmodule $SDK
( cd dir; $FRONTEND -frontend -c ../G.swift $SDK )

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

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

@ -1,9 +1,3 @@
#-----| [ModuleDecl] __ObjC
#-----| [ModuleDecl] cfg
#-----| [ModuleDecl] declarations
#-----| [ModuleDecl] expressions
#-----| [ModuleDecl] patterns
#-----| [ModuleDecl] statements
cfg.swift: cfg.swift:
# 1| [TopLevelCodeDecl] { ... } # 1| [TopLevelCodeDecl] { ... }
# 1| getBody(): [BraceStmt] { ... } # 1| getBody(): [BraceStmt] { ... }

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

@ -10,5 +10,7 @@ import TestUtils
* The hook to customize the entities printed by this query. * The hook to customize the entities printed by this query.
*/ */
class PrintAstConfigurationOverride extends PrintAstConfiguration { class PrintAstConfigurationOverride extends PrintAstConfiguration {
override predicate shouldPrint(Locatable e) { super.shouldPrint(e) and toBeTested(e) } override predicate shouldPrint(Locatable e) {
super.shouldPrint(e) and toBeTested(e) and not e instanceof ModuleDecl
}
} }