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
24 changes: 24 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,9 @@ bool Converter::VisitUnaryOperator(clang::UnaryOperator *expr) {
return false;
}
switch (opcode) {
case clang::UO_Extension:
Convert(sub_expr);
break;
case clang::UO_AddrOf: {
PushParen paren(*this);
ConvertAddrOf(sub_expr, expr->getType());
Expand Down Expand Up @@ -2091,6 +2094,27 @@ bool Converter::VisitUnaryOperator(clang::UnaryOperator *expr) {
return false;
}

bool Converter::VisitStmtExpr(clang::StmtExpr *expr) {
auto *body = expr->getSubStmt();
PushBrace brace(*this);
auto stmts = body->body();
size_t n = static_cast<size_t>(stmts.end() - stmts.begin());
size_t i = 0;
for (auto *s : stmts) {
++i;
if (i == n) {
if (auto *tail = clang::dyn_cast<clang::Expr>(s)) {
EmitStmtExprTail(tail);
continue;
}
}
Convert(s);
}
return false;
}

void Converter::EmitStmtExprTail(clang::Expr *tail) { Convert(tail); }

bool Converter::VisitConditionalOperator(clang::ConditionalOperator *expr) {
StrCat(keyword::kIf);
Convert(expr->getCond());
Expand Down
4 changes: 4 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual bool VisitUnaryOperator(clang::UnaryOperator *expr);

virtual bool VisitStmtExpr(clang::StmtExpr *expr);

virtual void EmitStmtExprTail(clang::Expr *tail);

virtual bool VisitConditionalOperator(clang::ConditionalOperator *expr);

virtual bool VisitDeclRefExpr(clang::DeclRefExpr *expr);
Expand Down
14 changes: 12 additions & 2 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,18 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
}
}

bool ConverterRefCount::VisitStmtExpr(clang::StmtExpr *expr) {
PushConversionKind push(*this, ConversionKind::FullRefCount);
return Converter::VisitStmtExpr(expr);
}

void ConverterRefCount::EmitStmtExprTail(clang::Expr *tail) {
StrCat("let __result = ");
Convert(tail);
StrCat(token::kSemiColon);
StrCat("__result");
}

bool ConverterRefCount::VisitBinaryOperator(clang::BinaryOperator *expr) {
auto *lhs = expr->getLHS();
auto *rhs = expr->getRHS();
Expand Down Expand Up @@ -1654,12 +1666,10 @@ void ConverterRefCount::ConvertVarInit(clang::QualType qual_type,
}

bool is_ref = qual_type->isReferenceType();
in_function_formals_ = true;
PushConversionKind push(*this, ConversionKind::Unboxed, is_ref);
StrCat(BoxValue((is_ref || qual_type->isFunctionPointerType())
? ConvertFreshPointer(expr)
: ConvertFreshRValue(expr)));
in_function_formals_ = false;
}

static std::unordered_set<const clang::ValueDecl *>
Expand Down
4 changes: 4 additions & 0 deletions cpp2rust/converter/models/converter_refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class ConverterRefCount final : public Converter {

bool VisitBinaryOperator(clang::BinaryOperator *expr) override;

bool VisitStmtExpr(clang::StmtExpr *expr) override;

void EmitStmtExprTail(clang::Expr *tail) override;

bool VisitInitListExpr(clang::InitListExpr *expr) override;

bool VisitArraySubscriptExpr(clang::ArraySubscriptExpr *expr) override;
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/out/refcount/stmt_expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let x: Value<i32> = Rc::new(RefCell::new({
let a: Value<i32> = Rc::new(RefCell::new(1));
let b: Value<i32> = Rc::new(RefCell::new(2));
let __result = ((*a.borrow()) + (*b.borrow()));
__result
}));
assert!(((*x.borrow()) == 3));
let counter: Value<i32> = Rc::new(RefCell::new(0));
let y: Value<i32> = Rc::new(RefCell::new({
(*counter.borrow_mut()).postfix_inc();
let __result = ((*counter.borrow()) * 10);
__result
}));
assert!(((*y.borrow()) == 10));
assert!(((*counter.borrow()) == 1));
let z: Value<i32> = Rc::new(RefCell::new({
let v: Value<i32> = Rc::new(RefCell::new(5));
if ((*v.borrow()) > 0) {
let __rhs = ((*v.borrow()) * 2);
(*v.borrow_mut()) = __rhs;
}
let __result = (*v.borrow());
__result
}));
assert!(((*z.borrow()) == 10));
assert!(
({
let inner: Value<i32> = Rc::new(RefCell::new({
let a: Value<i32> = Rc::new(RefCell::new(100));
let __result = (*a.borrow());
__result
}));
let __result = (*inner.borrow());
__result
} == 100)
);
return 0;
}
46 changes: 46 additions & 0 deletions tests/unit/out/unsafe/stmt_expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut x: i32 = {
let mut a: i32 = 1;
let mut b: i32 = 2;
((a) + (b))
};
assert!(((x) == (3)));
let mut counter: i32 = 0;
let mut y: i32 = {
counter.postfix_inc();
((counter) * (10))
};
assert!(((y) == (10)));
assert!(((counter) == (1)));
let mut z: i32 = {
let mut v: i32 = 5;
if ((v) > (0)) {
v = ((v) * (2));
}
v
};
assert!(((z) == (10)));
assert!(
(({
let mut inner: i32 = {
let mut a: i32 = 100;
a
};
inner
}) == (100))
);
return 0;
}
36 changes: 36 additions & 0 deletions tests/unit/stmt_expr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <cassert>

int main() {
int x = __extension__({
int a = 1;
int b = 2;
a + b;
});
assert(x == 3);

int counter = 0;
int y = ({
counter++;
counter * 10;
});
assert(y == 10);
assert(counter == 1);

int z = ({
int v = 5;
if (v > 0) {
v = v * 2;
}
v;
});
assert(z == 10);

assert(({
int inner = ({
int a = 100;
a;
});
inner;
}) == 100);
return 0;
}
Loading