This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] -fdump-translation-unit fix (fixes PR c/9177)


1. A description of the problem/bug and how your patch addresses it.

There is a bug that affects the gcc translation unit dump functionality.
When dumping the intermediate representation with the -fdump-translation-unit
option, gcc generates a .tu file that contains FUNCTION_DECL nodes without
a body.

.tu file generated by gcc

@1      function_decl    name: @2       type: @3       srcp: a.c:9
                         chan: @4       extern

The function body is not included in the dump. Invoking g++
with the -fdump-translation-unit option generates a .tu file
that contains the function body.

.tu file generated by g++

@3      function_decl    name: @4       type: @5       srcp: a.c:9
                         chan: @6       C              extern
                         body: @7

The body field points to a compound_stmt node, which contains the
function body. The C++ frontend behaves correctly. There is a bug
in the C frontend which makes it generate incomplete dumps.

The problem is in c-decl.c. After the RTL is generated, the function
body (pointed to by DECL_SAVED_TREE) is discarded.

  /* Generate the RTL for this function.  */
  expand_stmt (DECL_SAVED_TREE (fndecl));

  /* Keep the function body if it's needed for inlining or dumping */
  if (uninlinable)
    {
      /* Allow the body of the function to be garbage collected.  */
      DECL_SAVED_TREE (fndecl) = NULL_TREE;
    }

The code is only executed by the C frontend. The C++ frontend contains
a similar section of code in cp/semantics.c, but it checks whether
the dump mode is enabled and keeps the function body in that case.

  /* If possible, obliterate the body of the function so that it can
     be garbage collected.  */
  if (dump_enabled_p (TDI_all))
    /* Keep the body; we're going to dump it.  */
    ;
  else if (DECL_INLINE (fn) && flag_inline_trees)
    /* We might need the body of this function so that we can expand
       it inline somewhere else.  */
    ;
  else
    /* We don't need the body; blow it away.  */
    DECL_SAVED_TREE (fn) = NULL_TREE;

Patching c-decl.c to check for the dump mode is trivial. A patched
gcc generates the correct dump:

.tu file generated by patched gcc

@1      function_decl    name: @2       type: @3       srcp: a.c:9
                         chan: @4       extern         body: @5

------------------------------------------------------------------

2. Testcases

Testcases are not included, because the patch is a trivial bugfix
and only affects the generation of debugging dumps.

------------------------------------------------------------------

3. ChangeLog

2002-11-10  Alexander Sotirov <sluncho@mirizma.org>

    * c-decl.c (c_expand_body):  Don't garbage collect the function
    body if we are going to dump it later.

------------------------------------------------------------------

4. Bootstrapping and testing

The patched gcc successfully bootstraps on i686-pc-linux-gnu and
generates correct .tu dumps.

------------------------------------------------------------------

5. The patch itself

The patch applies cleanly to gcc-3.2 and to the current CVS code.


diff -ru gcc-3.2.orig/gcc/c-decl.c gcc-3.2/gcc/c-decl.c
--- gcc-3.2.orig/gcc/c-decl.c   2002-07-26 18:23:03.000000000 -0500
+++ gcc-3.2/gcc/c-decl.c    2002-11-10 02:04:50.000000000 -0600
@@ -7091,7 +7091,9 @@

   /* Generate the RTL for this function.  */
   expand_stmt (DECL_SAVED_TREE (fndecl));
-  if (uninlinable)
+
+  /* Keep the function body if it's needed for inlining or dumping */
+  if (uninlinable && !dump_enabled_p (TDI_all))
     {
       /* Allow the body of the function to be garbage collected.  */
       DECL_SAVED_TREE (fndecl) = NULL_TREE;



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]