Skip to content

Commit 63e2963

Browse files
authored
Support '-fmodule-file-home-is-cwd' for C++ modules. (llvm#135147)
1 parent e57f4e8 commit 63e2963

File tree

2 files changed

+90
-33
lines changed

2 files changed

+90
-33
lines changed

clang/lib/Serialization/ASTWriter.cpp

+36-33
Original file line numberDiff line numberDiff line change
@@ -1493,42 +1493,45 @@ 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+
1511+
// If the home of the module is the current working directory, then we
1512+
// want to pick up the cwd of the build process loading the module, not
1513+
// our cwd, when we load this module.
1514+
if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
1515+
(!PP.getHeaderSearchInfo()
1516+
.getHeaderSearchOpts()
1517+
.ModuleMapFileHomeIsCwd ||
1518+
WritingModule->Directory->getName() != ".")) {
1519+
// Module directory.
1520+
auto Abbrev = std::make_shared<BitCodeAbbrev>();
1521+
Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1522+
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1523+
unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1524+
1525+
RecordData::value_type Record[] = {MODULE_DIRECTORY};
1526+
Stream.EmitRecordWithBlob(AbbrevCode, Record, *BaseDir);
1527+
}
15221528

1523-
RecordData::value_type Record[] = {MODULE_DIRECTORY};
1524-
Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1529+
// Write out all other paths relative to the base directory if possible.
1530+
BaseDirectory.assign(BaseDir->begin(), BaseDir->end());
1531+
} else if (!isysroot.empty()) {
1532+
// Write out paths relative to the sysroot if possible.
1533+
BaseDirectory = std::string(isysroot);
15251534
}
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);
15321535
}
15331536

15341537
// Module map file
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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-file=hu-01.pcm -o hu-02-abs.pcm
11+
12+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-02.h \
13+
// RUN: -Wno-experimental-header-units -fmodule-file=hu-01.pcm -o hu-02-rel.pcm \
14+
// RUN: -fmodule-file-home-is-cwd
15+
16+
// RUN: %clang -module-file-info hu-02-abs.pcm | FileCheck %s --check-prefix=IMPORT-ABS -DPREFIX=%t
17+
// IMPORT-ABS: Imports module 'hu-01': [[PREFIX]]{{/|\\}}hu-01.pcm
18+
19+
// RUN: %clang -module-file-info hu-02-rel.pcm | FileCheck %s --check-prefix=IMPORT-REL
20+
// IMPORT-REL: Imports module 'hu-01': hu-01.pcm
21+
22+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/hu-02-abs.pcm \
23+
// RUN: | FileCheck %s --check-prefix=INPUT-ABS -DPREFIX=%t
24+
// INPUT-ABS: <INPUT_FILE {{.*}}/> blob data = '[[PREFIX]]{{/|\\}}hu-02.h'
25+
26+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/hu-02-rel.pcm \
27+
// RUN: | FileCheck %s --check-prefix=INPUT-REL
28+
// INPUT-REL: <INPUT_FILE {{.*}}/> blob data = 'hu-02.h'
29+
30+
//--- hu-01.h
31+
inline void f() {}
32+
33+
//--- hu-02.h
34+
import "hu-01.h";
35+
36+
inline void g() {
37+
f();
38+
}
39+
40+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/a.cppm -o %t/a-abs.pcm
41+
42+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/a.cppm -o %t/a-rel.pcm \
43+
// RUN: -fmodule-file-home-is-cwd
44+
45+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/a-abs.pcm \
46+
// RUN: | FileCheck %s --check-prefix=M-INPUT-ABS -DPREFIX=%t
47+
// M-INPUT-ABS: <INPUT_FILE {{.*}}/> blob data = '[[PREFIX]]{{/|\\}}a.cppm'
48+
49+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/a-rel.pcm \
50+
// RUN: | FileCheck %s --check-prefix=M-INPUT-REL
51+
// M-INPUT-REL: <INPUT_FILE {{.*}}/> blob data = 'a.cppm'
52+
53+
//--- a.cppm
54+
export module a;

0 commit comments

Comments
 (0)