cplusplus / draft

C++ standards drafts
http://www.open-std.org/jtc1/sc22/wg21/
5.66k stars 749 forks source link

Consider introducing a new grammar `pp-balanced-token-seq` #5022

Open tkoeppe opened 2 years ago

tkoeppe commented 2 years ago

Just a note from a discussion with Richard, for future consideration. We have a few places in which we spell out in prose how to form a balanced preprocessor token sequence. This could be simplified if we defined a new piece of grammar. The idea, as described by @zygoloid, might look like this:

pp-balanced-token:
    any preprocessor-token other than ( or )
    ( pp-balanced-token-seq[opt] )
pp-balanced-token-seq:
    pp-balanced-token
    pp-balanced-token-seq pp-balanced-token
va-opt-replacement:
    __VA_OPT__ ( pp-balanced-token-seq[opt] )

Then:

""" delete [cpp.replace.general]p5 (it's misplaced anyway; this belongs in p12). I n p12, delete "Each subsequent [...rest of paragraph 12 and paragraph 13...]".

Add new paragraph after p12, combining parts of p5, p12, p13:

"Each instance of a function-like macro name followed by a ( as the next preprocessing token shall be a function-like macro invocation of the form:

identifier ( pp-balanced-token-seq[opt] )

Within the sequence of preprocessing tokens making up an invocation of a function-like macro, new-line is considered a normal whitespace character. The arguments of the macro invocation are the sequences of pp-balanced-tokens separated by comma preprocessing tokens. If the identifier-list in the macro definition does not end with an ellipsis, the number of arguments (including those arguments consisting of no pp-balanced-tokens) shall equal the number of parameters in the macro definition. Otherwise, there shall be at least as many arguments in the invocation as there are parameters in the macro definition (excluding the ...). If there are sequences of preprocessing tokens within the list of arguments that would otherwise act as preprocessing directives,[Footnote] the behavior is undefined."

Then include p15 ("If there is a ... immediately preceding the )").

Then a new paragraph defining the semantics:

"Each function-like macro invocation is replaced by the replacement list in the macro definition, after performing argument substitution as described in [cpp.subst]."

Then finally p14 (the example).

I might also want to split out an "object-like macro" and a "function-like macro" section from [cpp.replace.general] """

jensmaurer commented 2 years ago

It seems a bit of a stretch to apply this editorially.

Also, this touches shared territory with C, so @ThePhD might want to offer an opinion as C project editor.

ThePhD commented 2 years ago

Introducing this as a grammar token is likely to make life a lot easier and I would probably introduce this change in coordination with __VA_OPT__ getting into C23!

tkoeppe commented 2 years ago

@gustedt: I think you had some views on this matter? It might be useful to gather early feedback on this!

AlisdairM commented 3 months ago

Did C23 adopt this reformulation? Is this something we should consider for C++26?

jensmaurer commented 3 months ago

WG14 N3095 has a "pp-balanced-token-sequence" for a pp-parameter-clause.