diff options
author | Michael Park <mcypark@gmail.com> | 2025-04-13 22:29:27 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-13 22:29:27 -0700 |
commit | 63e2963f4a24cb9365d1224e69f64bf643171023 (patch) | |
tree | 15df7ce0ea7e55e1f72655758fba0352ff2411c0 /clang/lib/Serialization/ASTWriter.cpp | |
parent | e57f4e8969db32f075d8f3e554506ec8b187a2f1 (diff) | |
download | llvm-63e2963f4a24cb9365d1224e69f64bf643171023.zip llvm-63e2963f4a24cb9365d1224e69f64bf643171023.tar.gz llvm-63e2963f4a24cb9365d1224e69f64bf643171023.tar.bz2 |
Support '-fmodule-file-home-is-cwd' for C++ modules. (#135147)
Diffstat (limited to 'clang/lib/Serialization/ASTWriter.cpp')
-rw-r--r-- | clang/lib/Serialization/ASTWriter.cpp | 69 |
1 files changed, 36 insertions, 33 deletions
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index a48c050..95b5718 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 |