Skip to content

Commit 9731f9f

Browse files
committed
Simplify some more macros with C23 syntax
1 parent f976255 commit 9731f9f

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

src/sanity.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
#ifndef BFS_SANITY_H
99
#define BFS_SANITY_H
1010

11+
#include "bfs.h"
1112
#include <stddef.h>
1213

13-
// Call macro(ptr, size) or macro(ptr, sizeof(*ptr))
14-
#define SANITIZE_CALL(...) \
15-
SANITIZE_CALL_(__VA_ARGS__, )
14+
/** Get the default size for a sanitize macro call. */
15+
#define SANITIZE_SIZE_(ptr, size) \
16+
BFS_VA_IF(size)(size)(sizeof(*ptr))
1617

17-
#define SANITIZE_CALL_(macro, ptr, ...) \
18-
SANITIZE_CALL__(macro, ptr, __VA_ARGS__ sizeof(*(ptr)), )
18+
// Call macro(ptr, size) or macro(ptr, sizeof(*ptr))
19+
#define SANITIZE_CALL(macro, ptr, ...) \
20+
SANITIZE_CALL_(macro, ptr, SANITIZE_SIZE_(ptr, __VA_ARGS__))
1921

20-
#define SANITIZE_CALL__(macro, ptr, size, ...) \
22+
#define SANITIZE_CALL_(macro, ptr, size) \
2123
macro(ptr, size)
2224

2325
#if __SANITIZE_ADDRESS__

src/thread.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424
} \
2525
} while (0)
2626

27-
#define THREAD_INFALLIBLE(...) \
28-
THREAD_INFALLIBLE_(__VA_ARGS__, 0, )
27+
#define THREAD_INFALLIBLE(expr, ...) \
28+
THREAD_INFALLIBLE_(expr, BFS_VA_IF(__VA_ARGS__)(__VA_ARGS__)(0))
2929

30-
#define THREAD_INFALLIBLE_(expr, allowed, ...) \
30+
#define THREAD_INFALLIBLE_(expr, allowed) \
3131
int err = expr; \
32-
bfs_verify(err == 0 || err == allowed, "%s: %s", #expr, xstrerror(err)); \
33-
(void)0
32+
bfs_verify(err == 0 || err == allowed, "%s: %s", #expr, xstrerror(err))
3433

3534
int thread_create(pthread_t *thread, const pthread_attr_t *attr, thread_fn *fn, void *arg) {
3635
THREAD_FALLIBLE(pthread_create(thread, attr, fn, arg));

tests/ptyx.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,16 @@ int main(int argc, char *argv[]) {
4848
const char *cmd = argc > 0 ? argv[0] : "ptyx";
4949

5050
/** Report an error message and exit. */
51-
#define die(...) die_(__VA_ARGS__, )
52-
53-
#define die_(format, ...) \
51+
#define die(format, ...) \
5452
do { \
55-
fprintf(stderr, "%s: " format "%s", cmd, __VA_ARGS__ "\n"); \
53+
fprintf(stderr, "%s: " format "\n", cmd __VA_OPT__(,) __VA_ARGS__); \
5654
exit(EXIT_FAILURE); \
5755
} while (0)
5856

5957
/** Report an error code and exit. */
60-
#define edie(...) edie_(__VA_ARGS__, )
61-
62-
#define edie_(format, ...) \
58+
#define edie(format, ...) \
6359
do { \
64-
fprintf(stderr, "%s: " format ": %s\n", cmd, __VA_ARGS__ errstr()); \
60+
fprintf(stderr, "%s: " format ": %s\n", cmd __VA_OPT__(,) __VA_ARGS__, errstr()); \
6561
exit(EXIT_FAILURE); \
6662
} while (0)
6763

0 commit comments

Comments
 (0)