Bug 45780 - Warning for arithmetic operations involving C99 _Bool variable
Summary: Warning for arithmetic operations involving C99 _Bool variable
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2010-09-24 18:39 UTC by Uroš Bizjak
Modified: 2022-10-09 23:45 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-09-24 18:44:45


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Uroš Bizjak 2010-09-24 18:39:26 UTC
Pasted from the thread that introduced _Bool in place of "GCC bool":

<quote>

> >> It can be done ultimately, but as a prerequisite, we should have
> >> warnings in -Wextra for all of
> >>
> >> ? boolvar++; ++boolvar;
> >> ? boolvar--; --boolvar;
> >> ? boolvar = nonbool;
> >> ? boolvar & nonbool; boolvar &= nonbool;
> >> ? boolvar | nonbool; boolvar |= nonbool;
> >> ? boolvar ^ nonbool; boolvar ^= nonbool;
> >
> > Fair enough. I have CCed Manuel, perhaps he is interested in this warning.
> 
> I am not sure it fits in -Wconversion. -Wbool-arith perhaps?

It sounds like a warning for a -Wc90-c99-compat or similar option 
(possibly in a more specific option such as -Wbool-arith) - pure C99 code 
has little use for such a warning, it's about code that might be compiled 
either with C99 _Bool, or with C90 unsigned char, and so needs to avoid 
cases where they are incompatible.  Such an option, similar in spirit to 
-Wtraditional, could also allow you to get warnings in C99 mode for things 
currently diagnosed with -std=c90 -pedantic.

</quote>

[1] http://gcc.gnu.org/ml/gcc-patches/2010-09/msg01941.html
Comment 1 Eric Blake 2010-11-03 20:05:00 UTC
Don't forget a warning on implicit conversions from int to bool, such as:

bool f(int i) { return i; }

although it might make sense to avoid warnings on provably 0/1 int conversion to bool, as in:

bool f(int i) { i = !!i; return i; }
Comment 2 Eric Gallager 2015-05-28 18:54:40 UTC
The -Wc90-c99-compat that made it into gcc5 currently warns about any usage of bool whatsoever, not just the specific usages of bool listed in this bug...
Comment 3 Marek Polacek 2015-05-28 22:28:12 UTC
(In reply to Eric Gallager from comment #2)
> The -Wc90-c99-compat that made it into gcc5 currently warns about any usage
> of bool whatsoever, not just the specific usages of bool listed in this
> bug...

Right, that was the point of it.
Comment 4 Eric Gallager 2015-05-29 02:29:05 UTC
(In reply to Marek Polacek from comment #3)
> (In reply to Eric Gallager from comment #2)
> > The -Wc90-c99-compat that made it into gcc5 currently warns about
> > any usage of bool whatsoever, not just the specific usages of bool
> > listed in this bug...
> 
> Right, that was the point of it.

...so is this bug worth keeping open then? It'd seem kinda redundant to me for extra, more-specific warnings about bools to be placed in -Wc90-c99-compat (as was originally proposed), when the flag already prints the more-generalized warnings that it currently does. Or would they get their own separate -Wbool-arith option? If so, what would happen when a user specifies both a hypothetical -Wbool-arith flag along with -Wc90-c99-compat at the same time? I'm just kinda worried that such a situation could lead to duplicated and/or excessive warnings...
Comment 5 Marek Polacek 2015-06-09 09:54:13 UTC
I think -Wbool-arith makes sense as a separate option.  While -Wc90-c99-compat warns about any use of bool (in a declaration, cast, ...), -Wbool-arith would only warn about suspicious uses of boolean vars.
Comment 6 Eric Gallager 2018-09-14 18:29:13 UTC
(In reply to Uroš Bizjak from comment #0)
> Pasted from the thread that introduced _Bool in place of "GCC bool":
> 
> <quote>
> 
> > >> It can be done ultimately, but as a prerequisite, we should have
> > >> warnings in -Wextra for all of
> > >>
> > >> ? boolvar++; ++boolvar;
> > >> ? boolvar--; --boolvar;
> > >> ? boolvar = nonbool;
> > >> ? boolvar & nonbool; boolvar &= nonbool;
> > >> ? boolvar | nonbool; boolvar |= nonbool;
> > >> ? boolvar ^ nonbool; boolvar ^= nonbool;
> > >
> > > Fair enough. I have CCed Manuel, perhaps he is interested in this warning.
> > 

cc-ing him on this, too
Comment 7 Eric Gallager 2019-06-15 04:36:19 UTC
(In reply to Uroš Bizjak from comment #0)
> > >> It can be done ultimately, but as a prerequisite, we should have
> > >> warnings in -Wextra for all of
> > >>
> > >> ? boolvar++; ++boolvar;
> > >> ? boolvar--; --boolvar;
> > >> ? boolvar = nonbool;
> > >> ? boolvar & nonbool; boolvar &= nonbool;
> > >> ? boolvar | nonbool; boolvar |= nonbool;
> > >> ? boolvar ^ nonbool; boolvar ^= nonbool;
> > >
> > > Fair enough. I have CCed Manuel, perhaps he is interested in this warning.
> > 
> > I am not sure it fits in -Wconversion. -Wbool-arith perhaps?
> 

I made a testcase that includes all of those:

$ cat 45780.c
#include <stdbool.h>
#include <stddef.h>

static bool f1(int i)
{
  return i;
}

static bool f2(int i)
{
  i = !!i;
  return i;
}

int main(int argc, char **argv)
{
  bool a = true;
  bool b = a++;
  bool c = ++b;
  bool d = argc;
  bool e = a & argc;
  bool f = b | argc;
  bool g = c ^ argc;
  f &= argc;
  g |= argc;
  e ^= argc;
  if (f1(argc))
    e--;
  else
    --e;
  if (!!argc)
    return ((argv != NULL) ? d : ((f > g) ? e : (f << g)));
  else
    return f2(argc);
}
$

gcc's -Wbool-operation currently catches the increments and decrements, but none of the rest of the operations:

$ /usr/local/bin/gcc -c -Wall -Wextra -Wbool-compare -Wint-in-bool-context -Wparentheses -pedantic -Wconversion 45780.c
45780.c: In function 'main':
45780.c:18:13: warning: increment of a boolean expression [-Wbool-operation]
   18 |   bool b = a++;
      |             ^~
45780.c:19:12: warning: increment of a boolean expression [-Wbool-operation]
   19 |   bool c = ++b;
      |            ^~
45780.c:28:6: warning: decrement of a boolean expression [-Wbool-operation]
   28 |     e--;
      |      ^~
45780.c:30:5: warning: decrement of a boolean expression [-Wbool-operation]
   30 |     --e;
      |     ^~
$