Skip to content

Commit

Permalink
[ClangModuleManager] Extend the ext4 file system workaround
Browse files Browse the repository at this point in the history
Extend the ext4 file system workaround so it covers `lookupByFileName`.
This doesn't fix all the problems from the workaround, but it allows
correct lookup of the previously added module/PCH files.

rdar://119269472
  • Loading branch information
cachemeifyoucan committed Jan 20, 2024
1 parent e92bb4e commit 52b09d8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion clang/lib/Basic/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ OptionalFileEntryRef FileManager::getBypassFile(FileEntryRef VF) {

// If we've already bypassed just use the existing one.
auto Insertion = SeenBypassFileEntries->insert(
{VF.getName(), std::errc::no_such_file_or_directory});
{VF.getNameAsRequested(), std::errc::no_such_file_or_directory});
if (!Insertion.second)
return FileEntryRef(*Insertion.first);

Expand Down
9 changes: 8 additions & 1 deletion clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1980,9 +1980,16 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(

// Check whether M refers to the file in the prebuilt module path.
if (M && M->getASTFile())
if (auto ModuleFile = FileMgr->getFile(ModuleFilename))
if (auto ModuleFile = FileMgr->getOptionalFileRef(ModuleFilename)) {
if (*ModuleFile == M->getASTFile())
return M;
#if !defined(__APPLE__)
// Workaround for ext4 file system. Also check bypass file if exists.
if (auto Bypass = FileMgr->getBypassFile(*ModuleFile))
if (*Bypass == M->getASTFile())
return M;
#endif
}

getDiagnostics().Report(ModuleNameLoc, diag::err_module_prebuilt)
<< ModuleName;
Expand Down
8 changes: 7 additions & 1 deletion clang/lib/Frontend/Rewrite/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,15 @@ class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {

void visitModuleFile(StringRef Filename,
serialization::ModuleKind Kind) override {
auto File = CI.getFileManager().getFile(Filename);
auto File = CI.getFileManager().getOptionalFileRef(Filename);
assert(File && "missing file for loaded module?");

#if !defined(__APPLE__)
// Workaround for ext4 file system.
if (auto Bypass = CI.getFileManager().getBypassFile(*File))
File = *Bypass;
#endif

// Only rewrite each module file once.
if (!Rewritten.insert(*File).second)
return;
Expand Down
19 changes: 16 additions & 3 deletions clang/lib/Serialization/ModuleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ using namespace clang;
using namespace serialization;

ModuleFile *ModuleManager::lookupByFileName(StringRef Name) const {
auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
/*CacheFailure=*/false);
auto Entry = FileMgr.getOptionalFileRef(Name, /*OpenFile=*/false,
/*CacheFailure=*/false);
#if !defined(__APPLE__)
if (Entry) {
// On Linux ext4 FileManager's inode caching system does not
// provide us correct behaviour for ModuleCache directories.
// inode can be reused after PCM delete resulting in cache misleading.
if (auto BypassFile = FileMgr.getBypassFile(*Entry))
Entry = *BypassFile;
}
#endif

if (Entry)
return lookup(*Entry);

Expand Down Expand Up @@ -449,7 +459,10 @@ bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize,
// On Linux ext4 FileManager's inode caching system does not
// provide us correct behaviour for ModuleCache directories.
// inode can be reused after PCM delete resulting in cache misleading.
File = FileMgr.getBypassFile(*File);
// Only use the bypass file if bypass succeed in case the underlying file
// system doesn't support bypass (thus there is no need for the workaround).
if (auto Bypass = FileMgr.getBypassFile(*File))
File = *Bypass;
}
#endif

Expand Down
3 changes: 3 additions & 0 deletions clang/test/Modules/explicit-build-relpath.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// Due to module bypass workaround in https://reviews.llvm.org/D97850 for ext4 FS, relative and absolute path do not match after bypass.
// REQUIRES: system-darwin

// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: cd %t
Expand Down

0 comments on commit 52b09d8

Please sign in to comment.