diff options
author | Carl Worth <cworth@cworth.org> | 2010-05-27 13:29:19 -0700 |
---|---|---|
committer | Carl Worth <cworth@cworth.org> | 2010-05-27 13:29:19 -0700 |
commit | a19297b26e971e5a9dbe00b4254931505da4b5a9 (patch) | |
tree | 81441fe0327023a2af8d679d9e5fe2f0f4c867b0 | |
parent | a65cf7b1d29e98ef3bf31051df8a06cb394d131f (diff) |
Provide support for empty arguments in macro invocations.
For this we always add a new argument to the argument list as soon as
possible, without waiting until we see some argument token. This does
mean we need to take some extra care when comparing the number of
arguments with the number of expected arguments. In addition to
matching numbers, we also support one (empty) argument when zero
arguments are expected.
Add a test case here for this, which does pass.
-rw-r--r-- | glcpp-parse.y | 20 | ||||
-rw-r--r-- | tests/057-empty-arguments.c | 6 |
2 files changed, 17 insertions, 9 deletions
diff --git a/glcpp-parse.y b/glcpp-parse.y index ba79a611f6..3e0a96528b 100644 --- a/glcpp-parse.y +++ b/glcpp-parse.y @@ -1044,7 +1044,8 @@ _arguments_parse (argument_list_t *arguments, token_node_t **node_ret) last = node; node = node->next; - argument = NULL; + argument = _token_list_create (arguments); + _argument_list_append (arguments, argument); for (paren_count = 1; node; last = node, node = node->next) { if (node->token->type == '(') @@ -1064,18 +1065,16 @@ _arguments_parse (argument_list_t *arguments, token_node_t **node_ret) if (node->token->type == ',' && paren_count == 1) { - if (argument) - _token_list_trim_trailing_space (argument); - argument = NULL; + _token_list_trim_trailing_space (argument); + argument = _token_list_create (arguments); + _argument_list_append (arguments, argument); } else { - if (argument == NULL) { + if (argument->head == NULL) { /* Don't treat initial whitespace as * part of the arguement. */ if (node->token->type == SPACE) continue; - argument = _token_list_create (arguments); - _argument_list_append (arguments, argument); } _token_list_append (argument, node->token); } @@ -1132,8 +1131,11 @@ _expand_function_onto (glcpp_parser_t *parser, return FUNCTION_STATUS_SUCCESS; } - if (_argument_list_length (arguments) != - _string_list_length (macro->parameters)) + if (! ((_argument_list_length (arguments) == + _string_list_length (macro->parameters)) || + (_string_list_length (macro->parameters) == 0 && + _argument_list_length (arguments) == 1 && + arguments->head->argument->head == NULL))) { fprintf (stderr, "Error: macro %s invoked with %d arguments (expected %d)\n", diff --git a/tests/057-empty-arguments.c b/tests/057-empty-arguments.c new file mode 100644 index 0000000000..6140232865 --- /dev/null +++ b/tests/057-empty-arguments.c @@ -0,0 +1,6 @@ +#define zero() success +zero() +#define one(x) success +one() +#define two(x,y) success +two(,) |