API to check if a decl is a pointer

Hi All,

Is there an API that allows one to check if a DeclRefExpr or its corresponding VarDecl is of a pointer type? For example:

int *pI; // yes – a pointer type
int i; // no.

I could use a string based check – getting the type name as string and then searching for ‘*’ in it, but I believe there must be a cleaner and robust way to do so.

Thanks!

Hi All,

Is there an API that allows one to check if a DeclRefExpr or its
corresponding VarDecl is of a pointer type? For example:

int *pI; // yes -- a pointer type
int i; // no.

I could use a string based check -- getting the type name as string and then
searching for '*' in it, but I believe there must be a cleaner and robust
way to do so.

You can call getType() on the VarDecl (because it inherits from
ValueDecl), and then check isPointerType() on the resulting QualType
object (it wraps a Type *). e.g.,

QualType QT = YourVarDecl->getType();
if (QT->isPointerType()) {

}

~Aaron

Thanks Aaron!

For anyone else who might want to do this, I believe the API has changed slightly. I think as per the new API, one needs to do:

bool isPtr = VD->getType().getTypePtr()->isPointerType();

For anyone else who might want to do this, I believe the API has changed
slightly. I think as per the new API, one needs to do:

bool isPtr = VD->getType().getTypePtr()->isPointerType();

You should not have to do this -- QualType wraps a Type * and
overloads operator-> so that you can call Type functions on a QualType
with -> (and QualType methods through .).

~Aaron

Ah okay! Thanks for pointing that out.