2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libiberty / testsuite / test-demangle.c
blobed28229ca344a374d509df5568d1e5259c58b050
1 /* Demangler test program,
2 Copyright (C) 2002 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"
30 struct line
32 size_t alloced;
33 char *data;
36 static unsigned int lineno;
38 /* Safely read a single line of arbitrary length from standard input. */
40 #define LINELEN 80
42 static void
43 getline(buf)
44 struct line *buf;
46 char *data = buf->data;
47 size_t alloc = buf->alloced;
48 size_t count = 0;
49 int c;
51 if (data == 0)
53 data = xmalloc (LINELEN);
54 alloc = LINELEN;
57 /* Skip comment lines. */
58 while ((c = getchar()) == '#')
60 while ((c = getchar()) != EOF && c != '\n');
61 lineno++;
64 /* c is the first character on the line, and it's not a comment
65 line: copy this line into the buffer and return. */
66 while (c != EOF && c != '\n')
68 if (count + 1 >= alloc)
70 alloc *= 2;
71 data = xrealloc (data, alloc);
73 data[count++] = c;
74 c = getchar();
76 lineno++;
77 data[count] = '\0';
79 buf->data = data;
80 buf->alloced = alloc;
83 /* The tester operates on a data file consisting of triples of lines:
84 format switch
85 input to be demangled
86 expected output
88 The format switch is expected to be either the empty string, a
89 line of the form --format=<name>, or just <name> by itself. */
91 #define FORMATS "--format="
92 #define FORMATL (sizeof FORMATS - 1)
94 int
95 main(argc, argv)
96 int argc;
97 char **argv;
99 enum demangling_styles style;
100 struct line format;
101 struct line input;
102 struct line expect;
103 char *fstyle;
104 char *result;
105 int failures = 0;
106 int tests = 0;
108 if (argc > 1)
110 fprintf (stderr, "usage: %s < test-set\n", argv[0]);
111 return 2;
114 format.data = 0;
115 input.data = 0;
116 expect.data = 0;
118 for (;;)
120 getline (&format);
121 if (feof (stdin))
122 break;
124 getline (&input);
125 getline (&expect);
127 tests++;
129 fstyle = format.data;
130 if (!strncmp (fstyle, FORMATS, FORMATL))
131 fstyle += FORMATL;
133 if (fstyle[0] == '\0')
134 style = auto_demangling;
135 else
136 style = cplus_demangle_name_to_style (fstyle);
138 if (style == unknown_demangling)
140 printf ("FAIL at line %d: unknown demangling style %s\n",
141 lineno, fstyle);
142 failures++;
143 continue;
146 cplus_demangle_set_style (style);
148 result = cplus_demangle (input.data,
149 DMGL_PARAMS|DMGL_ANSI|DMGL_TYPES);
151 if (result
152 ? strcmp (result, expect.data)
153 : strcmp (input.data, expect.data))
155 printf ("\
156 FAIL at line %d, style %s:\n\
157 in: %s\n\
158 out: %s\n\
159 exp: %s\n",
160 lineno, fstyle,
161 input.data,
162 result,
163 expect.data);
164 failures++;
166 free (result);
169 free (format.data);
170 free (input.data);
171 free (expect.data);
173 printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
174 return failures ? 1 : 0;