On 8/24/20 1:40 PM, Jakub Jelinek wrote:
On Mon, Aug 24, 2020 at 02:56:55PM +0200, Mark Wielaard wrote:
In DWARF5 class variables (static data members) are represented with a
DW_TAG_variable instead of a DW_TAG_member. Make sure the variable isn't
optimized away in the constexpr-var-1.C testcase so we can still match (2)
const_expr in the the assembly output.

Note that the same issue causes some failures in the gdb testsuite
for static data members when we enable DWARF5 by default:
https://sourceware.org/bugzilla/show_bug.cgi?id=26525
---
  gcc/testsuite/g++.dg/debug/dwarf2/constexpr-var-1.C | 1 +
  1 file changed, 1 insertion(+)

diff --git a/gcc/testsuite/g++.dg/debug/dwarf2/constexpr-var-1.C 
b/gcc/testsuite/g++.dg/debug/dwarf2/constexpr-var-1.C
index 19062e29fd59..c6ad3f645379 100644
--- a/gcc/testsuite/g++.dg/debug/dwarf2/constexpr-var-1.C
+++ b/gcc/testsuite/g++.dg/debug/dwarf2/constexpr-var-1.C
@@ -7,3 +7,4 @@ struct S
  {
    static constexpr int b = 6;
  } s;
+const int &c = s.b;

This looks incorrect to me, that is a workaround for a real GCC bug.

Shouldn't we instead do something like (untested) following patch?
I mean, for DWARF < 5 the static data members were using DW_TAG_member,
which has been always marked by the function, so IMHO we should also always
mark the DW_TAG_variables at the class scope that replaced those.

The earlier behavior seems like an accident, that happened because we always need to emit information about non-static data members. I don't think we should take it as guidance.

In this case one reason we don't emit debug info is because (before C++17) there's no definition of 'b'. After C++17 the in-class declaration of 'b' is a definition, but we don't have to give it a symbol, so there's still nothing for the debug info to describe.

This issue doesn't seem specific to class members; it also affects namespace-scope C++17 inline variables:

inline const int var = 42;
int main() { return var; }

Compiling this testcase with -g doesn't emit any debug info for 'var' even though it's used.

Should we assume that if a variable with DW_AT_const_value is TREE_USED, we need to write out debug info for it?

Jason


2020-08-24  Jakub Jelinek  <ja...@redhat.com>

        * dwarf2out.c (prune_unused_types_walk): Mark DW_TAG_variable DIEs
        at class scope for DWARF5+.

--- gcc/dwarf2out.c.jj  2020-07-28 15:39:09.883757946 +0200
+++ gcc/dwarf2out.c     2020-08-24 19:33:16.503961786 +0200
@@ -29392,6 +29392,13 @@ prune_unused_types_walk (dw_die_ref die)
          if (die->die_perennial_p)
            break;
+ /* For static data members, the declaration in the class is supposed
+            to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
+            DWARF5.  DW_TAG_member will be marked, so mark even such
+            DW_TAG_variables in DWARF5.  */
+         if (dwarf_version >= 5 && class_scope_p (die->die_parent))
+           break;
+
          /* premark_used_variables marks external variables --- don't mark
             them here.  But function-local externals are always considered
             used.  */


        Jakub


Reply via email to