Hi! I have noticed that '-fdump-tree-original-lineno' for Fortran (for example) does dump location information, but for C/C++ it does not. The reason is that Fortran (and other front ends) use code like:
/* Output the GENERIC tree. */ dump_function (TDI_original, fndecl); ..., but 'gcc/c-family/c-gimplify.cc:c_genericize' has some special code to "Dump the C-specific tree IR", and that (unless 'TDF_RAW') calls 'gcc/c-family/c-pretty-print.cc:print_c_tree', and appears to completely ignore the 'dump_flags_t'. (Ignores it in 'c_pretty_printer::statement', and passes 'TDF_NONE' into 'dump_generic_node'.) See the attached "Honor dump options for C/C++ '-fdump-tree-original'" for what I have quickly hacked up. Does that make any sense to do like this, and if yes, how much more polish does this need, or if no, how should we approach this issue otherwise? (I need this, no surprise, for use in test cases.) Grüße Thomas
>From 5820291e06c3f5ae7002ef1ec735e4e6b8590c1f Mon Sep 17 00:00:00 2001 From: Thomas Schwinge <tschwi...@baylibre.com> Date: Thu, 16 Jan 2025 15:32:56 +0100 Subject: [PATCH] Honor dump options for C/C++ '-fdump-tree-original' --- gcc/c-family/c-gimplify.cc | 2 +- gcc/c-family/c-pretty-print.cc | 29 ++++++++++++++++++++++++----- gcc/c-family/c-pretty-print.h | 6 ++++-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/gcc/c-family/c-gimplify.cc b/gcc/c-family/c-gimplify.cc index 89a1f5c1e80..54fb912124d 100644 --- a/gcc/c-family/c-gimplify.cc +++ b/gcc/c-family/c-gimplify.cc @@ -701,7 +701,7 @@ c_genericize (tree fndecl) dump_node (DECL_SAVED_TREE (fndecl), TDF_SLIM | local_dump_flags, dump_orig); else - print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl)); + print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl), local_dump_flags); fprintf (dump_orig, "\n"); } diff --git a/gcc/c-family/c-pretty-print.cc b/gcc/c-family/c-pretty-print.cc index 0b6810e1224..1ce19f54988 100644 --- a/gcc/c-family/c-pretty-print.cc +++ b/gcc/c-family/c-pretty-print.cc @@ -2858,6 +2858,9 @@ c_pretty_printer::statement (tree t) { case SWITCH_STMT: + if (dump_flags != TDF_NONE) + internal_error ("dump flags not handled here"); + pp_c_ws_string (this, "switch"); pp_space (this); pp_c_left_paren (this); @@ -2875,6 +2878,9 @@ c_pretty_printer::statement (tree t) for ( expression(opt) ; expression(opt) ; expression(opt) ) statement for ( declaration expression(opt) ; expression(opt) ) statement */ case WHILE_STMT: + if (dump_flags != TDF_NONE) + internal_error ("dump flags not handled here"); + pp_c_ws_string (this, "while"); pp_space (this); pp_c_left_paren (this); @@ -2887,6 +2893,9 @@ c_pretty_printer::statement (tree t) break; case DO_STMT: + if (dump_flags != TDF_NONE) + internal_error ("dump flags not handled here"); + pp_c_ws_string (this, "do"); pp_newline_and_indent (this, 3); statement (DO_BODY (t)); @@ -2901,6 +2910,9 @@ c_pretty_printer::statement (tree t) break; case FOR_STMT: + if (dump_flags != TDF_NONE) + internal_error ("dump flags not handled here"); + pp_c_ws_string (this, "for"); pp_space (this); pp_c_left_paren (this); @@ -2929,6 +2941,9 @@ c_pretty_printer::statement (tree t) continue ; return expression(opt) ; */ case BREAK_STMT: + if (dump_flags != TDF_NONE) + internal_error ("dump flags not handled here"); + pp_string (this, "break"); if (BREAK_NAME (t)) { @@ -2940,6 +2955,9 @@ c_pretty_printer::statement (tree t) break; case CONTINUE_STMT: + if (dump_flags != TDF_NONE) + internal_error ("dump flags not handled here"); + pp_string (this, "continue"); if (CONTINUE_NAME (t)) { @@ -2953,15 +2971,16 @@ c_pretty_printer::statement (tree t) default: if (pp_needs_newline (this)) pp_newline_and_indent (this, 0); - dump_generic_node (this, t, pp_indentation (this), TDF_NONE, true); + dump_generic_node (this, t, pp_indentation (this), dump_flags, true); } } /* Initialize the PRETTY-PRINTER for handling C codes. */ -c_pretty_printer::c_pretty_printer () +c_pretty_printer::c_pretty_printer (dump_flags_t dump_flags) : pretty_printer (), + dump_flags (dump_flags), offset_list (), flags () { @@ -2981,9 +3000,9 @@ c_pretty_printer::clone () const /* Print the tree T in full, on file FILE. */ void -print_c_tree (FILE *file, tree t) +print_c_tree (FILE *file, tree t, dump_flags_t dump_flags) { - c_pretty_printer pp; + c_pretty_printer pp (dump_flags); pp_needs_newline (&pp) = true; pp.set_output_stream (file); @@ -2996,7 +3015,7 @@ print_c_tree (FILE *file, tree t) DEBUG_FUNCTION void debug_c_tree (tree t) { - print_c_tree (stderr, t); + print_c_tree (stderr, t, TDF_NONE); fputc ('\n', stderr); } diff --git a/gcc/c-family/c-pretty-print.h b/gcc/c-family/c-pretty-print.h index 17e0266cbfe..c8fb6789991 100644 --- a/gcc/c-family/c-pretty-print.h +++ b/gcc/c-family/c-pretty-print.h @@ -49,8 +49,10 @@ typedef void (*c_pretty_print_fn) (c_pretty_printer *, tree); and cp/cxx-pretty-print.cc for an example of derivation. */ class c_pretty_printer : public pretty_printer { + dump_flags_t dump_flags; + public: - c_pretty_printer (); + c_pretty_printer (dump_flags_t = TDF_NONE); std::unique_ptr<pretty_printer> clone () const override; // Format string, possibly translated. @@ -137,6 +139,6 @@ void pp_c_identifier (c_pretty_printer *, const char *); void pp_c_string_literal (c_pretty_printer *, tree); void pp_c_integer_constant (c_pretty_printer *, tree); -void print_c_tree (FILE *file, tree t); +void print_c_tree (FILE *file, tree t, dump_flags_t); #endif /* GCC_C_PRETTY_PRINTER */ -- 2.34.1