http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55434



             Bug #: 55434

           Summary: const array with elements initialized by constructor

                    marked non-const in debug info

    Classification: Unclassified

           Product: gcc

           Version: 4.7.3

            Status: UNCONFIRMED

          Severity: minor

          Priority: P3

         Component: c++

        AssignedTo: unassig...@gcc.gnu.org

        ReportedBy: t56xjcu...@snkmail.com





Created attachment 28758

  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28758

C++ source file



In this code:



struct s

{

    int i1, i2;

};



const s c1 = { 1, 2 };

const s ca1[] = { { 1, 2} };

const s c2 = c1;

const s ca2[] = { c1 };



int main(void)

{

    return 0;

}





gdb sees all variables as const except for ca2:



(gdb) ptype ca1

type = const struct s {

    int i1;

    int i2;

} [1]

(gdb) ptype ca2

type = struct s {

    int i1;

    int i2;

} [1]



The problem seems to be in split_nonconstant_init() in cp/typeck2.c;  when

TREE_READONLY (dest) is set to zero, the information that it was ever read-only

is lost before the DWARF record is written.



The following patch seems to fix the problem.  I would not be surprised if

there were a more elegant way of doing this.



(You might be wondering:  How did I find this, and why do I care?  I've been

working on something to read object files and then flag variables that raise

thread-safety issues because they are (1) global or static and (2) not const. 

Reading DWARF records works really well, except for this particular problem.)



Index: gcc/cp/typeck2.c

===================================================================

--- gcc/cp/typeck2.c    (revision 193640)

+++ 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 193640)

+++ gcc/dwarf2out.c    (working copy)

@@ -18031,7 +18031,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 193640)

+++ gcc/tree.h    (working copy)

@@ -464,8 +464,9 @@

   unsigned packed_flag : 1;

   unsigned user_align : 1;

   unsigned nameless_flag : 1;

+  unsigned wasreadonly_flag : 1;



-  unsigned spare : 12;

+  unsigned spare : 11;



   /* This field is only used with type nodes; the only reason it is present

      in tree_base instead of tree_type is to save space.  The size of the

@@ -1344,6 +1345,7 @@

    Nonzero in a FUNCTION_DECL means this function should be treated

    as "const" function (can only read its arguments).  */

 #define TREE_READONLY(NODE) (NON_TYPE_CHECK (NODE)->base.readonly_flag)

+#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.  */

Reply via email to