Recently, I was writing some code, and noticed some slightly strange
warning formatting on a function taking a `bool` parameter
#include <stdbool.h>
void test(bool unused)
{
}
bruh.c: In function 'test':
bruh.c:2:16: warning: unused parameter 'unused' [-Wunused-parameter]
2 | void test(bool unused)
| ^
Notice that there is only a ^ pointing at the first character of the
indentifer
There is no ~~~~ underlining. Also, only the first "u" is colored purple
The same issue does not manifest for _Bool
bruh.c: In function 'test':
bruh.c:2:17: warning: unused parameter 'unused' [-Wunused-parameter]
2 | void test(_Bool unused)
| ~~~~~~^~~~~~
I was wondering why, and after some further investigation, I found the
reason
gcc's stdbool.h uses:
#define bool _Bool
to provide the type
I investigated that myself with:
#define test_type int
void test(test_type unused)
{
}
and also reproduced the same thing
bruh.c: In function 'test':
bruh.c:3:21: warning: unused parameter 'unused' [-Wunused-parameter]
3 | void test(test_type unused)
| ^
typedef however, does not have this problem.
So, I guess I'm asking:
1)
Why is #define used instead of typedef? I can't imagine how this could
possibly break any existing code.
Would it be acceptable to make stdbool.h do this instead?
2)
Is it possible to improve this diagnostic to cope with #define?
also, it's worth noting, clang has this same "problem" too. Both the
compiler emits the suboptimal underlining in the diagnostic, and its
stdbool.h uses #define for bool
https://clang.llvm.org/doxygen/stdbool_8h_source.html
https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/ginclude/stdbool.h