public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/114983] New: The -Wsizeof-array-div warning suppression using extra parenthesis (which is even suggested when in the warning itself) doesn't work inside templated code.
@ 2024-05-08 11:23 patryk.ludwikowski.7 at gmail dot com
  2024-05-08 13:48 ` [Bug c++/114983] The -Wsizeof-array-div warning suppression using extra parenthesis (which is suggested by " mpolacek at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: patryk.ludwikowski.7 at gmail dot com @ 2024-05-08 11:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114983
           Summary: The -Wsizeof-array-div warning suppression using extra
                    parenthesis (which is even suggested when in the
                    warning itself) doesn't work inside templated code.
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: patryk.ludwikowski.7 at gmail dot com
                CC: polacek at redhat dot com
  Target Milestone: ---

The -Wsizeof-array-div warning suppression using extra parenthesis (which is
even suggested when in the warning itself) doesn't work inside templated code,
or: the note in the warning is invalid for use in templated code. 

Code snippet to reproduce the problem:

```c++
#include <cstdint>

uint16_t samplesBuffer[40];

template <typename T>
constexpr inline auto getNumberOfSamples()
{
    // No matter how much parentheses are added, warnings still persists
    return ((sizeof(samplesBuffer)) / (sizeof(T))); 
}

int main()
{
    // return sizeof(samplesBuffer) / (sizeof(uint8_t)); // Works fine, no
warning
    return getNumberOfSamples<uint8_t>(); // Results in warning, how to silence
it?
}
```

Tried on Godbolt https://godbolt.org/z/63YK5dW5o with x86-64 gcc trunk. Other
GCC versions (older and other architectures) also seem affected. By the way,
CLANG compiles fine without warning. For the compiler options, the
-Wsizeof-array-div alone is enough to trigger the bug.

I tried it out on my system as well (sorry for Windows and older GCC version),
the same warning:

```
PS D:\Test> gcc -v
Using built-in specs.
COLLECT_GCC=F:\msys64\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-13.2.0/configure --prefix=/mingw64
--with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32
--host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32
--with-native-system-header-dir=/mingw64/include --libexecdir=/mingw64/lib
--enable-bootstrap --enable-checking=release --with-arch=nocona
--with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit
--enable-shared --enable-static --enable-libatomic --enable-threads=posix
--enable-graphite --enable-fully-dynamic-string
--enable-libstdcxx-filesystem-ts --enable-libstdcxx-time
--disable-libstdcxx-pch --enable-lto --enable-libgomp --disable-libssp
--disable-multilib --disable-rpath --disable-win32-registry --disable-nls
--disable-werror --disable-symvers --with-libiconv --with-system-zlib
--with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64
--with-isl=/mingw64 --with-pkgversion='Rev6, Built by MSYS2 project'
--with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as
--with-gnu-ld --disable-libstdcxx-debug --enable-plugin
--with-boot-ldflags=-static-libstdc++ --with-stage1-ldflags=-static-libstdc++
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.0 (Rev6, Built by MSYS2 project)
PS D:\Test> gcc test.cpp -Wsizeof-array-div
test.cpp: In instantiation of 'constexpr auto getNumberOfSamples() [with T =
unsigned char]':
test.cpp:15:36:   required from here
test.cpp:9:41: warning: expression does not compute the number of elements in
this array; element type is 'uint16_t' {aka 'short unsigned int'}, not
'unsigned char' [-Wsizeof-array-div]
    9 |         return ((sizeof(samplesBuffer)) / (sizeof(T)));
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
test.cpp:9:44: note: add parentheses around 'sizeof (unsigned char)' to silence
this warning
    9 |         return ((sizeof(samplesBuffer)) / (sizeof(T)));
      |                                           ~^~~~~~~~~~
      |                                           (          )
test.cpp:3:10: note: array 'samplesBuffer' declared here
    3 | uint16_t samplesBuffer[40];
      |          ^~~~~~~~~~~~~
```

Please note that the warning is correct, because T is not guaranteed to be the
array element type. IMO it would be nice if the warning was not thrown in this
case, or if there was other way of silencing it. Either way the note about
adding parentheses is wrong in this case for sure. 

I added CC to polacek@redhat.com because they seem to be the person who
implemented it (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91741 / mail
thread https://gcc.gnu.org/pipermail/gcc-patches/2020-September/553888.html),
no intention of blaming or anything, just they might be interested, idk. 

Workaround by diagnostic macros can be used for GCC:
```c++
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsizeof-array-div"
        return sizeof(samplesBuffer) / (sizeof(T));
#pragma GCC diagnostic pop
```

Let me know if something more is needed, it's my frist bug report here.

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

* [Bug c++/114983] The -Wsizeof-array-div warning suppression using extra parenthesis (which is suggested by in the warning itself) doesn't work inside templated code
  2024-05-08 11:23 [Bug c++/114983] New: The -Wsizeof-array-div warning suppression using extra parenthesis (which is even suggested when in the warning itself) doesn't work inside templated code patryk.ludwikowski.7 at gmail dot com
@ 2024-05-08 13:48 ` mpolacek at gcc dot gnu.org
  2024-05-09 19:51 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-05-08 13:48 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
   Last reconfirmed|                            |2024-05-08
             Status|UNCONFIRMED                 |ASSIGNED

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

* [Bug c++/114983] The -Wsizeof-array-div warning suppression using extra parenthesis (which is suggested by in the warning itself) doesn't work inside templated code
  2024-05-08 11:23 [Bug c++/114983] New: The -Wsizeof-array-div warning suppression using extra parenthesis (which is even suggested when in the warning itself) doesn't work inside templated code patryk.ludwikowski.7 at gmail dot com
  2024-05-08 13:48 ` [Bug c++/114983] The -Wsizeof-array-div warning suppression using extra parenthesis (which is suggested by " mpolacek at gcc dot gnu.org
@ 2024-05-09 19:51 ` cvs-commit at gcc dot gnu.org
  2024-05-09 19:52 ` mpolacek at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-09 19:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:646db3d30bd071a1b671b4f91c9ea2ab7f2be21c

commit r15-351-g646db3d30bd071a1b671b4f91c9ea2ab7f2be21c
Author: Marek Polacek <polacek@redhat.com>
Date:   Wed May 8 17:02:49 2024 -0400

    c++: failure to suppress -Wsizeof-array-div in template [PR114983]

    -Wsizeof-array-div offers a way to suppress the warning by wrapping
    the second operand of the division in parens:

      sizeof (samplesBuffer) / (sizeof(unsigned char))

    but this doesn't work in a template, because we fail to propagate
    the suppression bits.  Do it, then.

    The finish_parenthesized_expr hunk is not needed because suppress_warning
    isn't very fine-grained.  But I think it makes sense to be explicit and
    not rely on OPT_Wparentheses also suppressing OPT_Wsizeof_array_div.

            PR c++/114983

    gcc/cp/ChangeLog:

            * pt.cc (tsubst_expr) <case SIZEOF_EXPR>: Use copy_warning.
            * semantics.cc (finish_parenthesized_expr): Also suppress
            -Wsizeof-array-div.

    gcc/testsuite/ChangeLog:

            * g++.dg/warn/Wsizeof-array-div3.C: New test.

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

* [Bug c++/114983] The -Wsizeof-array-div warning suppression using extra parenthesis (which is suggested by in the warning itself) doesn't work inside templated code
  2024-05-08 11:23 [Bug c++/114983] New: The -Wsizeof-array-div warning suppression using extra parenthesis (which is even suggested when in the warning itself) doesn't work inside templated code patryk.ludwikowski.7 at gmail dot com
  2024-05-08 13:48 ` [Bug c++/114983] The -Wsizeof-array-div warning suppression using extra parenthesis (which is suggested by " mpolacek at gcc dot gnu.org
  2024-05-09 19:51 ` cvs-commit at gcc dot gnu.org
@ 2024-05-09 19:52 ` mpolacek at gcc dot gnu.org
  2024-05-22 22:21 ` cvs-commit at gcc dot gnu.org
  2024-05-22 22:21 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-05-09 19:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed on trunk so far, will backport to 14.

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

* [Bug c++/114983] The -Wsizeof-array-div warning suppression using extra parenthesis (which is suggested by in the warning itself) doesn't work inside templated code
  2024-05-08 11:23 [Bug c++/114983] New: The -Wsizeof-array-div warning suppression using extra parenthesis (which is even suggested when in the warning itself) doesn't work inside templated code patryk.ludwikowski.7 at gmail dot com
                   ` (2 preceding siblings ...)
  2024-05-09 19:52 ` mpolacek at gcc dot gnu.org
@ 2024-05-22 22:21 ` cvs-commit at gcc dot gnu.org
  2024-05-22 22:21 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-22 22:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-14 branch has been updated by Marek Polacek
<mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:c27d6c7fd03f95483d372eae2c96912ceee98a5e

commit r14-10235-gc27d6c7fd03f95483d372eae2c96912ceee98a5e
Author: Marek Polacek <polacek@redhat.com>
Date:   Wed May 8 17:02:49 2024 -0400

    c++: failure to suppress -Wsizeof-array-div in template [PR114983]

    -Wsizeof-array-div offers a way to suppress the warning by wrapping
    the second operand of the division in parens:

      sizeof (samplesBuffer) / (sizeof(unsigned char))

    but this doesn't work in a template, because we fail to propagate
    the suppression bits.  Do it, then.

    The finish_parenthesized_expr hunk is not needed because suppress_warning
    isn't very fine-grained.  But I think it makes sense to be explicit and
    not rely on OPT_Wparentheses also suppressing OPT_Wsizeof_array_div.

            PR c++/114983

    gcc/cp/ChangeLog:

            * pt.cc (tsubst_expr) <case SIZEOF_EXPR>: Use copy_warning.
            * semantics.cc (finish_parenthesized_expr): Also suppress
            -Wsizeof-array-div.

    gcc/testsuite/ChangeLog:

            * g++.dg/warn/Wsizeof-array-div3.C: New test.

    (cherry picked from commit 646db3d30bd071a1b671b4f91c9ea2ab7f2be21c)

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

* [Bug c++/114983] The -Wsizeof-array-div warning suppression using extra parenthesis (which is suggested by in the warning itself) doesn't work inside templated code
  2024-05-08 11:23 [Bug c++/114983] New: The -Wsizeof-array-div warning suppression using extra parenthesis (which is even suggested when in the warning itself) doesn't work inside templated code patryk.ludwikowski.7 at gmail dot com
                   ` (3 preceding siblings ...)
  2024-05-22 22:21 ` cvs-commit at gcc dot gnu.org
@ 2024-05-22 22:21 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-05-22 22:21 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2024-05-22 22:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-08 11:23 [Bug c++/114983] New: The -Wsizeof-array-div warning suppression using extra parenthesis (which is even suggested when in the warning itself) doesn't work inside templated code patryk.ludwikowski.7 at gmail dot com
2024-05-08 13:48 ` [Bug c++/114983] The -Wsizeof-array-div warning suppression using extra parenthesis (which is suggested by " mpolacek at gcc dot gnu.org
2024-05-09 19:51 ` cvs-commit at gcc dot gnu.org
2024-05-09 19:52 ` mpolacek at gcc dot gnu.org
2024-05-22 22:21 ` cvs-commit at gcc dot gnu.org
2024-05-22 22:21 ` mpolacek 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).