llvm-project/clang/test/SemaOpenCLCXX/reinterpret-cast.clcpp
Ole Strohm 85255a04e5 [C++][Sema] Ignore top-level qualifiers in casts
Ignore top-level qualifiers in casts, which fixes issues in reinterpret_cast.

This rule comes from [expr.type]/8.2.2 which explains that casting to a
pr-qualified type should actually cast to the unqualified type. In C++
this is only done for types that aren't classes or arrays.

Fixes: PR49221

Reviewed By: Anastasia

Differential Revision: https://reviews.llvm.org/D102689
2021-07-05 12:22:08 +01:00

38 lines
1.6 KiB
Plaintext

// RUN: %clang_cc1 %s -pedantic -verify -fsyntax-only
typedef int int2 __attribute__((ext_vector_type(2)));
typedef int int3 __attribute__((ext_vector_type(3)));
typedef int int4 __attribute__((ext_vector_type(4)));
struct X {};
__global int g = 0;
__global int *__global g_ptr = &g;
kernel void foo() {
// Testing conversions between vectors and vectors/scalars
long l1;
auto l_to_i2 = reinterpret_cast<int2>(l1);
int2 i2;
auto i2_to_l = reinterpret_cast<long>(i2);
auto i2_to_i = reinterpret_cast<int>(i2); // expected-error{{reinterpret_cast from vector 'int2' (vector of 2 'int' values) to scalar 'int' of different size}}
auto i2_to_i2 = reinterpret_cast<int2>(i2);
// Testing reinterpret_cast with address spaces.
__private short s;
auto s2 = reinterpret_cast<__private short>(s);
auto s3 = reinterpret_cast<decltype(s)>(s);
auto s4 = reinterpret_cast<__global short>(s);
__private X x;
auto x2 = reinterpret_cast<__private X>(x); // expected-error{{reinterpret_cast from '__private X' to '__private X' is not allowed}}
auto ptr = reinterpret_cast<__global int* __private>(g_ptr);
(void)reinterpret_cast<__private int* __private>(g_ptr); // expected-error{{reinterpret_cast from '__global int *' to '__private int *' is not allowed}}
// Only integral types (and pointer/references) can be reinterpret casted to themselves.
// Currently this does not include any opencl types.
reserve_id_t r_id1;
auto r_id2 = reinterpret_cast<reserve_id_t>(r_id1); // expected-error{{reinterpret_cast from 'reserve_id_t' to 'reserve_id_t' is not allowed}}
}