* 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
* Re: Suboptimal warning formatting with `bool` type in C
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
0 siblings, 1 reply; 4+ messages in thread
From: Joseph Myers @ 2023-11-01 23:13 UTC (permalink / raw)
To: peter0x44; +Cc: Gcc
On Wed, 1 Nov 2023, peter0x44 via Gcc wrote:
> Why is #define used instead of typedef? I can't imagine how this could
> possibly break any existing code.
That's how stdbool.h is specified up to C17. In C23, bool is a keyword
instead.
--
Joseph S. Myers
joseph@codesourcery.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Suboptimal warning formatting with `bool` type in C
2023-11-01 23:13 ` Joseph Myers
@ 2023-11-01 23:28 ` peter0x44
2023-11-02 9:24 ` David Brown
0 siblings, 1 reply; 4+ messages in thread
From: peter0x44 @ 2023-11-01 23:28 UTC (permalink / raw)
To: Joseph Myers; +Cc: Gcc
[-- Attachment #1: Type: text/plain, Size: 748 bytes --]
On 2023-11-01 23:13, Joseph Myers wrote:
> On Wed, 1 Nov 2023, peter0x44 via Gcc wrote:
>
>> Why is #define used instead of typedef? I can't imagine how this could
>> possibly break any existing code.
>
> That's how stdbool.h is specified up to C17. In C23, bool is a keyword
> instead.
I see, I didn't know it was specified that way. It seems quite strange
that typedef wouldn't be used for this purpose.
I suppose perhaps it matters if you #undef bool and then use it to
define your own type? Still, it seems very strange to do this.
Maybe it's something to offer as a GNU extension? Though, I'm leaning
towards too trivial to be worth it, just for a (very minor) improvement
to a diagnostic that can probably be handled in other ways.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Suboptimal warning formatting with `bool` type in C
2023-11-01 23:28 ` peter0x44
@ 2023-11-02 9:24 ` David Brown
0 siblings, 0 replies; 4+ messages in thread
From: David Brown @ 2023-11-02 9:24 UTC (permalink / raw)
To: peter0x44, Joseph Myers; +Cc: Gcc
On 02/11/2023 00:28, peter0x44 via Gcc wrote:
> On 2023-11-01 23:13, Joseph Myers wrote:
>
>> On Wed, 1 Nov 2023, peter0x44 via Gcc wrote:
>>
>>> Why is #define used instead of typedef? I can't imagine how this could
>>> possibly break any existing code.
>>
>> That's how stdbool.h is specified up to C17. In C23, bool is a keyword
>> instead.
>
> I see, I didn't know it was specified that way. It seems quite strange
> that typedef wouldn't be used for this purpose.
>
> I suppose perhaps it matters if you #undef bool and then use it to
> define your own type? Still, it seems very strange to do this.
>
Yes, that is part of the reason. The C standards mandate a number of
things to be macros when it would seem that typedef's, functions,
enumeration constants or other things would be "nicer" in some sense.
Macros have two advantages, however - you can "#undef" them, and you can
use "#ifdef" to test for them. This makes them useful in several cases
in the C standards, especially for changes that could break backwards
compatibility. Someone writing /new/ code would hopefully never make
their own "bool" type, but there's plenty of old code around - if you
ever need to include some pre-C99 headers with their own "bool" type and
post-C99 headers using <stdbool.h>, within the same C file, then it's
entirely possible that you'll be glad "bool" is a macro.
> Maybe it's something to offer as a GNU extension? Though, I'm leaning
> towards too trivial to be worth it, just for a (very minor) improvement
> to a diagnostic that can probably be handled in other ways.
>
Speaking as someone with absolutely zero authority (I'm a GCC user, not
a GCC developer), I strongly doubt that "bool" will be made a typedef as
a GCC extension.
But if there are problems with the interaction between pre-processor
macros and the formatting of diagnostic messages, then that is
definitely something that you should file as a bug report and which can
hopefully be fixed.
David
^ 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).