Search code examples
c++clanglibtooling

Print a fully qualified type of a parameter (ParmVarDecl) or a field (FieldDecl) with clang Libtooling API


Similar to this question: Print the type of a parameter (ParmVarDecl) with clang API, I want to get parameter/field type as a string, but importantly I want the type to be fully qualified, e.g. for

namespace n {
class A {};
class B {
    void f(std::vector<A> a) {}
    std::vector<A> m_a;
};
} // n

I want to have std::vector<n::A>, not std::vector<A>.

I've tried this solution: http://clang-developers.42468.n3.nabble.com/Getting-the-fully-scoped-type-of-a-function-parameter-td4028221.html (getAsCXXRecordDecl() and getQualifiedNameAsString()), but in both cases decl.getType()->getAsCXXRecordDecl() returns nullptr for me.

Update: Also, this solution How to print fully qualified Expr in clang? doesn't help, because there is no PrintCanonicalTypes in clang 7 I'm using.

I guess I'll have to upgrade to clang 8 and try again.


Solution

  • PrintCanonicalTypes flag does help with printing fully-qualified method/function parameters (in combination with decl.getOriginalType().getCanonicalType().getAsString(...)).

    For fields I've used decltype() of a field name instead of an explicit type, which worked for me (because I didn't need types per se, rather the generated code to compile).