[clang][lex] NFCI: Use DirectoryEntryRef in ModuleMap::parseModuleMapFile()
authorJan Svoboda <jan_svoboda@apple.com>
Thu, 25 May 2023 21:13:15 +0000 (14:13 -0700)
committerJan Svoboda <jan_svoboda@apple.com>
Tue, 30 May 2023 20:54:06 +0000 (13:54 -0700)
This patch changes the argument type of `ModuleMap::parseModuleMapFile()` from `const DirectoryEntry *` to `DirectoryEntryRef` in order to remove the deprecated uses of `DirectoryEntry::getName()`.

Depends on D127648.

Reviewed By: bnbarham

Differential Revision: https://reviews.llvm.org/D127651

clang-tools-extra/modularize/ModularizeUtilities.cpp
clang/include/clang/Lex/ModuleMap.h
clang/lib/Lex/ModuleMap.cpp

index a7cadf8..5b09c91 100644 (file)
@@ -258,34 +258,33 @@ std::error_code ModularizeUtilities::loadProblemHeaderList(
 std::error_code ModularizeUtilities::loadModuleMap(
     llvm::StringRef InputPath) {
   // Get file entry for module.modulemap file.
-  auto ModuleMapEntryOrErr =
-    SourceMgr->getFileManager().getFile(InputPath);
+  auto ModuleMapEntryOrErr = SourceMgr->getFileManager().getFileRef(InputPath);
 
   // return error if not found.
   if (!ModuleMapEntryOrErr) {
     llvm::errs() << "error: File \"" << InputPath << "\" not found.\n";
-    return ModuleMapEntryOrErr.getError();
+    return errorToErrorCode(ModuleMapEntryOrErr.takeError());
   }
-  const FileEntry *ModuleMapEntry = *ModuleMapEntryOrErr;
+  FileEntryRef ModuleMapEntry = *ModuleMapEntryOrErr;
 
   // Because the module map parser uses a ForwardingDiagnosticConsumer,
   // which doesn't forward the BeginSourceFile call, we do it explicitly here.
   DC.BeginSourceFile(*LangOpts, nullptr);
 
   // Figure out the home directory for the module map file.
-  const DirectoryEntry *Dir = ModuleMapEntry->getDir();
-  StringRef DirName(Dir->getName());
+  DirectoryEntryRef Dir = ModuleMapEntry.getDir();
+  StringRef DirName(Dir.getName());
   if (llvm::sys::path::filename(DirName) == "Modules") {
     DirName = llvm::sys::path::parent_path(DirName);
     if (DirName.endswith(".framework")) {
-      if (auto DirEntry = FileMgr->getDirectory(DirName))
-        Dir = *DirEntry;
-      else
-        Dir = nullptr;
+      auto FrameworkDirOrErr = FileMgr->getDirectoryRef(DirName);
+      if (!FrameworkDirOrErr) {
+        // This can happen if there's a race between the above check and the
+        // removal of the directory.
+        return errorToErrorCode(FrameworkDirOrErr.takeError());
+      }
+      Dir = *FrameworkDirOrErr;
     }
-    // FIXME: This assert can fail if there's a race between the above check
-    // and the removal of the directory.
-    assert(Dir && "parent must exist");
   }
 
   std::unique_ptr<ModuleMap> ModMap;
index d291afa..79cf0af 100644 (file)
@@ -729,8 +729,8 @@ public:
   ///
   /// \returns true if an error occurred, false otherwise.
   bool parseModuleMapFile(const FileEntry *File, bool IsSystem,
-                          const DirectoryEntry *HomeDir,
-                          FileID ID = FileID(), unsigned *Offset = nullptr,
+                          DirectoryEntryRef HomeDir, FileID ID = FileID(),
+                          unsigned *Offset = nullptr,
                           SourceLocation ExternModuleLoc = SourceLocation());
 
   /// Dump the contents of the module map, for debugging purposes.
index efe2df0..833287c 100644 (file)
@@ -1518,7 +1518,7 @@ namespace clang {
 
     /// The directory that file names in this module map file should
     /// be resolved relative to.
-    const DirectoryEntry *Directory;
+    DirectoryEntryRef Directory;
 
     /// Whether this module map is in a system header directory.
     bool IsSystem;
@@ -1584,7 +1584,7 @@ namespace clang {
     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
                              const TargetInfo *Target, DiagnosticsEngine &Diags,
                              ModuleMap &Map, const FileEntry *ModuleMapFile,
-                             const DirectoryEntry *Directory, bool IsSystem)
+                             DirectoryEntryRef Directory, bool IsSystem)
         : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
           ModuleMapFile(ModuleMapFile), Directory(Directory),
           IsSystem(IsSystem) {
@@ -2254,16 +2254,16 @@ void ModuleMapParser::parseExternModuleDecl() {
   StringRef FileNameRef = FileName;
   SmallString<128> ModuleMapFileName;
   if (llvm::sys::path::is_relative(FileNameRef)) {
-    ModuleMapFileName += Directory->getName();
+    ModuleMapFileName += Directory.getName();
     llvm::sys::path::append(ModuleMapFileName, FileName);
     FileNameRef = ModuleMapFileName;
   }
-  if (auto File = SourceMgr.getFileManager().getFile(FileNameRef))
+  if (auto File = SourceMgr.getFileManager().getOptionalFileRef(FileNameRef))
     Map.parseModuleMapFile(
         *File, IsSystem,
         Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd
             ? Directory
-            : (*File)->getDir(),
+            : File->getDir(),
         FileID(), nullptr, ExternLoc);
 }
 
@@ -2518,7 +2518,7 @@ void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
     Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName);
   } else {
     SmallString<128> PathName;
-    PathName = Directory->getName();
+    PathName = Directory.getName();
     llvm::sys::path::append(PathName, DirName);
     Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(PathName);
   }
@@ -3080,7 +3080,7 @@ bool ModuleMapParser::parseModuleMapFile() {
 }
 
 bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem,
-                                   const DirectoryEntry *Dir, FileID ID,
+                                   DirectoryEntryRef Dir, FileID ID,
                                    unsigned *Offset,
                                    SourceLocation ExternModuleLoc) {
   assert(Target && "Missing target information");