public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Suboptimal warning formatting with `bool` type in C
@ 2023-11-01 23:10 peter0x44
  2023-11-01 23:13 ` Joseph Myers
  0 siblings, 1 reply; 4+ messages in thread
From: peter0x44 @ 2023-11-01 23:10 UTC (permalink / raw)
  To: Gcc

[-- Attachment #1: Type: text/plain, Size: 1742 bytes --]

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-11-02  9:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-01 23:10 Suboptimal warning formatting with `bool` type in C peter0x44
2023-11-01 23:13 ` Joseph Myers
2023-11-01 23:28   ` peter0x44
2023-11-02  9:24     ` David Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).