summaryrefslogtreecommitdiffstats
path: root/examples/hello-world/main.cpp
diff options
context:
space:
mode:
authorTheresa Foley <tfoleyNV@users.noreply.github.com>2021-06-16 14:35:25 -0700
committerGitHub <noreply@github.com>2021-06-16 14:35:25 -0700
commit89051251016be7d3798c0b9586c6db7b4ed5f21d (patch)
tree684d40ec065e856adfbd92df5c721e3fa1c44527 /examples/hello-world/main.cpp
parent45f737dfde81d31b44afb6e5d7e89de88ee51160 (diff)
Initial support for variadic macros (#1887)
This change adds support for variadic macros in the C-style preprocessor, e.g.: #define DEBUG(MSG, ...) print(__FILE__, __LINE__, MSG, __VA_ARGS__) Similar to the gcc preprocessor, this feature supports both named variadic macro parameters and unnamed ones (which then default to `__VA_ARGS__`. The implementation work is mostly straightforward, although there are a few subtle design choices worth mentioning: * A variadic macro is represented by it having a variadic *parameter* that is part of the ordinary parameter list. * Argument parsing does *not* detect whether the macro being invoked is variadic and collect/combine arguments to form a single argument value for the variadic parameter. This is motivated by the need for some extensions to differentiate a variadic parameter receiving a single empty argument vs. zero arguments. * Because any reference to the variadic parameter needs to expand to the comma-separated arguments that match it, the logic for turning a macro parameter reference into a list of tokens has been factored out into a subroutine that handles the details. * The choice in the earlier refactor to have a macro invocation collect all the argument tokens (including the intervening commas) into a single token list seems to pay off here, because it means that the tokens in the expansion of a variadic parameter reference were already stored contiguously. * The special-case logic for handling an empty argument list had to be tweaked again to ensure that an empty argument list is treated as having zero arguments for the variadic parameter. Note that historically C did not define the behavior of this case, and always required at least one argument for any variadic macro parameter. * The logic for checking whether the number of arguments to a macro invocation is valid needed to handle variadic and non-variadic macros as distinct cases. There really isn't much overlap in how the checks need to work, even if we tried to change the underling representation. The main missing feature here is any way to discard a comma in a macro body that appears before a variadic parameter reference, e.g.: #define DEBUG(...) print("debug:", __VA_ARGS__) In this case, an empty invocation list `DEBUG()` will expand to `print("debug:",)` - a call with a trailing comma in the argument list. If users end up needing a way to discard commas in cases like this, we have many options we can consider. This change does not implement any of them to keep the initial work as minimal as possible.
Diffstat (limited to 'examples/hello-world/main.cpp')
0 files changed, 0 insertions, 0 deletions