@@ -43,11 +43,9 @@ SerializedModuleLoaderBase::~SerializedModuleLoaderBase() = default;
43
43
SerializedModuleLoader::~SerializedModuleLoader () = default ;
44
44
45
45
std::error_code SerializedModuleLoaderBase::openModuleFiles (
46
- AccessPathElem ModuleID, StringRef DirName, StringRef ModuleFilename,
47
- StringRef ModuleDocFilename,
46
+ AccessPathElem ModuleID, StringRef ModulePath, StringRef ModuleDocPath,
48
47
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
49
- std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
50
- llvm::SmallVectorImpl<char > &Scratch) {
48
+ std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) {
51
49
assert (((ModuleBuffer && ModuleDocBuffer) ||
52
50
(!ModuleBuffer && !ModuleDocBuffer)) &&
53
51
" Module and Module Doc buffer must both be initialized or NULL" );
@@ -56,12 +54,11 @@ std::error_code SerializedModuleLoaderBase::openModuleFiles(
56
54
57
55
// Try to open the module file first. If we fail, don't even look for the
58
56
// module documentation file.
59
- Scratch.clear ();
60
- llvm::sys::path::append (Scratch, DirName, ModuleFilename);
57
+
61
58
// If there are no buffers to load into, simply check for the existence of
62
59
// the module file.
63
60
if (!(ModuleBuffer || ModuleDocBuffer)) {
64
- llvm::ErrorOr<clang::vfs::Status> statResult = FS.status (Scratch );
61
+ llvm::ErrorOr<clang::vfs::Status> statResult = FS.status (ModulePath );
65
62
if (!statResult)
66
63
return statResult.getError ();
67
64
if (!statResult->exists ())
@@ -72,16 +69,14 @@ std::error_code SerializedModuleLoaderBase::openModuleFiles(
72
69
}
73
70
74
71
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ModuleOrErr =
75
- FS.getBufferForFile (StringRef (Scratch. data (), Scratch. size ()) );
72
+ FS.getBufferForFile (ModulePath );
76
73
if (!ModuleOrErr)
77
74
return ModuleOrErr.getError ();
78
75
79
76
// Try to open the module documentation file. If it does not exist, ignore
80
77
// the error. However, pass though all other errors.
81
- Scratch.clear ();
82
- llvm::sys::path::append (Scratch, DirName, ModuleDocFilename);
83
78
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ModuleDocOrErr =
84
- FS.getBufferForFile (StringRef (Scratch. data (), Scratch. size ()) );
79
+ FS.getBufferForFile (ModuleDocPath );
85
80
if (!ModuleDocOrErr &&
86
81
ModuleDocOrErr.getError () != std::errc::no_such_file_or_directory) {
87
82
return ModuleDocOrErr.getError ();
@@ -94,20 +89,23 @@ std::error_code SerializedModuleLoaderBase::openModuleFiles(
94
89
return std::error_code ();
95
90
}
96
91
97
- std::error_code SerializedModuleLoader::openModuleFiles (
98
- AccessPathElem ModuleID, StringRef DirName , StringRef ModuleFilename,
92
+ std::error_code SerializedModuleLoader::findModuleFilesInDirectory (
93
+ AccessPathElem ModuleID, StringRef DirPath , StringRef ModuleFilename,
99
94
StringRef ModuleDocFilename,
100
95
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
101
- std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
102
- llvm::SmallVectorImpl<char > &Scratch) {
96
+ std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) {
103
97
if (LoadMode == ModuleLoadingMode::OnlyParseable)
104
98
return std::make_error_code (std::errc::not_supported);
105
99
106
- return SerializedModuleLoaderBase::openModuleFiles (ModuleID, DirName,
107
- ModuleFilename,
108
- ModuleDocFilename,
100
+ llvm::SmallString<256 > ModulePath{DirPath};
101
+ llvm::sys::path::append (ModulePath, ModuleFilename);
102
+ llvm::SmallString<256 > ModuleDocPath{DirPath};
103
+ llvm::sys::path::append (ModuleDocPath, ModuleDocFilename);
104
+ return SerializedModuleLoaderBase::openModuleFiles (ModuleID,
105
+ ModulePath,
106
+ ModuleDocPath,
109
107
ModuleBuffer,
110
- ModuleDocBuffer, Scratch );
108
+ ModuleDocBuffer);
111
109
}
112
110
113
111
bool SerializedModuleLoader::maybeDiagnoseArchitectureMismatch (
@@ -194,10 +192,7 @@ SerializedModuleLoaderBase::findModule(AccessPathElem moduleID,
194
192
auto &fs = *Ctx.SourceMgr .getFileSystem ();
195
193
isFramework = false ;
196
194
197
- // Declare some reusable buffers up front; if we end up spilling onto the
198
- // heap once, we're likely to do so again.
199
- llvm::SmallString<128 > scratch;
200
- llvm::SmallString<128 > currPath;
195
+ llvm::SmallString<256 > currPath;
201
196
for (auto path : Ctx.SearchPathOpts .ImportSearchPaths ) {
202
197
std::error_code result;
203
198
@@ -207,18 +202,17 @@ SerializedModuleLoaderBase::findModule(AccessPathElem moduleID,
207
202
208
203
if (statResult && statResult->isDirectory ()) {
209
204
// A .swiftmodule directory contains architecture-specific files.
210
- result = openModuleFiles (moduleID, currPath,
211
- archFileNames. first , archFileNames.second ,
212
- moduleBuffer, moduleDocBuffer ,
213
- scratch );
205
+ result = findModuleFilesInDirectory (moduleID, currPath,
206
+ archFileNames.first ,
207
+ archFileNames. second ,
208
+ moduleBuffer, moduleDocBuffer );
214
209
215
210
if (result == std::errc::no_such_file_or_directory &&
216
211
!alternateArchName.empty ()) {
217
- result = openModuleFiles (moduleID, currPath,
218
- alternateArchFileNames.first ,
219
- alternateArchFileNames.second ,
220
- moduleBuffer, moduleDocBuffer,
221
- scratch);
212
+ result = findModuleFilesInDirectory (moduleID, currPath,
213
+ alternateArchFileNames.first ,
214
+ alternateArchFileNames.second ,
215
+ moduleBuffer, moduleDocBuffer);
222
216
}
223
217
224
218
if (result == std::errc::no_such_file_or_directory) {
@@ -231,10 +225,10 @@ SerializedModuleLoaderBase::findModule(AccessPathElem moduleID,
231
225
} else {
232
226
// We can't just return the error; the path we're looking for might not
233
227
// be "Foo.swiftmodule".
234
- result = openModuleFiles (moduleID, path,
235
- moduleFilename. str (), moduleDocFilename .str (),
236
- moduleBuffer, moduleDocBuffer ,
237
- scratch );
228
+ result = findModuleFilesInDirectory (moduleID, path,
229
+ moduleFilename .str (),
230
+ moduleDocFilename. str () ,
231
+ moduleBuffer, moduleDocBuffer );
238
232
}
239
233
if (!result)
240
234
return true ;
@@ -257,16 +251,17 @@ SerializedModuleLoaderBase::findModule(AccessPathElem moduleID,
257
251
// Frameworks always use architecture-specific files within a .swiftmodule
258
252
// directory.
259
253
llvm::sys::path::append (currPath, " Modules" , moduleFilename.str ());
260
- auto err = openModuleFiles (moduleID, currPath,
261
- archFileNames.first , archFileNames.second ,
262
- moduleBuffer, moduleDocBuffer, scratch);
254
+ auto err = findModuleFilesInDirectory (moduleID, currPath,
255
+ archFileNames.first ,
256
+ archFileNames.second ,
257
+ moduleBuffer, moduleDocBuffer);
263
258
264
259
if (err == std::errc::no_such_file_or_directory &&
265
260
!alternateArchName.empty ()) {
266
- err = openModuleFiles (moduleID, currPath,
267
- alternateArchFileNames.first ,
268
- alternateArchFileNames.second ,
269
- moduleBuffer, moduleDocBuffer, scratch );
261
+ err = findModuleFilesInDirectory (moduleID, currPath,
262
+ alternateArchFileNames.first ,
263
+ alternateArchFileNames.second ,
264
+ moduleBuffer, moduleDocBuffer);
270
265
}
271
266
272
267
if (err == std::errc::no_such_file_or_directory) {
@@ -287,6 +282,7 @@ SerializedModuleLoaderBase::findModule(AccessPathElem moduleID,
287
282
if (Ctx.LangOpts .Target .isOSDarwin ()) {
288
283
// Apple platforms have extra implicit framework search paths:
289
284
// $SDKROOT/System/Library/Frameworks/ and $SDKROOT/Library/Frameworks/
285
+ SmallString<256 > scratch;
290
286
scratch = Ctx.SearchPathOpts .SDKPath ;
291
287
llvm::sys::path::append (scratch, " System" , " Library" , " Frameworks" );
292
288
if (tryFrameworkImport (scratch))
@@ -305,9 +301,10 @@ SerializedModuleLoaderBase::findModule(AccessPathElem moduleID,
305
301
306
302
// Search the runtime import path.
307
303
isFramework = false ;
308
- return !openModuleFiles (moduleID, Ctx.SearchPathOpts .RuntimeLibraryImportPath ,
309
- moduleFilename.str (), moduleDocFilename.str (),
310
- moduleBuffer, moduleDocBuffer, scratch);
304
+ return !findModuleFilesInDirectory (
305
+ moduleID, Ctx.SearchPathOpts .RuntimeLibraryImportPath ,
306
+ moduleFilename.str (), moduleDocFilename.str (), moduleBuffer,
307
+ moduleDocBuffer);
311
308
}
312
309
313
310
static std::pair<StringRef, clang::VersionTuple>
0 commit comments