Debug info compileunit metadata strangeness..

Hi,

I have a question regarding the metadata for compileunit debug info. I
find a few things in it a bit strange, but maybe there it is a reason
for it to be that way that I just don't understand (but if that is the
case I guess the documentation needs to be clearer).

Consider this C program: "int X;"

Compiled with "clang -g" it debug metadata along these lines:

!llvm.dbg.cu = !{!0}
!0 = metadata !{i32 786449, .....
                 metadata !1, ;; List of enums types
                 metadata !1, ;; List of retained types
                 metadata !1, ;; List of subprograms
                 metadata !3 ;; List of global variables
                } ; [ DW_TAG_compile_unit ]
!1 = metadata !{metadata !2}
!2 = metadata !{i32 0}
!3 = metadata !{metadata !4}
!4 = metadata !{metadata !5}
!5 = metadata !{i32 786484, ...} ; [ DW_TAG_variable ]

Documentation says "List of global variables", but it is built as a
"One element list containing the list of global variables". Is there a
reason it is built that way?

Looking at DIBuilder::createCompileUnit it does indeed build it with
an extra indirection.

Another thing I find strange is the empty lists, such as "List of
enums types" in my example above. They apparently end up being
one-element lists containing "i32 0". I would expect that the
reference to the empty lists would be "null".

In other words, I'd expect it to look like this:

!llvm.dbg.cu = !{!0}

!0 = metadata !{i32 786449, ...
                 null, ;; List of enums types
                 null, ;; List of retained types
                 null, ;; List of subprograms
                 metadata !1 ;; List of global variables
                } ; [ DW_TAG_compile_unit ]
!1 = metadata !{metadata !2}
!2 = metadata !{i32 786484, ...} ; [ DW_TAG_variable ]

I attached two patches that changes compileunit metadata to have the
layout I think makes sense. I do not expect these patches to be
applied (especially the second one is just a gross hack), I'm
attaching them because maybe they may help show what I'm trying to say
(a patch says more than 1000 words?).

anders

0001-No-indirection-of-in-compileunit-debuginfo.patch (1.93 KB)

0002-hack-make-empty-lists-null-in-compileunit-debuginfo.patch (2.92 KB)

You’re right. This is weird. I haven’t seen a reason why it’s that way.

-eric