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