Skip to content

Commit 393ee2f

Browse files
committed
Key rules through explicit and implicit casts
1 parent b168af5 commit 393ee2f

8 files changed

Lines changed: 106 additions & 1 deletion

File tree

‎cpp2rust/converter/rules/rules_loader.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,13 @@ std::string DeclKey(const clang::NamedDecl *decl) {
473473
}
474474

475475
std::string ExprKey(clang::ASTContext &ctx, const clang::Expr *expr) {
476-
expr = expr->IgnoreParenImpCasts();
476+
for (auto inner = expr->IgnoreParens()->IgnoreImplicit(); inner != expr;
477+
inner = expr->IgnoreParens()->IgnoreImplicit()) {
478+
expr = inner;
479+
}
480+
if (auto cast = llvm::dyn_cast<clang::ExplicitCastExpr>(expr)) {
481+
return ExprKey(ctx, cast->getSubExpr());
482+
}
477483
if (llvm::isa<clang::IntegerLiteral>(expr) &&
478484
expr->getBeginLoc().isMacroID()) {
479485
return clang::Lexer::getImmediateMacroName(

‎rules/chrono/src_cxx20.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <chrono>
5+
6+
using t1 = std::chrono::day;
7+
8+
std::chrono::day f1(unsigned a0) { return std::chrono::day(a0); }
9+
10+
unsigned f2(const std::chrono::day &a0) { return static_cast<unsigned>(a0); }

‎rules/chrono/tgt_unsafe.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
fn t1() -> u32 {
5+
0_u32
6+
}
7+
8+
unsafe fn f1(a0: u32) -> u32 {
9+
a0
10+
}
11+
12+
unsafe fn f2(a0: u32) -> u32 {
13+
a0
14+
}

‎rules/fs_path/src_cxx17.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <filesystem>
5+
#include <string>
6+
7+
using t1 = std::filesystem::path;
8+
9+
std::filesystem::path f1(const char *a0) { return std::filesystem::path(a0); }
10+
11+
std::string f2(const std::filesystem::path &a0) {
12+
return static_cast<std::string>(a0);
13+
}

‎rules/fs_path/tgt_refcount.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use libcc2rs::*;
5+
6+
fn t1() -> Vec<i8> {
7+
vec![0]
8+
}
9+
10+
fn f1(a0: Ptr<i8>) -> Vec<i8> {
11+
let mut __v = Vec::new();
12+
a0.with_c_str(|__s| __v.extend_from_slice(__s));
13+
__v.push(0);
14+
__v
15+
}
16+
17+
fn f2(a0: Vec<i8>) -> Vec<i8> {
18+
a0
19+
}

‎rules/fs_path/tgt_unsafe.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
fn t1() -> Vec<libc::c_char> {
5+
vec![0]
6+
}
7+
8+
unsafe fn f1(a0: *const libc::c_char) -> Vec<libc::c_char> {
9+
let mut __v = ::std::ffi::CStr::from_ptr(a0)
10+
.to_bytes()
11+
.iter()
12+
.map(|&__b| __b as libc::c_char)
13+
.collect::<Vec<libc::c_char>>();
14+
__v.push(0);
15+
__v
16+
}
17+
18+
unsafe fn f2(a0: Vec<libc::c_char>) -> Vec<libc::c_char> {
19+
a0
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
2+
#include <assert.h>
3+
#include <chrono>
4+
5+
int main() {
6+
unsigned n = 17;
7+
std::chrono::day d(n);
8+
assert(static_cast<unsigned>(d) == 17);
9+
return 0;
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++17
2+
#include <assert.h>
3+
#include <filesystem>
4+
#include <string>
5+
6+
int main() {
7+
const char *name = "dir/file.txt";
8+
std::filesystem::path p(name);
9+
std::string s = static_cast<std::string>(p);
10+
assert(s.size() == 12);
11+
assert(s == "dir/file.txt");
12+
return 0;
13+
}

0 commit comments

Comments
 (0)