@@ -1700,8 +1700,10 @@ bool Converter::VisitFloatingLiteral(clang::FloatingLiteral *expr) {
17001700}
17011701
17021702bool Converter::VisitCharacterLiteral (clang::CharacterLiteral *expr) {
1703- std::string ch = GetEscapedCharLiteral (expr->getValue ());
1704- ch = " '" + std::move (ch) + " '" ;
1703+ auto uc = static_cast <unsigned char >(expr->getValue ());
1704+ std::string ch = uc > 0x7F
1705+ ? std::format (" '\\ u{{{:x}}}'" , uc)
1706+ : " '" + GetEscapedCharLiteral (expr->getValue ()) + " '" ;
17051707 {
17061708 PushParen paren (*this );
17071709 StrCat (ch, keyword::kAs , ToStringBase (expr->getType ()));
@@ -1710,7 +1712,8 @@ bool Converter::VisitCharacterLiteral(clang::CharacterLiteral *expr) {
17101712 return false ;
17111713}
17121714
1713- std::string Converter::GetEscapedCharLiteral (char character) const {
1715+ std::string Converter::GetEscapedCharLiteral (char character,
1716+ bool byte_string) const {
17141717 switch (character) {
17151718 case ' "' :
17161719 return " \\\" " ;
@@ -1728,7 +1731,7 @@ std::string Converter::GetEscapedCharLiteral(char character) const {
17281731 return " \\ 0" ;
17291732 }
17301733 auto uc = static_cast <unsigned char >(character);
1731- if (uc < 0x20 || uc >= 0x7F ) {
1734+ if (uc < 0x20 || uc == 0x7F || (byte_string && uc > 0x7F ) ) {
17321735 return std::format (" \\ x{:02x}" , uc);
17331736 }
17341737 return std::string (1 , character);
@@ -1747,14 +1750,15 @@ std::string Converter::GetEscapedUTF8CharLiteral(clang::Expr *expr) const {
17471750}
17481751
17491752std::string Converter::GetEscapedStringLiteral (clang::Expr *expr,
1750- uint64_t pad_nulls) const {
1753+ uint64_t pad_nulls,
1754+ bool byte_string) const {
17511755 auto str_expr = clang::dyn_cast<clang::StringLiteral>(expr->IgnoreCasts ());
17521756 assert (str_expr);
17531757 auto raw = str_expr->getString ();
17541758 std::string out;
17551759 out.push_back (' "' );
17561760 for (unsigned char c : raw) {
1757- out += GetEscapedCharLiteral (static_cast <char >(c));
1761+ out += GetEscapedCharLiteral (static_cast <char >(c), byte_string );
17581762 }
17591763 for (uint64_t i = 0 ; i < pad_nulls; ++i) {
17601764 out += " \\ 0" ;
@@ -1775,12 +1779,12 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
17751779 ? arr_size - expr->getString ().size ()
17761780 : 0 ;
17771781 StrCat (token::kStar ,
1778- std::format (" b{}" , GetEscapedStringLiteral (expr, pad)));
1782+ std::format (" b{}" , GetEscapedStringLiteral (expr, pad, true )));
17791783 return false ;
17801784 }
17811785 StrCat (token::kStar );
17821786 }
1783- StrCat (std::format (" b{}" , GetEscapedStringLiteral (expr, 1 )));
1787+ StrCat (std::format (" b{}" , GetEscapedStringLiteral (expr, 1 , true )));
17841788 return false ;
17851789}
17861790
0 commit comments