You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a module A exports a macro M, and a module B imports that macro and #undef's
it, importers of B should not see the macro. This is complicated by the fact
that A's macro could also be visible through a different path. The rules (as
hashed out on cfe-commits) are included as a documentation update in this
change.
With this, the number of regressions in libc++'s testsuite when modules are
enabled drops from 47 to 7. Those remaining 7 are also macro-related, and are
due to remaining bugs in this change (in particular, the handling of submodules
is imperfect).
llvm-svn: 202560
Copy file name to clipboardExpand all lines: clang/docs/Modules.rst
+34Lines changed: 34 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -198,6 +198,40 @@ Command-line parameters
198
198
``-fmodule-map-file=<file>``
199
199
Load the given module map file if a header from its directory or one of its subdirectories is loaded.
200
200
201
+
Module Semantics
202
+
================
203
+
204
+
Modules 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.
205
+
206
+
.. note::
207
+
208
+
This behavior is currently only approximated when building a module. 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.
209
+
210
+
As 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.
211
+
212
+
.. note::
213
+
214
+
Clang currently only performs minimal checking for violations of the One Definition Rule.
215
+
216
+
Macros
217
+
------
218
+
219
+
The 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:
220
+
221
+
* Each definition and undefinition of a macro is considered to be a distinct entity.
222
+
* 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.
223
+
* A ``#define X`` or ``#undef X`` directive *overrides* all definitions of ``X`` that are visible at the point of the directive.
224
+
* A ``#define`` or ``#undef`` directive is *active* if it is visible and no visible directive overrides it.
225
+
* 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).
226
+
* 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.
227
+
228
+
For example, suppose:
229
+
230
+
* ``<stdio.h>`` defines a macro ``getc`` (and exports its ``#define``)
231
+
* ``<cstdio>`` imports the ``<stdio.h>`` module and undefines the macro (and exports its ``#undef``)
232
+
233
+
The ``#undef`` overrides the ``#define``, and a source file that imports both modules *in any order* will not see ``getc`` defined as a macro.
0 commit comments