From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 38D4B3858CDA; Thu, 10 Nov 2022 15:42:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 38D4B3858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668094963; bh=nbHSnok7T3LHzTq61m2Va+RCr4SwFFbFpvVwD40n2ME=; h=From:To:Subject:Date:From; b=T1Lfrz8R7khfUl0VIh9yKfXs11Y+VO9bS3wubfWEmDNgTogHWteti8TFbvVrbpUfr KbAaguypc1GddkxElNFBA5FX3nvrZon0MtnI4GUz0kf+P7fQrOeBgRK60fxJAVInxE pSnafPl2NoiBcMbDXdjMiUGtE7dGxBRUo0uyec7Q= From: "yann at droneaud dot fr" To: gcc-bugs@gcc.gnu.org Subject: [Bug other/107618] New: Incorrect diagnostics when using -Og, builtin_expect(), and function attribute "warning" or "error" Date: Thu, 10 Nov 2022 15:42:40 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: other X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: yann at droneaud dot fr 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 attachments.created 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107618 Bug ID: 107618 Summary: Incorrect diagnostics when using -Og, builtin_expect(), and function attribute "warning" or "error" Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: other Assignee: unassigned at gcc dot gnu.org Reporter: yann at droneaud dot fr Target Milestone: --- Created attachment 53872 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D53872&action=3Dedit reproducer for -Og, __builtin_expect(), and function __attribute__((warning(""))) The code below compiled with -Og triggers a warning message that is not mandated: $ gcc -Og warning.c warning.c: In function =E2=80=98test_expect=E2=80=99: warning.c:26:17: warning: call to =E2=80=98size_mismatch_expect=E2=80= =99 declared with attribute warning: size mismatch (builtin_expect) [-Wattribute-warning] 26 | size_mismatch_expect(); | ^~~~~~~~~~~~~~~~~~~~~~ $ cat warning.c #include #include #include extern void size_mismatch_nonexpect(void) __attribute__((__warning__("size mismatch"))); static bool test_nonexpect(const void *addr, size_t len) { size_t sz =3D __builtin_object_size(addr, 0); if (sz !=3D (size_t)-1 && sz < len) { size_mismatch_nonexpect(); return false; } return true; } extern void size_mismatch_expect(void) __attribute__((__warning__("size mismatch (builtin_expect)"))); static bool test_expect(const void *addr, size_t len) { size_t sz =3D __builtin_object_size(addr, 0); if (__builtin_expect(sz !=3D (size_t)-1 && sz < len, 0)) { size_mismatch_expect(); return false; } return true; } int main(void) { int i =3D 0; if (!test_nonexpect(&i, sizeof(i))) return EXIT_FAILURE; if (!test_expect(&i, sizeof(i))) return EXIT_FAILURE; return EXIT_SUCCESS; } The warning at -Og level is not expected because there's no call to the size_mismatch_expect() in the generated assembler (for x86-64): $ head warning.s .file "warning.c" .text .type test_nonexpect, @function test_nonexpect: movl $1, %eax ret .size test_nonexpect, .-test_nonexpect .type test_expect, @function test_expect: movl $1, %eax ret .size test_expect, .-test_expect .globl main .type main, @function main: endbr64 subq $24, %rsp movq %fs:40, %rax movq %rax, 8(%rsp) xorl %eax, %eax movl $0, 4(%rsp) leaq 4(%rsp), %rdi movl $4, %esi call test_nonexpect testb %al, %al jne .L11 movl $1, %eax .L5: movq 8(%rsp), %rdx subq %fs:40, %rdx jne .L12 addq $24, %rsp ret .L11: leaq 4(%rsp), %rdi movl $4, %esi call test_expect testb %al, %al je .L9 movl $0, %eax jmp .L5 .L9: movl $1, %eax jmp .L5 See also https://godbolt.org/z/KEsaavhvG Compiling at optimization level s, z, 1, 2, or 3 doesn't produce that warni= ng. (but compiling with -O0 does produces two warnings, as expected, since call= s to the two functions are emitted). Having the warning at debug optimization level makes using -Og in some comp= lex code base challenging for no good reason when __attribute__((error(""))) is used (or -Werror=3Dattribute-warning). It should also be noted clang doesn't generate the warning for optimization level above 0.=