Skip to content

Commit b0df9d2

Browse files
committed
Drop the byte_string flag
1 parent 1040e1c commit b0df9d2

9 files changed

Lines changed: 99 additions & 27 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,11 @@ bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
12581258
std::string &fmt_args, const char *&fmt_trait,
12591259
std::string &fmt_width) {
12601260
std::string arg_str = Mapper::ToString(arg);
1261-
if (clang::isa<clang::StringLiteral>(arg->IgnoreImplicit())) {
1261+
if (auto *str_lit =
1262+
clang::dyn_cast<clang::StringLiteral>(arg->IgnoreImplicit())) {
1263+
if (!IsAsciiStringLiteral(str_lit)) {
1264+
return false;
1265+
}
12621266
auto str = GetEscapedStringLiteral(arg);
12631267
std::string_view trim(str);
12641268
// Delete " from string
@@ -1299,6 +1303,8 @@ bool Converter::GetRawArg(clang::Expr *arg, std::string &raw_args) {
12991303
raw_args += "(&(" + str + ")[..(" + str + ").len() - 1]";
13001304
} else if (Mapper::ToString(arg).contains("std::endl")) {
13011305
raw_args += "(&[b'\\n']";
1306+
} else if (clang::isa<clang::StringLiteral>(arg->IgnoreImplicit())) {
1307+
raw_args += "(b" + GetEscapedStringLiteral(arg);
13021308
} else {
13031309
return false;
13041310
}
@@ -1712,8 +1718,7 @@ bool Converter::VisitCharacterLiteral(clang::CharacterLiteral *expr) {
17121718
return false;
17131719
}
17141720

1715-
std::string Converter::GetEscapedCharLiteral(char character,
1716-
bool byte_string) const {
1721+
std::string Converter::GetEscapedCharLiteral(char character) const {
17171722
switch (character) {
17181723
case '"':
17191724
return "\\\"";
@@ -1731,7 +1736,7 @@ std::string Converter::GetEscapedCharLiteral(char character,
17311736
return "\\0";
17321737
}
17331738
auto uc = static_cast<unsigned char>(character);
1734-
if (uc < 0x20 || uc == 0x7F || (byte_string && uc > 0x7F)) {
1739+
if (uc < 0x20 || uc >= 0x7F) {
17351740
return std::format("\\x{:02x}", uc);
17361741
}
17371742
return std::string(1, character);
@@ -1750,15 +1755,14 @@ std::string Converter::GetEscapedUTF8CharLiteral(clang::Expr *expr) const {
17501755
}
17511756

17521757
std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
1753-
uint64_t pad_nulls,
1754-
bool byte_string) const {
1758+
uint64_t pad_nulls) const {
17551759
auto str_expr = clang::dyn_cast<clang::StringLiteral>(expr->IgnoreCasts());
17561760
assert(str_expr);
17571761
auto raw = str_expr->getString();
17581762
std::string out;
17591763
out.push_back('"');
17601764
for (unsigned char c : raw) {
1761-
out += GetEscapedCharLiteral(static_cast<char>(c), byte_string);
1765+
out += GetEscapedCharLiteral(static_cast<char>(c));
17621766
}
17631767
for (uint64_t i = 0; i < pad_nulls; ++i) {
17641768
out += "\\0";
@@ -1779,12 +1783,12 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
17791783
? arr_size - expr->getString().size()
17801784
: 0;
17811785
StrCat(token::kStar,
1782-
std::format("b{}", GetEscapedStringLiteral(expr, pad, true)));
1786+
std::format("b{}", GetEscapedStringLiteral(expr, pad)));
17831787
return false;
17841788
}
17851789
StrCat(token::kStar);
17861790
}
1787-
StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 1, true)));
1791+
StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 1)));
17881792
return false;
17891793
}
17901794

cpp2rust/converter/converter.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,12 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
240240

241241
virtual bool VisitCharacterLiteral(clang::CharacterLiteral *expr);
242242

243-
std::string GetEscapedCharLiteral(char character,
244-
bool byte_string = false) const;
243+
std::string GetEscapedCharLiteral(char character) const;
245244

246245
std::string GetEscapedUTF8CharLiteral(clang::Expr *expr) const;
247246

248-
std::string GetEscapedStringLiteral(clang::Expr *expr, uint64_t pad_nulls = 0,
249-
bool byte_string = false) const;
247+
std::string GetEscapedStringLiteral(clang::Expr *expr,
248+
uint64_t pad_nulls = 0) const;
250249
virtual bool VisitStringLiteral(clang::StringLiteral *expr);
251250

252251
virtual bool VisitCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr *expr);

cpp2rust/converter/converter_lib.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ bool IsCallToOstream(clang::CallExpr *expr) {
246246
return false;
247247
}
248248

249+
bool IsAsciiStringLiteral(const clang::StringLiteral *str) {
250+
for (unsigned char c : str->getString()) {
251+
if (c > 0x7F) {
252+
return false;
253+
}
254+
}
255+
return true;
256+
}
257+
249258
std::vector<clang::CXXConstructorDecl *>
250259
GetTemplateInstantiatedCtors(clang::CXXRecordDecl *decl) {
251260
std::vector<clang::CXXConstructorDecl *> out;

cpp2rust/converter/converter_lib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ bool IsUniquePtr(clang::QualType type);
6565

6666
bool IsCallToOstream(clang::CallExpr *expr);
6767

68+
bool IsAsciiStringLiteral(const clang::StringLiteral *str);
69+
6870
std::vector<clang::CXXConstructorDecl *>
6971
GetTemplateInstantiatedCtors(clang::CXXRecordDecl *decl);
7072

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,10 +1023,10 @@ bool ConverterRefCount::VisitStringLiteral(clang::StringLiteral *expr) {
10231023
: 0;
10241024
}
10251025
StrCat(std::format("Box::<[u8]>::from(b{}.as_slice())",
1026-
GetEscapedStringLiteral(expr, pad, true)));
1026+
GetEscapedStringLiteral(expr, pad)));
10271027
return false;
10281028
}
1029-
StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 0, true)));
1029+
StrCat(std::format("b{}", GetEscapedStringLiteral(expr, 0)));
10301030
return false;
10311031
}
10321032

tests/unit/out/refcount/char_printing.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ fn main_0() -> i32 {
2929
]
3030
.concat()),
3131
);
32-
write!(
33-
libcc2rs::cout(),
34-
"0x{:x} açordas?\nSim, 0x{:x}.\n",
35-
27,
36-
(*i.borrow()),
32+
write!(libcc2rs::cout(), "0x{:x}", 27,);
33+
libcc2rs::cout().write_all(
34+
&([
35+
(b" a\xc3\xa7ordas?" as &[u8]),
36+
(&[('\n' as u8)] as &[u8]),
37+
(b"Sim, 0x" as &[u8]),
38+
]
39+
.concat()),
3740
);
41+
write!(libcc2rs::cout(), "{:x}.\n", (*i.borrow()),);
3842
write!(libcc2rs::cout(), "Hello, World!\n",);
3943
libcc2rs::cout().write_all(
4044
&([

tests/unit/out/refcount/char_printing_cerr.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ fn main_0() -> i32 {
2929
]
3030
.concat()),
3131
);
32-
write!(
33-
libcc2rs::cerr(),
34-
"0x{:x} açordas?\nSim, 0x{:x}.\n",
35-
27,
36-
(*i.borrow()),
32+
write!(libcc2rs::cerr(), "0x{:x}", 27,);
33+
libcc2rs::cerr().write_all(
34+
&([
35+
(b" a\xc3\xa7ordas?" as &[u8]),
36+
(&[('\n' as u8)] as &[u8]),
37+
(b"Sim, 0x" as &[u8]),
38+
]
39+
.concat()),
3740
);
41+
write!(libcc2rs::cerr(), "{:x}.\n", (*i.borrow()),);
3842
write!(libcc2rs::cerr(), "Hello, World!\n",);
3943
libcc2rs::cerr().write_all(
4044
&([

tests/unit/out/unsafe/char_printing.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,33 @@ unsafe fn main_0() -> i32 {
5454
.unwrap()
5555
.into_raw_fd(),
5656
),
57-
"0x{:x} açordas?\nSim, 0x{:x}.\n",
57+
"0x{:x}",
5858
27,
59+
);
60+
std::fs::File::from_raw_fd(
61+
std::io::stdout()
62+
.as_fd()
63+
.try_clone_to_owned()
64+
.unwrap()
65+
.into_raw_fd(),
66+
)
67+
.write_all(
68+
&([
69+
(b" a\xc3\xa7ordas?" as &[u8]),
70+
(&[('\n' as u8)] as &[u8]),
71+
(b"Sim, 0x" as &[u8]),
72+
]
73+
.concat()),
74+
);
75+
write!(
76+
std::fs::File::from_raw_fd(
77+
std::io::stdout()
78+
.as_fd()
79+
.try_clone_to_owned()
80+
.unwrap()
81+
.into_raw_fd(),
82+
),
83+
"{:x}.\n",
5984
i,
6085
);
6186
write!(

tests/unit/out/unsafe/char_printing_cerr.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,33 @@ unsafe fn main_0() -> i32 {
5454
.unwrap()
5555
.into_raw_fd(),
5656
),
57-
"0x{:x} açordas?\nSim, 0x{:x}.\n",
57+
"0x{:x}",
5858
27,
59+
);
60+
std::fs::File::from_raw_fd(
61+
std::io::stderr()
62+
.as_fd()
63+
.try_clone_to_owned()
64+
.unwrap()
65+
.into_raw_fd(),
66+
)
67+
.write_all(
68+
&([
69+
(b" a\xc3\xa7ordas?" as &[u8]),
70+
(&[('\n' as u8)] as &[u8]),
71+
(b"Sim, 0x" as &[u8]),
72+
]
73+
.concat()),
74+
);
75+
write!(
76+
std::fs::File::from_raw_fd(
77+
std::io::stderr()
78+
.as_fd()
79+
.try_clone_to_owned()
80+
.unwrap()
81+
.into_raw_fd(),
82+
),
83+
"{:x}.\n",
5984
i,
6085
);
6186
write!(

0 commit comments

Comments
 (0)