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

login
register
mail settings
Submitter t56xjcu6dh@snkmail.com
Date Dec. 18, 2012, 1:07 a.m.
Message ID <19376-1355792868-725211@sneakemail.com>
Download mbox | patch
Permalink /patch/207014/
State New
Headers show

Comments

t56xjcu6dh@snkmail.com - Dec. 18, 2012, 1:07 a.m.
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.


--
Louis Krupp
Jason Merrill - Feb. 15, 2013, 5:29 p.m.
There's no need to track this on the decl, as the decl's type still 
reflects the constness of the declaration.  The issue is that the const 
applies to the array element type rather than the array type itself, so 
the code in dwarf2out.c needs to be fixed to find it there.

Jason

Patch

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)