From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 2CEF1385840F; Sat, 21 May 2022 08:39:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2CEF1385840F From: "sagebar at web dot de" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: sagebar at web dot de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 21 May 2022 08:39:19 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105682 Bug ID: 105682 Summary: Both `-Wsuggest-attribute=3Dpure` and `-Wsuggest-attribute=3Dconst` 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=3Dpure` and `-Wsuggest-attribute=3Dconst` warning for the same function. Additionally, = in this example, only `-Wsuggest-attribute=3Dpure` would be correct (the `-Wsuggest-attribute=3Dconst` is completely bogus): Compile with `gcc -c -O2 -Wsuggest-attribute=3Dconst -Wsuggest-attribute=3D= pure input.c`: ``` __attribute__((pure)) int callee(int x) { unsigned int a; __asm__ __volatile__(""); __asm__ __volatile__("" : "=3DX" (a)); x &=3D 1; if (a & 2) __asm__ __volatile__("" : "=3Dm" (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=3Dpure] > 12 | int caller(int x) { > | ^~~~~~ >input.c:12:5: warning: function might be candidate for attribute 'const' [= -Wsuggest-attribute=3Dconst] 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=3Dpure] > 12 | int caller(int x) { > | ^~~~~~ =3D=3D=3D After adding `__attribute__((pure))` to `caller` =3D=3D=3D ``` __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=3Dconst] > 13 | int caller(int x) { > | ^~~~~~ gcc-11.2.0 (expected behavior): > (No warnings) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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 `pur= e` 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=3D105676, but that one is abou= t a bogus `-Wsuggest-attribute=3Dpure` warning, while this one is about a bogus `-Wsuggest-attribute=3Dconst` warning. The two bugs may be related, but sim= ply suppressing `-Wsuggest-attribute=3Dpure` on const-functions wouldn't be eno= ugh to fix the bug addressed in this report.=