[patch,c++] Generate correct (i.e., const) debug entries for arrays declared const but not marked READONLY

t56xjcu6dh@snkmail.com t56xjcu6dh@snkmail.com
Tue Dec 18 01:08:00 GMT 2012


An array declared const is marked not READONLY if (1) its initializer list involves a constructor call, or (2) it's a static class member with an initializer list.  The array should have a debug information entry marking it as const, but it doesn't.  (In case 2, the array might be marked READONLY again after the initializer has been parsed, but by then, the debug entries have already been written.)

This patch has been tested on x86_64.  It fixes bug 55434.

Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c	(revision 194568)
+++ gcc/cp/typeck.c	(working copy)
@@ -8600,6 +8600,7 @@
 cp_apply_type_quals_to_decl (int type_quals, tree decl)
 {
   tree type = TREE_TYPE (decl);
+  int was_const;
 
   if (type == error_mark_node)
     return;
@@ -8610,6 +8611,9 @@
   gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
 		&& type_quals != TYPE_UNQUALIFIED));
 
+  /* Remember const qualifer even if we don't set TREE_READONLY */
+  was_const = type_quals & TYPE_QUAL_CONST;
+
   /* Avoid setting TREE_READONLY incorrectly.  */
   /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
      constructor can produce constant init, so rely on cp_finish_decl to
@@ -8621,6 +8625,9 @@
     type_quals &= ~TYPE_QUAL_CONST;
 
   c_apply_type_quals_to_decl (type_quals, decl);
+
+  if (was_const)
+    TREE_WASREADONLY(decl) = 1;
 }
 
 /* Subroutine of casts_away_constness.  Make T1 and T2 point at
Index: gcc/cp/typeck2.c
===================================================================
--- gcc/cp/typeck2.c	(revision 194568)
+++ gcc/cp/typeck2.c	(working copy)
@@ -633,6 +633,7 @@
 	init = NULL_TREE;
       code = pop_stmt_list (code);
       DECL_INITIAL (dest) = init;
+      TREE_WASREADONLY (dest) = TREE_READONLY (dest);
       TREE_READONLY (dest) = 0;
     }
   else
Index: gcc/dwarf2out.c
===================================================================
--- gcc/dwarf2out.c	(revision 194568)
+++ gcc/dwarf2out.c	(working copy)
@@ -18370,7 +18370,9 @@
       if (decl_by_reference_p (decl_or_origin))
 	add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
       else
-	add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
+	add_type_attribute (var_die, type,
+                TREE_READONLY (decl_or_origin) ||
+                  TREE_WASREADONLY (decl_or_origin),
 			    TREE_THIS_VOLATILE (decl_or_origin), context_die);
     }
 
Index: gcc/tree.h
===================================================================
--- gcc/tree.h	(revision 194568)
+++ gcc/tree.h	(working copy)
@@ -438,6 +438,9 @@
   unsigned deprecated_flag : 1;
   unsigned default_def_flag : 1;
 
+  unsigned wasreadonly_flag : 1;
+  unsigned spare : 7;
+
   union {
     /* The bits in the following structure should only be used with
        accessor macros that constrain inputs with tree checking.  */
@@ -1246,6 +1249,11 @@
    as "const" function (can only read its arguments).  */
 #define TREE_READONLY(NODE) (NON_TYPE_CHECK (NODE)->base.readonly_flag)
 
+/* Keep track of variables that were declared const, even if they might
+   not be placed in read-only memory.  This allows generation of more
+   accurate debug info. */
+#define TREE_WASREADONLY(NODE) (NON_TYPE_CHECK (NODE)->base.wasreadonly_flag)
+
 /* Value of expression is constant.  Always on in all ..._CST nodes.  May
    also appear in an expression or decl where the value is constant.  */
 #define TREE_CONSTANT(NODE) (NON_TYPE_CHECK (NODE)->base.constant_flag)

--
Louis Krupp



More information about the Gcc-patches mailing list