Home | History | Annotate | Line # | Download | only in lint1
msg_115.c revision 1.9
      1 /*	$NetBSD: msg_115.c,v 1.9 2021/08/16 16:19:47 rillig Exp $	*/
      2 # 3 "msg_115.c"
      3 
      4 // Test for message: %soperand of '%s' must be modifiable lvalue [115]
      5 
      6 void
      7 example(const int *const_ptr)
      8 {
      9 
     10 	*const_ptr = 3;		/* expect: 115 */
     11 	*const_ptr += 1;	/* expect: 115 */
     12 	*const_ptr -= 4;	/* expect: 115 */
     13 	*const_ptr *= 1;	/* expect: 115 */
     14 	*const_ptr /= 5;	/* expect: 115 */
     15 	*const_ptr %= 9;	/* expect: 115 */
     16 	(*const_ptr)++;		/* expect: 115 */
     17 
     18 	/* In the next example, the left operand is not an lvalue at all. */
     19 	/* expect+1: error: left operand of '=' must be lvalue [114] */
     20 	(const_ptr + 3) = const_ptr;
     21 }
     22 
     23 typedef struct {
     24 	int const member;
     25 } const_member;
     26 
     27 void take_const_member(const_member);
     28 
     29 /*
     30  * Before init.c 1.208 from 2021-08-14 and decl.c 1.221 from 2021-08-10,
     31  * lint issued a wrong "warning: left operand of '%s' must be modifiable
     32  * lvalue", even in cases where the left operand was being initialized
     33  * instead of overwritten.
     34  *
     35  * See initialization_expr_using_op, typeok_assign, has_constant_member.
     36  * See C99 6.2.5p25.
     37  */
     38 const_member
     39 initialize_const_struct_member(void)
     40 {
     41 	/* In a simple initialization, const members can be assigned. */
     42 	const_member cm1 = (const_member) { 12345 };
     43 
     44 	if (cm1.member != 0)
     45 		/* In a function call, const members can be assigned. */
     46 		take_const_member(cm1);
     47 
     48 	struct {
     49 		const_member member;
     50 	} cm2 = {
     51 	    /* In a nested initialization, const members can be assigned. */
     52 	    cm1,
     53 	};
     54 	if (cm2.member.member != 0) {
     55 	}
     56 
     57 	/* In a return statement, const members can be assigned. */
     58 	return cm1;
     59 }
     60