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
3 changes: 2 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2741,7 +2741,8 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
case clang::Stmt::CXXReinterpretCastExprClass:
case clang::Stmt::CXXStaticCastExprClass:
case clang::Stmt::CStyleCastExprClass:
if (expr->getType() == sub_expr->getType()) {
if (expr->getType() == sub_expr->getType() ||
(expr->getCastKind() == clang::CK_NoOp && type->isRecordType())) {
return Convert(sub_expr);
}
if (type->isFunctionPointerType() ||
Expand Down
9 changes: 9 additions & 0 deletions cpp2rust/converter/models/boxed_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "converter/models/boxed_vars.h"

#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Basic/SourceManager.h>

#include <algorithm>
#include <unordered_map>
Expand Down Expand Up @@ -110,6 +111,14 @@ class AddressTakenVisitor

bool shouldVisitTemplateInstantiations() const { return true; }

bool TraverseDecl(clang::Decl *decl) {
if (decl && !llvm::isa<clang::TranslationUnitDecl>(decl) &&
ctx_.getSourceManager().isInSystemHeader(decl->getLocation())) {
return true;
}
return RecursiveASTVisitor::TraverseDecl(decl);
}

// Only the semantic form of an initializer list is converted, in which the
// values of variables are read through lvalue-to-rvalue conversions.
bool TraverseInitListExpr(clang::InitListExpr *expr,
Expand Down
4 changes: 4 additions & 0 deletions cpp2rust/converter/rules/rules_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ std::string DeclKey(const clang::NamedDecl *decl) {
}

std::string ExprKey(clang::ASTContext &ctx, const clang::Expr *expr) {
expr = RuleDecls::SkipImplicit(expr);
if (auto cast = llvm::dyn_cast<clang::ExplicitCastExpr>(expr)) {
return ExprKey(ctx, cast->getSubExpr());
}
expr = expr->IgnoreParenImpCasts();
if (llvm::isa<clang::IntegerLiteral, clang::AtomicExpr>(expr) &&
expr->getBeginLoc().isMacroID()) {
Expand Down
10 changes: 10 additions & 0 deletions rules/chrono/src_cxx20.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include <chrono>

using t1 = std::chrono::day;

std::chrono::day f1(unsigned a0) { return std::chrono::day(a0); }

unsigned f2(const std::chrono::day &a0) { return static_cast<unsigned>(a0); }
14 changes: 14 additions & 0 deletions rules/chrono/tgt_unsafe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

fn t1() -> u32 {
0_u32
}

unsafe fn f1(a0: u32) -> u32 {
a0
}

unsafe fn f2(a0: u32) -> u32 {
a0
}
13 changes: 13 additions & 0 deletions rules/fs_path/src_cxx17.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include <filesystem>
#include <string>

using t1 = std::filesystem::path;

std::filesystem::path f1(const char *a0) { return std::filesystem::path(a0); }

std::string f2(const std::filesystem::path &a0) {
return static_cast<std::string>(a0);
}
19 changes: 19 additions & 0 deletions rules/fs_path/tgt_refcount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use libcc2rs::*;

fn t1() -> Vec<i8> {
vec![0]
}

fn f1(a0: Ptr<i8>) -> Vec<i8> {
let mut __v = Vec::new();
a0.with_c_str(|__s| __v.extend_from_slice(__s));
__v.push(0);
__v
}

fn f2(a0: Vec<i8>) -> Vec<i8> {
a0
}
20 changes: 20 additions & 0 deletions rules/fs_path/tgt_unsafe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

fn t1() -> Vec<libc::c_char> {
vec![0]
}

unsafe fn f1(a0: *const libc::c_char) -> Vec<libc::c_char> {
let mut __v = ::std::ffi::CStr::from_ptr(a0)
.to_bytes()
.iter()
.map(|&__b| __b as libc::c_char)
.collect::<Vec<libc::c_char>>();
__v.push(0);
__v
}

unsafe fn f2(a0: Vec<libc::c_char>) -> Vec<libc::c_char> {
a0
}
32 changes: 32 additions & 0 deletions tests/unit/out/refcount/stl_conversion_operators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
let mut n: u32 = 17_u32;
let d: Value<u32> = Rc::new(RefCell::new(n));
assert!(((*d.borrow()) == 17_u32));
let mut name: Ptr<i8> = Ptr::<i8>::from_string_literal(b"dir/file.txt");
let p: Value<Vec<i8>> = Rc::new(RefCell::new({
let mut __v = Vec::new();
name.with_c_str(|__s| __v.extend_from_slice(__s));
__v.push(0);
__v
}));
let mut s: Vec<i8> = (*p.borrow()).clone();
assert!(((s.len() - 1) == 12_usize));
assert!(
Ptr::<i8>::from_string_literal(b"dir/file.txt")
.with_c_str(|__s| s[..s.len().saturating_sub(1)] == *__s)
);
return 0;
}
pub fn __cpp2rust_init_globals() {}
40 changes: 40 additions & 0 deletions tests/unit/out/unsafe/stl_conversion_operators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut n: u32 = 17_u32;
let mut d: u32 = n;
assert!(((d) == (17_u32)));
let mut name: *const libc::c_char = c"dir/file.txt".as_ptr();
let mut p: Vec<libc::c_char> = {
let mut __v = ::std::ffi::CStr::from_ptr(name)
.to_bytes()
.iter()
.map(|&__b| __b as libc::c_char)
.collect::<Vec<libc::c_char>>();
__v.push(0);
__v
};
let mut s: Vec<libc::c_char> = p;
assert!(((s.len() - 1) == (12_usize)));
assert!(
s == {
let s = c"dir/file.txt".as_ptr();
std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1)
.to_vec()
}
);
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
18 changes: 18 additions & 0 deletions tests/unit/stl_conversion_operators.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
#include <assert.h>
#include <chrono>
#include <filesystem>
#include <string>

int main() {
unsigned n = 17;
std::chrono::day d(n);
assert(static_cast<unsigned>(d) == 17);

const char *name = "dir/file.txt";
std::filesystem::path p(name);
std::string s = static_cast<std::string>(p);
assert(s.size() == 12);
assert(s == "dir/file.txt");
return 0;
}
Loading