Remove CV type qualifiers from libclang's CXType

Viewed 258

I use libclang to parse source file and get reference to some type as CXType, say it is "const std::__1::basic_string<char>" (as reported by clang_getTypeSpelling). How can I get reference to same type but without const qualifier?

1 Answers

I was able to do this by visiting the type's cursor's children. For example, given a CXCursor 'cursor',

CXType type = clang_getCursorType(cursor);
if (clang_isConstQualifiedType(type))
{
    auto RemoveConstFromType = [](CXCursor c, CXCursor, CXClientData d)
    {
        *(CXType*)d = clang_getCursorType(c);
        return (clang_isConstQualifiedType(*(CXType*)d) ? CXChildVisit_Recurse : CXChildVisit_Break);
    };
    clang_visitChildren(cursor, RemoveConstFromType, &type);
}

I hope that helps. =)

Related