Skip to content

Commit f41f6ea

Browse files
authored
[C++20] [Modules] Offer -fmodules-embed-all-files option (llvm#107194)
See https://discourse.llvm.org/t/rfc-modules-should-we-embed-sources-to-the-bmi/81029 for details. Close llvm#72383
1 parent 6b8edc9 commit f41f6ea

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

clang/docs/StandardCPlusPlusModules.rst

+38
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,37 @@ Currently, Clang accepts the above example, though it may produce surprising
462462
results if the debugging code depends on consistent use of ``NDEBUG`` in other
463463
translation units.
464464

465+
Source Files Consistency
466+
^^^^^^^^^^^^^^^^^^^^^^^^
467+
468+
Clang may open the input files\ :sup:`1`` of a BMI during the compilation. This implies that
469+
when Clang consumes a BMI, all the input files need to be present in the original path
470+
and with the original contents.
471+
472+
To overcome these requirements and simplify cases like distributed builds and sandboxed
473+
builds, users can use the ``-fmodules-embed-all-files`` flag to embed all input files
474+
into the BMI so that Clang does not need to open the corresponding file on disk.
475+
476+
When the ``-fmodules-embed-all-files`` flag are enabled, Clang explicitly emits the source
477+
code into the BMI file, the contents of the BMI file contain a sufficiently verbose
478+
representation to reproduce the original source file.
479+
480+
:sup:`1`` Input files: The source files which took part in the compilation of the BMI.
481+
For example:
482+
483+
.. code-block:: c++
484+
485+
// M.cppm
486+
module;
487+
#include "foo.h"
488+
export module M;
489+
490+
// foo.h
491+
#pragma once
492+
#include "bar.h"
493+
494+
The ``M.cppm``, ``foo.h`` and ``bar.h`` are input files for the BMI of ``M.cppm``.
495+
465496
Object definition consistency
466497
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
467498

@@ -484,6 +515,13 @@ fragment is disabled by default. These checks can be enabled by specifying
484515
and you encounter incorrect or missing diagnostics, please report them via the
485516
`community issue tracker <https://github.com/llvm/llvm-project/issues/>`_.
486517

518+
Privacy Issue
519+
-------------
520+
521+
BMIs are not and should not be treated as an information hiding mechanism.
522+
They should always be assumed to contain all the information that was used to
523+
create them, in a recoverable form.
524+
487525
ABI Impacts
488526
-----------
489527

clang/include/clang/Driver/Options.td

+6-4
Original file line numberDiff line numberDiff line change
@@ -3177,6 +3177,12 @@ def modules_reduced_bmi : Flag<["-"], "fexperimental-modules-reduced-bmi">,
31773177
HelpText<"Generate the reduced BMI">,
31783178
MarshallingInfoFlag<FrontendOpts<"GenReducedBMI">>;
31793179

3180+
def fmodules_embed_all_files : Joined<["-"], "fmodules-embed-all-files">,
3181+
Visibility<[ClangOption, CC1Option, CLOption]>,
3182+
HelpText<"Embed the contents of all files read by this compilation into "
3183+
"the produced module file.">,
3184+
MarshallingInfoFlag<FrontendOpts<"ModulesEmbedAllFiles">>;
3185+
31803186
def fmodules_prune_interval : Joined<["-"], "fmodules-prune-interval=">, Group<i_Group>,
31813187
Visibility<[ClangOption, CC1Option]>, MetaVarName<"<seconds>">,
31823188
HelpText<"Specify the interval (in seconds) between attempts to prune the module cache">,
@@ -7685,10 +7691,6 @@ def fmodules_embed_file_EQ : Joined<["-"], "fmodules-embed-file=">,
76857691
HelpText<"Embed the contents of the specified file into the module file "
76867692
"being compiled.">,
76877693
MarshallingInfoStringVector<FrontendOpts<"ModulesEmbedFiles">>;
7688-
def fmodules_embed_all_files : Joined<["-"], "fmodules-embed-all-files">,
7689-
HelpText<"Embed the contents of all files read by this compilation into "
7690-
"the produced module file.">,
7691-
MarshallingInfoFlag<FrontendOpts<"ModulesEmbedAllFiles">>;
76927694
defm fimplicit_modules_use_lock : BoolOption<"f", "implicit-modules-use-lock",
76937695
FrontendOpts<"BuildingImplicitModuleUsesLock">, DefaultTrue,
76947696
NegFlag<SetFalse>,

clang/lib/Driver/ToolChains/Clang.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -4196,6 +4196,9 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
41964196
Args.ClaimAllArgs(options::OPT_fmodule_output_EQ);
41974197
}
41984198

4199+
if (Args.hasArg(options::OPT_fmodules_embed_all_files))
4200+
CmdArgs.push_back("-fmodules-embed-all-files");
4201+
41994202
return HaveModules;
42004203
}
42014204

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %clang -std=c++20 %s -fmodules-embed-all-files -### 2>&1 | FileCheck %s
2+
// CHECK: -fmodules-embed-all-files
3+
4+
// RUN: %clang -std=c++20 %s -### 2>&1 | FileCheck %s --check-prefix=NON-EMBED
5+
// NON-EMBED-NOT: -fmodules-embed-all-files

0 commit comments

Comments
 (0)