This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/51294] New: spurious warning from -Wconversion in C and C++


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51294

             Bug #: 51294
           Summary: spurious warning from -Wconversion in C and C++
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: tortoise_74@yahoo.co.uk


The code below produces a spurious warning when compiled with -Wconversion.
0 is a const so the compiler should be able to choose the correct type. This
works with gcc 4.1 on redhat 5 but not the latest snapshot of gcc 4.7 (which I
am trialing for its c++11 support). 
I do not have any intermediate compiler versions to test with.

spurious.cpp:

class foo
{
   char bar;

   foo(bool haveBar, char bar_):
      bar(haveBar?bar_:0)
   {
   }
};

##

test.sh
#!/bin/sh

gcc --version
gcc -Wconversion -c spurious.cpp

export LD_LIBRARY_PATH=/opt/gcc4.7/lib:$LD_LIBRARY_PATH
export PATH=/opt/gcc4.7/bin:$PATH
/opt/gcc4.7/bin/gcc --version
/opt/gcc4.7/bin/gcc -Werror -Wconversion -c spurious.cpp

##

brucea@:home/brucea/spurious>./test.sh
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-51)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

gcc (GCC) 4.7.0 20111119 (experimental)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

spurious.cpp: In constructor âfoo::foo(bool, char)â:
spurious.cpp:8:25: error: conversion to âcharâ from âintâ may alter its value
[-Werror=conversion]
cc1plus: all warnings being treated as errors

##

The workaround is either to disable the warning with -Wno-conversion or add
unecessary constants as below. I don't think this is acceptable.

class foo2
{
   static const char zero = 0;

   char bar;

   foo2(bool haveBar, char bar_):
      bar(haveBar?bar_:zero)
   {
   }
};

This is also a bug in the C front-end:

void foo(int haveBar, char bar_)
{
  char zuul = haveBar?bar_:0;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]