From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 8A49C3858402 for ; Wed, 17 Nov 2021 18:25:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8A49C3858402 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-340-Mfspomm2MiiuDaqBuuMbRg-1; Wed, 17 Nov 2021 13:25:49 -0500 X-MC-Unique: Mfspomm2MiiuDaqBuuMbRg-1 Received: by mail-qk1-f198.google.com with SMTP id az44-20020a05620a172c00b0046a828b4684so1302973qkb.22 for ; Wed, 17 Nov 2021 10:25:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=u2yAquSg+IVjnBCD7KBnD330Kq90tpVqb2RvLEwaq5o=; b=BxDIRphhJ5/eucmegeVB2JQomLOnKVNvY67hCBVwBr9/FQ99B2MminlU4OkVYGPgoM 6qrsFLeNr4d5XzENXSaBEglhQdag1N7MWWuB6GBt7vBktrnq7+UqVXbimdr3B1vzPUx6 aTgt5tN6Aafvsp9lp08E08OAHVrg9Z9dfFd7hNif3P66Gav3bch/Zp+/EswA8bW0At41 6X5lDMzBHAItItq3zztF05ePi/+9eG52+Z+cqvxlRZiyvKBaGvOkhymX5nO50IfPPFtR Rb5Ob81O3H7dpLMxS65gcMETkQG0mt5I5YPzRBKanE/yPm5VwFnwoOurw98Pn+9KBreN /2cw== X-Gm-Message-State: AOAM5310jNKGsJNuxfuQorLY+I1OyY5+sfbQ9KS7oMPGjOrHQjcpJLug /oMEB9ztfzOfFML5R//AgnAoYO4rvrjQHsdV2KYUiKlfabu6iJIPHNlL7lE8eSz9TLApP3G8hg3 urBBzjvGGgvfwoKbWPQ== X-Received: by 2002:a37:e20d:: with SMTP id g13mr14838162qki.121.1637173548221; Wed, 17 Nov 2021 10:25:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3v8uV6XZYSpaM++qA/s9JyUPmsK8pGLxNSr7ks8WlEJiy6r+uZg20DraLt7zPxuq5tYxb9w== X-Received: by 2002:a37:e20d:: with SMTP id g13mr14838109qki.121.1637173547664; Wed, 17 Nov 2021 10:25:47 -0800 (PST) Received: from [192.168.1.149] (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 u9sm332819qta.17.2021.11.17.10.25.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 17 Nov 2021 10:25:46 -0800 (PST) Message-ID: <17241ec0-9e13-e804-ac34-2ca6cdd0dc4b@redhat.com> Date: Wed, 17 Nov 2021 13:25:46 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.3.1 Subject: Re: [RFC] c++: Print function template parms when relevant (was: [PATCH v4] c++: Add gnu::diagnose_as attribute) To: Matthias Kretz , gcc-patches@gcc.gnu.org References: <4361366.VLH7GnMWUR@minbar> <1694479.jsd7nNDzyu@excalibur> <768aba03-0419-6934-96f9-d86cf9ef6547@redhat.com> <1716806.eMUbO1HJkO@excalibur> From: Jason Merrill In-Reply-To: <1716806.eMUbO1HJkO@excalibur> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, 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-List-Received-Date: Wed, 17 Nov 2021 18:25:55 -0000 On 11/17/21 04:04, Matthias Kretz wrote: > On Wednesday, 17 November 2021 07:09:18 CET Jason Merrill wrote: >>> - if (CHECKING_P) >>> - SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a)); >>> + SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, nondefault); >> >> should have been >> >> if (CHECKING_P || nondefault != TREE_VEC_LENGTH (a)) >> SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, nondefault); > > TBH, I don't understand the purpose of CHECKING_P here, or rather it makes me > nervous because AFAIU I'm only testing with CHECKING_P enabled. Why make > behavior dependent on CHECKING_P? I expected CHECKING_P to basically only add > more assertions. The idea when NON_DEFAULT_TEMPLATE_ARGS_COUNT was added years back was to leave the TREE_CHAIN null when !CHECKING_P and treat that as equivalent to TREE_VEC_LENGTH (args). But perhaps you're right that it's not a savings worth the complexity. >>> (copy_template_args): Jason? >> >> Only copy the non-default template args count on TREE_VECs that should >> have it. > > Why not simply set the count on all args? Is it a performance concern? The > INTEGER_CST the TREE_CHAIN has to point to exists anyway, so it's not wasting > any memory, right? In this case the TREE_VEC we're excluding is the one wrapping multiple levels of template args; it doesn't contain args directly, so setting NON_DEFAULT_ARGS_COUNT on it doesn't make sense. >>> + /* Pretty print only template instantiations. Don't pretty print >>> explicit >>> + specializations like 'template <> void fun (int)'. >> >> This seems like a significant change of behavior unrelated to printing >> default template arguments. What's the rationale for handling >> specializations differently from instantiations? > > Right, this is about "The general idea of this change is to print template > parms wherever they would appear in the source code as well". > > Initially, the change to print function template arguments/parameters only if > the args were explicitly specified lead to printing 'void fun (T) [with T = > ...]' or 'template <> void fun (int)'. Both are not telling the full story, > even if the former is how the function would be called. and the latter is how I expect the specialization to be declared, not with the deducible template argument made explicit. > But if the reader > should quickly recognize what code is getting called, it is helpful to see > right away that a function template specialization is called. (It might also > reveal an implementation detail of a library, so it's not 100% obvious how to > choose here.) Also, saying 'T = int' is kind of wrong. Yes, 'int' was deduced. > But there's no T in fun: > > template void fun (T); > template <> void fun (int); There's a T in the template, and as you said above, that's how it's called (and mangled). > __FUNCTION__ was 'fun' all the time, but __PRETTY_FUNCTION__ was 'void > fun(T) [with T = int]'. Isn't that true for instantiations, as well? > It's more consistent that __PRETTY_FUNCTION__ contains __FUNCTION__, IMHO I suppose, but I don't see that as a strong enough motivation to mix this up. > so it would have to be at least 'void fun(T) [with T > = int]'. But that's strange: How it uses T and int for the same type. So I > settled on 'void fun(int)'. > >> I also don't understand the purpose of TFF_AS_PRIMARY. > > dump_function_decl generalizes the TEMPLATE_DECL (if flag_pretty_templates is > true) and, before this change, passes the generalized TEMPLATE_DECL to > dump_type (... DECL_CONTEXT (t) ...) and dump_function_name (... t ...). > That's why the whole template is printed as primary template (i.e. with > template parms instead of template args, as is needed for > flag_pretty_templates). But this drops the count of non-default template args. Ah, you're trying to omit defaulted parms from the ? I'm not sure that's necessary, leaving them out of the [with ...] list should be sufficient. > To retain the count, dump_type and dump_function_name need to be called with > the original TEMPLATE_DECL. But if I do this, pretty-templates is broken. > 'template struct A { template void f(T, U); };' would > print as 'A::f(T, U) [with U = float, T = int]'. To get back to > 'A::f(T, U) [with U = float, T = int]' I needed to tell > dump_template_parms that even though the template args are there, it should > print only the template parms. The most obvious way to do that was to carry it > through via flags. > > Note that this creates another problem. Given > > template struct Outer { > template struct A; > template struct A { > void f(); > }; > }; > > we want to print e.g. 'void Outer::A::f() [with X = int, T0 = > int]', but certainly not 'void Outer::A::f() [with X = int, T0 = > int]'. However, specialized_t holds A which is printed as A > with TFF_AS_PRIMARY. Only most_general_template of the function's > TEMPLATE_DECL can give us A as DECL_CONTEXT. > > I have a solution in the diagnose_as patch, where I had to solve a similar > problem because for the diagnose_as attribute (dump_template_scope). > >>> +/* Print function template parameters if: >>> + 1. t is template, and >>> + 2. flags did not request "show only template-name", and >>> + 3. t is a specialization of a template (Why is this needed? This was >>> present + since 1999 via !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION: >>> "Don't crash if + given a friend pseudo-instantiation". The >>> DECL_USE_TEMPLATE should likely + inform the PRIMARY parameter of >>> dump_template_parms.), and >> >> Good question. It seems that the existing >> !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION has mostly been excluding the >> most general template; removing that line changes things like >> >> int bar(T) [with T = int] >> >> to >> >> int bar(T) [with T = int] >> >> >> >>> + 4. either >>> + - flags requests to show no function arguments, in which case >>> deduced + types could be hidden, or >>> + - at least one function template argument was given explicitly, or >>> + - we're printing a DWARF name, >> >> ...but perhaps this can replace the above. > > I'll rerun the tests with DECL_USE_TEMPLATE moved to the PRIMARY parameter of > dump_template_parms. > >> Though, why do we want >> different behavior here when printing a DWARF name? > > Sorry, I should have asked ... I also added the same issue as an open question > on the diagnose_as patch. When I ran the whole testsuite I had failures in the > DWARF tests. This change resolved them. I don't know enough about how those > strings are used and whether they may change between GCC versions. Anyway, the > DWARF strings in that test had only the function name and template argument > list (i.e. no function arguments). If they only have arguments, isn't TFF_NO_FUNCTION_ARGUMENTS set? > If the template argument list was removed > (as was the case without the condition here), the DWARF strings lost important > information, giving several different functions the same name. That seemed > like a regression. So either the DWARF strings need to include the function > arguments, or we need this condition to keep showing the template arguments > independent of whether they were explicitly given or not. > >>> + 5. either >>> + - t is a member friend template of a template class (see >>> DECL_TI_TEMPLATE + documentation), or >>> + - >> >> Missing the last item. :) > > Oh, I got distracted while trying to figure this out. :) I'll try to > understand why PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)) is needed and > document it. >