PR tree-optimization/22230
[official-gcc.git] / libiberty / testsuite / test-demangle.c
blob4d515fab5343109756afd916bed2e617ffbaea28
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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.
118 For compatibility, just in case it matters, the options line may be
119 empty, to mean --format=auto. If it doesn't start with --, then it
120 may contain only a format name.
124 main(argc, argv)
125 int argc;
126 char **argv;
128 enum demangling_styles style = auto_demangling;
129 int no_params;
130 int is_v3_ctor;
131 int is_v3_dtor;
132 struct line format;
133 struct line input;
134 struct line expect;
135 char *result;
136 int failures = 0;
137 int tests = 0;
139 if (argc > 1)
141 fprintf (stderr, "usage: %s < test-set\n", argv[0]);
142 return 2;
145 format.data = 0;
146 input.data = 0;
147 expect.data = 0;
149 for (;;)
151 getline (&format);
152 if (feof (stdin))
153 break;
155 getline (&input);
156 getline (&expect);
158 tests++;
160 no_params = 0;
161 is_v3_ctor = 0;
162 is_v3_dtor = 0;
163 if (format.data[0] == '\0')
164 style = auto_demangling;
165 else if (format.data[0] != '-')
167 style = cplus_demangle_name_to_style (format.data);
168 if (style == unknown_demangling)
170 printf ("FAIL at line %d: unknown demangling style %s\n",
171 lineno, format.data);
172 failures++;
173 continue;
176 else
178 char *p;
179 char *opt;
181 p = format.data;
182 while (*p != '\0')
184 char c;
186 opt = p;
187 p += strcspn (p, " \t=");
188 c = *p;
189 *p = '\0';
190 if (strcmp (opt, "--format") == 0 && c == '=')
192 char *fstyle;
194 *p = c;
195 ++p;
196 fstyle = p;
197 p += strcspn (p, " \t");
198 c = *p;
199 *p = '\0';
200 style = cplus_demangle_name_to_style (fstyle);
201 if (style == unknown_demangling)
203 printf ("FAIL at line %d: unknown demangling style %s\n",
204 lineno, fstyle);
205 failures++;
206 continue;
209 else if (strcmp (opt, "--no-params") == 0)
210 no_params = 1;
211 else if (strcmp (opt, "--is-v3-ctor") == 0)
212 is_v3_ctor = 1;
213 else if (strcmp (opt, "--is-v3-dtor") == 0)
214 is_v3_dtor = 1;
215 else
217 printf ("FAIL at line %d: unrecognized option %s\n",
218 lineno, opt);
219 failures++;
220 continue;
222 *p = c;
223 p += strspn (p, " \t");
227 if (is_v3_ctor || is_v3_dtor)
229 char buf[20];
231 if (is_v3_ctor)
233 enum gnu_v3_ctor_kinds kc;
235 kc = is_gnu_v3_mangled_ctor (input.data);
236 sprintf (buf, "%d", (int) kc);
238 else
240 enum gnu_v3_dtor_kinds kd;
242 kd = is_gnu_v3_mangled_dtor (input.data);
243 sprintf (buf, "%d", (int) kd);
246 if (strcmp (buf, expect.data) != 0)
248 fail (lineno, format.data, input.data, buf, expect.data);
249 failures++;
252 continue;
255 cplus_demangle_set_style (style);
257 result = cplus_demangle (input.data,
258 DMGL_PARAMS|DMGL_ANSI|DMGL_TYPES);
260 if (result
261 ? strcmp (result, expect.data)
262 : strcmp (input.data, expect.data))
264 fail (lineno, format.data, input.data, result, expect.data);
265 failures++;
267 free (result);
269 if (no_params)
271 getline (&expect);
272 result = cplus_demangle (input.data, DMGL_ANSI|DMGL_TYPES);
274 if (result
275 ? strcmp (result, expect.data)
276 : strcmp (input.data, expect.data))
278 fail (lineno, format.data, input.data, result, expect.data);
279 failures++;
281 free (result);
285 free (format.data);
286 free (input.data);
287 free (expect.data);
289 printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
290 return failures ? 1 : 0;