home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / gnu / gcc / bug / 2127 < prev    next >
Encoding:
Text File  |  1992-08-14  |  3.0 KB  |  95 lines

  1. Newsgroups: gnu.gcc.bug
  2. Path: sparky!uunet!cis.ohio-state.edu!twinsun.COM!eggert
  3. From: eggert@twinsun.COM (Paul Eggert)
  4. Subject: GCC 2.2.2 doesn't warn about `if (x = 0)' etc
  5. Message-ID: <9208140022.AA29648@farside.twinsun.com>
  6. Sender: gnulists@ai.mit.edu
  7. Organization: GNUs Not Usenet
  8. Distribution: gnu
  9. Date: Fri, 14 Aug 1992 00:22:47 GMT
  10. Approved: bug-gcc@prep.ai.mit.edu
  11. Lines: 82
  12.  
  13. GCC 2.2.2 doesn't warn about the common C coding error of using `=' when you
  14. meant to use `=='.  For example, in the program
  15.  
  16.     int main(int argc, char **argv) {
  17.         char **a = argv;
  18.         for (;;) {
  19.             printf("%s", *a);
  20.             if (*++a = 0)
  21.                 break;
  22.             printf(" ");
  23.         }
  24.         printf("\n");
  25.         return 0;
  26.     }
  27.  
  28. there's no option to make GCC warn that `if (*++a = 0) ...' does not do what
  29. the programmer probably expected.
  30.  
  31. The following patch causes GCC to warn about using `=' in a truth value
  32. context if the -Wparentheses option is specified.  If you really mean `='
  33. instead of `==', you can prevent the warning by writing an extra set of
  34. parentheses around the assignment, e.g. `if ((*++a = 0)) ...'.  The extra
  35. parentheses help signal the reader of the unusual case where an assignment
  36. appears where an equality test is normally expected.
  37.  
  38.  
  39. Thu Aug 13 23:59:47 1992  Paul Eggert  (eggert@twinsun.com)
  40.  
  41.     * c-common.c (truthvalue_conversion): Optionally warn if an
  42.     unparenthesized assignment is used as a truth value.
  43.     * c-parse.y (expr_no_commas): Mark assignments for warning.
  44.  
  45. ===================================================================
  46. RCS file: c-common.c,v
  47. retrieving revision 2.2
  48. diff -c -r2.2 c-common.c
  49. *** c-common.c    1992/05/26 19:22:26    2.2
  50. --- c-common.c    1992/08/13 23:59:47
  51. ***************
  52. *** 879,884 ****
  53. --- 879,889 ----
  54.                     fold (build1 (NOP_EXPR,
  55.                           TREE_TYPE (TREE_OPERAND (expr, 0)),
  56.                           TREE_OPERAND (expr, 1))), 1);
  57. +     case MODIFY_EXPR:
  58. +       if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
  59. +     warning ("suggest parentheses around assignment used as truth value");
  60. +       break;
  61.       }
  62.   
  63.     return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
  64. ===================================================================
  65. RCS file: c-parse.y,v
  66. retrieving revision 2.2.2.2
  67. diff -c -r2.2.2.2 c-parse.y
  68. *** c-parse.y    1992/08/01 03:25:39    2.2.2.2
  69. --- c-parse.y    1992/08/13 23:59:47
  70. ***************
  71. *** 428,436 ****
  72.       | expr_no_commas '?' xexpr ':' expr_no_commas
  73.           { $$ = build_conditional_expr ($1, $3, $5); }
  74.       | expr_no_commas '=' expr_no_commas
  75. !         { $$ = build_modify_expr ($1, NOP_EXPR, $3); }
  76.       | expr_no_commas ASSIGN expr_no_commas
  77. !         { $$ = build_modify_expr ($1, $2, $3); }
  78.       ;
  79.   
  80.   primary:
  81. --- 428,438 ----
  82.       | expr_no_commas '?' xexpr ':' expr_no_commas
  83.           { $$ = build_conditional_expr ($1, $3, $5); }
  84.       | expr_no_commas '=' expr_no_commas
  85. !         { $$ = build_modify_expr ($1, NOP_EXPR, $3);
  86. !           C_SET_EXP_ORIGINAL_CODE ($$, MODIFY_EXPR); }
  87.       | expr_no_commas ASSIGN expr_no_commas
  88. !         { $$ = build_modify_expr ($1, $2, $3);
  89. !           C_SET_EXP_ORIGINAL_CODE ($$, MODIFY_EXPR); }
  90.       ;
  91.   
  92.   primary:
  93.  
  94.