[c++ patch] Warn for assignment from int to bool

Erik Mouw J.A.K.Mouw@its.tudelft.nl
Mon Feb 28 14:47:00 GMT 2000


Hi all,

A month ago Martin von Loewis and myself had a discussion on the gcc
mailing list if g++ should issue a warning for line 6 in the following
piece of code:

foo.c
1:  int foo(void)
2:  {
3:    bool rv = false;
4:    int level = 42;
5:
6:    rv = level;
7:
8:    return(rv);
9:  }

Our (me and my colleague Cor Veenman) rationale for this was that The Wave
Book (The C++ programming language, third edition) states on page 76:

  "If a value c can be represented exactly in a variable of type T, a
  conversion of v to T is value-preserving and no problem. The cases where
  conversions are not value-preserving are best avoided."

In the example code above, the assignment from int to bool (line 6) and
back from bool to int (line 8) is not value preserving: the final return
value becomes 1, while the initial value of the integer was 42. In our
opinion g++ should issue a warning, also because it does warn about the
same kind of assignments from float to int:

bar.c
1:  float bar(void)
2:  {
3:    int rv = 0;
4:    float level = 42.0;
5:
6:    rv = level;
7:
8:    return(rv);
9:  }

Martin asked me to submit a patch, so here it is at the end of this
message. The patch is against gcc-2.95.2, I hope it still applies to the
current CVS version. Credits should go to Cor Veenman, he did most of the
work.

This is the warning message from the patched compiler when compiling the
first example:

  erik@dutein75:/fibre0/erik >install/bin/g++ -Wall -c foo.c
  foo.c: In function `int foo()':
  foo.c:6: warning: assignment to `bool' from `int'
  erik@dutein75:/fibre0/erik >

This is the same kind of warning I get from the second example:

  erik@dutein75:/fibre0/erik >install/bin/g++ -Wall -c bar.c
  bar.c: In function `float bar()':
  bar.c:6: warning: assignment to `int' from `float'
  erik@dutein75:/fibre0/erik >

To show that this patch only warns about real assignments from int to bool
and doesn't break some popular C/C++ idiom:

frob.c:
01:  int frob(void)
02:  {
03:    int i = 10;
04:    char *ptr = (char *)1;
05:
06:    while(i)
07:      i--;
08:
09:    if(ptr)
10:      i++;
11:
12:    return(i);
13:  }

  erik@dutein75:/fibre0/erik >install/bin/g++ -Wall -c frob.c
  erik@dutein75:/fibre0/erik >

Compiles without warnings.

Please CC all followups to me, because I'm not subscribed to this mailing
list.


Erik

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031,  2600 GA Delft, The Netherlands
Phone: +31-15-2785859  Fax: +31-15-2781843  Email J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/


diff -u -r gcc-2.95.2-pristine/gcc/cp/typeck.c gcc-2.95.2/gcc/cp/typeck.c
--- gcc-2.95.2-pristine/gcc/cp/typeck.c Mon Aug  9 10:46:20 1999
+++ gcc-2.95.2/gcc/cp/typeck.c  Mon Feb 28 23:35:12 2000
@@ -6808,6 +6808,15 @@
          else
            cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
        }
+      /* And we should warn if assigning non-BOOLEAN_TYPE to BOOLEAN_TYPE.  */
+      else if (coder != BOOLEAN_TYPE && codel == BOOLEAN_TYPE)
+       {
+         if (fndecl)
+           cp_warning ("`%T' used for argument %P of `%D'",
+                       rhstype, parmnum, fndecl);
+         else
+           cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
+       }
       /* And we should warn if assigning a negative value to
         an unsigned variable.  */
       else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)







More information about the Gcc-patches mailing list