libclang: Spelling on typedefs lacks namespaces


I'm using libclang to parse header files and generate code from them. I found that clang_getTypeSpelling() usually includes the namespace(s) a type was declared in. However with the exception being typedefs (and same for "using A = B"). Not sure if this is a bug or intended behavior, but it seems at least inconsistent. I also couldn't really find a good workaround for this. I'd have to manually figure out all typedefs (not just pure typedefs, they could also be nested template arguments or whatever) and then their originating namespaces. This is a bit cumbersome and not really straight forward. Getting the canonical type is also not really a solution as I would like to keep the typedefs.

Minimal example:

namespace foo {
class Bar {
typedef Bar BarDef;

clang_getTypeSpelling on "Bar" (kind "Record") gives: "foo::Bar"
clang_getTypeSpelling on "BarDef" (kind "Typedef") gives: "BarDef" (<== missing "foo::")

I had a look into the clang sources. It seems this little patch would fix the problem. This was pure guess work, I don't know what deeper consequences this might have.

Index: lib/AST/TypePrinter.cpp

Hi Michael,

Thanks for the detailed report! I believe you’re correct, clang_getTypeSpelling should return the fully qualified name for the typedefs since it does so for the other types.

It looks like you’re on the right track with the diff that you’ve shown. I would encourage you to submit it as a patch (with tests) to cfe-commits, either by using Phabricator ( or by emailing the patch directly to the list.


Cool, sounds good. It's a few more tests to fix than expected, but stay tuned for a patch.



Please see