[clang]: Add DeclContext::dumpAsDecl().
authorTom Honermann <tom.honermann@intel.com>
Sat, 1 Oct 2022 11:06:58 +0000 (04:06 -0700)
committerTom Honermann <tom@honermann.net>
Mon, 3 Oct 2022 21:25:44 +0000 (17:25 -0400)
commit4247cdb568eca4c31b14d91105fe5ee140225036
treeedc24549c12d4ad47e9f06300ca58f537e34fbbf
parent69a6417406a1b0316a1fa6aeb63339d0e1d2abbd
[clang]: Add DeclContext::dumpAsDecl().

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
clang/include/clang/AST/ASTDumper.h
clang/include/clang/AST/DeclBase.h
clang/lib/AST/ASTDumper.cpp
clang/lib/AST/DeclBase.cpp