Skip to content

Commit 828378b

Browse files
committed
Support '-fmodule-file-home-is-cwd' for C++ modules.
'-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 828378b

File tree

2 files changed

+78
-33
lines changed

2 files changed

+78
-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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: split-file %s %t
4+
5+
// CHECK-NOT: MODULE_DIRECTORY
6+
7+
// RUN: cd %t
8+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/a.cppm -o %t/a-abs.pcm
9+
10+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/a-abs.pcm | FileCheck %s
11+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/a-abs.pcm \
12+
// RUN: | FileCheck %s --check-prefix=INPUT-ABS -DPREFIX=%t
13+
14+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/a.cppm -o %t/a-rel.pcm \
15+
// RUN: -fmodule-file-home-is-cwd
16+
17+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/a-rel.pcm | FileCheck %s
18+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/a-rel.pcm \
19+
// RUN: | FileCheck %s --check-prefix=INPUT-REL
20+
21+
// INPUT-ABS: <INPUT_FILE {{.*}}/> blob data = '[[PREFIX]]{{/|\\}}a.cppm'
22+
// INPUT-REL: <INPUT_FILE {{.*}}/> blob data = 'a.cppm'
23+
24+
//--- a.cppm
25+
export module a;
26+
27+
// RUN: cd %S
28+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header Inputs/cxx-header.h \
29+
// RUN: -o %t/cxx-header-abs.pcm
30+
31+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/cxx-header-abs.pcm | FileCheck %s
32+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/cxx-header-abs.pcm \
33+
// RUN: | FileCheck %s --check-prefix=HU-INPUT-ABS -DPREFIX=%S
34+
35+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header Inputs/cxx-header.h \
36+
// RUN: -fmodule-file-home-is-cwd -o %t/cxx-header-rel.pcm
37+
38+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/cxx-header-rel.pcm | FileCheck %s
39+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/cxx-header-rel.pcm \
40+
// RUN: | FileCheck %s --check-prefix=HU-INPUT-REL
41+
42+
// HU-INPUT-ABS: <INPUT_FILE {{.*}}/> blob data = '[[PREFIX]]{{/|\\}}Inputs{{/|\\}}cxx-header.h'
43+
// HU-INPUT-REL: <INPUT_FILE {{.*}}/> blob data = 'Inputs{{/|\\}}cxx-header.h'

0 commit comments

Comments
 (0)