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
39 changes: 21 additions & 18 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1576,9 +1576,9 @@ bool Converter::VisitAttributedStmt(clang::AttributedStmt *stmt) {
}

bool Converter::VisitWhileStmt(clang::WhileStmt *stmt) {
ScopedValue break_loop(break_switch_, false);
ScopedValue break_loop(break_label_, "");
StrCat("'loop_:");
ScopedValue no_inc(curr_for_inc_, nullptr);
ScopedValue no_inc(continue_for_, false);
if (auto *cond_var = stmt->getConditionVariableDeclStmt()) {
StrCat(keyword::kLoop);
PushBrace loop_brace(*this);
Expand All @@ -1605,7 +1605,7 @@ void Converter::ConvertConditionVariableTest(clang::DeclStmt *cond_var,
}

bool Converter::VisitDoStmt(clang::DoStmt *stmt) {
ScopedValue break_loop(break_switch_, false);
ScopedValue break_loop(break_label_, "");
const char *control_var = "__do_while";
StrCat(keyword::kLet, "mut", control_var, token::kAssign, keyword::kTrue,
token::kSemiColon);
Expand All @@ -1617,14 +1617,14 @@ bool Converter::VisitDoStmt(clang::DoStmt *stmt) {
{
PushBrace loop_brace(*this);
StrCat(control_var, token::kAssign, keyword::kFalse, token::kSemiColon);
ScopedValue no_inc(curr_for_inc_, nullptr);
ScopedValue no_inc(continue_for_, false);
ConvertBodyStmts(stmt->getBody());
}
return false;
}

bool Converter::VisitForStmt(clang::ForStmt *stmt) {
ScopedValue break_loop(break_switch_, false);
ScopedValue break_loop(break_label_, stmt->getInc() ? "'loop_" : "");
// The variables of the init statement are only visible in the for.
PushBrace scope(*this, stmt->getInit() &&
clang::isa<clang::DeclStmt>(stmt->getInit()));
Expand All @@ -1647,7 +1647,14 @@ bool Converter::VisitForStmt(clang::ForStmt *stmt) {
ConvertConditionVariableTest(cond_var, stmt->getCond());
}
{
ScopedValue inc(curr_for_inc_, stmt->getInc());
ScopedValue inc(continue_for_, stmt->getInc() != nullptr);
// Leaving the body, including via continue, destroys its locals before
// the increment. The condition variable stays alive through the
// increment.
if (continue_for_) {
StrCat("'for_body:");
}
PushBrace body(*this, continue_for_);
ConvertBodyStmts(stmt->getBody());
}
Convert(stmt->getInc());
Expand All @@ -1657,7 +1664,7 @@ bool Converter::VisitForStmt(clang::ForStmt *stmt) {
}

bool Converter::VisitCXXForRangeStmt(clang::CXXForRangeStmt *stmt) {
ScopedValue break_loop(break_switch_, false);
ScopedValue break_loop(break_label_, "");
Convert(stmt->getInit());
Convert(stmt->getRangeStmt());
auto *begin =
Expand All @@ -1672,28 +1679,24 @@ bool Converter::VisitCXXForRangeStmt(clang::CXXForRangeStmt *stmt) {
ConvertFreshRValue(end->getInit()), ')');
{
PushBrace brace(*this);
ScopedValue no_inc(curr_for_inc_, nullptr);
ScopedValue no_inc(continue_for_, false);
Convert(stmt->getLoopVarStmt());
ConvertBodyStmts(stmt->getBody());
}
return false;
}

bool Converter::VisitBreakStmt([[maybe_unused]] clang::BreakStmt *stmt) {
StrCat(keyword::kBreak);
if (break_switch_) {
StrCat("'switch");
}
StrCat(keyword::kBreak, break_label_);
return false;
}

bool Converter::VisitContinueStmt([[maybe_unused]] clang::ContinueStmt *stmt) {
if (curr_for_inc_) {
Convert(curr_for_inc_);
StrCat(token::kSemiColon);
if (continue_for_) {
StrCat(keyword::kBreak, "'for_body");
} else {
StrCat(keyword::kContinue, "'loop_");
}
StrCat(keyword::kContinue);
StrCat("'loop_");
return false;
}

Expand Down Expand Up @@ -4524,7 +4527,7 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
return !arm.label.empty() || arm.has_fallthrough;
});

ScopedValue break_switch(break_switch_, !needs_switch_macro);
ScopedValue break_switch(break_label_, needs_switch_macro ? "" : "'switch");

// The variables of the init statement and of the condition are only
// visible in the switch.
Expand Down
10 changes: 5 additions & 5 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1066,16 +1066,16 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
bool constant_evaluated_ = false;
std::optional<bool> autoref_mut_;

// The increment of the innermost loop, which continue runs.
clang::Expr *curr_for_inc_ = nullptr;
// Whether continue exits 'for_body to run the for-loop increment.
bool continue_for_ = false;
// The names of the iterators of range-based for loops.
std::unordered_map<const clang::ValueDecl *, std::string>
range_iterator_names_;
std::vector<clang::QualType> curr_init_type_;

// Whether break exits a switch labeled 'switch, rather than a loop or a
// switch! macro, which a plain break exits.
bool break_switch_ = false;
// Breaks in a for body must leave the loop, not just the labeled body.
// A switch! macro owns plain breaks; a plain match uses 'switch.
const char *break_label_ = "";

// Local variables hoisted outside a goto_block so that all labels can see and
// use the variables.
Expand Down
30 changes: 19 additions & 11 deletions tests/std/basic/basic.scope/out/refcount/basic.scope.block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ pub fn test_8() {
{
let mut index: i32 = 0;
'loop_: while (index < 3) {
iterations.prefix_inc();
sum += index;
'for_body: {
iterations.prefix_inc();
sum += index;
}
index.prefix_inc();
}
}
Expand All @@ -151,9 +153,11 @@ pub fn test_9() {
{
let mut index: i32 = 0;
'loop_: while (index < 3) {
let scoped: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked::new()));
let _dtor_scoped =
std::mem::DropGuard::new(scoped.clone(), |__p| __p.as_pointer().destructor());
'for_body: {
let scoped: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked::new()));
let _dtor_scoped =
std::mem::DropGuard::new(scoped.clone(), |__p| __p.as_pointer().destructor());
}
index.prefix_inc();
}
}
Expand Down Expand Up @@ -224,12 +228,16 @@ pub fn test_12() {
{
let mut index: i32 = 0;
'loop_: while (index < 2) {
{
let mut index: i32 = 10;
'loop_: while (index < 13) {
inner_iterations.prefix_inc();
sum += index;
index.prefix_inc();
'for_body: {
{
let mut index: i32 = 10;
'loop_: while (index < 13) {
'for_body: {
inner_iterations.prefix_inc();
sum += index;
}
index.prefix_inc();
}
}
}
{
Expand Down
30 changes: 19 additions & 11 deletions tests/std/basic/basic.scope/out/unsafe/basic.scope.block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ pub unsafe fn test_8() {
{
let mut index: i32 = 0;
'loop_: while ((index) < (3)) {
iterations.prefix_inc();
sum += index;
'for_body: {
iterations.prefix_inc();
sum += index;
}
index.prefix_inc();
}
}
Expand All @@ -139,9 +141,11 @@ pub unsafe fn test_9() {
{
let mut index: i32 = 0;
'loop_: while ((index) < (3)) {
let mut scoped: LifetimeTracked = LifetimeTracked::new();
let _dtor_scoped =
std::mem::DropGuard::new(&raw mut scoped, |__p| unsafe { (*__p).destructor() });
'for_body: {
let mut scoped: LifetimeTracked = LifetimeTracked::new();
let _dtor_scoped =
std::mem::DropGuard::new(&raw mut scoped, |__p| unsafe { (*__p).destructor() });
}
index.prefix_inc();
}
}
Expand Down Expand Up @@ -208,12 +212,16 @@ pub unsafe fn test_12() {
{
let mut index: i32 = 0;
'loop_: while ((index) < (2)) {
{
let mut index: i32 = 10;
'loop_: while ((index) < (13)) {
inner_iterations.prefix_inc();
sum += index;
index.prefix_inc();
'for_body: {
{
let mut index: i32 = 10;
'loop_: while ((index) < (13)) {
'for_body: {
inner_iterations.prefix_inc();
sum += index;
}
index.prefix_inc();
}
}
}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ pub fn test_0() {
{
let mut k: i32 = 0;
'loop_: while (k < 3) {
sum += (*y.prefix_inc());
'for_body: {
sum += (*y.prefix_inc());
}
k.prefix_inc();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ pub unsafe fn test_0() {
{
let mut k: i32 = 0;
'loop_: while ((k) < (3)) {
sum += (*y.prefix_inc());
'for_body: {
sum += (*y.prefix_inc());
}
k.prefix_inc();
}
}
Expand Down
11 changes: 7 additions & 4 deletions tests/ub/out/refcount/ub6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ pub fn any_2(arr: Ptr<Option<Value<Box<[Ptr<i32>]>>>>, n1: Ptr<i32>) -> bool {
{
let mut i: i32 = 0;
'loop_: while ({ i } < { (n1.read()) }) {
let __rhs = (out)
|| (((({ (*arr.upgrade().deref()).decay().offset((i as usize)) }).read()).read())
== 0);
out = __rhs;
'for_body: {
let __rhs = (out)
|| (((({ (*arr.upgrade().deref()).decay().offset((i as usize)) }).read())
.read())
== 0);
out = __rhs;
}
i.prefix_inc();
}
}
Expand Down
14 changes: 8 additions & 6 deletions tests/ub/out/unsafe/ub6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ pub unsafe fn any_2(arr: *mut Option<Box<[*mut i32]>>, n1: *mut i32) -> bool {
{
let mut i: i32 = 0;
'loop_: while ((i) < (*n1)) {
out = (out)
|| ((*(*(*arr)
.as_deref()
.map_or(::std::ptr::null_mut(), |s| s.as_ptr() as *mut *mut i32)
.add((i as usize))))
== (0));
'for_body: {
out = (out)
|| ((*(*(*arr)
.as_deref()
.map_or(::std::ptr::null_mut(), |s| s.as_ptr() as *mut *mut i32)
.add((i as usize))))
== (0));
}
i.prefix_inc();
}
}
Expand Down
86 changes: 86 additions & 0 deletions tests/unit/for_body_destructor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <cstdio>

struct Guard {
int digit;
~Guard() { std::printf("%d ", digit); }
explicit operator bool() const { return digit != 0; }
};

void step(int &i) {
std::printf("9 ");
++i;
}

int main() {
std::printf("normal/continue: ");
for (int i = 0; i < 2; step(i)) {
Guard first{1};
Guard second{2};
if (i == 0)
continue;
}

std::printf("\nbreak: ");
for (int i = 0; i < 2; step(i)) {
Guard g{3};
break;
}

std::printf("\nnested for: ");
for (int i = 0; i < 1; step(i)) {
Guard outer{4};
for (int j = 0; j < 2; step(j)) {
Guard inner{5};
if (j == 0)
continue;
break;
}
continue;
}

std::printf("\ninner while/do/range: ");
for (int i = 0; i < 1; step(i)) {
Guard outer{4};
int j = 0;
while (++j < 3) {
Guard inner{5};
if (j == 1)
continue;
break;
}
do {
Guard inner{6};
continue;
} while (false);
int values[] = {1, 2};
for (int value : values) {
Guard inner{value};
if (value == 1)
continue;
break;
}
continue;
}

std::printf("\ncondition: ");
for (int i = 2; Guard condition{i};
--i, std::printf("inc%d ", condition.digit)) {
Guard body{4};
if (i == 2)
continue;
}

std::printf("\ninit/noncompound: ");
int i = 0;
for (Guard init{5}; i < 2; step(i))
Guard body{6};

std::printf("\nno increment: ");
for (int j = 0; j < 2;) {
Guard g{++j};
continue;
}

std::printf("\n");
return 0;
}
4 changes: 3 additions & 1 deletion tests/unit/out/refcount/alloc_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub fn All_0(arr: Ptr<Option<Value<Box<[i32]>>>>, mut N: i32, mut element: i32)
{
let mut i: i32 = 0;
'loop_: while (i < N) {
all.decay().offset((i as usize)).write(element);
'for_body: {
all.decay().offset((i as usize)).write(element);
}
i.prefix_inc();
}
}
Expand Down
Loading
Loading