Hi Iain,

I guess the hard thing is for the developer to know that (s)he wants
the option and what to do when a conflict occurs.

Actually, I just hit on a much simpler solution.

We translate all symbols to lower case (except for those
with BIND(C), which do not matter in this context).  So,
prefixing everything with GFC_ (upper case) should work on
any toolchain that can handle C, which is all we care about.

So, here is a patch which implements this method. The tree
dump now looks like this:

    struct __st_parameter_dt GFC_dt_parm_0;

    GFC_dt_parm_0.common.filename = &"hello.f90"[1]{lb: 1 sz: 1};
    GFC_dt_parm_0.common.line = 2;
    GFC_dt_parm_0.common.flags = 128;
    GFC_dt_parm_0.common.unit = 6;
    _gfortran_st_write (&GFC_dt_parm_0);
_gfortran_transfer_character_write (&GFC_dt_parm_0, &"Hello, world"[1]{lb: 1 sz: 1}, 12);
    _gfortran_st_write_done (&GFC_dt_parm_0);

So, thanks for raising your concerns, I like this much better now :-)

Regression-tested. Also tested with "make dvi" and "make pdf".
If there are no other comments, I'd like to commit this as having
no user impact and kind of obvious (now :-) tomorrow or so.

Best regards

        Thomas
diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 8bdc8a6b038..0386818a93d 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -160,7 +160,7 @@ and warnings}.
 @item Debugging Options
 @xref{Debugging Options,,Options for debugging your program or GNU Fortran}.
 @gccoptlist{-fbacktrace -fdump-fortran-optimized -fdump-fortran-original @gol
--fdump-fortran-global -fdump-parse-tree -ffpe-trap=@var{list} @gol
+-fdebug-aux-vars -fdump-fortran-global -fdump-parse-tree -ffpe-trap=@var{list} @gol
 -ffpe-summary=@var{list}
 }
 
@@ -1219,6 +1219,15 @@ change between releases. This option may also generate internal
 compiler errors for features which have only recently been added. This
 option is deprecated; use @code{-fdump-fortran-original} instead.
 
+@item -fdebug-aux-vars
+@opindex @code{fdebug-aux-vars}
+Renames internal variables created by the gfortran front end and makes
+them accessible to a debugger.  The name of the internal variables then
+start with @code{GFC_}. This option is useful for debugging the
+compiler's code generation together with @code{-fdump-tree-original} and
+enabling debugging of the executable program by using @code{-g} or
+@code{-ggdb3}.
+
 @item -fdump-fortran-global
 @opindex @code{fdump-fortran-global}
 Output a list of the global identifiers after translating into
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
index 96ed208cb85..57b0264458e 100644
--- a/gcc/fortran/lang.opt
+++ b/gcc/fortran/lang.opt
@@ -452,6 +452,10 @@ fd-lines-as-comments
 Fortran RejectNegative
 Treat lines with 'D' in column one as comments.
 
+fdebug-aux-vars
+Fortran Var(flag_debug_aux_vars)
+Issue debug information for compiler-generated auxiliary variables.
+
 fdec
 Fortran Var(flag_dec)
 Enable all DEC language extensions.
diff --git a/gcc/fortran/trans.c b/gcc/fortran/trans.c
index 025abe38985..0219115812a 100644
--- a/gcc/fortran/trans.c
+++ b/gcc/fortran/trans.c
@@ -73,6 +73,42 @@ gfc_advance_chain (tree t, int n)
   return t;
 }
 
+static int num_var;
+
+#define MAX_PREFIX_LEN 20
+
+static tree
+create_var_debug_raw (tree type, const char *prefix)
+{
+  /* Space for "GFC_" + prefix + "_" + 10-digit-number + \0.  */
+  char name_buf[4 + MAX_PREFIX_LEN + 1 + 10 + 1];
+  tree t;
+
+  if (prefix != NULL)
+    {
+      gcc_assert (strlen (prefix) <= MAX_PREFIX_LEN);
+      snprintf (name_buf, sizeof (name_buf), "GFC_%s_%d", prefix, num_var++);
+    }
+  else
+    snprintf (name_buf, sizeof (name_buf), "GFC_%d", num_var++);
+
+  t = build_decl (input_location, VAR_DECL, get_identifier (name_buf), type);
+
+  /* We want debug info for it.  */
+  DECL_IGNORED_P (t) = 0;
+  /* It should not be nameless.  */
+  DECL_NAMELESS (t) = 0;
+
+  /* Make the variable writable.  */
+  TREE_READONLY (t) = 0;
+
+  DECL_EXTERNAL (t) = 0;
+  TREE_STATIC (t) = 0;
+  TREE_USED (t) = 1;
+
+  return t;
+}
+
 /* Creates a variable declaration with a given TYPE.  */
 
 tree
@@ -80,6 +116,9 @@ gfc_create_var_np (tree type, const char *prefix)
 {
   tree t;
 
+  if (flag_debug_aux_vars)
+    return create_var_debug_raw (type, prefix);
+
   t = create_tmp_var_raw (type, prefix);
 
   /* No warnings for anonymous variables.  */

Reply via email to