Skip to content

Commit 6f631b4

Browse files
authored
Merge pull request github#11622 from github/redsun82/swift-fix-parent-paths
Swift: fix extraction of sources from `..`
2 parents 89cd479 + 7645d4d commit 6f631b4

File tree

11 files changed

+28
-34
lines changed

11 files changed

+28
-34
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "swift/extractor/translators/SwiftVisitor.h"
1313
#include "swift/extractor/TargetTrapFile.h"
1414
#include "swift/extractor/SwiftBuiltinSymbols.h"
15-
#include "swift/extractor/infra/Path.h"
15+
#include "swift/extractor/infra/file/Path.h"
1616

1717
using namespace codeql;
1818
using namespace std::string_literals;
@@ -28,27 +28,23 @@ static void ensureDirectory(const char* label, const fs::path& dir) {
2828
}
2929

3030
static void archiveFile(const SwiftExtractorConfiguration& config, swift::SourceFile& file) {
31-
ensureDirectory("TRAP", config.trapDir);
32-
ensureDirectory("source archive", config.sourceArchiveDir);
31+
auto source = codeql::resolvePath(file.getFilename());
32+
auto destination = config.sourceArchiveDir / source.relative_path();
3333

34-
fs::path srcFilePath = codeql::getCodeQLPath(file.getFilename());
35-
auto dstFilePath = config.sourceArchiveDir;
36-
dstFilePath += srcFilePath;
37-
38-
ensureDirectory("source archive destination", dstFilePath.parent_path());
34+
ensureDirectory("source archive destination", destination.parent_path());
3935

4036
std::error_code ec;
41-
fs::copy(srcFilePath, dstFilePath, fs::copy_options::overwrite_existing, ec);
37+
fs::copy(source, destination, fs::copy_options::overwrite_existing, ec);
4238

4339
if (ec) {
44-
std::cerr << "Cannot archive source file " << srcFilePath << " -> " << dstFilePath << ": "
40+
std::cerr << "Cannot archive source file " << source << " -> " << destination << ": "
4541
<< ec.message() << "\n";
4642
}
4743
}
4844

4945
static fs::path getFilename(swift::ModuleDecl& module, swift::SourceFile* primaryFile) {
5046
if (primaryFile) {
51-
return primaryFile->getFilename().str();
47+
return resolvePath(primaryFile->getFilename());
5248
}
5349
// PCM clang module
5450
if (module.isNonSwiftModule()) {
@@ -57,7 +53,7 @@ static fs::path getFilename(swift::ModuleDecl& module, swift::SourceFile* primar
5753
// Moreover, pcm files may come from caches located in different directories, but are
5854
// unambiguously identified by the base file name, so we can discard the absolute directory
5955
fs::path filename = "/pcms";
60-
filename /= getCodeQLPath(module.getModuleFilename()).filename();
56+
filename /= fs::path{std::string_view{module.getModuleFilename()}}.filename();
6157
filename += "-";
6258
filename += module.getName().str();
6359
return filename;
@@ -66,13 +62,13 @@ static fs::path getFilename(swift::ModuleDecl& module, swift::SourceFile* primar
6662
// The Builtin module has an empty filename, let's fix that
6763
return "/__Builtin__";
6864
}
69-
auto filename = getCodeQLPath(module.getModuleFilename());
65+
std::string_view filename = module.getModuleFilename();
7066
// there is a special case of a module without an actual filename reporting `<imports>`: in this
7167
// case we want to avoid the `<>` characters, in case a dirty DB is imported on Windows
7268
if (filename == "<imports>") {
7369
return "/__imports__";
7470
}
75-
return filename;
71+
return resolvePath(filename);
7672
}
7773

7874
/* The builtin module is special, as it does not publish any top-level declaration

swift/extractor/infra/Path.h

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

swift/extractor/infra/SwiftLocationExtractor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "swift/extractor/trap/generated/TrapEntries.h"
66
#include "swift/extractor/trap/generated/TrapClasses.h"
77
#include "swift/extractor/infra/SwiftLocationExtractor.h"
8-
#include "swift/extractor/infra/Path.h"
8+
#include "swift/extractor/infra/file/Path.h"
99

1010
using namespace codeql;
1111

@@ -17,7 +17,7 @@ void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceMa
1717
// invalid locations seem to come from entities synthesized by the compiler
1818
return;
1919
}
20-
auto file = getCodeQLPath(sourceManager.getDisplayNameForLoc(start));
20+
auto file = resolvePath(sourceManager.getDisplayNameForLoc(start));
2121
DbLocation entry{{}};
2222
entry.file = fetchFileLabel(file);
2323
std::tie(entry.start_line, entry.start_column) = sourceManager.getLineAndColumnInBuffer(start);
@@ -30,7 +30,7 @@ void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceMa
3030
}
3131

3232
void SwiftLocationExtractor::emitFile(llvm::StringRef path) {
33-
fetchFileLabel(getCodeQLPath(path));
33+
fetchFileLabel(resolvePath(path));
3434
}
3535

3636
TrapLabel<FileTag> SwiftLocationExtractor::fetchFileLabel(const std::filesystem::path& file) {

swift/extractor/infra/Path.cpp renamed to swift/extractor/infra/file/Path.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "swift/extractor/infra/Path.h"
1+
#include "swift/extractor/infra/file/Path.h"
22
#include <iostream>
33
#include <unistd.h>
44

@@ -16,7 +16,7 @@ static bool shouldCanonicalize() {
1616
return true;
1717
}
1818

19-
std::filesystem::path getCodeQLPath(std::string_view path) {
19+
std::filesystem::path resolvePath(std::string_view path) {
2020
std::error_code ec;
2121
std::filesystem::path ret = {};
2222
static const auto canonicalize = shouldCanonicalize();
@@ -28,7 +28,7 @@ std::filesystem::path getCodeQLPath(std::string_view path) {
2828
if (ec) {
2929
std::cerr << "Cannot get " << (canonicalize ? "canonical" : "absolute")
3030
<< " path: " << std::quoted(path) << ": " << ec.message() << "\n";
31-
return {};
31+
return path;
3232
}
3333
return ret;
3434
}

swift/extractor/infra/file/Path.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <filesystem>
4+
5+
namespace codeql {
6+
std::filesystem::path resolvePath(std::string_view path);
7+
}

swift/integration-tests/posix-only/frontend-invocations/Files.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
| E.swift:0:0:0:0 | E.swift |
66
| F1.swift:0:0:0:0 | F1.swift |
77
| F2.swift:0:0:0:0 | F2.swift |
8+
| G.swift:0:0:0:0 | G.swift |
89
| file://:0:0:0:0 | |

swift/integration-tests/posix-only/frontend-invocations/G.swift

Whitespace-only changes.

swift/integration-tests/posix-only/frontend-invocations/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ $FRONTEND -frontend -c -primary-file E.swift Esup.swift -o E.o $SDK
1616
$FRONTEND -frontend -emit-module -primary-file F1.swift F2.swift -module-name F -o F1.swiftmodule $SDK
1717
$FRONTEND -frontend -emit-module F1.swift -primary-file F2.swift -module-name F -o F2.swiftmodule $SDK
1818
$FRONTEND -merge-modules F1.swiftmodule F2.swiftmodule -o F.swiftmodule $SDK
19+
( cd dir; $FRONTEND -frontend -c ../G.swift $SDK )

swift/integration-tests/posix-only/frontend-invocations/dir/.empty

Whitespace-only changes.

swift/ql/test/library-tests/ast/PrintAst.expected

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
#-----| [ModuleDecl] __ObjC
2-
#-----| [ModuleDecl] cfg
3-
#-----| [ModuleDecl] declarations
4-
#-----| [ModuleDecl] expressions
5-
#-----| [ModuleDecl] patterns
6-
#-----| [ModuleDecl] statements
71
cfg.swift:
82
# 1| [TopLevelCodeDecl] { ... }
93
# 1| getBody(): [BraceStmt] { ... }

0 commit comments

Comments
 (0)