Skip to content

Commit bd576fe

Browse files
authored
[clang][driver][clang-cl] Support --precompile and -fmodule-* options in Clang-CL (#98761)
This PR is the first step in improving the situation for `clang-cl` detailed in [this LLVM Discourse thread](https://discourse.llvm.org/t/clang-cl-exe-support-for-c-modules/72257/28). There has been some work done in #89772. I believe this is somewhat orthogonal. This is a work-in-progress; the functionality has only been tested with the [basic 'Hello World' example](https://clang.llvm.org/docs/StandardCPlusPlusModules.html#quick-start), and proper test cases need to be written. I'd like some thoughts on this, thanks! Partially resolves #64118.
1 parent 3c3ea7e commit bd576fe

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

clang/docs/StandardCPlusPlusModules.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,16 @@ BMIs cannot be shipped in an archive to create a module library. Instead, the
398398
BMIs(``*.pcm``) are compiled into object files(``*.o``) and those object files
399399
are added to the archive instead.
400400

401+
clang-cl
402+
~~~~~~~~
403+
404+
``clang-cl`` supports the same options as ``clang++`` for modules as detailed above;
405+
there is no need to prefix these options with ``/clang:``. Note that ``cl.exe``
406+
`options to emit/consume IFC files <https://devblogs.microsoft.com/cppblog/using-cpp-modules-in-msvc-from-the-command-line-part-1/>` are *not* supported.
407+
The resultant precompiled modules are also not compatible for use with ``cl.exe``.
408+
409+
We recommend that build system authors use the above-mentioned ``clang++`` options with ``clang-cl`` to build modules.
410+
401411
Consistency Requirements
402412
~~~~~~~~~~~~~~~~~~~~~~~~
403413

@@ -1387,13 +1397,6 @@ have ``.cppm`` (or ``.ccm``, ``.cxxm``, ``.c++m``) as the file extension.
13871397
However, the behavior is inconsistent with other compilers. This is tracked by
13881398
`#57416 <https://github.com/llvm/llvm-project/issues/57416>`_.
13891399

1390-
clang-cl is not compatible with standard C++ modules
1391-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1392-
1393-
``/clang:-fmodule-file`` and ``/clang:-fprebuilt-module-path`` cannot be used
1394-
to specify the BMI with ``clang-cl.exe``. This is tracked by
1395-
`#64118 <https://github.com/llvm/llvm-project/issues/64118>`_.
1396-
13971400
Incorrect ODR violation diagnostics
13981401
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13991402

clang/docs/UsersManual.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4745,6 +4745,12 @@ Execute ``clang-cl /?`` to see a list of supported options:
47454745
-flto=<value> Set LTO mode to either 'full' or 'thin'
47464746
-flto Enable LTO in 'full' mode
47474747
-fmerge-all-constants Allow merging of constants
4748+
-fmodule-file=<module_name>=<module-file>
4749+
Use the specified module file that provides the module <module_name>
4750+
-fmodule-header=<header>
4751+
Build <header> as a C++20 header unit
4752+
-fmodule-output=<path>
4753+
Save intermediate module file results when compiling a standard C++ module unit.
47484754
-fms-compatibility-version=<value>
47494755
Dot-separated value representing the Microsoft compiler version
47504756
number to report in _MSC_VER (0 = don't define it; default is same value as installed cl.exe, or 1933)

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,7 +3105,7 @@ def fmodules_user_build_path : Separate<["-"], "fmodules-user-build-path">, Grou
31053105
HelpText<"Specify the module user build path">,
31063106
MarshallingInfoString<HeaderSearchOpts<"ModuleUserBuildPath">>;
31073107
def fprebuilt_module_path : Joined<["-"], "fprebuilt-module-path=">, Group<i_Group>,
3108-
Flags<[]>, Visibility<[ClangOption, CC1Option]>,
3108+
Flags<[]>, Visibility<[ClangOption, CLOption, CC1Option]>,
31093109
MetaVarName<"<directory>">,
31103110
HelpText<"Specify the prebuilt module path">;
31113111
defm prebuilt_implicit_modules : BoolFOption<"prebuilt-implicit-modules",
@@ -3114,11 +3114,11 @@ defm prebuilt_implicit_modules : BoolFOption<"prebuilt-implicit-modules",
31143114
NegFlag<SetFalse>, BothFlags<[], [ClangOption, CC1Option]>>;
31153115

31163116
def fmodule_output_EQ : Joined<["-"], "fmodule-output=">,
3117-
Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
3117+
Flags<[NoXarchOption]>, Visibility<[ClangOption, CLOption, CC1Option]>,
31183118
MarshallingInfoString<FrontendOpts<"ModuleOutputPath">>,
31193119
HelpText<"Save intermediate module file results when compiling a standard C++ module unit.">;
31203120
def fmodule_output : Flag<["-"], "fmodule-output">, Flags<[NoXarchOption]>,
3121-
Visibility<[ClangOption, CC1Option]>,
3121+
Visibility<[ClangOption, CLOption, CC1Option]>,
31223122
HelpText<"Save intermediate module file results when compiling a standard C++ module unit.">;
31233123

31243124
defm skip_odr_check_in_gmf : BoolOption<"f", "skip-odr-check-in-gmf",
@@ -3302,8 +3302,10 @@ def fretain_comments_from_system_headers : Flag<["-"], "fretain-comments-from-sy
33023302
Visibility<[ClangOption, CC1Option]>,
33033303
MarshallingInfoFlag<LangOpts<"RetainCommentsFromSystemHeaders">>;
33043304
def fmodule_header : Flag <["-"], "fmodule-header">, Group<f_Group>,
3305+
Visibility<[ClangOption, CLOption]>,
33053306
HelpText<"Build a C++20 Header Unit from a header">;
33063307
def fmodule_header_EQ : Joined<["-"], "fmodule-header=">, Group<f_Group>,
3308+
Visibility<[ClangOption, CLOption]>,
33073309
MetaVarName<"<kind>">,
33083310
HelpText<"Build a C++20 Header Unit from a header that should be found in the user (fmodule-header=user) or system (fmodule-header=system) search path.">;
33093311

@@ -5955,6 +5957,7 @@ def _output : Separate<["--"], "output">, Alias<o>;
59555957
def _param : Separate<["--"], "param">, Group<CompileOnly_Group>;
59565958
def _param_EQ : Joined<["--"], "param=">, Alias<_param>;
59575959
def _precompile : Flag<["--"], "precompile">, Flags<[NoXarchOption]>,
5960+
Visibility<[ClangOption, CLOption]>,
59585961
Group<Action_Group>, HelpText<"Only precompile the input">;
59595962
def _prefix_EQ : Joined<["--"], "prefix=">, Alias<B>;
59605963
def _prefix : Separate<["--"], "prefix">, Alias<B>;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cl /std:c++20 --precompile -### -- %s 2>&1 | FileCheck --check-prefix=PRECOMPILE %s
2+
// PRECOMPILE: -emit-module-interface
3+
4+
// RUN: %clang_cl /std:c++20 --fmodule-file=Foo=Foo.pcm -### -- %s 2>&1 | FileCheck --check-prefix=FMODULEFILE %s
5+
// FMODULEFILE: -fmodule-file=Foo=Foo.pcm
6+
7+
// RUN: %clang_cl /std:c++20 --fprebuilt-module-path=. -### -- %s 2>&1 | FileCheck --check-prefix=FPREBUILT %s
8+
// FPREBUILT: -fprebuilt-module-path=.

0 commit comments

Comments
 (0)