Debug info fixes for 3.0 branch

Zack Weinberg zack@codesourcery.com
Thu Jan 31 16:32:00 GMT 2002


Here's the patch I'm applying to the 3.0 branch.  It backports Jeff
Law's patch for multi-line conditionals, and applies half of my patch
for #line in the middle.  The debug_info hooks don't exist in 3.0, so
the other half doesn't fit.  I suspect that the debug_start_source_file
call is not being done right in 3.0, but that is merely a scrupulous-
correctness issue rather than something with a test case I can point
to and say "this used to work, now it doesn't."  So I'm not going to
bother figuring out the 3.0 equivalent.

Jason's patch for #line in the middle has already been applied to the
3.0 branch.

zw

Zack Weinberg  <zack@codesourcery.com>

	* c-decl.c (c_expand_body): Reset input_filename from
	DECL_SOURCE_FILE (fndecl) before calling init_function_start.

Jeffrey A Law  <law@redhat.com>

	* c-common.c (c_expand_start_cond): Expect the IF_STMT node to
	be passed in, do not build it.
	(c_begin_if_stmt): New function.
	(c_begin_while_stmt, c_finish_while_stmt_cond): Likewise.
	* c-common.h (c_expand_start_cond): Update prototype.
	(c_begin_if_stmt): Prototype new function.
	(c_begin_while_stmt, c_finish_while_stmt_cond): Likewise.
	* c-parse.in (if_prefix): Use c_begin_if_stmt,
	c_begin_while_stmt and c_finish_while_stmt_cond.

===================================================================
Index: c-common.c
--- c-common.c	2002/01/18 03:37:51	1.226.2.8
+++ c-common.c	2002/02/01 00:24:18
@@ -252,15 +252,20 @@ static int if_stack_space = 0;
 static int if_stack_pointer = 0;
 
 /* Record the start of an if-then, and record the start of it
-   for ambiguous else detection.  */
+   for ambiguous else detection.
 
+   COND is the condition for the if-then statement.
+
+   IF_STMT is the statement node that has already been created for
+   this if-then statement.  It is created before parsing the
+   condition to keep line number information accurate.  */
+
 void
-c_expand_start_cond (cond, compstmt_count)
+c_expand_start_cond (cond, compstmt_count, if_stmt)
      tree cond;
      int compstmt_count;
+     tree if_stmt;
 {
-  tree if_stmt;
-
   /* Make sure there is enough space on the stack.  */
   if (if_stack_space == 0)
     {
@@ -273,7 +278,6 @@ c_expand_start_cond (cond, compstmt_coun
       if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
     }
 
-  if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
   IF_COND (if_stmt) = cond;
   add_stmt (if_stmt);
 
@@ -337,6 +341,46 @@ c_finish_else ()
 {
   tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
   RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
+}
+
+/* Begin an if-statement.  Returns a newly created IF_STMT if
+   appropriate.
+
+   Unlike the C++ front-end, we do not call add_stmt here; it is
+   probably safe to do so, but I am not very familiar with this
+   code so I am being extra careful not to change its behavior
+   beyond what is strictly necessary for correctness.  */
+
+tree
+c_begin_if_stmt ()
+{
+  tree r;
+  r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
+  return r;
+}
+
+/* Begin a while statement.  Returns a newly created WHILE_STMT if
+   appropriate.
+
+   Unlike the C++ front-end, we do not call add_stmt here; it is
+   probably safe to do so, but I am not very familiar with this
+   code so I am being extra careful not to change its behavior
+   beyond what is strictly necessary for correctness.  */
+
+tree
+c_begin_while_stmt ()
+{
+  tree r;
+  r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
+  return r;
+}
+
+void
+c_finish_while_stmt_cond (cond, while_stmt)
+     tree while_stmt;
+     tree cond;
+{
+  WHILE_COND (while_stmt) = cond;
 }
 
 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__.  */
===================================================================
Index: c-common.h
--- c-common.h	2002/01/18 03:37:52	1.64.4.5
+++ c-common.h	2002/02/01 00:24:19
@@ -329,6 +329,9 @@ extern tree walk_stmt_tree			PARAMS ((tr
 extern void prep_stmt                           PARAMS ((tree));
 extern void expand_stmt                         PARAMS ((tree));
 extern void mark_stmt_tree                      PARAMS ((void *));
+extern tree c_begin_if_stmt			PARAMS ((void));
+extern tree c_begin_while_stmt			PARAMS ((void));
+extern void c_finish_while_stmt_cond		PARAMS ((tree, tree));
 
 /* Extra information associated with a DECL.  Other C dialects extend
    this structure in various ways.  The C front-end only uses this
@@ -497,7 +500,7 @@ extern void c_apply_type_quals_to_decl		
    NOP_EXPR is used as a special case (see truthvalue_conversion).  */
 extern void binary_op_error			PARAMS ((enum tree_code));
 extern tree c_expand_expr_stmt			PARAMS ((tree));
-extern void c_expand_start_cond			PARAMS ((tree, int));
+extern void c_expand_start_cond			PARAMS ((tree, int, tree));
 extern void c_finish_then                       PARAMS ((void));
 extern void c_expand_start_else			PARAMS ((void));
 extern void c_finish_else                   PARAMS ((void));
===================================================================
Index: c-decl.c
--- c-decl.c	2001/06/12 12:19:54	1.207.2.17
+++ c-decl.c	2002/02/01 00:24:22
@@ -6728,6 +6728,7 @@ c_expand_body (fndecl, nested_p)
 
   /* Initialize the RTL code for the function.  */
   current_function_decl = fndecl;
+  input_filename = DECL_SOURCE_FILE (fndecl);
   init_function_start (fndecl, input_filename, DECL_SOURCE_LINE (fndecl));
 
   /* This function is being processed in whole-function mode.  */
===================================================================
Index: c-parse.in
--- c-parse.in	2001/12/11 22:17:13	1.82.2.3
+++ c-parse.in	2002/02/01 00:24:22
@@ -1781,13 +1781,23 @@ simple_if:
 	;
 
 if_prefix:
-	  IF '(' expr ')'
-		{ c_expand_start_cond (truthvalue_conversion ($3), 
-				       compstmt_count);
+	  /* We must build the IF_STMT node before parsing its
+	     condition so that STMT_LINENO refers to the line
+	     containing the "if", and not the line containing
+	     the close-parenthesis.
+
+	     c_begin_if_stmt returns the IF_STMT node, which
+	     we later pass to c_expand_start_cond to fill
+	     in the condition and other tidbits.  */
+          IF
+                { $<ttype>$ = c_begin_if_stmt (); }
+            '(' expr ')'
+		{ c_expand_start_cond (truthvalue_conversion ($4), 
+				       compstmt_count,$<ttype>2);
 		  $<itype>$ = stmt_count;
 		  if_stmt_file = $<filename>-2;
 		  if_stmt_line = $<lineno>-1; }
-	;
+        ;
 
 /* This is a subroutine of stmt.
    It is used twice, once for valid DO statements
@@ -1882,12 +1892,22 @@ select_or_iter_stmt:
    Otherwise a crash is likely.  */
 	| simple_if ELSE error
 		{ c_expand_end_cond (); }
+       /* We must build the WHILE_STMT node before parsing its
+	  condition so that STMT_LINENO refers to the line
+	  containing the "while", and not the line containing
+	  the close-parenthesis.
+
+	  c_begin_while_stmt returns the WHILE_STMT node, which
+	  we later pass to c_finish_while_stmt_cond to fill
+	  in the condition and other tidbits.  */
 	| WHILE
-                { stmt_count++; }
+                { stmt_count++; 
+		  $<ttype>$ = c_begin_while_stmt (); }
 	  '(' expr ')'
                 { $4 = truthvalue_conversion ($4);
-		  $<ttype>$ 
-		    = add_stmt (build_stmt (WHILE_STMT, $4, NULL_TREE)); }
+		  c_finish_while_stmt_cond (truthvalue_conversion ($4),
+					    $<ttype>2);
+		  $<ttype>$ = add_stmt ($<ttype>2); }
 	  c99_block_lineno_labeled_stmt
 		{ RECHAIN_STMTS ($<ttype>6, WHILE_BODY ($<ttype>6)); }
 	| do_stmt_start



More information about the Gcc-patches mailing list