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

            Bug ID: 92117
           Summary: Warning from -Wbad-function-cast when casting from
                    bool to int
           Product: gcc
           Version: 5.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: thomas.kolb at iis dot fraunhofer.de
  Target Milestone: ---

Created attachment 47045
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47045&action=edit
Preprocessed test source

Hello,

when I try to compile the following code, I get the warning below:

| #include <stdbool.h>
| 
| static bool safe_read_bool(void)
| {
|     return false;
| }
| 
| void pb_enc_bool(void)
| {
|     int value = (int)safe_read_bool();
| }

| $ gcc -O2 -std=c99 -Wbad-function-cast --save-temps -c -o test.o test.c
| test.c: In function 'pb_enc_bool':
| test.c:10:17: warning: cast from function call of type '_Bool' to
non-matching type 'int' [-Wbad-function-cast]
|    10 |     int value = (int)safe_read_bool();
|       |                 ^

The pre-processed source file is attached.

As a bool can be fully represented by an integer, I would expect that is is a
valid cast. When the return value is assigned directly to the integer variable,
no warning is given.

This was tested on various versions of GCC which all show this behaviour. Here
are the details of the latest version:

| $ gcc -v
| Using built-in specs.
| COLLECT_GCC=gcc
| COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/9/lto-wrapper
| OFFLOAD_TARGET_NAMES=hsa:nvptx-none
| Target: x86_64-suse-linux
| Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-languages=c,c++,objc,fortran,obj-c++,ada,go,d
--enable-offload-targets=hsa,nvptx-none=/usr/nvptx-none, --without-cuda-driver
--disable-werror --with-gxx-include-dir=/usr/include/c++/9 --enable-ssp
--disable-libssp --disable-libvtv --disable-cet --disable-libcc1
--enable-plugin --with-bugurl=https://bugs.opensuse.org/
--with-pkgversion='SUSE Linux' --with-slibdir=/lib64 --with-system-zlib
--enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-libphobos
--enable-version-specific-runtime-libs --with-gcc-major-version-only
--enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function
--program-suffix=-9 --without-system-libunwind --enable-multilib
--with-arch-32=x86-64 --with-tune=generic
--with-build-config=bootstrap-lto-lean --enable-link-mutex
--build=x86_64-suse-linux --host=x86_64-suse-linux
| Thread model: posix
| gcc version 9.2.1 20190903 [gcc-9-branch revision 275330] (SUSE Linux) 

Thanks for your time and attention.

Reply via email to