Article View: gnu.gcc.bug
Article #942GCC 2.2.2 doesn't warn about `if (x = 0)' etc
From: Paul Eggert
Date: Fri, 14 Aug 1992 00:22
Date: Fri, 14 Aug 1992 00:22
81 lines
3159 bytes
3159 bytes
GCC 2.2.2 doesn't warn about the common C coding error of using `=' when you
meant to use `=='. For example, in the program
int main(int argc, char **argv) {
char **a = argv;
for (;;) {
printf("%s", *a);
if (*++a = 0)
break;
printf(" ");
}
printf("\n");
return 0;
}
there's no option to make GCC warn that `if (*++a = 0) ...' does not do what
the programmer probably expected.
The following patch causes GCC to warn about using `=' in a truth value
context if the -Wparentheses option is specified. If you really mean `='
instead of `==', you can prevent the warning by writing an extra set of
parentheses around the assignment, e.g. `if ((*++a = 0)) ...'. The extra
parentheses help signal the reader of the unusual case where an assignment
appears where an equality test is normally expected.
Thu Aug 13 23:59:47 1992 Paul Eggert (eggert@twinsun.com)
* c-common.c (truthvalue_conversion): Optionally warn if an
unparenthesized assignment is used as a truth value.
* c-parse.y (expr_no_commas): Mark assignments for warning.
===================================================================
RCS file: c-common.c,v
retrieving revision 2.2
diff -c -r2.2 c-common.c
*** c-common.c 1992/05/26 19:22:26 2.2
--- c-common.c 1992/08/13 23:59:47
***************
*** 879,884 ****
--- 879,889 ----
fold (build1 (NOP_EXPR,
TREE_TYPE (TREE_OPERAND (expr, 0)),
TREE_OPERAND (expr, 1))), 1);
+
+ case MODIFY_EXPR:
+ if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
+ warning ("suggest parentheses around assignment used as truth value");
+ break;
}
return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
===================================================================
RCS file: c-parse.y,v
retrieving revision 2.2.2.2
diff -c -r2.2.2.2 c-parse.y
*** c-parse.y 1992/08/01 03:25:39 2.2.2.2
--- c-parse.y 1992/08/13 23:59:47
***************
*** 428,436 ****
| expr_no_commas '?' xexpr ':' expr_no_commas
{ $$ = build_conditional_expr ($1, $3, $5); }
| expr_no_commas '=' expr_no_commas
! { $$ = build_modify_expr ($1, NOP_EXPR, $3); }
| expr_no_commas ASSIGN expr_no_commas
! { $$ = build_modify_expr ($1, $2, $3); }
;
primary:
--- 428,438 ----
| expr_no_commas '?' xexpr ':' expr_no_commas
{ $$ = build_conditional_expr ($1, $3, $5); }
| expr_no_commas '=' expr_no_commas
! { $$ = build_modify_expr ($1, NOP_EXPR, $3);
! C_SET_EXP_ORIGINAL_CODE ($$, MODIFY_EXPR); }
| expr_no_commas ASSIGN expr_no_commas
! { $$ = build_modify_expr ($1, $2, $3);
! C_SET_EXP_ORIGINAL_CODE ($$, MODIFY_EXPR); }
;
primary:
Technical Details
Message-ID:
<9208140022.AA29648@farside.twinsun.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!twinsun.COM!eggertShow Full Headers
Complete Article Headers
Newsgroups: gnu.gcc.bug Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!twinsun.COM!eggert From: eggert@twinsun.COM (Paul Eggert) Subject: GCC 2.2.2 doesn't warn about `if (x = 0)' etc Message-ID: <9208140022.AA29648@farside.twinsun.com> Sender: gnulists@ai.mit.edu Organization: GNUs Not Usenet Distribution: gnu Date: Fri, 14 Aug 1992 00:22:47 GMT Approved: bug-gcc@prep.ai.mit.edu Lines: 82