How to test QualType equality


In writing tests for a clang tool, I couldn’t find a way to compare a QualType parsed from the AST with a programmatically defined (not parsed from the AST) QualType. In particular, I find it hard to create a QualType from scratch, since although QualType has a default constructor, Type subclasses don’t have public constructors. This added flexibility will greatly reduce boilerplate code for my tests. How is this usually done?

Some context:

I’m currently writing some tests for a clang-cast tool that converts C-style casts to C++ style casts. I wanted to test a specific scenario where a reinterpret_cast should be followed by a const_cast, and we havey an intermediate type which doesn’t have the necessary qualifiers. For example:

void f() {

const int * const ptr;
(double*) ptr;
const_cast<double*>(reinterpret_cast<const double*>(ptr));

where the intermediate type above is const double* (it could also be const double* const, depending on where you want the implicit lvalue-to-rvalue conversion to happen). In any case, given the subexpression type (ptr), and the cast type (double*) as QualTypes, I wanted to say something along the lines of:

ASSERT_TRUE(ChangeQualifiers(CastType, SubExpressionType), QualType(“const double*”));

Obviously, this doesn’t exactly work because there is no such constructor for QualType. I can append a VarDecl statement into the above code to match a QualType:

const double* ptr; // match the type here

I just want to make sure there’s no better way to do this.


Use ASTContext factory methods (getFunctionType, etc) to produce new type objects. It will own the memory for them and structurally unique them, so that there will only be one type for “const double”.