Skip to content

Commit 7ffaf61

Browse files
authored
Treat u8 literals by their element type (#492)
Pre C++-20 u8 literals were `const char*`, post C++-20 they are `const char8_t*`
1 parent 4810320 commit 7ffaf61

4 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ bool IsStringLiteralExpr(const clang::Expr *expr) {
259259

260260
bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr) {
261261
return expr->getCharByteWidth() != 1 ||
262-
expr->getKind() == clang::StringLiteralKind::UTF8;
262+
expr->getType()->getArrayElementTypeNoTypeQual()->isChar8Type();
263263
}
264264

265265
bool IsUserDefinedDecl(const clang::Decl *decl) {

‎tests/unit/char_signed.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ int main() {
1919
printf("%d %d\n", lit[0], ulit[0]);
2020
printf("%d\n", is_negative(lit));
2121

22+
const char *u8s = u8"\u00e9";
23+
printf("%d %d\n", u8s[0], u8s[1]);
24+
2225
const char *p = "\x80";
2326
printf("%d %d\n", p[0], (unsigned char)p[0]);
2427

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ fn main_0() -> i32 {
3232
"{}",
3333
(({ is_negative_1((lit.as_pointer() as Ptr::<i8>),) }) as i32)
3434
);
35+
let mut u8s: Ptr<i8> = Ptr::<i8>::from_string_literal(b"\xc3\xa9");
36+
println!(
37+
"{} {}",
38+
((elem!(u8s, 0).read()) as i32),
39+
((elem!(u8s, 1).read()) as i32)
40+
);
3541
let mut p: Ptr<i8> = Ptr::<i8>::from_string_literal(b"\x80");
3642
println!(
3743
"{} {}",

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ unsafe fn main_0() -> i32 {
3838
c"%d\n".as_ptr() as *const i8,
3939
((unsafe { is_negative_1((lit.as_mut_ptr()).cast_const()) }) as i32),
4040
);
41+
let mut u8s: *const libc::c_char = c"\xc3\xa9".as_ptr();
42+
printf(
43+
c"%d %d\n".as_ptr() as *const i8,
44+
((*u8s.offset((0) as isize)) as i32),
45+
((*u8s.offset((1) as isize)) as i32),
46+
);
4147
let mut p: *const libc::c_char = c"\x80".as_ptr();
4248
printf(
4349
c"%d %d\n".as_ptr() as *const i8,

0 commit comments

Comments
 (0)