xref: /netbsd-src/usr.bin/xlint/common/tyname.c (revision aef5eb5f59cdfe8314f1b5f78ac04eb144e44010)
1 /*	$NetBSD: tyname.c,v 1.53 2022/07/03 14:35:54 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.53 2022/07/03 14:35:54 rillig Exp $");
39 #endif
40 
41 #include <limits.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <err.h>
45 
46 #if defined(IS_LINT1)
47 #include "lint1.h"
48 #else
49 #include "lint2.h"
50 #endif
51 
52 #ifndef INTERNAL_ERROR
53 #define INTERNAL_ERROR(fmt, args...) \
54 	do { \
55 		(void)warnx("%s, %d: " fmt, __FILE__, __LINE__, ##args); \
56 		abort(); \
57 	} while (false)
58 #endif
59 
60 /* A tree of strings. */
61 typedef struct name_tree_node {
62 	const char *ntn_name;
63 	struct name_tree_node *ntn_less;
64 	struct name_tree_node *ntn_greater;
65 } name_tree_node;
66 
67 /* A growable string buffer. */
68 typedef struct buffer {
69 	size_t	len;
70 	size_t	cap;
71 	char *	data;
72 } buffer;
73 
74 static name_tree_node *type_names;
75 
76 static name_tree_node *
77 new_name_tree_node(const char *name)
78 {
79 	name_tree_node *n;
80 
81 	n = xmalloc(sizeof(*n));
82 	n->ntn_name = xstrdup(name);
83 	n->ntn_less = NULL;
84 	n->ntn_greater = NULL;
85 	return n;
86 }
87 
88 /* Return the canonical instance of the string, with unlimited lifetime. */
89 static const char *
90 intern(const char *name)
91 {
92 	name_tree_node *n = type_names, **next;
93 	int cmp;
94 
95 	if (n == NULL) {
96 		n = new_name_tree_node(name);
97 		type_names = n;
98 		return n->ntn_name;
99 	}
100 
101 	while ((cmp = strcmp(name, n->ntn_name)) != 0) {
102 		next = cmp < 0 ? &n->ntn_less : &n->ntn_greater;
103 		if (*next == NULL) {
104 			*next = new_name_tree_node(name);
105 			return (*next)->ntn_name;
106 		}
107 		n = *next;
108 	}
109 	return n->ntn_name;
110 }
111 
112 static void
113 buf_init(buffer *buf)
114 {
115 	buf->len = 0;
116 	buf->cap = 128;
117 	buf->data = xmalloc(buf->cap);
118 	buf->data[0] = '\0';
119 }
120 
121 static void
122 buf_done(buffer *buf)
123 {
124 	free(buf->data);
125 }
126 
127 static void
128 buf_add(buffer *buf, const char *s)
129 {
130 	size_t len = strlen(s);
131 
132 	while (buf->len + len + 1 >= buf->cap) {
133 		buf->data = xrealloc(buf->data, 2 * buf->cap);
134 		buf->cap = 2 * buf->cap;
135 	}
136 
137 	memcpy(buf->data + buf->len, s, len + 1);
138 	buf->len += len;
139 }
140 
141 static void
142 buf_add_int(buffer *buf, int n)
143 {
144 	char num[1 + sizeof(n) * CHAR_BIT + 1];
145 
146 	(void)snprintf(num, sizeof(num), "%d", n);
147 	buf_add(buf, num);
148 }
149 
150 const char *
151 tspec_name(tspec_t t)
152 {
153 	const char *name = ttab[t].tt_name;
154 	if (name == NULL)
155 		INTERNAL_ERROR("tspec_name(%d)", t);
156 	return name;
157 }
158 
159 static void
160 type_name_of_function(buffer *buf, const type_t *tp)
161 {
162 	const char *sep = "";
163 
164 	buf_add(buf, "(");
165 	if (tp->t_proto) {
166 #ifdef IS_LINT1
167 		sym_t *arg;
168 
169 		arg = tp->t_args;
170 		if (arg == NULL)
171 			buf_add(buf, "void");
172 		for (; arg != NULL; arg = arg->s_next) {
173 			buf_add(buf, sep), sep = ", ";
174 			buf_add(buf, type_name(arg->s_type));
175 		}
176 #else
177 		type_t **argtype;
178 
179 		argtype = tp->t_args;
180 		if (argtype == NULL)
181 			buf_add(buf, "void");
182 		for (; *argtype != NULL; argtype++) {
183 			buf_add(buf, sep), sep = ", ";
184 			buf_add(buf, type_name(*argtype));
185 		}
186 #endif
187 	}
188 	if (tp->t_vararg) {
189 		buf_add(buf, sep);
190 		buf_add(buf, "...");
191 	}
192 	buf_add(buf, ") returning ");
193 	buf_add(buf, type_name(tp->t_subt));
194 }
195 
196 static void
197 type_name_of_struct_or_union(buffer *buf, const type_t *tp)
198 {
199 	buf_add(buf, " ");
200 #ifdef IS_LINT1
201 	if (tp->t_str->sou_tag->s_name == unnamed &&
202 	    tp->t_str->sou_first_typedef != NULL) {
203 		buf_add(buf, "typedef ");
204 		buf_add(buf, tp->t_str->sou_first_typedef->s_name);
205 	} else {
206 		buf_add(buf, tp->t_str->sou_tag->s_name);
207 	}
208 #else
209 	buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
210 #endif
211 }
212 
213 static void
214 type_name_of_enum(buffer *buf, const type_t *tp)
215 {
216 	buf_add(buf, " ");
217 #ifdef IS_LINT1
218 	if (tp->t_enum->en_tag->s_name == unnamed &&
219 	    tp->t_enum->en_first_typedef != NULL) {
220 		buf_add(buf, "typedef ");
221 		buf_add(buf, tp->t_enum->en_first_typedef->s_name);
222 	} else {
223 		buf_add(buf, tp->t_enum->en_tag->s_name);
224 	}
225 #else
226 	buf_add(buf, tp->t_isuniqpos ? "*anonymous*" : tp->t_tag->h_name);
227 #endif
228 }
229 
230 static void
231 type_name_of_array(buffer *buf, const type_t *tp)
232 {
233 	buf_add(buf, "[");
234 #ifdef IS_LINT1
235 	if (tp->t_incomplete_array)
236 		buf_add(buf, "unknown_size");
237 	else
238 		buf_add_int(buf, tp->t_dim);
239 #else
240 	buf_add_int(buf, tp->t_dim);
241 #endif
242 	buf_add(buf, "]");
243 	buf_add(buf, " of ");
244 	buf_add(buf, type_name(tp->t_subt));
245 }
246 
247 const char *
248 type_name(const type_t *tp)
249 {
250 	tspec_t t;
251 	buffer buf;
252 	const char *name;
253 
254 	if (tp == NULL)
255 		return "(null)";
256 
257 	if ((t = tp->t_tspec) == INT && tp->t_is_enum)
258 		t = ENUM;
259 
260 	buf_init(&buf);
261 	if (tp->t_const)
262 		buf_add(&buf, "const ");
263 	if (tp->t_volatile)
264 		buf_add(&buf, "volatile ");
265 
266 #ifdef IS_LINT1
267 	if (is_struct_or_union(t) && tp->t_str->sou_incomplete)
268 		buf_add(&buf, "incomplete ");
269 #endif
270 	buf_add(&buf, tspec_name(t));
271 
272 #ifdef IS_LINT1
273 	if (tp->t_bitfield) {
274 		buf_add(&buf, ":");
275 		buf_add_int(&buf, (int)tp->t_flen);
276 	}
277 #endif
278 
279 	switch (t) {
280 	case PTR:
281 		buf_add(&buf, " to ");
282 		buf_add(&buf, type_name(tp->t_subt));
283 		break;
284 	case ENUM:
285 		type_name_of_enum(&buf, tp);
286 		break;
287 	case STRUCT:
288 	case UNION:
289 		type_name_of_struct_or_union(&buf, tp);
290 		break;
291 	case ARRAY:
292 		type_name_of_array(&buf, tp);
293 		break;
294 	case FUNC:
295 		type_name_of_function(&buf, tp);
296 		break;
297 	default:
298 		break;
299 	}
300 
301 	name = intern(buf.data);
302 	buf_done(&buf);
303 	return name;
304 }
305