also handles special macros like '__LINE__'. Although these macros expand to a single token which cannot contain any further macros, for reasons of token spacing (*note Token Spacing::) and simplicity of implementation, cpplib handles these special macros by pushing a context containing just that one token. The final thing that 'enter_macro_context' does before returning is to mark the macro disabled for expansion (except for special macros like '__TIME__'). The macro is re-enabled when its context is later popped from the context stack, as described above. This strict ordering ensures that a macro is disabled whilst its expansion is being scanned, but that it is _not_ disabled whilst any arguments to it are being expanded. Scanning the replacement list for macros to expand ================================================== The C standard states that, after any parameters have been replaced with their possibly-expanded arguments, the replacement list is scanned for nested macros. Further, any identifiers in the replacement list that are not expanded during this scan are never again eligible for expansion in the future, if the reason they were not expanded is that the macro in question was disabled. Clearly this latter condition can only apply to tokens resulting from argument pre-expansion. Other tokens never have an opportunity to be re-tested for expansion. It is possible for identifiers that are function-like macros to not expand initially but to expand during a later scan. This occurs when the identifier is the last token of an argument (and therefore originally followed by a comma or a closing parenthesis in its macro's argument list), and when it replaces its parameter in the macro's replacement list, the subsequent token happens to be an opening parenthesis (itself possibly the first token of an argument). It is important to note that when cpplib reads the last token of a given context, that context still remains on the stack. Only when looking for the _next_ token do we pop it off the stack and drop to a lower context. This makes backing up by one token easy, but more importantly ensures that the macro corresponding to the current context is still disabled when we are considering the last token of its replacement list for expansion (or indeed expanding it). As an example, which illustrates many of the points above, consider #define foo(x) bar x foo(foo) (2) which fully expands to 'bar foo (2)'. During pre-expansion of the argument, 'foo' does not expand even though the macro is enabled, since it has no following parenthesis [pre-expansion of an argument only uses tokens from that argument; it cannot take tokens from whatever follows the macro invocation]. This still leaves the argument token 'foo' eligible for future expansion. Then, when re-scanning after argument replacement, the token 'foo' is rejected for expansion, and marked ineligible for future expansion, since the macro is now disabled. It is disabled because the replacement list 'bar foo' of the macro is still on the context stack. If instead the algorithm looked for an opening parenthesis first and then tested whether the macro were disabled it would be subtly wrong. In the example above, the replacement list of 'foo' would be popped in the process of finding the parenthesis, re-enabling 'foo' and expanding it a second time. Looking for a function-like macro's opening parenthesis ======================================================= Function-like macros only expand when immediately followed by a parenthesis. To do this cpplib needs to temporarily disable macros and read the next token. Unfortunately, because of spacing issues (*note Token Spacing::), there can be fake padding tokens in-between, and if the next real token is not a parenthesis cpplib needs to be able to back up that one token as well as retain the information in any intervening padding tokens. Backing up more than one token when macros are involved is not permitted by cpplib, because in general it might involve issues like restoring popped contexts onto the context stack, which are too hard. Instead, searching for the parenthesis is handled by a special function, 'funlike_invocation_p', which remembers padding information as it reads tokens. If the next real token is not an opening parenthesis, it backs up that one token, and then pushes an extra context just containing the padding information if necessary. Marking tokens ineligible for future expansion ============================================== As discussed above, cpplib needs a way of marking tokens as unexpandable. Since the tokens cpplib handles are read-only once they have been lexed, it instead makes a copy of the token and adds the flag 'NO_EXPAND' to the copy. For efficiency and to simplify memory management by avoiding having to remember to free these tokens, they are allocated as temporary tokens from the lexer's current token run (*note Lexing a line::) using the function '_cpp_temp_token'. The tokens are then re-used once the current line of tokens has been read in. This might sound unsafe. However, tokens runs are not re-used at the end of a line if it happens to be in the middle of a macro argument list, and cpplib only wants to back-up more than one lexer token in situations where no macro expansion is involved, so the optimization is safe.