File: static-accessed-through-instance.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (365 lines) | stat: -rw-r--r-- 8,495 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// RUN: %check_clang_tidy %s readability-static-accessed-through-instance %t -- -- -isystem %S/Inputs/static-accessed-through-instance
#include <__clang_cuda_builtin_vars.h>

enum OutEnum {
  E0,
};

struct C {
  static void foo();
  static int x;
  int nsx;
  enum {
    Anonymous,
  };
  enum E {
    E1,
  };
  using enum OutEnum;
  void mf() {
    (void)&x;    // OK, x is accessed inside the struct.
    (void)&C::x; // OK, x is accessed using a qualified-id.
    foo();       // OK, foo() is accessed inside the struct.
  }
  void ns() const;
};

int C::x = 0;

struct CC {
  void foo();
  int x;
};

template <typename T> struct CT {
  static T foo();
  static T x;
  int nsx;
  void mf() {
    (void)&x;    // OK, x is accessed inside the struct.
    (void)&C::x; // OK, x is accessed using a qualified-id.
    foo();       // OK, foo() is accessed inside the struct.
  }
};

// Expressions with side effects
C &f(int, int, int, int);
void g() {
  f(1, 2, 3, 4).x;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member accessed through instance  [readability-static-accessed-through-instance]
  // CHECK-FIXES: {{^}}  f(1, 2, 3, 4).x;{{$}}
}

int i(int &);
void j(int);
C h();
bool a();
int k(bool);

void f(C c) {
  j(i(h().x));
  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: static member
  // CHECK-FIXES: {{^}}  j(i(h().x));{{$}}

  // The execution of h() depends on the return value of a().
  j(k(a() && h().x));
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: static member
  // CHECK-FIXES: {{^}}  j(k(a() && h().x));{{$}}

  if ([c]() {
        c.ns();
        return c;
      }().x == 15)
    ;
  // CHECK-MESSAGES: :[[@LINE-5]]:7: warning: static member
  // CHECK-FIXES: {{^}}  if ([c]() {{{$}}
}

// Nested specifiers
namespace N {
struct V {
  static int v;
  struct T {
    static int t;
    struct U {
      static int u;
    };
  };
};
}

void f(N::V::T::U u) {
  N::V v;
  v.v = 12;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  N::V::v = 12;{{$}}

  N::V::T w;
  w.t = 12;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  N::V::T::t = 12;{{$}}

  // u.u is not changed to N::V::T::U::u; because the nesting level is over 3.
  u.u = 12;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  u.u = 12;{{$}}

  using B = N::V::T::U;
  B b;
  b.u;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  B::u;{{$}}
}

// Templates
template <typename T> T CT<T>::x;

template <typename T> struct CCT {
  T foo();
  T x;
};

typedef C D;

using E = D;

#define FOO(c) c.foo()
#define X(c) c.x

template <typename T> void f(T t, C c) {
  t.x; // OK, t is a template parameter.
  c.x;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  C::x;{{$}}
}

template <int N> struct S { static int x; };

template <> struct S<0> { int x; };

template <int N> void h() {
  S<N> sN;
  sN.x; // OK, value of N affects whether x is static or not.

  S<2> s2;
  s2.x;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  S<2>::x;{{$}}
}

void static_through_instance() {
  C *c1 = new C();
  c1->foo(); // 1
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  C::foo(); // 1{{$}}
  c1->x; // 2
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  C::x; // 2{{$}}
  c1->Anonymous; // 3
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  C::Anonymous; // 3{{$}}
  c1->E1; // 4
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  C::E1; // 4{{$}}
  c1->E0; // 5
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  C::E0; // 5{{$}}

  c1->nsx; // OK, nsx is a non-static member.

  const C *c2 = new C();
  c2->foo(); // 2
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  C::foo(); // 2{{$}}

  C::foo(); // OK, foo() is accessed using a qualified-id.
  C::x;     // OK, x is accessed using a qualified-id.

  D d;
  d.foo();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  D::foo();{{$}}
  d.x;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  D::x;{{$}}

  E e;
  e.foo();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  E::foo();{{$}}
  e.x;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  E::x;{{$}}

  CC *cc = new CC;

  f(*c1, *c1);
  f(*cc, *c1);

  // Macros: OK, macros are not checked.
  FOO((*c1));
  X((*c1));
  FOO((*cc));
  X((*cc));

  // Templates
  CT<int> ct;
  ct.foo();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  CT<int>::foo();{{$}}
  ct.x;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  CT<int>::x;{{$}}
  ct.nsx; // OK, nsx is a non-static member

  CCT<int> cct;
  cct.foo(); // OK, CCT has no static members.
  cct.x;     // OK, CCT has no static members.

  h<4>();
}

struct SP {
  static int I;
} P;

void usep() {
  P.I;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  SP::I;{{$}}
}

namespace NSP {
struct SP {
  static int I;
} P;
} // namespace NSP

void usensp() {
  NSP::P.I;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  NSP::SP::I;{{$}}
}

// Overloaded member access operator
struct Q {
  static int K;
  int y = 0;
};

int Q::K = 0;

struct Qptr {
  Q *q;

  explicit Qptr(Q *qq) : q(qq) {}

  Q *operator->() {
    ++q->y;
    return q;
  }
};

int func(Qptr qp) {
  qp->y = 10; // OK, the overloaded operator might have side-effects.
  qp->K = 10; //
}

namespace {
  struct Anonymous {
    static int I;
  };
}

void use_anonymous() {
  Anonymous Anon;
  Anon.I;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  Anonymous::I;{{$}}
}

namespace Outer {
  inline namespace Inline {
  struct S {
    static int I;
  };
  }
}

void use_inline() {
  Outer::S V;
  V.I;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
  // CHECK-FIXES: {{^}}  Outer::S::I;{{$}}
}

// https://bugs.llvm.org/show_bug.cgi?id=48758
namespace Bugzilla_48758 {

unsigned int x1 = threadIdx.x;
// CHECK-MESSAGES-NOT: :[[@LINE-1]]:10: warning: static member
unsigned int x2 = blockIdx.x;
// CHECK-MESSAGES-NOT: :[[@LINE-1]]:10: warning: static member
unsigned int x3 = blockDim.x;
// CHECK-MESSAGES-NOT: :[[@LINE-1]]:10: warning: static member
unsigned int x4 = gridDim.x;
// CHECK-MESSAGES-NOT: :[[@LINE-1]]:10: warning: static member

} // namespace Bugzilla_48758

// https://github.com/llvm/llvm-project/issues/61736
namespace llvm_issue_61736
{

struct {
  static void f() {}
} AnonStruct, *AnonStructPointer;

class {
  public:
  static void f() {}
} AnonClass, *AnonClassPointer;

void testAnonymousStructAndClass() {
  AnonStruct.f();
  AnonStructPointer->f();

  AnonClass.f();
  AnonClassPointer->f();
}

struct Embedded {
  struct {
    static void f() {}
  } static EmbeddedStruct, *EmbeddedStructPointer;

  class {
    public:
      static void f() {}
  } static EmbeddedClass, *EmbeddedClassPointer;
};

void testEmbeddedAnonymousStructAndClass() {
  Embedded::EmbeddedStruct.f();
  Embedded::EmbeddedStructPointer->f();

  Embedded::EmbeddedClass.f();
  Embedded::EmbeddedClassPointer->f();

  Embedded E;
  E.EmbeddedStruct.f();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member accessed through instance [readability-static-accessed-through-instance]
  // CHECK-FIXES: {{^}}  llvm_issue_61736::Embedded::EmbeddedStruct.f();{{$}}
  E.EmbeddedStructPointer->f();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member accessed through instance [readability-static-accessed-through-instance]
  // CHECK-FIXES: {{^}}  llvm_issue_61736::Embedded::EmbeddedStructPointer->f();{{$}}

  E.EmbeddedClass.f();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member accessed through instance [readability-static-accessed-through-instance]
  // CHECK-FIXES: {{^}}  llvm_issue_61736::Embedded::EmbeddedClass.f();{{$}}
  E.EmbeddedClassPointer->f();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member accessed through instance [readability-static-accessed-through-instance]
  // CHECK-FIXES: {{^}}  llvm_issue_61736::Embedded::EmbeddedClassPointer->f();{{$}}
}

} // namespace llvm_issue_61736