Skip to content

Commit 4df95e2

Browse files
committed
[clang] Make explicitly-built modules independent of the CWD (llvm#164840)
PR llvm#150123 changed how we normalize the modules cache path. Unfortunately, empty path would get normalized to the current working directory. This means that even explicitly-built PCMs that don't rely on the CWD now embed it, leading to surprising behavior. This PR fixes that by normalizing an empty modules cache path to an empty string.
1 parent fce17c2 commit 4df95e2

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,14 +563,11 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
563563
std::string CompilerInstance::getSpecificModuleCachePath(StringRef ModuleHash) {
564564
assert(FileMgr && "Specific module cache path requires a FileManager");
565565

566-
if (getHeaderSearchOpts().ModuleCachePath.empty())
567-
return "";
568-
569566
// Set up the module path, including the hash for the module-creation options.
570567
SmallString<256> SpecificModuleCache;
571568
normalizeModuleCachePath(*FileMgr, getHeaderSearchOpts().ModuleCachePath,
572569
SpecificModuleCache);
573-
if (!getHeaderSearchOpts().DisableModuleHash)
570+
if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash)
574571
llvm::sys::path::append(SpecificModuleCache, ModuleHash);
575572
return std::string(SpecificModuleCache);
576573
}

clang/lib/Lex/HeaderSearch.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,8 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(
20982098
void clang::normalizeModuleCachePath(FileManager &FileMgr, StringRef Path,
20992099
SmallVectorImpl<char> &NormalizedPath) {
21002100
NormalizedPath.assign(Path.begin(), Path.end());
2101-
FileMgr.makeAbsolutePath(NormalizedPath);
2102-
llvm::sys::path::remove_dots(NormalizedPath);
2101+
if (!NormalizedPath.empty()) {
2102+
FileMgr.makeAbsolutePath(NormalizedPath);
2103+
llvm::sys::path::remove_dots(NormalizedPath);
2104+
}
21032105
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This test checks that explicitly building the same module from different
2+
// working directories results in the same PCM contents.
3+
4+
// RUN: rm -rf %t
5+
// RUN: split-file %s %t
6+
// RUN: mkdir %t/one
7+
// RUN: mkdir %t/two
8+
9+
//--- module.modulemap
10+
module M { header "M.h" }
11+
12+
//--- M.h
13+
14+
// RUN: cd %t/one && %clang_cc1 -fmodules -emit-module %t/module.modulemap -fmodule-name=M -o %t/M_one.pcm
15+
// RUN: cd %t/two && %clang_cc1 -fmodules -emit-module %t/module.modulemap -fmodule-name=M -o %t/M_two.pcm
16+
17+
// RUN: diff %t/M_one.pcm %t/M_two.pcm

0 commit comments

Comments
 (0)