@@ -1689,7 +1689,7 @@ std::string Converter::GetEscapedUTF8CharLiteral(clang::Expr *expr) const {
16891689}
16901690
16911691std::string Converter::GetEscapedStringLiteral (clang::Expr *expr,
1692- bool add_null_char ) const {
1692+ uint64_t pad_nulls ) const {
16931693 auto str_expr = clang::dyn_cast<clang::StringLiteral>(expr->IgnoreCasts ());
16941694 assert (str_expr);
16951695 auto raw = str_expr->getString ();
@@ -1698,7 +1698,7 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
16981698 for (unsigned char c : raw) {
16991699 out += GetEscapedCharLiteral (static_cast <char >(c));
17001700 }
1701- if (add_null_char ) {
1701+ for ( uint64_t i = 0 ; i < pad_nulls; ++i ) {
17021702 out += " \\ 0" ;
17031703 }
17041704 out.push_back (' "' );
@@ -1707,12 +1707,17 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
17071707
17081708bool Converter::VisitStringLiteral (clang::StringLiteral *expr) {
17091709 if (!curr_init_type_.empty () && curr_init_type_.top ()->isArrayType ()) {
1710- // b"" has type &static [u8; N]. For translating char str[] =
1711- // "string_literal"; we need an initializer of type [u8; N]. Dereferencing
1712- // the &static [u8; N] achieves this.
17131710 StrCat (token::kStar );
1711+ if (auto *arr_ty = ctx_.getAsConstantArrayType (curr_init_type_.top ())) {
1712+ uint64_t target = arr_ty->getSize ().getZExtValue ();
1713+ uint64_t pad = target > expr->getString ().size ()
1714+ ? target - expr->getString ().size ()
1715+ : 0 ;
1716+ StrCat (std::format (" b{}" , GetEscapedStringLiteral (expr, pad)));
1717+ return false ;
1718+ }
17141719 }
1715- StrCat (std::format (" b{}" , GetEscapedStringLiteral (expr, true )));
1720+ StrCat (std::format (" b{}" , GetEscapedStringLiteral (expr, 1 )));
17161721 return false ;
17171722}
17181723
0 commit comments