Skip to content

[modules] Accept equivalent module caches from different symlink #90925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,36 +829,37 @@ bool SimpleASTReaderListener::ReadPreprocessorOptions(
OptionValidateNone);
}

/// Check the header search options deserialized from the control block
/// against the header search options in an existing preprocessor.
/// Check that the specified and the existing module cache paths are equivalent.
///
/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
StringRef SpecificModuleCachePath,
StringRef ExistingModuleCachePath,
DiagnosticsEngine *Diags,
const LangOptions &LangOpts,
const PreprocessorOptions &PPOpts) {
if (LangOpts.Modules) {
if (SpecificModuleCachePath != ExistingModuleCachePath &&
!PPOpts.AllowPCHWithDifferentModulesCachePath) {
if (Diags)
Diags->Report(diag::err_pch_modulecache_mismatch)
<< SpecificModuleCachePath << ExistingModuleCachePath;
return true;
}
}

return false;
/// \returns true when the module cache paths differ.
static bool checkModuleCachePath(llvm::vfs::FileSystem &VFS,
StringRef SpecificModuleCachePath,
StringRef ExistingModuleCachePath,
DiagnosticsEngine *Diags,
const LangOptions &LangOpts,
const PreprocessorOptions &PPOpts) {
if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
SpecificModuleCachePath == ExistingModuleCachePath)
return false;
auto EqualOrErr =
VFS.equivalent(SpecificModuleCachePath, ExistingModuleCachePath);
if (EqualOrErr && *EqualOrErr)
return false;
if (Diags)
Diags->Report(diag::err_pch_modulecache_mismatch)
<< SpecificModuleCachePath << ExistingModuleCachePath;
return true;
}

bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
StringRef SpecificModuleCachePath,
bool Complain) {
return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
PP.getHeaderSearchInfo().getModuleCachePath(),
Complain ? &Reader.Diags : nullptr,
PP.getLangOpts(), PP.getPreprocessorOpts());
return checkModuleCachePath(Reader.getFileManager().getVirtualFileSystem(),
SpecificModuleCachePath,
PP.getHeaderSearchInfo().getModuleCachePath(),
Complain ? &Reader.Diags : nullptr,
PP.getLangOpts(), PP.getPreprocessorOpts());
}

void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
Expand Down Expand Up @@ -5389,9 +5390,9 @@ namespace {
bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
StringRef SpecificModuleCachePath,
bool Complain) override {
return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
ExistingModuleCachePath, nullptr,
ExistingLangOpts, ExistingPPOpts);
return checkModuleCachePath(
FileMgr.getVirtualFileSystem(), SpecificModuleCachePath,
ExistingModuleCachePath, nullptr, ExistingLangOpts, ExistingPPOpts);
}

bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Expand Down
14 changes: 14 additions & 0 deletions clang/test/Modules/module-symlink.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// REQUIRES: shell

// RUN: rm -rf %t
// RUN: %clang_cc1 -fmodules-cache-path=%t/modules -fmodules -fimplicit-module-maps -I %S/Inputs -emit-pch -o %t.pch %s -verify

// RUN: ln -s %t/modules %t/modules.symlink
// RUN: %clang_cc1 -fmodules-cache-path=%t/modules.symlink -fmodules -fimplicit-module-maps -I %S/Inputs -include-pch %t.pch %s -verify
// RUN: not %clang_cc1 -fmodules-cache-path=%t/modules.dne -fmodules -fimplicit-module-maps -I %S/Inputs -include-pch %t.pch %s -verify

// expected-no-diagnostics

@import ignored_macros;

struct Point p;
4 changes: 4 additions & 0 deletions llvm/include/llvm/Support/VirtualFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem>,
/// platform-specific error_code.
virtual std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const;

/// \returns true if \p A and \p B represent the same file, or an error or
/// false if they do not.
llvm::ErrorOr<bool> equivalent(const Twine &A, const Twine &B);

enum class PrintType { Summary, Contents, RecursiveContents };
void print(raw_ostream &OS, PrintType Type = PrintType::Contents,
unsigned IndentLevel = 0) const {
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Support/VirtualFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ bool FileSystem::exists(const Twine &Path) {
return Status && Status->exists();
}

llvm::ErrorOr<bool> FileSystem::equivalent(const Twine &A, const Twine &B) {
auto StatusA = status(A);
if (!StatusA)
return StatusA.getError();
auto StatusB = status(B);
if (!StatusB)
return StatusB.getError();
return StatusA->equivalent(*StatusB);
}

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void FileSystem::dump() const { print(dbgs(), PrintType::RecursiveContents); }
#endif
Expand Down