[RFC] c++: Print function template parms when relevant (was: [PATCH v4] c++: Add gnu::diagnose_as attribute)

Matthias Kretz m.kretz@gsi.de
Mon Nov 8 16:40:44 GMT 2021


On Tuesday, 17 August 2021 20:31:54 CET Jason Merrill wrote:
> > 2. Given a DECL_TI_ARGS tree, can I query whether an argument was deduced
> > or explicitly specified? I'm asking because I still consider diagnostics
> > of function templates unfortunate. `template <class T> void f()` is fine,
> > as is `void f(T) [with T = float]`, but `void f() [with T = float]` could
> > be better. I.e. if the template parameter appears somewhere in the
> > function parameter list, dump_template_parms would only produce noise.
> > If, however, the template parameter was given explicitly, it would be
> > nice if it could show up accordingly in diagnostics.
> 
> NON_DEFAULT_TEMPLATE_ARGS_COUNT has that information, though there are
> some issues with it.  Attached is my WIP from May to improve it
> somewhat, if that's interesting.

It is interesting. I used your patch to come up with the attached. Patch. I 
must say, I didn't try to read through all the cp/pt.c code to understand all 
of what you did there (which is why my ChangeLog entry says "Jason?"), but it 
works for me (and all of `make check`).

Anyway, I'd like to propose the following before finishing my diagnose_as 
patch. I believe it's useful to fix this part first. The diagnostic/default-
template-args-[12].C tests show a lot of examples of the intent of this patch. 
And the remaining changes to the testsuite show how it changes diagnostic 
output.

---------------------- 8< --------------------

The choice when to print a function template parameter was still
suboptimal. That's because sometimes the function template parameter
list only adds noise, while in other situations the lack of a function
template parameter list makes diagnostic messages hard to understand.

The general idea of this change is to print template parms wherever they
would appear in the source code as well. Thus, the diagnostics code
needs to know whether any template parameter was given explicitly.

Signed-off-by: Matthias Kretz <m.kretz@gsi.de>

gcc/testsuite/ChangeLog:

        * g++.dg/debug/dwarf2/template-params-12n.C: Optionally, allow
        DW_AT_default_value.
        * g++.dg/diagnostic/default-template-args-1.C: New.
        * g++.dg/diagnostic/default-template-args-2.C: New.
        * g++.dg/diagnostic/param-type-mismatch-2.C: Expect template
        parms in diagnostic.
        * g++.dg/ext/pretty1.C: Expect function template specialization
        to not pretty-print template parms.
        * g++.old-deja/g++.ext/pretty3.C: Ditto.
        * g++.old-deja/g++.pt/memtemp77.C: Ditto.
        * g++.dg/goacc/template.C: Expect function template parms for
        explicit arguments.
        * g++.dg/gomp/declare-variant-7.C: Expect no function template
        parms for deduced arguments.
        * g++.dg/template/error40.C: Expect only non-default template
        arguments in diagnostic.

gcc/cp/ChangeLog:

        * cp-tree.h (GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT): Return
        absolute value of stored constant.
        (EXPLICIT_TEMPLATE_ARGS_P): New.
        (SET_EXPLICIT_TEMPLATE_ARGS_P): New.
        (TFF_AS_PRIMARY): New constant.
        * error.c (get_non_default_template_args_count): Avoid
        GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT if
        NON_DEFAULT_TEMPLATE_ARGS_COUNT is a NULL_TREE. Make independent
        of flag_pretty_templates.
        (dump_template_bindings): Add flags parameter to be passed to
        get_non_default_template_args_count. Print only non-default
        template arguments.
        (dump_function_decl): Call dump_function_name and dump_type of
        the DECL_CONTEXT with specialized template and set
        TFF_AS_PRIMARY for their flags.
        (dump_function_name): Add and document conditions for calling
        dump_template_parms.
        (dump_template_parms): Print only non-default template
        parameters.
        * pt.c (determine_specialization): Jason?
        (template_parms_level_to_args): Jason?
        (copy_template_args): Jason?
        (fn_type_unification): Set EXPLICIT_TEMPLATE_ARGS_P on the
        template arguments tree if any template parameter was explicitly
        given.
        (type_unification_real): Jason?
        (get_partial_spec_bindings): Jason?
        (tsubst_template_args): Determine number of defaulted arguments
        from new argument vector, if possible.
---
 gcc/cp/cp-tree.h                              | 18 +++-
 gcc/cp/error.c                                | 83 ++++++++++++++-----
 gcc/cp/pt.c                                   | 58 +++++++++----
 .../g++.dg/debug/dwarf2/template-params-12n.C |  2 +-
 .../diagnostic/default-template-args-1.C      | 73 ++++++++++++++++
 .../diagnostic/default-template-args-2.C      | 37 +++++++++
 .../g++.dg/diagnostic/param-type-mismatch-2.C |  2 +-
 gcc/testsuite/g++.dg/ext/pretty1.C            |  2 +-
 gcc/testsuite/g++.dg/goacc/template.C         |  8 +-
 gcc/testsuite/g++.dg/gomp/declare-variant-7.C |  4 +-
 gcc/testsuite/g++.dg/template/error40.C       |  6 +-
 gcc/testsuite/g++.old-deja/g++.ext/pretty3.C  |  2 +-
 gcc/testsuite/g++.old-deja/g++.pt/memtemp77.C |  2 +-
 13 files changed, 242 insertions(+), 55 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/diagnostic/default-template-args-1.C
 create mode 100644 gcc/testsuite/g++.dg/diagnostic/default-template-args-2.C


-- 
──────────────────────────────────────────────────────────────────────────
 Dr. Matthias Kretz                           https://mattkretz.github.io
 GSI Helmholtz Centre for Heavy Ion Research               https://gsi.de
 stdₓ::simd
──────────────────────────────────────────────────────────────────────────
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-c-Print-function-template-parms-when-relevant.patch
Type: text/x-patch
Size: 27049 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20211108/470f8c89/attachment-0001.bin>


More information about the Gcc-patches mailing list