Skip to content

Commit f423f64

Browse files
committed
transpile: handle statements in --translate-const-macros conservative
1 parent 2e89c7d commit f423f64

File tree

1 file changed

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

1 file changed

+48
-3
lines changed

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,54 @@ impl TypedAstContext {
684684
}
685685
}
686686

687-
pub fn is_const_stmt(&self, _stmt: CStmtId) -> bool {
688-
// TODO
689-
false
687+
pub fn is_const_stmt(&self, stmt: CStmtId) -> bool {
688+
let is_const = |stmt| self.is_const_stmt(stmt);
689+
let is_const_expr = |expr| self.is_const_expr(expr);
690+
691+
use CStmtKind::*;
692+
match self[stmt].kind {
693+
Label(stmt) => is_const(stmt),
694+
Case(expr, stmt, _const_expr) => is_const_expr(expr) && is_const(stmt),
695+
Default(stmt) => is_const(stmt),
696+
Compound(ref stmts) => stmts.iter().copied().all(is_const),
697+
Expr(expr) => is_const_expr(expr),
698+
Empty => true,
699+
If {
700+
scrutinee,
701+
true_variant,
702+
false_variant,
703+
} => {
704+
is_const_expr(scrutinee)
705+
&& [Some(true_variant), false_variant]
706+
.into_iter()
707+
.flatten()
708+
.all(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+
}
690735
}
691736

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

0 commit comments

Comments
 (0)