Skip to content

Commit 1dd3141

Browse files
committed
Swift: address more code review comments
1 parent 35da75f commit 1dd3141

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

swift/extractor/SwiftOutputRewrite.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <optional>
1212
#include <iostream>
1313

14-
// Creates a copy of the output file map and updated remapping table in place
14+
// Creates a copy of the output file map and updates remapping table in place
1515
// It does not change the original map file as it is depended upon by the original compiler
1616
// Returns path to the newly created output file map on success, or None in a case of failure
1717
static std::optional<std::string> rewriteOutputFileMap(
@@ -96,7 +96,12 @@ static std::vector<std::string> computeModuleAliases(llvm::StringRef modulePath,
9696
if (!modulePath.endswith(".swiftmodule")) {
9797
return {};
9898
}
99-
99+
// Deconstructing the Xcode generated path
100+
//
101+
// clang-format off
102+
// intermediatesDirIndex destinationDir (2) arch(5)
103+
// DerivedData/FooBar/Build/Intermediates.noindex/FooBar.build/Debug-iphonesimulator/FooBar.build/Objects-normal/x86_64/FooBar.swiftmodule
104+
// clang-format on
100105
llvm::SmallVector<llvm::StringRef> chunks;
101106
modulePath.split(chunks, '/');
102107
size_t intermediatesDirIndex = 0;
@@ -110,9 +115,14 @@ static std::vector<std::string> computeModuleAliases(llvm::StringRef modulePath,
110115
if (intermediatesDirIndex == 0) {
111116
return {};
112117
}
118+
size_t destinationDirIndex = intermediatesDirIndex + 2;
119+
size_t archIndex = intermediatesDirIndex + 5;
120+
if (archIndex >= chunks.size()) {
121+
return {};
122+
}
113123
// e.g. Debug-iphoneos, Release-iphonesimulator, etc.
114-
auto destinationDir = chunks[intermediatesDirIndex + 2].str();
115-
auto arch = chunks[intermediatesDirIndex + 5].str();
124+
auto destinationDir = chunks[destinationDirIndex].str();
125+
auto arch = chunks[archIndex].str();
116126
auto moduleNameWithExt = chunks.back();
117127
auto moduleName = moduleNameWithExt.substr(0, moduleNameWithExt.find_last_of('.'));
118128
std::string relocatedModulePath = chunks[0].str();

swift/extractor/SwiftOutputRewrite.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ std::unordered_map<std::string, std::string> rewriteOutputsInPlace(
1717
std::vector<std::string>& CLIArgs);
1818

1919
// Create directories for all the redirected new paths as the Swift compiler expects them to exist.
20-
void ensureDirectoriesForNewPathsExist(const std::unordered_map<std::string, std::string>& remapping);
20+
void ensureDirectoriesForNewPathsExist(
21+
const std::unordered_map<std::string, std::string>& remapping);
2122

2223
// Stores remapped `.swiftmoduile`s in a YAML file for later consumption by the
2324
// llvm::RedirectingFileSystem via Swift's VFSOverlayFiles.
2425
void storeRemappingForVFS(const SwiftExtractorConfiguration& config,
2526
const std::unordered_map<std::string, std::string>& remapping);
2627

2728
// Returns a list of VFS YAML files produced by all the extractor processes.
29+
// This is separate from storeRemappingForVFS as we also collect files produced by other processes.
2830
std::vector<std::string> collectVFSFiles(const SwiftExtractorConfiguration& config);
2931

3032
} // namespace codeql

0 commit comments

Comments
 (0)