[DebugInfo][DIBuilder] Good way to pass arguments to createClassType/createMemberType

I’m trying to use DIBuilder to build my debug info and export it into a PDB, but I don’t understand the way some DIBuilder method work.
My question might have a straightforward answer but I don’t see it … (and I can’t find any example code using these methods anywhere).
Indeed createClassType takes an “Elements” argument, and createMemberType takes a “Scope” argument. Like the “who’s first, egg or hen”, I don’t know If I need to create my members first using le compilation unit as the scope, put them in an array and give them to createClassType Elements parameter. Or forget the Elements parameter, call createClassType first then give it as “Scope” of createMemberType…
Thanks !

A good rough rule of thumb here is “what would/does Clang do?” & it looks (to me, on a very cursory glance) like Clang mostly passes the unit’s DIFile as the scope parameter for a member - so perhaps it’s just ignored? not sure.

At least for generating DWARF the scope of of a composite type member should be irrelevant since it will always be nested inside its parent.

– adrian

Commentary in CGDebugInfo::CreateTypeDefinition says it first creates a forward declaration for the record, then adds the members to it using the forward declaration as the scope; then it does some sort of fixup. It looks like the fixup part is MDNode::replaceWithPermanent which is admittedly not a particularly smooth interface.

Adrian or David might be able to comment on that part.


Thanks all, indeed it seems that the scope is not mandatory for fields and other class members, the DINodeArray Elements seems to be the rule to follow.

As a side note, this implementation of DebugInfo based on dynamic MetaData structures and opaque construction is very hard to understand, read and debug…
Each time you look at a DIXxx variable in the debugger, you just have opaque variables even with the natvis…
That would be nice to be able to explore the whole DI hierarchy when exploring a DINode, for example :
-DICompositeType "class Foo

  • DISubprogram “method0”
  • DISubprogram “method1”
  • DIDerivedType "field0
    To at least know if you are doing the right thing.

For example I have another issue with template arguments I want to give to createClassType.
The parameter type for TemplateArgs is MDNode … ? What should I do with that ? How can I understand what I need to give it except by exploring CGDebugInfo code ? (which does not use it btw)
This is not intuitive.
The documentation is just only repeating what the code already tells, without giving any hint on how to use the functions.
It’s like “try your luck and prey it works as you imagined”.
For template arguments I tried giving the class as the scope of a TemplateTypeParameter, I tried to create a MDTuple with my parameters and give it to the createClassType, nothing works.
I get out of ideas here.
In Clang it uses a “replaceArrays” somewhere to put template arguments after a code that does replacements everywhere.
It looks like Clang never uses createClassType but always create replaceable composite types (I will try this, but that would be
very bad news for the design that it works using replacements while not working with the straightforward way…)
And the replaceArrays takes a TemplateParameterArray, why not a MDNode like createClassType ? (or the other way)

I’m sorry If I’m a bit critical here, but it is very hard to work by “hoping it is what i should have done”…

Anyway, If someone can help me with this template parameter thing it would be very appreciated :))
Thank you !

I know that this is exactly the opposite of what you are asking, but from a practical perspective I highly recommend to just write the smallest possible program that would produce the kind of debug info you want to generate and compile it with "clang -g -S -emit-llvm -o -" and see how Clang does it.

-- adrian