Skip to content

Commit b1705c3

Browse files
committed
Merge branch 'master' into void-round-trip
2 parents 93681bd + 963701d commit b1705c3

102 files changed

Lines changed: 1882 additions & 596 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ bool TypeImplementsByteRepr(clang::QualType qt) {
204204
if (qt->isIntegerType() || qt->isFloatingType() || qt->isEnumeralType()) {
205205
return true;
206206
}
207+
if (qt->isPointerType()) {
208+
return true;
209+
}
207210
if (const auto *arr = qt->getAsArrayTypeUnsafe()) {
208211
return TypeImplementsByteRepr(arr->getElementType());
209212
}

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,10 +503,13 @@ void ConverterRefCount::EmitRustUnion(clang::RecordDecl *decl) {
503503
{
504504
PushBrace impl_brace(*this);
505505
for (auto *field : decl->fields()) {
506+
PushConversionKind push(*this, ConversionKind::FullRefCount);
507+
std::string storage_ty = ToString(field->getType());
508+
Unwrap(storage_ty, "Value<", ">");
506509
StrCat(std::format(
507510
"pub fn {}(&self) -> Ptr<{}> {{ (self.__bytes.as_pointer() "
508511
"as Ptr<u8>).reinterpret_cast() }}",
509-
GetNamedDeclAsString(field), Mapper::Map(field->getType())));
512+
GetNamedDeclAsString(field), storage_ty));
510513
}
511514
}
512515

@@ -1310,9 +1313,9 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
13101313
} else if (expr->getSubExpr()->getType()->isPointerType() &&
13111314
!expr->getSubExpr()->isNullPointerConstant(
13121315
ctx_, clang::Expr::NPC_ValueDependentIsNull)) {
1313-
StrCat(std::format("({}.to_strong().as_pointer() as {})",
1316+
StrCat(std::format("{}.reinterpret_cast::<{}>()",
13141317
ToString(expr->getSubExpr()),
1315-
ToString(expr->getType())));
1318+
ConvertPointeeType(expr->getType())));
13161319
return false;
13171320
}
13181321
return Converter::VisitExplicitCastExpr(expr);
@@ -1499,6 +1502,7 @@ bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) {
14991502
}
15001503

15011504
void ConverterRefCount::ConvertUnionMemberAccessor(clang::MemberExpr *expr) {
1505+
auto member = expr->getMemberDecl();
15021506
std::string str;
15031507
{
15041508
Buffer buf(*this);
@@ -1509,15 +1513,24 @@ void ConverterRefCount::ConvertUnionMemberAccessor(clang::MemberExpr *expr) {
15091513
str += "()";
15101514

15111515
if (isAddrOf()) {
1512-
StrCat(str);
1516+
if (member->getType()->isArrayType()) {
1517+
PushConversionKind push(*this, ConversionKind::Unboxed);
1518+
StrCat(std::format(
1519+
"{}.reinterpret_cast::<{}>()", str,
1520+
ToString(
1521+
member->getType()->getAsArrayTypeUnsafe()->getElementType())));
1522+
} else {
1523+
StrCat(str);
1524+
}
15131525
computed_expr_type_ = ComputedExprType::Pointer;
15141526
return;
15151527
}
1528+
15161529
if (isLValue()) {
15171530
pending_deref_.set(str);
15181531
return;
15191532
}
1520-
StrCat(DerefPtrExpr(str, expr->getMemberDecl()->getType()));
1533+
StrCat(DerefPtrExpr(str, member->getType()));
15211534
}
15221535

15231536
bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) {
@@ -2071,7 +2084,7 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr(
20712084
StrCat(
20722085
std::format("{}.as_ref().unwrap()", ConvertRValue(expr->getArg(0))));
20732086
if (isAddrOf()) {
2074-
StrCat(std::format(".as_pointer().offset(({}) as isize)",
2087+
StrCat(std::format(".as_pointer().offset(({}))",
20752088
ConvertRValue(expr->getArg(1))));
20762089
} else {
20772090
if (isRValue()) {
@@ -2091,7 +2104,7 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr(
20912104

20922105
if (isLValue()) {
20932106
PushConversionKind push_ck(*this, ConversionKind::Unboxed);
2094-
pending_deref_.set(std::format("({} as {}).offset({} as isize)",
2107+
pending_deref_.set(std::format("({} as {}).offset({})",
20952108
ConvertObject(expr->getArg(0)),
20962109
ConvertPtrType(expr->getArg(0)->getType()),
20972110
ConvertRValue(expr->getArg(1))),
@@ -2111,7 +2124,7 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr(
21112124
}
21122125

21132126
PushConversionKind push(*this, ConversionKind::Unboxed);
2114-
StrCat(std::format("({} as {}).offset({} as isize)",
2127+
StrCat(std::format("({} as {}).offset({})",
21152128
ConvertObject(expr->getArg(0)),
21162129
ConvertPtrType(expr->getArg(0)->getType()),
21172130
ConvertRValue(expr->getArg(1))));
@@ -2162,7 +2175,7 @@ void ConverterRefCount::ConvertArraySubscript(clang::Expr *base,
21622175

21632176
{
21642177
PushParen paren(*this, is_inner_boxed);
2165-
StrCat(std::format("({} as {}).offset({} as isize)",
2178+
StrCat(std::format("({} as {}).offset({})",
21662179
ToString(base->IgnoreImplicit()),
21672180
ConvertPtrType(base->IgnoreImplicit()->getType()),
21682181
ConvertRValue(idx)));

‎libcc2rs/src/rc.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ impl<T> Ptr<T> {
296296
}
297297

298298
#[inline]
299-
pub fn offset(&self, offset: isize) -> Self {
299+
pub fn offset(&self, offset: impl TryInto<isize>) -> Self {
300+
let offset = offset
301+
.try_into()
302+
.ok()
303+
.expect("the offset must fit in a isize");
300304
let step = self.elem_step();
301305
Self {
302306
kind: self.kind.clone(),
@@ -1205,6 +1209,7 @@ impl<T: ?Sized> AsPointerDyn<T> for Rc<RefCell<T>> {
12051209
}
12061210

12071211
impl<T: 'static> ByteRepr for Ptr<T> {}
1212+
impl ByteRepr for AnyPtr {}
12081213

12091214
#[cfg(test)]
12101215
mod tests {

‎tests/lit/lit/formats/Cpp2RustTest.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
PTR_RE = re.compile(r"0x[0-9a-fA-F]+")
1919

2020
RE_XFAIL = re.compile(r"//\s*XFAIL:\s*(.*)")
21+
RE_PANIC_UB = re.compile(r"//\s*panic-ub\s*(?::\s*(.*))?$", re.MULTILINE)
2122
RE_PANIC = re.compile(r"//\s*panic\s*(?::\s*(.*))?$", re.MULTILINE)
2223
RE_NOCOMPILE = re.compile(r"//\s*no-compile\s*(?::\s*(.*))?$", re.MULTILINE)
2324
RE_TRANS_FAIL = re.compile(r"//\s*translation-fail\s*(?::\s*(.*))?$", re.MULTILINE)
@@ -27,6 +28,7 @@
2728
@dataclass
2829
class TestExpectations:
2930
should_panic: bool = False
31+
should_panic_ub: bool = False
3032
should_not_compile: bool = False
3133
should_not_translate: bool = False
3234
is_nondet_result: bool = False
@@ -47,6 +49,7 @@ def matches(match):
4749
xfail = xfail_m is not None and model in re.split(r"\s*,\s*", xfail_m.group(1))
4850
return cls(
4951
should_panic=matches(RE_PANIC.search(text)),
52+
should_panic_ub=matches(RE_PANIC_UB.search(text)),
5053
should_not_compile=matches(RE_NOCOMPILE.search(text)),
5154
should_not_translate=matches(RE_TRANS_FAIL.search(text)),
5255
is_nondet_result=matches(RE_NONDET.search(text)),
@@ -254,14 +257,22 @@ def run_rust(self):
254257
return None
255258
self.rust_result = RunResult(*lit.util.executeCommand(str(self.rust_bin)))
256259

257-
if exp.should_panic:
260+
if exp.should_panic_ub:
258261
err = str(self.rust_result.stderr)
259262
if not re.search(
260263
r"thread 'main' \(\d+\) panicked at", err
261264
) or self.rust_result.returncode not in [-6, 101]:
262265
return (exp.fail_code, "expected panic\n" + err)
263266
return self.success_result()
264267

268+
if exp.should_panic:
269+
err = str(self.rust_result.stderr)
270+
if not re.search(
271+
r"thread 'main' \(\d+\) panicked at", err
272+
) or self.rust_result.returncode not in [-6, 101]:
273+
return (exp.fail_code, "expected panic\n" + err)
274+
return (lit.Test.XFAIL, "")
275+
265276
if exp.is_nondet_result:
266277
return self.success_result()
267278
return None

‎tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,21 @@ pub struct container {
1111
pub p: Value<Ptr<opaque>>,
1212
pub x: Value<i32>,
1313
}
14-
impl ByteRepr for container {}
14+
impl ByteRepr for container {
15+
fn byte_size() -> usize {
16+
16
17+
}
18+
fn to_bytes(&self, buf: &mut [u8]) {
19+
(*self.p.borrow()).to_bytes(&mut buf[0..8]);
20+
(*self.x.borrow()).to_bytes(&mut buf[8..12]);
21+
}
22+
fn from_bytes(buf: &[u8]) -> Self {
23+
Self {
24+
p: Rc::new(RefCell::new(<Ptr<opaque>>::from_bytes(&buf[0..8]))),
25+
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[8..12]))),
26+
}
27+
}
28+
}
1529
pub fn main() {
1630
std::process::exit(main_0());
1731
}

‎tests/ub/dangling-prvalue-as-lvalue.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
// panic: refcount
4+
// panic-ub: refcount
55

66
#include <vector>
77

‎tests/ub/enum_out_of_range_cast.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// panic
1+
// panic-ub
22

33
enum Color { RED, GREEN, BLUE };
44

‎tests/ub/enum_out_of_range_increment.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// panic
1+
// panic-ub
22

33
enum color { RED, GREEN, BLUE };
44

‎tests/ub/max.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

44
// ub: unsafe
5-
// panic: refcount
5+
// panic-ub: refcount
66

77
#include <algorithm>
88

‎tests/ub/out/refcount/ub7.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ fn main_0() -> i32 {
2626
('n' as u8),
2727
('g' as u8),
2828
])));
29-
return (({ strlen_0(((s.as_pointer() as Ptr<u8>).offset(0 as isize))) }) as i32);
29+
return (({ strlen_0(((s.as_pointer() as Ptr<u8>).offset(0))) }) as i32);
3030
}

0 commit comments

Comments
 (0)