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.
27 #include "libiberty.h"
42 static unsigned int lineno
;
44 /* Safely read a single line of arbitrary length from standard input. */
52 char *data
= buf
->data
;
53 size_t alloc
= buf
->alloced
;
59 data
= xmalloc (LINELEN
);
63 /* Skip comment lines. */
64 while ((c
= getchar()) == '#')
66 while ((c
= getchar()) != EOF
&& c
!= '\n');
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
)
77 data
= xrealloc (data
, 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
97 #if defined(MAP_ANON) && ! defined (MAP_ANONYMOUS)
98 #define MAP_ANONYMOUS MAP_ANON
102 protect_end (const char * s
)
104 #if defined(HAVE_MMAP) && defined (MAP_ANONYMOUS)
105 size_t pagesize
= getpagesize();
107 size_t s_len
= strlen (s
);
110 /* Don't try if S is too long. */
111 if (s_len
>= pagesize
)
114 /* Allocate one page of allocated space followed by an unmapped
118 buf
= mmap (NULL
, pagesize
* 2, PROT_READ
| PROT_WRITE
,
119 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
122 munmap (buf
+ pagesize
, pagesize
);
125 result
= buf
+ (pagesize
- s_len
- 1);
126 memcpy (result
, s
, s_len
+ 1);
134 fail (lineno
, opts
, in
, out
, exp
)
142 FAIL at line %d, options %s:\n\
146 lineno
, opts
, in
, out
!= NULL
? out
: "(null)", exp
);
149 /* The tester operates on a data file consisting of groups of lines:
151 input to be demangled
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.
174 enum demangling_styles style
= auto_demangling
;
178 int ret_postfix
, ret_drop
;
188 fprintf (stderr
, "usage: %s < test-set\n", argv
[0]);
207 inp
= protect_end (input
.data
);
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
);
240 p
+= strcspn (p
, " \t=");
243 if (strcmp (opt
, "--format") == 0 && c
== '=')
250 p
+= strcspn (p
, " \t");
253 style
= cplus_demangle_name_to_style (fstyle
);
254 if (style
== unknown_demangling
)
256 printf ("FAIL at line %d: unknown demangling style %s\n",
262 else if (strcmp (opt
, "--no-params") == 0)
264 else if (strcmp (opt
, "--is-v3-ctor") == 0)
266 else if (strcmp (opt
, "--is-v3-dtor") == 0)
268 else if (strcmp (opt
, "--ret-postfix") == 0)
270 else if (strcmp (opt
, "--ret-drop") == 0)
274 printf ("FAIL at line %d: unrecognized option %s\n",
280 p
+= strspn (p
, " \t");
284 if (is_v3_ctor
|| is_v3_dtor
)
290 enum gnu_v3_ctor_kinds kc
;
292 kc
= is_gnu_v3_mangled_ctor (inp
);
293 sprintf (buf
, "%d", (int) kc
);
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
);
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)));
319 ? strcmp (result
, expect
.data
)
320 : strcmp (input
.data
, expect
.data
))
322 fail (lineno
, format
.data
, input
.data
, result
, expect
.data
);
330 result
= cplus_demangle (inp
, DMGL_ANSI
|DMGL_TYPES
);
333 ? strcmp (result
, expect
.data
)
334 : strcmp (input
.data
, expect
.data
))
336 fail (lineno
, format
.data
, input
.data
, result
, expect
.data
);
347 printf ("%s: %d tests, %d failures\n", argv
[0], tests
, failures
);
348 return failures
? 1 : 0;