The C++ source file is processed by the compiler as if the following phases take place, in this exact order:
'0'
to '9'
'a'
to 'z'
and from 'A'
to 'Z'
_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , \ " '
\u
or \U
) or by some implementation-defined form that is handled equivalently.3) Trigraph sequences are replaced by corresponding single-character representations. | (until C++17) |
\uXXXX
) is formed in this phase, the behavior is undefined.<iostream>
or "myfile.h"
2) Any transformations performed during phases 1 and 2 between the initial and the final double quote of any raw string literal are reverted. | (since C++11) |
Newlines are kept, and it's unspecified whether non-newline whitespace sequences may be collapsed into single space characters.
If the input has been parsed into preprocessing tokens up to a given character, the next preprocessing token is generally taken to be the longest sequence of characters that could constitute a preprocessing token, even if that would cause subsequent analysis to fail. This is commonly known as maximal munch.
int foo = 1; int bar = 0xE+foo; // error, invalid preprocessing number 0xE+foo int baz = 0xE + foo; // OK int quux = bar+++++baz; // error: bar++ ++ +baz, not bar++ + ++baz.
The sole exceptions to the maximal munch rule are:
#define R "x" const char* s = R"y"; // ill-formed raw string literal, not "x" "y" const char* s2 = R"(a)" "b)"; // a raw string literal followed by a normal string literal
struct Foo { static const int v = 1; }; std::vector<::Foo> x; // OK, <: not taken as the alternative token for [ extern int y<::>; // OK, same as extern int y[]. int z<:::Foo::value:>; // OK, int z[::Foo::value]; | (since C++11) |
#include
directive. std::vector<int> x; // OK, <int> not a header-name
Note: the conversion performed at this stage can be controlled by command line options in some implementations: gcc and clang use -finput-charset
to specify the encoding of the source character set, -fexec-charset
and -fwide-exec-charset
to specify the encodings of the execution character set in the string and character literals that don't have an encoding prefix (since C++11), while Visual Studio 2015 Update 2 and later uses /source-charset
and /execution-charset
to specify the source character set and execution character set respectively.
Adjacent string literals are concatenated.
Compilation takes place: each preprocessing token is converted to a token. The tokens are syntactically and semantically analyzed and translated as a translation unit.
Each translation unit is examined to produce a list of required template instantiations, including the ones requested by explicit instantiations. The definitions of the templates are located, and the required instantiations are performed to produce instantiation units.
Translation units, instantiation units, and library components needed to satisfy external references are collected into a program image which contains information needed for execution in its execution environment.
Some compilers don't implement instantiation units (also known as template repositories or template registries) and simply compile each template instantiation at Phase 7, storing the code in the object file where it is implicitly or explicitly requested, and then the linker collapses these compiled instantiations into one at Phase 9.
C documentation for phases of translation |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/language/translation_phases