Sync with gcc.
[deliverable/binutils-gdb.git] / libiberty / testsuite / test-demangle.c
1 /* Demangler test program,
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Zack Weinberg <zack@codesourcery.com
4
5 This file is part of GNU libiberty.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "ansidecl.h"
26 #include <stdio.h>
27 #include "libiberty.h"
28 #include "demangle.h"
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #endif
32 #if HAVE_STDLIB_H
33 # include <stdlib.h>
34 #endif
35
36 struct line
37 {
38 size_t alloced;
39 char *data;
40 };
41
42 static unsigned int lineno;
43
44 /* Safely read a single line of arbitrary length from standard input. */
45
46 #define LINELEN 80
47
48 static void
49 get_line(buf)
50 struct line *buf;
51 {
52 char *data = buf->data;
53 size_t alloc = buf->alloced;
54 size_t count = 0;
55 int c;
56
57 if (data == 0)
58 {
59 data = xmalloc (LINELEN);
60 alloc = LINELEN;
61 }
62
63 /* Skip comment lines. */
64 while ((c = getchar()) == '#')
65 {
66 while ((c = getchar()) != EOF && c != '\n');
67 lineno++;
68 }
69
70 /* c is the first character on the line, and it's not a comment
71 line: copy this line into the buffer and return. */
72 while (c != EOF && c != '\n')
73 {
74 if (count + 1 >= alloc)
75 {
76 alloc *= 2;
77 data = xrealloc (data, alloc);
78 }
79 data[count++] = c;
80 c = getchar();
81 }
82 lineno++;
83 data[count] = '\0';
84
85 buf->data = data;
86 buf->alloced = alloc;
87 }
88
89 /* If we have mmap() and mprotect(), copy the string S just before a
90 protected page, so that if the demangler runs over the end of the
91 string we'll get a fault, and return the address of the new string.
92 If no mmap, or it fails, or it looks too hard, just return S. */
93
94 #ifdef HAVE_SYS_MMAN_H
95 #include <sys/mman.h>
96 #endif
97 #if defined(MAP_ANON) && ! defined (MAP_ANONYMOUS)
98 #define MAP_ANONYMOUS MAP_ANON
99 #endif
100
101 static const char *
102 protect_end (const char * s)
103 {
104 #if defined(HAVE_MMAP) && defined (MAP_ANONYMOUS)
105 size_t pagesize = getpagesize();
106 static char * buf;
107 size_t s_len = strlen (s);
108 char * result;
109
110 /* Don't try if S is too long. */
111 if (s_len >= pagesize)
112 return s;
113
114 /* Allocate one page of allocated space followed by an unmapped
115 page. */
116 if (buf == NULL)
117 {
118 buf = mmap (NULL, pagesize * 2, PROT_READ | PROT_WRITE,
119 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
120 if (! buf)
121 return s;
122 munmap (buf + pagesize, pagesize);
123 }
124
125 result = buf + (pagesize - s_len - 1);
126 memcpy (result, s, s_len + 1);
127 return result;
128 #else
129 return s;
130 #endif
131 }
132
133 static void
134 fail (lineno, opts, in, out, exp)
135 int lineno;
136 const char *opts;
137 const char *in;
138 const char *out;
139 const char *exp;
140 {
141 printf ("\
142 FAIL at line %d, options %s:\n\
143 in: %s\n\
144 out: %s\n\
145 exp: %s\n",
146 lineno, opts, in, out != NULL ? out : "(null)", exp);
147 }
148
149 /* The tester operates on a data file consisting of groups of lines:
150 options
151 input to be demangled
152 expected output
153
154 Supported options:
155 --format=<name> Sets the demangling style.
156 --no-params There are two lines of expected output; the first
157 is with DMGL_PARAMS, the second is without it.
158 --is-v3-ctor Calls is_gnu_v3_mangled_ctor on input; expected
159 output is an integer representing ctor_kind.
160 --is-v3-dtor Likewise, but for dtors.
161 --ret-postfix Passes the DMGL_RET_POSTFIX option
162 --ret-drop Passes the DMGL_RET_DROP option
163
164 For compatibility, just in case it matters, the options line may be
165 empty, to mean --format=auto. If it doesn't start with --, then it
166 may contain only a format name.
167 */
168
169 int
170 main(argc, argv)
171 int argc;
172 char **argv;
173 {
174 enum demangling_styles style = auto_demangling;
175 int no_params;
176 int is_v3_ctor;
177 int is_v3_dtor;
178 int ret_postfix, ret_drop;
179 struct line format;
180 struct line input;
181 struct line expect;
182 char *result;
183 int failures = 0;
184 int tests = 0;
185
186 if (argc > 1)
187 {
188 fprintf (stderr, "usage: %s < test-set\n", argv[0]);
189 return 2;
190 }
191
192 format.data = 0;
193 input.data = 0;
194 expect.data = 0;
195
196 for (;;)
197 {
198 const char *inp;
199
200 get_line (&format);
201 if (feof (stdin))
202 break;
203
204 get_line (&input);
205 get_line (&expect);
206
207 inp = protect_end (input.data);
208
209 tests++;
210
211 no_params = 0;
212 ret_postfix = 0;
213 ret_drop = 0;
214 is_v3_ctor = 0;
215 is_v3_dtor = 0;
216 if (format.data[0] == '\0')
217 style = auto_demangling;
218 else if (format.data[0] != '-')
219 {
220 style = cplus_demangle_name_to_style (format.data);
221 if (style == unknown_demangling)
222 {
223 printf ("FAIL at line %d: unknown demangling style %s\n",
224 lineno, format.data);
225 failures++;
226 continue;
227 }
228 }
229 else
230 {
231 char *p;
232 char *opt;
233
234 p = format.data;
235 while (*p != '\0')
236 {
237 char c;
238
239 opt = p;
240 p += strcspn (p, " \t=");
241 c = *p;
242 *p = '\0';
243 if (strcmp (opt, "--format") == 0 && c == '=')
244 {
245 char *fstyle;
246
247 *p = c;
248 ++p;
249 fstyle = p;
250 p += strcspn (p, " \t");
251 c = *p;
252 *p = '\0';
253 style = cplus_demangle_name_to_style (fstyle);
254 if (style == unknown_demangling)
255 {
256 printf ("FAIL at line %d: unknown demangling style %s\n",
257 lineno, fstyle);
258 failures++;
259 continue;
260 }
261 }
262 else if (strcmp (opt, "--no-params") == 0)
263 no_params = 1;
264 else if (strcmp (opt, "--is-v3-ctor") == 0)
265 is_v3_ctor = 1;
266 else if (strcmp (opt, "--is-v3-dtor") == 0)
267 is_v3_dtor = 1;
268 else if (strcmp (opt, "--ret-postfix") == 0)
269 ret_postfix = 1;
270 else if (strcmp (opt, "--ret-drop") == 0)
271 ret_drop = 1;
272 else
273 {
274 printf ("FAIL at line %d: unrecognized option %s\n",
275 lineno, opt);
276 failures++;
277 continue;
278 }
279 *p = c;
280 p += strspn (p, " \t");
281 }
282 }
283
284 if (is_v3_ctor || is_v3_dtor)
285 {
286 char buf[20];
287
288 if (is_v3_ctor)
289 {
290 enum gnu_v3_ctor_kinds kc;
291
292 kc = is_gnu_v3_mangled_ctor (inp);
293 sprintf (buf, "%d", (int) kc);
294 }
295 else
296 {
297 enum gnu_v3_dtor_kinds kd;
298
299 kd = is_gnu_v3_mangled_dtor (inp);
300 sprintf (buf, "%d", (int) kd);
301 }
302
303 if (strcmp (buf, expect.data) != 0)
304 {
305 fail (lineno, format.data, input.data, buf, expect.data);
306 failures++;
307 }
308
309 continue;
310 }
311
312 cplus_demangle_set_style (style);
313
314 result = cplus_demangle (inp, (DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES
315 | (ret_postfix ? DMGL_RET_POSTFIX : 0)
316 | (ret_drop ? DMGL_RET_DROP : 0)));
317
318 if (result
319 ? strcmp (result, expect.data)
320 : strcmp (input.data, expect.data))
321 {
322 fail (lineno, format.data, input.data, result, expect.data);
323 failures++;
324 }
325 free (result);
326
327 if (no_params)
328 {
329 get_line (&expect);
330 result = cplus_demangle (inp, DMGL_ANSI|DMGL_TYPES);
331
332 if (result
333 ? strcmp (result, expect.data)
334 : strcmp (input.data, expect.data))
335 {
336 fail (lineno, format.data, input.data, result, expect.data);
337 failures++;
338 }
339 free (result);
340 }
341 }
342
343 free (format.data);
344 free (input.data);
345 free (expect.data);
346
347 printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
348 return failures ? 1 : 0;
349 }
This page took 0.037268 seconds and 4 git commands to generate.