summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/checkers/bugprone-posix-return.cpp
blob: 271893c70706950efdad387d78726de9f4129521 (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
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
// RUN: %check_clang_tidy %s bugprone-posix-return %t

#define NULL nullptr
#define ZERO 0
#define NEGATIVE_ONE -1

typedef int pid_t;
typedef long off_t;
typedef decltype(sizeof(int)) size_t;
typedef struct __posix_spawn_file_actions* posix_spawn_file_actions_t;
typedef struct __posix_spawnattr* posix_spawnattr_t;
# define __CPU_SETSIZE 1024
# define __NCPUBITS (8 * sizeof (__cpu_mask))
typedef unsigned long int __cpu_mask;
typedef struct
{
  __cpu_mask __bits[__CPU_SETSIZE / __NCPUBITS];
} cpu_set_t;
typedef struct _opaque_pthread_t *__darwin_pthread_t;
typedef __darwin_pthread_t pthread_t;
typedef struct pthread_attr_t_ *pthread_attr_t;

extern "C" int posix_fadvise(int fd, off_t offset, off_t len, int advice);
extern "C" int posix_fallocate(int fd, off_t offset, off_t len);
extern "C" int posix_madvise(void *addr, size_t len, int advice);
extern "C" int posix_memalign(void **memptr, size_t alignment, size_t size);
extern "C" int posix_openpt(int flags);
extern "C" int posix_spawn(pid_t *pid, const char *path,
                const posix_spawn_file_actions_t *file_actions,
                const posix_spawnattr_t *attrp,
                char *const argv[], char *const envp[]);
extern "C" int posix_spawnp(pid_t *pid, const char *file,
                 const posix_spawn_file_actions_t *file_actions,
                 const posix_spawnattr_t *attrp,
                 char *const argv[], char *const envp[]);
extern "C" int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
extern "C" int pthread_attr_setaffinity_np(pthread_attr_t *attr, size_t cpusetsize, const cpu_set_t *cpuset);
extern "C" int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
extern "C" int pthread_attr_init(pthread_attr_t *attr);
extern "C" int pthread_yield(void);


void warningLessThanZero() {
  if (posix_fadvise(0, 0, 0, 0) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: the comparison always evaluates to false because posix_fadvise always returns non-negative values
  // CHECK-FIXES: posix_fadvise(0, 0, 0, 0) > 0
  if (posix_fallocate(0, 0, 0) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:32: warning:
  // CHECK-FIXES: posix_fallocate(0, 0, 0) > 0
  if (posix_madvise(NULL, 0, 0) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  // CHECK-FIXES: posix_madvise(NULL, 0, 0) > 0
  if (posix_memalign(NULL, 0, 0) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:34: warning:
  // CHECK-FIXES: posix_memalign(NULL, 0, 0) > 0
  if (posix_spawn(NULL, NULL, NULL, NULL, {NULL}, {NULL}) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:59: warning:
  // CHECK-FIXES: posix_spawn(NULL, NULL, NULL, NULL, {NULL}, {NULL}) > 0
  if (posix_spawnp(NULL, NULL, NULL, NULL, {NULL}, {NULL}) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:60: warning:
  // CHECK-FIXES: posix_spawnp(NULL, NULL, NULL, NULL, {NULL}, {NULL}) > 0
  if (pthread_create(NULL, NULL, NULL, NULL) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: the comparison always evaluates to false because pthread_create always returns non-negative values
  // CHECK-FIXES: pthread_create(NULL, NULL, NULL, NULL) > 0
  if (pthread_attr_setaffinity_np(NULL, 0, NULL) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:50: warning:
  // CHECK-FIXES: pthread_attr_setaffinity_np(NULL, 0, NULL) > 0
  if (pthread_attr_setschedpolicy(NULL, 0) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:44: warning:
  // CHECK-FIXES: pthread_attr_setschedpolicy(NULL, 0) > 0)
  if (pthread_attr_init(NULL) < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning:
  // CHECK-FIXES: pthread_attr_init(NULL) > 0
  if (pthread_yield() < 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:23: warning:
  // CHECK-FIXES: pthread_yield() > 0

}

void warningAlwaysTrue() {
  if (posix_fadvise(0, 0, 0, 0) >= 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: the comparison always evaluates to true because posix_fadvise always returns non-negative values
  if (pthread_create(NULL, NULL, NULL, NULL) >= 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: the comparison always evaluates to true because pthread_create always returns non-negative values
  if (pthread_attr_setaffinity_np(NULL, 0, NULL) >= 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:50: warning:
  if (pthread_attr_setschedpolicy(NULL, 0) >= 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:44: warning:
  if (pthread_attr_init(NULL) >= 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning:
  if (pthread_yield() >= 0) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:23: warning:

}

void warningEqualsNegative() {
  if (posix_fadvise(0, 0, 0, 0) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: posix_fadvise
  if (posix_fadvise(0, 0, 0, 0) != -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) <= -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) < -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fallocate(0, 0, 0) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:32: warning:
  if (posix_madvise(NULL, 0, 0) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_memalign(NULL, 0, 0) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:34: warning:
  if (posix_spawn(NULL, NULL, NULL, NULL, {NULL}, {NULL}) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:59: warning:
  if (posix_spawnp(NULL, NULL, NULL, NULL, {NULL}, {NULL}) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:60: warning:
  if (pthread_create(NULL, NULL, NULL, NULL) == -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: pthread_create
  if (pthread_create(NULL, NULL, NULL, NULL) != -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
  if (pthread_create(NULL, NULL, NULL, NULL) <= -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
  if (pthread_create(NULL, NULL, NULL, NULL) < -1) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:

}

void WarningWithMacro() {
  if (posix_fadvise(0, 0, 0, 0) < ZERO) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  // CHECK-FIXES: posix_fadvise(0, 0, 0, 0) > ZERO
  if (posix_fadvise(0, 0, 0, 0) >= ZERO) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) == NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) != NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) <= NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (posix_fadvise(0, 0, 0, 0) < NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:33: warning:
  if (pthread_create(NULL, NULL, NULL, NULL) < ZERO) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
  // CHECK-FIXES: pthread_create(NULL, NULL, NULL, NULL) > ZERO
  if (pthread_create(NULL, NULL, NULL, NULL) >= ZERO) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
  if (pthread_create(NULL, NULL, NULL, NULL) == NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
  if (pthread_create(NULL, NULL, NULL, NULL) != NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
  if (pthread_create(NULL, NULL, NULL, NULL) <= NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:
  if (pthread_create(NULL, NULL, NULL, NULL) < NEGATIVE_ONE) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:46: warning:

}

void noWarning() {
  if (posix_openpt(0) < 0) {}
  if (posix_openpt(0) <= 0) {}
  if (posix_openpt(0) == -1) {}
  if (posix_openpt(0) != -1) {}
  if (posix_openpt(0) <= -1) {}
  if (posix_openpt(0) < -1) {}
  if (posix_fadvise(0, 0, 0, 0) <= 0) {}
  if (posix_fadvise(0, 0, 0, 0) == 1) {}
}

namespace i {
int posix_fadvise(int fd, off_t offset, off_t len, int advice);
int pthread_yield(void);

void noWarning() {
  if (posix_fadvise(0, 0, 0, 0) < 0) {}
  if (posix_fadvise(0, 0, 0, 0) >= 0) {}
  if (posix_fadvise(0, 0, 0, 0) == -1) {}
  if (posix_fadvise(0, 0, 0, 0) != -1) {}
  if (posix_fadvise(0, 0, 0, 0) <= -1) {}
  if (posix_fadvise(0, 0, 0, 0) < -1) {}
    if (pthread_yield() < 0) {}
    if (pthread_yield() >= 0) {}
    if (pthread_yield() == -1) {}
    if (pthread_yield() != -1) {}
    if (pthread_yield() <= -1) {}
    if (pthread_yield() < -1) {}
}

} // namespace i

class G {
 public:
  int posix_fadvise(int fd, off_t offset, off_t len, int advice);
  int pthread_yield(void);

  void noWarning() {
    if (posix_fadvise(0, 0, 0, 0) < 0) {}
    if (posix_fadvise(0, 0, 0, 0) >= 0) {}
    if (posix_fadvise(0, 0, 0, 0) == -1) {}
    if (posix_fadvise(0, 0, 0, 0) != -1) {}
    if (posix_fadvise(0, 0, 0, 0) <= -1) {}
    if (posix_fadvise(0, 0, 0, 0) < -1) {}
    if (pthread_yield() < 0) {}
    if (pthread_yield() >= 0) {}
    if (pthread_yield() == -1) {}
    if (pthread_yield() != -1) {}
    if (pthread_yield() <= -1) {}
    if (pthread_yield() < -1) {}
  }
};
OpenPOWER on IntegriCloud