🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Paul Eggert Fri, 14 Aug 1992 00:22
GCC 2.2.2 doesn't warn about `if (x = 0)' etc
#942
Author: Paul Eggert
Date: Fri, 14 Aug 1992 00:22
81 lines
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:
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads