Bug 86985 - Generated DWARF does not distinguish between zero-length and flexible array types
Summary: Generated DWARF does not distinguish between zero-length and flexible array t...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: debug (show other bugs)
Version: unknown
: P3 normal
Target Milestone: 9.0
Assignee: Not yet assigned to anyone
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2018-08-17 04:20 UTC by Omar Sandoval
Modified: 2018-09-13 10:29 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-08-22 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Omar Sandoval 2018-08-17 04:20:12 UTC
It is currently impossible for a debugger to distinguish between a zero-length
and a flexible array given the debug info generated by GCC:

$ cat zero_length.c
struct {
        int foo;
        int bar[0];
} baz;
$ cat flexible.c
struct {
        int foo;
        int bar[];
} baz;
$ gcc -g -c zero_length.c
$ gcc -g -c flexible.c
$ gdb -batch -ex 'ptype baz' zero_length.o
type = struct {
    int foo;
    int bar[];
}
$ gdb -batch -ex 'ptype baz' flexible.o
type = struct {
    int foo;
    int bar[];
}

However, when compiling with clang:

$ clang -g -c zero_length.c
$ clang -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 distinction would be useful for certain tools. For example, I have a tool
which feeds the structure definitions from debug info back into a C compiler,
but this fails in some cases where zero-length and flexible array types are not
interchangable.
Comment 1 Omar Sandoval 2018-08-17 04:40:59 UTC
Posted a patch here: https://gcc.gnu.org/ml/gcc-patches/2018-08/msg00994.html.
Comment 2 Andrew Pinski 2018-08-17 05:16:35 UTC
Considering GCC considers zero length array as the same as flexible array types, I don't think this is really an issue.  

>this fails in some cases where zero-length and flexible array types are not
interchangable.

There should never be a possiblity of this anyways.

NOTE zero-length array types are a GCC extension anyways.
Comment 3 Omar Sandoval 2018-08-17 05:26:05 UTC
They are not equivalent in GCC, see https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html. In particular, flexible arrays cannot be the only member in a struct, cannot be a member of a union, cannot be used as a dimension of a multidimensional array, and can have a static initializer with elements; the opposite is true for zero length arrays.
Comment 4 Richard Biener 2018-08-22 08:43:17 UTC
Confirmed.
Comment 6 Tom de Vries 2018-09-13 10:27:44 UTC
Author: vries
Date: Thu Sep 13 10:27:12 2018
New Revision: 264267

URL: https://gcc.gnu.org/viewcvs?rev=264267&root=gcc&view=rev
Log:
[debug] DWARF: add DW_AT_count to zero-length arrays

2018-09-13  Omar Sandoval  <osandov@osandov.com>
	    Tom de Vries  <tdevries@suse.de>

	PR debug/86985
	* dwarf2out.c (is_c): New function.
	(add_subscript_info): Add DW_AT_count of 0 for C zero-length arrays.

	* gcc.dg/guality/zero-length-array.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/guality/zero-length-array.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/dwarf2out.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Tom de Vries 2018-09-13 10:29:12 UTC
Patch with test-case committed, marking resolved-fixed.