[cfe-dev] How to test QualType equality

Ray Zhang via cfe-dev cfe-dev at lists.llvm.org
Mon Oct 12 13:26:57 PDT 2020


Hi,

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
<https://timsong-cpp.github.io/cppwp/n4140/conv#lval> 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.

Best,
Ray
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20201012/eb212be8/attachment.html>


More information about the cfe-dev mailing list