From patchwork Sun Dec 12 05:39:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1566974 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=MTcUyYYI; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JBYPr1Cm2z9sR4 for ; Sun, 12 Dec 2021 16:40:14 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2E34D3857813 for ; Sun, 12 Dec 2021 05:40:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E34D3857813 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1639287610; bh=5DWYZRx9D20vl9A+zISR3NhgAF78DWqbILz73zzAd3A=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=MTcUyYYIUaA8wHS8bK+7z0J2xy9pjETE8dDQ4JLNyaI7mkTrY3ephDa83m9b6ODtD J+Y/+TfUyoNowDzINgcBPyU0bsB7CZqPFlUH/skatQK4d0/NvrhcBUKfwK1rLNkBxe 7HUL4QFKK00NCaYYGLvqu82Vb+Hru6fkPFdCLhZ4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 8823E3858D39 for ; Sun, 12 Dec 2021 05:39:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8823E3858D39 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-77-fz1dHv0wMLG3TzjcIIpBRw-1; Sun, 12 Dec 2021 00:39:47 -0500 X-MC-Unique: fz1dHv0wMLG3TzjcIIpBRw-1 Received: by mail-qv1-f72.google.com with SMTP id kd7-20020a056214400700b003b54713452cso19782996qvb.13 for ; Sat, 11 Dec 2021 21:39:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=5DWYZRx9D20vl9A+zISR3NhgAF78DWqbILz73zzAd3A=; b=X0BODzRBEScohIn+64lULUe8UQ8B40SZ7GIenj/w69AEHoFkmzpvGkPSQ2Tl4wrFrs PskfyLp/L1ygad4YRuIIlGApOaC8vB+68Ybg2a74kXS6jEcazrjiSbDzxuC6+TKRL4CO NZbGdFADGpamiLVIBEt4Ciqp+DWIVItReJKwrixH1+iVf3ahB9czVcoNvutSbs9Iq+5N H1g/+hFfjwhKioRXAZNAN5bdKTMl0lapUU5wHyy3549lNBvQNdqjkLN8T/3zKakQpXiT pbSC2XeoLs7wr+WAoOoZMx2eQK4z2LPqcfbVBRauKTkFJii+EiciYs617WdiiIttQqMy JgKw== X-Gm-Message-State: AOAM530ul0sMHh/JG2YeXgZlilAYGOxLyq7ueENuev7vLghor2XPYkrd 5pYRI5YE6jGbxVd2yBFNg/snV7JcrORpDpTn38zxv6QI69f7WMVW5sbQo5GE9/BTb3jYGCMp4o+ TEtT9uXFzQ8TMd9y97Y/233ZCBDbJkGjVsJ8pJqkl+MpjMEJagP+ACPOcRkCORhIzYw== X-Received: by 2002:a05:620a:4514:: with SMTP id t20mr28094210qkp.171.1639287586081; Sat, 11 Dec 2021 21:39:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJzZvGv+NvYmVNMonAcUqa3XWAounjb62sv5I0/2azN2Soa+4IMDeC1FkChJdm+UM06pA/MvIw== X-Received: by 2002:a05:620a:4514:: with SMTP id t20mr28094200qkp.171.1639287585590; Sat, 11 Dec 2021 21:39:45 -0800 (PST) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id 14sm6138750qtx.84.2021.12.11.21.39.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Dec 2021 21:39:44 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH RFC] c++: add color to function decl printing Date: Sun, 12 Dec 2021 00:39:42 -0500 Message-Id: <20211212053942.240160-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Cc: jwakely@redhat.com Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" In reading C++ diagnostics, it's often hard to find the name of the function in the middle of the template header, return type, parameters, and template arguments. So let's colorize it, and maybe the template argument bindings while we're at it. I've somewhat arbitrarily chosen bold green for the function name, and non-bold magenta for the template arguments. I'm not at all attached to these choices. A side-effect is that when this happens in a quote (i.e. %qD), the rest of the quote after the function name is no longer bold. I think that's acceptable; returning to the bold would require maintaining a colorize stack instead of the on/off controls we have now. Any thoughts? gcc/cp/ChangeLog: * error.c (decl_to_string): Add show_color parameter. (cp_printer): Pass it. (dump_function_name): Use "fnname" color. (dump_template_bindings): Use "targs" color. (struct colorize_guard): New. (reinit_cxx_pp): Clear pp_show_color. (cp_print_error_function): Use %qD. (function_category): Use %qD. gcc/ChangeLog: * diagnostic-color.c: Add fnname and targs color entries. * doc/invoke.texi: Document them. --- gcc/doc/invoke.texi | 8 ++++++ gcc/cp/error.c | 64 ++++++++++++++++++++++++++++++------------ gcc/diagnostic-color.c | 2 ++ 3 files changed, 56 insertions(+), 18 deletions(-) base-commit: 2e8067041d1d69da02bd7578f58abc11eb35a04b diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 9b4371b9213..cdfddd75343 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -4803,6 +4803,14 @@ SGR substring for location information, @samp{file:line} or @vindex quote GCC_COLORS @r{capability} SGR substring for information printed within quotes. +@item fnname= +@vindex fnname GCC_COLORS @r{capability} +SGR substring for names of C++ functions. + +@item targs= +@vindex targs GCC_COLORS @r{capability} +SGR substring for C++ function template parameter bindings. + @item fixit-insert= @vindex fixit-insert GCC_COLORS @r{capability} SGR substring for fix-it hints suggesting text to diff --git a/gcc/cp/error.c b/gcc/cp/error.c index daea3b39a15..e03079e3b8f 100644 --- a/gcc/cp/error.c +++ b/gcc/cp/error.c @@ -1,4 +1,4 @@ -/* Call-backs for C++ error reporting. +/* Call-backs for -*- C++ -*- error reporting. This code is non-reentrant. Copyright (C) 1993-2021 Free Software Foundation, Inc. This file is part of GCC. @@ -25,6 +25,7 @@ along with GCC; see the file COPYING3. If not see #include "cp-tree.h" #include "stringpool.h" #include "tree-diagnostic.h" +#include "diagnostic-color.h" #include "langhooks-def.h" #include "intl.h" #include "cxx-pretty-print.h" @@ -56,7 +57,7 @@ static cxx_pretty_printer * const cxx_pp = &actual_pretty_printer; static const char *args_to_string (tree, int); static const char *code_to_string (enum tree_code); static const char *cv_to_string (tree, int); -static const char *decl_to_string (tree, int); +static const char *decl_to_string (tree, int, bool); static const char *fndecl_to_string (tree, int); static const char *op_to_string (bool, enum tree_code); static const char *parm_to_string (int); @@ -390,6 +391,7 @@ dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args, else { pp_cxx_whitespace (pp); + pp_string (pp, colorize_start (pp_show_color (pp), "targs")); pp_cxx_left_bracket (pp); pp->translate_string ("with"); pp_cxx_whitespace (pp); @@ -400,7 +402,10 @@ dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args, ~prepost_semicolon () { if (need_semicolon) - pp_cxx_right_bracket (pp); + { + pp_cxx_right_bracket (pp); + pp_string (pp, colorize_stop (pp_show_color (pp))); + } } } semicolon_or_introducer = {pp, false}; @@ -1158,6 +1163,22 @@ dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags) dump_type_suffix (pp, type, flags); } +struct colorize_guard +{ + bool colorize; + cxx_pretty_printer *pp; + + colorize_guard (bool _colorize, cxx_pretty_printer *pp, const char *name) + : colorize (_colorize && pp_show_color (pp)), pp (pp) + { + pp_string (pp, colorize_start (colorize, name)); + } + ~colorize_guard () + { + pp_string (pp, colorize_stop (colorize)); + } +}; + /* Print an IDENTIFIER_NODE that is the name of a declaration. */ static void @@ -1928,6 +1949,11 @@ dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags) static void dump_function_name (cxx_pretty_printer *pp, tree t, int flags) { + bool colorize = flags & (TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE + | TFF_TEMPLATE_HEADER); + + colorize_guard g (colorize, pp, "fnname"); + tree name = DECL_NAME (t); /* We can get here with a decl that was synthesized by language- @@ -3062,6 +3088,7 @@ reinit_cxx_pp (void) cxx_pp->padding = pp_none; pp_indentation (cxx_pp) = 0; pp_needs_newline (cxx_pp) = false; + pp_show_color (cxx_pp) = false; cxx_pp->enclosing_scope = current_function_decl; } @@ -3208,7 +3235,7 @@ location_of (tree t) function. */ static const char * -decl_to_string (tree decl, int verbose) +decl_to_string (tree decl, int verbose, bool show_color) { int flags = 0; @@ -3222,6 +3249,7 @@ decl_to_string (tree decl, int verbose) flags |= TFF_TEMPLATE_HEADER; reinit_cxx_pp (); + pp_show_color (cxx_pp) = show_color; dump_decl (cxx_pp, decl, flags); return pp_ggc_formatted_text (cxx_pp); } @@ -3517,7 +3545,7 @@ cp_print_error_function (diagnostic_context *context, fndecl = current_function_decl; pp_printf (context->printer, function_category (fndecl), - cxx_printable_name_translate (fndecl, 2)); + fndecl); while (abstract_origin) { @@ -3561,19 +3589,19 @@ cp_print_error_function (diagnostic_context *context, { if (context->show_column && s.column != 0) pp_printf (context->printer, - _(" inlined from %qs at %r%s:%d:%d%R"), - cxx_printable_name_translate (fndecl, 2), + _(" inlined from %qD at %r%s:%d:%d%R"), + fndecl, "locus", s.file, s.line, s.column); else pp_printf (context->printer, - _(" inlined from %qs at %r%s:%d%R"), - cxx_printable_name_translate (fndecl, 2), + _(" inlined from %qD at %r%s:%d%R"), + fndecl, "locus", s.file, s.line); } else - pp_printf (context->printer, _(" inlined from %qs"), - cxx_printable_name_translate (fndecl, 2)); + pp_printf (context->printer, _(" inlined from %qD"), + fndecl); } } pp_character (context->printer, ':'); @@ -3598,20 +3626,20 @@ function_category (tree fn) && DECL_FUNCTION_MEMBER_P (fn)) { if (DECL_STATIC_FUNCTION_P (fn)) - return _("In static member function %qs"); + return _("In static member function %qD"); else if (DECL_COPY_CONSTRUCTOR_P (fn)) - return _("In copy constructor %qs"); + return _("In copy constructor %qD"); else if (DECL_CONSTRUCTOR_P (fn)) - return _("In constructor %qs"); + return _("In constructor %qD"); else if (DECL_DESTRUCTOR_P (fn)) - return _("In destructor %qs"); + return _("In destructor %qD"); else if (LAMBDA_FUNCTION_P (fn)) return _("In lambda function"); else - return _("In member function %qs"); + return _("In member function %qD"); } else - return _("In function %qs"); + return _("In function %qD"); } /* Disable warnings about missing quoting in GCC diagnostics for @@ -4393,7 +4421,7 @@ cp_printer (pretty_printer *pp, text_info *text, const char *spec, break; } } - result = decl_to_string (temp, verbose); + result = decl_to_string (temp, verbose, pp_show_color (pp)); } break; case 'E': result = expr_to_string (next_tree); break; diff --git a/gcc/diagnostic-color.c b/gcc/diagnostic-color.c index 1fc5a9079c7..c1406b45d8b 100644 --- a/gcc/diagnostic-color.c +++ b/gcc/diagnostic-color.c @@ -91,6 +91,8 @@ static struct color_cap color_dict[] = { "locus", SGR_SEQ (COLOR_BOLD), 5, false }, { "quote", SGR_SEQ (COLOR_BOLD), 5, false }, { "path", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false }, + { "fnname", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 6, false }, + { "targs", SGR_SEQ (COLOR_FG_MAGENTA), 5, false }, { "fixit-insert", SGR_SEQ (COLOR_FG_GREEN), 12, false }, { "fixit-delete", SGR_SEQ (COLOR_FG_RED), 12, false }, { "diff-filename", SGR_SEQ (COLOR_BOLD), 13, false },