diff --git a/clang/include/clang/Basic/DirectoryEntry.h b/clang/include/clang/Basic/DirectoryEntry.h index 6580e54e3c58b..5d083e68facd7 100644 --- a/clang/include/clang/Basic/DirectoryEntry.h +++ b/clang/include/clang/Basic/DirectoryEntry.h @@ -72,7 +72,7 @@ class DirectoryEntryRef { bool isSameRef(DirectoryEntryRef RHS) const { return ME == RHS.ME; } DirectoryEntryRef() = delete; - DirectoryEntryRef(const MapEntry &ME) : ME(&ME) {} + explicit DirectoryEntryRef(const MapEntry &ME) : ME(&ME) {} /// Allow DirectoryEntryRef to degrade into 'const DirectoryEntry*' to /// facilitate incremental adoption. @@ -197,6 +197,21 @@ static_assert(std::is_trivially_copyable::value, } // namespace clang namespace llvm { + +template <> struct PointerLikeTypeTraits { + static inline void *getAsVoidPointer(clang::DirectoryEntryRef Dir) { + return const_cast(&Dir.getMapEntry()); + } + + static inline clang::DirectoryEntryRef getFromVoidPointer(void *Ptr) { + return clang::DirectoryEntryRef( + *reinterpret_cast(Ptr)); + } + + static constexpr int NumLowBitsAvailable = PointerLikeTypeTraits< + const clang::DirectoryEntryRef::MapEntry *>::NumLowBitsAvailable; +}; + /// Specialisation of DenseMapInfo for DirectoryEntryRef. template <> struct DenseMapInfo { static inline clang::DirectoryEntryRef getEmptyKey() { diff --git a/clang/include/clang/Basic/FileEntry.h b/clang/include/clang/Basic/FileEntry.h index fdeafe5348cd0..50110b8572ef4 100644 --- a/clang/include/clang/Basic/FileEntry.h +++ b/clang/include/clang/Basic/FileEntry.h @@ -234,6 +234,21 @@ static_assert(std::is_trivially_copyable::value, } // namespace clang namespace llvm { + +template <> struct PointerLikeTypeTraits { + static inline void *getAsVoidPointer(clang::FileEntryRef File) { + return const_cast(&File.getMapEntry()); + } + + static inline clang::FileEntryRef getFromVoidPointer(void *Ptr) { + return clang::FileEntryRef( + *reinterpret_cast(Ptr)); + } + + static constexpr int NumLowBitsAvailable = PointerLikeTypeTraits< + const clang::FileEntryRef::MapEntry *>::NumLowBitsAvailable; +}; + /// Specialisation of DenseMapInfo for FileEntryRef. template <> struct DenseMapInfo { static inline clang::FileEntryRef getEmptyKey() { diff --git a/clang/include/clang/Basic/Module.h b/clang/include/clang/Basic/Module.h index b3b5376160107..a4ad8ad2f768f 100644 --- a/clang/include/clang/Basic/Module.h +++ b/clang/include/clang/Basic/Module.h @@ -156,9 +156,7 @@ class alignas(8) Module { std::string PresumedModuleMapFile; /// The umbrella header or directory. - llvm::PointerUnion - Umbrella; + llvm::PointerUnion Umbrella; /// The module signature. ASTFileSignature Signature; @@ -650,19 +648,18 @@ class alignas(8) Module { /// Retrieve the umbrella directory as written. std::optional getUmbrellaDirAsWritten() const { - if (const auto *ME = - Umbrella.dyn_cast()) + if (Umbrella && Umbrella.is()) return DirectoryName{UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory, - DirectoryEntryRef(*ME)}; + Umbrella.get()}; return std::nullopt; } /// Retrieve the umbrella header as written. std::optional
getUmbrellaHeaderAsWritten() const { - if (const auto *ME = Umbrella.dyn_cast()) + if (Umbrella && Umbrella.is()) return Header{UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory, - FileEntryRef(*ME)}; + Umbrella.get()}; return std::nullopt; } diff --git a/clang/lib/Basic/Module.cpp b/clang/lib/Basic/Module.cpp index 2bdbe8d2b110d..8ec68237a0fc0 100644 --- a/clang/lib/Basic/Module.cpp +++ b/clang/lib/Basic/Module.cpp @@ -264,10 +264,10 @@ bool Module::fullModuleNameIs(ArrayRef nameParts) const { } OptionalDirectoryEntryRef Module::getEffectiveUmbrellaDir() const { - if (const auto *ME = Umbrella.dyn_cast()) - return FileEntryRef(*ME).getDir(); - if (const auto *ME = Umbrella.dyn_cast()) - return DirectoryEntryRef(*ME); + if (Umbrella && Umbrella.is()) + return Umbrella.get().getDir(); + if (Umbrella && Umbrella.is()) + return Umbrella.get(); return std::nullopt; } diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp index 7a22fead3a089..c480719abc367 100644 --- a/clang/lib/Lex/ModuleMap.cpp +++ b/clang/lib/Lex/ModuleMap.cpp @@ -1162,7 +1162,7 @@ void ModuleMap::setUmbrellaHeaderAsWritten( Module *Mod, FileEntryRef UmbrellaHeader, const Twine &NameAsWritten, const Twine &PathRelativeToRootModuleDirectory) { Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); - Mod->Umbrella = &UmbrellaHeader.getMapEntry(); + Mod->Umbrella = UmbrellaHeader; Mod->UmbrellaAsWritten = NameAsWritten.str(); Mod->UmbrellaRelativeToRootModuleDirectory = PathRelativeToRootModuleDirectory.str(); @@ -1176,7 +1176,7 @@ void ModuleMap::setUmbrellaHeaderAsWritten( void ModuleMap::setUmbrellaDirAsWritten( Module *Mod, DirectoryEntryRef UmbrellaDir, const Twine &NameAsWritten, const Twine &PathRelativeToRootModuleDirectory) { - Mod->Umbrella = &UmbrellaDir.getMapEntry(); + Mod->Umbrella = UmbrellaDir; Mod->UmbrellaAsWritten = NameAsWritten.str(); Mod->UmbrellaRelativeToRootModuleDirectory = PathRelativeToRootModuleDirectory.str();