ctest 0.5.1

Automated testing of FFI bindings in Rust.
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
//! Generation of tests from templates for both Rust and C.

use askama::Template;
use proc_macro2::Span;
use quote::ToTokens;
use syn::spanned::Spanned;

use crate::cdecl::Constness;
use crate::ffi_items::FfiItems;
use crate::translator::{
    TranslationErrorKind,
    Translator,
};
use crate::{
    BoxStr,
    Field,
    MapInput,
    Result,
    TestGenerator,
    TranslationError,
    VolatileItemKind,
    cdecl,
};

/// Represents the Rust side of the generated testing suite.
#[derive(Template, Clone)]
#[template(path = "test.rs")]
pub(crate) struct RustTestTemplate {
    pub template: TestTemplate,
    pub extern_keyword: BoxStr,
}

impl RustTestTemplate {
    pub(crate) fn new(
        ffi_items: &FfiItems,
        generator: &TestGenerator,
    ) -> Result<Self, TranslationError> {
        Ok(Self {
            template: TestTemplate::new(ffi_items, generator)?,
            extern_keyword: "extern".into(),
        })
    }

    /// Modify the generated template such that it supports edition 2024.
    pub(crate) fn edition(&mut self, edition: u32) -> &Self {
        match edition {
            2021 => {
                self.extern_keyword = "extern".into();
            }
            2024 => {
                // For now we only use this to convert extern to unsafe extern.
                self.extern_keyword = "unsafe extern".into();
            }
            _ => panic!("unsupported or invalid edition: {edition}"),
        }
        self
    }
}

/// Represents the C side of the generated testing suite.
#[derive(Template, Clone)]
#[template(path = "test.c")]
pub(crate) struct CTestTemplate {
    pub template: TestTemplate,
    pub headers: Vec<(BoxStr, Vec<BoxStr>)>,
}

impl CTestTemplate {
    pub(crate) fn new(
        ffi_items: &FfiItems,
        generator: &TestGenerator,
    ) -> Result<Self, TranslationError> {
        Ok(Self {
            template: TestTemplate::new(ffi_items, generator)?,
            headers: generator.headers.clone(),
        })
    }
}

/// Stores all information necessary for generation of tests for all items.
#[derive(Clone, Debug, Default)]
pub(crate) struct TestTemplate {
    pub foreign_static_tests: Vec<TestForeignStatic>,
    pub field_ptr_tests: Vec<TestFieldPtr>,
    pub field_size_offset_tests: Vec<TestFieldSizeOffset>,
    pub roundtrip_tests: Vec<TestRoundtrip>,
    pub foreign_fn_tests: Vec<TestForeignFn>,
    pub signededness_tests: Vec<TestSignededness>,
    pub size_align_tests: Vec<TestSizeAlign>,
    pub const_cstr_tests: Vec<TestCStr>,
    pub const_tests: Vec<TestConst>,
    pub test_idents: Vec<BoxStr>,
}

impl TestTemplate {
    /// Populate all tests for all items depending on the configuration provided.
    pub(crate) fn new(
        ffi_items: &FfiItems,
        generator: &TestGenerator,
    ) -> Result<Self, TranslationError> {
        let helper = TranslateHelper::new(ffi_items, generator);

        let mut template = Self::default();
        template.populate_const_and_cstr_tests(&helper)?;
        template.populate_size_align_tests(&helper)?;
        template.populate_signededness_tests(&helper)?;
        template.populate_field_size_offset_tests(&helper)?;
        template.populate_field_ptr_tests(&helper)?;
        template.populate_roundtrip_tests(&helper)?;
        template.populate_foreign_fn_tests(&helper)?;
        template.populate_foreign_static_tests(&helper)?;

        Ok(template)
    }

    /// Populates tests for constants and C-str constants, keeping track of the names of each test.
    fn populate_const_and_cstr_tests(
        &mut self,
        helper: &TranslateHelper,
    ) -> Result<(), TranslationError> {
        for constant in helper.filtered_ffi_items.constants() {
            if let syn::Type::Ptr(ptr) = &constant.ty
                && let syn::Type::Path(path) = &*ptr.elem
                && path.path.segments.last().unwrap().ident == "c_char"
                && ptr.mutability.is_none()
            {
                let item = TestCStr {
                    id: constant.ident().into(),
                    test_name: cstr_test_ident(constant.ident()),
                    rust_val: constant.ident().into(),
                    c_val: helper.c_ident(constant).into(),
                };
                self.const_cstr_tests.push(item.clone());
                self.test_idents.push(item.test_name);
            } else {
                let item = TestConst {
                    id: constant.ident().into(),
                    test_name: const_test_ident(constant.ident()),
                    rust_val: constant.ident().into(),
                    rust_ty: constant.ty.to_token_stream().to_string().into_boxed_str(),
                    c_val: helper.c_ident(constant).into(),
                    c_ty: helper.c_type(constant)?.into(),
                };
                self.const_tests.push(item.clone());
                self.test_idents.push(item.test_name);
            }
        }

        Ok(())
    }

    /// Populates size and alignment tests for aliases, structs, and unions.
    ///
    /// It also keeps track of the names of each test.
    fn populate_size_align_tests(
        &mut self,
        helper: &TranslateHelper,
    ) -> Result<(), TranslationError> {
        for alias in helper.filtered_ffi_items.aliases() {
            let item = TestSizeAlign {
                test_name: size_align_test_ident(alias.ident()),
                id: alias.ident().into(),
                rust_ty: alias.ident().into(),
                c_ty: helper.c_type(alias)?.into(),
            };
            self.size_align_tests.push(item.clone());
            self.test_idents.push(item.test_name);
        }
        for struct_ in helper.filtered_ffi_items.structs() {
            let item = TestSizeAlign {
                test_name: size_align_test_ident(struct_.ident()),
                id: struct_.ident().into(),
                rust_ty: struct_.ident().into(),
                c_ty: helper.c_type(struct_)?.into(),
            };
            self.size_align_tests.push(item.clone());
            self.test_idents.push(item.test_name);
        }
        for union_ in helper.filtered_ffi_items.unions() {
            let item = TestSizeAlign {
                test_name: size_align_test_ident(union_.ident()),
                id: union_.ident().into(),
                rust_ty: union_.ident().into(),
                c_ty: helper.c_type(union_)?.into(),
            };
            self.size_align_tests.push(item.clone());
            self.test_idents.push(item.test_name);
        }

        Ok(())
    }

    /// Populates signededness tests for aliases.
    ///
    /// It also keeps track of the names of each test.
    fn populate_signededness_tests(
        &mut self,
        helper: &TranslateHelper,
    ) -> Result<(), TranslationError> {
        for alias in helper.filtered_ffi_items.aliases() {
            let should_skip_signededness_test = helper
                .generator
                .skip_signededness
                .as_ref()
                .is_some_and(|skip| skip(alias.ident()));

            if !helper.translator.is_signed(&alias.ty) || should_skip_signededness_test {
                continue;
            }
            let item = TestSignededness {
                test_name: signededness_test_ident(alias.ident()),
                id: alias.ident().into(),
                c_ty: helper.c_type(alias)?.into(),
            };
            self.signededness_tests.push(item.clone());
            self.test_idents.push(item.test_name);
        }

        Ok(())
    }

    /// Populates field size and offset tests for structs/unions.
    ///
    /// It also keeps track of the names of each test.
    fn populate_field_size_offset_tests(
        &mut self,
        helper: &TranslateHelper,
    ) -> Result<(), TranslationError> {
        let should_skip = |map_input| helper.generator.skips.iter().any(|f| f(&map_input));

        let struct_fields = helper
            .filtered_ffi_items
            .structs()
            .iter()
            .flat_map(|struct_| struct_.fields.iter().map(move |field| (struct_, field)))
            .filter(|(struct_, field)| {
                !should_skip(MapInput::StructField(struct_, field)) && field.public
            })
            .map(|(struct_, field)| {
                (
                    struct_.ident(),
                    field,
                    helper.c_type(struct_),
                    helper.c_ident(MapInput::StructField(struct_, field)),
                )
            });
        let union_fields = helper
            .filtered_ffi_items
            .unions()
            .iter()
            .flat_map(|union_| union_.fields.iter().map(move |field| (union_, field)))
            .filter(|(union_, field)| {
                !should_skip(MapInput::UnionField(union_, field)) && field.public
            })
            .map(|(union_, field)| {
                (
                    union_.ident(),
                    field,
                    helper.c_type(union_),
                    helper.c_ident(MapInput::UnionField(union_, field)),
                )
            });

        for (id, field, c_ty, c_field) in struct_fields.chain(union_fields) {
            let item = TestFieldSizeOffset {
                test_name: field_size_offset_test_ident(id, field.ident()),
                id: id.into(),
                c_ty: c_ty?.into(),
                field: field.clone(),
                c_field: c_field.into_boxed_str(),
            };
            self.field_size_offset_tests.push(item.clone());
            self.test_idents.push(item.test_name);
        }

        Ok(())
    }

    /// Populates roundtrip tests for aliases/structs/unions.
    ///
    /// It also keeps track of the names of each test.
    fn populate_roundtrip_tests(
        &mut self,
        helper: &TranslateHelper,
    ) -> Result<(), TranslationError> {
        for alias in helper.filtered_ffi_items.aliases() {
            // Arrays cannot be roundtripped, and are automatically skipped.
            if let syn::Type::Array(_) = alias.ty {
                continue;
            }
            let c_ty = helper.c_type(alias)?;
            self.add_roundtrip_test(helper, alias.ident(), &[], &c_ty, true);
        }
        for struct_ in helper.filtered_ffi_items.structs() {
            let c_ty = helper.c_type(struct_)?;
            self.add_roundtrip_test(helper, struct_.ident(), &struct_.fields, &c_ty, false);
        }
        for union_ in helper.filtered_ffi_items.unions() {
            let c_ty = helper.c_type(union_)?;
            self.add_roundtrip_test(helper, union_.ident(), &union_.fields, &c_ty, false);
        }

        Ok(())
    }

    fn add_roundtrip_test(
        &mut self,
        helper: &TranslateHelper,
        ident: &str,
        fields: &[Field],
        c_ty: &str,
        is_alias: bool,
    ) {
        let should_skip_roundtrip_test = helper
            .generator
            .skip_roundtrip
            .as_ref()
            .is_some_and(|skip| skip(ident));
        if !should_skip_roundtrip_test {
            let item = TestRoundtrip {
                test_name: roundtrip_test_ident(ident),
                id: ident.into(),
                fields: fields.iter().filter(|f| f.public).cloned().collect(),
                c_ty: c_ty.into(),
                is_alias,
            };
            self.roundtrip_tests.push(item.clone());
            self.test_idents.push(item.test_name);
        }
    }

    /// Populates field tests for structs/unions.
    ///
    /// It also keeps track of the names of each test.
    fn populate_field_ptr_tests(
        &mut self,
        helper: &TranslateHelper,
    ) -> Result<(), TranslationError> {
        let should_skip = |map_input| helper.generator.skips.iter().any(|f| f(&map_input));

        let struct_fields = helper
            .filtered_ffi_items
            .structs()
            .iter()
            .flat_map(|s| s.fields.iter().map(move |f| (s, f)))
            .filter(|(s, f)| {
                !(should_skip(MapInput::StructField(s, f))
                    || should_skip(MapInput::StructFieldType(s, f))
                    || !f.public)
            })
            .map(|(s, f)| {
                (
                    s.ident(),
                    f,
                    helper.c_type(s),
                    helper.c_ident(MapInput::StructField(s, f)),
                    if !helper.generator.volatile_items.is_empty()
                        && helper
                            .generator
                            .volatile_items
                            .iter()
                            .any(|vf| vf(VolatileItemKind::StructField(s.clone(), f.clone())))
                    {
                        "volatile "
                    } else {
                        ""
                    },
                )
            });
        let union_fields = helper
            .filtered_ffi_items
            .unions()
            .iter()
            .flat_map(|u| u.fields.iter().map(move |f| (u, f)))
            .filter(|(u, f)| {
                !(should_skip(MapInput::UnionField(u, f))
                    || should_skip(MapInput::UnionFieldType(u, f))
                    || !f.public)
            })
            .map(|(u, f)| {
                (
                    u.ident(),
                    f,
                    helper.c_type(u),
                    helper.c_ident(MapInput::UnionField(u, f)),
                    "",
                )
            });

        for (id, field, c_ty, c_field, volatile_keyword) in struct_fields.chain(union_fields) {
            let field_return_type = cdecl::cdecl(
                &cdecl::ptr(
                    helper.translator.translate_type(&field.ty)?,
                    cdecl::Constness::Mut,
                ),
                format!("ctest_field_ty__{}__{}", id, field.ident()),
            )
            .map_err(|_| {
                TranslationError::new(
                    TranslationErrorKind::InvalidReturn,
                    &field.ty.to_token_stream().to_string(),
                    field.ty.span(),
                )
            })?
            .into_boxed_str();
            let item = TestFieldPtr {
                test_name: field_ptr_test_ident(id, field.ident()),
                id: id.into(),
                c_ty: c_ty?.into(),
                field: field.clone(),
                c_field: c_field.into_boxed_str(),
                volatile_keyword: volatile_keyword.into(),
                field_return_type,
            };
            self.field_ptr_tests.push(item.clone());
            self.test_idents.push(item.test_name);
        }

        Ok(())
    }

    /// Populates tests for extern functions.
    ///
    /// It also keeps track of the names of each test.
    fn populate_foreign_fn_tests(
        &mut self,
        helper: &TranslateHelper,
    ) -> Result<(), TranslationError> {
        let should_skip_fn_test = |ident| {
            helper
                .generator
                .skip_fn_ptrcheck
                .as_ref()
                .is_some_and(|skip| skip(ident))
        };
        for func in helper.filtered_ffi_items.foreign_functions() {
            if should_skip_fn_test(func.ident()) {
                continue;
            }

            let item = TestForeignFn {
                test_name: foreign_fn_test_ident(func.ident()),
                id: func.ident().into(),
                c_val: helper.c_ident(func).into_boxed_str(),
            };

            self.foreign_fn_tests.push(item.clone());
            self.test_idents.push(item.test_name);
        }

        Ok(())
    }

    /// Populates tests for foreign statics, keeping track of the names of each test.
    fn populate_foreign_static_tests(
        &mut self,
        helper: &TranslateHelper,
    ) -> Result<(), TranslationError> {
        for static_ in helper.filtered_ffi_items.foreign_statics() {
            let rust_ty = static_.ty.to_token_stream().to_string().into_boxed_str();

            let item = TestForeignStatic {
                test_name: static_test_ident(static_.ident()),
                id: static_.ident().into(),
                c_val: helper.c_ident(static_).into_boxed_str(),
                rust_ty,
            };

            self.foreign_static_tests.push(item.clone());
            self.test_idents.push(item.test_name);
        }

        Ok(())
    }
}

/* Many test structures have the following fields:
 *
 * - `test_name`: The function name.
 * - `id`: An identifier that can be used to create functions related to this type without conflict,
 *    usually also part of `test_name`.
 * - `rust_val`: Identifier for a Rust value, with path qualifications if needed.
 * - `rust_ty`: The Rust type of the relevant item, with path qualifications if needed.
 * - `c_val`: Identifier for a C value (e.g. `#define`)
 * - `c_ty`: The C type of the constant, qualified with `struct` or `union` if needed.
 */

#[derive(Clone, Debug)]
pub(crate) struct TestSignededness {
    pub test_name: BoxStr,
    pub id: BoxStr,
    pub c_ty: BoxStr,
}

#[derive(Clone, Debug)]
pub(crate) struct TestSizeAlign {
    pub test_name: BoxStr,
    pub id: BoxStr,
    pub rust_ty: BoxStr,
    pub c_ty: BoxStr,
}

/// Information required to test a constant CStr.
#[derive(Clone, Debug)]
pub(crate) struct TestCStr {
    pub test_name: BoxStr,
    pub id: BoxStr,
    pub rust_val: BoxStr,
    pub c_val: BoxStr,
}

/// Information required to test a constant.
#[derive(Clone, Debug)]
pub(crate) struct TestConst {
    pub test_name: BoxStr,
    pub id: BoxStr,
    pub rust_val: BoxStr,
    pub c_val: BoxStr,
    pub rust_ty: BoxStr,
    pub c_ty: BoxStr,
}

#[derive(Clone, Debug)]
pub(crate) struct TestFieldPtr {
    pub test_name: BoxStr,
    pub id: BoxStr,
    pub field: Field,
    pub c_field: BoxStr,
    pub c_ty: BoxStr,
    pub volatile_keyword: BoxStr,
    pub field_return_type: BoxStr,
}

#[derive(Clone, Debug)]
pub(crate) struct TestFieldSizeOffset {
    pub test_name: BoxStr,
    pub id: BoxStr,
    pub field: Field,
    pub c_field: BoxStr,
    pub c_ty: BoxStr,
}

#[derive(Clone, Debug)]
pub(crate) struct TestRoundtrip {
    pub test_name: BoxStr,
    pub id: BoxStr,
    pub fields: Vec<Field>,
    pub c_ty: BoxStr,
    pub is_alias: bool,
}

#[derive(Clone, Debug)]
pub(crate) struct TestForeignFn {
    pub test_name: BoxStr,
    pub c_val: BoxStr,
    pub id: BoxStr,
}

#[derive(Clone, Debug)]
pub(crate) struct TestForeignStatic {
    pub test_name: BoxStr,
    pub id: BoxStr,
    pub c_val: BoxStr,
    pub rust_ty: BoxStr,
}

fn signededness_test_ident(ident: &str) -> BoxStr {
    format!("ctest_signededness_{ident}").into()
}

fn size_align_test_ident(ident: &str) -> BoxStr {
    format!("ctest_size_align_{ident}").into()
}

fn cstr_test_ident(ident: &str) -> BoxStr {
    format!("ctest_const_cstr_{ident}").into()
}

fn const_test_ident(ident: &str) -> BoxStr {
    format!("ctest_const_{ident}").into()
}

fn field_ptr_test_ident(ident: &str, field_ident: &str) -> BoxStr {
    format!("ctest_field_ptr_{ident}_{field_ident}").into()
}

fn field_size_offset_test_ident(ident: &str, field_ident: &str) -> BoxStr {
    format!("ctest_field_size_offset_{ident}_{field_ident}").into()
}

fn roundtrip_test_ident(ident: &str) -> BoxStr {
    format!("ctest_roundtrip_{ident}").into()
}

fn foreign_fn_test_ident(ident: &str) -> BoxStr {
    format!("ctest_foreign_fn_{ident}").into()
}

fn static_test_ident(ident: &str) -> BoxStr {
    format!("ctest_static_{ident}").into()
}

/// Wrap methods that depend on both ffi items and the generator.
pub(crate) struct TranslateHelper<'a> {
    filtered_ffi_items: FfiItems,
    generator: &'a TestGenerator,
    translator: Translator<'a>,
}

impl<'a> TranslateHelper<'a> {
    /// Create a new translation helper.
    pub(crate) fn new(ffi_items: &'a FfiItems, generator: &'a TestGenerator) -> Self {
        let mut helper = Self {
            filtered_ffi_items: ffi_items.clone(),
            generator,
            translator: Translator::new(ffi_items, generator),
        };
        helper.filter_ffi_items();

        helper
    }

    /// Skips entire items such as structs, constants, and aliases from being tested.
    ///
    /// Does not skip specific tests or specific fields. If `skip_private` is true,
    /// it will skip tests for all private items.
    fn filter_ffi_items(&mut self) {
        let verbose = self.generator.verbose_skip;

        let skipped = self.filtered_ffi_items.aliases.extract_if(.., |alias| {
            self.generator
                .skips
                .iter()
                .any(|f| f(&MapInput::CEnumType(alias.ident())))
        });

        for item in skipped {
            if verbose {
                eprintln!("Skipping C enum type {}", item.ident());
            }
        }

        let skipped = self
            .filtered_ffi_items
            .constants
            .extract_if(.., |constant| {
                self.generator.skips.iter().any(|f| {
                    f(&MapInput::CEnumType(
                        &constant.ty.to_token_stream().to_string(),
                    ))
                })
            });

        for item in skipped {
            if verbose {
                eprintln!("Skipping C enum constant {}", item.ident());
            }
        }

        macro_rules! filter {
            ($field:ident, $variant:ident, $label:literal) => {{
                let skipped = self.filtered_ffi_items.$field.extract_if(.., |item| {
                    (self.generator.skip_private && !item.public)
                        || self
                            .generator
                            .skips
                            .iter()
                            .any(|f| f(&MapInput::$variant(item)))
                });
                for item in skipped {
                    if verbose {
                        eprintln!("Skipping {} \"{}\"", $label, item.ident())
                    }
                }
            }};
        }

        filter!(aliases, Alias, "alias");
        filter!(constants, Const, "const");
        filter!(structs, Struct, "struct");
        filter!(unions, Union, "union");
        filter!(foreign_functions, Fn, "fn");
        filter!(foreign_statics, Static, "static");
    }

    /// Returns the equivalent C/Cpp identifier of the Rust item.
    pub(crate) fn c_ident(&self, item: impl Into<MapInput<'a>>) -> String {
        self.generator.rty_to_cty(item)
    }

    /// Returns the equivalent C/Cpp type of the Rust item.
    pub(crate) fn c_type(&self, item: impl Into<MapInput<'a>>) -> Result<String, TranslationError> {
        let item: MapInput = item.into();

        let (ident, ty) = match item {
            MapInput::Const(c) => (c.ident(), self.translator.translate_type(&c.ty)?),
            MapInput::StructField(_, f) => (f.ident(), self.translator.translate_type(&f.ty)?),
            MapInput::UnionField(_, f) => (f.ident(), self.translator.translate_type(&f.ty)?),
            MapInput::Static(s) => (s.ident(), self.translator.translate_type(&s.ty)?),
            // For functions, their type would be a bare fn signature, which would need to be saved
            // inside of `Fn` when parsed.
            MapInput::Fn(_) => unimplemented!(),
            // For structs/unions/aliases, their type is the same as their identifier.
            MapInput::Alias(a) => (a.ident(), cdecl::named(a.ident(), Constness::Mut)),
            MapInput::Struct(s) => (s.ident(), cdecl::named(s.ident(), Constness::Mut)),
            MapInput::Union(u) => (u.ident(), cdecl::named(u.ident(), Constness::Mut)),

            MapInput::StructType(_) => panic!("MapInput::StructType is not allowed!"),
            MapInput::UnionType(_) => panic!("MapInput::UnionType is not allowed!"),
            MapInput::CEnumType(_) => panic!("MapInput::CEnumType is not allowed!"),
            MapInput::StructFieldType(_, _) => panic!("MapInput::StructFieldType is not allowed!"),
            MapInput::UnionFieldType(_, _) => panic!("MapInput::UnionFieldType is not allowed!"),
            MapInput::Type(_) => panic!("MapInput::Type is not allowed!"),
        };

        let ty = cdecl::cdecl(&ty, "".to_string()).map_err(|_| {
            TranslationError::new(
                TranslationErrorKind::InvalidReturn,
                ident,
                Span::call_site(),
            )
        })?;

        let item = self.translator.map_rust_name_to_c(&ty);

        Ok(self.generator.rty_to_cty(item))
    }
}