summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/google-readability-casting.mm
blob: b987dc2279aa4fed085e8970c42dea9118053193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// RUN: clang-tidy %s -checks=-*,google-readability-casting -- \
// RUN:   -xobjective-c++ -fobjc-abi-version=2 -fobjc-arc | count 0

// Note: this test expects no diagnostics, but FileCheck cannot handle that,
// hence the use of | count 0.

bool g() { return false; }

enum Enum { Enum1 };
struct X {};
struct Y : public X {};

void f(int a, double b, const char *cpc, const void *cpv, X *pX) {

  typedef const char *Typedef1;
  typedef const char *Typedef2;
  Typedef1 t1;
  (Typedef2)t1;
  (const char*)t1;
  (Typedef1)cpc;

  typedef char Char;
  char *pc;
  Char *pChar = (Char*)pc;

  (Char)*cpc;

  (char)*pChar;

  (const char*)cpv;

  char *pc2 = (char*)(cpc + 33);

  const char &crc = *cpc;
  char &rc = (char&)crc;

  char &rc2 = (char&)*cpc;

  char ** const* const* ppcpcpc;
  char ****ppppc = (char****)ppcpcpc;

  char ***pppc = (char***)*(ppcpcpc);

  char ***pppc2 = (char***)(*ppcpcpc);

  char *pc5 = (char*)(const char*)(cpv);

  int b1 = (int)b;
  b1 = (const int&)b;

  b1 = (int) b;

  b1 = (int)         b;

  b1 = (int) (b);

  b1 = (int)         (b);

  Y *pB = (Y*)pX;
  Y &rB = (Y&)*pX;

  const char *pc3 = (const char*)cpv;

  char *pc4 = (char*)cpv;

  b1 = (int)Enum1;

  Enum e = (Enum)b1;

  int b2 = int(b);
  int b3 = static_cast<double>(b);
  int b4 = b;
  double aa = a;
  (void)b2;
  return (void)g();
}

template <typename T>
void template_function(T t, int n) {
  int i = (int)t;
}

template <typename T>
struct TemplateStruct {
  void f(T t, int n) {
    int k = (int)t;
  }
};

void test_templates() {
  template_function(1, 42);
  template_function(1.0, 42);
  TemplateStruct<int>().f(1, 42);
  TemplateStruct<double>().f(1.0, 42);
}

extern "C" {
void extern_c_code(const char *cpc) {
  char *pc = (char*)cpc;
}
}

#define CAST(type, value) (type)(value)
void macros(double d) {
  int i = CAST(int, d);
}

enum E { E1 = 1 };
template <E e>
struct A {
  // Usage of template argument e = E1 is represented as (E)1 in the AST for
  // some reason. We have a special treatment of this case to avoid warnings
  // here.
  static const E ee = e;
};
struct B : public A<E1> {};


void overloaded_function();
void overloaded_function(int);

template<typename Fn>
void g(Fn fn) {
  fn();
}

void function_casts() {
  typedef void (*FnPtrVoid)();
  typedef void (&FnRefVoid)();
  typedef void (&FnRefInt)(int);

  g((void (*)())overloaded_function);
  g((void (*)())&overloaded_function);
  g((void (&)())overloaded_function);

  g((FnPtrVoid)overloaded_function);
  g((FnPtrVoid)&overloaded_function);
  g((FnRefVoid)overloaded_function);

  FnPtrVoid fn0 = (void (*)())&overloaded_function;
  FnPtrVoid fn1 = (void (*)())overloaded_function;
  FnPtrVoid fn1a = (FnPtrVoid)overloaded_function;
  FnRefInt fn2 = (void (&)(int))overloaded_function;
  auto fn3 = (void (*)())&overloaded_function;
  auto fn4 = (void (*)())overloaded_function;
  auto fn5 = (void (&)(int))overloaded_function;

  void (*fn6)() = (void (*)())&overloaded_function;
  void (*fn7)() = (void (*)())overloaded_function;
  void (*fn8)() = (FnPtrVoid)overloaded_function;
  void (&fn9)(int) = (void (&)(int))overloaded_function;

  void (*correct1)() = static_cast<void (*)()>(overloaded_function);
  FnPtrVoid correct2 = static_cast<void (*)()>(&overloaded_function);
  FnRefInt correct3 = static_cast<void (&)(int)>(overloaded_function);
}

struct S {
    S(const char *);
};
struct ConvertibleToS {
  operator S() const;
};
struct ConvertibleToSRef {
  operator const S&() const;
};

void conversions() {
  //auto s1 = (const S&)"";
  auto s2 = (S)"";
  auto s2a = (struct S)"";
  auto s2b = (const S)"";
  ConvertibleToS c;
  auto s3 = (const S&)c;
  auto s4 = (S)c;
  ConvertibleToSRef cr;
  auto s5 = (const S&)cr;
  auto s6 = (S)cr;
}
OpenPOWER on IntegriCloud