Module Name:    src
Committed By:   rillig
Date:           Thu Feb  1 18:37:07 UTC 2024

Modified Files:
        src/usr.bin/xlint/common: lint.h tyname.c
        src/usr.bin/xlint/lint1: cgram.y ckgetopt.c debug.c emit1.c externs1.h
            init.c lex.c lint1.h tree.c

Log Message:
lint: use standard buffer for storing string values

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/usr.bin/xlint/common/lint.h
cvs rdiff -u -r1.58 -r1.59 src/usr.bin/xlint/common/tyname.c
cvs rdiff -u -r1.484 -r1.485 src/usr.bin/xlint/lint1/cgram.y
cvs rdiff -u -r1.19 -r1.20 src/usr.bin/xlint/lint1/ckgetopt.c
cvs rdiff -u -r1.67 -r1.68 src/usr.bin/xlint/lint1/debug.c
cvs rdiff -u -r1.82 -r1.83 src/usr.bin/xlint/lint1/emit1.c
cvs rdiff -u -r1.212 -r1.213 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.256 -r1.257 src/usr.bin/xlint/lint1/init.c
cvs rdiff -u -r1.204 -r1.205 src/usr.bin/xlint/lint1/lex.c
cvs rdiff -u -r1.210 -r1.211 src/usr.bin/xlint/lint1/lint1.h
cvs rdiff -u -r1.599 -r1.600 src/usr.bin/xlint/lint1/tree.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/common/lint.h
diff -u src/usr.bin/xlint/common/lint.h:1.47 src/usr.bin/xlint/common/lint.h:1.48
--- src/usr.bin/xlint/common/lint.h:1.47	Sat Jan 20 10:25:57 2024
+++ src/usr.bin/xlint/common/lint.h	Thu Feb  1 18:37:06 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: lint.h,v 1.47 2024/01/20 10:25:57 rillig Exp $	*/
+/*	$NetBSD: lint.h,v 1.48 2024/02/01 18:37:06 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -48,6 +48,14 @@
 #include "param.h"
 
 #if IS_LINT1 || IS_LINT2
+
+// Null-terminated character buffer, may contain null characters.
+typedef struct {
+	size_t	len;		/* excluding the terminating '\0' */
+	size_t	cap;
+	char	*data;
+} buffer;
+
 /*
  * Type specifiers, used in type structures (type_t) and elsewhere.
  */

Index: src/usr.bin/xlint/common/tyname.c
diff -u src/usr.bin/xlint/common/tyname.c:1.58 src/usr.bin/xlint/common/tyname.c:1.59
--- src/usr.bin/xlint/common/tyname.c:1.58	Sat Jan 20 10:25:57 2024
+++ src/usr.bin/xlint/common/tyname.c	Thu Feb  1 18:37:06 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: tyname.c,v 1.58 2024/01/20 10:25:57 rillig Exp $	*/
+/*	$NetBSD: tyname.c,v 1.59 2024/02/01 18:37:06 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2005 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tyname.c,v 1.58 2024/01/20 10:25:57 rillig Exp $");
+__RCSID("$NetBSD: tyname.c,v 1.59 2024/02/01 18:37:06 rillig Exp $");
 #endif
 
 #include <assert.h>
@@ -56,13 +56,6 @@ typedef struct name_tree_node {
 	struct name_tree_node *ntn_greater;
 } name_tree_node;
 
-/* A growable string buffer. */
-typedef struct buffer {
-	size_t	len;
-	size_t	cap;
-	char *	data;
-} buffer;
-
 static name_tree_node *type_names;
 
 static name_tree_node *

Index: src/usr.bin/xlint/lint1/cgram.y
diff -u src/usr.bin/xlint/lint1/cgram.y:1.484 src/usr.bin/xlint/lint1/cgram.y:1.485
--- src/usr.bin/xlint/lint1/cgram.y:1.484	Tue Jan 23 19:44:28 2024
+++ src/usr.bin/xlint/lint1/cgram.y	Thu Feb  1 18:37:06 2024
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.484 2024/01/23 19:44:28 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.485 2024/02/01 18:37:06 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: cgram.y,v 1.484 2024/01/23 19:44:28 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.485 2024/02/01 18:37:06 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -148,7 +148,7 @@ is_either(const char *s, const char *a, 
 	type_t	*y_type;
 	tnode_t	*y_tnode;
 	range_t	y_range;
-	strg_t	*y_string;
+	buffer	*y_string;
 	qual_ptr *y_qual_ptr;
 	bool	y_seen_statement;
 	struct generic_association *y_generic;

Index: src/usr.bin/xlint/lint1/ckgetopt.c
diff -u src/usr.bin/xlint/lint1/ckgetopt.c:1.19 src/usr.bin/xlint/lint1/ckgetopt.c:1.20
--- src/usr.bin/xlint/lint1/ckgetopt.c:1.19	Mon Jan 29 21:30:24 2024
+++ src/usr.bin/xlint/lint1/ckgetopt.c	Thu Feb  1 18:37:06 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: ckgetopt.c,v 1.19 2024/01/29 21:30:24 rillig Exp $ */
+/* $NetBSD: ckgetopt.c,v 1.20 2024/02/01 18:37:06 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: ckgetopt.c,v 1.19 2024/01/29 21:30:24 rillig Exp $");
+__RCSID("$NetBSD: ckgetopt.c,v 1.20 2024/02/01 18:37:06 rillig Exp $");
 #endif
 
 #include <stdbool.h>
@@ -80,7 +80,7 @@ static bool
 is_getopt_condition(const tnode_t *tn, char **out_options)
 {
 	const tnode_t *call, *last_arg;
-	const strg_t *str;
+	const buffer *str;
 
 	if (tn != NULL
 	    && tn->tn_op == NE
@@ -99,8 +99,8 @@ is_getopt_condition(const tnode_t *tn, c
 	    && (last_arg = call->tn_right->tn_left)->tn_op == CVT
 	    && last_arg->tn_left->tn_op == ADDR
 	    && last_arg->tn_left->tn_left->tn_op == STRING
-	    && (str = last_arg->tn_left->tn_left->tn_string)->st_char) {
-		*out_options = xstrdup(str->st_chars);
+	    && (str = last_arg->tn_left->tn_left->tn_string)->data != NULL) {
+		*out_options = xstrdup(str->data);
 		return true;
 	}
 	return false;

Index: src/usr.bin/xlint/lint1/debug.c
diff -u src/usr.bin/xlint/lint1/debug.c:1.67 src/usr.bin/xlint/lint1/debug.c:1.68
--- src/usr.bin/xlint/lint1/debug.c:1.67	Mon Jan 29 21:30:24 2024
+++ src/usr.bin/xlint/lint1/debug.c	Thu Feb  1 18:37:06 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.67 2024/01/29 21:30:24 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.68 2024/02/01 18:37:06 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: debug.c,v 1.67 2024/01/29 21:30:24 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.68 2024/02/01 18:37:06 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -235,12 +235,10 @@ debug_node(const tnode_t *tn) // NOLINT(
 		debug_printf("\n");
 		break;
 	case STRING:
-		if (tn->tn_string->st_char)
-			debug_printf(", length %zu, \"%s\"\n",
-			    tn->tn_string->st_len,
-			    tn->tn_string->st_chars);
-		else
-			debug_printf(", length %zu\n", tn->tn_string->st_len);
+		debug_printf(", length %zu\n", tn->tn_string->len);
+		if (tn->tn_string->data != NULL)
+			// TODO: May contain \0 or control characters.
+			debug_printf(", \"%s\"\n", tn->tn_string->data);
 		break;
 	default:
 		debug_printf("\n");

Index: src/usr.bin/xlint/lint1/emit1.c
diff -u src/usr.bin/xlint/lint1/emit1.c:1.82 src/usr.bin/xlint/lint1/emit1.c:1.83
--- src/usr.bin/xlint/lint1/emit1.c:1.82	Mon Jan 29 21:30:24 2024
+++ src/usr.bin/xlint/lint1/emit1.c	Thu Feb  1 18:37:06 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: emit1.c,v 1.82 2024/01/29 21:30:24 rillig Exp $ */
+/* $NetBSD: emit1.c,v 1.83 2024/02/01 18:37:06 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,13 +38,13 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: emit1.c,v 1.82 2024/01/29 21:30:24 rillig Exp $");
+__RCSID("$NetBSD: emit1.c,v 1.83 2024/02/01 18:37:06 rillig Exp $");
 #endif
 
 #include "lint1.h"
 
 static void outtt(sym_t *, sym_t *);
-static void outfstrg(strg_t *);
+static void outfstrg(const char *);
 
 /*
  * Write type into the output file, encoded as follows:
@@ -367,11 +367,11 @@ outcall(const tnode_t *tn, bool retval_u
 			}
 		} else if (arg->tn_op == ADDR &&
 		    arg->tn_left->tn_op == STRING &&
-		    arg->tn_left->tn_string->st_char) {
+		    arg->tn_left->tn_string->data != NULL) {
 			/* constant string, write all format specifiers */
 			outchar('s');
 			outint(n);
-			outfstrg(arg->tn_left->tn_string);
+			outfstrg(arg->tn_left->tn_string->data);
 		}
 	}
 	outchar((char)(retval_discarded ? 'd' : retval_used ? 'u' : 'i'));
@@ -448,12 +448,9 @@ outqchar(char c)
  * writes them, enclosed in "" and quoted if necessary, to the output file
  */
 static void
-outfstrg(strg_t *strg)
+outfstrg(const char *cp)
 {
 
-	lint_assert(strg->st_char);
-	const char *cp = strg->st_chars;
-
 	outchar('"');
 
 	char c = *cp++;

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.212 src/usr.bin/xlint/lint1/externs1.h:1.213
--- src/usr.bin/xlint/lint1/externs1.h:1.212	Tue Jan 23 19:44:28 2024
+++ src/usr.bin/xlint/lint1/externs1.h	Thu Feb  1 18:37:06 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.212 2024/01/23 19:44:28 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.213 2024/02/01 18:37:06 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -275,7 +275,7 @@ type_t *expr_derive_type(type_t *, tspec
 bool is_compiler_builtin(const char *);
 tnode_t *build_constant(type_t *, val_t *);
 tnode_t *build_name(sym_t *, bool);
-tnode_t *build_string(strg_t *);
+tnode_t *build_string(buffer *);
 tnode_t *build_generic_selection(const tnode_t *,
 		    struct generic_association *);
 
@@ -298,7 +298,7 @@ val_t *integer_constant(tnode_t *, bool)
 void expr(tnode_t *, bool, bool, bool, bool);
 void check_expr_misc(const tnode_t *, bool, bool, bool, bool, bool, bool);
 bool constant_addr(const tnode_t *, const sym_t **, ptrdiff_t *);
-strg_t *cat_strings(strg_t *, strg_t *);
+buffer *cat_strings(buffer *, buffer *);
 unsigned int type_size_in_bits(const type_t *);
 sym_t *find_member(const struct_or_union *, const char *);
 

Index: src/usr.bin/xlint/lint1/init.c
diff -u src/usr.bin/xlint/lint1/init.c:1.256 src/usr.bin/xlint/lint1/init.c:1.257
--- src/usr.bin/xlint/lint1/init.c:1.256	Sat Jan 13 11:24:57 2024
+++ src/usr.bin/xlint/lint1/init.c	Thu Feb  1 18:37:06 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: init.c,v 1.256 2024/01/13 11:24:57 rillig Exp $	*/
+/*	$NetBSD: init.c,v 1.257 2024/02/01 18:37:06 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: init.c,v 1.256 2024/01/13 11:24:57 rillig Exp $");
+__RCSID("$NetBSD: init.c,v 1.257 2024/02/01 18:37:06 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -887,7 +887,7 @@ initialization_init_array_from_string(in
 	if (!can_init_character_array(tp, tn))
 		return false;
 
-	size_t len = tn->tn_string->st_len;
+	size_t len = tn->tn_string->len;
 	if (!tp->t_incomplete_array && (size_t)tp->t_dim < len) {
 		/* string literal too long (%lu) for target array (%lu) */
 		warning(187, (unsigned long)len, (unsigned long)tp->t_dim);

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.204 src/usr.bin/xlint/lint1/lex.c:1.205
--- src/usr.bin/xlint/lint1/lex.c:1.204	Mon Jan 29 21:30:25 2024
+++ src/usr.bin/xlint/lint1/lex.c	Thu Feb  1 18:37:06 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.204 2024/01/29 21:30:25 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.205 2024/02/01 18:37:06 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: lex.c,v 1.204 2024/01/29 21:30:25 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.205 2024/02/01 18:37:06 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -1275,12 +1275,11 @@ lex_string(void)
 		/* unterminated string constant */
 		error(258);
 
-	strg_t *strg = xcalloc(1, sizeof(*strg));
-	strg->st_char = true;
-	strg->st_len = s_len;
-	strg->st_chars = s;
+	buffer *str = xcalloc(1, sizeof(*str));
+	str->len = s_len;
+	str->data = s;
 
-	yylval.y_string = strg;
+	yylval.y_string = str;
 	return T_STRING;
 }
 
@@ -1328,11 +1327,10 @@ lex_wide_string(void)
 	free(s);
 	free(ws);
 
-	strg_t *strg = xcalloc(1, sizeof(*strg));
-	strg->st_char = false;
-	strg->st_len = wlen;
+	buffer *str = xcalloc(1, sizeof(*str));
+	str->len = wlen;
 
-	yylval.y_string = strg;
+	yylval.y_string = str;
 	return T_STRING;
 }
 
@@ -1576,8 +1574,8 @@ freeyyv(void *sp, int tok)
 		val_t *val = *(val_t **)sp;
 		free(val);
 	} else if (tok == T_STRING) {
-		strg_t *strg = *(strg_t **)sp;
-		free(strg->st_chars);
-		free(strg);
+		buffer *str = *(buffer **)sp;
+		free(str->data);
+		free(str);
 	}
 }

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.210 src/usr.bin/xlint/lint1/lint1.h:1.211
--- src/usr.bin/xlint/lint1/lint1.h:1.210	Mon Jan 29 21:30:25 2024
+++ src/usr.bin/xlint/lint1/lint1.h	Thu Feb  1 18:37:06 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.210 2024/01/29 21:30:25 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.211 2024/02/01 18:37:06 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -71,19 +71,6 @@ typedef struct {
 	int	p_uniq;			/* uniquifier */
 } pos_t;
 
-/*
- * Strings cannot be referenced simply by a pointer to their first
- * char. This is because strings can contain NUL characters other than the
- * trailing NUL.
- *
- * Strings are stored with a trailing NUL.
- */
-typedef struct strg {
-	bool	st_char;	/* string doesn't have an 'L' prefix */
-	size_t	st_len;		/* length without trailing NUL */
-	char	*st_chars;	/* only if st_char */
-} strg_t;
-
 // TODO: Use bit-fields instead of plain bool, but keep an eye on arm and
 // powerpc, on which NetBSD's GCC 10.5.0 (but not the upstream GCC) generates
 // code that leads to extra 327 warnings, even in msg_327.c, which does not
@@ -324,7 +311,9 @@ typedef struct tnode {
 		} tn_s;
 		sym_t	*_tn_sym;	/* symbol if op == NAME */
 		val_t	_tn_val;	/* value if op == CON */
-		strg_t	*_tn_string;	/* string if op == STRING */
+		buffer	*_tn_string;	/* string if op == STRING; for wide
+					 * char strings, data is NULL but len
+					 * is valid */
 	} tn_u;
 } tnode_t;
 

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.599 src/usr.bin/xlint/lint1/tree.c:1.600
--- src/usr.bin/xlint/lint1/tree.c:1.599	Mon Jan 29 21:30:25 2024
+++ src/usr.bin/xlint/lint1/tree.c	Thu Feb  1 18:37:06 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.599 2024/01/29 21:30:25 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.600 2024/02/01 18:37:06 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.599 2024/01/29 21:30:25 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.600 2024/02/01 18:37:06 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -522,13 +522,13 @@ build_name(sym_t *sym, bool is_funcname)
 }
 
 tnode_t *
-build_string(strg_t *strg)
+build_string(buffer *strg)
 {
-	size_t len = strg->st_len;
+	size_t len = strg->len;
 
 	type_t *tp = expr_zero_alloc(sizeof(*tp), "type");
 	tp->t_tspec = ARRAY;
-	tp->t_subt = gettyp(strg->st_char ? CHAR : WCHAR_TSPEC);
+	tp->t_subt = gettyp(strg->data != NULL ? CHAR : WCHAR_TSPEC);
 	tp->t_dim = (int)(len + 1);
 
 	tnode_t *n = expr_alloc_tnode();
@@ -537,16 +537,13 @@ build_string(strg_t *strg)
 	n->tn_lvalue = true;
 
 	n->tn_string = expr_zero_alloc(sizeof(*n->tn_string), "tnode.string");
-	n->tn_string->st_char = strg->st_char;
-	n->tn_string->st_len = len;
+	n->tn_string->len = len;
 
-	size_t chsize = strg->st_char ? sizeof(char) : sizeof(wchar_t);
-	size_t size = (len + 1) * chsize;
-	if (strg->st_char) {
-		n->tn_string->st_chars = expr_zero_alloc(size,
+	if (strg->data != NULL) {
+		n->tn_string->data = expr_zero_alloc(len + 1,
 		    "tnode.string.data");
-		(void)memcpy(n->tn_string->st_chars, strg->st_chars, size);
-		free(strg->st_chars);
+		(void)memcpy(n->tn_string->data, strg->data, len + 1);
+		free(strg->data);
 	}
 	free(strg);
 
@@ -4691,24 +4688,22 @@ constant_addr(const tnode_t *tn, const s
 }
 
 /* Append s2 to s1, then free s2. */
-strg_t *
-cat_strings(strg_t *s1, strg_t *s2)
+buffer *
+cat_strings(buffer *s1, buffer *s2)
 {
 
-	if (s1->st_char != s2->st_char) {
+	if ((s1->data != NULL) != (s2->data != NULL)) {
 		/* cannot concatenate wide and regular string literals */
 		error(292);
 		return s1;
 	}
 
-	size_t len1 = s1->st_len;
-	size_t len2 = s2->st_len;
-	if (s1->st_char) {
-		s1->st_chars = xrealloc(s1->st_chars, len1 + len2 + 1);
-		memcpy(s1->st_chars + len1, s2->st_chars, len2 + 1);
-		free(s2->st_chars);
+	if (s1->data != NULL) {
+		s1->data = xrealloc(s1->data, s1->len + s2->len + 1);
+		memcpy(s1->data + s1->len, s2->data, s2->len + 1);
+		free(s2->data);
 	}
-	s1->st_len = len1 + len2;
+	s1->len += s2->len;
 	free(s2);
 
 	return s1;

Reply via email to