This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] DWARF: add DW_AT_count to zero-length arrays
- From: Omar Sandoval <osandov at osandov dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 16 Aug 2018 21:29:00 -0700
- Subject: [PATCH] DWARF: add DW_AT_count to zero-length arrays
Hi,
This fixes the issue that it is impossible to distinguish a zero-length array
type from a flexible array type given the DWARF produced by GCC (which I
reported here [1]). We do so by adding a DW_AT_count attribute with a value of
zero only for zero-length arrays (this is what clang does in this case, too).
1: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86985
The reproducer from the PR now produces the expected output:
$ ~/gcc-build/gcc/xgcc -B ~/gcc-build/gcc -g -c zero_length.c
$ ~/gcc-build/gcc/xgcc -B ~/gcc-build/gcc -g -c flexible.c
$ gdb -batch -ex 'ptype baz' zero_length.o
type = struct {
int foo;
int bar[0];
}
$ gdb -batch -ex 'ptype baz' flexible.o
type = struct {
int foo;
int bar[];
}
This was bootstrapped and tested on x86_64-pc-linux-gnu.
I don't have commit rights (first time contributor), so if this change is okay
could it please be applied?
Thanks!
2018-08-16 Omar Sandoval <osandov@osandov.com>
* dwarf2out.c (is_c_family): New.
(add_subscript_info): Add DW_AT_count of 0 for C zero-length arrays.
diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index 5a74131d332..b638942c156 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -3671,6 +3671,7 @@ static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
+static bool is_c_family (void);
static bool is_cxx (void);
static bool is_cxx (const_tree);
static bool is_fortran (void);
@@ -5434,6 +5435,21 @@ get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
return a ? AT_file (a) : NULL;
}
+/* Return TRUE if the language is C or C++. */
+
+static inline bool
+is_c_family (void)
+{
+ unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
+
+ return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
+ || lang == DW_LANG_C11 || lang == DW_LANG_C_plus_plus
+ || lang == DW_LANG_ObjC_plus_plus || lang == DW_LANG_C_plus_plus_11
+ || lang == DW_LANG_C_plus_plus_14);
+
+
+}
+
/* Return TRUE if the language is C++. */
static inline bool
@@ -20918,12 +20934,24 @@ add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
dimension arr(N:*)
Since the debugger is definitely going to need to know N
to produce useful results, go ahead and output the lower
- bound solo, and hope the debugger can cope. */
+ bound solo, and hope the debugger can cope.
+
+ For C and C++, if upper is NULL, this may be a zero-length array
+ or a flexible array; we'd like to be able to distinguish between
+ the two. Set DW_AT_count to 0 for the former. TYPE_SIZE is NULL
+ for the latter. */
if (!get_AT (subrange_die, DW_AT_lower_bound))
add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
- if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
- add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
+ if (!get_AT (subrange_die, DW_AT_upper_bound)
+ && !get_AT (subrange_die, DW_AT_count))
+ {
+ if (upper)
+ add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
+ else if (is_c_family () && TYPE_SIZE (type))
+ add_bound_info (subrange_die, DW_AT_count,
+ build_int_cst (TREE_TYPE (lower), 0), NULL);
+ }
}
/* Otherwise we have an array type with an unspecified length. The