From patchwork Wed Oct 26 16:37:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 79455 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp158688qge; Wed, 26 Oct 2016 09:37:53 -0700 (PDT) X-Received: by 10.99.97.15 with SMTP id v15mr4829523pgb.10.1477499873636; Wed, 26 Oct 2016 09:37:53 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id t184si3598567pgd.119.2016.10.26.09.37.53 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Oct 2016 09:37:53 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-439649-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-439649-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-439649-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:cc:message-id:date:mime-version:content-type; q= dns; s=default; b=PMZPJN4ewHS4qHJ4zkUgkR/ghRYUfTDX2bVW4lyix2xYEB w/EN1kSVEEexJRlWws+9AQOlRX+d5hJdehN6BmkfqUSOXxbVYK/3wv1wBQfOb5K4 hjzqP4KHT1Lm1zmrc36lqSJ5MA/fqIggkn3pTirolLdqsSg/oQ7ajHotf0I7I= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:cc:message-id:date:mime-version:content-type; s= default; bh=KFL8e/zCUdW4bUwTZG7Yz+gM0fE=; b=w9MOeDluhwmAQBSsap27 g4NGZuWerw4EQvJ6D1NRuaUIYQlmHrt9EfOnG+/RL8VxO4wTFTvT9Jfk6eeGvocg ft4frqDuaLKCqVE3qxSQECPAtKUnc7Jx6cP5MDah6PbwB58SVwkesKBT9foFQSDd Ook78ZXnIi0zGEfPaZzvlHI= Received: (qmail 10409 invoked by alias); 26 Oct 2016 16:37:34 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 10331 invoked by uid 89); 26 Oct 2016 16:37:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=pound, bc, qwl X-HELO: mail-qk0-f178.google.com Received: from mail-qk0-f178.google.com (HELO mail-qk0-f178.google.com) (209.85.220.178) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 26 Oct 2016 16:37:31 +0000 Received: by mail-qk0-f178.google.com with SMTP id x11so10289471qka.1 for ; Wed, 26 Oct 2016 09:37:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:to:from:subject:cc:message-id:date:user-agent :mime-version; bh=CE4RyWkNkfX0mKUudekQRRhbPlFNUcHrEihk8V4sb4E=; b=XSrOQ9bVj4X1opabA1hUz+pvgGEjnTTh+zHlUKM+s3wLcG2U8fMZh5GCjmSi59Nymv jIPseFjgQXJuQ8LluUnCadj9rI1a8rLU1ZVx6lXYVe7BfLIqrRU4xQNr+qsgJ+ia/ANG CdHvFqPAdI8S0rpdGs3yRc78S8C1DbZHlG/Q4hee899sISyqQuvzcEEoE5NSCJJXnX62 HHOmV4ORP9htXixSfxqbK/vrRXW2EQAzO6ECKoNh4nGwEhPN7e0074RP7u5vKb0hIWYk QrXUsLazVYslwafwIkZpPDn7M/u5sd+hsdKwQ0psVdY+AbFFO63Y3vHz3LzcSPSsOCp5 v9/Q== X-Gm-Message-State: ABUngvcpguTpZ1pAnqDBNhERM0tTTAmO+V+4aLX7xiJe4wC5b5FUyMVd6tfBonmrFL0mGQ== X-Received: by 10.55.11.136 with SMTP id 130mr2491620qkl.291.1477499849806; Wed, 26 Oct 2016 09:37:29 -0700 (PDT) Received: from [192.168.0.26] (71-212-250-41.hlrn.qwest.net. [71.212.250.41]) by smtp.gmail.com with ESMTPSA id g143sm1386100qke.18.2016.10.26.09.37.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Oct 2016 09:37:29 -0700 (PDT) To: Gcc Patch List From: Martin Sebor Subject: [RFC PATCH] avoid printing type suffix with %E Cc: David Malcolm Message-ID: <79de46b9-9770-02dd-6d98-9c72b7e6c59d@gmail.com> Date: Wed, 26 Oct 2016 10:37:27 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 X-IsSubscribed: yes When formatting an integer constant using the %E directive GCC includes a suffix that indicates its type. This can perhaps be useful in some situations but in my experience it's distracting and gets in the way when writing tests. Here's an example: $ cat b.c && gcc b.c constexpr __SIZE_TYPE__ x = 2; enum E: bool { e = x }; b.c:3:20: error: enumerator value 2ul is outside the range of underlying type ‘bool’ enum E: bool { e = x }; ^ Notice the "2ul" in the error message. As far as I can tell, Clang avoids printing the suffix and I think it would be nice if the GCC pretty printer made it possible to avoid it as well. The attached patch implements one such approach by having the pretty printer recognize the space format flag to suppress the type suffix, so "%E" still prints the suffix but "% E" does not. I did this to preserve the existing output but I think it would be nicer to avoid printing the suffix with %E and treat (for instance) the pound sign as a request to add the suffix. I have tested the attached patch but not the alternative. Does anyone have any comments/suggestions for which of the two approaches would be preferable (or what I may have missed here)? I CC David as the diagnostic maintainer. Thanks Martin gcc/c/ChangeLog: 2016-10-26 Martin Sebor * c-objc-common.c (c_tree_printer::get_flag): Declare new function (c_tree_printer::set_flag): Same. gcc/c-family/ChangeLog: 2016-10-26 Martin Sebor * c-pretty-print.c (c_tree_printer::get_flag): Define. (c_tree_printer::set_flag): Define. (pp_c_integer_constant): Don't print type suffix when space is set in flags. * c-pretty-print.h (pp_c_flag_space): Add enumerator. gcc/cp/ChangeLog: 2016-10-26 Martin Sebor * error.c (cp_printer): Set space in flags. gcc/ChangeLog: 2016-10-26 Martin Sebor * pretty-print.c (pp_format): Recognize space. * pretty-print.h (pretty_printer::get_flag): New function. (pretty_printer::set_flag): Same. diff --git a/gcc/c-family/c-pretty-print.c b/gcc/c-family/c-pretty-print.c index 90428ca..7d9375d 100644 --- a/gcc/c-family/c-pretty-print.c +++ b/gcc/c-family/c-pretty-print.c @@ -292,6 +292,26 @@ pp_c_pointer (c_pretty_printer *pp, tree t) } } +bool +c_pretty_printer::get_flag (char flag) +{ + return ' ' == flag && (flags & pp_c_flag_space); +} + +bool +c_pretty_printer::set_flag (char flag, bool value) +{ + gcc_assert (flag == ' '); + + bool previous = c_pretty_printer::get_flag (flag); + if (value) + flag |= pp_c_flag_space; + else + flag &= ~pp_c_flag_space; + + return previous; +} + /* simple-type-specifier: type-specifier @@ -926,24 +946,34 @@ pp_c_integer_constant (c_pretty_printer *pp, tree i) print_hex (wi, pp_buffer (pp)->digit_buffer); pp_string (pp, pp_buffer (pp)->digit_buffer); } - if (TYPE_UNSIGNED (type)) - pp_character (pp, 'u'); - if (type == long_integer_type_node || type == long_unsigned_type_node) - pp_character (pp, 'l'); - else if (type == long_long_integer_type_node - || type == long_long_unsigned_type_node) - pp_string (pp, "ll"); - else for (idx = 0; idx < NUM_INT_N_ENTS; idx ++) - if (int_n_enabled_p[idx]) - { - char buf[2+20]; - if (type == int_n_trees[idx].signed_type - || type == int_n_trees[idx].unsigned_type) - { - sprintf (buf, "I%d", int_n_data[idx].bitsize); - pp_string (pp, buf); - } - } + + if (pp->get_flag (' ')) + { + if (TYPE_UNSIGNED (type)) + pp_character (pp, 'u'); + + if (type == long_integer_type_node || type == long_unsigned_type_node) + pp_character (pp, 'l'); + else if (type == long_long_integer_type_node + || type == long_long_unsigned_type_node) + pp_string (pp, "ll"); + else + { + for (idx = 0; idx < NUM_INT_N_ENTS; idx ++) + { + if (int_n_enabled_p[idx]) + { + char buf[2+20]; + if (type == int_n_trees[idx].signed_type + || type == int_n_trees[idx].unsigned_type) + { + sprintf (buf, "I%d", int_n_data[idx].bitsize); + pp_string (pp, buf); + } + } + } + } + } } /* Print out a CHARACTER literal. */ diff --git a/gcc/c-family/c-pretty-print.h b/gcc/c-family/c-pretty-print.h index 253f192..10ad5d9 100644 --- a/gcc/c-family/c-pretty-print.h +++ b/gcc/c-family/c-pretty-print.h @@ -30,7 +30,8 @@ enum pp_c_pretty_print_flags { pp_c_flag_abstract = 1 << 1, pp_c_flag_gnu_v3 = 1 << 2, - pp_c_flag_last_bit = 3 + pp_c_flag_space = 1 << 3, + pp_c_flag_last_bit = 4 }; @@ -51,6 +52,10 @@ struct c_pretty_printer : pretty_printer { c_pretty_printer (); + /* Get and set the format flag. */ + virtual bool get_flag (char); + virtual bool set_flag (char, bool); + // Format string, possibly translated. void translate_string (const char *); diff --git a/gcc/c/c-objc-common.c b/gcc/c/c-objc-common.c index 20dc024..1bd60cf 100644 --- a/gcc/c/c-objc-common.c +++ b/gcc/c/c-objc-common.c @@ -99,6 +99,12 @@ c_tree_printer (pretty_printer *pp, text_info *text, const char *spec, text->set_location (0, DECL_SOURCE_LOCATION (t), true); } + if (' ' == *spec) + { + pp->set_flag (' ', true); + ++spec; + } + switch (*spec) { case 'D': diff --git a/gcc/cp/error.c b/gcc/cp/error.c index 20b20b4..d24ae26 100644 --- a/gcc/cp/error.c +++ b/gcc/cp/error.c @@ -3539,6 +3539,12 @@ cp_printer (pretty_printer *pp, text_info *text, const char *spec, if (precision != 0 || wide) return false; + if (*spec == ' ') + { + pp->set_flag (' '); + ++spec; + } + switch (*spec) { case 'A': result = args_to_string (next_tree, verbose); break; diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c index a39815e..93b7419 100644 --- a/gcc/pretty-print.c +++ b/gcc/pretty-print.c @@ -293,6 +293,11 @@ pp_indent (pretty_printer *pp) %.*s: a substring the length of which is specified by an argument integer. %Ns: likewise, but length specified as constant in the format string. + + Flag 'w': wide integer (HOST_WIDE_INT). + Flag '+': always print a sign for integer and real constants. + Flag ' ': printer-specific. + Flag '#': printer-specific. Flag 'q': quote formatted text (must come immediately after '%'). Arguments can be used sequentially, or through %N$ resp. *N$ @@ -425,12 +430,14 @@ pp_format (pretty_printer *pp, text_info *text) gcc_assert (argno < PP_NL_ARGMAX); gcc_assert (!formatters[argno]); formatters[argno] = &args[chunk]; + + /* Add known format flags. */ do { obstack_1grow (&buffer->chunk_obstack, *p); p++; } - while (strchr ("qwl+#", p[-1])); + while (strchr ("qwl+# ", p[-1])); if (p[-1] == '.') { diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h index f49f35a..9d4386f 100644 --- a/gcc/pretty-print.h +++ b/gcc/pretty-print.h @@ -213,6 +213,13 @@ struct pretty_printer virtual ~pretty_printer (); + /* Get and set the format flag. */ + virtual bool get_flag (char) { return false; } + virtual bool set_flag (char, bool) { return false; } + + /* Not virtual. Provided for convenience. */ + bool set_flag (char flag) { return set_flag (flag, true); } + /* Where we print external representation of ENTITY. */ output_buffer *buffer;