HomePhabricator

[c++2a] Implement P0306 __VA_OPT__ (Comma omission and comma deletion)

Description

[c++2a] Implement P0306 VA_OPT (Comma omission and comma deletion)

This patch implements an extension to the preprocessor:

VA_OPT(contents) --> which expands into its contents if variadic arguments are supplied to the parent macro, or behaves as an empty token if none.

  • Currently this feature is only enabled for C++2a (this could be enabled, with some careful tweaks, for other dialects with the appropriate extension or compatibility warnings)
  • The patch was reviewed here: https://reviews.llvm.org/D35782 and asides from the above (and moving some of the definition and expansion recognition logic into the corresponding state machines), I believe I incorporated all of Richard's suggestions.

A few technicalities (most of which were clarified through private correspondence between rsmith, hubert and thomas) are worth mentioning. Given:

#define F(a,...) a #__VA_OPT__(a ## a)  a ## __VA_OPT__(__VA_ARGS__)

- The call F(,) Does not supply any tokens for the variadic arguments and hence VA_OPT behaves as a placeholder.
- When expanding VA_OPT (for e.g. F(,1) token pasting occurs eagerly within its contents if the contents need to be stringified.
- A hash or a hashhash prior to VA_OPT does not inhibit expansion of arguments if they are the first token within VA_OPT.
- When a variadic argument is supplied, argument substitution occurs within the contents as does stringification - and these resulting tokens are inserted back into the macro expansions token stream just prior to the entire stream being rescanned and concatenated.

See wg21.link/P0306 for further details on the feature.

Acknowledgment: This patch would have been poorer if not for Richard Smith's usual thoughtful analysis and feedback.

Details

Committed
faisalvOct 14 2017, 6:26 PM
Parents
rL315839: [zorg] Use max available number of CPUs
Branches
Unknown
Tags
Unknown