1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* SPDX-FileCopyrightText: © 2025 Decompollaborate */
/* SPDX-License-Identifier: MIT OR Apache-2.0 */
/// Tweak how a symbol should be disassembled.
///
/// The constructors provide sensible defaults, so there's usually no need to
/// override each option.
///
/// Refer to each option to see what it does and examples.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub struct DemangleConfig {
/// Recreate a c++filt bug where it won't emit the
/// "global constructors keyed to " prefix for a namespaced function.
///
/// This is just another c++filt compatibility setting.
///
/// # Examples
///
/// Turning off this setting (mimicking c++filt behavior):
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.fix_namespaced_global_constructor_bug = false;
///
/// let demangled = demangle("_GLOBAL_$I$__Q210Scenegraph10Scenegraph", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("Scenegraph::Scenegraph::Scenegraph(void)")
/// );
/// ```
///
/// The setting turned on:
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.fix_namespaced_global_constructor_bug = true;
///
/// let demangled = demangle("_GLOBAL_$I$__Q210Scenegraph10Scenegraph", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("global constructors keyed to Scenegraph::Scenegraph::Scenegraph(void)")
/// );
pub fix_namespaced_global_constructor_bug: bool,
/// By default g++ subtracts 1 from the length of array arguments, thus
/// producing a confusing mangled name.
///
/// c++filt uses this length as-is, which produces a demangled symbol that
/// does not match the original C++ symbol.
///
/// This setting adds 1 to the length, making the demangled symbol match
/// more accurately the real symbol.
///
/// This is just another c++filt compatibility setting.
///
/// # Examples
///
/// Turning off this setting (mimicking c++filt behavior):
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.fix_array_length_arg = false;
///
/// let demangled = demangle("simpler_array__FPA41_A24_Ci", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("simpler_array(int const (*)[41][24])")
/// );
/// ```
///
/// The setting turned on:
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.fix_array_length_arg = true;
///
/// let demangled = demangle("simpler_array__FPA41_A24_Ci", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("simpler_array(int const (*)[42][25])")
/// );
/// ```
pub fix_array_length_arg: bool,
/// Recognize and demangle symbols prefixed by `_GLOBAL_$F$`.
///
/// c++filt does not recognizes this prefix, so it tries to demangle it as
/// other mangled kinds, like functions, methods, etc.
///
/// When turned on, the symbol gets demangled the same way `_GLOBAL_$I$`
/// and `_GLOBAL_$D$` are demangled, but the word "frames" is used instead
/// of "constructors" or "destructors". This name is made-up based on some
/// usages from projects that have this symbol present.
///
/// This is just another c++filt compatibility setting.
///
/// # Examples
///
/// Turning off this setting (mimicking c++filt behavior):
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.demangle_global_keyed_frames = false;
///
/// let demangled = demangle("_GLOBAL_$F$__7istreamiP9streambufP7ostream", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("istream::_GLOBAL_$F$(int, streambuf *, ostream *)")
/// );
/// let demangled = demangle("_GLOBAL_$F$__default_terminate", &config);
/// assert!(
/// demangled.is_err()
/// );
/// ```
///
/// The setting turned on:
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.demangle_global_keyed_frames = true;
///
/// let demangled = demangle("_GLOBAL_$F$__7istreamiP9streambufP7ostream", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("global frames keyed to istream::istream(int, streambuf *, ostream *)")
/// );
/// let demangled = demangle("_GLOBAL_$F$__default_terminate", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("global frames keyed to __default_terminate")
/// );
/// ```
pub demangle_global_keyed_frames: bool,
/// Emit an space between a comma and an ellipsis (`...`) in the argument
/// list.
///
/// This is just another c++filt compatibility setting.
///
/// # Examples
///
/// Turning off this setting (mimicking c++filt behavior):
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.ellipsis_emit_space_after_comma = false;
///
/// let demangled = demangle("Printf__7ConsolePce", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("Console::Printf(char *,...)")
/// );
/// ```
///
/// The setting turned on:
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.ellipsis_emit_space_after_comma = true;
///
/// let demangled = demangle("Printf__7ConsolePce", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("Console::Printf(char *, ...)")
/// );
/// ```
pub ellipsis_emit_space_after_comma: bool,
/// If enabled, emit `__int128_t` and `__uint128_t` types instead of
/// `int128_t` and `unsigned int128_t`.
///
/// The former is valid syntax in g++ for this GNU integer extension type,
/// while the latter is the syntax used by c++filt, but not accepted by g++.
///
/// This is just another c++filt compatibility setting.
///
/// # Examples
///
/// Turning off this setting (mimicking c++filt behavior):
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.fix_extension_int = false;
///
/// let demangled = demangle("testing_func__FRCI80", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("testing_func(int128_t const &)")
/// );
/// let demangled = demangle("testing_func__FRCUI80", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("testing_func(unsigned int128_t const &)")
/// );
/// ```
///
/// The setting turned on:
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.fix_extension_int = true;
///
/// let demangled = demangle("testing_func__FRCI80", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("testing_func(__int128_t const &)")
/// );
/// let demangled = demangle("testing_func__FRCUI80", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("testing_func(__uint128_t const &)")
/// );
/// ```
pub fix_extension_int: bool,
/// If enabled, emit proper syntax for arrays as return types in templated
/// functions.
///
/// Disabling this option make it mimic the c++filt behavior for arrays in
/// return position, which is not valid C++ but is simpler to read.
///
/// # Examples
///
/// Turning off this setting (mimicking c++filt behavior):
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.fix_array_in_return_position = false;
///
/// let demangled = demangle("an_array__H1Zi_X01_PA3_f", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("float (*)[3] an_array<int>(int)")
/// );
/// ```
///
/// The setting turned on:
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.fix_array_in_return_position = true;
///
/// let demangled = demangle("an_array__H1Zi_X01_PA3_f", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("float (*an_array<int>(int))[3]")
/// );
/// ```
pub fix_array_in_return_position: bool,
/// If enabled, emit proper syntax for return types of function pointers in
/// template lists.
///
/// Disabling this option make it mimic the c++filt behavior for function
/// pointers in template lists, which is not valid C++ but is simpler to
/// read.
///
/// The c++filt behavior also omits the return type of the function pointer
/// while this option does explicitly shows it.
///
/// # Examples
///
/// Turning off this setting (mimicking c++filt behavior):
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.fix_function_pointers_in_template_lists = false;
///
/// let demangled = demangle("alloc__t5Table1PFUi_Pv16DefaultFunc__FUiUi", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("Table<&DefaultFunc(unsigned int)>::alloc(unsigned int)")
/// );
/// ```
///
/// The setting turned on:
///
/// ```
/// use gnuv2_demangle::{demangle, DemangleConfig};
///
/// let mut config = DemangleConfig::new();
/// config.fix_function_pointers_in_template_lists = true;
///
/// let demangled = demangle("alloc__t5Table1PFUi_Pv16DefaultFunc__FUiUi", &config);
/// assert_eq!(
/// demangled.as_deref(),
/// Ok("Table<(void *(*)(unsigned int)) &DefaultFunc>::alloc(unsigned int)")
/// );
/// ```
pub fix_function_pointers_in_template_lists: bool,
}
impl DemangleConfig {
/// The default configuration.
#[must_use]
#[inline]
#[track_caller]
pub const fn new() -> Self {
Self::new_g2dem()
}
/// Use improved output and valid C++ syntax whenever possible.
#[must_use]
#[inline]
#[track_caller]
pub const fn new_g2dem() -> Self {
Self {
fix_namespaced_global_constructor_bug: true,
fix_array_length_arg: true,
demangle_global_keyed_frames: true,
ellipsis_emit_space_after_comma: true,
fix_extension_int: true,
fix_array_in_return_position: true,
fix_function_pointers_in_template_lists: true,
}
}
/// Mimics the (rather questionable) c++filt's behavior, including what may
/// be considered c++filt bugs.
///
/// Useful for validating demangling against c++filt.
#[must_use]
#[inline]
#[track_caller]
pub const fn new_cfilt() -> Self {
Self {
fix_namespaced_global_constructor_bug: false,
fix_array_length_arg: false,
demangle_global_keyed_frames: false,
ellipsis_emit_space_after_comma: false,
fix_extension_int: false,
fix_array_in_return_position: false,
fix_function_pointers_in_template_lists: false,
}
}
}
impl Default for DemangleConfig {
fn default() -> Self {
Self::new()
}
}