Skip to content

Commit ced8e5b

Browse files
jansvoboda11Lukacma
authored andcommitted
[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 d31cd39 commit ced8e5b

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

clang/lib/Frontend/CompilerInstance.cpp

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

549-
if (getHeaderSearchOpts().ModuleCachePath.empty())
550-
return "";
551-
552549
// Set up the module path, including the hash for the module-creation options.
553550
SmallString<256> SpecificModuleCache;
554551
normalizeModuleCachePath(*FileMgr, getHeaderSearchOpts().ModuleCachePath,
555552
SpecificModuleCache);
556-
if (!getHeaderSearchOpts().DisableModuleHash)
553+
if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash)
557554
llvm::sys::path::append(SpecificModuleCache, ModuleHash);
558555
return std::string(SpecificModuleCache);
559556
}

clang/lib/Lex/HeaderSearch.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,8 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(
21862186
void clang::normalizeModuleCachePath(FileManager &FileMgr, StringRef Path,
21872187
SmallVectorImpl<char> &NormalizedPath) {
21882188
NormalizedPath.assign(Path.begin(), Path.end());
2189-
FileMgr.makeAbsolutePath(NormalizedPath);
2190-
llvm::sys::path::remove_dots(NormalizedPath);
2189+
if (!NormalizedPath.empty()) {
2190+
FileMgr.makeAbsolutePath(NormalizedPath);
2191+
llvm::sys::path::remove_dots(NormalizedPath);
2192+
}
21912193
}

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,12 @@ bool initializeScanCompilerInstance(
524524
// Use the dependency scanning optimized file system if requested to do so.
525525
if (DepFS) {
526526
DepFS->resetBypassedPathPrefix();
527-
if (!ScanInstance.getHeaderSearchOpts().ModuleCachePath.empty()) {
528-
SmallString<256> ModulesCachePath;
529-
normalizeModuleCachePath(
530-
ScanInstance.getFileManager(),
531-
ScanInstance.getHeaderSearchOpts().ModuleCachePath, ModulesCachePath);
527+
SmallString<256> ModulesCachePath;
528+
normalizeModuleCachePath(ScanInstance.getFileManager(),
529+
ScanInstance.getHeaderSearchOpts().ModuleCachePath,
530+
ModulesCachePath);
531+
if (!ModulesCachePath.empty())
532532
DepFS->setBypassedPathPrefix(ModulesCachePath);
533-
}
534533

535534
ScanInstance.setDependencyDirectivesGetter(
536535
std::make_unique<ScanningDependencyDirectivesGetter>(
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)