c++parsingclangllvmabstract-syntax-tree

Getting the under type using getUnqualifiedDesugaredType() function from CLANG


I have this code in my header file.h:

using UWORD = uint16_t;
UWORD var;

And what I wanted to get is this "uint16_t".

Therefore I wrote this code using CLANG:

std::string qualTypeToUnderTypeStr(const clang::QualType& qualType, const clang::ASTContext& context)
{
    clang::Type const* underType = qualType.getTypePtr()->getUnqualifiedDesugaredType();
    std::string output;
    llvm::raw_string_ostream stream(output);
    clang::PrintingPolicy printingPolicy(context.getLangOpts());
    clang::QualType::print(underType, clang::Qualifiers(), stream, printingPolicy, llvm::Twine());

    return output;
}

which returns "unsigned short" instead of "uint16_t". Any solutions?


Solution

  • Update: The answer lies in this function in QualType class:

    auto baseType = qualType.getSingleStepDesugaredType(context).getTypePtr();
    

    This will return the type before the last "sugar".