Skip to content

Commit c8a00f1

Browse files
committed
format
1 parent 94f7ef7 commit c8a00f1

6 files changed

Lines changed: 44 additions & 50 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,31 +2281,32 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
22812281
return out;
22822282
}
22832283

2284-
std::string Converter::GetCodeUnitArrayLiteral(const clang::StringLiteral *expr,
2285-
uint64_t pad) {
2284+
bool Converter::IsArrayInitContext() const {
2285+
return !curr_init_type_.empty() && curr_init_type_.back()->isArrayType();
2286+
}
2287+
2288+
std::string
2289+
Converter::GetCodeUnitArrayLiteral(const clang::StringLiteral *expr) {
22862290
auto elem_type =
22872291
ToStringBase(ctx_.getAsArrayType(expr->getType())->getElementType());
2288-
std::string out = "[";
2289-
for (unsigned i = 0; i < expr->getLength(); ++i) {
2290-
out += std::format("{} as {}, ", expr->getCodeUnit(i), elem_type);
2292+
uint64_t len = expr->getLength();
2293+
uint64_t total = len + 1;
2294+
if (IsArrayInitContext()) {
2295+
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.back())) {
2296+
total = std::max(arr_ty->getSize().getZExtValue(), len);
2297+
}
22912298
}
2292-
for (uint64_t i = 0; i < pad; ++i) {
2293-
out += std::format("0 as {}, ", elem_type);
2299+
std::string out = "[";
2300+
for (uint64_t i = 0; i < total; ++i) {
2301+
out += std::format("{} as {}, ", i < len ? expr->getCodeUnit(i) : 0,
2302+
elem_type);
22942303
}
22952304
return out + ']';
22962305
}
22972306

22982307
bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
2299-
if (expr->getCharByteWidth() != 1 ||
2300-
expr->getKind() == clang::StringLiteralKind::UTF8) {
2301-
uint64_t pad = 1;
2302-
if (!curr_init_type_.empty() && curr_init_type_.back()->isArrayType()) {
2303-
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.back())) {
2304-
uint64_t arr_size = arr_ty->getSize().getZExtValue();
2305-
pad = arr_size > expr->getLength() ? arr_size - expr->getLength() : 0;
2306-
}
2307-
}
2308-
StrCat(GetCodeUnitArrayLiteral(expr, pad));
2308+
if (IsCodeUnitStringLiteral(expr)) {
2309+
StrCat(GetCodeUnitArrayLiteral(expr));
23092310
computed_expr_type_ = ComputedExprType::FreshValue;
23102311
return false;
23112312
}

‎cpp2rust/converter/converter.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
349349
virtual bool VisitCharacterLiteral(clang::CharacterLiteral *expr);
350350

351351
std::string GetEscapedCharLiteral(char character) const;
352-
std::string GetCodeUnitArrayLiteral(const clang::StringLiteral *expr,
353-
uint64_t pad);
352+
std::string GetCodeUnitArrayLiteral(const clang::StringLiteral *expr);
353+
bool IsArrayInitContext() const;
354354

355355
std::string GetEscapedUTF8CharLiteral(clang::Expr *expr) const;
356356

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ bool IsStringLiteralExpr(const clang::Expr *expr) {
149149
clang::isa<clang::PredefinedExpr>(stripped);
150150
}
151151

152+
bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr) {
153+
return expr->getCharByteWidth() != 1 ||
154+
expr->getKind() == clang::StringLiteralKind::UTF8;
155+
}
156+
152157
bool IsUserDefinedDecl(const clang::Decl *decl) {
153158
const auto &ctx = decl->getASTContext();
154159
const auto &src_mgr = ctx.getSourceManager();

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ bool IsUnionArrayMember(const clang::Expr *base);
4646

4747
bool IsStringLiteralExpr(const clang::Expr *expr);
4848

49+
bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr);
50+
4951
bool IsUserDefinedDecl(const clang::Decl *decl);
5052

5153
bool RefersToUserDefinedDecl(const clang::Expr *expr);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,6 @@ void ConverterRefCount::PendingDeref::set_unchecked(std::string str, bool fresh,
8686
type = ComputedExprType::Pending;
8787
}
8888

89-
std::string
90-
ConverterRefCount::GetStringLiteralCodeUnit(const clang::Expr *expr) {
91-
const auto *literal = clang::dyn_cast<clang::StringLiteral>(
92-
expr->IgnoreParens()->IgnoreImplicit());
93-
if (literal == nullptr ||
94-
(literal->getCharByteWidth() == 1 &&
95-
literal->getKind() != clang::StringLiteralKind::UTF8)) {
96-
return "u8";
97-
}
98-
return ToStringBase(
99-
ctx_.getAsArrayType(literal->getType())->getElementType());
100-
}
101-
10289
std::string ConverterRefCount::GetInnerType(clang::QualType type) {
10390
PushConversionKind push(*this, ConversionKind::Unboxed);
10491
auto str = ToString(type);
@@ -1192,23 +1179,15 @@ bool ConverterRefCount::VisitCallExpr(clang::CallExpr *expr) {
11921179
}
11931180

11941181
bool ConverterRefCount::VisitStringLiteral(clang::StringLiteral *expr) {
1195-
if (expr->getCharByteWidth() != 1 ||
1196-
expr->getKind() == clang::StringLiteralKind::UTF8) {
1197-
uint64_t pad = 1;
1198-
if (!curr_init_type_.empty() && curr_init_type_.back()->isArrayType()) {
1199-
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.back())) {
1200-
uint64_t arr_size = arr_ty->getSize().getZExtValue();
1201-
pad = arr_size > expr->getLength() ? arr_size - expr->getLength() : 0;
1202-
}
1203-
StrCat(std::format("Box::from({})", GetCodeUnitArrayLiteral(expr, pad)));
1204-
} else {
1205-
StrCat(std::format("&{}", GetCodeUnitArrayLiteral(expr, pad)));
1206-
}
1182+
if (IsCodeUnitStringLiteral(expr)) {
1183+
auto arr = GetCodeUnitArrayLiteral(expr);
1184+
StrCat(IsArrayInitContext() ? std::format("Box::from({})", arr)
1185+
: '&' + arr);
12071186
computed_expr_type_ = ComputedExprType::FreshValue;
12081187
return false;
12091188
}
12101189

1211-
if (!curr_init_type_.empty() && curr_init_type_.back()->isArrayType()) {
1190+
if (IsArrayInitContext()) {
12121191
uint64_t pad = 1;
12131192
if (auto *arr_ty = ctx_.getAsConstantArrayType(curr_init_type_.back())) {
12141193
uint64_t arr_size = arr_ty->getSize().getZExtValue();
@@ -1301,7 +1280,10 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
13011280
return false;
13021281
}
13031282
if (IsStringLiteralExpr(sub_expr)) {
1304-
auto code_unit = GetStringLiteralCodeUnit(sub_expr);
1283+
auto code_unit = ToStringBase(
1284+
ctx_.getAsArrayType(
1285+
sub_expr->IgnoreParens()->IgnoreImplicit()->getType())
1286+
->getElementType());
13051287
StrCat(std::format("Ptr::<{}>::from_string_literal({})", code_unit,
13061288
ToString(sub_expr->IgnoreParens())));
13071289
computed_expr_type_ = ComputedExprType::FreshPointer;
@@ -2174,7 +2156,10 @@ std::string ConverterRefCount::ConvertVarInitValue(clang::QualType qual_type,
21742156
}
21752157
if (qual_type.getNonReferenceType()->isArrayType()) {
21762158
if (IsStringLiteralExpr(expr)) {
2177-
auto code_unit = GetStringLiteralCodeUnit(expr);
2159+
auto code_unit = ToStringBase(
2160+
ctx_.getAsArrayType(
2161+
expr->IgnoreParens()->IgnoreImplicit()->getType())
2162+
->getElementType());
21782163
return std::format("Ptr::<Box<[{}]>>::from_string_literal_array({})",
21792164
code_unit,
21802165
ToString(expr->IgnoreParens()->IgnoreImplicit()));
@@ -2446,7 +2431,10 @@ void ConverterRefCount::ConvertArraySubscript(clang::Expr *base,
24462431
{
24472432
PushParen paren(*this, is_inner_boxed);
24482433
if (IsStringLiteralExpr(base)) {
2449-
auto code_unit = GetStringLiteralCodeUnit(base);
2434+
auto code_unit = ToStringBase(
2435+
ctx_.getAsArrayType(
2436+
base->IgnoreParens()->IgnoreImplicit()->getType())
2437+
->getElementType());
24502438
StrCat(std::format("Ptr::<{}>::from_string_literal({}).offset({})",
24512439
code_unit,
24522440
ToString(base->IgnoreParens()->IgnoreImplicit()),

‎cpp2rust/converter/models/converter_refcount.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,6 @@ class ConverterRefCount final : public Converter {
267267

268268
std::string GetInnerType(clang::QualType type);
269269

270-
std::string GetStringLiteralCodeUnit(const clang::Expr *expr);
271-
272270
std::string ConvertFreshLValue(clang::Expr *expr);
273271
std::string ConvertObject(clang::Expr *expr);
274272
std::string ConvertFreshObject(clang::Expr *expr) override;

0 commit comments

Comments
 (0)