public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: peter0x44 <peter0x44@disroot.org>
To: Gcc <gcc@gcc.gnu.org>
Subject: Suboptimal warning formatting with `bool` type in C
Date: Wed, 01 Nov 2023 23:10:50 +0000	[thread overview]
Message-ID: <ac008db44fa681b1b1ac0bda785a11db@disroot.org> (raw)

[-- 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

             reply	other threads:[~2023-11-01 23:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-01 23:10 peter0x44 [this message]
2023-11-01 23:13 ` Joseph Myers
2023-11-01 23:28   ` peter0x44
2023-11-02  9:24     ` David Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ac008db44fa681b1b1ac0bda785a11db@disroot.org \
    --to=peter0x44@disroot.org \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).