[cfe-dev] How to test QualType equality

Reid Kleckner via cfe-dev cfe-dev at lists.llvm.org
Tue Oct 13 11:55:15 PDT 2020


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".

On Mon, Oct 12, 2020 at 1:27 PM Ray Zhang via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> 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
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20201013/efaac34a/attachment.html>


More information about the cfe-dev mailing list