Index: clang/include/clang/Basic/LangOptions.def =================================================================== --- clang/include/clang/Basic/LangOptions.def +++ clang/include/clang/Basic/LangOptions.def @@ -171,6 +171,7 @@ LANGOPT(Modules , 1, 0, "modules semantics") COMPATIBLE_LANGOPT(ModulesTS , 1, 0, "C++ Modules TS syntax") COMPATIBLE_LANGOPT(CPlusPlusModules, 1, 0, "C++ modules syntax") +COMPATIBLE_LANGOPT(AllAdditionalTUReachable, 1, 0, "C++ modules syntax") BENIGN_ENUM_LANGOPT(CompilingModule, CompilingModuleKind, 3, CMK_None, "compiling a module interface") BENIGN_LANGOPT(CompilingPCH, 1, 0, "building a pch") Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -1428,6 +1428,10 @@ NegFlag, PosFlag, BothFlags<[NoXarchOption], " modules for C++">>, ShouldParseIf; +defm all_additional_TU_reachable : BoolFOption<"all-additional-TU-reachable", + LangOpts<"AllAdditionalTUReachable">, DefaultFalse, + NegFlag, PosFlag, + BothFlags<[NoXarchOption], " treat all additional variable as reachable">>; def fdebug_pass_arguments : Flag<["-"], "fdebug-pass-arguments">, Group; def fdebug_pass_structure : Flag<["-"], "fdebug-pass-structure">, Group; def fdepfile_entry : Joined<["-"], "fdepfile-entry=">, Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -3640,6 +3640,12 @@ options::OPT_fno_implicit_module_maps, HaveClangModules)) CmdArgs.push_back("-fimplicit-module-maps"); + // -fall-additional-TU-reachable enables to treat all translation unit as + // reachable. + if (Args.hasFlag(options::OPT_fall_additional_TU_reachable, + options::OPT_fno_all_additional_TU_reachable, false)) + CmdArgs.push_back("-fall-additional-TU-reachable"); + // -fmodules-decluse checks that modules used are declared so (off by default) Args.addOptInFlag(CmdArgs, options::OPT_fmodules_decluse, options::OPT_fno_modules_decluse); Index: clang/lib/Sema/SemaLookup.cpp =================================================================== --- clang/lib/Sema/SemaLookup.cpp +++ clang/lib/Sema/SemaLookup.cpp @@ -1953,9 +1953,9 @@ // considered reachable, but it is unspecified which are and under what // circumstances. // - // The decision here is to treat all additional tranditional units as - // unreachable. - return false; + // The default value for AllAdditionalTUReachable is false. It implies all + // additional translation unit is not considered as reachable for portability. + return SemaRef.getLangOpts().AllAdditionalTUReachable; } bool Sema::isAcceptableSlow(const NamedDecl *D, Sema::AcceptableKind Kind) { Index: clang/test/CXX/module/module.reach/ex1.cpp =================================================================== --- clang/test/CXX/module/module.reach/ex1.cpp +++ clang/test/CXX/module/module.reach/ex1.cpp @@ -10,6 +10,7 @@ // // RUN: %clang_cc1 -std=c++20 -emit-module-interface -fprebuilt-module-path=%t %t/M.cppm -o %t/M.pcm // RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/X.cppm -fsyntax-only -verify +// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/X-relax.cppm -fsyntax-only -verify -fall-additional-TU-reachable // //--- M-A.cppm export module M:A; @@ -41,3 +42,11 @@ // expected-note@* {{definition here is not reachable}} expected-note@* {{}} // FIXME: We should emit an error for unreachable definition of B. void g() { f(); } + +//--- X-relax.cppm +// Tests that it wouldn't complain if we treat all additional TU as reachable TU. +// expected-no-diagnostics +export module X; +import M; +B b3; +void g() { f(); } Index: clang/test/CXX/module/module.reach/p2.cpp =================================================================== --- clang/test/CXX/module/module.reach/p2.cpp +++ clang/test/CXX/module/module.reach/p2.cpp @@ -3,6 +3,7 @@ // RUN: split-file %s %t // RUN: %clang_cc1 -std=c++20 %t/impl.cppm -emit-module-interface -o %t/M-impl.pcm // RUN: %clang_cc1 -std=c++20 %t/M.cppm -emit-module-interface -fprebuilt-module-path=%t -o %t/M.pcm +// RUN: %clang_cc1 -std=c++20 %t/Use.cpp -fprebuilt-module-path=%t -verify -fsyntax-only -fall-additional-TU-reachable // RUN: %clang_cc1 -std=c++20 %t/UseStrict.cpp -fprebuilt-module-path=%t -verify -fsyntax-only //--- impl.cppm @@ -14,6 +15,13 @@ import :impl; export A f(); +//--- Use.cpp +// expected-no-diagnostics +import M; +void test() { + auto a = f(); +} + //--- UseStrict.cpp import M; void test() {