@@ -1106,160 +1106,6 @@ void ConverterRefCount::ConvertDeclRefValue(clang::Expr *expr,
11061106 SetValueFreshness (expr->getType ());
11071107}
11081108
1109- static std::vector<const char *> printf2fmt (std::string &format) {
1110- std::vector<const char *> types;
1111- std::string escaped;
1112- for (char c : format) {
1113- escaped += c;
1114- if (c == ' {' || c == ' }' ) {
1115- escaped += c;
1116- }
1117- }
1118- format = std::move (escaped);
1119- size_t pos = 0 ;
1120- while ((pos = format.find (' %' , pos)) != std::string::npos) {
1121- if (pos + 1 >= format.size ())
1122- break ;
1123-
1124- switch (auto c = format[pos + 1 ]) {
1125- case ' c' :
1126- types.emplace_back (" u8 as char" );
1127- format.replace (pos, 2 , " {}" );
1128- pos += 2 ;
1129- continue ;
1130- case ' d' :
1131- case ' i' :
1132- case ' s' :
1133- case ' u' :
1134- types.emplace_back ();
1135- format.replace (pos, 2 , " {}" );
1136- pos += 2 ;
1137- continue ;
1138- case ' p' :
1139- types.emplace_back ();
1140- format.replace (pos, 2 , " {:?}" );
1141- pos += 2 ;
1142- continue ;
1143- case ' %' :
1144- format.replace (pos, 2 , " %" );
1145- pos += 1 ;
1146- continue ;
1147- case ' l' :
1148- if (pos + 2 < format.size () &&
1149- (format[pos + 2 ] == ' d' || format[pos + 2 ] == ' u' )) {
1150- types.emplace_back ();
1151- format.replace (pos, 3 , " {}" );
1152- pos += 2 ;
1153- continue ;
1154- }
1155- if (pos + 3 < format.size () && format[pos + 2 ] == ' l' &&
1156- (format[pos + 3 ] == ' d' || format[pos + 3 ] == ' u' )) {
1157- types.emplace_back ();
1158- format.replace (pos, 4 , " {}" );
1159- pos += 2 ;
1160- continue ;
1161- }
1162- break ;
1163- case ' j' :
1164- case ' z' :
1165- if (pos + 2 < format.size () &&
1166- (format[pos + 2 ] == ' d' || format[pos + 2 ] == ' u' )) {
1167- types.emplace_back ();
1168- format.replace (pos, 3 , " {}" );
1169- pos += 2 ;
1170- continue ;
1171- }
1172- break ;
1173- case ' .' :
1174- if (pos + 3 < format.size () && format[pos + 2 ] == ' 0' ) {
1175- auto end = format.find_first_not_of (" 0123456789" , pos + 3 );
1176- if (end != std::string::npos && format[end] == ' f' ) {
1177- auto repl = " {:." + format.substr (pos + 3 , end - pos - 3 ) + ' }' ;
1178- format.replace (pos, end - pos + 1 , repl);
1179- pos += repl.size ();
1180- types.emplace_back ();
1181- continue ;
1182- }
1183- }
1184- break ;
1185- default :
1186- if (c >= ' 0' && c <= ' 9' ) {
1187- auto end = format.find_first_not_of (" 0123456789" , pos + 2 );
1188- if (end != std::string::npos) {
1189- auto repl = " {:" + format.substr (pos + 1 , end - pos - 1 );
1190- bool ok = true ;
1191- switch (c = format[end]) {
1192- case ' d' :
1193- break ;
1194- case ' x' :
1195- repl += c;
1196- break ;
1197- case ' z' :
1198- if (end + 1 < format.size () && format[end + 1 ] == ' u' ) {
1199- ++end;
1200- } else {
1201- ok = false ;
1202- }
1203- break ;
1204- default :
1205- ok = false ;
1206- break ;
1207- }
1208- if (ok) {
1209- repl += ' }' ;
1210- format.replace (pos, end - pos + 1 , repl);
1211- pos += repl.size ();
1212- types.emplace_back ();
1213- continue ;
1214- }
1215- }
1216- }
1217- }
1218- llvm::errs () << " Unknown printf format: " << format << ' \n ' ;
1219- assert (0 );
1220- }
1221- return types;
1222- }
1223-
1224- void ConverterRefCount::ConvertPrintf (clang::CallExpr *expr) {
1225- bool is_fprintf = IsCDecl (expr->getDirectCallee (), " fprintf" );
1226- auto format = GetEscapedStringLiteral (clang::cast<clang::StringLiteral>(
1227- expr->getArg (is_fprintf)->IgnoreImplicit ()));
1228- bool ends_newline = format.ends_with (" \\ n\" " );
1229-
1230- llvm::StringRef stream = " stdout" ;
1231- if (is_fprintf) {
1232- auto loc = expr->getArg (0 )->IgnoreParenImpCasts ()->getBeginLoc ();
1233- stream = loc.isMacroID ()
1234- ? clang::Lexer::getImmediateMacroName (
1235- loc, ctx_.getSourceManager (), ctx_.getLangOpts ())
1236- : " " ;
1237- }
1238- if (stream == " stdout" ) {
1239- StrCat (ends_newline ? " println!(" : " print!(" );
1240- } else if (stream == " stderr" ) {
1241- StrCat (ends_newline ? " eprintln!(" : " eprint!(" );
1242- } else {
1243- llvm::errs () << " Unknown fprintf fd:\n " ;
1244- expr->getArg (0 )->dump (llvm::errs (), ctx_);
1245- exit (1 );
1246- }
1247- if (ends_newline) {
1248- format.replace (format.size () - 3 , 2 , " " );
1249- }
1250- auto types = printf2fmt (format);
1251- StrCat (format);
1252-
1253- unsigned j = 0 ;
1254- for (unsigned i = is_fprintf + 1 , e = expr->getNumArgs (); i < e; ++i) {
1255- StrCat (token::kComma );
1256- Convert (expr->getArg (i));
1257- if (auto *type = types[j++])
1258- StrCat (keyword::kAs , type);
1259- }
1260- StrCat (' )' );
1261- }
1262-
12631109void ConverterRefCount::ConvertVAArgCall (clang::CallExpr *expr) {
12641110 if (IsBuiltinVaStart (expr)) {
12651111 StrCat (ToString (expr->getArg (0 )->IgnoreImpCasts ()), " = __args.decay()" );
@@ -1271,11 +1117,22 @@ void ConverterRefCount::ConvertVAArgCall(clang::CallExpr *expr) {
12711117std::optional<Converter::TempMaterializationCtx>
12721118ConverterRefCount::ConvertCallExpr (clang::CallExpr *expr) {
12731119 PushConversionKind push (*this , ConversionKind::Unboxed);
1120+ // With a literal format, printf and fprintf become macros, which read like
1121+ // print!.
12741122 bool is_fprintf = IsCDecl (expr->getDirectCallee (), " fprintf" );
12751123 if ((is_fprintf || IsCDecl (expr->getDirectCallee (), " printf" )) &&
12761124 clang::isa<clang::StringLiteral>(
12771125 expr->getArg (is_fprintf)->IgnoreImplicit ())) {
1278- ConvertPrintf (expr);
1126+ StrCat (is_fprintf ? " fprintf!(" : " printf!(" );
1127+ if (is_fprintf) {
1128+ StrCat (ConvertFreshPointer (expr->getArg (0 )), token::kComma );
1129+ }
1130+ StrCat (" b" + GetEscapedStringLiteral (expr->getArg (is_fprintf)));
1131+ for (unsigned i = is_fprintf + 1 , e = expr->getNumArgs (); i < e; ++i) {
1132+ StrCat (token::kComma );
1133+ ConvertVariadicArg (expr->getArg (i));
1134+ }
1135+ StrCat (' )' );
12791136 return std::nullopt ;
12801137 }
12811138 return Converter::ConvertCallExpr (expr);
@@ -2153,6 +2010,10 @@ std::string ConverterRefCount::ConvertStream(clang::Expr *expr) {
21532010 return ConvertPointer (expr);
21542011}
21552012
2013+ std::string ConverterRefCount::GetCStrBytes (std::string_view ptr) const {
2014+ return std::format (" &({}).to_c_u8_bytes()" , ptr);
2015+ }
2016+
21562017bool ConverterRefCount::VisitCXXConstructExpr (clang::CXXConstructExpr *expr) {
21572018 if (expr->isElidable ()) {
21582019 Convert (expr->getArg (0 ));
0 commit comments