1*67e74705SXin Li======= 2*67e74705SXin LiModules 3*67e74705SXin Li======= 4*67e74705SXin Li 5*67e74705SXin Li.. contents:: 6*67e74705SXin Li :local: 7*67e74705SXin Li 8*67e74705SXin LiIntroduction 9*67e74705SXin Li============ 10*67e74705SXin LiMost software is built using a number of software libraries, including libraries supplied by the platform, internal libraries built as part of the software itself to provide structure, and third-party libraries. For each library, one needs to access both its interface (API) and its implementation. In the C family of languages, the interface to a library is accessed by including the appropriate header files(s): 11*67e74705SXin Li 12*67e74705SXin Li.. code-block:: c 13*67e74705SXin Li 14*67e74705SXin Li #include <SomeLib.h> 15*67e74705SXin Li 16*67e74705SXin LiThe implementation is handled separately by linking against the appropriate library. For example, by passing ``-lSomeLib`` to the linker. 17*67e74705SXin Li 18*67e74705SXin LiModules provide an alternative, simpler way to use software libraries that provides better compile-time scalability and eliminates many of the problems inherent to using the C preprocessor to access the API of a library. 19*67e74705SXin Li 20*67e74705SXin LiProblems with the current model 21*67e74705SXin Li------------------------------- 22*67e74705SXin LiThe ``#include`` mechanism provided by the C preprocessor is a very poor way to access the API of a library, for a number of reasons: 23*67e74705SXin Li 24*67e74705SXin Li* **Compile-time scalability**: Each time a header is included, the 25*67e74705SXin Li compiler must preprocess and parse the text in that header and every 26*67e74705SXin Li header it includes, transitively. This process must be repeated for 27*67e74705SXin Li every translation unit in the application, which involves a huge 28*67e74705SXin Li amount of redundant work. In a project with *N* translation units 29*67e74705SXin Li and *M* headers included in each translation unit, the compiler is 30*67e74705SXin Li performing *M x N* work even though most of the *M* headers are 31*67e74705SXin Li shared among multiple translation units. C++ is particularly bad, 32*67e74705SXin Li because the compilation model for templates forces a huge amount of 33*67e74705SXin Li code into headers. 34*67e74705SXin Li 35*67e74705SXin Li* **Fragility**: ``#include`` directives are treated as textual 36*67e74705SXin Li inclusion by the preprocessor, and are therefore subject to any 37*67e74705SXin Li active macro definitions at the time of inclusion. If any of the 38*67e74705SXin Li active macro definitions happens to collide with a name in the 39*67e74705SXin Li library, it can break the library API or cause compilation failures 40*67e74705SXin Li in the library header itself. For an extreme example, 41*67e74705SXin Li ``#define std "The C++ Standard"`` and then include a standard 42*67e74705SXin Li library header: the result is a horrific cascade of failures in the 43*67e74705SXin Li C++ Standard Library's implementation. More subtle real-world 44*67e74705SXin Li problems occur when the headers for two different libraries interact 45*67e74705SXin Li due to macro collisions, and users are forced to reorder 46*67e74705SXin Li ``#include`` directives or introduce ``#undef`` directives to break 47*67e74705SXin Li the (unintended) dependency. 48*67e74705SXin Li 49*67e74705SXin Li* **Conventional workarounds**: C programmers have 50*67e74705SXin Li adopted a number of conventions to work around the fragility of the 51*67e74705SXin Li C preprocessor model. Include guards, for example, are required for 52*67e74705SXin Li the vast majority of headers to ensure that multiple inclusion 53*67e74705SXin Li doesn't break the compile. Macro names are written with 54*67e74705SXin Li ``LONG_PREFIXED_UPPERCASE_IDENTIFIERS`` to avoid collisions, and some 55*67e74705SXin Li library/framework developers even use ``__underscored`` names 56*67e74705SXin Li in headers to avoid collisions with "normal" names that (by 57*67e74705SXin Li convention) shouldn't even be macros. These conventions are a 58*67e74705SXin Li barrier to entry for developers coming from non-C languages, are 59*67e74705SXin Li boilerplate for more experienced developers, and make our headers 60*67e74705SXin Li far uglier than they should be. 61*67e74705SXin Li 62*67e74705SXin Li* **Tool confusion**: In a C-based language, it is hard to build tools 63*67e74705SXin Li that work well with software libraries, because the boundaries of 64*67e74705SXin Li the libraries are not clear. Which headers belong to a particular 65*67e74705SXin Li library, and in what order should those headers be included to 66*67e74705SXin Li guarantee that they compile correctly? Are the headers C, C++, 67*67e74705SXin Li Objective-C++, or one of the variants of these languages? What 68*67e74705SXin Li declarations in those headers are actually meant to be part of the 69*67e74705SXin Li API, and what declarations are present only because they had to be 70*67e74705SXin Li written as part of the header file? 71*67e74705SXin Li 72*67e74705SXin LiSemantic import 73*67e74705SXin Li--------------- 74*67e74705SXin LiModules improve access to the API of software libraries by replacing the textual preprocessor inclusion model with a more robust, more efficient semantic model. From the user's perspective, the code looks only slightly different, because one uses an ``import`` declaration rather than a ``#include`` preprocessor directive: 75*67e74705SXin Li 76*67e74705SXin Li.. code-block:: c 77*67e74705SXin Li 78*67e74705SXin Li import std.io; // pseudo-code; see below for syntax discussion 79*67e74705SXin Li 80*67e74705SXin LiHowever, this module import behaves quite differently from the corresponding ``#include <stdio.h>``: when the compiler sees the module import above, it loads a binary representation of the ``std.io`` module and makes its API available to the application directly. Preprocessor definitions that precede the import declaration have no impact on the API provided by ``std.io``, because the module itself was compiled as a separate, standalone module. Additionally, any linker flags required to use the ``std.io`` module will automatically be provided when the module is imported [#]_ 81*67e74705SXin LiThis semantic import model addresses many of the problems of the preprocessor inclusion model: 82*67e74705SXin Li 83*67e74705SXin Li* **Compile-time scalability**: The ``std.io`` module is only compiled once, and importing the module into a translation unit is a constant-time operation (independent of module system). Thus, the API of each software library is only parsed once, reducing the *M x N* compilation problem to an *M + N* problem. 84*67e74705SXin Li 85*67e74705SXin Li* **Fragility**: Each module is parsed as a standalone entity, so it has a consistent preprocessor environment. This completely eliminates the need for ``__underscored`` names and similarly defensive tricks. Moreover, the current preprocessor definitions when an import declaration is encountered are ignored, so one software library can not affect how another software library is compiled, eliminating include-order dependencies. 86*67e74705SXin Li 87*67e74705SXin Li* **Tool confusion**: Modules describe the API of software libraries, and tools can reason about and present a module as a representation of that API. Because modules can only be built standalone, tools can rely on the module definition to ensure that they get the complete API for the library. Moreover, modules can specify which languages they work with, so, e.g., one can not accidentally attempt to load a C++ module into a C program. 88*67e74705SXin Li 89*67e74705SXin LiProblems modules do not solve 90*67e74705SXin Li----------------------------- 91*67e74705SXin LiMany programming languages have a module or package system, and because of the variety of features provided by these languages it is important to define what modules do *not* do. In particular, all of the following are considered out-of-scope for modules: 92*67e74705SXin Li 93*67e74705SXin Li* **Rewrite the world's code**: It is not realistic to require applications or software libraries to make drastic or non-backward-compatible changes, nor is it feasible to completely eliminate headers. Modules must interoperate with existing software libraries and allow a gradual transition. 94*67e74705SXin Li 95*67e74705SXin Li* **Versioning**: Modules have no notion of version information. Programmers must still rely on the existing versioning mechanisms of the underlying language (if any exist) to version software libraries. 96*67e74705SXin Li 97*67e74705SXin Li* **Namespaces**: Unlike in some languages, modules do not imply any notion of namespaces. Thus, a struct declared in one module will still conflict with a struct of the same name declared in a different module, just as they would if declared in two different headers. This aspect is important for backward compatibility, because (for example) the mangled names of entities in software libraries must not change when introducing modules. 98*67e74705SXin Li 99*67e74705SXin Li* **Binary distribution of modules**: Headers (particularly C++ headers) expose the full complexity of the language. Maintaining a stable binary module format across architectures, compiler versions, and compiler vendors is technically infeasible. 100*67e74705SXin Li 101*67e74705SXin LiUsing Modules 102*67e74705SXin Li============= 103*67e74705SXin LiTo enable modules, pass the command-line flag ``-fmodules``. This will make any modules-enabled software libraries available as modules as well as introducing any modules-specific syntax. Additional `command-line parameters`_ are described in a separate section later. 104*67e74705SXin Li 105*67e74705SXin LiObjective-C Import declaration 106*67e74705SXin Li------------------------------ 107*67e74705SXin LiObjective-C provides syntax for importing a module via an *@import declaration*, which imports the named module: 108*67e74705SXin Li 109*67e74705SXin Li.. parsed-literal:: 110*67e74705SXin Li 111*67e74705SXin Li @import std; 112*67e74705SXin Li 113*67e74705SXin LiThe ``@import`` declaration above imports the entire contents of the ``std`` module (which would contain, e.g., the entire C or C++ standard library) and make its API available within the current translation unit. To import only part of a module, one may use dot syntax to specific a particular submodule, e.g., 114*67e74705SXin Li 115*67e74705SXin Li.. parsed-literal:: 116*67e74705SXin Li 117*67e74705SXin Li @import std.io; 118*67e74705SXin Li 119*67e74705SXin LiRedundant import declarations are ignored, and one is free to import modules at any point within the translation unit, so long as the import declaration is at global scope. 120*67e74705SXin Li 121*67e74705SXin LiAt present, there is no C or C++ syntax for import declarations. Clang 122*67e74705SXin Liwill track the modules proposal in the C++ committee. See the section 123*67e74705SXin Li`Includes as imports`_ to see how modules get imported today. 124*67e74705SXin Li 125*67e74705SXin LiIncludes as imports 126*67e74705SXin Li------------------- 127*67e74705SXin LiThe primary user-level feature of modules is the import operation, which provides access to the API of software libraries. However, today's programs make extensive use of ``#include``, and it is unrealistic to assume that all of this code will change overnight. Instead, modules automatically translate ``#include`` directives into the corresponding module import. For example, the include directive 128*67e74705SXin Li 129*67e74705SXin Li.. code-block:: c 130*67e74705SXin Li 131*67e74705SXin Li #include <stdio.h> 132*67e74705SXin Li 133*67e74705SXin Liwill be automatically mapped to an import of the module ``std.io``. Even with specific ``import`` syntax in the language, this particular feature is important for both adoption and backward compatibility: automatic translation of ``#include`` to ``import`` allows an application to get the benefits of modules (for all modules-enabled libraries) without any changes to the application itself. Thus, users can easily use modules with one compiler while falling back to the preprocessor-inclusion mechanism with other compilers. 134*67e74705SXin Li 135*67e74705SXin Li.. note:: 136*67e74705SXin Li 137*67e74705SXin Li The automatic mapping of ``#include`` to ``import`` also solves an implementation problem: importing a module with a definition of some entity (say, a ``struct Point``) and then parsing a header containing another definition of ``struct Point`` would cause a redefinition error, even if it is the same ``struct Point``. By mapping ``#include`` to ``import``, the compiler can guarantee that it always sees just the already-parsed definition from the module. 138*67e74705SXin Li 139*67e74705SXin LiWhile building a module, ``#include_next`` is also supported, with one caveat. 140*67e74705SXin LiThe usual behavior of ``#include_next`` is to search for the specified filename 141*67e74705SXin Liin the list of include paths, starting from the path *after* the one 142*67e74705SXin Liin which the current file was found. 143*67e74705SXin LiBecause files listed in module maps are not found through include paths, a 144*67e74705SXin Lidifferent strategy is used for ``#include_next`` directives in such files: the 145*67e74705SXin Lilist of include paths is searched for the specified header name, to find the 146*67e74705SXin Lifirst include path that would refer to the current file. ``#include_next`` is 147*67e74705SXin Liinterpreted as if the current file had been found in that path. 148*67e74705SXin LiIf this search finds a file named by a module map, the ``#include_next`` 149*67e74705SXin Lidirective is translated into an import, just like for a ``#include`` 150*67e74705SXin Lidirective.`` 151*67e74705SXin Li 152*67e74705SXin LiModule maps 153*67e74705SXin Li----------- 154*67e74705SXin LiThe crucial link between modules and headers is described by a *module map*, which describes how a collection of existing headers maps on to the (logical) structure of a module. For example, one could imagine a module ``std`` covering the C standard library. Each of the C standard library headers (``<stdio.h>``, ``<stdlib.h>``, ``<math.h>``, etc.) would contribute to the ``std`` module, by placing their respective APIs into the corresponding submodule (``std.io``, ``std.lib``, ``std.math``, etc.). Having a list of the headers that are part of the ``std`` module allows the compiler to build the ``std`` module as a standalone entity, and having the mapping from header names to (sub)modules allows the automatic translation of ``#include`` directives to module imports. 155*67e74705SXin Li 156*67e74705SXin LiModule maps are specified as separate files (each named ``module.modulemap``) alongside the headers they describe, which allows them to be added to existing software libraries without having to change the library headers themselves (in most cases [#]_). The actual `Module map language`_ is described in a later section. 157*67e74705SXin Li 158*67e74705SXin Li.. note:: 159*67e74705SXin Li 160*67e74705SXin Li To actually see any benefits from modules, one first has to introduce module maps for the underlying C standard library and the libraries and headers on which it depends. The section `Modularizing a Platform`_ describes the steps one must take to write these module maps. 161*67e74705SXin Li 162*67e74705SXin LiOne can use module maps without modules to check the integrity of the use of header files. To do this, use the ``-fimplicit-module-maps`` option instead of the ``-fmodules`` option, or use ``-fmodule-map-file=`` option to explicitly specify the module map files to load. 163*67e74705SXin Li 164*67e74705SXin LiCompilation model 165*67e74705SXin Li----------------- 166*67e74705SXin LiThe binary representation of modules is automatically generated by the compiler on an as-needed basis. When a module is imported (e.g., by an ``#include`` of one of the module's headers), the compiler will spawn a second instance of itself [#]_, with a fresh preprocessing context [#]_, to parse just the headers in that module. The resulting Abstract Syntax Tree (AST) is then persisted into the binary representation of the module that is then loaded into translation unit where the module import was encountered. 167*67e74705SXin Li 168*67e74705SXin LiThe binary representation of modules is persisted in the *module cache*. Imports of a module will first query the module cache and, if a binary representation of the required module is already available, will load that representation directly. Thus, a module's headers will only be parsed once per language configuration, rather than once per translation unit that uses the module. 169*67e74705SXin Li 170*67e74705SXin LiModules maintain references to each of the headers that were part of the module build. If any of those headers changes, or if any of the modules on which a module depends change, then the module will be (automatically) recompiled. The process should never require any user intervention. 171*67e74705SXin Li 172*67e74705SXin LiCommand-line parameters 173*67e74705SXin Li----------------------- 174*67e74705SXin Li``-fmodules`` 175*67e74705SXin Li Enable the modules feature. 176*67e74705SXin Li 177*67e74705SXin Li``-fimplicit-module-maps`` 178*67e74705SXin Li Enable implicit search for module map files named ``module.modulemap`` and similar. This option is implied by ``-fmodules``. If this is disabled with ``-fno-implicit-module-maps``, module map files will only be loaded if they are explicitly specified via ``-fmodule-map-file`` or transitively used by another module map file. 179*67e74705SXin Li 180*67e74705SXin Li``-fmodules-cache-path=<directory>`` 181*67e74705SXin Li Specify the path to the modules cache. If not provided, Clang will select a system-appropriate default. 182*67e74705SXin Li 183*67e74705SXin Li``-fno-autolink`` 184*67e74705SXin Li Disable automatic linking against the libraries associated with imported modules. 185*67e74705SXin Li 186*67e74705SXin Li``-fmodules-ignore-macro=macroname`` 187*67e74705SXin Li Instruct modules to ignore the named macro when selecting an appropriate module variant. Use this for macros defined on the command line that don't affect how modules are built, to improve sharing of compiled module files. 188*67e74705SXin Li 189*67e74705SXin Li``-fmodules-prune-interval=seconds`` 190*67e74705SXin Li Specify the minimum delay (in seconds) between attempts to prune the module cache. Module cache pruning attempts to clear out old, unused module files so that the module cache itself does not grow without bound. The default delay is large (604,800 seconds, or 7 days) because this is an expensive operation. Set this value to 0 to turn off pruning. 191*67e74705SXin Li 192*67e74705SXin Li``-fmodules-prune-after=seconds`` 193*67e74705SXin Li Specify the minimum time (in seconds) for which a file in the module cache must be unused (according to access time) before module pruning will remove it. The default delay is large (2,678,400 seconds, or 31 days) to avoid excessive module rebuilding. 194*67e74705SXin Li 195*67e74705SXin Li``-module-file-info <module file name>`` 196*67e74705SXin Li Debugging aid that prints information about a given module file (with a ``.pcm`` extension), including the language and preprocessor options that particular module variant was built with. 197*67e74705SXin Li 198*67e74705SXin Li``-fmodules-decluse`` 199*67e74705SXin Li Enable checking of module ``use`` declarations. 200*67e74705SXin Li 201*67e74705SXin Li``-fmodule-name=module-id`` 202*67e74705SXin Li Consider a source file as a part of the given module. 203*67e74705SXin Li 204*67e74705SXin Li``-fmodule-map-file=<file>`` 205*67e74705SXin Li Load the given module map file if a header from its directory or one of its subdirectories is loaded. 206*67e74705SXin Li 207*67e74705SXin Li``-fmodules-search-all`` 208*67e74705SXin Li If a symbol is not found, search modules referenced in the current module maps but not imported for symbols, so the error message can reference the module by name. Note that if the global module index has not been built before, this might take some time as it needs to build all the modules. Note that this option doesn't apply in module builds, to avoid the recursion. 209*67e74705SXin Li 210*67e74705SXin Li``-fno-implicit-modules`` 211*67e74705SXin Li All modules used by the build must be specified with ``-fmodule-file``. 212*67e74705SXin Li 213*67e74705SXin Li``-fmodule-file=<file>`` 214*67e74705SXin Li Load the given precompiled module file. 215*67e74705SXin Li 216*67e74705SXin LiModule Semantics 217*67e74705SXin Li================ 218*67e74705SXin Li 219*67e74705SXin LiModules are modeled as if each submodule were a separate translation unit, and a module import makes names from the other translation unit visible. Each submodule starts with a new preprocessor state and an empty translation unit. 220*67e74705SXin Li 221*67e74705SXin Li.. note:: 222*67e74705SXin Li 223*67e74705SXin Li This behavior is currently only approximated when building a module with submodules. Entities within a submodule that has already been built are visible when building later submodules in that module. This can lead to fragile modules that depend on the build order used for the submodules of the module, and should not be relied upon. This behavior is subject to change. 224*67e74705SXin Li 225*67e74705SXin LiAs an example, in C, this implies that if two structs are defined in different submodules with the same name, those two types are distinct types (but may be *compatible* types if their definitions match). In C++, two structs defined with the same name in different submodules are the *same* type, and must be equivalent under C++'s One Definition Rule. 226*67e74705SXin Li 227*67e74705SXin Li.. note:: 228*67e74705SXin Li 229*67e74705SXin Li Clang currently only performs minimal checking for violations of the One Definition Rule. 230*67e74705SXin Li 231*67e74705SXin LiIf any submodule of a module is imported into any part of a program, the entire top-level module is considered to be part of the program. As a consequence of this, Clang may diagnose conflicts between an entity declared in an unimported submodule and an entity declared in the current translation unit, and Clang may inline or devirtualize based on knowledge from unimported submodules. 232*67e74705SXin Li 233*67e74705SXin LiMacros 234*67e74705SXin Li------ 235*67e74705SXin Li 236*67e74705SXin LiThe C and C++ preprocessor assumes that the input text is a single linear buffer, but with modules this is not the case. It is possible to import two modules that have conflicting definitions for a macro (or where one ``#define``\s a macro and the other ``#undef``\ines it). The rules for handling macro definitions in the presence of modules are as follows: 237*67e74705SXin Li 238*67e74705SXin Li* Each definition and undefinition of a macro is considered to be a distinct entity. 239*67e74705SXin Li* Such entities are *visible* if they are from the current submodule or translation unit, or if they were exported from a submodule that has been imported. 240*67e74705SXin Li* A ``#define X`` or ``#undef X`` directive *overrides* all definitions of ``X`` that are visible at the point of the directive. 241*67e74705SXin Li* A ``#define`` or ``#undef`` directive is *active* if it is visible and no visible directive overrides it. 242*67e74705SXin Li* A set of macro directives is *consistent* if it consists of only ``#undef`` directives, or if all ``#define`` directives in the set define the macro name to the same sequence of tokens (following the usual rules for macro redefinitions). 243*67e74705SXin Li* If a macro name is used and the set of active directives is not consistent, the program is ill-formed. Otherwise, the (unique) meaning of the macro name is used. 244*67e74705SXin Li 245*67e74705SXin LiFor example, suppose: 246*67e74705SXin Li 247*67e74705SXin Li* ``<stdio.h>`` defines a macro ``getc`` (and exports its ``#define``) 248*67e74705SXin Li* ``<cstdio>`` imports the ``<stdio.h>`` module and undefines the macro (and exports its ``#undef``) 249*67e74705SXin Li 250*67e74705SXin LiThe ``#undef`` overrides the ``#define``, and a source file that imports both modules *in any order* will not see ``getc`` defined as a macro. 251*67e74705SXin Li 252*67e74705SXin LiModule Map Language 253*67e74705SXin Li=================== 254*67e74705SXin Li 255*67e74705SXin Li.. warning:: 256*67e74705SXin Li 257*67e74705SXin Li The module map language is not currently guaranteed to be stable between major revisions of Clang. 258*67e74705SXin Li 259*67e74705SXin LiThe module map language describes the mapping from header files to the 260*67e74705SXin Lilogical structure of modules. To enable support for using a library as 261*67e74705SXin Lia module, one must write a ``module.modulemap`` file for that library. The 262*67e74705SXin Li``module.modulemap`` file is placed alongside the header files themselves, 263*67e74705SXin Liand is written in the module map language described below. 264*67e74705SXin Li 265*67e74705SXin Li.. note:: 266*67e74705SXin Li For compatibility with previous releases, if a module map file named 267*67e74705SXin Li ``module.modulemap`` is not found, Clang will also search for a file named 268*67e74705SXin Li ``module.map``. This behavior is deprecated and we plan to eventually 269*67e74705SXin Li remove it. 270*67e74705SXin Li 271*67e74705SXin LiAs an example, the module map file for the C standard library might look a bit like this: 272*67e74705SXin Li 273*67e74705SXin Li.. parsed-literal:: 274*67e74705SXin Li 275*67e74705SXin Li module std [system] [extern_c] { 276*67e74705SXin Li module assert { 277*67e74705SXin Li textual header "assert.h" 278*67e74705SXin Li header "bits/assert-decls.h" 279*67e74705SXin Li export * 280*67e74705SXin Li } 281*67e74705SXin Li 282*67e74705SXin Li module complex { 283*67e74705SXin Li header "complex.h" 284*67e74705SXin Li export * 285*67e74705SXin Li } 286*67e74705SXin Li 287*67e74705SXin Li module ctype { 288*67e74705SXin Li header "ctype.h" 289*67e74705SXin Li export * 290*67e74705SXin Li } 291*67e74705SXin Li 292*67e74705SXin Li module errno { 293*67e74705SXin Li header "errno.h" 294*67e74705SXin Li header "sys/errno.h" 295*67e74705SXin Li export * 296*67e74705SXin Li } 297*67e74705SXin Li 298*67e74705SXin Li module fenv { 299*67e74705SXin Li header "fenv.h" 300*67e74705SXin Li export * 301*67e74705SXin Li } 302*67e74705SXin Li 303*67e74705SXin Li // ...more headers follow... 304*67e74705SXin Li } 305*67e74705SXin Li 306*67e74705SXin LiHere, the top-level module ``std`` encompasses the whole C standard library. It has a number of submodules containing different parts of the standard library: ``complex`` for complex numbers, ``ctype`` for character types, etc. Each submodule lists one of more headers that provide the contents for that submodule. Finally, the ``export *`` command specifies that anything included by that submodule will be automatically re-exported. 307*67e74705SXin Li 308*67e74705SXin LiLexical structure 309*67e74705SXin Li----------------- 310*67e74705SXin LiModule map files use a simplified form of the C99 lexer, with the same rules for identifiers, tokens, string literals, ``/* */`` and ``//`` comments. The module map language has the following reserved words; all other C identifiers are valid identifiers. 311*67e74705SXin Li 312*67e74705SXin Li.. parsed-literal:: 313*67e74705SXin Li 314*67e74705SXin Li ``config_macros`` ``export`` ``private`` 315*67e74705SXin Li ``conflict`` ``framework`` ``requires`` 316*67e74705SXin Li ``exclude`` ``header`` ``textual`` 317*67e74705SXin Li ``explicit`` ``link`` ``umbrella`` 318*67e74705SXin Li ``extern`` ``module`` ``use`` 319*67e74705SXin Li 320*67e74705SXin LiModule map file 321*67e74705SXin Li--------------- 322*67e74705SXin LiA module map file consists of a series of module declarations: 323*67e74705SXin Li 324*67e74705SXin Li.. parsed-literal:: 325*67e74705SXin Li 326*67e74705SXin Li *module-map-file*: 327*67e74705SXin Li *module-declaration** 328*67e74705SXin Li 329*67e74705SXin LiWithin a module map file, modules are referred to by a *module-id*, which uses periods to separate each part of a module's name: 330*67e74705SXin Li 331*67e74705SXin Li.. parsed-literal:: 332*67e74705SXin Li 333*67e74705SXin Li *module-id*: 334*67e74705SXin Li *identifier* ('.' *identifier*)* 335*67e74705SXin Li 336*67e74705SXin LiModule declaration 337*67e74705SXin Li------------------ 338*67e74705SXin LiA module declaration describes a module, including the headers that contribute to that module, its submodules, and other aspects of the module. 339*67e74705SXin Li 340*67e74705SXin Li.. parsed-literal:: 341*67e74705SXin Li 342*67e74705SXin Li *module-declaration*: 343*67e74705SXin Li ``explicit``:sub:`opt` ``framework``:sub:`opt` ``module`` *module-id* *attributes*:sub:`opt` '{' *module-member** '}' 344*67e74705SXin Li ``extern`` ``module`` *module-id* *string-literal* 345*67e74705SXin Li 346*67e74705SXin LiThe *module-id* should consist of only a single *identifier*, which provides the name of the module being defined. Each module shall have a single definition. 347*67e74705SXin Li 348*67e74705SXin LiThe ``explicit`` qualifier can only be applied to a submodule, i.e., a module that is nested within another module. The contents of explicit submodules are only made available when the submodule itself was explicitly named in an import declaration or was re-exported from an imported module. 349*67e74705SXin Li 350*67e74705SXin LiThe ``framework`` qualifier specifies that this module corresponds to a Darwin-style framework. A Darwin-style framework (used primarily on Mac OS X and iOS) is contained entirely in directory ``Name.framework``, where ``Name`` is the name of the framework (and, therefore, the name of the module). That directory has the following layout: 351*67e74705SXin Li 352*67e74705SXin Li.. parsed-literal:: 353*67e74705SXin Li 354*67e74705SXin Li Name.framework/ 355*67e74705SXin Li Modules/module.modulemap Module map for the framework 356*67e74705SXin Li Headers/ Subdirectory containing framework headers 357*67e74705SXin Li Frameworks/ Subdirectory containing embedded frameworks 358*67e74705SXin Li Resources/ Subdirectory containing additional resources 359*67e74705SXin Li Name Symbolic link to the shared library for the framework 360*67e74705SXin Li 361*67e74705SXin LiThe ``system`` attribute specifies that the module is a system module. When a system module is rebuilt, all of the module's headers will be considered system headers, which suppresses warnings. This is equivalent to placing ``#pragma GCC system_header`` in each of the module's headers. The form of attributes is described in the section Attributes_, below. 362*67e74705SXin Li 363*67e74705SXin LiThe ``extern_c`` attribute specifies that the module contains C code that can be used from within C++. When such a module is built for use in C++ code, all of the module's headers will be treated as if they were contained within an implicit ``extern "C"`` block. An import for a module with this attribute can appear within an ``extern "C"`` block. No other restrictions are lifted, however: the module currently cannot be imported within an ``extern "C"`` block in a namespace. 364*67e74705SXin Li 365*67e74705SXin LiModules can have a number of different kinds of members, each of which is described below: 366*67e74705SXin Li 367*67e74705SXin Li.. parsed-literal:: 368*67e74705SXin Li 369*67e74705SXin Li *module-member*: 370*67e74705SXin Li *requires-declaration* 371*67e74705SXin Li *header-declaration* 372*67e74705SXin Li *umbrella-dir-declaration* 373*67e74705SXin Li *submodule-declaration* 374*67e74705SXin Li *export-declaration* 375*67e74705SXin Li *use-declaration* 376*67e74705SXin Li *link-declaration* 377*67e74705SXin Li *config-macros-declaration* 378*67e74705SXin Li *conflict-declaration* 379*67e74705SXin Li 380*67e74705SXin LiAn extern module references a module defined by the *module-id* in a file given by the *string-literal*. The file can be referenced either by an absolute path or by a path relative to the current map file. 381*67e74705SXin Li 382*67e74705SXin LiRequires declaration 383*67e74705SXin Li~~~~~~~~~~~~~~~~~~~~ 384*67e74705SXin LiA *requires-declaration* specifies the requirements that an importing translation unit must satisfy to use the module. 385*67e74705SXin Li 386*67e74705SXin Li.. parsed-literal:: 387*67e74705SXin Li 388*67e74705SXin Li *requires-declaration*: 389*67e74705SXin Li ``requires`` *feature-list* 390*67e74705SXin Li 391*67e74705SXin Li *feature-list*: 392*67e74705SXin Li *feature* (',' *feature*)* 393*67e74705SXin Li 394*67e74705SXin Li *feature*: 395*67e74705SXin Li ``!``:sub:`opt` *identifier* 396*67e74705SXin Li 397*67e74705SXin LiThe requirements clause allows specific modules or submodules to specify that they are only accessible with certain language dialects or on certain platforms. The feature list is a set of identifiers, defined below. If any of the features is not available in a given translation unit, that translation unit shall not import the module. The optional ``!`` indicates that a feature is incompatible with the module. 398*67e74705SXin Li 399*67e74705SXin LiThe following features are defined: 400*67e74705SXin Li 401*67e74705SXin Lialtivec 402*67e74705SXin Li The target supports AltiVec. 403*67e74705SXin Li 404*67e74705SXin Liblocks 405*67e74705SXin Li The "blocks" language feature is available. 406*67e74705SXin Li 407*67e74705SXin Licplusplus 408*67e74705SXin Li C++ support is available. 409*67e74705SXin Li 410*67e74705SXin Licplusplus11 411*67e74705SXin Li C++11 support is available. 412*67e74705SXin Li 413*67e74705SXin Liobjc 414*67e74705SXin Li Objective-C support is available. 415*67e74705SXin Li 416*67e74705SXin Liobjc_arc 417*67e74705SXin Li Objective-C Automatic Reference Counting (ARC) is available 418*67e74705SXin Li 419*67e74705SXin Liopencl 420*67e74705SXin Li OpenCL is available 421*67e74705SXin Li 422*67e74705SXin Litls 423*67e74705SXin Li Thread local storage is available. 424*67e74705SXin Li 425*67e74705SXin Li*target feature* 426*67e74705SXin Li A specific target feature (e.g., ``sse4``, ``avx``, ``neon``) is available. 427*67e74705SXin Li 428*67e74705SXin Li 429*67e74705SXin Li**Example:** The ``std`` module can be extended to also include C++ and C++11 headers using a *requires-declaration*: 430*67e74705SXin Li 431*67e74705SXin Li.. parsed-literal:: 432*67e74705SXin Li 433*67e74705SXin Li module std { 434*67e74705SXin Li // C standard library... 435*67e74705SXin Li 436*67e74705SXin Li module vector { 437*67e74705SXin Li requires cplusplus 438*67e74705SXin Li header "vector" 439*67e74705SXin Li } 440*67e74705SXin Li 441*67e74705SXin Li module type_traits { 442*67e74705SXin Li requires cplusplus11 443*67e74705SXin Li header "type_traits" 444*67e74705SXin Li } 445*67e74705SXin Li } 446*67e74705SXin Li 447*67e74705SXin LiHeader declaration 448*67e74705SXin Li~~~~~~~~~~~~~~~~~~ 449*67e74705SXin LiA header declaration specifies that a particular header is associated with the enclosing module. 450*67e74705SXin Li 451*67e74705SXin Li.. parsed-literal:: 452*67e74705SXin Li 453*67e74705SXin Li *header-declaration*: 454*67e74705SXin Li ``private``:sub:`opt` ``textual``:sub:`opt` ``header`` *string-literal* 455*67e74705SXin Li ``umbrella`` ``header`` *string-literal* 456*67e74705SXin Li ``exclude`` ``header`` *string-literal* 457*67e74705SXin Li 458*67e74705SXin LiA header declaration that does not contain ``exclude`` nor ``textual`` specifies a header that contributes to the enclosing module. Specifically, when the module is built, the named header will be parsed and its declarations will be (logically) placed into the enclosing submodule. 459*67e74705SXin Li 460*67e74705SXin LiA header with the ``umbrella`` specifier is called an umbrella header. An umbrella header includes all of the headers within its directory (and any subdirectories), and is typically used (in the ``#include`` world) to easily access the full API provided by a particular library. With modules, an umbrella header is a convenient shortcut that eliminates the need to write out ``header`` declarations for every library header. A given directory can only contain a single umbrella header. 461*67e74705SXin Li 462*67e74705SXin Li.. note:: 463*67e74705SXin Li Any headers not included by the umbrella header should have 464*67e74705SXin Li explicit ``header`` declarations. Use the 465*67e74705SXin Li ``-Wincomplete-umbrella`` warning option to ask Clang to complain 466*67e74705SXin Li about headers not covered by the umbrella header or the module map. 467*67e74705SXin Li 468*67e74705SXin LiA header with the ``private`` specifier may not be included from outside the module itself. 469*67e74705SXin Li 470*67e74705SXin LiA header with the ``textual`` specifier will not be compiled when the module is 471*67e74705SXin Libuilt, and will be textually included if it is named by a ``#include`` 472*67e74705SXin Lidirective. However, it is considered to be part of the module for the purpose 473*67e74705SXin Liof checking *use-declaration*\s, and must still be a lexically-valid header 474*67e74705SXin Lifile. In the future, we intend to pre-tokenize such headers and include the 475*67e74705SXin Litoken sequence within the prebuilt module representation. 476*67e74705SXin Li 477*67e74705SXin LiA header with the ``exclude`` specifier is excluded from the module. It will not be included when the module is built, nor will it be considered to be part of the module, even if an ``umbrella`` header or directory would otherwise make it part of the module. 478*67e74705SXin Li 479*67e74705SXin Li**Example:** The C header ``assert.h`` is an excellent candidate for a textual header, because it is meant to be included multiple times (possibly with different ``NDEBUG`` settings). However, declarations within it should typically be split into a separate modular header. 480*67e74705SXin Li 481*67e74705SXin Li.. parsed-literal:: 482*67e74705SXin Li 483*67e74705SXin Li module std [system] { 484*67e74705SXin Li textual header "assert.h" 485*67e74705SXin Li } 486*67e74705SXin Li 487*67e74705SXin LiA given header shall not be referenced by more than one *header-declaration*. 488*67e74705SXin Li 489*67e74705SXin LiUmbrella directory declaration 490*67e74705SXin Li~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 491*67e74705SXin LiAn umbrella directory declaration specifies that all of the headers in the specified directory should be included within the module. 492*67e74705SXin Li 493*67e74705SXin Li.. parsed-literal:: 494*67e74705SXin Li 495*67e74705SXin Li *umbrella-dir-declaration*: 496*67e74705SXin Li ``umbrella`` *string-literal* 497*67e74705SXin Li 498*67e74705SXin LiThe *string-literal* refers to a directory. When the module is built, all of the header files in that directory (and its subdirectories) are included in the module. 499*67e74705SXin Li 500*67e74705SXin LiAn *umbrella-dir-declaration* shall not refer to the same directory as the location of an umbrella *header-declaration*. In other words, only a single kind of umbrella can be specified for a given directory. 501*67e74705SXin Li 502*67e74705SXin Li.. note:: 503*67e74705SXin Li 504*67e74705SXin Li Umbrella directories are useful for libraries that have a large number of headers but do not have an umbrella header. 505*67e74705SXin Li 506*67e74705SXin Li 507*67e74705SXin LiSubmodule declaration 508*67e74705SXin Li~~~~~~~~~~~~~~~~~~~~~ 509*67e74705SXin LiSubmodule declarations describe modules that are nested within their enclosing module. 510*67e74705SXin Li 511*67e74705SXin Li.. parsed-literal:: 512*67e74705SXin Li 513*67e74705SXin Li *submodule-declaration*: 514*67e74705SXin Li *module-declaration* 515*67e74705SXin Li *inferred-submodule-declaration* 516*67e74705SXin Li 517*67e74705SXin LiA *submodule-declaration* that is a *module-declaration* is a nested module. If the *module-declaration* has a ``framework`` specifier, the enclosing module shall have a ``framework`` specifier; the submodule's contents shall be contained within the subdirectory ``Frameworks/SubName.framework``, where ``SubName`` is the name of the submodule. 518*67e74705SXin Li 519*67e74705SXin LiA *submodule-declaration* that is an *inferred-submodule-declaration* describes a set of submodules that correspond to any headers that are part of the module but are not explicitly described by a *header-declaration*. 520*67e74705SXin Li 521*67e74705SXin Li.. parsed-literal:: 522*67e74705SXin Li 523*67e74705SXin Li *inferred-submodule-declaration*: 524*67e74705SXin Li ``explicit``:sub:`opt` ``framework``:sub:`opt` ``module`` '*' *attributes*:sub:`opt` '{' *inferred-submodule-member** '}' 525*67e74705SXin Li 526*67e74705SXin Li *inferred-submodule-member*: 527*67e74705SXin Li ``export`` '*' 528*67e74705SXin Li 529*67e74705SXin LiA module containing an *inferred-submodule-declaration* shall have either an umbrella header or an umbrella directory. The headers to which the *inferred-submodule-declaration* applies are exactly those headers included by the umbrella header (transitively) or included in the module because they reside within the umbrella directory (or its subdirectories). 530*67e74705SXin Li 531*67e74705SXin LiFor each header included by the umbrella header or in the umbrella directory that is not named by a *header-declaration*, a module declaration is implicitly generated from the *inferred-submodule-declaration*. The module will: 532*67e74705SXin Li 533*67e74705SXin Li* Have the same name as the header (without the file extension) 534*67e74705SXin Li* Have the ``explicit`` specifier, if the *inferred-submodule-declaration* has the ``explicit`` specifier 535*67e74705SXin Li* Have the ``framework`` specifier, if the 536*67e74705SXin Li *inferred-submodule-declaration* has the ``framework`` specifier 537*67e74705SXin Li* Have the attributes specified by the \ *inferred-submodule-declaration* 538*67e74705SXin Li* Contain a single *header-declaration* naming that header 539*67e74705SXin Li* Contain a single *export-declaration* ``export *``, if the \ *inferred-submodule-declaration* contains the \ *inferred-submodule-member* ``export *`` 540*67e74705SXin Li 541*67e74705SXin Li**Example:** If the subdirectory "MyLib" contains the headers ``A.h`` and ``B.h``, then the following module map: 542*67e74705SXin Li 543*67e74705SXin Li.. parsed-literal:: 544*67e74705SXin Li 545*67e74705SXin Li module MyLib { 546*67e74705SXin Li umbrella "MyLib" 547*67e74705SXin Li explicit module * { 548*67e74705SXin Li export * 549*67e74705SXin Li } 550*67e74705SXin Li } 551*67e74705SXin Li 552*67e74705SXin Liis equivalent to the (more verbose) module map: 553*67e74705SXin Li 554*67e74705SXin Li.. parsed-literal:: 555*67e74705SXin Li 556*67e74705SXin Li module MyLib { 557*67e74705SXin Li explicit module A { 558*67e74705SXin Li header "A.h" 559*67e74705SXin Li export * 560*67e74705SXin Li } 561*67e74705SXin Li 562*67e74705SXin Li explicit module B { 563*67e74705SXin Li header "B.h" 564*67e74705SXin Li export * 565*67e74705SXin Li } 566*67e74705SXin Li } 567*67e74705SXin Li 568*67e74705SXin LiExport declaration 569*67e74705SXin Li~~~~~~~~~~~~~~~~~~ 570*67e74705SXin LiAn *export-declaration* specifies which imported modules will automatically be re-exported as part of a given module's API. 571*67e74705SXin Li 572*67e74705SXin Li.. parsed-literal:: 573*67e74705SXin Li 574*67e74705SXin Li *export-declaration*: 575*67e74705SXin Li ``export`` *wildcard-module-id* 576*67e74705SXin Li 577*67e74705SXin Li *wildcard-module-id*: 578*67e74705SXin Li *identifier* 579*67e74705SXin Li '*' 580*67e74705SXin Li *identifier* '.' *wildcard-module-id* 581*67e74705SXin Li 582*67e74705SXin LiThe *export-declaration* names a module or a set of modules that will be re-exported to any translation unit that imports the enclosing module. Each imported module that matches the *wildcard-module-id* up to, but not including, the first ``*`` will be re-exported. 583*67e74705SXin Li 584*67e74705SXin Li**Example:** In the following example, importing ``MyLib.Derived`` also provides the API for ``MyLib.Base``: 585*67e74705SXin Li 586*67e74705SXin Li.. parsed-literal:: 587*67e74705SXin Li 588*67e74705SXin Li module MyLib { 589*67e74705SXin Li module Base { 590*67e74705SXin Li header "Base.h" 591*67e74705SXin Li } 592*67e74705SXin Li 593*67e74705SXin Li module Derived { 594*67e74705SXin Li header "Derived.h" 595*67e74705SXin Li export Base 596*67e74705SXin Li } 597*67e74705SXin Li } 598*67e74705SXin Li 599*67e74705SXin LiNote that, if ``Derived.h`` includes ``Base.h``, one can simply use a wildcard export to re-export everything ``Derived.h`` includes: 600*67e74705SXin Li 601*67e74705SXin Li.. parsed-literal:: 602*67e74705SXin Li 603*67e74705SXin Li module MyLib { 604*67e74705SXin Li module Base { 605*67e74705SXin Li header "Base.h" 606*67e74705SXin Li } 607*67e74705SXin Li 608*67e74705SXin Li module Derived { 609*67e74705SXin Li header "Derived.h" 610*67e74705SXin Li export * 611*67e74705SXin Li } 612*67e74705SXin Li } 613*67e74705SXin Li 614*67e74705SXin Li.. note:: 615*67e74705SXin Li 616*67e74705SXin Li The wildcard export syntax ``export *`` re-exports all of the 617*67e74705SXin Li modules that were imported in the actual header file. Because 618*67e74705SXin Li ``#include`` directives are automatically mapped to module imports, 619*67e74705SXin Li ``export *`` provides the same transitive-inclusion behavior 620*67e74705SXin Li provided by the C preprocessor, e.g., importing a given module 621*67e74705SXin Li implicitly imports all of the modules on which it depends. 622*67e74705SXin Li Therefore, liberal use of ``export *`` provides excellent backward 623*67e74705SXin Li compatibility for programs that rely on transitive inclusion (i.e., 624*67e74705SXin Li all of them). 625*67e74705SXin Li 626*67e74705SXin LiUse declaration 627*67e74705SXin Li~~~~~~~~~~~~~~~ 628*67e74705SXin LiA *use-declaration* specifies another module that the current top-level module 629*67e74705SXin Liintends to use. When the option *-fmodules-decluse* is specified, a module can 630*67e74705SXin Lionly use other modules that are explicitly specified in this way. 631*67e74705SXin Li 632*67e74705SXin Li.. parsed-literal:: 633*67e74705SXin Li 634*67e74705SXin Li *use-declaration*: 635*67e74705SXin Li ``use`` *module-id* 636*67e74705SXin Li 637*67e74705SXin Li**Example:** In the following example, use of A from C is not declared, so will trigger a warning. 638*67e74705SXin Li 639*67e74705SXin Li.. parsed-literal:: 640*67e74705SXin Li 641*67e74705SXin Li module A { 642*67e74705SXin Li header "a.h" 643*67e74705SXin Li } 644*67e74705SXin Li 645*67e74705SXin Li module B { 646*67e74705SXin Li header "b.h" 647*67e74705SXin Li } 648*67e74705SXin Li 649*67e74705SXin Li module C { 650*67e74705SXin Li header "c.h" 651*67e74705SXin Li use B 652*67e74705SXin Li } 653*67e74705SXin Li 654*67e74705SXin LiWhen compiling a source file that implements a module, use the option 655*67e74705SXin Li``-fmodule-name=module-id`` to indicate that the source file is logically part 656*67e74705SXin Liof that module. 657*67e74705SXin Li 658*67e74705SXin LiThe compiler at present only applies restrictions to the module directly being built. 659*67e74705SXin Li 660*67e74705SXin LiLink declaration 661*67e74705SXin Li~~~~~~~~~~~~~~~~ 662*67e74705SXin LiA *link-declaration* specifies a library or framework against which a program should be linked if the enclosing module is imported in any translation unit in that program. 663*67e74705SXin Li 664*67e74705SXin Li.. parsed-literal:: 665*67e74705SXin Li 666*67e74705SXin Li *link-declaration*: 667*67e74705SXin Li ``link`` ``framework``:sub:`opt` *string-literal* 668*67e74705SXin Li 669*67e74705SXin LiThe *string-literal* specifies the name of the library or framework against which the program should be linked. For example, specifying "clangBasic" would instruct the linker to link with ``-lclangBasic`` for a Unix-style linker. 670*67e74705SXin Li 671*67e74705SXin LiA *link-declaration* with the ``framework`` specifies that the linker should link against the named framework, e.g., with ``-framework MyFramework``. 672*67e74705SXin Li 673*67e74705SXin Li.. note:: 674*67e74705SXin Li 675*67e74705SXin Li Automatic linking with the ``link`` directive is not yet widely 676*67e74705SXin Li implemented, because it requires support from both the object file 677*67e74705SXin Li format and the linker. The notion is similar to Microsoft Visual 678*67e74705SXin Li Studio's ``#pragma comment(lib...)``. 679*67e74705SXin Li 680*67e74705SXin LiConfiguration macros declaration 681*67e74705SXin Li~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 682*67e74705SXin LiThe *config-macros-declaration* specifies the set of configuration macros that have an effect on the API of the enclosing module. 683*67e74705SXin Li 684*67e74705SXin Li.. parsed-literal:: 685*67e74705SXin Li 686*67e74705SXin Li *config-macros-declaration*: 687*67e74705SXin Li ``config_macros`` *attributes*:sub:`opt` *config-macro-list*:sub:`opt` 688*67e74705SXin Li 689*67e74705SXin Li *config-macro-list*: 690*67e74705SXin Li *identifier* (',' *identifier*)* 691*67e74705SXin Li 692*67e74705SXin LiEach *identifier* in the *config-macro-list* specifies the name of a macro. The compiler is required to maintain different variants of the given module for differing definitions of any of the named macros. 693*67e74705SXin Li 694*67e74705SXin LiA *config-macros-declaration* shall only be present on a top-level module, i.e., a module that is not nested within an enclosing module. 695*67e74705SXin Li 696*67e74705SXin LiThe ``exhaustive`` attribute specifies that the list of macros in the *config-macros-declaration* is exhaustive, meaning that no other macro definition is intended to have an effect on the API of that module. 697*67e74705SXin Li 698*67e74705SXin Li.. note:: 699*67e74705SXin Li 700*67e74705SXin Li The ``exhaustive`` attribute implies that any macro definitions 701*67e74705SXin Li for macros not listed as configuration macros should be ignored 702*67e74705SXin Li completely when building the module. As an optimization, the 703*67e74705SXin Li compiler could reduce the number of unique module variants by not 704*67e74705SXin Li considering these non-configuration macros. This optimization is not 705*67e74705SXin Li yet implemented in Clang. 706*67e74705SXin Li 707*67e74705SXin LiA translation unit shall not import the same module under different definitions of the configuration macros. 708*67e74705SXin Li 709*67e74705SXin Li.. note:: 710*67e74705SXin Li 711*67e74705SXin Li Clang implements a weak form of this requirement: the definitions 712*67e74705SXin Li used for configuration macros are fixed based on the definitions 713*67e74705SXin Li provided by the command line. If an import occurs and the definition 714*67e74705SXin Li of any configuration macro has changed, the compiler will produce a 715*67e74705SXin Li warning (under the control of ``-Wconfig-macros``). 716*67e74705SXin Li 717*67e74705SXin Li**Example:** A logging library might provide different API (e.g., in the form of different definitions for a logging macro) based on the ``NDEBUG`` macro setting: 718*67e74705SXin Li 719*67e74705SXin Li.. parsed-literal:: 720*67e74705SXin Li 721*67e74705SXin Li module MyLogger { 722*67e74705SXin Li umbrella header "MyLogger.h" 723*67e74705SXin Li config_macros [exhaustive] NDEBUG 724*67e74705SXin Li } 725*67e74705SXin Li 726*67e74705SXin LiConflict declarations 727*67e74705SXin Li~~~~~~~~~~~~~~~~~~~~~ 728*67e74705SXin LiA *conflict-declaration* describes a case where the presence of two different modules in the same translation unit is likely to cause a problem. For example, two modules may provide similar-but-incompatible functionality. 729*67e74705SXin Li 730*67e74705SXin Li.. parsed-literal:: 731*67e74705SXin Li 732*67e74705SXin Li *conflict-declaration*: 733*67e74705SXin Li ``conflict`` *module-id* ',' *string-literal* 734*67e74705SXin Li 735*67e74705SXin LiThe *module-id* of the *conflict-declaration* specifies the module with which the enclosing module conflicts. The specified module shall not have been imported in the translation unit when the enclosing module is imported. 736*67e74705SXin Li 737*67e74705SXin LiThe *string-literal* provides a message to be provided as part of the compiler diagnostic when two modules conflict. 738*67e74705SXin Li 739*67e74705SXin Li.. note:: 740*67e74705SXin Li 741*67e74705SXin Li Clang emits a warning (under the control of ``-Wmodule-conflict``) 742*67e74705SXin Li when a module conflict is discovered. 743*67e74705SXin Li 744*67e74705SXin Li**Example:** 745*67e74705SXin Li 746*67e74705SXin Li.. parsed-literal:: 747*67e74705SXin Li 748*67e74705SXin Li module Conflicts { 749*67e74705SXin Li explicit module A { 750*67e74705SXin Li header "conflict_a.h" 751*67e74705SXin Li conflict B, "we just don't like B" 752*67e74705SXin Li } 753*67e74705SXin Li 754*67e74705SXin Li module B { 755*67e74705SXin Li header "conflict_b.h" 756*67e74705SXin Li } 757*67e74705SXin Li } 758*67e74705SXin Li 759*67e74705SXin Li 760*67e74705SXin LiAttributes 761*67e74705SXin Li---------- 762*67e74705SXin LiAttributes are used in a number of places in the grammar to describe specific behavior of other declarations. The format of attributes is fairly simple. 763*67e74705SXin Li 764*67e74705SXin Li.. parsed-literal:: 765*67e74705SXin Li 766*67e74705SXin Li *attributes*: 767*67e74705SXin Li *attribute* *attributes*:sub:`opt` 768*67e74705SXin Li 769*67e74705SXin Li *attribute*: 770*67e74705SXin Li '[' *identifier* ']' 771*67e74705SXin Li 772*67e74705SXin LiAny *identifier* can be used as an attribute, and each declaration specifies what attributes can be applied to it. 773*67e74705SXin Li 774*67e74705SXin LiPrivate Module Map Files 775*67e74705SXin Li------------------------ 776*67e74705SXin LiModule map files are typically named ``module.modulemap`` and live 777*67e74705SXin Lieither alongside the headers they describe or in a parent directory of 778*67e74705SXin Lithe headers they describe. These module maps typically describe all of 779*67e74705SXin Lithe API for the library. 780*67e74705SXin Li 781*67e74705SXin LiHowever, in some cases, the presence or absence of particular headers 782*67e74705SXin Liis used to distinguish between the "public" and "private" APIs of a 783*67e74705SXin Liparticular library. For example, a library may contain the headers 784*67e74705SXin Li``Foo.h`` and ``Foo_Private.h``, providing public and private APIs, 785*67e74705SXin Lirespectively. Additionally, ``Foo_Private.h`` may only be available on 786*67e74705SXin Lisome versions of library, and absent in others. One cannot easily 787*67e74705SXin Liexpress this with a single module map file in the library: 788*67e74705SXin Li 789*67e74705SXin Li.. parsed-literal:: 790*67e74705SXin Li 791*67e74705SXin Li module Foo { 792*67e74705SXin Li header "Foo.h" 793*67e74705SXin Li 794*67e74705SXin Li explicit module Private { 795*67e74705SXin Li header "Foo_Private.h" 796*67e74705SXin Li } 797*67e74705SXin Li } 798*67e74705SXin Li 799*67e74705SXin Li 800*67e74705SXin Libecause the header ``Foo_Private.h`` won't always be available. The 801*67e74705SXin Limodule map file could be customized based on whether 802*67e74705SXin Li``Foo_Private.h`` is available or not, but doing so requires custom 803*67e74705SXin Libuild machinery. 804*67e74705SXin Li 805*67e74705SXin LiPrivate module map files, which are named ``module.private.modulemap`` 806*67e74705SXin Li(or, for backward compatibility, ``module_private.map``), allow one to 807*67e74705SXin Liaugment the primary module map file with an additional submodule. For 808*67e74705SXin Liexample, we would split the module map file above into two module map 809*67e74705SXin Lifiles: 810*67e74705SXin Li 811*67e74705SXin Li.. code-block:: c 812*67e74705SXin Li 813*67e74705SXin Li /* module.modulemap */ 814*67e74705SXin Li module Foo { 815*67e74705SXin Li header "Foo.h" 816*67e74705SXin Li } 817*67e74705SXin Li 818*67e74705SXin Li /* module.private.modulemap */ 819*67e74705SXin Li explicit module Foo.Private { 820*67e74705SXin Li header "Foo_Private.h" 821*67e74705SXin Li } 822*67e74705SXin Li 823*67e74705SXin Li 824*67e74705SXin LiWhen a ``module.private.modulemap`` file is found alongside a 825*67e74705SXin Li``module.modulemap`` file, it is loaded after the ``module.modulemap`` 826*67e74705SXin Lifile. In our example library, the ``module.private.modulemap`` file 827*67e74705SXin Liwould be available when ``Foo_Private.h`` is available, making it 828*67e74705SXin Lieasier to split a library's public and private APIs along header 829*67e74705SXin Liboundaries. 830*67e74705SXin Li 831*67e74705SXin LiModularizing a Platform 832*67e74705SXin Li======================= 833*67e74705SXin LiTo get any benefit out of modules, one needs to introduce module maps for software libraries starting at the bottom of the stack. This typically means introducing a module map covering the operating system's headers and the C standard library headers (in ``/usr/include``, for a Unix system). 834*67e74705SXin Li 835*67e74705SXin LiThe module maps will be written using the `module map language`_, which provides the tools necessary to describe the mapping between headers and modules. Because the set of headers differs from one system to the next, the module map will likely have to be somewhat customized for, e.g., a particular distribution and version of the operating system. Moreover, the system headers themselves may require some modification, if they exhibit any anti-patterns that break modules. Such common patterns are described below. 836*67e74705SXin Li 837*67e74705SXin Li**Macro-guarded copy-and-pasted definitions** 838*67e74705SXin Li System headers vend core types such as ``size_t`` for users. These types are often needed in a number of system headers, and are almost trivial to write. Hence, it is fairly common to see a definition such as the following copy-and-pasted throughout the headers: 839*67e74705SXin Li 840*67e74705SXin Li .. parsed-literal:: 841*67e74705SXin Li 842*67e74705SXin Li #ifndef _SIZE_T 843*67e74705SXin Li #define _SIZE_T 844*67e74705SXin Li typedef __SIZE_TYPE__ size_t; 845*67e74705SXin Li #endif 846*67e74705SXin Li 847*67e74705SXin Li Unfortunately, when modules compiles all of the C library headers together into a single module, only the first actual type definition of ``size_t`` will be visible, and then only in the submodule corresponding to the lucky first header. Any other headers that have copy-and-pasted versions of this pattern will *not* have a definition of ``size_t``. Importing the submodule corresponding to one of those headers will therefore not yield ``size_t`` as part of the API, because it wasn't there when the header was parsed. The fix for this problem is either to pull the copied declarations into a common header that gets included everywhere ``size_t`` is part of the API, or to eliminate the ``#ifndef`` and redefine the ``size_t`` type. The latter works for C++ headers and C11, but will cause an error for non-modules C90/C99, where redefinition of ``typedefs`` is not permitted. 848*67e74705SXin Li 849*67e74705SXin Li**Conflicting definitions** 850*67e74705SXin Li Different system headers may provide conflicting definitions for various macros, functions, or types. These conflicting definitions don't tend to cause problems in a pre-modules world unless someone happens to include both headers in one translation unit. Since the fix is often simply "don't do that", such problems persist. Modules requires that the conflicting definitions be eliminated or that they be placed in separate modules (the former is generally the better answer). 851*67e74705SXin Li 852*67e74705SXin Li**Missing includes** 853*67e74705SXin Li Headers are often missing ``#include`` directives for headers that they actually depend on. As with the problem of conflicting definitions, this only affects unlucky users who don't happen to include headers in the right order. With modules, the headers of a particular module will be parsed in isolation, so the module may fail to build if there are missing includes. 854*67e74705SXin Li 855*67e74705SXin Li**Headers that vend multiple APIs at different times** 856*67e74705SXin Li Some systems have headers that contain a number of different kinds of API definitions, only some of which are made available with a given include. For example, the header may vend ``size_t`` only when the macro ``__need_size_t`` is defined before that header is included, and also vend ``wchar_t`` only when the macro ``__need_wchar_t`` is defined. Such headers are often included many times in a single translation unit, and will have no include guards. There is no sane way to map this header to a submodule. One can either eliminate the header (e.g., by splitting it into separate headers, one per actual API) or simply ``exclude`` it in the module map. 857*67e74705SXin Li 858*67e74705SXin LiTo detect and help address some of these problems, the ``clang-tools-extra`` repository contains a ``modularize`` tool that parses a set of given headers and attempts to detect these problems and produce a report. See the tool's in-source documentation for information on how to check your system or library headers. 859*67e74705SXin Li 860*67e74705SXin LiFuture Directions 861*67e74705SXin Li================= 862*67e74705SXin LiModules support is under active development, and there are many opportunities remaining to improve it. Here are a few ideas: 863*67e74705SXin Li 864*67e74705SXin Li**Detect unused module imports** 865*67e74705SXin Li Unlike with ``#include`` directives, it should be fairly simple to track whether a directly-imported module has ever been used. By doing so, Clang can emit ``unused import`` or ``unused #include`` diagnostics, including Fix-Its to remove the useless imports/includes. 866*67e74705SXin Li 867*67e74705SXin Li**Fix-Its for missing imports** 868*67e74705SXin Li It's fairly common for one to make use of some API while writing code, only to get a compiler error about "unknown type" or "no function named" because the corresponding header has not been included. Clang can detect such cases and auto-import the required module, but should provide a Fix-It to add the import. 869*67e74705SXin Li 870*67e74705SXin Li**Improve modularize** 871*67e74705SXin Li The modularize tool is both extremely important (for deployment) and extremely crude. It needs better UI, better detection of problems (especially for C++), and perhaps an assistant mode to help write module maps for you. 872*67e74705SXin Li 873*67e74705SXin LiWhere To Learn More About Modules 874*67e74705SXin Li================================= 875*67e74705SXin LiThe Clang source code provides additional information about modules: 876*67e74705SXin Li 877*67e74705SXin Li``clang/lib/Headers/module.modulemap`` 878*67e74705SXin Li Module map for Clang's compiler-specific header files. 879*67e74705SXin Li 880*67e74705SXin Li``clang/test/Modules/`` 881*67e74705SXin Li Tests specifically related to modules functionality. 882*67e74705SXin Li 883*67e74705SXin Li``clang/include/clang/Basic/Module.h`` 884*67e74705SXin Li The ``Module`` class in this header describes a module, and is used throughout the compiler to implement modules. 885*67e74705SXin Li 886*67e74705SXin Li``clang/include/clang/Lex/ModuleMap.h`` 887*67e74705SXin Li The ``ModuleMap`` class in this header describes the full module map, consisting of all of the module map files that have been parsed, and providing facilities for looking up module maps and mapping between modules and headers (in both directions). 888*67e74705SXin Li 889*67e74705SXin LiPCHInternals_ 890*67e74705SXin Li Information about the serialized AST format used for precompiled headers and modules. The actual implementation is in the ``clangSerialization`` library. 891*67e74705SXin Li 892*67e74705SXin Li.. [#] Automatic linking against the libraries of modules requires specific linker support, which is not widely available. 893*67e74705SXin Li 894*67e74705SXin Li.. [#] There are certain anti-patterns that occur in headers, particularly system headers, that cause problems for modules. The section `Modularizing a Platform`_ describes some of them. 895*67e74705SXin Li 896*67e74705SXin Li.. [#] The second instance is actually a new thread within the current process, not a separate process. However, the original compiler instance is blocked on the execution of this thread. 897*67e74705SXin Li 898*67e74705SXin Li.. [#] The preprocessing context in which the modules are parsed is actually dependent on the command-line options provided to the compiler, including the language dialect and any ``-D`` options. However, the compiled modules for different command-line options are kept distinct, and any preprocessor directives that occur within the translation unit are ignored. See the section on the `Configuration macros declaration`_ for more information. 899*67e74705SXin Li 900*67e74705SXin Li.. _PCHInternals: PCHInternals.html 901*67e74705SXin Li 902