leiless/assertf.h

-DASSERTF_DISABLE and assert_*() causes implicit declaration of function

leiless opened this issue · 1 comments

Env

Ubuntu 20.04 LTS
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

HOWTO reproduce

  1. wget https://raw.githubusercontent.com/leiless/assertf.h/master/assertf.h
  2. Save the following code into foo.c
#include <stdio.h>

#define ASSERTF_DEF_ONCE
#include "assertf.h"

int main(void)
{
    assert_eq(1, 1, %d);
    return 0;
}
  1. gcc -Wall -DASSERTF_DISABLE foo.c (reproduced)
In file included from foo.c:4:
foo.c: In function ‘main’:
assertf.h:222:13: warning: implicit declaration of function ‘COL’ [-Wimplicit-function-declaration]
  222 |             COL(CYAN), (a), COL(RST),                                                       \
      |             ^~~
assertf.h:180:87: note: in definition of macro ‘assertf’
  180 | #define assertf(e, fmt, ...)        (void) __vunused((void *) (uintptr_t) (e), fmt, ##__VA_ARGS__)
      |                                                                                       ^~~~~~~~~~~
assertf.h:248:41: note: in expansion of macro ‘__assert_cmp0’
  248 | #define assert_eq(a, b, fs)             __assert_cmp0(a, b, fs, ==)
      |                                         ^~~~~~~~~~~~~
foo.c:8:5: note: in expansion of macro ‘assert_eq’
    8 |     assert_eq(1, 1, %d);
      |     ^~~~~~~~~
assertf.h:222:17: error: ‘CYAN’ undeclared (first use in this function)
  222 |             COL(CYAN), (a), COL(RST),                                                       \
      |                 ^~~~
...

Bug fixed, tested in macOS

# lei @ lei-mbp in /tmp/t [22:16:17]
$ gcc -Wall -DASSERTF_DISABLE foo.c

# lei @ lei-mbp in /tmp/t [22:16:19]
$ ls -l a.out
-rwxr-xr-x  1 lei  wheel  16576 May 19 22:16 a.out

# lei @ lei-mbp in /tmp/t [22:16:24]
$ nm a.out
0000000100003f70 T ___vunused
0000000100000000 T __mh_execute_header
0000000100003f90 T _main
                 U dyld_stub_binder

# lei @ lei-mbp in /tmp/t [22:16:25]
$ strings a.out
lhs: %s%d%s rhs: %s%d%s

# lei @ lei-mbp in /tmp/t [22:16:26]
$ gcc -Wall -Wextra -O2 -DASSERTF_DISABLE foo.c

# lei @ lei-mbp in /tmp/t [22:16:41]
$ ls -l a.out
-rwxr-xr-x  1 lei  wheel  16576 May 19 22:16 a.out

# lei @ lei-mbp in /tmp/t [22:16:44]
$ nm a.out
0000000100003fa0 T ___vunused
0000000100000000 T __mh_execute_header
0000000100003fb0 T _main
                 U dyld_stub_binder

# lei @ lei-mbp in /tmp/t [22:16:44]
$ strings a.out