From patchwork Sat Sep 23 03:03:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 23106 Received: (qmail 94907 invoked by alias); 23 Sep 2017 03:03:56 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 94898 invoked by uid 89); 23 Sep 2017 03:03:55 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gproxy7-pub.mail.unifiedlayer.com Received: from gproxy7-pub.mail.unifiedlayer.com (HELO gproxy7-pub.mail.unifiedlayer.com) (70.40.196.235) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 23 Sep 2017 03:03:53 +0000 Received: from CMOut01 (unknown [10.0.90.82]) by gproxy7.mail.unifiedlayer.com (Postfix) with ESMTP id 8192E215C47 for ; Fri, 22 Sep 2017 21:03:51 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by CMOut01 with id Cr3o1w0062f2jeq01r3riF; Fri, 22 Sep 2017 21:03:51 -0600 X-Authority-Analysis: v=2.2 cv=K4VSJ2eI c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=2JCJgTwv5E4A:10 a=20KFwNOVAAAA:8 a=zstS-IiYAAAA:8 a=f57B6TknA5droBj3uYgA:9 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 75-166-76-94.hlrn.qwest.net ([75.166.76.94]:38446 helo=bapiya) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dvajM-001pGJ-0O; Fri, 22 Sep 2017 21:03:48 -0600 From: Tom Tromey To: Pedro Alves Cc: Tom Tromey , gdb-patches@sourceware.org Subject: Re: [RFA] Add support for __VA_OPT__ References: <20170918024223.5607-1-tom@tromey.com> <31c997d6-8866-2080-5fd2-ebed0871c98f@redhat.com> <874lrwsonf.fsf@tromey.com> <63ab0211-8160-7ef2-5c9d-cfe5e85f9258@redhat.com> Date: Fri, 22 Sep 2017 21:03:47 -0600 In-Reply-To: <63ab0211-8160-7ef2-5c9d-cfe5e85f9258@redhat.com> (Pedro Alves's message of "Thu, 21 Sep 2017 10:01:16 +0100") Message-ID: <87fubei0a4.fsf@tromey.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) MIME-Version: 1.0 X-BWhitelist: no X-Exim-ID: 1dvajM-001pGJ-0O X-Source-Sender: 75-166-76-94.hlrn.qwest.net (bapiya) [75.166.76.94]:38446 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes >>>>> "Pedro" == Pedro Alves writes: >> But I wonder if gdb should just error() on the invalid ones. >> My first thought was no, why make life harder -- but at the same time, >> the invalid cases really aren't that useful either. Pedro> Yeah, error might be better - e.g., for someone trying Pedro> to write a "macro define" interactively (without Pedro> going via the compiler first), and puzzling about why it Pedro> doesn't exactly work [due to some typo]. But we can Pedro> decide to do that incrementally. Fine with me to push Pedro> as is if you'd like. I've switched it; and good thing, too, because this caught a bug in the previous patch that could cause an infinite loop. Tom commit dbaa7cadd373972d362c10cedc8f34f5cf1d6e2a Author: Tom Tromey Date: Sun Sep 17 20:36:41 2017 -0600 Add support for __VA_OPT__ C++2a adds a "__VA_OPT__" feature that can be used to control the pesky "," emission when the final (variable) argument of a variadic macro is empty. This patch implements this feature for gdb. (A patch to implement it for gcc is pending.) ChangeLog 2017-09-17 Tom Tromey * macroexp.c (get_next_token_for_substitution): New function. (substitute_args): Call it. Check for __VA_OPT__. testsuite/ChangeLog 2017-09-17 Tom Tromey * gdb.base/macscp.exp: Add __VA_OPT__ tests. diff --git a/gdb/ChangeLog b/gdb/ChangeLog index aa50dc9..d660fb0 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2017-09-17 Tom Tromey + + * macroexp.c (get_next_token_for_substitution): New function. + (substitute_args): Call it. Check for __VA_OPT__. + 2017-09-22 Tom Tromey * utils.c (class scoped_input_handler) : Change diff --git a/gdb/macroexp.c b/gdb/macroexp.c index e7a0dad..7d5b876 100644 --- a/gdb/macroexp.c +++ b/gdb/macroexp.c @@ -946,6 +946,30 @@ find_parameter (const struct macro_buffer *tok, return -1; } +/* Helper function for substitute_args that gets the next token and + updates the passed-in state variables. */ + +static void +get_next_token_for_substitution (struct macro_buffer *replacement_list, + struct macro_buffer *token, + char **start, + struct macro_buffer *lookahead, + char **lookahead_start, + int *lookahead_valid, + bool *keep_going) +{ + if (!*lookahead_valid) + *keep_going = false; + else + { + *keep_going = true; + *token = *lookahead; + *start = *lookahead_start; + *lookahead_start = replacement_list->text; + *lookahead_valid = get_token (lookahead, replacement_list); + } +} + /* Given the macro definition DEF, being invoked with the actual arguments given by ARGC and ARGV, substitute the arguments into the replacement list, and store the result in DEST. @@ -996,8 +1020,67 @@ substitute_args (struct macro_buffer *dest, lookahead_rl_start = replacement_list.text; lookahead_valid = get_token (&lookahead, &replacement_list); - for (;;) + /* __VA_OPT__ state variable. The states are: + 0 - nothing happening + 1 - saw __VA_OPT__ + >= 2 in __VA_OPT__, the value encodes the parenthesis depth. */ + unsigned vaopt_state = 0; + + /* Whether the loop should keep going. */ + bool keep_going = true; + + for (; + keep_going; + get_next_token_for_substitution (&replacement_list, + &tok, + &original_rl_start, + &lookahead, + &lookahead_rl_start, + &lookahead_valid, + &keep_going)) { + bool token_is_vaopt = (tok.len == 10 + && strncmp (tok.text, "__VA_OPT__", 10) == 0); + + if (vaopt_state > 0) + { + if (token_is_vaopt) + error (_("__VA_OPT__ cannot appear inside __VA_OPT__")); + else if (tok.len == 1 && tok.text[0] == '(') + { + ++vaopt_state; + /* We just entered __VA_OPT__, so don't emit this + token. */ + continue; + } + else if (vaopt_state == 1) + error (_("__VA_OPT__ must be followed by an open parenthesis")); + else if (tok.len == 1 && tok.text[0] == ')') + { + --vaopt_state; + if (vaopt_state == 1) + { + /* Done with __VA_OPT__. */ + vaopt_state = 0; + /* Don't emit. */ + continue; + } + } + + /* If __VA_ARGS__ is empty, then drop the contents of + __VA_OPT__. */ + if (argv[argc - 1].len == 0) + continue; + } + else if (token_is_vaopt) + { + if (!is_varargs) + error (_("__VA_OPT__ is only valid in a variadic macro")); + vaopt_state = 1; + /* Don't emit this token. */ + continue; + } + /* Just for aesthetics. If we skipped some whitespace, copy that to DEST. */ if (tok.text > original_rl_start) @@ -1157,16 +1240,10 @@ substitute_args (struct macro_buffer *dest, if (! substituted) append_tokens_without_splicing (dest, &tok); } - - if (! lookahead_valid) - break; - - tok = lookahead; - original_rl_start = lookahead_rl_start; - - lookahead_rl_start = replacement_list.text; - lookahead_valid = get_token (&lookahead, &replacement_list); } + + if (vaopt_state > 0) + error (_("Unterminated __VA_OPT__")); } diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 6584a23..ddc17d2 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2017-09-17 Tom Tromey + + * gdb.base/macscp.exp: Add __VA_OPT__ tests. + 2017-09-21 Kevin Buettner * gdb.python/py-thrhandle.c, gdb.python/py-thrhandle.exp: New diff --git a/gdb/testsuite/gdb.base/macscp.exp b/gdb/testsuite/gdb.base/macscp.exp index 54b5ab2..c5cd899 100644 --- a/gdb/testsuite/gdb.base/macscp.exp +++ b/gdb/testsuite/gdb.base/macscp.exp @@ -620,6 +620,18 @@ gdb_test_no_output "macro define va_gnu(args...) varfunc (fixedarg, args)" \ gdb_test_no_output "macro define va2_gnu(args...) varfunc (fixedarg, ## args)" \ "define fourth varargs helper" +gdb_test_no_output \ + "macro define va3_cxx2a(x, ...) varfunc (x __VA_OPT__(,) __VA_ARGS__)" \ + "define fifth varargs helper" + +gdb_test_no_output \ + "macro define va4_cxx2a(x, ...) varfunc (x __VA_OPT__(, __VA_ARGS__))" \ + "define sixth varargs helper" + +gdb_test_no_output \ + "macro define va5_cxx2a(x, ...) varfunc (x __VA_OPT__(,) __VA_OPT__(__VA_ARGS__))" \ + "define seventh varargs helper" + gdb_test "macro expand va_c99(one, two, three)" \ "expands to: *varfunc \\(fixedarg, *one, two, three\\)" \ "c99 varargs expansion" @@ -644,6 +656,58 @@ gdb_test "macro expand va2_gnu()" \ "expands to: *varfunc \\(fixedarg\\)" \ "gnu varargs expansion special splicing without an argument" +gdb_test "macro expand va3_cxx2a(23)" \ + "expands to: *varfunc \\(23 \\)" \ + "C++2a __VA_OPT__ handling without variable argument" + +gdb_test "macro expand va3_cxx2a(23, 24, 25)" \ + "expands to: *varfunc \\(23, 24, 25\\)" \ + "C++2a __VA_OPT__ handling with variable argument" + +gdb_test "macro expand va4_cxx2a(23, 24, 25)" \ + "expands to: *varfunc \\(23, 24, 25\\)" \ + "C++2a __VA_OPT__ conditional __VA_ARGS__ handling with variable argument" + +gdb_test "macro expand va4_cxx2a(23)" \ + "expands to: *varfunc \\(23\\)" \ + "C++2a __VA_OPT__ conditional __VA_ARGS__ handling without variable argument" + +gdb_test "macro expand va5_cxx2a(23, 24, 25)" \ + "expands to: *varfunc \\(23,24, 25\\)" \ + "C++2a double __VA_OPT__ conditional __VA_ARGS__ handling with variable argument" + +gdb_test "macro expand va5_cxx2a(23)" \ + "expands to: *varfunc \\(23\\)" \ + "C++2a double __VA_OPT__ conditional __VA_ARGS__ handling without variable argument" + +gdb_test_no_output \ + "macro define badopt1(x, ...) __VA_OPT__) x" \ + "define first invalid varargs helper" +gdb_test "macro expand badopt1(5)" \ + "__VA_OPT__ must be followed by an open parenthesis" \ + "__VA_OPT__ without open paren" + +gdb_test_no_output \ + "macro define badopt2(x, ...) __VA_OPT__(__VA_OPT__(,)) x" \ + "define second invalid varargs helper" +gdb_test "macro expand badopt2(5)" \ + "__VA_OPT__ cannot appear inside __VA_OPT__" \ + "__VA_OPT__ inside __VA_OPT__" + +gdb_test_no_output \ + "macro define badopt3(x) __VA_OPT__" \ + "define third invalid varargs helper" +gdb_test "macro expand badopt3(5)" \ + "__VA_OPT__ is only valid in a variadic macro" \ + "__VA_OPT__ not in variadic macro" + +gdb_test_no_output \ + "macro define badopt4(x, ...) __VA_OPT__(x" \ + "define fourth invalid varargs helper" +gdb_test "macro expand badopt4(5)" \ + "Unterminated __VA_OPT__" \ + "__VA_OPT__ without closing paren" + # Stringification tests. gdb_test_no_output "macro define str(x) #x" \