Skip to content

Commit 57f05d6

Browse files
authored
Merge pull request #41476 from ahoppen/pr/store-path-in-modulesearchpath
[Serialization] Store the path inside ModuleSearchPath as a string, not a StringRef
2 parents 496da9d + bffd3ca commit 57f05d6

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

include/swift/AST/SearchPathOptions.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ enum class ModuleSearchPathKind {
3636

3737
/// A single module search path that can come from different sources, e.g.
3838
/// framework search paths, import search path etc.
39-
struct ModuleSearchPath {
40-
/// The actual path of the module search path. References a search path string
41-
/// stored inside \c SearchPathOptions, which must outlive this reference.
42-
StringRef Path;
39+
class ModuleSearchPath : public llvm::RefCountedBase<ModuleSearchPath> {
40+
/// The actual path of the module search path.
41+
std::string Path;
4342

4443
/// The kind of the search path.
4544
ModuleSearchPathKind Kind;
@@ -52,6 +51,18 @@ struct ModuleSearchPath {
5251
/// different file names in \c searchPathsContainingFile.
5352
unsigned Index;
5453

54+
public:
55+
ModuleSearchPath(StringRef Path, ModuleSearchPathKind Kind, bool IsSystem,
56+
unsigned Index)
57+
: Path(Path), Kind(Kind), IsSystem(IsSystem), Index(Index) {}
58+
59+
StringRef getPath() const { return Path; }
60+
ModuleSearchPathKind getKind() const { return Kind; }
61+
62+
bool isSystem() const { return IsSystem; }
63+
64+
unsigned getIndex() const { return Index; }
65+
5566
bool operator<(const ModuleSearchPath &Other) const {
5667
if (this->Kind == Other.Kind) {
5768
return this->Index < Other.Index;
@@ -61,6 +72,8 @@ struct ModuleSearchPath {
6172
}
6273
};
6374

75+
using ModuleSearchPathPtr = llvm::IntrusiveRefCntPtr<ModuleSearchPath>;
76+
6477
class SearchPathOptions;
6578

6679
/// Maintains a mapping of filenames to search paths that contain a file with
@@ -97,7 +110,7 @@ class ModuleSearchPathLookup {
97110
const SearchPathOptions *Opts;
98111
} State;
99112

100-
llvm::StringMap<SmallVector<ModuleSearchPath, 4>> LookupTable;
113+
llvm::StringMap<SmallVector<ModuleSearchPathPtr, 4>> LookupTable;
101114

102115
/// Scan the directory at \p SearchPath for files and add those files to the
103116
/// lookup table. \p Kind specifies the search path kind and \p Index the

lib/AST/SearchPathOptions.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,24 @@ void ModuleSearchPathLookup::addFilesInPathToLookupTable(
1919
llvm::vfs::FileSystem *FS, StringRef SearchPath, ModuleSearchPathKind Kind,
2020
bool IsSystem, unsigned SearchPathIndex) {
2121
std::error_code Error;
22-
assert(llvm::all_of(LookupTable, [&](const auto &LookupTableEntry) {
23-
return llvm::none_of(LookupTableEntry.second, [&](const ModuleSearchPath &ExistingSearchPath) -> bool {
24-
return ExistingSearchPath.Kind == Kind && ExistingSearchPath.Index == SearchPathIndex;
22+
auto entryAlreadyExists = [this](ModuleSearchPathKind Kind,
23+
unsigned SearchPathIndex) -> bool {
24+
return llvm::any_of(LookupTable, [&](const auto &LookupTableEntry) {
25+
return llvm::any_of(
26+
LookupTableEntry.second, [&](ModuleSearchPathPtr ExistingSearchPath) {
27+
return ExistingSearchPath->getKind() == Kind &&
28+
ExistingSearchPath->getIndex() == SearchPathIndex;
29+
});
2530
});
26-
}) && "Search path with this kind and index already exists");
31+
};
32+
assert(!entryAlreadyExists(Kind, SearchPathIndex) &&
33+
"Search path with this kind and index already exists");
34+
ModuleSearchPathPtr TableEntry =
35+
new ModuleSearchPath(SearchPath, Kind, IsSystem, SearchPathIndex);
2736
for (auto Dir = FS->dir_begin(SearchPath, Error);
2837
!Error && Dir != llvm::vfs::directory_iterator(); Dir.increment(Error)) {
2938
StringRef Filename = llvm::sys::path::filename(Dir->path());
30-
LookupTable[Filename].push_back(
31-
{SearchPath, Kind, IsSystem, SearchPathIndex});
39+
LookupTable[Filename].push_back(TableEntry);
3240
}
3341
}
3442

@@ -92,8 +100,9 @@ ModuleSearchPathLookup::searchPathsContainingFile(
92100

93101
for (auto &Filename : Filenames) {
94102
for (auto &Entry : LookupTable[Filename]) {
95-
if (ResultIds.insert(std::make_pair(Entry.Kind, Entry.Index)).second) {
96-
Result.push_back(&Entry);
103+
if (ResultIds.insert(std::make_pair(Entry->getKind(), Entry->getIndex()))
104+
.second) {
105+
Result.push_back(Entry.get());
97106
}
98107
}
99108
}

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,10 @@ SerializedModuleLoaderBase::findModule(ImportPath::Element moduleID,
608608
InterestingFilenames, Ctx.SourceMgr.getFileSystem().get(),
609609
Ctx.LangOpts.Target.isOSDarwin());
610610
for (const auto &searchPath : searchPaths) {
611-
currPath = searchPath->Path;
612-
isSystemModule = searchPath->IsSystem;
611+
currPath = searchPath->getPath();
612+
isSystemModule = searchPath->isSystem();
613613

614-
switch (searchPath->Kind) {
614+
switch (searchPath->getKind()) {
615615
case ModuleSearchPathKind::Import:
616616
case ModuleSearchPathKind::RuntimeLibrary: {
617617
isFramework = false;
@@ -621,7 +621,7 @@ SerializedModuleLoaderBase::findModule(ImportPath::Element moduleID,
621621
// This was not always true on non-Apple platforms, and in order to
622622
// ease the transition, check both layouts.
623623
bool checkTargetSpecificModule = true;
624-
if (searchPath->Kind != ModuleSearchPathKind::RuntimeLibrary ||
624+
if (searchPath->getKind() != ModuleSearchPathKind::RuntimeLibrary ||
625625
!Ctx.LangOpts.Target.isOSDarwin()) {
626626
auto modulePath = currPath;
627627
llvm::sys::path::append(modulePath, genericModuleFileName);

0 commit comments

Comments
 (0)