1// RUN: clang-tidy %s -checks=-*,google-readability-casting -- \ 2// RUN: -xobjective-c++ -fobjc-abi-version=2 -fobjc-arc | count 0 3 4// Note: this test expects no diagnostics, but FileCheck cannot handle that, 5// hence the use of | count 0. 6 7bool g() { return false; } 8 9enum Enum { Enum1 }; 10struct X {}; 11struct Y : public X {}; 12 13void f(int a, double b, const char *cpc, const void *cpv, X *pX) { 14 15 typedef const char *Typedef1; 16 typedef const char *Typedef2; 17 Typedef1 t1; 18 (Typedef2)t1; 19 (const char*)t1; 20 (Typedef1)cpc; 21 22 typedef char Char; 23 char *pc; 24 Char *pChar = (Char*)pc; 25 26 (Char)*cpc; 27 28 (char)*pChar; 29 30 (const char*)cpv; 31 32 char *pc2 = (char*)(cpc + 33); 33 34 const char &crc = *cpc; 35 char &rc = (char&)crc; 36 37 char &rc2 = (char&)*cpc; 38 39 char ** const* const* ppcpcpc; 40 char ****ppppc = (char****)ppcpcpc; 41 42 char ***pppc = (char***)*(ppcpcpc); 43 44 char ***pppc2 = (char***)(*ppcpcpc); 45 46 char *pc5 = (char*)(const char*)(cpv); 47 48 int b1 = (int)b; 49 b1 = (const int&)b; 50 51 b1 = (int) b; 52 53 b1 = (int) b; 54 55 b1 = (int) (b); 56 57 b1 = (int) (b); 58 59 Y *pB = (Y*)pX; 60 Y &rB = (Y&)*pX; 61 62 const char *pc3 = (const char*)cpv; 63 64 char *pc4 = (char*)cpv; 65 66 b1 = (int)Enum1; 67 68 Enum e = (Enum)b1; 69 70 int b2 = int(b); 71 int b3 = static_cast<double>(b); 72 int b4 = b; 73 double aa = a; 74 (void)b2; 75 return (void)g(); 76} 77 78template <typename T> 79void template_function(T t, int n) { 80 int i = (int)t; 81} 82 83template <typename T> 84struct TemplateStruct { 85 void f(T t, int n) { 86 int k = (int)t; 87 } 88}; 89 90void test_templates() { 91 template_function(1, 42); 92 template_function(1.0, 42); 93 TemplateStruct<int>().f(1, 42); 94 TemplateStruct<double>().f(1.0, 42); 95} 96 97extern "C" { 98void extern_c_code(const char *cpc) { 99 char *pc = (char*)cpc; 100} 101} 102 103#define CAST(type, value) (type)(value) 104void macros(double d) { 105 int i = CAST(int, d); 106} 107 108enum E { E1 = 1 }; 109template <E e> 110struct A { 111 // Usage of template argument e = E1 is represented as (E)1 in the AST for 112 // some reason. We have a special treatment of this case to avoid warnings 113 // here. 114 static const E ee = e; 115}; 116struct B : public A<E1> {}; 117 118 119void overloaded_function(); 120void overloaded_function(int); 121 122template<typename Fn> 123void g(Fn fn) { 124 fn(); 125} 126 127void function_casts() { 128 typedef void (*FnPtrVoid)(); 129 typedef void (&FnRefVoid)(); 130 typedef void (&FnRefInt)(int); 131 132 g((void (*)())overloaded_function); 133 g((void (*)())&overloaded_function); 134 g((void (&)())overloaded_function); 135 136 g((FnPtrVoid)overloaded_function); 137 g((FnPtrVoid)&overloaded_function); 138 g((FnRefVoid)overloaded_function); 139 140 FnPtrVoid fn0 = (void (*)())&overloaded_function; 141 FnPtrVoid fn1 = (void (*)())overloaded_function; 142 FnPtrVoid fn1a = (FnPtrVoid)overloaded_function; 143 FnRefInt fn2 = (void (&)(int))overloaded_function; 144 auto fn3 = (void (*)())&overloaded_function; 145 auto fn4 = (void (*)())overloaded_function; 146 auto fn5 = (void (&)(int))overloaded_function; 147 148 void (*fn6)() = (void (*)())&overloaded_function; 149 void (*fn7)() = (void (*)())overloaded_function; 150 void (*fn8)() = (FnPtrVoid)overloaded_function; 151 void (&fn9)(int) = (void (&)(int))overloaded_function; 152 153 void (*correct1)() = static_cast<void (*)()>(overloaded_function); 154 FnPtrVoid correct2 = static_cast<void (*)()>(&overloaded_function); 155 FnRefInt correct3 = static_cast<void (&)(int)>(overloaded_function); 156} 157 158struct S { 159 S(const char *); 160}; 161struct ConvertibleToS { 162 operator S() const; 163}; 164struct ConvertibleToSRef { 165 operator const S&() const; 166}; 167 168void conversions() { 169 //auto s1 = (const S&)""; 170 auto s2 = (S)""; 171 auto s2a = (struct S)""; 172 auto s2b = (const S)""; 173 ConvertibleToS c; 174 auto s3 = (const S&)c; 175 auto s4 = (S)c; 176 ConvertibleToSRef cr; 177 auto s5 = (const S&)cr; 178 auto s6 = (S)cr; 179} 180