public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "yann at droneaud dot fr" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/107618] Incorrect diagnostics when using -Og, builtin_expect(), and function attribute "warning" or "error"
Date: Thu, 10 Nov 2022 17:45:21 +0000	[thread overview]
Message-ID: <bug-107618-4-RI9Yg3UWiI@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107618-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107618

--- Comment #2 from Yann Droneaud <yann at droneaud dot fr> ---
I was wondering what GCC expects __builtin_object_size(0, 0) to be, and used
the following:

    void a_1 (void) __attribute__((__warning__("-1")));
    void a0 (void) __attribute__((__warning__("0")));
    void a1 (void) __attribute__((__warning__("1")));
    void a2 (void) __attribute__((__warning__("2")));
    void a3 (void) __attribute__((__warning__("3")));
    void a4 (void) __attribute__((__warning__("4")));

    int main(void) {
      unsigned long b = __builtin_object_size(0, 0);
      if (__builtin_expect(b == (unsigned long)-1, 0))
        a_1();
      if (__builtin_expect(b == 0, 0))
        a0();
      if (__builtin_expect(b == 1, 0))
        a1();
      if (__builtin_expect(b == 2, 0))
        a2();
      if (__builtin_expect(b == 3, 0))
        a3();
      if (__builtin_expect(b == 4, 0))
        a4();
    }

It works as expected for any level above 0, except -Og:

    $ gcc -O1 -S warning.c
    warning.c: In function ‘main’:
    warning.c:11:5: warning: call to ‘a_1’ declared with attribute warning: -1
[-Wattribute-warning]
       11 |     a_1();
          |     ^~~~~

It's quite reassuring that __builtin_object_size(0, 0) returns -1, hence the
emitted call to a_1():

    $ cat warning.s
            .file        "warning.c"
            .text
            .globl        main
            .type        main, @function
    main:
            endbr64
            subq        $8, %rsp
            call        a_1@PLT
            movl        $0, %eax
            addq        $8, %rsp
            ret

For -Og, there's a firework of warnings (shockingly they're the same than -O0
level):

    $ gcc -Og -S warning.c
    warning.c: In function ‘main’:
    warning.c:11:5: warning: call to ‘a_1’ declared with attribute warning: -1
[-Wattribute-warning]
       11 |     a_1();
          |     ^~~~~
    warning.c:13:5: warning: call to ‘a0’ declared with attribute warning: 0
[-Wattribute-warning]
       13 |     a0();
          |     ^~~~
    warning.c:15:5: warning: call to ‘a1’ declared with attribute warning: 1
[-Wattribute-warning]
       15 |     a1();
          |     ^~~~
    warning.c:17:5: warning: call to ‘a2’ declared with attribute warning: 2
[-Wattribute-warning]
       17 |     a2();
          |     ^~~~
    warning.c:19:5: warning: call to ‘a3’ declared with attribute warning: 3
[-Wattribute-warning]
       19 |     a3();
          |     ^~~~
    warning.c:21:5: warning: call to ‘a4’ declared with attribute warning: 4
[-Wattribute-warning]
       21 |     a4();
          |     ^~~~

Generated assembly for -Og is the same than for levels above 0: a call to a_1()
function is emitted, which mean GCC, at some point, knows
__builtin_object_size(0, 0) is -1 here. Thus most of the warnings are not very
useful.

    $ cat warning.s
            .file        "warning.c"
            .text
            .globl        main
            .type        main, @function
    main:
            endbr64
            subq        $8, %rsp
            call        a_1@PLT
            movl        $0, %eax
            addq        $8, %rsp
            ret

  parent reply	other threads:[~2022-11-10 17:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-10 15:42 [Bug other/107618] New: " yann at droneaud dot fr
2022-11-10 17:21 ` [Bug middle-end/107618] " yann at droneaud dot fr
2022-11-10 17:45 ` yann at droneaud dot fr [this message]
2022-11-11  7:47 ` [Bug tree-optimization/107618] " rguenth at gcc dot gnu.org
2022-11-11 13:32 ` cvs-commit at gcc dot gnu.org
2022-11-11 13:33 ` rguenth at gcc dot gnu.org
2022-11-11 14:37 ` yann at droneaud dot fr
2022-11-28 22:19 ` pinskia at gcc dot gnu.org

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=bug-107618-4-RI9Yg3UWiI@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).