@@ -1685,7 +1685,7 @@ std::string Converter::GetEscapedUTF8CharLiteral(clang::Expr *expr) const {
16851685}
16861686
16871687std::string Converter::GetEscapedStringLiteral (clang::Expr *expr,
1688- bool add_null_char ) const {
1688+ uint64_t pad_nulls ) const {
16891689 auto str_expr = clang::dyn_cast<clang::StringLiteral>(expr->IgnoreCasts ());
16901690 assert (str_expr);
16911691 auto raw = str_expr->getString ();
@@ -1694,7 +1694,7 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
16941694 for (unsigned char c : raw) {
16951695 out += GetEscapedCharLiteral (static_cast <char >(c));
16961696 }
1697- if (add_null_char ) {
1697+ for ( uint64_t i = 0 ; i < pad_nulls; ++i ) {
16981698 out += " \\ 0" ;
16991699 }
17001700 out.push_back (' "' );
@@ -1703,12 +1703,17 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
17031703
17041704bool Converter::VisitStringLiteral (clang::StringLiteral *expr) {
17051705 if (!curr_init_type_.empty () && curr_init_type_.top ()->isArrayType ()) {
1706- // b"" has type &static [u8; N]. For translating char str[] =
1707- // "string_literal"; we need an initializer of type [u8; N]. Dereferencing
1708- // the &static [u8; N] achieves this.
17091706 StrCat (token::kStar );
1707+ if (auto *arr_ty = ctx_.getAsConstantArrayType (curr_init_type_.top ())) {
1708+ uint64_t target = arr_ty->getSize ().getZExtValue ();
1709+ uint64_t pad = target > expr->getString ().size ()
1710+ ? target - expr->getString ().size ()
1711+ : 0 ;
1712+ StrCat (std::format (" b{}" , GetEscapedStringLiteral (expr, pad)));
1713+ return false ;
1714+ }
17101715 }
1711- StrCat (std::format (" b{}" , GetEscapedStringLiteral (expr, true )));
1716+ StrCat (std::format (" b{}" , GetEscapedStringLiteral (expr, 1 )));
17121717 return false ;
17131718}
17141719
0 commit comments