This is an archive of the discontinued LLVM Phabricator instance.

[clang]: Add DeclContext::dumpAsDecl().
ClosedPublic

Authored by tahonermann on Sep 8 2022, 8:06 AM.

Details

Summary

This change enables a declaration to be conveniently displayed within a debugger when only a pointer to its DeclContext is available. For example, in gdb:

(gdb) p Ctx
$1 = (const clang::DeclContext *) 0x14c1a580
(gdb) p Ctx->dumpAsDecl()
ClassTemplateSpecializationDecl 0x14c1a540 <t.cpp:1:1, line:7:1> line:2:8 struct ct
`-TemplateArgument type 'int'
  `-BuiltinType 0x14bac420 'int'
$2 = void

In the event that the pointed to DeclContext is invalid (that it has an invalid DeclKind as a result of a dangling pointer, memory corruption, etc...) it is not possible to dump its associated declaration. In this case, the DeclContext will be reported as invalid. For example, in gdb:

(gdb) p Ctx->dumpAsDecl()
DeclContext 0x14c1a580 <unrecognized Decl kind 127>
$3 = void

Diff Detail

Event Timeline

tahonermann created this revision.Sep 8 2022, 8:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 8 2022, 8:06 AM
tahonermann requested review of this revision.Sep 8 2022, 8:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 8 2022, 8:06 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
erichkeane added inline comments.Sep 8 2022, 8:15 AM
clang/lib/AST/ASTDumper.cpp
233

One thing to note is that the 'else' case here is a little uninformative. See https://clang.llvm.org/doxygen/DeclBase_8cpp_source.html#l00915 for some similar logic here (though not sure we should be emulating that).

More, I wonder if there is SOME message here that should be dumped for 'else'.

aaron.ballman added inline comments.Sep 8 2022, 10:46 AM
clang/lib/AST/ASTDumper.cpp
232
233

Looking at what inherits from DeclContext, is there use of it which is *not* a Decl? I couldn't find a use where it's not also a Decl.

erichkeane added inline comments.Sep 8 2022, 10:48 AM
clang/lib/AST/ASTDumper.cpp
233

I've DEFINITELY run into it in the debugger before, but I have no idea WHAT case that is. It is sometimes just "DeclContext is an invalid pointer" kinda thing, so it might be worth-while to have SOME output besides "print nothing", particularly when debugging.

aaron.ballman added inline comments.Sep 8 2022, 11:13 AM
clang/lib/AST/ASTDumper.cpp
233

IIRC, the case this comes up in is when the object is only partially constructed, and so I agree that having an else clause here would be useful -- because this interface is predominately used from a debugger, it has to deal with special "impossible" situations a bit more carefully.

Nice, thank you for doing this.

shafik added inline comments.Sep 8 2022, 6:19 PM
clang/lib/AST/ASTDumper.cpp
231

Interesting DeclContext::dumpDeclContext() is in DeclPrinter.cpp I wonder why these were split like this.

233

So looking at the other dump member functions, all of them seem to assume we have a valid DeclContext and so they do not have any else either.

So maybe we want to think about making the rest a bit smarter as well?

aaron.ballman added inline comments.Sep 9 2022, 5:12 AM
clang/lib/AST/ASTDumper.cpp
233

So maybe we want to think about making the rest a bit smarter as well?

What do you have in mind?

I think it's plausible to be in a partially constructed state in terms of when ctors are called, but I'm far less worried about people calling a dump method in the middle of the member inits happening.

shafik added inline comments.Sep 9 2022, 10:28 AM
clang/lib/AST/ASTDumper.cpp
233

So maybe we want to think about making the rest a bit smarter as well?

What do you have in mind?

I think it's plausible to be in a partially constructed state in terms of when ctors are called, but I'm far less worried about people calling a dump method in the middle of the member inits happening.

Apologies, what I meant was if we are going to add an else here then we should be consistent and add it in the other dump functions as well.

aaron.ballman added inline comments.Sep 9 2022, 10:37 AM
clang/lib/AST/ASTDumper.cpp
233

None of the other DeclContext dump functions cast to Decl, so I'm not certain there's an else to add to any of them, or am I misunderstanding?

tahonermann updated this revision to Diff 464487.EditedOct 1 2022, 4:39 AM
tahonermann retitled this revision from [clang]: Add DeclContext::dumpDecl() in order to conveniently dump an AST from a DeclContext. to [clang]: Add DeclContext::dumpAsDecl()..
tahonermann edited the summary of this revision. (Show Details)

Addressed review feedback; when presented with an invalid DeclContext, output will now be produced that explains why it is invalid.

tahonermann marked 9 inline comments as done.

Addressed a suggested edit I originally failed to notice.

tahonermann marked an inline comment as done.Oct 1 2022, 4:52 AM
tahonermann added inline comments.
clang/lib/AST/ASTDumper.cpp
231

It looks like the implementation of DeclContext::dumpDeclContext() requires class DeclPrinter to be defined. DeclPrinter is defined in clang/lib/AST/DeclPrinter.cpp in an unnamed namespace.

erichkeane accepted this revision.Oct 3 2022, 6:48 AM

Please see the two comments, otherwise this LGTM. Feel free to fix the below as a part of the commit process.

clang/lib/AST/ASTDumper.cpp
240

This gets a little overly coy for our comments...

250

This can now just be 'cast'

This revision is now accepted and ready to land.Oct 3 2022, 6:48 AM
tahonermann closed this revision.Oct 3 2022, 2:28 PM
tahonermann marked 2 inline comments as done.

Pushed, but I forgot to add the Differential Revision tag to the commit, so closing manually. The changes landed as commit 4247cdb568eca4c31b14d91105fe5ee140225036.

clang/lib/AST/ASTDumper.cpp
240

Aw, I was having fun writing that comment! No problem, I'll sprinkle some professional polishing salts on it.