file bfin-parse.h was initially added on branch binutils-2_17-branch.
[binutils.git] / libiberty / testsuite / test-demangle.c
blob93793996fe0d2d20710f77c112c49e09c53e02f4
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 getline(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 static void
90 fail (lineno, opts, in, out, exp)
91 int lineno;
92 const char *opts;
93 const char *in;
94 const char *out;
95 const char *exp;
97 printf ("\
98 FAIL at line %d, options %s:\n\
99 in: %s\n\
100 out: %s\n\
101 exp: %s\n",
102 lineno, opts, in, out != NULL ? out : "(null)", exp);
105 /* The tester operates on a data file consisting of groups of lines:
106 options
107 input to be demangled
108 expected output
110 Supported options:
111 --format=<name> Sets the demangling style.
112 --no-params There are two lines of expected output; the first
113 is with DMGL_PARAMS, the second is without it.
114 --is-v3-ctor Calls is_gnu_v3_mangled_ctor on input; expected
115 output is an integer representing ctor_kind.
116 --is-v3-dtor Likewise, but for dtors.
117 --ret-postfix Passes the DMGL_RET_POSTFIX option
119 For compatibility, just in case it matters, the options line may be
120 empty, to mean --format=auto. If it doesn't start with --, then it
121 may contain only a format name.
125 main(argc, argv)
126 int argc;
127 char **argv;
129 enum demangling_styles style = auto_demangling;
130 int no_params;
131 int is_v3_ctor;
132 int is_v3_dtor;
133 int ret_postfix;
134 struct line format;
135 struct line input;
136 struct line expect;
137 char *result;
138 int failures = 0;
139 int tests = 0;
141 if (argc > 1)
143 fprintf (stderr, "usage: %s < test-set\n", argv[0]);
144 return 2;
147 format.data = 0;
148 input.data = 0;
149 expect.data = 0;
151 for (;;)
153 getline (&format);
154 if (feof (stdin))
155 break;
157 getline (&input);
158 getline (&expect);
160 tests++;
162 no_params = 0;
163 ret_postfix = 0;
164 is_v3_ctor = 0;
165 is_v3_dtor = 0;
166 if (format.data[0] == '\0')
167 style = auto_demangling;
168 else if (format.data[0] != '-')
170 style = cplus_demangle_name_to_style (format.data);
171 if (style == unknown_demangling)
173 printf ("FAIL at line %d: unknown demangling style %s\n",
174 lineno, format.data);
175 failures++;
176 continue;
179 else
181 char *p;
182 char *opt;
184 p = format.data;
185 while (*p != '\0')
187 char c;
189 opt = p;
190 p += strcspn (p, " \t=");
191 c = *p;
192 *p = '\0';
193 if (strcmp (opt, "--format") == 0 && c == '=')
195 char *fstyle;
197 *p = c;
198 ++p;
199 fstyle = p;
200 p += strcspn (p, " \t");
201 c = *p;
202 *p = '\0';
203 style = cplus_demangle_name_to_style (fstyle);
204 if (style == unknown_demangling)
206 printf ("FAIL at line %d: unknown demangling style %s\n",
207 lineno, fstyle);
208 failures++;
209 continue;
212 else if (strcmp (opt, "--no-params") == 0)
213 no_params = 1;
214 else if (strcmp (opt, "--is-v3-ctor") == 0)
215 is_v3_ctor = 1;
216 else if (strcmp (opt, "--is-v3-dtor") == 0)
217 is_v3_dtor = 1;
218 else if (strcmp (opt, "--ret-postfix") == 0)
219 ret_postfix = 1;
220 else
222 printf ("FAIL at line %d: unrecognized option %s\n",
223 lineno, opt);
224 failures++;
225 continue;
227 *p = c;
228 p += strspn (p, " \t");
232 if (is_v3_ctor || is_v3_dtor)
234 char buf[20];
236 if (is_v3_ctor)
238 enum gnu_v3_ctor_kinds kc;
240 kc = is_gnu_v3_mangled_ctor (input.data);
241 sprintf (buf, "%d", (int) kc);
243 else
245 enum gnu_v3_dtor_kinds kd;
247 kd = is_gnu_v3_mangled_dtor (input.data);
248 sprintf (buf, "%d", (int) kd);
251 if (strcmp (buf, expect.data) != 0)
253 fail (lineno, format.data, input.data, buf, expect.data);
254 failures++;
257 continue;
260 cplus_demangle_set_style (style);
262 result = cplus_demangle (input.data,
263 DMGL_PARAMS|DMGL_ANSI|DMGL_TYPES
264 |(ret_postfix ? DMGL_RET_POSTFIX : 0));
266 if (result
267 ? strcmp (result, expect.data)
268 : strcmp (input.data, expect.data))
270 fail (lineno, format.data, input.data, result, expect.data);
271 failures++;
273 free (result);
275 if (no_params)
277 getline (&expect);
278 result = cplus_demangle (input.data, DMGL_ANSI|DMGL_TYPES);
280 if (result
281 ? strcmp (result, expect.data)
282 : strcmp (input.data, expect.data))
284 fail (lineno, format.data, input.data, result, expect.data);
285 failures++;
287 free (result);
291 free (format.data);
292 free (input.data);
293 free (expect.data);
295 printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
296 return failures ? 1 : 0;