Skip to content

Commit 35bc419

Browse files
committed
Merge branch 'master' into idiomatic-name
2 parents 749c8ad + 7a349c9 commit 35bc419

5 files changed

Lines changed: 139 additions & 1 deletion

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,12 @@ bool Converter::VisitIfStmt(clang::IfStmt *stmt) {
13761376
return false;
13771377
}
13781378
StrCat(keyword::kIf);
1379-
ConvertCondition(stmt->getCond());
1379+
if (auto *cond = clang::dyn_cast<clang::ConstantExpr>(stmt->getCond());
1380+
cond && stmt->isConstexpr()) {
1381+
StrCat(cond->getResultAsAPSInt() != 0 ? keyword::kTrue : keyword::kFalse);
1382+
} else {
1383+
ConvertCondition(stmt->getCond());
1384+
}
13801385
ConvertBody(stmt->getThen());
13811386
if (stmt->hasElseStorage()) {
13821387
StrCat(keyword::kElse);

‎cpp2rust/converter/converter.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
438438
VisitUnaryExprOrTypeTraitExpr(clang::UnaryExprOrTypeTraitExpr *expr);
439439

440440
virtual bool VisitTypeTraitExpr(clang::TypeTraitExpr *expr);
441+
441442
virtual bool VisitSizeOfPackExpr(clang::SizeOfPackExpr *expr);
442443

443444
virtual bool VisitOffsetOfExpr(clang::OffsetOfExpr *expr);

‎tests/unit/if_constexpr.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <cassert>
2+
#include <type_traits>
3+
4+
template <typename T> int classify(T x) {
5+
if constexpr (std::is_pointer_v<T>) {
6+
return *x;
7+
} else if constexpr (sizeof(T) > 4) {
8+
return 2;
9+
}
10+
return 1;
11+
}
12+
13+
int keep_both(int x) {
14+
if constexpr (sizeof(int) > 2) {
15+
return x + 1;
16+
} else {
17+
return x - 1;
18+
}
19+
}
20+
21+
int main() {
22+
int v = 7;
23+
assert(classify(&v) == 7);
24+
assert(classify(1L) == 2);
25+
assert(classify(1) == 1);
26+
assert(keep_both(1) == 2);
27+
return 0;
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn classify_0(x: Ptr<i32>) -> i32 {
10+
let x: Value<Ptr<i32>> = Rc::new(RefCell::new(x));
11+
if true {
12+
return ((*x.borrow()).read());
13+
} else {
14+
}
15+
return 1;
16+
}
17+
pub fn classify_1(x: i64) -> i32 {
18+
let x: Value<i64> = Rc::new(RefCell::new(x));
19+
if false {
20+
} else if true {
21+
return 2;
22+
}
23+
return 1;
24+
}
25+
pub fn classify_2(x: i32) -> i32 {
26+
let x: Value<i32> = Rc::new(RefCell::new(x));
27+
if false {
28+
} else if false {
29+
}
30+
return 1;
31+
}
32+
pub fn keep_both_3(x: i32) -> i32 {
33+
let x: Value<i32> = Rc::new(RefCell::new(x));
34+
if true {
35+
return ((*x.borrow()) + 1);
36+
} else {
37+
return ((*x.borrow()) - 1);
38+
}
39+
panic!("ub: non-void function does not return a value")
40+
}
41+
pub fn main() {
42+
__cpp2rust_init_globals();
43+
std::process::exit(main_0());
44+
}
45+
fn main_0() -> i32 {
46+
let v: Value<i32> = Rc::new(RefCell::new(7));
47+
assert!((({ classify_0((v.as_pointer()),) }) == 7));
48+
assert!((({ classify_1(1_i64,) }) == 2));
49+
assert!((({ classify_2(1,) }) == 1));
50+
assert!((({ keep_both_3(1,) }) == 2));
51+
return 0;
52+
}
53+
pub fn __cpp2rust_init_globals() {}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn classify_0(mut x: *mut i32) -> i32 {
10+
if true {
11+
return (*x);
12+
} else {
13+
}
14+
return 1;
15+
}
16+
pub unsafe fn classify_1(mut x: i64) -> i32 {
17+
if false {
18+
} else if true {
19+
return 2;
20+
}
21+
return 1;
22+
}
23+
pub unsafe fn classify_2(mut x: i32) -> i32 {
24+
if false {
25+
} else if false {
26+
}
27+
return 1;
28+
}
29+
pub unsafe fn keep_both_3(mut x: i32) -> i32 {
30+
if true {
31+
return ((x) + (1));
32+
} else {
33+
return ((x) - (1));
34+
}
35+
panic!("ub: non-void function does not return a value")
36+
}
37+
pub fn main() {
38+
unsafe {
39+
__cpp2rust_init_globals();
40+
std::process::exit(main_0() as i32);
41+
}
42+
}
43+
unsafe fn main_0() -> i32 {
44+
let mut v: i32 = 7;
45+
assert!(((unsafe { classify_0((&mut v as *mut i32),) }) == (7)));
46+
assert!(((unsafe { classify_1(1_i64,) }) == (2)));
47+
assert!(((unsafe { classify_2(1,) }) == (1)));
48+
assert!(((unsafe { keep_both_3(1,) }) == (2)));
49+
return 0;
50+
}
51+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)