Suboptimal warning formatting with `bool` type in C

David Brown david@westcontrol.com
Thu Nov 2 09:24:09 GMT 2023


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




More information about the Gcc mailing list