[cfe-dev] contributing a library of Clang debugging helpers

Zhanyong Wan (λx.x x) wan at google.com
Fri Feb 4 10:34:35 PST 2011


Hi,

I wrote a small library to help me learn and debug Clang's AST
representation.  The idea is to link some convenient helpers into a
Clang-based tool (or perhaps Clang itself), and in GDB we can call
such helpers to more easily inspect various values.

Here are some examples of what it lets you do in GDB:

- To print a Clang value x, use command

  (gdb) p Print(x)

without worrying about what x's type is.  x can be a temporary object
(e.g. one returned by a function by value).

- GDB doesn't allow you to invoke a method on a temporary object or
pass it to a function that takes the argument by reference.  Therefore

  (gdb) p D->getType().getTypePointer()

doesn't work (getType() returns a QualType by value).  Instead, you
can the Ref() helper and write

  (gdb) p Ref(D->getType()).getTypePointer()

- GDB doesn't let you call dyn_cast<> (at least I don't know how).  To
convert a Stmt*, Type*, or Decl* to a pointer of a more specific type
Foo, use DynCastFoo().  For example:

  (gdb) p D->getDeclKindName()
  0x... "TranslationUnit"
  (gdb) p DynCastTranslationUnitDecl(D)
  (const clang::TranslationUnitDecl *) 0x...

Is it a good idea to add this library to the Clang codebase?  If yes,
where would be a good place?  Thanks,

-- 
Zhanyong



More information about the cfe-dev mailing list