Home | History | Annotate | Line # | Download | only in lint1
msg_171.c revision 1.7
      1 /*	$NetBSD: msg_171.c,v 1.7 2021/12/21 23:12:21 rillig Exp $	*/
      2 # 3 "msg_171.c"
      3 
      4 // Test for message: cannot assign to '%s' from '%s' [171]
      5 
      6 struct s {
      7 	int member;
      8 };
      9 
     10 /*ARGSUSED*/
     11 void
     12 example(int i, void *vp, struct s *s)
     13 {
     14 	i = *s;			/* expect: 171 */
     15 	*s = i;			/* expect: 171 */
     16 
     17 	vp = *s;		/* expect: 171 */
     18 	*s = vp;		/* expect: 171 */
     19 }
     20 
     21 /*
     22  * C99 6.5.2.5 says that a compound literal evaluates to an unnamed object
     23  * with automatic storage duration, like any normal named object.  It is an
     24  * lvalue, which means that it is possible to take the address of the object.
     25  * Seen in external/mpl/bind/dist/lib/dns/rbtdb.c, update_rrsetstats.
     26  *
     27  * Before init.c 1.111 from 2021-03-23, lint could not handle these nested
     28  * initializations (the outer one for the variable 'p', the inner one for the
     29  * compound literal) and wrongly complained about a type mismatch between
     30  * 'struct point' and 'pointer to struct point'.
     31  */
     32 void
     33 pointer_to_compound_literal(void)
     34 {
     35 	struct point {
     36 		int x;
     37 		int y;
     38 	};
     39 	struct point *p = &(struct point){
     40 		12, 5,
     41 	};
     42 
     43 	/*
     44 	 * A sizeof expression is another way to create nested
     45 	 * initializations.
     46 	 */
     47 	struct point p2 = {
     48 		(int)sizeof(struct point){
     49 			(int)sizeof(struct point){
     50 				(int)sizeof(struct point){
     51 					(int)sizeof(struct point){
     52 						0,
     53 						0,
     54 					},
     55 					0,
     56 				},
     57 				0,
     58 			},
     59 			0,
     60 		},
     61 		0,
     62 	};
     63 }
     64