public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "sagebar at web dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/105682] New: Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function
Date: Sat, 21 May 2022 08:39:18 +0000	[thread overview]
Message-ID: <bug-105682-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 105682
           Summary: Both `-Wsuggest-attribute=pure` and
                    `-Wsuggest-attribute=const` on same function
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sagebar at web dot de
  Target Milestone: ---

The following example causes both a `-Wsuggest-attribute=pure` and
`-Wsuggest-attribute=const` warning for the same function. Additionally, in
this example, only `-Wsuggest-attribute=pure` would be correct (the
`-Wsuggest-attribute=const` is completely bogus):

Compile with `gcc -c -O2 -Wsuggest-attribute=const -Wsuggest-attribute=pure
input.c`:

```
__attribute__((pure))
int callee(int x) {
        unsigned int a;
        __asm__ __volatile__("");
        __asm__ __volatile__("" : "=X" (a));
        x &= 1;
        if (a & 2)
                __asm__ __volatile__("" : "=m" (x));
        return x & 4;
}

int caller(int x) {
        return callee(x);
}
```

Output (gcc-12.1.0):
>input.c: In function 'caller':
>input.c:12:5: warning: function might be candidate for attribute 'pure' [-Wsuggest-attribute=pure]
>   12 | int caller(int x) {
>      |     ^~~~~~
>input.c:12:5: warning: function might be candidate for attribute 'const' [-Wsuggest-attribute=const]

Output (gcc-11.2.0; expected behavior):
>input.c: In function 'caller':
>input.c:12:5: warning: function might be candidate for attribute 'pure' [-Wsuggest-attribute=pure]
>   12 | int caller(int x) {
>      |     ^~~~~~


=== After adding `__attribute__((pure))` to `caller` ===

```
__attribute__((pure))
int caller(int x) {
        return callee(x);
}
```

gcc-12.1.0 (bogus warning: `caller()` has no right to be const; it calls a pure
function, and that function even contains inline assembly):
>infile.c: In function 'caller':
>infile.c:13:5: warning: function might be candidate for attribute 'const' [-Wsuggest-attribute=const]
>   13 | int caller(int x) {
>      |     ^~~~~~

gcc-11.2.0 (expected behavior):
> (No warnings)

=======================

NOTES:
- Not only does gcc suggest to add both pure & const to the same function, the
later wouldn't even make any sense in this scenario (though suggesting `pure`
is correct).
- The strange-looking body of `callee()` is required to reproduce the bug. -
Trying to further simplify it tends to make the warning go away, suggesting a
problem with some kind of heuristic.
- I know I also just reported
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105676, but that one is about a
bogus `-Wsuggest-attribute=pure` warning, while this one is about a bogus
`-Wsuggest-attribute=const` warning. The two bugs may be related, but simply
suppressing `-Wsuggest-attribute=pure` on const-functions wouldn't be enough to
fix the bug addressed in this report.

             reply	other threads:[~2022-05-21  8:39 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-21  8:39 sagebar at web dot de [this message]
2022-05-21 21:34 ` [Bug ipa/105682] 12/13 Regression] " pinskia at gcc dot gnu.org
2022-05-23  6:52 ` [Bug ipa/105682] [12/13 " rguenth at gcc dot gnu.org
2022-07-15 10:40 ` [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r10-3542-g0b92cf305dcf3438 marxin at gcc dot gnu.org
2022-07-15 19:22 ` pinskia at gcc dot gnu.org
2022-07-15 19:24 ` pinskia at gcc dot gnu.org
2022-07-18  8:30 ` [Bug ipa/105682] [12/13 Regression] Both `-Wsuggest-attribute=pure` and `-Wsuggest-attribute=const` on same function since r12-5177-g494bdadf28d0fb35 marxin at gcc dot gnu.org
2022-07-18  9:53 ` hubicka at gcc dot gnu.org
2022-09-05 17:15 ` sagebar at web dot de
2023-05-08 12:24 ` [Bug ipa/105682] [12/13/14 " rguenth 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-105682-4@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).