> On 16 Sep 2025, at 20:07, Harald Anlauf <[email protected]> wrote:
> 
> Hi All,
> 
> when trying to debug the gfortran frontend, I often get lost in the
> forest: too much forest, trees everywhere, leaves, ...
> 
> OK, seriously, what is an efficient way to debug under e.g. gdb,
> when there is code that uses macros on trees, like TREE_TYPE ()
> etc., without losing your sanity?
> 
> I do know that there exists e.g. debug_tree, but this can dump a lot
> of stuff on me.  And the comment at the top of gcc/print-tree.cc:
> 
> /* Prints out tree in human readable form - GCC
> 
> makes me feel non-human.  :-(
> 
> I was now thinking of writing helper functions, like:
> 
> diff --git a/gcc/fortran/dump-parse-tree.cc b/gcc/fortran/dump-parse-tree.cc
> index eda0659d6e2..9156c440bcb 100644
> --- a/gcc/fortran/dump-parse-tree.cc
> +++ b/gcc/fortran/dump-parse-tree.cc
> @@ -39,6 +39,8 @@ along with GCC; see the file COPYING3.  If not see
> #include "parse.h"  /* For gfc_ascii_statement.  */
> #include "omp-api.h"  /* For omp_get_name_from_fr_id.  */
> #include "gomp-constants.h"  /* For GOMP_INTEROP_IFR_SEPARATOR.  */
> +#include "tree.h"
> +#include "print-tree.h"
> 
> /* Keep track of indentation for symbol tree dumps.  */
> static int show_level = 0;
> @@ -4670,3 +4672,10 @@ debug (gfc_array_ref *ar)
>   fputc ('\n', dumpfile);
>   dumpfile = tmp;
> }
> +
> +/* Helper function for looking at TREE_TYPE (node).  */
> +DEBUG_FUNCTION void
> +debug_tree_type (tree node)
> +{
> +  debug_tree (TREE_TYPE (node));
> +}
> 
> This way I can do:
> 
> (gdb) call debug_tree_type(tree)
> 
> and get more concise output.  But the code uses the various macros
> TREE_* also in nested form, so I would have to write a wrapper for
> each combination that appears in the code.
> 
> So is there a better way?  There should be!

There are helpers available to view some if not all the macros (but that
can depend on the platform you use - they don’t work for me on macOS).

====

I am not sure if it counts as “better” but, providing you do not mind
viewing the code as “C-like” you can use the pretty-printer.

For example, I have debug functions in my local tree like this...

DEBUG_FUNCTION void
coro_pretty_p_expression (tree expr, const char *msg = NULL)
{
  if (msg)
    fprintf (stderr, "%s", msg);
  cxx_pretty_printer pp;
  pp.set_output_stream (stderr);
  if (expr)
    {
      pp_newline_and_indent (&pp, 0);
      pp.expression (expr);
    }
  else
    pp_string (&pp, "<null>");
  pp_newline_and_flush (&pp);
}

so, in the code you can call this kind of thing with a “msg” to say what you 
were dumping
and in the debugger
“call coro_pretty_p_expression (expr_tree, “”) “

obviously you’d name the functions differently :)

I have in addition : 
DEBUG_FUNCTION void
coro_pretty_p_decl (tree decl, const char *msg = NULL)

DEBUG_FUNCTION void
coro_pretty_p_type (tree type, const char *msg = NULL)

DEBUG_FUNCTION void
coro_pretty_p_statements (tree stmts, const char *msg = NULL)

I guess you could make a fortran pretty-printer .. but that is probably a 
project in its own right.

FWIW, I find that debug_tree() can be more useful sometimes - it has more 
details and with some judicious casting you can drill down into it..
.. but, as you say, seeing the wood for the trees can be hard,

Iain

>  call debug_tree_type(tree)
> 
> Thanks for any insight!
> 
> Harald
> 
> 

Reply via email to