xref: /netbsd-src/usr.bin/xlint/common/tyname.c (revision a8016b51bcf5204ff836e46c2432dc27e5b12586)
1 /*	$NetBSD: tyname.c,v 1.65 2024/12/08 17:12:00 rillig Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 #if defined(__RCSID)
38 __RCSID("$NetBSD: tyname.c,v 1.65 2024/12/08 17:12:00 rillig Exp $");
39 #endif
40 
41 #include <assert.h>
42 #include <limits.h>
43 #include <string.h>
44 #include <stdlib.h>
45 
46 #if IS_LINT1
47 #include "lint1.h"
48 #else
49 #include "lint2.h"
50 #endif
51 
52 /* A tree of strings. */
53 typedef struct name_tree_node {
54 	const char *ntn_name;
55 	struct name_tree_node *ntn_less;
56 	struct name_tree_node *ntn_greater;
57 } name_tree_node;
58 
59 static name_tree_node *type_names;
60 
61 static name_tree_node *
62 new_name_tree_node(const char *name)
63 {
64 	name_tree_node *n;
65 
66 	n = xmalloc(sizeof(*n));
67 	n->ntn_name = xstrdup(name);
68 	n->ntn_less = NULL;
69 	n->ntn_greater = NULL;
70 	return n;
71 }
72 
73 /* Return the canonical instance of the string, with unlimited lifetime. */
74 static const char *
75 intern(const char *name)
76 {
77 	name_tree_node *n = type_names, **next;
78 	int cmp;
79 
80 	if (n == NULL) {
81 		n = new_name_tree_node(name);
82 		type_names = n;
83 		return n->ntn_name;
84 	}
85 
86 	while ((cmp = strcmp(name, n->ntn_name)) != 0) {
87 		next = cmp < 0 ? &n->ntn_less : &n->ntn_greater;
88 		if (*next == NULL) {
89 			*next = new_name_tree_node(name);
90 			return (*next)->ntn_name;
91 		}
92 		n = *next;
93 	}
94 	return n->ntn_name;
95 }
96 
97 #if !IS_LINT1
98 static
99 #endif
100 void
101 buf_init(buffer *buf)
102 {
103 	buf->len = 0;
104 	buf->cap = 128;
105 	buf->data = xmalloc(buf->cap);
106 	buf->data[0] = '\0';
107 }
108 
109 static void
110 buf_done(buffer *buf)
111 {
112 	free(buf->data);
113 }
114 
115 static void
116 buf_add_mem(buffer *buf, const char *s, size_t n)
117 {
118 	while (buf->len + n + 1 >= buf->cap) {
119 		buf->cap *= 2;
120 		buf->data = xrealloc(buf->data, buf->cap);
121 	}
122 
123 	memcpy(buf->data + buf->len, s, n);
124 	buf->len += n;
125 	buf->data[buf->len] = '\0';
126 }
127 
128 #if IS_LINT1
129 void
130 buf_add_char(buffer *buf, char c)
131 {
132 	buf_add_mem(buf, &c, 1);
133 }
134 #endif
135 
136 #if !IS_LINT1
137 static
138 #endif
139 void
140 buf_add(buffer *buf, const char *s)
141 {
142 	buf_add_mem(buf, s, strlen(s));
143 }
144 
145 static void
146 buf_add_int(buffer *buf, int n)
147 {
148 	char num[1 + sizeof(n) * CHAR_BIT + 1];
149 
150 	(void)snprintf(num, sizeof(num), "%d", n);
151 	buf_add(buf, num);
152 }
153 
154 const char *
155 tspec_name(tspec_t t)
156 {
157 	const char *name = ttab[t].tt_name;
158 	assert(name != NULL);
159 	return name;
160 }
161 
162 static void
163 type_name_of_function(buffer *buf, const type_t *tp)
164 {
165 	const char *sep = "";
166 
167 	buf_add(buf, "(");
168 	if (tp->t_proto) {
169 #if IS_LINT1
170 		const sym_t *param = tp->u.params;
171 		if (param == NULL)
172 			buf_add(buf, "void");
173 		for (; param != NULL; param = param->s_next) {
174 			buf_add(buf, sep), sep = ", ";
175 			buf_add(buf, type_name(param->s_type));
176 		}
177 #else
178 		type_t **argtype;
179 
180 		argtype = tp->t_args;
181 		if (*argtype == NULL)
182 			buf_add(buf, "void");
183 		for (; *argtype != NULL; argtype++) {
184 			buf_add(buf, sep), sep = ", ";
185 			buf_add(buf, type_name(*argtype));
186 		}
187 #endif
188 	}
189 	if (tp->t_vararg) {
190 		buf_add(buf, sep);
191 		buf_add(buf, "...");
192 	}
193 	buf_add(buf, ") returning ");
194 	buf_add(buf, type_name(tp->t_subt));
195 }
196 
197 static void
198 type_name_of_struct_or_union(buffer *buf, const type_t *tp)
199 {
200 	buf_add(buf, " ");
201 #if IS_LINT1
202 	if (tp->u.sou->sou_tag->s_name == unnamed &&
203 	    tp->u.sou->sou_first_typedef != NULL) {
204 		buf_add(buf, "typedef ");
205 		buf_add(buf, tp->u.sou->sou_first_typedef->s_name);
206 	} else {
207 		buf_add(buf, tp->u.sou->sou_tag->s_name);
208 	}
209 #else
210 	buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
211 #endif
212 }
213 
214 static void
215 type_name_of_enum(buffer *buf, const type_t *tp)
216 {
217 	buf_add(buf, " ");
218 #if IS_LINT1
219 	if (tp->u.enumer->en_tag->s_name == unnamed &&
220 	    tp->u.enumer->en_first_typedef != NULL) {
221 		buf_add(buf, "typedef ");
222 		buf_add(buf, tp->u.enumer->en_first_typedef->s_name);
223 	} else {
224 		buf_add(buf, tp->u.enumer->en_tag->s_name);
225 	}
226 #else
227 	buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
228 #endif
229 }
230 
231 static void
232 type_name_of_array(buffer *buf, const type_t *tp)
233 {
234 	buf_add(buf, "[");
235 #if IS_LINT1
236 	if (tp->t_incomplete_array)
237 		buf_add(buf, "unknown_size");
238 	else
239 		buf_add_int(buf, tp->u.dimension);
240 #else
241 	buf_add_int(buf, tp->t_dim);
242 #endif
243 	buf_add(buf, "]");
244 	buf_add(buf, " of ");
245 	buf_add(buf, type_name(tp->t_subt));
246 }
247 
248 const char *
249 type_name(const type_t *tp)
250 {
251 	tspec_t t;
252 	buffer buf;
253 	const char *name;
254 
255 	if (tp == NULL)
256 		return "(null)";
257 
258 	if ((t = tp->t_tspec) == INT && tp->t_is_enum)
259 		t = ENUM;
260 
261 	buf_init(&buf);
262 	if (tp->t_const)
263 		buf_add(&buf, "const ");
264 	if (tp->t_volatile)
265 		buf_add(&buf, "volatile ");
266 #if IS_LINT1
267 	if (tp->t_noreturn)
268 		buf_add(&buf, "noreturn ");
269 #endif
270 
271 #if IS_LINT1
272 	if (is_struct_or_union(t) && tp->u.sou->sou_incomplete)
273 		buf_add(&buf, "incomplete ");
274 #endif
275 	buf_add(&buf, tspec_name(t));
276 
277 #if IS_LINT1
278 	if (tp->t_bitfield) {
279 		buf_add(&buf, ":");
280 		buf_add_int(&buf, (int)tp->t_bit_field_width);
281 	}
282 #endif
283 
284 	switch (t) {
285 	case PTR:
286 		buf_add(&buf, " to ");
287 		buf_add(&buf, type_name(tp->t_subt));
288 		break;
289 	case ENUM:
290 		type_name_of_enum(&buf, tp);
291 		break;
292 	case STRUCT:
293 	case UNION:
294 		type_name_of_struct_or_union(&buf, tp);
295 		break;
296 	case ARRAY:
297 		type_name_of_array(&buf, tp);
298 		break;
299 	case FUNC:
300 		type_name_of_function(&buf, tp);
301 		break;
302 	default:
303 		break;
304 	}
305 
306 	name = intern(buf.data);
307 	buf_done(&buf);
308 	return name;
309 }
310