Fri Apr 2 09:45:55 2021 UTC ()
lint: extract filename ID counter into separate function

No functional change.


(rillig)
diff -r1.38 -r1.39 src/usr.bin/xlint/lint1/mem1.c

cvs diff -r1.38 -r1.39 src/usr.bin/xlint/lint1/mem1.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/mem1.c 2021/04/02 09:39:25 1.38
+++ src/usr.bin/xlint/lint1/mem1.c 2021/04/02 09:45:55 1.39
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: mem1.c,v 1.38 2021/04/02 09:39:25 rillig Exp $ */ 1/* $NetBSD: mem1.c,v 1.39 2021/04/02 09:45:55 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#if HAVE_NBTOOL_CONFIG_H 34#if HAVE_NBTOOL_CONFIG_H
35#include "nbtool_config.h" 35#include "nbtool_config.h"
36#endif 36#endif
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39#if defined(__RCSID) && !defined(lint) 39#if defined(__RCSID) && !defined(lint)
40__RCSID("$NetBSD: mem1.c,v 1.38 2021/04/02 09:39:25 rillig Exp $"); 40__RCSID("$NetBSD: mem1.c,v 1.39 2021/04/02 09:45:55 rillig Exp $");
41#endif 41#endif
42 42
43#include <sys/types.h> 43#include <sys/types.h>
44#include <sys/param.h> 44#include <sys/param.h>
45#include <stdlib.h> 45#include <stdlib.h>
46#include <string.h> 46#include <string.h>
47#include <unistd.h> 47#include <unistd.h>
48 48
49#include "lint1.h" 49#include "lint1.h"
50 50
51/* 51/*
52 * Filenames allocated by record_filename are shared. 52 * Filenames allocated by record_filename are shared.
53 */ 53 */
@@ -105,51 +105,57 @@ transform_filename(const char *name, siz @@ -105,51 +105,57 @@ transform_filename(const char *name, siz
105 static char buf[MAXPATHLEN]; 105 static char buf[MAXPATHLEN];
106 const struct filename_replacement *r; 106 const struct filename_replacement *r;
107 107
108 for (r = filename_replacements; r != NULL; r = r->next) 108 for (r = filename_replacements; r != NULL; r = r->next)
109 if (r->orig_len < len && 109 if (r->orig_len < len &&
110 memcmp(name, r->orig, r->orig_len) == 0) 110 memcmp(name, r->orig, r->orig_len) == 0)
111 break; 111 break;
112 if (r == NULL) 112 if (r == NULL)
113 return name; 113 return name;
114 snprintf(buf, sizeof buf, "%s%s", r->repl, name + r->orig_len); 114 snprintf(buf, sizeof buf, "%s%s", r->repl, name + r->orig_len);
115 return buf; 115 return buf;
116} 116}
117 117
 118static int
 119next_filename_id(void)
 120{
 121 static int next_id = 0;
 122
 123 return next_id++;
 124}
 125
118/* 126/*
119 * Return a copy of the filename s with unlimited lifetime. 127 * Return a copy of the filename s with unlimited lifetime.
120 * If the filename is new, write it to the output file. 128 * If the filename is new, write it to the output file.
121 */ 129 */
122const char * 130const char *
123record_filename(const char *s, size_t slen) 131record_filename(const char *s, size_t slen)
124{ 132{
125 const struct filename *existing_fn; 133 const struct filename *existing_fn;
126 struct filename *fn; 134 struct filename *fn;
127 135
128 static int nxt_id = 0; 
129 
130 if (s == NULL) 136 if (s == NULL)
131 return NULL; 137 return NULL;
132 138
133 if ((existing_fn = search_filename(s, slen)) != NULL) 139 if ((existing_fn = search_filename(s, slen)) != NULL)
134 return existing_fn->fn_name; 140 return existing_fn->fn_name;
135 141
136 fn = xmalloc(sizeof(*fn)); 142 fn = xmalloc(sizeof(*fn));
137 /* Do not use strdup() because s is not NUL-terminated.*/ 143 /* Do not use strdup() because s is not NUL-terminated.*/
138 fn->fn_name = xmalloc(slen + 1); 144 fn->fn_name = xmalloc(slen + 1);
139 (void)memcpy(fn->fn_name, s, slen); 145 (void)memcpy(fn->fn_name, s, slen);
140 fn->fn_name[slen] = '\0'; 146 fn->fn_name[slen] = '\0';
141 fn->fn_len = slen; 147 fn->fn_len = slen;
142 fn->fn_id = nxt_id++; 148 fn->fn_id = next_filename_id();
143 fn->fn_next = filenames; 149 fn->fn_next = filenames;
144 filenames = fn; 150 filenames = fn;
145 151
146 /* Write the ID of this filename to the output file. */ 152 /* Write the ID of this filename to the output file. */
147 outclr(); 153 outclr();
148 outint(fn->fn_id); 154 outint(fn->fn_id);
149 outchar('s'); 155 outchar('s');
150 outstrg(transform_filename(fn->fn_name, fn->fn_len)); 156 outstrg(transform_filename(fn->fn_name, fn->fn_len));
151 157
152 return fn->fn_name; 158 return fn->fn_name;
153} 159}
154 160
155/* Get the ID of a filename. */ 161/* Get the ID of a filename. */