1 /*****************************************************************************
3 * argcheck - macros for argument value checking
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 ****************************************************************************/
25 #ifndef CCAN_ARGCHECK_H
26 #define CCAN_ARGCHECK_H
31 #include <ccan/likely/likely.h>
32 #include <ccan/compiler/compiler.h>
35 * ARGCHECK_DISABLE_LOGGING - define to disable any logging done by
36 * argcheck. This does not disable the actual checks, merely the invocation
37 * of the argcheck_log function, be it custom or the default logging
40 #ifdef ARGCHECK_DISABLE_LOGGING
42 #define argcheck_log(...) /* __VA_ARGS__ */
46 * argcheck_log - logging function for failed argcheck tests
48 * Override this function to hook up your own logging function. The function
49 * will be called once for each failed test.
51 #define argcheck_log argcheck_log_
52 #endif /* argcheck_log */
54 static inline void COLD PRINTF_FMT(4, 5)
55 argcheck_log_(const char *file, int line, const char *func,
60 fprintf(stderr, "argcheck: %s:%d %s\nargcheck: ", file, line, func);
61 vfprintf(stderr, msg, args);
67 * argcheck_int_eq - check the argument is equal to the value.
69 #define argcheck_int_eq(arg, val) \
70 argcheck_int_eq_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
72 * argcheck_int_ne - check the argument is not equal to the value
74 #define argcheck_int_ne(arg, val) \
75 argcheck_int_ne_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
77 * argcheck_int_ge - check the argument is equal or greater than the value
79 #define argcheck_int_ge(arg, val) \
80 argcheck_int_ge_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
82 * argcheck_int_gt - check the argument is greater than the value
84 #define argcheck_int_gt(arg, val) \
85 argcheck_int_gt_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
87 * argcheck_int_le - check the argument is equal or less than the value
89 #define argcheck_int_le(arg, val) \
90 argcheck_int_le_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
92 * argcheck_int_lt - check the argument is less than the value
94 #define argcheck_int_lt(arg, val) \
95 argcheck_int_lt_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
97 * argcheck_int_range - check the argument is within a range (inclusive)
99 #define argcheck_int_range(arg, min, max) \
100 argcheck_int_range_(arg, min, max, #arg, #min, #max, __FILE__, __LINE__, __func__)
102 * argcheck_flag_set - check if a flag is set
104 #define argcheck_flag_set(arg, flag) \
105 argcheck_flag_set_(arg, flag, #arg, #flag, __FILE__, __LINE__, __func__)
107 * argcheck_flag_unset - check if a flag is not set
109 #define argcheck_flag_unset(arg, flag) \
110 argcheck_flag_unset_(arg, flag, #arg, #flag, __FILE__, __LINE__, __func__)
112 * argcheck_ptr_not_null - check that a pointer is not NULL
114 #define argcheck_ptr_not_null(arg) \
115 argcheck_ptr_not_null_(arg, #arg, __FILE__, __LINE__, __func__)
117 * argcheck_ptr_null - check that a pointer is NULL
119 #define argcheck_ptr_null(arg) \
120 argcheck_ptr_null_(arg, #arg, __FILE__, __LINE__, __func__)
122 * argcheck_str_null - see argcheck_ptr_null
124 #define argcheck_str_null(arg) \
125 argcheck_str_null_(arg, #arg, __FILE__, __LINE__, __func__)
127 * argcheck_str_not_null - see argcheck_ptr_not_null
129 #define argcheck_str_not_null(arg) \
130 argcheck_str_not_null_(arg, #arg, __FILE__, __LINE__, __func__)
132 * argcheck_str_zero - check that a string is not NULL and of zero length
134 #define argcheck_str_zero_len(arg) \
135 argcheck_str_zero_len_(arg, #arg, __FILE__, __LINE__, __func__)
137 * argcheck_str_null_or_zero - check that a string is either NULL or of zero length
139 #define argcheck_str_null_or_zero_len(arg) \
140 argcheck_str_null_or_zero_len_(arg, #arg, __FILE__, __LINE__, __func__)
142 * argcheck_str_not_zero - check that a string is not NULL and has a length greater than 0
144 #define argcheck_str_not_zero_len(arg) \
145 argcheck_str_not_zero_len_(arg, #arg, __FILE__, __LINE__, __func__)
147 * argcheck_str_null_or_not_zero - check that a string is either NULL or has a length greater than 0
149 #define argcheck_str_null_or_not_zero_len(arg) \
150 argcheck_str_null_or_not_zero_len_(arg, #arg, __FILE__, __LINE__, __func__)
152 * argcheck_str_min_len - check that a string is not NULL and has a length greater than or equal to a minimum
154 #define argcheck_str_min_len(arg, min) \
155 argcheck_str_min_len_(arg, #arg, min, #min, __FILE__, __LINE__, __func__)
157 * argcheck_str_max_len - check that a string is not NULL and has a length less than or equal to a maximum
159 #define argcheck_str_max_len(arg, max) \
160 argcheck_str_max_len_(arg, #arg, max, #max, __FILE__, __LINE__, __func__)
162 * argcheck_str_null_or_min_len - check that a string is NULL or has a length greater than or equal to a minimum
164 #define argcheck_str_null_or_min_len(arg, min) \
165 argcheck_str_null_or_min_len_(arg, #arg, min, #min, __FILE__, __LINE__, __func__)
167 * argcheck_str_null_or_max_len - check that a string is NULL or has a length less than or equal to a maximum
169 #define argcheck_str_null_or_max_len(arg, max) \
170 argcheck_str_null_or_max_len_(arg, #arg, max, #max, __FILE__, __LINE__, __func__)
174 below is the actual implemenation. do not use it directly, use the macros
179 static inline int argcheck_int_eq_(int a, int b, const char *astr, const char *bstr,
180 const char *file, int line, const char *func)
185 argcheck_log(file, line, func,
186 "condition \"(%s == %s)\" (%d == %d) failed\n", astr, bstr, a, b);
190 static inline int argcheck_int_ne_(int a, int b, const char *astr, const char *bstr,
191 const char *file, int line, const char *func)
196 argcheck_log(file, line, func,
197 "condition \"(%s != %s)\" (%d != %d) failed\n", astr, bstr, a, b);
201 static inline int argcheck_int_ge_(int a, int b, const char *astr, const char *bstr,
202 const char *file, int line, const char *func)
207 argcheck_log(file, line, func,
208 "condition \"(%s >= %s)\" (%d >= %d) failed\n", astr, bstr, a, b);
212 static inline int argcheck_int_gt_(int a, int b, const char *astr, const char *bstr,
213 const char *file, int line, const char *func)
218 argcheck_log(file, line, func,
219 "condition \"(%s > %s)\" (%d > %d) failed\n", astr, bstr, a, b);
223 static inline int argcheck_int_le_(int a, int b, const char *astr, const char *bstr,
224 const char *file, int line, const char *func)
229 argcheck_log(file, line, func,
230 "condition \"(%s <= %s)\" (%d <= %d) failed\n", astr, bstr, a, b);
234 static inline int argcheck_int_lt_(int a, int b, const char *astr, const char *bstr,
235 const char *file, int line, const char *func)
240 argcheck_log(file, line, func,
241 "condition \"(%s < %s)\" (%d < %d) failed\n", astr, bstr, a, b);
245 static inline int argcheck_int_range_(int v, int min, int max,
247 const char *minstr, const char *maxstr,
248 const char *file, int line, const char *func)
250 if (!argcheck_int_le_(min, max, minstr, maxstr, file, line, func))
253 if (likely(v >= min && v <= max))
256 argcheck_log(file, line, func,
257 "condition \"(%s <= %s <= %s)\" (%d <= %d <= %d) failed\n",
258 minstr, vstr, maxstr, min, v, max);
262 static inline int argcheck_flag_set_(int arg, int flag,
263 const char *argstr, const char *flagstr,
264 const char *file, int line, const char *func)
266 if (!argcheck_int_ne_(flag, 0, flagstr, "0", file, line, func))
269 if (likely(arg & flag))
272 argcheck_log(file, line, func,
273 "flag \"%s\" (%d) is not set on \"%s\" (%d)\"\n",
274 flagstr, flag, argstr, arg);
278 static inline int argcheck_flag_unset_(int arg, int flag,
279 const char *argstr, const char *flagstr,
280 const char *file, int line, const char *func)
282 if (!argcheck_int_ne_(flag, 0, flagstr, "0", file, line, func))
285 if (likely((arg & flag) == 0))
288 argcheck_log(file, line, func,
289 "flag \"%s\" (%d) must not be set on \"%s\" (%d)\"\n",
290 flagstr, flag, argstr, arg);
294 static inline int argcheck_ptr_not_null_(const void *arg, const char *argstr,
295 const char *file, int line, const char *func)
297 if (likely(arg != NULL))
300 argcheck_log(file, line, func,
301 "\"%s\" must not be NULL\n", argstr);
305 static inline int argcheck_ptr_null_(const void *arg, const char *argstr,
306 const char *file, int line, const char *func)
308 if (likely(arg == NULL))
311 argcheck_log(file, line, func,
312 "\"%s\" must be NULL\n", argstr);
316 static inline int argcheck_str_null_(const char *arg, const char *argstr,
317 const char *file, int line, const char *func)
319 return argcheck_ptr_null_(arg, argstr, file, line, func);
322 static inline int argcheck_str_not_null_(const char *arg, const char *argstr,
323 const char *file, int line, const char *func)
325 return argcheck_ptr_not_null_(arg, argstr, file, line, func);
328 static inline int argcheck_str_zero_len_(const char *arg, const char *argstr,
329 const char *file, int line, const char *func)
331 if (!argcheck_str_not_null_(arg, argstr, file, line, func))
334 if (likely(*arg == '\0'))
337 argcheck_log(file, line, func,
338 "\"%s\" must be a zero-length string\n", argstr);
343 static inline int argcheck_str_null_or_zero_len_(const char *arg, const char *argstr,
344 const char *file, int line, const char *func)
346 if (likely(arg == NULL || *arg == '\0'))
349 argcheck_log(file, line, func,
350 "\"%s\" must be NULL or a zero-length string\n", argstr);
354 static inline int argcheck_str_not_zero_len_(const char *arg, const char *argstr,
355 const char *file, int line, const char *func)
357 if (!argcheck_str_not_null_(arg, argstr, file, line, func))
360 if (likely(*arg != '\0'))
363 argcheck_log(file, line, func,
364 "\"%s\" must not be a zero-length string\n", argstr);
368 static inline int argcheck_str_null_or_not_zero_len_(const char *arg, const char *argstr,
369 const char *file, int line, const char *func)
371 if (likely(arg == NULL || *arg != '\0'))
374 argcheck_log(file, line, func,
375 "\"%s\" must be NULL or not a zero-length string\n", argstr);
379 static inline int argcheck_str_min_len_(const char *arg, const char *argstr,
380 int min, const char *minstr,
381 const char *file, int line, const char *func)
385 if (!argcheck_str_not_null_(arg, argstr, file, line, func))
389 if (likely(len >= min))
392 argcheck_log(file, line, func,
393 "\"%s\" must be at least \"%s\" (%d) long, is '%s' (length %d)\n", argstr,
394 minstr, min, arg, len);
398 static inline int argcheck_str_max_len_(const char *arg, const char *argstr,
399 int max, const char *maxstr,
400 const char *file, int line, const char *func)
404 if (!argcheck_str_not_null_(arg, argstr, file, line, func))
408 if (likely(len <= max))
411 argcheck_log(file, line, func,
412 "\"%s\" must be at most \"%s\" (%d) long, is '%s' (length %d)\n", argstr,
413 maxstr, max, arg, len);
417 static inline int argcheck_str_null_or_min_len_(const char *arg, const char *argstr,
418 int min, const char *minstr,
419 const char *file, int line, const char *func)
423 if (likely(arg == NULL))
427 if (likely(len >= min))
430 argcheck_log(file, line, func,
431 "\"%s\" must be NULL or at least \"%s\" (%d) long, is '%s' (length %d)\n", argstr,
432 minstr, min, arg, len);
436 static inline int argcheck_str_null_or_max_len_(const char *arg, const char *argstr,
437 int max, const char *maxstr,
438 const char *file, int line, const char *func)
442 if (likely(arg == NULL))
446 if (likely(len <= max))
449 argcheck_log(file, line, func,
450 "\"%s\" must be NULL or at most \"%s\" (%d) long, is '%s' (length %d)\n", argstr,
451 maxstr, max, arg, len);
455 #endif /* CCAN_ARGCHECK_H */