From d16088a15739bdf417fc752ad05f63b6566ecaaf Mon Sep 17 00:00:00 2001
From: Andrei Herman <Andrei_Herman@codesourcery.com>
Date: Thu, 13 Feb 2014 17:39:52 +0200
Subject: [PATCH 2/3]     Use flag_force_dwarf_blocks.

    When flag_force_dwarf_blocks is true, create lexical blocks
    for function body, compound statement, loop body, switch body and
    if-then/if-else statements.

    * gcc/c/c-parser.c (c_parser_declaration_or_fndef): Force a block for
    function body.
    (c_parser_statement_after_labels): Likewise for compound statement.
    (c_parser_c99_block_statement): Likewise for switch and loop statement
    if its not a compound statement.
    (c_parser_if_body): Likewise for if-then statement.
    (c_parser_else_body): Likewise for if-else statement.

    * gcc/dwarf2out.c (gen_block_die): Force output a lexical block
    die even without vars.

Signed-off-by: Andrei Herman <Andrei_Herman@codesourcery.com>
---
 gcc/c/c-parser.c |   37 ++++++++++++++++++++++++++++++-------
 gcc/dwarf2out.c  |   39 ++++++++++++++++++++++++---------------
 2 files changed, 54 insertions(+), 22 deletions(-)

diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index 66625aa..d22f79a 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -1918,7 +1918,17 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
 				   omp_declare_simd_clauses);
       DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
 	= c_parser_peek_token (parser)->location;
-      fnbody = c_parser_compound_statement (parser);
+      if (flag_force_dwarf_blocks)
+        {
+          /* Force creating a block to represent the function body.  */
+          tree block = c_begin_compound_stmt (true);
+          location_t loc = c_parser_peek_token (parser)->location;
+          keep_next_level ();
+          add_stmt (c_parser_compound_statement (parser));
+          fnbody = c_end_compound_stmt (loc, block, true);
+        }
+      else
+        fnbody = c_parser_compound_statement (parser);
       if (flag_cilkplus && contains_array_notation_expr (fnbody))
 	fnbody = expand_array_notation_exprs (fnbody);
       if (nested)
@@ -4858,6 +4868,8 @@ c_parser_statement_after_labels (c_parser *parser)
   switch (c_parser_peek_token (parser)->type)
     {
     case CPP_OPEN_BRACE:
+      if (flag_force_dwarf_blocks)
+        keep_next_level ();
       add_stmt (c_parser_compound_statement (parser));
       break;
     case CPP_KEYWORD:
@@ -5044,10 +5056,14 @@ c_parser_paren_condition (c_parser *parser)
 static tree
 c_parser_c99_block_statement (c_parser *parser)
 {
-  tree block = c_begin_compound_stmt (flag_isoc99);
+  bool force_scope = flag_force_dwarf_blocks
+    && !c_parser_next_token_is (parser, CPP_OPEN_BRACE);
+  if (force_scope)
+    keep_next_level ();
+  tree block = c_begin_compound_stmt (flag_isoc99 || force_scope);
   location_t loc = c_parser_peek_token (parser)->location;
   c_parser_statement (parser);
-  return c_end_compound_stmt (loc, block, flag_isoc99);
+  return c_end_compound_stmt (loc, block, flag_isoc99 || force_scope);
 }
 
 /* Parse the body of an if statement.  This is just parsing a
@@ -5061,7 +5077,9 @@ c_parser_c99_block_statement (c_parser *parser)
 static tree
 c_parser_if_body (c_parser *parser, bool *if_p)
 {
-  tree block = c_begin_compound_stmt (flag_isoc99);
+  if (flag_force_dwarf_blocks)
+    keep_next_level ();
+  tree block = c_begin_compound_stmt (flag_isoc99 || flag_force_dwarf_blocks);
   location_t body_loc = c_parser_peek_token (parser)->location;
   while (c_parser_next_token_is_keyword (parser, RID_CASE)
 	 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
@@ -5082,7 +5100,8 @@ c_parser_if_body (c_parser *parser, bool *if_p)
     add_stmt (c_parser_compound_statement (parser));
   else
     c_parser_statement_after_labels (parser);
-  return c_end_compound_stmt (body_loc, block, flag_isoc99);
+  return c_end_compound_stmt (body_loc, block, 
+                              flag_isoc99 || flag_force_dwarf_blocks);
 }
 
 /* Parse the else body of an if statement.  This is just parsing a
@@ -5093,7 +5112,11 @@ static tree
 c_parser_else_body (c_parser *parser)
 {
   location_t else_loc = c_parser_peek_token (parser)->location;
-  tree block = c_begin_compound_stmt (flag_isoc99);
+  bool force_scope = flag_force_dwarf_blocks
+    && !c_parser_next_token_is (parser, CPP_OPEN_BRACE);
+  if (force_scope)
+    keep_next_level ();
+  tree block = c_begin_compound_stmt (flag_isoc99 || force_scope);
   while (c_parser_next_token_is_keyword (parser, RID_CASE)
 	 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
 	 || (c_parser_next_token_is (parser, CPP_NAME)
@@ -5110,7 +5133,7 @@ c_parser_else_body (c_parser *parser)
     }
   else
     c_parser_statement_after_labels (parser);
-  return c_end_compound_stmt (else_loc, block, flag_isoc99);
+  return c_end_compound_stmt (else_loc, block, flag_isoc99 || force_scope);
 }
 
 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index d1ca4ba..0e8850b 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -19908,21 +19908,30 @@ gen_block_die (tree stmt, dw_die_ref context_die, int depth)
     must_output_die = 1;
   else
     {
-      /* Determine if this block directly contains any "significant"
-	 local declarations which we will need to output DIEs for.  */
-      if (debug_info_level > DINFO_LEVEL_TERSE)
-	/* We are not in terse mode so *any* local declaration counts
-	   as being a "significant" one.  */
-	must_output_die = ((BLOCK_VARS (stmt) != NULL
-			    || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
-			   && (TREE_USED (stmt)
-			       || TREE_ASM_WRITTEN (stmt)
-			       || BLOCK_ABSTRACT (stmt)));
-      else if ((TREE_USED (stmt)
-		|| TREE_ASM_WRITTEN (stmt)
-		|| BLOCK_ABSTRACT (stmt))
-      	       && !dwarf2out_ignore_block (stmt))
-	must_output_die = 1;
+      if (flag_force_dwarf_blocks) 
+        {
+          must_output_die = (TREE_USED (stmt)
+                             || TREE_ASM_WRITTEN (stmt)
+                             || BLOCK_ABSTRACT (stmt));
+        }
+      else 
+        {
+          /* Determine if this block directly contains any "significant"
+             local declarations which we will need to output DIEs for.  */
+          if (debug_info_level > DINFO_LEVEL_TERSE)
+            /* We are not in terse mode so *any* local declaration counts
+               as being a "significant" one.  */
+            must_output_die = ((BLOCK_VARS (stmt) != NULL
+                                || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
+                               && (TREE_USED (stmt)
+                                   || TREE_ASM_WRITTEN (stmt)
+                                   || BLOCK_ABSTRACT (stmt)));
+          else if ((TREE_USED (stmt)
+                    || TREE_ASM_WRITTEN (stmt)
+                    || BLOCK_ABSTRACT (stmt))
+                   && !dwarf2out_ignore_block (stmt))
+            must_output_die = 1;
+        }
     }
 
   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
-- 
1.7.1

