3

While iterating the AST I encounter VarDecl objects. These have a type, "QualType". Now I would like to find the location in the original source of these QualTypes. I've heard somewhere that TypeLocBuilder can be used for this but whatever I try it always throws an assert like `T == LastTy && "type doesn't match last type pushed!"'. So the question is: how can I use TypeLocBuilder for this? And if it is not useful for this, what alternative is there?

TypeLoc buildTypeLoc(ASTContext *const c, const QualType qt)
{
       TypeLocBuilder tlb;
       return tlb.getTypeLocInContext(*c, qt);
}

SourceRange getQualTypeSourceRange(ASTContext *const c, const QualType qt)
{
      return buildTypeLoc(c, qt).getSourceRange();
}

for(clang::ASTUnit::top_level_iterator dit = au -> top_level_begin(); dit != au -> top_level_end(); dit++) {
        if (isa<VarDecl>(*dit)) {
                const VarDecl *vd = static_cast<const VarDecl *>(*dit);

                SourceRange sr = getQualTypeSourceRange(&au -> getASTContext(), vd -> getType());
        }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.