How to test QualType equality

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to test QualType equality

Manas via cfe-dev
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 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
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to test QualType equality

Manas via cfe-dev
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 <[hidden email]> 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 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
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev