public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/105676] New: Bogus `-Wsuggest-attribute=pure` on function marked `__attribute__((const))`
@ 2022-05-20 17:46 sagebar at web dot de
  2022-05-20 17:50 ` [Bug ipa/105676] [12/13 Regression] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: sagebar at web dot de @ 2022-05-20 17:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105676
           Summary: Bogus `-Wsuggest-attribute=pure` on function marked
                    `__attribute__((const))`
           Product: gcc
           Version: 12.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sagebar at web dot de
  Target Milestone: ---

When compiling with `-Wsuggest-attribute=pure`, gcc warns about missing
`__attribute__((pure))` on functions declared as `__attribute__((const))`.

It is common knowledge that any function marked as `__attribute__((const))`
also implicitly has the behavior of a function marked as
`__attribute__((pure))` (const implies pure).

Therefor, it stands to reason that such a warning is bogus (also: earlier
version of gcc didn't emit a warning in this case; know: gcc-9 already
supported these warnings, but didn't emit `-Wsuggest-attribute=pure` on a
`__attribute__((const))` function).

Example (`gcc -Wsuggest-attribute=pure -c -O2 input.c`):

```
__attribute__((const))
extern int do_expensive_calculation(void);

__attribute__((const))
int getval(void) {
        static int cache = -1;
        if (cache == -1)
                cache = do_expensive_calculation();
        return cache;
}
```

>test.c: In function 'getval':
>test.c:5:5: warning: function might be candidate for attribute 'pure' >[-Wsuggest-attribute=pure]
>    5 | int getval(void) {
>      |     ^~~~~~

When trying to declare as both pure+const:
>test.c:5:1: warning: ignoring attribute 'const' because it conflicts with attribute 'pure' [-Wattributes]
>    5 | int getval(void) {
>      | ^~~


==== Explaination of why using `__attribute__((const))` is valid here ====

I see why gcc might think that `getval()` is *only* `pure`, but there is
nothing wrong with the `__attribute__((const))` annotation since we don't "read
global memory" (emphasis on the "global"), and thus don't depend on the global
state (also: what counts as "global" vs. "non-global" isn't something that can
be quantified. - It depends on the application and how memory is used).

As such, the use of `__attribute__((const))` is very much valid (and gcc might
even consider suggesting `__attribute__((const))` instead of
`__attribute__((pure))`, since because `cache` is scoped-static, it not being
used outside of `getval()` can actually be proven, though that isn't what this
bug report is about...). However, so-long as it believes that the function is
pure, there is no reason to ever emit a about that fact so-long as `getval()`
remains annotated as `__attribute__((const))`.

==== End of Explaination ====

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-04-17  9:15 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-20 17:46 [Bug c/105676] New: Bogus `-Wsuggest-attribute=pure` on function marked `__attribute__((const))` sagebar at web dot de
2022-05-20 17:50 ` [Bug ipa/105676] [12/13 Regression] " pinskia at gcc dot gnu.org
2022-05-23  6:48 ` rguenth at gcc dot gnu.org
2022-07-15 10:42 ` [Bug ipa/105676] [12/13 Regression] Bogus `-Wsuggest-attribute=pure` on function marked `__attribute__((const))` since r12-5437-g09a4ffb72aa2f513 marxin at gcc dot gnu.org
2022-07-18 10:16 ` hubicka at gcc dot gnu.org
2022-09-18 17:52 ` mibjst9as at mozmail dot com
2022-12-07  9:25 ` rguenth at gcc dot gnu.org
2022-12-07 10:28 ` cvs-commit at gcc dot gnu.org
2022-12-07 10:28 ` [Bug ipa/105676] [12 " rguenth at gcc dot gnu.org
2023-04-17  9:14 ` cvs-commit at gcc dot gnu.org
2023-04-17  9:15 ` rguenth at gcc dot gnu.org

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).