* gas/config/tc-arm.h (DOUBLESLASH_LINE_COMMENTS): Define.
[binutils.git] / libiberty / testsuite / test-demangle.c
blob11d9729999b80259cb82189012d08bd449e58f5f
1 /* Demangler test program,
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Zack Weinberg <zack@codesourcery.com
5 This file is part of GNU libiberty.
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.
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.
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.
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
36 struct line
38 size_t alloced;
39 char *data;
42 static unsigned int lineno;
44 /* Safely read a single line of arbitrary length from standard input. */
46 #define LINELEN 80
48 static void
49 get_line(buf)
50 struct line *buf;
52 char *data = buf->data;
53 size_t alloc = buf->alloced;
54 size_t count = 0;
55 int c;
57 if (data == 0)
59 data = xmalloc (LINELEN);
60 alloc = LINELEN;
63 /* Skip comment lines. */
64 while ((c = getchar()) == '#')
66 while ((c = getchar()) != EOF && c != '\n');
67 lineno++;
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')
74 if (count + 1 >= alloc)
76 alloc *= 2;
77 data = xrealloc (data, alloc);
79 data[count++] = c;
80 c = getchar();
82 lineno++;
83 data[count] = '\0';
85 buf->data = data;
86 buf->alloced = alloc;
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. */
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
101 static const char *
102 protect_end (const char * s)
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;
110 /* Don't try if S is too long. */
111 if (s_len >= pagesize)
112 return s;
114 /* Allocate one page of allocated space followed by an unmapped
115 page. */
116 if (buf == NULL)
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);
125 result = buf + (pagesize - s_len - 1);
126 memcpy (result, s, s_len + 1);
127 return result;
128 #else
129 return s;
130 #endif
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;
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);
149 /* The tester operates on a data file consisting of groups of lines:
150 options
151 input to be demangled
152 expected output
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
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.
170 main(argc, argv)
171 int argc;
172 char **argv;
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;
186 if (argc > 1)
188 fprintf (stderr, "usage: %s < test-set\n", argv[0]);
189 return 2;
192 format.data = 0;
193 input.data = 0;
194 expect.data = 0;
196 for (;;)
198 const char *inp;
200 get_line (&format);
201 if (feof (stdin))
202 break;
204 get_line (&input);
205 get_line (&expect);
207 inp = protect_end (input.data);
209 tests++;
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] != '-')
220 style = cplus_demangle_name_to_style (format.data);
221 if (style == unknown_demangling)
223 printf ("FAIL at line %d: unknown demangling style %s\n",
224 lineno, format.data);
225 failures++;
226 continue;
229 else
231 char *p;
232 char *opt;
234 p = format.data;
235 while (*p != '\0')
237 char c;
239 opt = p;
240 p += strcspn (p, " \t=");
241 c = *p;
242 *p = '\0';
243 if (strcmp (opt, "--format") == 0 && c == '=')
245 char *fstyle;
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)
256 printf ("FAIL at line %d: unknown demangling style %s\n",
257 lineno, fstyle);
258 failures++;
259 continue;
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
274 printf ("FAIL at line %d: unrecognized option %s\n",
275 lineno, opt);
276 failures++;
277 continue;
279 *p = c;
280 p += strspn (p, " \t");
284 if (is_v3_ctor || is_v3_dtor)
286 char buf[20];
288 if (is_v3_ctor)
290 enum gnu_v3_ctor_kinds kc;
292 kc = is_gnu_v3_mangled_ctor (inp);
293 sprintf (buf, "%d", (int) kc);
295 else
297 enum gnu_v3_dtor_kinds kd;
299 kd = is_gnu_v3_mangled_dtor (inp);
300 sprintf (buf, "%d", (int) kd);
303 if (strcmp (buf, expect.data) != 0)
305 fail (lineno, format.data, input.data, buf, expect.data);
306 failures++;
309 continue;
312 cplus_demangle_set_style (style);
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)));
318 if (result
319 ? strcmp (result, expect.data)
320 : strcmp (input.data, expect.data))
322 fail (lineno, format.data, input.data, result, expect.data);
323 failures++;
325 free (result);
327 if (no_params)
329 get_line (&expect);
330 result = cplus_demangle (inp, DMGL_ANSI|DMGL_TYPES);
332 if (result
333 ? strcmp (result, expect.data)
334 : strcmp (input.data, expect.data))
336 fail (lineno, format.data, input.data, result, expect.data);
337 failures++;
339 free (result);
343 free (format.data);
344 free (input.data);
345 free (expect.data);
347 printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
348 return failures ? 1 : 0;