Skip to content
Open
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
8 changes: 8 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,14 @@ void Converter::ConvertCondition(clang::Expr *cond) {
}

bool Converter::VisitIfStmt(clang::IfStmt *stmt) {
if (stmt->isConstexpr()) {
if (auto taken = stmt->getNondiscardedCase(ctx_)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, it would be good to keep all branches for documentation purposes. Constant folding here is not what a real project wants in general.

if (*taken) {
Convert(*taken);
}
return false;
}
}
StrCat(keyword::kIf);
ConvertCondition(stmt->getCond());
ConvertBody(stmt->getThen());
Expand Down
1 change: 1 addition & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
VisitUnaryExprOrTypeTraitExpr(clang::UnaryExprOrTypeTraitExpr *expr);

virtual bool VisitTypeTraitExpr(clang::TypeTraitExpr *expr);

virtual bool VisitSizeOfPackExpr(clang::SizeOfPackExpr *expr);

virtual bool VisitOffsetOfExpr(clang::OffsetOfExpr *expr);
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/if_constexpr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <cassert>
#include <type_traits>

template <typename T> int classify(T x) {
if constexpr (std::is_pointer_v<T>) {
return *x;
} else if constexpr (sizeof(T) > 4) {
return 2;
}
return 1;
}

int main() {
int v = 7;
assert(classify(&v) == 7);
assert(classify(1L) == 2);
assert(classify(1) == 1);
return 0;
}
36 changes: 36 additions & 0 deletions tests/unit/out/refcount/if_constexpr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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 classify_0(x: Ptr<i32>) -> i32 {
let x: Value<Ptr<i32>> = Rc::new(RefCell::new(x));
{
return ((*x.borrow()).read());
}
return 1;
}
pub fn classify_1(x: i64) -> i32 {
let x: Value<i64> = Rc::new(RefCell::new(x));
{
return 2;
}
return 1;
}
pub fn classify_2(x: i32) -> i32 {
let x: Value<i32> = Rc::new(RefCell::new(x));
return 1;
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let v: Value<i32> = Rc::new(RefCell::new(7));
assert!((({ classify_0((v.as_pointer()),) }) == 7));
assert!((({ classify_1(1_i64,) }) == 2));
assert!((({ classify_2(1,) }) == 1));
return 0;
}
35 changes: 35 additions & 0 deletions tests/unit/out/unsafe/if_constexpr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 unsafe fn classify_0(mut x: *mut i32) -> i32 {
{
return (*x);
}
return 1;
}
pub unsafe fn classify_1(mut x: i64) -> i32 {
{
return 2;
}
return 1;
}
pub unsafe fn classify_2(mut x: i32) -> i32 {
return 1;
}
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut v: i32 = 7;
assert!(((unsafe { classify_0((&mut v as *mut i32),) }) == (7)));
assert!(((unsafe { classify_1(1_i64,) }) == (2)));
assert!(((unsafe { classify_2(1,) }) == (1)));
return 0;
}
Loading