Skip to content

Commit 4d3e71f

Browse files
committed
Fix explict cast around operands
1 parent cc5788d commit 4d3e71f

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎cpp2rust/converter/rules/expr_matcher.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ clang::Expr *getObject(clang::ASTContext &ctx, clang::Expr *object) {
119119
std::vector<clang::Expr *> getOperands(clang::ASTContext &ctx,
120120
const clang::Expr *use) {
121121
auto expr = const_cast<clang::Expr *>(RuleDecls::SkipImplicit(use));
122+
while (auto cast = llvm::dyn_cast<clang::ExplicitCastExpr>(expr)) {
123+
expr =
124+
const_cast<clang::Expr *>(RuleDecls::SkipImplicit(cast->getSubExpr()));
125+
}
122126
std::vector<clang::Expr *> operands;
123127
if (auto rewritten =
124128
llvm::dyn_cast<clang::CXXRewrittenBinaryOperator>(expr)) {

‎tests/unit/out/refcount/string2.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ fn main_0() -> i32 {
2323
Ptr::<i8>::from_string_literal(b"fbo")
2424
.with_c_str(|__s| (*arr.borrow())[..(*arr.borrow()).len().saturating_sub(1)] == *__s)
2525
);
26+
let mut names: [Ptr<i8>; 1] = [(arr.as_pointer() as Ptr<i8>)];
27+
let mut copy: Vec<i8> = {
28+
let mut __bytes = names[(0) as usize].to_c_bytes();
29+
__bytes.push(0);
30+
__bytes
31+
};
32+
assert!(
33+
Ptr::<i8>::from_string_literal(b"fbo")
34+
.with_c_str(|__s| copy[..copy.len().saturating_sub(1)] == *__s)
35+
);
2636
return 0;
2737
}
2838
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/string2.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ unsafe fn main_0() -> i32 {
2727
.to_vec()
2828
}
2929
);
30+
let mut names: [*mut libc::c_char; 1] = [arr.as_mut_ptr()];
31+
let copy: Vec<libc::c_char> = {
32+
let s = (names[(0) as usize]).cast_const();
33+
std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1).to_vec()
34+
};
35+
assert!(
36+
copy == {
37+
let s = c"fbo".as_ptr();
38+
std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1)
39+
.to_vec()
40+
}
41+
);
3042
return 0;
3143
}
3244
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/string2.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ int main() {
77
const char *p = arr.c_str() + 1;
88
assert(*p == 'b');
99
assert(arr == "fbo");
10+
char *names[] = {arr.data()};
11+
const std::string copy = std::string(names[0]);
12+
assert(copy == "fbo");
1013
return 0;
1114
}

0 commit comments

Comments
 (0)