From 0a39ef2415e5b43755556e5554533b33ff86f16d Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <thomas@codesourcery.com>
Date: Fri, 11 Feb 2022 10:10:25 +0100
Subject: [PATCH] Consider 'TDF_UID', 'TDF_NOUID' in 'print_node_brief',
 'print_node'

Running GCC with '-fdump-tree-all-uid' (so that 'TDF_UID' is set in
'dump_flags') and '-wrapper gdb,--args', then for a 'call debug_tree(decl)',
that does (pretty-)print all kinds of things -- but not the 'DECL_UID':

    [...]
    (gdb) print dump_flags & TDF_UID
    $1 = 256
    (gdb) call debug_tree(decl)
     <var_decl 0x7ffff7fc3f30 i
        type <integer_type 0x7ffff75555e8 integer(kind=4) public SI
            size <integer_cst 0x7ffff753ee40 constant 32>
            unit-size <integer_cst 0x7ffff753ee58 constant 4>
            align:32 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type 0x7ffff75555e8 precision:32 min <integer_cst 0x7ffff753edf8 -2147483648> max <integer_cst 0x7ffff753ee10 2147483647>
            pointer_to_this <pointer_type 0x7ffff755da80>>
        used SI source-gcc/gcc/testsuite/gfortran.dg/goacc-gomp/pr102330-3.f90:10:3 size <integer_cst 0x7ffff753ee40 32> unit-size <integer_cst 0x7ffff753ee58 4>
        align:32 warn_if_not_align:0 context <function_decl 0x7ffff7730d00 p>>
    (gdb) print decl.decl_minimal.uid
    $3 = 4249

In my opinion, that's a bit unfortunate, as the 'DECL_UID' is very important
for debugging certain classes of issues.

With this patch, there is no change if 'TDF_UID' isn't set, but if it is, we
now append 'uid [DECL_UID]':

     <var_decl 0x7ffff7fc3f30 i uid 4249
        type <integer_type 0x7ffff75555e8 integer(kind=4) public SI
            size <integer_cst 0x7ffff753ee40 constant 32>
            unit-size <integer_cst 0x7ffff753ee58 constant 4>
            align:32 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type 0x7ffff75555e8 precision:32 min <integer_cst 0x7ffff753edf8 -2147483648> max <integer_cst 0x7ffff753ee10 2147483647>
            pointer_to_this <pointer_type 0x7ffff755da80>>
        used SI source-gcc/gcc/testsuite/gfortran.dg/goacc-gomp/pr102330-3.f90:6:3 size <integer_cst 0x7ffff753ee40 32> unit-size <integer_cst 0x7ffff753ee58 4>
        align:32 warn_if_not_align:0 context <function_decl 0x7ffff7730d00 p uid 4227>>

Notice 'var_decl': 'i uid 4249', and 'function_decl': 'p uid 4227'.

A few 'print_node_brief' examples:

'!TDF_UID', '!TDF_NOUID' (no change):

    <var_decl 0x7ffff76bc120 iftmp.1>

    <function_decl 0x7ffff7692400 main>

    <label_decl 0x7ffff76b9000 labl>
    <label_decl 0x7ffff76b7d00 L.0>
    <label_decl 0x7ffff76b9100 D.2138>

'!TDF_UID', 'TDF_NOUID' (no change):

    <var_decl 0x7ffff76bc120 iftmp.1>

    <function_decl 0x7ffff7692400 main>

    <label_decl 0x7ffff76b9000 labl>
    <label_decl 0x7ffff76b7d00 L.xxxx>
    <label_decl 0x7ffff76b9100 D.xxxx>

'TDF_UID', '!TDF_NOUID' (now appends 'uid [DECL_UID]'):

    <var_decl 0x7ffff76bc120 iftmp.1 uid 2136>

    <function_decl 0x7ffff7692400 main uid 2124>

    <label_decl 0x7ffff76b9000 labl uid 2129>
    <label_decl 0x7ffff76b7d00 L.0 uid 2243
    <label_decl 0x7ffff76b9100 D.2138 uid 2138>

'TDF_UID', 'TDF_NOUID' (now appends 'uid xxxx'):

    <var_decl 0x7ffff76bc120 iftmp.1 uid xxxx>

    <function_decl 0x7ffff7692400 main uid xxxx>

    <label_decl 0x7ffff76b9000 labl uid xxxx>
    <label_decl 0x7ffff76b7d00 L.xxxx uid xxxx>
    <label_decl 0x7ffff76b9100 D.xxxx uid xxxx>

	gcc/
	* print-tree.cc (print_node_brief, print_node): Consider
	'TDF_UID', 'TDF_NOUID'.
---
 gcc/print-tree.cc | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/gcc/print-tree.cc b/gcc/print-tree.cc
index 0876da873a9..38dd032fbf7 100644
--- a/gcc/print-tree.cc
+++ b/gcc/print-tree.cc
@@ -158,6 +158,14 @@ print_node_brief (FILE *file, const char *prefix, const_tree node, int indent)
 		     TREE_CODE (node) == CONST_DECL ? 'C' : 'D',
 		     DECL_UID (node));
 	}
+
+      if (dump_flags & TDF_UID)
+	{
+	  if (dump_flags & TDF_NOUID)
+	    fprintf (file, " uid xxxx");
+	  else
+	    fprintf (file, " uid %d", DECL_UID (node));
+	}
     }
   else if (tclass == tcc_type)
     {
@@ -301,6 +309,14 @@ print_node (FILE *file, const char *prefix, tree node, int indent,
 	    fprintf (file, " %c.%u", code == CONST_DECL ? 'C' : 'D',
 		     DECL_UID (node));
 	}
+
+      if (dump_flags & TDF_UID)
+	{
+	  if (dump_flags & TDF_NOUID)
+	    fprintf (file, " uid xxxx");
+	  else
+	    fprintf (file, " uid %d", DECL_UID (node));
+	}
     }
   else if (tclass == tcc_type)
     {
-- 
2.34.1

