Skip to content

Commit 1bc514b

Browse files
committed
transpile: handle statements in --translate-const-macros conservative
1 parent 731bf4e commit 1bc514b

File tree

1 file changed

+46
-3
lines changed
  • c2rust-transpile/src/c_ast

1 file changed

+46
-3
lines changed

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,52 @@ impl TypedAstContext {
686686
}
687687
}
688688

689-
pub fn is_const_stmt(&self, _stmt: CStmtId) -> bool {
690-
// TODO
691-
false
689+
pub fn is_const_stmt(&self, stmt: CStmtId) -> bool {
690+
let is_const = |stmt| self.is_const_stmt(stmt);
691+
let is_const_expr = |expr| self.is_const_expr(expr);
692+
693+
use CStmtKind::*;
694+
match self[stmt].kind {
695+
Label(stmt) => is_const(stmt),
696+
Case(expr, stmt, _const_expr) => is_const_expr(expr) && is_const(stmt),
697+
Default(stmt) => is_const(stmt),
698+
Compound(ref stmts) => stmts.iter().copied().all(is_const),
699+
Expr(expr) => is_const_expr(expr),
700+
Empty => true,
701+
If {
702+
scrutinee,
703+
true_variant,
704+
false_variant,
705+
} => {
706+
is_const_expr(scrutinee)
707+
&& is_const(true_variant)
708+
&& false_variant.map_or(true, is_const)
709+
}
710+
Switch { scrutinee, body } => is_const_expr(scrutinee) && is_const(body),
711+
While { condition, body } => is_const_expr(condition) && is_const(body),
712+
DoWhile { body, condition } => is_const(body) && is_const_expr(condition),
713+
ForLoop {
714+
init,
715+
condition,
716+
increment,
717+
body,
718+
} => {
719+
init.map_or(true, is_const)
720+
&& condition.map_or(true, is_const_expr)
721+
&& increment.map_or(true, is_const_expr)
722+
&& is_const(body)
723+
}
724+
Goto(label) => is_const(label),
725+
Break => true,
726+
Continue => true,
727+
Return(expr) => expr.map_or(true, is_const_expr),
728+
Decls(ref _decls) => true,
729+
Asm { .. } => false,
730+
Attributed {
731+
attributes: _,
732+
substatement,
733+
} => is_const(substatement),
734+
}
692735
}
693736

694737
pub fn prune_unwanted_decls(&mut self, want_unused_functions: bool) {

0 commit comments

Comments
 (0)