[cpp.import] (original) (raw)
15 Preprocessing directives [cpp]
15.6 Header unit importation [cpp.import]
A pp-import shall not appear in a context where importor (if it is the first preprocessing token of the pp-import) exportis an identifier defined as an object-like macro.
The preprocessing tokens after the import preprocessing token in the import control-lineare processed just as in normal text (i.e., each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens).
[Note 1:
An import directive matching the first two forms of a pp-importinstructs the preprocessor to import macros from the header unit ([module.import]) denoted by the , as described below.
— _end note_]
The point of macro import for the first two forms of pp-import is immediately after the new-line terminating the pp-import.
The last form of pp-import is only considered if the first two forms did not match, and does not have a point of macro import.
If a pp-import is produced by source file inclusion (including by the rewrite produced when a #include directive names an importable header) while processing the group of a module-file, the program is ill-formed.
In all three forms of pp-import, the import and export (if it exists) preprocessing tokens are replaced by the import-keyword and_export-keyword_ preprocessing tokens respectively.
[Note 2:
This makes the line no longer a directive so it is not removed at the end of phase 4.
— _end note_]
Additionally, in the second form of pp-import, a token is formed as if the were the pp-tokens of a #include directive.
The are replaced by the token.
[Note 3:
This ensures that imports are treated consistently by the preprocessor and later phases of translation.
— _end note_]
Each #define directive encountered when preprocessing each translation unit in a program results in a distinctmacro definition.
[Note 4:
A predefined macro name ([cpp.predefined]) is not introduced by a #define directive.
Implementations providing mechanisms to predefine additional macros are encouraged to not treat them as being introduced by a #define directive.
— _end note_]
Each macro definition has at most one point of definition in each translation unit and at most one point of undefinition, as follows:
- The point of definitionof a macro definition within a translation unit T is
- if the #define directive of the macro definition occurs within T, the point at which that directive occurs, or otherwise,
- if the macro name is not lexically identical to a keyword ([lex.key]) or to the identifiers module or import, the first point of macro import in T of a header unit containing a point of definition for the macro definition, if any.
In the latter case, the macro is said to be imported from the header unit.
- The point of undefinitionof a macro definition within a translation unit is the first point at which a #undef directive naming the macro occurs after its point of definition, or the first point of macro import of a header unit containing a point of undefinition for the macro definition, whichever (if any) occurs first.
A macro definition is active at a source location if it has a point of definition in that translation unit preceding the location, and does not have a point of undefinition in that translation unit preceding the location.
If a macro would be replaced or redefined, and multiple macro definitions are active for that macro name, the active macro definitions shall all be valid redefinitions of the same macro ([cpp.replace]).
[Note 5:
The relative order of pp-imports has no bearing on whether a particular macro definition is active.
— _end note_]
[Example 1:
Importable header "a.h":#define X 123 #define Y 45 #define Z a #undef X
Importable header "b.h":import "a.h"; #define X 456 #define Y 6
Importable header "c.h":#define Y 45 #define Z c
Importable header "d.h":import "c.h";
Importable header "e.h":import "a.h"; import "d.h"; int a = Y; int c = Z;
Module unit f:export module f;export import "a.h";int a = Y;
Translation unit #1:import f;int x = Y; — _end example_]