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
1717static 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 ();
0 commit comments