Module Name:    src
Committed By:   rillig
Date:           Tue Mar 23 17:36:56 UTC 2021

Modified Files:
        src/usr.bin/xlint/lint1: cgram.y decl.c externs1.h init.c

Log Message:
lint: add indirection for accessing the current initialization

This indirection will be needed to handle nested initializations, which
are a new feature of C99.  These are currently not handled correctly,
see msg_171.c.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.196 -r1.197 src/usr.bin/xlint/lint1/cgram.y
cvs rdiff -u -r1.157 -r1.158 src/usr.bin/xlint/lint1/decl.c
cvs rdiff -u -r1.82 -r1.83 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.109 -r1.110 src/usr.bin/xlint/lint1/init.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/xlint/lint1/cgram.y
diff -u src/usr.bin/xlint/lint1/cgram.y:1.196 src/usr.bin/xlint/lint1/cgram.y:1.197
--- src/usr.bin/xlint/lint1/cgram.y:1.196	Sun Mar 21 14:49:21 2021
+++ src/usr.bin/xlint/lint1/cgram.y	Tue Mar 23 17:36:55 2021
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.196 2021/03/21 14:49:21 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.197 2021/03/23 17:36:55 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: cgram.y,v 1.196 2021/03/21 14:49:21 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.197 2021/03/23 17:36:55 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -1885,24 +1885,24 @@ term:
 	    expr_statement_list {
 		block_level--;
 		mem_block_level--;
-		initsym = mktempsym(duptyp($4->tn_type));
+		*current_initsym() = mktempsym(duptyp($4->tn_type));
 		mem_block_level++;
 		block_level++;
 		/* ({ }) is a GCC extension */
 		gnuism(320);
 	 } compound_statement_rbrace T_RPAREN {
-		$$ = new_name_node(initsym, 0);
+		$$ = new_name_node(*current_initsym(), 0);
 	 }
 	| T_LPAREN compound_statement_lbrace expr_statement_list {
 		block_level--;
 		mem_block_level--;
-		initsym = mktempsym($3->tn_type);
+		*current_initsym() = mktempsym($3->tn_type);
 		mem_block_level++;
 		block_level++;
 		/* ({ }) is a GCC extension */
 		gnuism(320);
 	 } compound_statement_rbrace T_RPAREN {
-		$$ = new_name_node(initsym, 0);
+		$$ = new_name_node(*current_initsym(), 0);
 	 }
 	| term T_INCDEC {
 		$$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
@@ -1995,7 +1995,7 @@ term:
 		if (!Sflag)
 			 /* compound literals are a C9X/GCC extension */
 			 gnuism(319);
-		$$ = new_name_node(initsym, 0);
+		$$ = new_name_node(*current_initsym(), 0);
 	  }
 	;
 

Index: src/usr.bin/xlint/lint1/decl.c
diff -u src/usr.bin/xlint/lint1/decl.c:1.157 src/usr.bin/xlint/lint1/decl.c:1.158
--- src/usr.bin/xlint/lint1/decl.c:1.157	Sun Mar 21 20:18:45 2021
+++ src/usr.bin/xlint/lint1/decl.c	Tue Mar 23 17:36:56 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.157 2021/03/21 20:18:45 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.158 2021/03/23 17:36:56 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: decl.c,v 1.157 2021/03/21 20:18:45 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.158 2021/03/23 17:36:56 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -1896,8 +1896,8 @@ declare(sym_t *decl, bool initflg, sbuf_
 {
 	char *s;
 
-	initerr = false;
-	initsym = decl;
+	*current_initerr() = false;
+	*current_initsym() = decl;
 
 	switch (dcs->d_ctx) {
 	case EXTERN:
@@ -1929,7 +1929,7 @@ declare(sym_t *decl, bool initflg, sbuf_
 		break;
 	}
 
-	if (initflg && !initerr)
+	if (initflg && !*current_initerr())
 		initstack_init();
 }
 
@@ -1946,7 +1946,7 @@ decl1ext(sym_t *dsym, bool initflg)
 
 	check_type(dsym);
 
-	if (initflg && !(initerr = check_init(dsym)))
+	if (initflg && !(*current_initerr() = check_init(dsym)))
 		dsym->s_def = DEF;
 
 	/*
@@ -2424,7 +2424,7 @@ declare_argument(sym_t *sym, bool initfl
 	if (initflg) {
 		/* cannot initialize parameter: %s */
 		error(52, sym->s_name);
-		initerr = true;
+		*current_initerr() = true;
 	}
 
 	if ((t = sym->s_type->t_tspec) == ARRAY) {
@@ -2759,7 +2759,7 @@ declare_local(sym_t *dsym, bool initflg)
 
 	}
 
-	if (initflg && !(initerr = check_init(dsym))) {
+	if (initflg && !(*current_initerr() = check_init(dsym))) {
 		dsym->s_def = DEF;
 		mark_as_set(dsym);
 	}

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.82 src/usr.bin/xlint/lint1/externs1.h:1.83
--- src/usr.bin/xlint/lint1/externs1.h:1.82	Sun Mar 21 19:08:10 2021
+++ src/usr.bin/xlint/lint1/externs1.h	Tue Mar 23 17:36:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.82 2021/03/21 19:08:10 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.83 2021/03/23 17:36:56 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -292,8 +292,8 @@ extern	void	bitfieldtype(int);
 /*
  * init.c
  */
-extern	bool	initerr;
-extern	sym_t	*initsym;
+extern	bool	*current_initerr(void);
+extern	sym_t	**current_initsym(void);
 
 extern	void	initstack_init(void);
 extern	void	init_rbrace(void);

Index: src/usr.bin/xlint/lint1/init.c
diff -u src/usr.bin/xlint/lint1/init.c:1.109 src/usr.bin/xlint/lint1/init.c:1.110
--- src/usr.bin/xlint/lint1/init.c:1.109	Mon Mar 22 19:29:43 2021
+++ src/usr.bin/xlint/lint1/init.c	Tue Mar 23 17:36:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: init.c,v 1.109 2021/03/22 19:29:43 rillig Exp $	*/
+/*	$NetBSD: init.c,v 1.110 2021/03/23 17:36:56 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: init.c,v 1.109 2021/03/22 19:29:43 rillig Exp $");
+__RCSID("$NetBSD: init.c,v 1.110 2021/03/23 17:36:56 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -199,26 +199,60 @@ typedef struct namlist {
 	struct namlist *n_next;
 } namlist_t;
 
+struct initialization {
+	/*
+	 * initerr is set as soon as a fatal error occurred in an initialization.
+	 * The effect is that the rest of the initialization is ignored (parsed
+	 * by yacc, expression trees built, but no initialization takes place).
+	 */
+	bool	initerr;
 
-/*
- * initerr is set as soon as a fatal error occurred in an initialization.
- * The effect is that the rest of the initialization is ignored (parsed
- * by yacc, expression trees built, but no initialization takes place).
- */
-bool	initerr;
+	/* Pointer to the symbol which is to be initialized. */
+	sym_t	*initsym;
+
+	/* Points to the top element of the initialization stack. */
+	initstack_element *initstk;
 
-/* Pointer to the symbol which is to be initialized. */
-sym_t	*initsym;
+	/* Points to a c9x named member. */
+	namlist_t *namedmem;
 
-/* Points to the top element of the initialization stack. */
-static initstack_element *initstk;
+	struct initialization *next;
+};
 
-/* Points to a c9x named member; */
-static namlist_t *namedmem = NULL;
+static struct initialization init;
 
 
 static	bool	init_array_using_string(tnode_t *);
 
+bool *
+current_initerr(void)
+{
+	return &init.initerr;
+}
+
+sym_t **
+current_initsym(void)
+{
+	return &init.initsym;
+}
+
+static namlist_t **
+current_namedmem(void)
+{
+	return &init.namedmem;
+}
+
+static initstack_element **
+current_initstk(void)
+{
+	return &init.initstk;
+}
+
+#define initerr (*current_initerr())
+#define initsym (*current_initsym())
+#define initstk (*current_initstk())
+#define namedmem (*current_namedmem())
+
 #ifndef DEBUG
 
 #define debug_printf(fmt, ...)	do { } while (false)

Reply via email to