Author: Michael Park Date: 2025-04-13T22:29:27-07:00 New Revision: 63e2963f4a24cb9365d1224e69f64bf643171023
URL: https://github.com/llvm/llvm-project/commit/63e2963f4a24cb9365d1224e69f64bf643171023 DIFF: https://github.com/llvm/llvm-project/commit/63e2963f4a24cb9365d1224e69f64bf643171023.diff LOG: Support '-fmodule-file-home-is-cwd' for C++ modules. (#135147) Added: clang/test/Modules/relocatable-modules.cpp Modified: clang/lib/Serialization/ASTWriter.cpp Removed: ################################################################################ diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index a48c05061626a..95b5718f1d140 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -1493,42 +1493,45 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, StringRef isysroot) { unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev)); RecordData::value_type Record[] = {MODULE_NAME}; Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name); - } - if (WritingModule && WritingModule->Directory) { - SmallString<128> BaseDir; - if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) { - // Use the current working directory as the base path for all inputs. - auto CWD = FileMgr.getOptionalDirectoryRef("."); - BaseDir.assign(CWD->getName()); - } else { - BaseDir.assign(WritingModule->Directory->getName()); - } - cleanPathForOutput(FileMgr, BaseDir); - - // If the home of the module is the current working directory, then we - // want to pick up the cwd of the build process loading the module, not - // our cwd, when we load this module. - if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd && - (!PP.getHeaderSearchInfo() - .getHeaderSearchOpts() - .ModuleMapFileHomeIsCwd || - WritingModule->Directory->getName() != ".")) { - // Module directory. - auto Abbrev = std::make_shared<BitCodeAbbrev>(); - Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY)); - Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory - unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev)); + auto BaseDir = [&]() -> std::optional<SmallString<128>> { + if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) { + // Use the current working directory as the base path for all inputs. + auto CWD = FileMgr.getOptionalDirectoryRef("."); + return CWD->getName(); + } + if (WritingModule->Directory) { + return WritingModule->Directory->getName(); + } + return std::nullopt; + }(); + if (BaseDir) { + cleanPathForOutput(FileMgr, *BaseDir); + + // If the home of the module is the current working directory, then we + // want to pick up the cwd of the build process loading the module, not + // our cwd, when we load this module. + if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd && + (!PP.getHeaderSearchInfo() + .getHeaderSearchOpts() + .ModuleMapFileHomeIsCwd || + WritingModule->Directory->getName() != ".")) { + // Module directory. + auto Abbrev = std::make_shared<BitCodeAbbrev>(); + Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY)); + Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory + unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev)); + + RecordData::value_type Record[] = {MODULE_DIRECTORY}; + Stream.EmitRecordWithBlob(AbbrevCode, Record, *BaseDir); + } - RecordData::value_type Record[] = {MODULE_DIRECTORY}; - Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir); + // Write out all other paths relative to the base directory if possible. + BaseDirectory.assign(BaseDir->begin(), BaseDir->end()); + } else if (!isysroot.empty()) { + // Write out paths relative to the sysroot if possible. + BaseDirectory = std::string(isysroot); } - - // Write out all other paths relative to the base directory if possible. - BaseDirectory.assign(BaseDir.begin(), BaseDir.end()); - } else if (!isysroot.empty()) { - // Write out paths relative to the sysroot if possible. - BaseDirectory = std::string(isysroot); } // Module map file diff --git a/clang/test/Modules/relocatable-modules.cpp b/clang/test/Modules/relocatable-modules.cpp new file mode 100644 index 0000000000000..c8d1e6d455666 --- /dev/null +++ b/clang/test/Modules/relocatable-modules.cpp @@ -0,0 +1,54 @@ +// RUN: rm -rf %t +// RUN: mkdir -p %t +// RUN: split-file %s %t +// RUN: cd %t + +// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-01.h \ +// RUN: -fmodule-name=hu-01 -o hu-01.pcm + +// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-02.h \ +// RUN: -Wno-experimental-header-units -fmodule-file=hu-01.pcm -o hu-02-abs.pcm + +// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-02.h \ +// RUN: -Wno-experimental-header-units -fmodule-file=hu-01.pcm -o hu-02-rel.pcm \ +// RUN: -fmodule-file-home-is-cwd + +// RUN: %clang -module-file-info hu-02-abs.pcm | FileCheck %s --check-prefix=IMPORT-ABS -DPREFIX=%t +// IMPORT-ABS: Imports module 'hu-01': [[PREFIX]]{{/|\\}}hu-01.pcm + +// RUN: %clang -module-file-info hu-02-rel.pcm | FileCheck %s --check-prefix=IMPORT-REL +// IMPORT-REL: Imports module 'hu-01': hu-01.pcm + +// RUN: llvm-bcanalyzer --dump --disable-histogram %t/hu-02-abs.pcm \ +// RUN: | FileCheck %s --check-prefix=INPUT-ABS -DPREFIX=%t +// INPUT-ABS: <INPUT_FILE {{.*}}/> blob data = '[[PREFIX]]{{/|\\}}hu-02.h' + +// RUN: llvm-bcanalyzer --dump --disable-histogram %t/hu-02-rel.pcm \ +// RUN: | FileCheck %s --check-prefix=INPUT-REL +// INPUT-REL: <INPUT_FILE {{.*}}/> blob data = 'hu-02.h' + +//--- hu-01.h +inline void f() {} + +//--- hu-02.h +import "hu-01.h"; + +inline void g() { + f(); +} + +// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/a.cppm -o %t/a-abs.pcm + +// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/a.cppm -o %t/a-rel.pcm \ +// RUN: -fmodule-file-home-is-cwd + +// RUN: llvm-bcanalyzer --dump --disable-histogram %t/a-abs.pcm \ +// RUN: | FileCheck %s --check-prefix=M-INPUT-ABS -DPREFIX=%t +// M-INPUT-ABS: <INPUT_FILE {{.*}}/> blob data = '[[PREFIX]]{{/|\\}}a.cppm' + +// RUN: llvm-bcanalyzer --dump --disable-histogram %t/a-rel.pcm \ +// RUN: | FileCheck %s --check-prefix=M-INPUT-REL +// M-INPUT-REL: <INPUT_FILE {{.*}}/> blob data = 'a.cppm' + +//--- a.cppm +export module a; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits