]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/c/c-typeck.c
re PR c/77490 (bit-not (~) on boolean should be warned about)
[thirdparty/gcc.git] / gcc / c / c-typeck.c
index 059ad1fc2e021c632164dcf091b1252566d1f5ca..e5c725603978dd8d5b75a0f3e4b7f697a3d49c85 100644 (file)
@@ -4196,6 +4196,22 @@ build_unary_op (location_t location, enum tree_code code, tree xarg,
          || (typecode == VECTOR_TYPE
              && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
        {
+         tree e = arg;
+
+         /* Warn if the expression has boolean value.  */
+         while (TREE_CODE (e) == COMPOUND_EXPR)
+           e = TREE_OPERAND (e, 1);
+
+         if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
+              || truth_value_p (TREE_CODE (e)))
+             && warning_at (location, OPT_Wbool_operation,
+                            "%<~%> on a boolean expression"))
+           {
+             gcc_rich_location richloc (location);
+             richloc.add_fixit_insert_before (location, "!");
+             inform_at_rich_loc (&richloc, "did you mean to use logical "
+                                 "not?");
+           }
          if (!noconvert)
            arg = default_conversion (arg);
        }
@@ -4306,6 +4322,16 @@ build_unary_op (location_t location, enum tree_code code, tree xarg,
                        "decrement of enumeration value is invalid in C++");
        }
 
+      if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
+       {
+         if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
+           warning_at (location, OPT_Wbool_operation,
+                       "increment of a boolean expression");
+         else
+           warning_at (location, OPT_Wbool_operation,
+                       "decrement of a boolean expression");
+       }
+
       /* Ensure the argument is fully folded inside any SAVE_EXPR.  */
       arg = c_fully_fold (arg, false, NULL);