1// RUN: %check_clang_tidy %s darwin-dispatch-once-nonstatic %t
2
3typedef int dispatch_once_t;
4extern void dispatch_once(dispatch_once_t *pred, void(^block)(void));
5
6
7void bad_dispatch_once(dispatch_once_t once, void(^block)(void)) {}
8// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: dispatch_once_t variables must have static or global storage duration; function parameters should be pointer references [darwin-dispatch-once-nonstatic]
9
10// file-scope dispatch_once_ts have static storage duration.
11dispatch_once_t global_once;
12static dispatch_once_t file_static_once;
13namespace {
14dispatch_once_t anonymous_once;
15} // end anonymous namespace
16
17int Correct(void) {
18 static int value;
19 static dispatch_once_t once;
20 dispatch_once(pred: &once, block: ^{
21 value = 1;
22 });
23 return value;
24}
25
26int Incorrect(void) {
27 static int value;
28 dispatch_once_t once;
29 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: dispatch_once_t variables must have static or global storage duration [darwin-dispatch-once-nonstatic]
30 // CHECK-FIXES: static dispatch_once_t once;
31 dispatch_once(pred: &once, block: ^{
32 value = 1;
33 });
34 return value;
35}
36
37struct OnceStruct {
38 static dispatch_once_t staticOnce; // Allowed
39 int value;
40 dispatch_once_t once; // Allowed (at this time)
41};
42
43@interface MyObject {
44 dispatch_once_t _once;
45 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: dispatch_once_t variables must have static or global storage duration and cannot be Objective-C instance variables [darwin-dispatch-once-nonstatic]
46 // CHECK-FIXES: dispatch_once_t _once;
47}
48@end
49

source code of clang-tools-extra/test/clang-tidy/checkers/darwin/dispatch-once-nonstatic.mm