From 1782e8f9e882e8f4fb59968ff555c8c93827ea02 Mon Sep 17 00:00:00 2001 From: Chuanqi Xu Date: Wed, 8 Feb 2023 16:24:39 +0800 Subject: [C++20] [Modules] Allow -fmodule-file== for implementation unit and document the behavior Close https://github.com/llvm/llvm-project/issues/57293. Previsouly we can't use `-fmodule-file==` for implementation units, it is a bug. Also the behavior of the above option is not tested nor documented for C++20 Modules. This patch addresses the 2 problems. --- clang/lib/Sema/SemaModule.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'clang/lib/Sema/SemaModule.cpp') diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp index f52c024..194239a 100644 --- a/clang/lib/Sema/SemaModule.cpp +++ b/clang/lib/Sema/SemaModule.cpp @@ -337,20 +337,29 @@ Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc, } case ModuleDeclKind::Implementation: { - std::pair ModuleNameLoc( - PP.getIdentifierInfo(ModuleName), Path[0].second); // C++20 A module-declaration that contains neither an export- // keyword nor a module-partition implicitly imports the primary // module interface unit of the module as if by a module-import- // declaration. + std::pair ModuleNameLoc( + PP.getIdentifierInfo(ModuleName), Path[0].second); + + // The module loader will assume we're trying to import the module that + // we're building if `LangOpts.CurrentModule` equals to 'ModuleName'. + // Change the value for `LangOpts.CurrentModule` temporarily to make the + // module loader work properly. + const_cast(getLangOpts()).CurrentModule = ""; Mod = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc}, Module::AllVisible, /*IsInclusionDirective=*/false); + const_cast(getLangOpts()).CurrentModule = ModuleName; + if (!Mod) { Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName; // Create an empty module interface unit for error recovery. Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName); } + } break; case ModuleDeclKind::PartitionImplementation: -- cgit v1.1