xref: /aosp_15_r20/external/clang/test/SemaCXX/cast-conversion.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown -verify %s -std=c++11
2*67e74705SXin Li 
3*67e74705SXin Li struct R {
4*67e74705SXin Li   R(int);
5*67e74705SXin Li };
6*67e74705SXin Li 
7*67e74705SXin Li struct A {
8*67e74705SXin Li   A(R);
9*67e74705SXin Li };
10*67e74705SXin Li 
11*67e74705SXin Li struct B { // expected-note 3 {{candidate constructor (the implicit copy constructor) not viable}} \
12*67e74705SXin Li               expected-note 3 {{candidate constructor (the implicit move constructor) not viable}}
13*67e74705SXin Li   B(A); // expected-note 3 {{candidate constructor not viable}}
14*67e74705SXin Li };
15*67e74705SXin Li 
main()16*67e74705SXin Li int main () {
17*67e74705SXin Li   B(10);	// expected-error {{no matching conversion for functional-style cast from 'int' to 'B'}}
18*67e74705SXin Li   (B)10;	// expected-error {{no matching conversion for C-style cast from 'int' to 'B'}}
19*67e74705SXin Li   static_cast<B>(10);	// expected-error {{no matching conversion for static_cast from 'int' to 'B'}}
20*67e74705SXin Li }
21*67e74705SXin Li 
22*67e74705SXin Li template<class T>
23*67e74705SXin Li struct X0 {
24*67e74705SXin Li   X0(const T &);
25*67e74705SXin Li };
26*67e74705SXin Li 
27*67e74705SXin Li template<class T>
make_X0(const T & Val)28*67e74705SXin Li X0<T> make_X0(const T &Val) {
29*67e74705SXin Li   return X0<T>(Val);
30*67e74705SXin Li }
31*67e74705SXin Li 
test_X0()32*67e74705SXin Li void test_X0() {
33*67e74705SXin Li   const char array[2] = { 'a', 'b' };
34*67e74705SXin Li   make_X0(array);
35*67e74705SXin Li }
36*67e74705SXin Li 
37*67e74705SXin Li // PR5210 recovery
38*67e74705SXin Li class C {
39*67e74705SXin Li protected:
40*67e74705SXin Li   template <int> float* &f0(); // expected-note{{candidate}}
41*67e74705SXin Li   template <unsigned> float* &f0(); // expected-note{{candidate}}
42*67e74705SXin Li 
f1()43*67e74705SXin Li   void f1() {
44*67e74705SXin Li     static_cast<float*>(f0<0>()); // expected-error{{ambiguous}}
45*67e74705SXin Li   }
46*67e74705SXin Li };
47*67e74705SXin Li 
intToPointer1(short s)48*67e74705SXin Li void *intToPointer1(short s) {
49*67e74705SXin Li   return (void*)s; // expected-warning{{cast to 'void *' from smaller integer type 'short'}}
50*67e74705SXin Li }
51*67e74705SXin Li 
intToPointer2(short s)52*67e74705SXin Li void *intToPointer2(short s) {
53*67e74705SXin Li   return reinterpret_cast<void*>(s);
54*67e74705SXin Li }
55*67e74705SXin Li 
intToPointer3(bool b)56*67e74705SXin Li void *intToPointer3(bool b) {
57*67e74705SXin Li   return (void*)b;
58*67e74705SXin Li }
59*67e74705SXin Li 
intToPointer4()60*67e74705SXin Li void *intToPointer4() {
61*67e74705SXin Li   return (void*)(3 + 7);
62*67e74705SXin Li }
63*67e74705SXin Li 
intToPointer5(long l)64*67e74705SXin Li void *intToPointer5(long l) {
65*67e74705SXin Li   return (void*)l;
66*67e74705SXin Li }
67*67e74705SXin Li 
68*67e74705SXin Li struct AmbiguousCast {
69*67e74705SXin Li   operator int(); // expected-note {{candidate function}}
70*67e74705SXin Li   operator unsigned int(); // expected-note {{candidate function}}
71*67e74705SXin Li };
AmbiguousCastFunc(AmbiguousCast & a)72*67e74705SXin Li long long AmbiguousCastFunc(AmbiguousCast& a) {
73*67e74705SXin Li   return static_cast<long long>(a); // expected-error {{ambiguous conversion for static_cast from 'AmbiguousCast' to 'long long'}}
74*67e74705SXin Li }
75*67e74705SXin Li 
76*67e74705SXin Li namespace PR16680 {
77*67e74705SXin Li   void f(int (*__pf)());
g()78*67e74705SXin Li   int g() {
79*67e74705SXin Li     f(reinterpret_cast<int>(0.0f)); // expected-error {{reinterpret_cast from 'float' to 'int' is not allowed}}
80*67e74705SXin Li   }
81*67e74705SXin Li }
82