Search code examples
c++clangllvmlibtooling

How to get the complete type as string with namespace info (if any) from the Decl/ParmDecl


I am trying to extract all the function signatures, essentially parameters complete type, with namespace from a c/cpp source. The objective is to create a file in which I can decleare the same type variable, without opening the namespace in which they are and to be able to compile that file successfully.

I've objects of Type* from ParamVarDecl* using RecursiveASTVisitor, in very simple cases I can get the type as string, which is fine but in this case-

//signature.cpp
//#includes...

using namespace std;

    void func(size_t pin) {
        //some def.
    }

dumping Type* obtained from, paramDecl->getType().getTypePtrOrNull() I get the AST as,

ElaboratedType 0x1c06b378df0 'size_t' sugar
`-TypedefType 0x1c06b378dc0 'size_t' sugar
  |-Typedef 0x1c06b378b28 'size_t'
  `-BuiltinType 0x1c0698845e0 'unsigned long long'

In this AST there is no info of std::, How do I get this info?

I can extract 'unsigned long long' and use that, but I am specifically looking for a way to get the namspaces in such cases.

Many thanks.


Solution

  • Thanks for valuable comments. I can use size_t instead of std::size_t to decleare it in another file, since both size_t and std::size_t are typedefs in C++.