From patchwork Mon Sep 18 02:42:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 22916 Received: (qmail 59492 invoked by alias); 18 Sep 2017 02:42:37 -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 59477 invoked by uid 89); 18 Sep 2017 02:42:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.2 spammy=pesky, states X-HELO: gproxy8-pub.mail.unifiedlayer.com Received: from gproxy8-pub.mail.unifiedlayer.com (HELO gproxy8-pub.mail.unifiedlayer.com) (67.222.33.93) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 18 Sep 2017 02:42:33 +0000 Received: from cmgw3 (unknown [10.0.90.84]) by gproxy8.mail.unifiedlayer.com (Postfix) with ESMTP id 593711AB204 for ; Sun, 17 Sep 2017 20:42:32 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by cmgw3 with id AqiV1w0052f2jeq01qiYby; Sun, 17 Sep 2017 20:42:32 -0600 X-Authority-Analysis: v=2.2 cv=K/VSJ2eI c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=2JCJgTwv5E4A:10 a=zstS-IiYAAAA:8 a=VvkkiGHcw8dSB3KbOSUA:9 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 75-166-76-94.hlrn.qwest.net ([75.166.76.94]:47622 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dtm0z-002Z7I-5C; Sun, 17 Sep 2017 20:42:29 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA] Add support for __VA_OPT__ Date: Sun, 17 Sep 2017 20:42:23 -0600 Message-Id: <20170918024223.5607-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1dtm0z-002Z7I-5C X-Source-Sender: 75-166-76-94.hlrn.qwest.net (bapiya.Home) [75.166.76.94]:47622 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes 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.) gdb/ChangeLog 2017-09-17 Tom Tromey * macroexp.c (get_next_token_for_substitution): New function. (substitute_args): Call it. Check for __VA_OPT__. gdb/testsuite/ChangeLog 2017-09-17 Tom Tromey * gdb.base/macscp.exp: Add __VA_OPT__ tests. --- gdb/ChangeLog | 5 +++ gdb/macroexp.c | 73 +++++++++++++++++++++++++++++++++++---- gdb/testsuite/ChangeLog | 4 +++ gdb/testsuite/gdb.base/macscp.exp | 12 +++++++ 4 files changed, 87 insertions(+), 7 deletions(-) diff --git a/gdb/macroexp.c b/gdb/macroexp.c index e7a0dad..369fc78 100644 --- a/gdb/macroexp.c +++ b/gdb/macroexp.c @@ -946,6 +946,22 @@ 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 int +get_next_token_for_substitution (struct macro_buffer *replacement_list, + struct macro_buffer *token, + char **start, + struct macro_buffer *lookahead, + char **lookahead_start) +{ + *token = *lookahead; + *start = *lookahead_start; + *lookahead_start = replacement_list->text; + return 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 +1012,57 @@ 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; + + for (;; + lookahead_valid = get_next_token_for_substitution (&replacement_list, + &tok, + &original_rl_start, + &lookahead, + &lookahead_rl_start)) { + if (vaopt_state > 0) + { + if (tok.len == 1) + { + if (tok.text[0] == '(') + { + ++vaopt_state; + /* We just entered __VA_OPT__, so don't emit this + token. */ + continue; + } + else if (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 (is_varargs + && tok.len == 10 + && strncmp (tok.text, "__VA_OPT__", 10) == 0) + { + 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) @@ -1160,12 +1225,6 @@ substitute_args (struct macro_buffer *dest, 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); } } diff --git a/gdb/testsuite/gdb.base/macscp.exp b/gdb/testsuite/gdb.base/macscp.exp index 54b5ab2..a08ec46 100644 --- a/gdb/testsuite/gdb.base/macscp.exp +++ b/gdb/testsuite/gdb.base/macscp.exp @@ -620,6 +620,10 @@ 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 "macro expand va_c99(one, two, three)" \ "expands to: *varfunc \\(fixedarg, *one, two, three\\)" \ "c99 varargs expansion" @@ -644,6 +648,14 @@ 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" + # Stringification tests. gdb_test_no_output "macro define str(x) #x" \