Skip to content

Commit 7f3d696

Browse files
mbyxtgross35
authored andcommitted
ctest: improve translation backend
1 parent 0f69069 commit 7f3d696

File tree

8 files changed

+328
-416
lines changed

8 files changed

+328
-416
lines changed

ctest-next/src/cdecl.rs

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fmt::Write;
44

55
type BoxStr = Box<str>;
66

7-
#[cfg_attr(not(test), expect(dead_code))]
87
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
98
pub(crate) enum Constness {
109
Const,
@@ -15,7 +14,6 @@ pub(crate) enum Constness {
1514
use Constness::{Const, Mut};
1615

1716
/// A basic representation of C's types.
18-
#[cfg_attr(not(test), expect(dead_code))]
1917
#[derive(Clone, Debug)]
2018
pub(crate) enum CTy {
2119
/// `int`, `struct foo`, etc. There is only ever one basic type per decl.
@@ -85,7 +83,6 @@ pub(crate) struct InvalidReturn;
8583
/// value to allow reusing allocations.
8684
///
8785
/// If needed, `name` can be empty (e.g. for function arguments).
88-
#[cfg_attr(not(test), expect(dead_code))]
8986
pub(crate) fn cdecl(cty: &CTy, mut name: String) -> Result<String, InvalidReturn> {
9087
cdecl_impl(cty, &mut name, None)?;
9188
Ok(name)
@@ -178,6 +175,74 @@ fn space_if(yes: bool, s: &mut String) {
178175
}
179176
}
180177

178+
pub(crate) fn named(name: &str, constness: Constness) -> CTy {
179+
CTy::Named {
180+
name: name.into(),
181+
qual: Qual {
182+
constness,
183+
volatile: false,
184+
restrict: false,
185+
},
186+
}
187+
}
188+
189+
#[cfg_attr(not(test), expect(unused))]
190+
pub(crate) fn named_qual(name: &str, qual: Qual) -> CTy {
191+
CTy::Named {
192+
name: name.into(),
193+
qual,
194+
}
195+
}
196+
197+
pub(crate) fn ptr(inner: CTy, constness: Constness) -> CTy {
198+
ptr_qual(
199+
inner,
200+
Qual {
201+
constness,
202+
volatile: false,
203+
restrict: false,
204+
},
205+
)
206+
}
207+
208+
pub(crate) fn ptr_qual(inner: CTy, qual: Qual) -> CTy {
209+
CTy::Ptr {
210+
ty: Box::new(inner),
211+
qual,
212+
}
213+
}
214+
215+
pub(crate) fn array(inner: CTy, len: Option<&str>) -> CTy {
216+
CTy::Array {
217+
ty: Box::new(inner),
218+
len: len.map(Into::into),
219+
}
220+
}
221+
222+
/// Function type (not a pointer)
223+
#[cfg_attr(not(test), expect(unused))]
224+
pub(crate) fn func(args: Vec<CTy>, ret: CTy) -> CTy {
225+
CTy::Fn {
226+
args,
227+
ret: Box::new(ret),
228+
}
229+
}
230+
231+
/// Function pointer
232+
pub(crate) fn func_ptr(args: Vec<CTy>, ret: CTy) -> CTy {
233+
CTy::Ptr {
234+
ty: Box::new(CTy::Fn {
235+
args,
236+
ret: Box::new(ret),
237+
}),
238+
qual: Qual {
239+
constness: Constness::Mut,
240+
volatile: false,
241+
restrict: false,
242+
},
243+
}
244+
}
245+
181246
/// Checked with <https://cdecl.org/>.
182247
#[cfg(test)]
183248
mod tests {
@@ -210,68 +275,6 @@ mod tests {
210275
named("int", Const)
211276
}
212277

213-
fn named(name: &str, constness: Constness) -> CTy {
214-
CTy::Named {
215-
name: name.into(),
216-
qual: Qual {
217-
constness,
218-
volatile: false,
219-
restrict: false,
220-
},
221-
}
222-
}
223-
224-
fn named_qual(name: &str, qual: Qual) -> CTy {
225-
CTy::Named {
226-
name: name.into(),
227-
qual,
228-
}
229-
}
230-
231-
fn ptr(inner: CTy, constness: Constness) -> CTy {
232-
ptr_qual(
233-
inner,
234-
Qual {
235-
constness,
236-
volatile: false,
237-
restrict: false,
238-
},
239-
)
240-
}
241-
242-
fn ptr_qual(inner: CTy, qual: Qual) -> CTy {
243-
CTy::Ptr {
244-
ty: Box::new(inner),
245-
qual,
246-
}
247-
}
248-
249-
fn array(inner: CTy, len: Option<&str>) -> CTy {
250-
CTy::Array {
251-
ty: Box::new(inner),
252-
len: len.map(Into::into),
253-
}
254-
}
255-
256-
/// Function type (not a pointer)
257-
fn func(args: Vec<CTy>, ret: CTy) -> CTy {
258-
CTy::Fn {
259-
args,
260-
ret: Box::new(ret),
261-
}
262-
}
263-
264-
/// Function pointer
265-
fn func_ptr(args: Vec<CTy>, ret: CTy) -> CTy {
266-
ptr(
267-
CTy::Fn {
268-
args,
269-
ret: Box::new(ret),
270-
},
271-
Mut,
272-
)
273-
}
274-
275278
#[test]
276279
fn basic() {
277280
assert_decl(&const_int(), "const int foo");
@@ -350,7 +353,7 @@ mod tests {
350353
// Can't return an array
351354
assert!(cdecl(&func(vec![], array(mut_int(), None)), "foo".to_owned(),).is_err(),);
352355
// Can't return a function
353-
assert!(cdecl(&func(vec![], func(vec![], mut_int()),), "foo".to_owned(),).is_err(),);
356+
assert!(cdecl(&func(vec![], func(vec![], mut_int())), "foo".to_owned(),).is_err(),);
354357
}
355358

356359
#[test]

ctest-next/src/generator.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use thiserror::Error;
99

1010
use crate::ffi_items::FfiItems;
1111
use crate::template::{CTestTemplate, RustTestTemplate};
12+
use crate::translator::translate_primitive_type;
1213
use crate::{
1314
Const, Field, MapInput, Parameter, Result, Static, Struct, TranslationError, Type, Union,
1415
VolatileItemKind, expand,
@@ -962,7 +963,7 @@ impl TestGenerator {
962963
MapInput::UnionType(ty) => format!("union {ty}"),
963964
MapInput::StructFieldType(_, f) => f.ident().to_string(),
964965
MapInput::UnionFieldType(_, f) => f.ident().to_string(),
965-
MapInput::Type(ty) => ty.to_string(),
966+
MapInput::Type(ty) => translate_primitive_type(ty),
966967
}
967968
}
968969
}

0 commit comments

Comments
 (0)