Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions .github/workflows/internal-testsuite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,6 @@ jobs:
rustup toolchain install nightly-2023-04-15 \
--profile minimal --component rustfmt

# TODO(pl): Figure out why json-c fails when caching compile commands
# - name: Get Image Version
# id: get-image-ver
# run: |
# echo "::set-output name=version::$(echo $ImageVersion)"
# shell: bash

# - name: Cache testsuite compile_commands
# uses: actions/cache@v4
# with:
# path: |
# ${{ github.workspace }}/tests/integration/tests/**/compile_commands.json
# key: ${{ runner.os }}-ccdb-${{ steps.get-image-ver.outputs.version }}

- name: Provision Debian Packages
run: |
sudo apt-get -qq update
Expand Down Expand Up @@ -120,10 +106,8 @@ jobs:
(cd tools && cargo build --release --manifest-path split_rust/Cargo.toml)
(cd tools && cargo build --release --manifest-path merge_rust/Cargo.toml)

# TODO(pl): figure out why compile_commands.json may cause json-c to fail
- name: Run c2rust testsuite
run: |
find tests/integration -type f -name compile_commands.json -delete
export PATH=$PWD/target/release:$PWD/c2rust-postprocess:$HOME/.local/bin:$PATH
echo "PATH=$PATH"
export C2RUST_DIR=$PWD
Expand Down
17 changes: 7 additions & 10 deletions c2rust-transpile/src/translator/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,16 +448,13 @@ impl<'c> Translation<'c> {

let args = self.convert_call_args(ctx.used(), args, arg_tys.as_deref(), is_variadic)?;

let mut call_expr = args.map(|args| mk().call_expr(func, args));
if let Some(expected_ty) = override_ty {
if call_expr_ty != expected_ty {
let ret_ty = self.convert_type(expected_ty.ctype)?;
call_expr = call_expr.map(|call| mk().cast_expr(call, ret_ty));
}
}

let res: TranslationResult<_> = Ok(call_expr);
res
let call_expr = args.map(|args| mk().call_expr(func, args));
self.make_cast(
ctx,
call_expr_ty,
override_ty.unwrap_or(call_expr_ty),
call_expr,
)
})?;

self.convert_side_effects_expr(
Expand Down
130 changes: 82 additions & 48 deletions c2rust-transpile/src/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2905,33 +2905,31 @@ impl<'c> Translation<'c> {
pub fn compute_size_of_type(
&self,
ctx: ExprContext,
expected_type_id: Option<CQualTypeId>,
result_type_id: CQualTypeId,
type_id: CTypeId,
override_ty: Option<CQualTypeId>,
) -> TranslationResult<WithStmts<Box<Expr>>> {
if let CTypeKind::VariableArray(elts, len) = self.ast_context.resolve_type(type_id).kind {
let len = len.expect("Sizeof a VLA type with count expression omitted");

let elts = self.compute_size_of_type(ctx, elts, override_ty)?;
let elts = self.compute_size_of_type(ctx, expected_type_id, result_type_id, elts)?;
return elts.and_then_try(|lhs| {
let len = self.convert_expr(ctx.used().not_static(), len, override_ty)?;
let len = self.convert_expr(ctx.used().not_static(), len, expected_type_id)?;
Ok(len.map(|len| {
let rhs = cast_int(len, "usize", true);
mk().binary_expr(BinOp::Mul(Default::default()), lhs, rhs)
}))
});
}
let ty = self.convert_type(type_id)?;
let mut result = self.mk_size_of_ty_expr(ty);
// cast to expected ty if one is known
if let Some(expected_ty) = override_ty {
trace!(
"Converting result of sizeof to {:?}",
self.ast_context.resolve_type(expected_ty.ctype)
);
let result_ty = self.convert_type(expected_ty.ctype)?;
result = result.map(|x| x.map(|x| mk().cast_expr(x, result_ty)));
}
result
let result = self.mk_size_of_ty_expr(ty)?;

self.make_cast(
ctx,
result_type_id,
expected_type_id.unwrap_or(result_type_id),
result,
)
}

fn mk_size_of_ty_expr(&self, ty: Box<Type>) -> TranslationResult<WithStmts<Box<Expr>>> {
Expand All @@ -2949,6 +2947,9 @@ impl<'c> Translation<'c> {

pub fn compute_align_of_type(
&self,
ctx: ExprContext,
expected_type_id: Option<CQualTypeId>,
result_type_id: CQualTypeId,
mut type_id: CTypeId,
preferred: bool,
src_loc: &Option<SrcSpan>,
Expand Down Expand Up @@ -2985,7 +2986,12 @@ impl<'c> Translation<'c> {
path.push(mk().path_segment_with_args("align_of", mk().angle_bracketed_args(tys)));
}
let call = mk().call_expr(mk().abs_path_expr(path), vec![]);
Ok(WithStmts::new_val(call))
self.make_cast(
ctx,
result_type_id,
expected_type_id.unwrap_or(result_type_id),
WithStmts::new_val(call),
)
}

/// Convert multiple expressions (while collecting a context of statements) given either all or
Expand Down Expand Up @@ -3066,13 +3072,19 @@ impl<'c> Translation<'c> {
}),
ConvertVector(..) => Err(TranslationError::generic("convert vector not supported")),

UnaryType(_ty, kind, opt_expr, arg_ty) => {
UnaryType(result_type_id, kind, opt_expr, arg_ty) => {
let result = match kind {
CUnTypeOp::SizeOf => match opt_expr {
None => self.compute_size_of_type(ctx, arg_ty.ctype, override_ty)?,
None => self.compute_size_of_type(
ctx,
override_ty,
result_type_id,
arg_ty.ctype,
)?,
Some(_) => {
let inner = self.variable_array_base_type(arg_ty.ctype);
let inner_size = self.compute_size_of_type(ctx, inner, override_ty)?;
let inner_size =
self.compute_size_of_type(ctx, override_ty, result_type_id, inner)?;

if let Some(sz) = self.compute_size_of_expr(arg_ty.ctype) {
inner_size.map(|x| {
Expand All @@ -3084,12 +3096,22 @@ impl<'c> Translation<'c> {
}
}
},
CUnTypeOp::AlignOf => {
self.compute_align_of_type(arg_ty.ctype, false, src_loc)?
}
CUnTypeOp::PreferredAlignOf => {
self.compute_align_of_type(arg_ty.ctype, true, src_loc)?
}
CUnTypeOp::AlignOf => self.compute_align_of_type(
ctx,
override_ty,
result_type_id,
arg_ty.ctype,
false,
src_loc,
)?,
CUnTypeOp::PreferredAlignOf => self.compute_align_of_type(
ctx,
override_ty,
result_type_id,
arg_ty.ctype,
true,
src_loc,
)?,
};

Ok(result)
Expand All @@ -3099,7 +3121,7 @@ impl<'c> Translation<'c> {
if let Some(constant) = value {
self.convert_constant(constant).map(WithStmts::new_val)
} else {
self.convert_expr(ctx, child, Some(ty))
self.convert_expr(ctx, child, Some(override_ty.unwrap_or(ty)))
}
}

Expand Down Expand Up @@ -3223,14 +3245,13 @@ impl<'c> Translation<'c> {
val = mk().method_call_expr(val, "as_mut_ptr", vec![]);
}

// if the context wants a different type, add a cast
if let Some(expected_ty) = override_ty {
if lrvalue.is_rvalue() && expected_ty != qual_ty {
val = mk().cast_expr(val, self.convert_type(expected_ty.ctype)?);
}
let mut val = WithStmts::new_val(val).merge_unsafe(set_unsafe);

if lrvalue.is_rvalue() {
val = self.make_cast(ctx, qual_ty, override_ty.unwrap_or(qual_ty), val)?;
}

Ok(WithStmts::new_val(val).merge_unsafe(set_unsafe))
Ok(val)
}

OffsetOf(ty, ref kind) => match kind {
Expand Down Expand Up @@ -3356,10 +3377,12 @@ impl<'c> Translation<'c> {
}

BinaryConditional(ty, lhs, rhs) => {
let rhs = self.convert_expr(ctx, rhs, None)?;

if ctx.is_unused() {
let mut lhs = self.convert_condition(ctx, false, lhs)?;
let rhs = self.convert_expr(ctx, rhs, None)?;
lhs = lhs.merge_unsafe(rhs.is_unsafe());
let lhs = self
.convert_condition(ctx, false, lhs)?
.merge_unsafe(rhs.is_unsafe());

Ok(lhs.and_then(|val| {
WithStmts::new(
Expand All @@ -3374,19 +3397,28 @@ impl<'c> Translation<'c> {
)
}))
} else {
self.name_reference_write_read(ctx, lhs)?.try_map(
|NamedReference {
rvalue: lhs_val, ..
}| {
let cond = self.match_bool(ctx, true, ty.ctype, lhs_val.clone())?;
let ite = mk().ifte_expr(
cond,
mk().block(vec![mk().expr_stmt(lhs_val)]),
Some(self.convert_expr(ctx, rhs, None)?.to_expr()),
);
Ok(ite)
},
)
let lhs = self
.convert_expr(ctx.used(), lhs, None)?
.merge_unsafe(rhs.is_unsafe());
let fresh_name = self.renamer.borrow_mut().fresh();

lhs.and_then_try(|lhs| {
let fresh_stmt = mk().local_stmt(Box::new(mk().local(
mk().ident_pat(&fresh_name),
None,
Some(lhs),
)));

let cond =
self.match_bool(ctx, true, ty.ctype, mk().ident_expr(&fresh_name))?;
let ite = mk().ifte_expr(
cond,
mk().block(vec![mk().expr_stmt(mk().ident_expr(&fresh_name))]),
Some(rhs.to_expr()),
);

Ok(WithStmts::new(vec![fresh_stmt], ite))
})
}
}

Expand Down Expand Up @@ -3426,7 +3458,9 @@ impl<'c> Translation<'c> {
self.convert_init_list(ctx, override_ty, ty, ids, opt_union_field_id)
}

ImplicitValueInit(ty) => self.implicit_default_expr(ctx, ty.ctype),
ImplicitValueInit(ty) => {
self.implicit_default_expr(ctx, override_ty.unwrap_or(ty).ctype)
}

Predefined(_, val_id) => self.convert_expr(ctx, val_id, override_ty),

Expand Down
11 changes: 7 additions & 4 deletions c2rust-transpile/src/translator/named_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,16 @@ impl<'c> Translation<'c> {
} else {
// This is the case where we explicitly need to factor out possible side-effects.

let ptr_name = self.renamer.borrow_mut().fresh();
let ptr_name = self.renamer.borrow_mut().pick_name("c2rust_lvalue_ptr");

// let ref mut p = lhs;
// let p = &raw mut lhs;
// Use a raw pointer here so that it doesn't create borrow conflicts and
// doesn't invalidate other pointers.
self.use_feature("raw_ref_op");
let compute_ref = mk().local_stmt(Box::new(mk().local(
mk().mutbl().ident_ref_pat(&ptr_name),
mk().ident_pat(&ptr_name),
None,
Some(reference),
Some(mk().mutbl().raw_borrow_expr(reference)),
)));

let write =
Expand Down
12 changes: 6 additions & 6 deletions c2rust-transpile/src/translator/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<'c> Translation<'c> {
)))
} else {
let lhs = self.make_cast(
ctx,
ctx.used(),
initial_lhs_type_id,
compute_lhs_type_id,
WithStmts::new_val(read.clone()),
Expand All @@ -231,7 +231,7 @@ impl<'c> Translation<'c> {
)
})?;

let val = self.make_cast(ctx, compute_res_type_id, lhs_type_id, val)?;
let val = self.make_cast(ctx.used(), compute_res_type_id, lhs_type_id, val)?;

Ok(val.map(|val| mk().assign_expr(write.clone(), val)))
}
Expand Down Expand Up @@ -420,7 +420,7 @@ impl<'c> Translation<'c> {
.expect("Cannot convert non-assignment operator");

let lhs = self.make_cast(
ctx,
ctx.used(),
initial_lhs_type_id,
expr_or_comp_type_id,
WithStmts::new_val(read.clone()),
Expand All @@ -440,7 +440,7 @@ impl<'c> Translation<'c> {
)?;

let val = self.make_cast(
ctx,
ctx.used(),
result_type_id,
expr_type_id,
val,
Expand Down Expand Up @@ -638,7 +638,7 @@ impl<'c> Translation<'c> {
};

self.convert_assignment_operator_with_rhs(
ctx.used(),
ctx,
op,
ty,
arg,
Expand Down Expand Up @@ -777,7 +777,7 @@ impl<'c> Translation<'c> {
.map(|a| mk().unary_expr(UnOp::Not(Default::default()), a))),

CUnOp::Not => {
let val = self.convert_condition(ctx, false, arg)?;
let val = self.convert_condition(ctx.used(), false, arg)?;
Ok(val.map(|x| mk().cast_expr(x, mk().abs_path_ty(vec!["core", "ffi", "c_int"]))))
}
CUnOp::Extension => {
Expand Down
15 changes: 9 additions & 6 deletions c2rust-transpile/src/translator/pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,16 @@ impl<'c> Translation<'c> {

let mut val =
self.convert_pointer_offset(lhs, rhs, pointee_type_id.ctype, false, deref);
// if the context wants a different type, add a cast
if let Some(expected_ty) = override_ty {
if lrvalue.is_rvalue() && expected_ty != pointee_type_id {
let ty = self.convert_type(expected_ty.ctype)?;
val = val.map(|val| mk().cast_expr(val, ty));
}

if lrvalue.is_rvalue() {
val = self.make_cast(
ctx,
pointee_type_id,
override_ty.unwrap_or(pointee_type_id),
val,
)?;
}

Ok(val)
})
}
Expand Down
8 changes: 2 additions & 6 deletions c2rust-transpile/src/translator/structs_unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,12 +1043,8 @@ impl<'a> Translation<'a> {
val = val.map(|v| mk().field_expr(v, field_name));
};

// if the context wants a different type, add a cast
if let Some(expected_ty) = override_ty {
if lrvalue.is_rvalue() && expected_ty != qual_ty {
let ty = self.convert_type(expected_ty.ctype)?;
val = val.map(|v| mk().cast_expr(v, ty));
}
if lrvalue.is_rvalue() {
val = self.make_cast(ctx, qual_ty, override_ty.unwrap_or(qual_ty), val)?;
}

Ok(val)
Expand Down
Loading
Loading