blob: 141d3b85753f8d9d80722ab21ae20afdc383708f [file] [log] [blame]
Douglas Gregor30e9b6c2013-09-27 21:23:28 +00001=======
2Modules
3=======
Douglas Gregore703f2d2013-03-20 06:25:14 +00004
Sean Silva98c64d42013-09-09 19:57:49 +00005.. contents::
6 :local:
7
Douglas Gregore703f2d2013-03-20 06:25:14 +00008Introduction
9============
10Most 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
12.. code-block:: c
13
14 #include <SomeLib.h>
15
16The implementation is handled separately by linking against the appropriate library. For example, by passing ``-lSomeLib`` to the linker.
17
18Modules 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
Douglas Gregor485996c2013-03-22 07:05:07 +000020Problems with the current model
Douglas Gregore703f2d2013-03-20 06:25:14 +000021-------------------------------
22The ``#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
24* **Compile-time scalability**: Each time a header is included, the
25 compiler must preprocess and parse the text in that header and every
26 header it includes, transitively. This process must be repeated for
27 every translation unit in the application, which involves a huge
28 amount of redundant work. In a project with *N* translation units
29 and *M* headers included in each translation unit, the compiler is
30 performing *M x N* work even though most of the *M* headers are
31 shared among multiple translation units. C++ is particularly bad,
32 because the compilation model for templates forces a huge amount of
33 code into headers.
34
35* **Fragility**: ``#include`` directives are treated as textual
36 inclusion by the preprocessor, and are therefore subject to any
37 active macro definitions at the time of inclusion. If any of the
38 active macro definitions happens to collide with a name in the
39 library, it can break the library API or cause compilation failures
40 in the library header itself. For an extreme example,
41 ``#define std "The C++ Standard"`` and then include a standard
42 library header: the result is a horrific cascade of failures in the
43 C++ Standard Library's implementation. More subtle real-world
44 problems occur when the headers for two different libraries interact
45 due to macro collisions, and users are forced to reorder
46 ``#include`` directives or introduce ``#undef`` directives to break
47 the (unintended) dependency.
48
49* **Conventional workarounds**: C programmers have
50 adopted a number of conventions to work around the fragility of the
51 C preprocessor model. Include guards, for example, are required for
52 the vast majority of headers to ensure that multiple inclusion
53 doesn't break the compile. Macro names are written with
54 ``LONG_PREFIXED_UPPERCASE_IDENTIFIERS`` to avoid collisions, and some
55 library/framework developers even use ``__underscored`` names
56 in headers to avoid collisions with "normal" names that (by
57 convention) shouldn't even be macros. These conventions are a
58 barrier to entry for developers coming from non-C languages, are
59 boilerplate for more experienced developers, and make our headers
60 far uglier than they should be.
61
62* **Tool confusion**: In a C-based language, it is hard to build tools
63 that work well with software libraries, because the boundaries of
64 the libraries are not clear. Which headers belong to a particular
65 library, and in what order should those headers be included to
66 guarantee that they compile correctly? Are the headers C, C++,
67 Objective-C++, or one of the variants of these languages? What
68 declarations in those headers are actually meant to be part of the
69 API, and what declarations are present only because they had to be
70 written as part of the header file?
71
Douglas Gregor485996c2013-03-22 07:05:07 +000072Semantic import
Douglas Gregore703f2d2013-03-20 06:25:14 +000073---------------
74Modules 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
76.. code-block:: c
77
78 import std.io; // pseudo-code; see below for syntax discussion
79
80However, 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 [#]_
81This semantic import model addresses many of the problems of the preprocessor inclusion model:
82
83* **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
85* **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
87* **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
Douglas Gregor485996c2013-03-22 07:05:07 +000089Problems modules do not solve
Douglas Gregore703f2d2013-03-20 06:25:14 +000090-----------------------------
91Many 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
93* **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
95* **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
97* **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
Douglas Gregorbb1c7e32013-03-20 17:11:13 +000099* **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.
Douglas Gregore703f2d2013-03-20 06:25:14 +0000100
101Using Modules
102=============
Richard Smithffb65082014-09-30 23:10:19 +0000103To 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.
Douglas Gregore703f2d2013-03-20 06:25:14 +0000104
Douglas Gregor30e9b6c2013-09-27 21:23:28 +0000105Objective-C Import declaration
106------------------------------
107Objective-C provides syntax for importing a module via an *@import declaration*, which imports the named module:
Douglas Gregor485996c2013-03-22 07:05:07 +0000108
109.. parsed-literal::
110
Douglas Gregor30e9b6c2013-09-27 21:23:28 +0000111 @import std;
Douglas Gregor485996c2013-03-22 07:05:07 +0000112
Richard Smithbf653002014-09-29 17:46:41 +0000113The ``@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.,
Douglas Gregor485996c2013-03-22 07:05:07 +0000114
115.. parsed-literal::
116
Douglas Gregor30e9b6c2013-09-27 21:23:28 +0000117 @import std.io;
Douglas Gregor485996c2013-03-22 07:05:07 +0000118
119Redundant 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
Douglas Gregor30e9b6c2013-09-27 21:23:28 +0000121At present, there is no C or C++ syntax for import declarations. Clang
122will track the modules proposal in the C++ committee. See the section
123`Includes as imports`_ to see how modules get imported today.
Douglas Gregor485996c2013-03-22 07:05:07 +0000124
125Includes as imports
Douglas Gregore703f2d2013-03-20 06:25:14 +0000126-------------------
Douglas Gregor485996c2013-03-22 07:05:07 +0000127The 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
Douglas Gregore703f2d2013-03-20 06:25:14 +0000128
129.. code-block:: c
130
131 #include <stdio.h>
132
Douglas Gregorbb1c7e32013-03-20 17:11:13 +0000133will 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.
Douglas Gregore703f2d2013-03-20 06:25:14 +0000134
Douglas Gregor485996c2013-03-22 07:05:07 +0000135.. note::
136
137 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
Richard Smith25d50752014-10-20 00:15:49 +0000139While building a module, ``#include_next`` is also supported, with one caveat.
140The usual behavior of ``#include_next`` is to search for the specified filename
141in the list of include paths, starting from the path *after* the one
142in which the current file was found.
143Because files listed in module maps are not found through include paths, a
144different strategy is used for ``#include_next`` directives in such files: the
145list of include paths is searched for the specified header name, to find the
146first include path that would refer to the current file. ``#include_next`` is
147interpreted as if the current file had been found in that path.
148If this search finds a file named by a module map, the ``#include_next``
149directive is translated into an import, just like for a ``#include``
150directive.``
151
Douglas Gregor485996c2013-03-22 07:05:07 +0000152Module maps
Douglas Gregore703f2d2013-03-20 06:25:14 +0000153-----------
154The 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
Ben Langmuir984e1df2014-03-19 20:23:34 +0000156Module 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.
Douglas Gregore703f2d2013-03-20 06:25:14 +0000157
Douglas Gregor485996c2013-03-22 07:05:07 +0000158.. note::
159
160 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.
Daniel Jasper07e6c402013-08-05 20:26:17 +0000161
Richard Smithcf18b792015-06-16 00:20:23 +0000162One 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.
Douglas Gregor485996c2013-03-22 07:05:07 +0000163
164Compilation model
Douglas Gregore703f2d2013-03-20 06:25:14 +0000165-----------------
Douglas Gregor485996c2013-03-22 07:05:07 +0000166The 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.
Douglas Gregore703f2d2013-03-20 06:25:14 +0000167
168The 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
170Modules 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
172Command-line parameters
173-----------------------
174``-fmodules``
Richard Smithffb65082014-09-30 23:10:19 +0000175 Enable the modules feature.
Douglas Gregore703f2d2013-03-20 06:25:14 +0000176
Elad Cohen938f5162016-10-31 08:21:54 +0000177``-fbuiltin-module-map``
178 Load the Clang builtins module map file. (Equivalent to ``-fmodule-map-file=<resource dir>/include/module.modulemap``)
179
Richard Smithcf18b792015-06-16 00:20:23 +0000180``-fimplicit-module-maps``
181 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.
Daniel Jasper07e6c402013-08-05 20:26:17 +0000182
Douglas Gregore703f2d2013-03-20 06:25:14 +0000183``-fmodules-cache-path=<directory>``
184 Specify the path to the modules cache. If not provided, Clang will select a system-appropriate default.
185
Daniel Dunbare246fbe2013-04-16 18:21:19 +0000186``-fno-autolink``
187 Disable automatic linking against the libraries associated with imported modules.
Douglas Gregore703f2d2013-03-20 06:25:14 +0000188
189``-fmodules-ignore-macro=macroname``
190 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.
191
Douglas Gregor527b1c92013-03-25 21:19:16 +0000192``-fmodules-prune-interval=seconds``
193 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.
194
195``-fmodules-prune-after=seconds``
196 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.
197
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000198``-module-file-info <module file name>``
199 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.
200
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000201``-fmodules-decluse``
202 Enable checking of module ``use`` declarations.
203
204``-fmodule-name=module-id``
205 Consider a source file as a part of the given module.
206
Daniel Jasperca9f7382013-09-24 09:27:13 +0000207``-fmodule-map-file=<file>``
208 Load the given module map file if a header from its directory or one of its subdirectories is loaded.
209
John Thompson2255f2c2014-04-23 12:57:01 +0000210``-fmodules-search-all``
211 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.
212
Manuel Klimekd2e8b042015-02-20 11:44:41 +0000213``-fno-implicit-modules``
214 All modules used by the build must be specified with ``-fmodule-file``.
215
216``-fmodule-file=<file>``
217 Load the given precompiled module file.
218
Manman Ren11f2a472016-08-18 17:42:15 +0000219``-fprebuilt-module-path=<directory>``
220 Specify the path to the prebuilt modules. If specified, we will look for modules in this directory for a given top-level module name. We don't need a module map for loading prebuilt modules in this directory and the compiler will not try to rebuild these modules. This can be specified multiple times.
221
Richard Smith49f906a2014-03-01 00:08:04 +0000222Module Semantics
223================
224
225Modules 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.
226
227.. note::
228
Richard Smithf89e4922014-07-24 03:42:38 +0000229 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.
Richard Smith49f906a2014-03-01 00:08:04 +0000230
Adrian Prantlb20f1d02015-09-17 15:58:54 +0000231As 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.
Richard Smith49f906a2014-03-01 00:08:04 +0000232
233.. note::
234
235 Clang currently only performs minimal checking for violations of the One Definition Rule.
236
Richard Smithf89e4922014-07-24 03:42:38 +0000237If 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.
238
Richard Smith49f906a2014-03-01 00:08:04 +0000239Macros
240------
241
242The 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:
243
244* Each definition and undefinition of a macro is considered to be a distinct entity.
245* 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.
246* A ``#define X`` or ``#undef X`` directive *overrides* all definitions of ``X`` that are visible at the point of the directive.
247* A ``#define`` or ``#undef`` directive is *active* if it is visible and no visible directive overrides it.
248* 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).
249* 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.
250
251For example, suppose:
252
253* ``<stdio.h>`` defines a macro ``getc`` (and exports its ``#define``)
254* ``<cstdio>`` imports the ``<stdio.h>`` module and undefines the macro (and exports its ``#undef``)
255
256The ``#undef`` overrides the ``#define``, and a source file that imports both modules *in any order* will not see ``getc`` defined as a macro.
257
Douglas Gregore703f2d2013-03-20 06:25:14 +0000258Module Map Language
259===================
Douglas Gregore703f2d2013-03-20 06:25:14 +0000260
Richard Smithffb65082014-09-30 23:10:19 +0000261.. warning::
262
263 The module map language is not currently guaranteed to be stable between major revisions of Clang.
264
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000265The module map language describes the mapping from header files to the
266logical structure of modules. To enable support for using a library as
Ben Langmuir984e1df2014-03-19 20:23:34 +0000267a module, one must write a ``module.modulemap`` file for that library. The
268``module.modulemap`` file is placed alongside the header files themselves,
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000269and is written in the module map language described below.
270
Ben Langmuir984e1df2014-03-19 20:23:34 +0000271.. note::
Dmitri Gribenko3be06ff2014-03-28 19:25:09 +0000272 For compatibility with previous releases, if a module map file named
273 ``module.modulemap`` is not found, Clang will also search for a file named
274 ``module.map``. This behavior is deprecated and we plan to eventually
275 remove it.
Ben Langmuir984e1df2014-03-19 20:23:34 +0000276
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000277As an example, the module map file for the C standard library might look a bit like this:
278
279.. parsed-literal::
280
Richard Smith77944862014-03-02 05:58:18 +0000281 module std [system] [extern_c] {
Richard Smith306d8922014-10-22 23:50:56 +0000282 module assert {
283 textual header "assert.h"
284 header "bits/assert-decls.h"
285 export *
286 }
287
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000288 module complex {
289 header "complex.h"
290 export *
291 }
292
293 module ctype {
294 header "ctype.h"
295 export *
296 }
297
298 module errno {
299 header "errno.h"
300 header "sys/errno.h"
301 export *
302 }
303
304 module fenv {
305 header "fenv.h"
306 export *
307 }
308
309 // ...more headers follow...
310 }
311
312Here, 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.
313
Douglas Gregor485996c2013-03-22 07:05:07 +0000314Lexical structure
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000315-----------------
316Module 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.
317
318.. parsed-literal::
319
Richard Smith306d8922014-10-22 23:50:56 +0000320 ``config_macros`` ``export`` ``private``
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000321 ``conflict`` ``framework`` ``requires``
Richard Smith306d8922014-10-22 23:50:56 +0000322 ``exclude`` ``header`` ``textual``
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000323 ``explicit`` ``link`` ``umbrella``
Richard Smith306d8922014-10-22 23:50:56 +0000324 ``extern`` ``module`` ``use``
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000325
Douglas Gregor485996c2013-03-22 07:05:07 +0000326Module map file
327---------------
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000328A module map file consists of a series of module declarations:
329
330.. parsed-literal::
331
332 *module-map-file*:
333 *module-declaration**
334
335Within a module map file, modules are referred to by a *module-id*, which uses periods to separate each part of a module's name:
336
337.. parsed-literal::
338
339 *module-id*:
Dmitri Gribenko22ee0c12013-03-22 10:25:15 +0000340 *identifier* ('.' *identifier*)*
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000341
Douglas Gregor485996c2013-03-22 07:05:07 +0000342Module declaration
343------------------
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000344A module declaration describes a module, including the headers that contribute to that module, its submodules, and other aspects of the module.
345
346.. parsed-literal::
347
348 *module-declaration*:
349 ``explicit``:sub:`opt` ``framework``:sub:`opt` ``module`` *module-id* *attributes*:sub:`opt` '{' *module-member** '}'
Daniel Jasper97292842013-09-11 07:20:44 +0000350 ``extern`` ``module`` *module-id* *string-literal*
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000351
Richard Smith306d8922014-10-22 23:50:56 +0000352The *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.
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000353
354The ``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.
355
356The ``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:
357
358.. parsed-literal::
359
360 Name.framework/
Ben Langmuir984e1df2014-03-19 20:23:34 +0000361 Modules/module.modulemap Module map for the framework
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000362 Headers/ Subdirectory containing framework headers
363 Frameworks/ Subdirectory containing embedded frameworks
364 Resources/ Subdirectory containing additional resources
365 Name Symbolic link to the shared library for the framework
366
Richard Smith77944862014-03-02 05:58:18 +0000367The ``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.
368
369The ``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.
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000370
Bruno Cardoso Lopesed84df02016-10-21 01:41:56 +0000371The ``no_undeclared_includes`` attribute specifies that the module can only reach non-modular headers and headers from used modules. Since some headers could be present in more than one search path and map to different modules in each path, this mechanism helps clang to find the right header, i.e., prefer the one for the current module or in a submodule instead of the first usual match in the search paths.
372
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000373Modules can have a number of different kinds of members, each of which is described below:
374
Dmitri Gribenko22ee0c12013-03-22 10:25:15 +0000375.. parsed-literal::
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000376
377 *module-member*:
378 *requires-declaration*
379 *header-declaration*
380 *umbrella-dir-declaration*
381 *submodule-declaration*
382 *export-declaration*
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000383 *use-declaration*
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000384 *link-declaration*
385 *config-macros-declaration*
386 *conflict-declaration*
387
Daniel Jasper97292842013-09-11 07:20:44 +0000388An 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.
389
Douglas Gregor485996c2013-03-22 07:05:07 +0000390Requires declaration
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000391~~~~~~~~~~~~~~~~~~~~
392A *requires-declaration* specifies the requirements that an importing translation unit must satisfy to use the module.
393
394.. parsed-literal::
395
396 *requires-declaration*:
397 ``requires`` *feature-list*
398
399 *feature-list*:
Richard Smitha3feee22013-10-28 22:18:19 +0000400 *feature* (',' *feature*)*
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000401
Richard Smitha3feee22013-10-28 22:18:19 +0000402 *feature*:
403 ``!``:sub:`opt` *identifier*
404
405The 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.
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000406
407The following features are defined:
408
409altivec
410 The target supports AltiVec.
411
412blocks
413 The "blocks" language feature is available.
414
415cplusplus
416 C++ support is available.
417
418cplusplus11
419 C++11 support is available.
420
Elad Cohenfb6358d2016-09-04 06:00:42 +0000421freestanding
422 A freestanding environment is available.
423
Bruno Cardoso Lopes6736e192016-08-30 21:25:42 +0000424gnuinlineasm
425 GNU inline ASM is available.
426
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000427objc
428 Objective-C support is available.
429
430objc_arc
431 Objective-C Automatic Reference Counting (ARC) is available
432
433opencl
434 OpenCL is available
435
436tls
437 Thread local storage is available.
438
439*target feature*
440 A specific target feature (e.g., ``sse4``, ``avx``, ``neon``) is available.
441
442
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000443**Example:** The ``std`` module can be extended to also include C++ and C++11 headers using a *requires-declaration*:
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000444
445.. parsed-literal::
446
447 module std {
448 // C standard library...
449
450 module vector {
451 requires cplusplus
452 header "vector"
453 }
454
455 module type_traits {
456 requires cplusplus11
457 header "type_traits"
458 }
459 }
460
Douglas Gregor485996c2013-03-22 07:05:07 +0000461Header declaration
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000462~~~~~~~~~~~~~~~~~~
463A header declaration specifies that a particular header is associated with the enclosing module.
464
465.. parsed-literal::
466
467 *header-declaration*:
Richard Smith202210b2014-10-24 20:23:01 +0000468 ``private``:sub:`opt` ``textual``:sub:`opt` ``header`` *string-literal*
469 ``umbrella`` ``header`` *string-literal*
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000470 ``exclude`` ``header`` *string-literal*
471
Richard Smith306d8922014-10-22 23:50:56 +0000472A 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.
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000473
474A 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.
475
476.. note::
477 Any headers not included by the umbrella header should have
478 explicit ``header`` declarations. Use the
479 ``-Wincomplete-umbrella`` warning option to ask Clang to complain
480 about headers not covered by the umbrella header or the module map.
481
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000482A header with the ``private`` specifier may not be included from outside the module itself.
483
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000484A header with the ``textual`` specifier will not be compiled when the module is
485built, and will be textually included if it is named by a ``#include``
486directive. However, it is considered to be part of the module for the purpose
487of checking *use-declaration*\s, and must still be a lexically-valid header
488file. In the future, we intend to pre-tokenize such headers and include the
489token sequence within the prebuilt module representation.
Richard Smith306d8922014-10-22 23:50:56 +0000490
Richard Smith202210b2014-10-24 20:23:01 +0000491A 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.
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000492
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000493**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.
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000494
495.. parsed-literal::
496
497 module std [system] {
Richard Smith202210b2014-10-24 20:23:01 +0000498 textual header "assert.h"
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000499 }
500
501A given header shall not be referenced by more than one *header-declaration*.
502
Douglas Gregor485996c2013-03-22 07:05:07 +0000503Umbrella directory declaration
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000504~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
505An umbrella directory declaration specifies that all of the headers in the specified directory should be included within the module.
506
507.. parsed-literal::
508
509 *umbrella-dir-declaration*:
510 ``umbrella`` *string-literal*
511
512The *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.
513
514An *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.
515
516.. note::
517
518 Umbrella directories are useful for libraries that have a large number of headers but do not have an umbrella header.
519
520
Douglas Gregor485996c2013-03-22 07:05:07 +0000521Submodule declaration
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000522~~~~~~~~~~~~~~~~~~~~~
523Submodule declarations describe modules that are nested within their enclosing module.
524
525.. parsed-literal::
526
527 *submodule-declaration*:
528 *module-declaration*
529 *inferred-submodule-declaration*
530
531A *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.
532
533A *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*.
534
535.. parsed-literal::
536
537 *inferred-submodule-declaration*:
538 ``explicit``:sub:`opt` ``framework``:sub:`opt` ``module`` '*' *attributes*:sub:`opt` '{' *inferred-submodule-member** '}'
539
540 *inferred-submodule-member*:
541 ``export`` '*'
542
543A 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).
544
545For 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:
546
547* Have the same name as the header (without the file extension)
548* Have the ``explicit`` specifier, if the *inferred-submodule-declaration* has the ``explicit`` specifier
549* Have the ``framework`` specifier, if the
550 *inferred-submodule-declaration* has the ``framework`` specifier
551* Have the attributes specified by the \ *inferred-submodule-declaration*
552* Contain a single *header-declaration* naming that header
553* Contain a single *export-declaration* ``export *``, if the \ *inferred-submodule-declaration* contains the \ *inferred-submodule-member* ``export *``
554
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000555**Example:** If the subdirectory "MyLib" contains the headers ``A.h`` and ``B.h``, then the following module map:
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000556
557.. parsed-literal::
558
559 module MyLib {
560 umbrella "MyLib"
561 explicit module * {
562 export *
563 }
564 }
565
566is equivalent to the (more verbose) module map:
567
568.. parsed-literal::
569
570 module MyLib {
571 explicit module A {
572 header "A.h"
573 export *
574 }
575
576 explicit module B {
577 header "B.h"
578 export *
579 }
580 }
581
Douglas Gregor485996c2013-03-22 07:05:07 +0000582Export declaration
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000583~~~~~~~~~~~~~~~~~~
584An *export-declaration* specifies which imported modules will automatically be re-exported as part of a given module's API.
585
586.. parsed-literal::
587
588 *export-declaration*:
589 ``export`` *wildcard-module-id*
590
591 *wildcard-module-id*:
592 *identifier*
593 '*'
594 *identifier* '.' *wildcard-module-id*
595
596The *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.
597
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000598**Example:** In the following example, importing ``MyLib.Derived`` also provides the API for ``MyLib.Base``:
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000599
600.. parsed-literal::
601
602 module MyLib {
603 module Base {
604 header "Base.h"
605 }
606
607 module Derived {
608 header "Derived.h"
609 export Base
610 }
611 }
612
613Note that, if ``Derived.h`` includes ``Base.h``, one can simply use a wildcard export to re-export everything ``Derived.h`` includes:
614
615.. parsed-literal::
616
617 module MyLib {
618 module Base {
619 header "Base.h"
620 }
621
622 module Derived {
623 header "Derived.h"
624 export *
625 }
626 }
627
628.. note::
629
630 The wildcard export syntax ``export *`` re-exports all of the
631 modules that were imported in the actual header file. Because
632 ``#include`` directives are automatically mapped to module imports,
633 ``export *`` provides the same transitive-inclusion behavior
634 provided by the C preprocessor, e.g., importing a given module
635 implicitly imports all of the modules on which it depends.
636 Therefore, liberal use of ``export *`` provides excellent backward
637 compatibility for programs that rely on transitive inclusion (i.e.,
638 all of them).
639
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000640Use declaration
641~~~~~~~~~~~~~~~
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000642A *use-declaration* specifies another module that the current top-level module
643intends to use. When the option *-fmodules-decluse* is specified, a module can
644only use other modules that are explicitly specified in this way.
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000645
646.. parsed-literal::
647
648 *use-declaration*:
649 ``use`` *module-id*
650
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000651**Example:** In the following example, use of A from C is not declared, so will trigger a warning.
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000652
653.. parsed-literal::
654
655 module A {
656 header "a.h"
657 }
658
659 module B {
660 header "b.h"
661 }
662
663 module C {
664 header "c.h"
665 use B
666 }
667
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000668When compiling a source file that implements a module, use the option
669``-fmodule-name=module-id`` to indicate that the source file is logically part
670of that module.
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000671
672The compiler at present only applies restrictions to the module directly being built.
673
Douglas Gregor485996c2013-03-22 07:05:07 +0000674Link declaration
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000675~~~~~~~~~~~~~~~~
676A *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.
677
678.. parsed-literal::
679
680 *link-declaration*:
681 ``link`` ``framework``:sub:`opt` *string-literal*
682
683The *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.
684
685A *link-declaration* with the ``framework`` specifies that the linker should link against the named framework, e.g., with ``-framework MyFramework``.
686
687.. note::
688
689 Automatic linking with the ``link`` directive is not yet widely
690 implemented, because it requires support from both the object file
691 format and the linker. The notion is similar to Microsoft Visual
692 Studio's ``#pragma comment(lib...)``.
693
Douglas Gregor485996c2013-03-22 07:05:07 +0000694Configuration macros declaration
695~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Eric Christopher2c4555a2015-06-19 01:52:53 +0000696The *config-macros-declaration* specifies the set of configuration macros that have an effect on the API of the enclosing module.
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000697
698.. parsed-literal::
699
700 *config-macros-declaration*:
701 ``config_macros`` *attributes*:sub:`opt` *config-macro-list*:sub:`opt`
702
703 *config-macro-list*:
704 *identifier* (',' *identifier*)*
705
706Each *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.
707
708A *config-macros-declaration* shall only be present on a top-level module, i.e., a module that is not nested within an enclosing module.
709
710The ``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.
711
712.. note::
713
714 The ``exhaustive`` attribute implies that any macro definitions
715 for macros not listed as configuration macros should be ignored
716 completely when building the module. As an optimization, the
717 compiler could reduce the number of unique module variants by not
718 considering these non-configuration macros. This optimization is not
719 yet implemented in Clang.
720
721A translation unit shall not import the same module under different definitions of the configuration macros.
722
723.. note::
724
725 Clang implements a weak form of this requirement: the definitions
726 used for configuration macros are fixed based on the definitions
727 provided by the command line. If an import occurs and the definition
728 of any configuration macro has changed, the compiler will produce a
729 warning (under the control of ``-Wconfig-macros``).
730
731**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:
732
733.. parsed-literal::
734
735 module MyLogger {
736 umbrella header "MyLogger.h"
737 config_macros [exhaustive] NDEBUG
738 }
739
Douglas Gregor485996c2013-03-22 07:05:07 +0000740Conflict declarations
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000741~~~~~~~~~~~~~~~~~~~~~
742A *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.
743
744.. parsed-literal::
745
746 *conflict-declaration*:
747 ``conflict`` *module-id* ',' *string-literal*
748
749The *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.
750
751The *string-literal* provides a message to be provided as part of the compiler diagnostic when two modules conflict.
752
753.. note::
754
755 Clang emits a warning (under the control of ``-Wmodule-conflict``)
756 when a module conflict is discovered.
757
758**Example:**
759
760.. parsed-literal::
761
762 module Conflicts {
763 explicit module A {
764 header "conflict_a.h"
765 conflict B, "we just don't like B"
766 }
767
768 module B {
769 header "conflict_b.h"
770 }
771 }
772
773
774Attributes
775----------
776Attributes are used in a number of places in the grammar to describe specific behavior of other declarations. The format of attributes is fairly simple.
777
778.. parsed-literal::
779
780 *attributes*:
781 *attribute* *attributes*:sub:`opt`
782
783 *attribute*:
784 '[' *identifier* ']'
785
786Any *identifier* can be used as an attribute, and each declaration specifies what attributes can be applied to it.
787
Douglas Gregorb5ecb902014-03-28 19:05:18 +0000788Private Module Map Files
789------------------------
790Module map files are typically named ``module.modulemap`` and live
791either alongside the headers they describe or in a parent directory of
792the headers they describe. These module maps typically describe all of
793the API for the library.
794
795However, in some cases, the presence or absence of particular headers
796is used to distinguish between the "public" and "private" APIs of a
797particular library. For example, a library may contain the headers
798``Foo.h`` and ``Foo_Private.h``, providing public and private APIs,
799respectively. Additionally, ``Foo_Private.h`` may only be available on
800some versions of library, and absent in others. One cannot easily
801express this with a single module map file in the library:
802
803.. parsed-literal::
804
805 module Foo {
806 header "Foo.h"
807
808 explicit module Private {
809 header "Foo_Private.h"
810 }
811 }
812
813
814because the header ``Foo_Private.h`` won't always be available. The
815module map file could be customized based on whether
Reid Kleckner230f6622014-04-18 21:55:49 +0000816``Foo_Private.h`` is available or not, but doing so requires custom
Douglas Gregorb5ecb902014-03-28 19:05:18 +0000817build machinery.
818
819Private module map files, which are named ``module.private.modulemap``
820(or, for backward compatibility, ``module_private.map``), allow one to
821augment the primary module map file with an additional submodule. For
822example, we would split the module map file above into two module map
823files:
824
Reid Kleckner230f6622014-04-18 21:55:49 +0000825.. code-block:: c
Douglas Gregorb5ecb902014-03-28 19:05:18 +0000826
827 /* module.modulemap */
828 module Foo {
829 header "Foo.h"
830 }
831
832 /* module.private.modulemap */
833 explicit module Foo.Private {
834 header "Foo_Private.h"
835 }
836
837
838When a ``module.private.modulemap`` file is found alongside a
839``module.modulemap`` file, it is loaded after the ``module.modulemap``
840file. In our example library, the ``module.private.modulemap`` file
841would be available when ``Foo_Private.h`` is available, making it
842easier to split a library's public and private APIs along header
843boundaries.
844
Douglas Gregor485996c2013-03-22 07:05:07 +0000845Modularizing a Platform
846=======================
847To 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).
848
849The 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.
850
851**Macro-guarded copy-and-pasted definitions**
852 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:
853
854 .. parsed-literal::
855
856 #ifndef _SIZE_T
857 #define _SIZE_T
858 typedef __SIZE_TYPE__ size_t;
859 #endif
860
861 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.
862
863**Conflicting definitions**
864 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).
865
866**Missing includes**
867 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.
868
869**Headers that vend multiple APIs at different times**
870 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.
871
872To 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.
873
Douglas Gregoraeb2a3c2013-03-22 07:08:56 +0000874Future Directions
875=================
Richard Smithffb65082014-09-30 23:10:19 +0000876Modules support is under active development, and there are many opportunities remaining to improve it. Here are a few ideas:
Douglas Gregoraeb2a3c2013-03-22 07:08:56 +0000877
878**Detect unused module imports**
879 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.
880
881**Fix-Its for missing imports**
Richard Smithbf653002014-09-29 17:46:41 +0000882 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.
Douglas Gregoraeb2a3c2013-03-22 07:08:56 +0000883
884**Improve modularize**
885 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.
886
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000887Where To Learn More About Modules
888=================================
889The Clang source code provides additional information about modules:
890
Ben Langmuir47d1ca42014-04-17 00:52:48 +0000891``clang/lib/Headers/module.modulemap``
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000892 Module map for Clang's compiler-specific header files.
893
894``clang/test/Modules/``
895 Tests specifically related to modules functionality.
896
Douglas Gregor485996c2013-03-22 07:05:07 +0000897``clang/include/clang/Basic/Module.h``
898 The ``Module`` class in this header describes a module, and is used throughout the compiler to implement modules.
899
900``clang/include/clang/Lex/ModuleMap.h``
901 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).
902
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000903PCHInternals_
Douglas Gregor485996c2013-03-22 07:05:07 +0000904 Information about the serialized AST format used for precompiled headers and modules. The actual implementation is in the ``clangSerialization`` library.
Douglas Gregore703f2d2013-03-20 06:25:14 +0000905
906.. [#] Automatic linking against the libraries of modules requires specific linker support, which is not widely available.
907
Douglas Gregor485996c2013-03-22 07:05:07 +0000908.. [#] 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.
Douglas Gregore703f2d2013-03-20 06:25:14 +0000909
Douglas Gregor485996c2013-03-22 07:05:07 +0000910.. [#] 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.
Douglas Gregore703f2d2013-03-20 06:25:14 +0000911
Douglas Gregor485996c2013-03-22 07:05:07 +0000912.. [#] 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.
Douglas Gregorde0beaa2013-03-22 06:21:35 +0000913
914.. _PCHInternals: PCHInternals.html
915