Skip to content

Commit 06b533a

Browse files
committed
Support '-fmodule-file-home-is-cwd' for C++ header units.
'-fmodule-file-home-is-cwd' was added in 646e502 to support relocatable PCMs for Clang modules. This PR extends the functionality for standard C++ modules.
1 parent 3fd0d22 commit 06b533a

File tree

2 files changed

+67
-33
lines changed

2 files changed

+67
-33
lines changed

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,42 +1493,44 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, StringRef isysroot) {
14931493
unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
14941494
RecordData::value_type Record[] = {MODULE_NAME};
14951495
Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1496-
}
14971496

1498-
if (WritingModule && WritingModule->Directory) {
1499-
SmallString<128> BaseDir;
1500-
if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
1501-
// Use the current working directory as the base path for all inputs.
1502-
auto CWD = FileMgr.getOptionalDirectoryRef(".");
1503-
BaseDir.assign(CWD->getName());
1504-
} else {
1505-
BaseDir.assign(WritingModule->Directory->getName());
1506-
}
1507-
cleanPathForOutput(FileMgr, BaseDir);
1508-
1509-
// If the home of the module is the current working directory, then we
1510-
// want to pick up the cwd of the build process loading the module, not
1511-
// our cwd, when we load this module.
1512-
if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
1513-
(!PP.getHeaderSearchInfo()
1514-
.getHeaderSearchOpts()
1515-
.ModuleMapFileHomeIsCwd ||
1516-
WritingModule->Directory->getName() != ".")) {
1517-
// Module directory.
1518-
auto Abbrev = std::make_shared<BitCodeAbbrev>();
1519-
Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1520-
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1521-
unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1497+
auto BaseDir = [&]() -> std::optional<SmallString<128>> {
1498+
if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
1499+
// Use the current working directory as the base path for all inputs.
1500+
auto CWD = FileMgr.getOptionalDirectoryRef(".");
1501+
return CWD->getName();
1502+
}
1503+
if (WritingModule->Directory) {
1504+
return WritingModule->Directory->getName();
1505+
}
1506+
return std::nullopt;
1507+
}();
1508+
if (BaseDir) {
1509+
cleanPathForOutput(FileMgr, *BaseDir);
1510+
// If the home of the module is the current working directory, then we
1511+
// want to pick up the cwd of the build process loading the module, not
1512+
// our cwd, when we load this module.
1513+
if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
1514+
(!PP.getHeaderSearchInfo()
1515+
.getHeaderSearchOpts()
1516+
.ModuleMapFileHomeIsCwd ||
1517+
WritingModule->Directory->getName() != ".")) {
1518+
// Module directory.
1519+
auto Abbrev = std::make_shared<BitCodeAbbrev>();
1520+
Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1521+
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1522+
unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1523+
1524+
RecordData::value_type Record[] = {MODULE_DIRECTORY};
1525+
Stream.EmitRecordWithBlob(AbbrevCode, Record, *BaseDir);
1526+
}
15221527

1523-
RecordData::value_type Record[] = {MODULE_DIRECTORY};
1524-
Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1528+
// Write out all other paths relative to the base directory if possible.
1529+
BaseDirectory.assign(BaseDir->begin(), BaseDir->end());
1530+
} else if (!isysroot.empty()) {
1531+
// Write out paths relative to the sysroot if possible.
1532+
BaseDirectory = std::string(isysroot);
15251533
}
1526-
1527-
// Write out all other paths relative to the base directory if possible.
1528-
BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1529-
} else if (!isysroot.empty()) {
1530-
// Write out paths relative to the sysroot if possible.
1531-
BaseDirectory = std::string(isysroot);
15321534
}
15331535

15341536
// Module map file
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: split-file %s %t
4+
// RUN: cd %t
5+
6+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-01.h \
7+
// RUN: -fmodule-name=hu-01 -o hu-01.pcm
8+
9+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-02.h \
10+
// RUN: -Wno-experimental-header-units -fmodule-name=hu-02 \
11+
// RUN: -fmodule-file=hu-01.pcm -o hu-02-abs.pcm
12+
13+
// RUN: %clang -module-file-info hu-02-abs.pcm | FileCheck %s --check-prefix=IMPORT-ABS -DPREFIX=%t
14+
15+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-02.h \
16+
// RUN: -Wno-experimental-header-units -fmodule-name=hu-02 \
17+
// RUN: -fmodule-file=hu-01.pcm -o hu-02-rel.pcm -fmodule-file-home-is-cwd
18+
19+
// RUN: %clang -module-file-info hu-02-rel.pcm | FileCheck %s --check-prefix=IMPORT-REL
20+
21+
// IMPORT-ABS: Imports module 'hu-01': [[PREFIX]]{{/|\\}}hu-01.pcm
22+
// IMPORT-REL: Imports module 'hu-01': hu-01.pcm
23+
24+
//--- hu-01.h
25+
inline void f() {}
26+
27+
//--- hu-02.h
28+
import "hu-01.h";
29+
30+
inline void g() {
31+
f();
32+
}

0 commit comments

Comments
 (0)