1 /* Demangler test program,
2 Copyright (C) 2002-2022 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"
45 static unsigned int lineno
;
47 /* Safely read a single line of arbitrary length from standard input. */
55 char *data
= buf
->data
;
56 size_t alloc
= buf
->alloced
;
62 data
= xmalloc (LINELEN
);
66 /* Skip comment lines. */
67 while ((c
= getchar()) == '#')
69 while ((c
= getchar()) != EOF
&& c
!= '\n');
73 /* c is the first character on the line, and it's not a comment
74 line: copy this line into the buffer and return. */
75 while (c
!= EOF
&& c
!= '\n')
77 if (count
+ 1 >= alloc
)
80 data
= xrealloc (data
, alloc
);
92 /* If we have mmap() and mprotect(), copy the string S just before a
93 protected page, so that if the demangler runs over the end of the
94 string we'll get a fault, and return the address of the new string.
95 If no mmap, or it fails, or it looks too hard, just return S. */
97 #ifdef HAVE_SYS_MMAN_H
100 #if defined(MAP_ANON) && ! defined (MAP_ANONYMOUS)
101 #define MAP_ANONYMOUS MAP_ANON
105 protect_end (const char * s
)
107 #if defined(HAVE_MMAP) && defined (MAP_ANONYMOUS)
108 size_t pagesize
= getpagesize();
110 size_t s_len
= strlen (s
);
113 /* Don't try if S is too long. */
114 if (s_len
>= pagesize
)
117 /* Allocate one page of allocated space followed by an unmapped
121 buf
= mmap (NULL
, pagesize
* 2, PROT_READ
| PROT_WRITE
,
122 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
125 munmap (buf
+ pagesize
, pagesize
);
128 result
= buf
+ (pagesize
- s_len
- 1);
129 memcpy (result
, s
, s_len
+ 1);
137 fail (lineno
, opts
, in
, out
, exp
)
145 FAIL at line %d, options %s:\n\
149 lineno
, opts
, in
, out
!= NULL
? out
: "(null)", exp
);
152 /* The tester operates on a data file consisting of groups of lines:
154 input to be demangled
158 --format=<name> Sets the demangling style.
159 --no-params There are two lines of expected output; the first
160 is with DMGL_PARAMS, the second is without it.
161 --is-v3-ctor Calls is_gnu_v3_mangled_ctor on input; expected
162 output is an integer representing ctor_kind.
163 --is-v3-dtor Likewise, but for dtors.
164 --ret-postfix Passes the DMGL_RET_POSTFIX option
165 --ret-drop Passes the DMGL_RET_DROP option
167 For compatibility, just in case it matters, the options line may be
168 empty, to mean --format=auto. If it doesn't start with --, then it
169 may contain only a format name.
177 enum demangling_styles style
= auto_demangling
;
181 int ret_postfix
, ret_drop
;
191 fprintf (stderr
, "usage: %s < test-set\n", argv
[0]);
210 inp
= protect_end (input
.data
);
219 if (format
.data
[0] == '\0')
220 style
= auto_demangling
;
221 else if (format
.data
[0] != '-')
223 style
= cplus_demangle_name_to_style (format
.data
);
224 if (style
== unknown_demangling
)
226 printf ("FAIL at line %d: unknown demangling style %s\n",
227 lineno
, format
.data
);
243 p
+= strcspn (p
, " \t=");
246 if (strcmp (opt
, "--format") == 0 && c
== '=')
253 p
+= strcspn (p
, " \t");
256 style
= cplus_demangle_name_to_style (fstyle
);
257 if (style
== unknown_demangling
)
259 printf ("FAIL at line %d: unknown demangling style %s\n",
265 else if (strcmp (opt
, "--no-params") == 0)
267 else if (strcmp (opt
, "--is-v3-ctor") == 0)
269 else if (strcmp (opt
, "--is-v3-dtor") == 0)
271 else if (strcmp (opt
, "--ret-postfix") == 0)
273 else if (strcmp (opt
, "--ret-drop") == 0)
277 printf ("FAIL at line %d: unrecognized option %s\n",
283 p
+= strspn (p
, " \t");
287 if (is_v3_ctor
|| is_v3_dtor
)
293 enum gnu_v3_ctor_kinds kc
;
295 kc
= is_gnu_v3_mangled_ctor (inp
);
296 sprintf (buf
, "%d", (int) kc
);
300 enum gnu_v3_dtor_kinds kd
;
302 kd
= is_gnu_v3_mangled_dtor (inp
);
303 sprintf (buf
, "%d", (int) kd
);
306 if (strcmp (buf
, expect
.data
) != 0)
308 fail (lineno
, format
.data
, input
.data
, buf
, expect
.data
);
315 cplus_demangle_set_style (style
);
317 result
= cplus_demangle (inp
, (DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
318 | (ret_postfix
? DMGL_RET_POSTFIX
: 0)
319 | (ret_drop
? DMGL_RET_DROP
: 0)));
322 ? strcmp (result
, expect
.data
)
323 : strcmp (input
.data
, expect
.data
))
325 fail (lineno
, format
.data
, input
.data
, result
, expect
.data
);
333 result
= cplus_demangle (inp
, DMGL_ANSI
|DMGL_TYPES
);
336 ? strcmp (result
, expect
.data
)
337 : strcmp (input
.data
, expect
.data
))
339 fail (lineno
, format
.data
, input
.data
, result
, expect
.data
);
350 printf ("%s: %d tests, %d failures\n", argv
[0], tests
, failures
);
351 return failures
? 1 : 0;