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
7 changes: 5 additions & 2 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2533,8 +2533,11 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
}
if (expr->getString().contains('\0')) {
std::string out = "(&[";
for (unsigned char c : expr->getString()) {
out += getTypedLiteral(std::to_string(c).c_str(), CharRustType()) + ", ";
for (char c : expr->getString()) {
auto value = CharRustTypeIsSigned()
? std::to_string(static_cast<signed char>(c))
: std::to_string(static_cast<unsigned char>(c));
out += getTypedLiteral(value.c_str(), CharRustType()) + ", ";
}
out += getTypedLiteral("0", CharRustType()) + "])";
StrCat(out);
Expand Down
13 changes: 7 additions & 6 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,16 @@ llvm::APSInt GetIntegerLiteralValue(const clang::ASTContext &ctx,
const clang::QualType *type,
bool char_is_signed) {
auto value = expr->getValue();
auto target = type ? *type : expr->getType();
bool is_signed = false;
if (type && (*type)->isBuiltinType() && (*type)->isIntegerType() &&
!(*type)->isBooleanType()) {
value = value.zextOrTrunc(ctx.getIntWidth(*type));
if (target->isBuiltinType() && target->isIntegerType() &&
!target->isBooleanType()) {
value = value.zextOrTrunc(ctx.getIntWidth(target));
is_signed =
(*type)->isSpecificBuiltinType(clang::BuiltinType::Char_S) ||
(*type)->isSpecificBuiltinType(clang::BuiltinType::Char_U)
target->isSpecificBuiltinType(clang::BuiltinType::Char_S) ||
target->isSpecificBuiltinType(clang::BuiltinType::Char_U)
? char_is_signed
: (*type)->isSignedIntegerType();
: target->isSignedIntegerType();
}
return llvm::APSInt(value, !is_signed);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/out/refcount/size_t_template_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ impl RefCtor_unsigned_long_ {
}
}
}
pub fn neg_ll_1() -> i64 {
return -9000000_i64;
}
pub fn neg_char_2() -> i8 {
return (b'\x80' as i8);
}
pub fn neg_int_3() -> i32 {
return -1;
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -54,6 +63,9 @@ fn main_0() -> i32 {
let mut rc: RefCtor_unsigned_long_ =
RefCtor_unsigned_long_::new({ (v1.as_pointer()).reinterpret_cast::<u64>() });
assert!((rc.v == 7_u64));
assert!((({ neg_ll_1() }) == (-9000000_i32 as i64)));
assert!(((({ neg_char_2() }) as i32) == ((b'\x80' as i8) as i32)));
assert!((({ neg_int_3() }) == -1_i32));
return 0;
}
pub fn __cpp2rust_init_globals() {}
2 changes: 2 additions & 0 deletions tests/unit/out/refcount/string_literal_interior_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ fn main_0() -> i32 {
let mut b: i32 = ({ sum_bytes_0((*g_packet_1.with(Value::clone).borrow()).clone(), 2_u32) });
assert!((a == b));
assert!((a == 1));
let mut high: i32 = ({ sum_bytes_0(Ptr::<i8>::from_string_literal(b"\xc2\0A"), 3_u32) });
assert!((high == (194 + 65)));
let mut c: i32 = ((b"\r\n.\r\n"[(0) as usize] as i32) + (b"\r\n.\r\n"[(3) as usize] as i32));
assert!((c == ((('\r' as i8) as i32) + (('\r' as i8) as i32))));
let mut idx: i32 = 1;
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/out/unsafe/size_t_template_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ impl RefCtor_unsigned_long_ {
this
}
}
pub unsafe fn neg_ll_1() -> i64 {
return -9000000_i64;
}
pub unsafe fn neg_char_2() -> libc::c_char {
return (b'\x80' as libc::c_char);
}
pub unsafe fn neg_int_3() -> i32 {
return -1;
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -55,6 +64,9 @@ unsafe fn main_0() -> i32 {
let mut rc: RefCtor_unsigned_long_ =
RefCtor_unsigned_long_::new({ &*(&raw const v1).cast::<u64>() });
assert!(((rc.v) == (7_u64)));
assert!(((unsafe { neg_ll_1() }) == (-9000000_i32 as i64)));
assert!((((unsafe { neg_char_2() }) as i32) == ((b'\x80' as libc::c_char) as i32)));
assert!(((unsafe { neg_int_3() }) == (-1_i32)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
13 changes: 13 additions & 0 deletions tests/unit/out/unsafe/string_literal_interior_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ unsafe fn main_0() -> i32 {
});
assert!(((a) == (b)));
assert!(((a) == (1)));
let mut high: i32 = (unsafe {
sum_bytes_0(
(&[
(-62 as libc::c_char),
(0 as libc::c_char),
(65 as libc::c_char),
(0 as libc::c_char),
])
.as_ptr(),
3_u32,
)
});
assert!(((high) == ((194) + (65))));
let mut c: i32 = (((*c"\r\n.\r\n".as_ptr().offset((0) as isize)) as i32)
+ ((*c"\r\n.\r\n".as_ptr().offset((3) as isize)) as i32));
assert!(((c) == ((('\r' as libc::c_char) as i32) + (('\r' as libc::c_char) as i32))));
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/size_t_template_param.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ template <class T> struct RefCtor {
RefCtor(const T &x) : v(x + 1) {}
};

template <long long N> static long long neg_ll() { return N; }

template <char C> static char neg_char() { return C; }

template <int N> static int neg_int() { return N; }

int main() {
size_t a1[] = {1, 2, 3};
assert(array_ref(a1) == 3);
Expand All @@ -28,5 +34,9 @@ int main() {
size_t v1 = 6;
RefCtor<size_t> rc(v1);
assert(rc.v == 7);

assert(neg_ll<-9000000>() == -9000000);
assert(neg_char<'\x80'>() == '\x80');
assert(neg_int<-1>() == -1);
return 0;
}
3 changes: 3 additions & 0 deletions tests/unit/string_literal_interior_null.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ int main() {
assert(a == b);
assert(a == 1);

int high = sum_bytes("\xc2\x00\x41", 3);
assert(high == 0xc2 + 0x41);

int c = "\r\n.\r\n"[0] + "\r\n.\r\n"[3];
assert(c == '\r' + '\r');

Expand Down
Loading