1655 lines · cpp
1// RUN: %check_clang_tidy -check-suffixes=,STRICT \2// RUN: -std=c++23-or-later %s modernize-use-std-print %t -- \3// RUN: -config="{CheckOptions: {modernize-use-std-print.StrictMode: true}}" \4// RUN: -- -isystem %clang_tidy_headers -fexceptions \5// RUN: -DPRI_CMDLINE_MACRO="\"s\"" \6// RUN: -D__PRI_CMDLINE_MACRO="\"s\""7// RUN: %check_clang_tidy -check-suffixes=,NOTSTRICT \8// RUN: -std=c++23-or-later %s modernize-use-std-print %t -- \9// RUN: -config="{CheckOptions: {modernize-use-std-print.StrictMode: false}}" \10// RUN: -- -isystem %clang_tidy_headers -fexceptions \11// RUN: -DPRI_CMDLINE_MACRO="\"s\"" \12// RUN: -D__PRI_CMDLINE_MACRO="\"s\""13#include <cstddef>14#include <cstdint>15#include <cstdio>16// CHECK-FIXES: #include <print>17#include <inttypes.h>18#include <string.h>19#include <string>20 21template <typename T>22struct iterator {23 T *operator->();24 T &operator*();25};26 27void printf_simple() {28 printf("Hello");29 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]30 // CHECK-FIXES: std::print("Hello");31}32 33void printf_newline() {34 printf("Hello\n");35 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]36 // CHECK-FIXES: std::println("Hello");37 38 printf("Split" "\n");39 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]40 // CHECK-FIXES: std::println("Split");41 42 printf("Double\n\n");43 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]44 // CHECK-FIXES: std::println("Double\n");45}46 47void printf_deceptive_newline() {48 printf("Hello\\n");49 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]50 // CHECK-FIXES: std::print("Hello\\n");51 52 printf("Hello\x0a");53 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]54 // CHECK-FIXES: std::println("Hello");55}56 57void printf_utf8_text() {58 printf("你好世界\n");59 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]60 // CHECK-FIXES: std::println("你好世界");61}62 63void printf_crlf_newline() {64 printf("Hello\r\n");65 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]66 // CHECK-FIXES: std::print("Hello\r\n");67 68 printf("Hello\r\\n");69 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]70 // CHECK-FIXES: std::print("Hello\r\\n");71}72 73// std::print returns nothing, so any callers that use the return74// value cannot be automatically translated.75int printf_uses_return_value(int choice) {76 const int i = printf("Return value assigned to variable %d\n", 42);77 78 extern void accepts_int(int);79 accepts_int(printf("Return value passed to function %d\n", 42));80 81 if (choice == 0)82 printf("if body %d\n", i);83 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]84 // CHECK-FIXES: std::println("if body {}", i);85 else if (choice == 1)86 printf("else if body %d\n", i);87 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]88 // CHECK-FIXES: std::println("else if body {}", i);89 else90 printf("else body %d\n", i);91 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]92 // CHECK-FIXES: std::println("else body {}", i);93 94 if (printf("Return value used as boolean in if statement"))95 if (printf("Return value used in expression if statement") == 44)96 if (const int j = printf("Return value used in assignment in if statement"))97 if (const int k = printf("Return value used with initializer in if statement"); k == 44)98 ;99 100 int d = 0;101 while (printf("%d", d) < 2)102 ++d;103 104 while (true)105 printf("while body %d\n", i);106 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]107 // CHECK-FIXES: std::println("while body {}", i);108 109 do110 printf("do body %d\n", i);111 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]112 // CHECK-FIXES: std::println("do body {}", i);113 while (true);114 115 for (;;)116 printf("for body %d\n", i);117 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]118 // CHECK-FIXES: std::println("for body {}", i);119 120 for (printf("for init statement %d\n", i);;)121 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]122 // CHECK-FIXES: for (std::println("for init statement {}", i);;)123 ;;124 125 for (int j = printf("for init statement %d\n", i);;)126 ;;127 128 for (; printf("for condition %d\n", i);)129 ;;130 131 for (;; printf("for expression %d\n", i))132 // CHECK-MESSAGES: [[@LINE-1]]:11: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]133 // CHECK-FIXES: for (;; std::println("for expression {}", i))134 ;;135 136 for (auto C : "foo")137 printf("ranged-for body %d\n", i);138 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]139 // CHECK-FIXES: std::println("ranged-for body {}", i);140 141 switch (1) {142 case 1:143 printf("switch case body %d\n", i);144 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]145 // CHECK-FIXES: std::println("switch case body {}", i);146 break;147 default:148 printf("switch default body %d\n", i);149 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]150 // CHECK-FIXES: std::println("switch default body {}", i);151 break;152 }153 154 try {155 printf("try body %d\n", i);156 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]157 // CHECK-FIXES: std::println("try body {}", i);158 } catch (int) {159 printf("catch body %d\n", i);160 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]161 // CHECK-FIXES: std::println("catch body {}", i);162 }163 164 (printf("Parenthesised expression %d\n", i));165 // CHECK-MESSAGES: [[@LINE-1]]:4: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]166 // CHECK-FIXES: (std::println("Parenthesised expression {}", i));167 168 // Ideally we would convert these two, but the current check doesn't cope with169 // that.170 (void)printf("cast to void %d\n", i);171 // CHECK-MESSAGES-NOT: [[@LINE-1]]:9: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]172 // CHECK-FIXES-NOT: std::println("cast to void {}", i);173 174 static_cast<void>(printf("static_cast to void %d\n", i));175 // CHECK-MESSAGES-NOT: [[@LINE-1]]:9: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]176 // CHECK-FIXES-NOT: std::println("static cast to void {}", i);177 178 const int x = ({ printf("GCC statement expression using return value immediately %d\n", i); });179 const int y = ({ const int y = printf("GCC statement expression using return value immediately %d\n", i); y; });180 181 // Ideally we would convert this one, but the current check doesn't cope with182 // that.183 ({ printf("GCC statement expression with unused result %d\n", i); });184 // CHECK-MESSAGES-NOT: [[@LINE-1]]:6: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]185 // CHECK-FIXES-NOT: std::println("GCC statement expression with unused result {}", i);186 187 return printf("Return value used in return\n");188}189 190int fprintf_uses_return_value(int choice) {191 const int i = fprintf(stderr, "Return value assigned to variable %d\n", 42);192 193 extern void accepts_int(int);194 accepts_int(fprintf(stderr, "Return value passed to function %d\n", 42));195 196 if (choice == 0)197 fprintf(stderr, "if body %d\n", i);198 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]199 // CHECK-FIXES: std::println(stderr, "if body {}", i);200 else if (choice == 1)201 fprintf(stderr, "else if body %d\n", i);202 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]203 // CHECK-FIXES: std::println(stderr, "else if body {}", i);204 else205 fprintf(stderr, "else body %d\n", i);206 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]207 // CHECK-FIXES: std::println(stderr, "else body {}", i);208 209 if (fprintf(stderr, "Return value used as boolean in if statement"))210 if (fprintf(stderr, "Return value used in expression if statement") == 44)211 if (const int j = fprintf(stderr, "Return value used in assignment in if statement"))212 if (const int k = fprintf(stderr, "Return value used with initializer in if statement"); k == 44)213 ;214 215 int d = 0;216 while (fprintf(stderr, "%d", d) < 2)217 ++d;218 219 while (true)220 fprintf(stderr, "while body %d\n", i);221 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]222 // CHECK-FIXES: std::println(stderr, "while body {}", i);223 224 do225 fprintf(stderr, "do body %d\n", i);226 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]227 // CHECK-FIXES: std::println(stderr, "do body {}", i);228 while (true);229 230 for (;;)231 fprintf(stderr, "for body %d\n", i);232 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]233 // CHECK-FIXES: std::println(stderr, "for body {}", i);234 235 for (fprintf(stderr, "for init statement %d\n", i);;)236 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]237 // CHECK-FIXES: for (std::println(stderr, "for init statement {}", i);;)238 ;;239 240 for (int j = fprintf(stderr, "for init statement %d\n", i);;)241 ;;242 243 for (; fprintf(stderr, "for condition %d\n", i);)244 ;;245 246 for (;; fprintf(stderr, "for expression %d\n", i))247 // CHECK-MESSAGES: [[@LINE-1]]:11: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]248 // CHECK-FIXES: for (;; std::println(stderr, "for expression {}", i))249 ;;250 251 for (auto C : "foo")252 fprintf(stderr, "ranged-for body %d\n", i);253 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]254 // CHECK-FIXES: std::println(stderr, "ranged-for body {}", i);255 256 switch (1) {257 case 1:258 fprintf(stderr, "switch case body %d\n", i);259 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]260 // CHECK-FIXES: std::println(stderr, "switch case body {}", i);261 break;262 default:263 fprintf(stderr, "switch default body %d\n", i);264 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]265 // CHECK-FIXES: std::println(stderr, "switch default body {}", i);266 break;267 }268 269 try {270 fprintf(stderr, "try body %d\n", i);271 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]272 // CHECK-FIXES: std::println(stderr, "try body {}", i);273 } catch (int) {274 fprintf(stderr, "catch body %d\n", i);275 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]276 // CHECK-FIXES: std::println(stderr, "catch body {}", i);277 }278 279 280 (printf("Parenthesised expression %d\n", i));281 // CHECK-MESSAGES: [[@LINE-1]]:4: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]282 // CHECK-FIXES: (std::println("Parenthesised expression {}", i));283 284 // Ideally we would convert these two, but the current check doesn't cope with285 // that.286 (void)fprintf(stderr, "cast to void %d\n", i);287 // CHECK-MESSAGES-NOT: [[@LINE-1]]:9: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]288 // CHECK-FIXES-NOT: std::println(stderr, "cast to void {}", i);289 290 static_cast<void>(fprintf(stderr, "static_cast to void %d\n", i));291 // CHECK-MESSAGES-NOT: [[@LINE-1]]:9: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]292 // CHECK-FIXES-NOT: std::println(stderr, "static cast to void {}", i);293 294 const int x = ({ fprintf(stderr, "GCC statement expression using return value immediately %d\n", i); });295 const int y = ({ const int y = fprintf(stderr, "GCC statement expression using return value immediately %d\n", i); y; });296 297 // Ideally we would convert this one, but the current check doesn't cope with298 // that.299 ({ fprintf(stderr, "GCC statement expression with unused result %d\n", i); });300 // CHECK-MESSAGES-NOT: [[@LINE-1]]:6: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]301 // CHECK-FIXES-NOT: std::println("GCC statement expression with unused result {}", i);302 303 return fprintf(stderr, "Return value used in return\n");304}305 306void fprintf_simple() {307 fprintf(stderr, "Hello");308 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'fprintf' [modernize-use-std-print]309 // CHECK-FIXES: std::print(stderr, "Hello");310}311 312void fprintf_utf8_text() {313 fprintf(stderr, "你好世界\n");314 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]315 // CHECK-FIXES: std::println(stderr, "你好世界");316}317 318void std_printf_simple() {319 std::printf("std::Hello");320 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]321 // CHECK-FIXES: std::print("std::Hello");322}323 324void printf_escape() {325 printf("before \t");326 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]327 // CHECK-FIXES: std::print("before \t");328 329 printf("\n after");330 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]331 // CHECK-FIXES: std::print("\n after");332 333 printf("before \a after");334 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]335 // CHECK-FIXES: std::print("before \a after");336 337 printf("Bell\a%dBackspace\bFF%s\fNewline\nCR\rTab\tVT\vEscape\x1b\x07%d", 42, "string", 99);338 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]339 // CHECK-FIXES: std::print("Bell\a{}Backspace\bFF{}\fNewline\nCR\rTab\tVT\vEscape\x1b\a{}", 42, "string", 99);340 341 printf("not special \x1b\x01\x7f");342 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]343 // CHECK-FIXES: std::print("not special \x1b\x01\x7f");344}345 346void printf_percent() {347 printf("before %%");348 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]349 // CHECK-FIXES: std::print("before %");350 351 printf("%% after");352 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]353 // CHECK-FIXES: std::print("% after");354 355 printf("before %% after");356 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]357 // CHECK-FIXES: std::print("before % after");358 359 printf("Hello %% and another %%");360 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]361 // CHECK-FIXES: std::print("Hello % and another %");362 363 printf("Not a string %%s");364 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]365 // CHECK-FIXES: std::print("Not a string %s");366}367 368void printf_curlies() {369 printf("%d {}", 42);370 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]371 // CHECK-FIXES: std::print("{} {{[{][{]}}}}", 42);372 373 printf("{}");374 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]375 // CHECK-FIXES: std::print("{{[{][{]}}}}");376}377 378void printf_unsupported_format_specifiers() {379 int pos;380 printf("%d %n %d\n", 42, &pos, 72);381 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::println' instead of 'printf' because '%n' is not supported in format string [modernize-use-std-print]382 383 printf("Error %m\n");384 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::println' instead of 'printf' because '%m' is not supported in format string [modernize-use-std-print]385}386 387void printf_not_string_literal(const char *fmt) {388 // We can't convert the format string if it's not a literal389 printf(fmt, 42);390}391 392void printf_inttypes_ugliness() {393 // The one advantage of the checker seeing the token pasted version of the394 // format string is that we automatically cope with the horrendously-ugly395 // inttypes.h macros!396 int64_t u64 = 42;397 uintmax_t umax = 4242;398 printf("uint64:%" PRId64 " uintmax:%" PRIuMAX "\n", u64, umax);399 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]400 // CHECK-FIXES: std::println("uint64:{} uintmax:{}", u64, umax);401}402 403void printf_raw_string() {404 // This one doesn't require the format string to be changed, so it stays intact405 printf(R"(First\Second)");406 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]407 // CHECK-FIXES: std::print(R"(First\Second)");408 409 // This one does require the format string to be changed, so unfortunately it410 // gets reformatted as a normal string.411 printf(R"(First %d\Second)", 42);412 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]413 // CHECK-FIXES: std::print("First {}\\Second", 42);414}415 416void printf_integer_d() {417 const bool b = true;418 // The "d" type is necessary here for compatibility with printf since419 // std::print will print booleans as "true" or "false".420 printf("Integer %d from bool\n", b);421 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]422 // CHECK-FIXES: std::println("Integer {:d} from bool", b);423 424 // The "d" type is necessary here for compatibility with printf since425 // std::print will print booleans as "true" or "false".426 printf("Integer %i from bool\n", b);427 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]428 // CHECK-FIXES: std::println("Integer {:d} from bool", b);429 430 // The 'd' is always necessary if we pass a char since otherwise the431 // parameter will be formatted as a character. In StrictMode, the432 // cast is always necessary to maintain the printf behaviour since433 // char may be unsigned, but this also means that the 'd' is not434 // necessary.435 const char c = 'A';436 printf("Integers %d %hhd from char\n", c, c);437 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]438 // CHECK-FIXES-NOTSTRICT: std::println("Integers {:d} {:d} from char", c, c);439 // CHECK-FIXES-STRICT: std::println("Integers {} {} from char", static_cast<signed char>(c), static_cast<signed char>(c));440 441 printf("Integers %i %hhi from char\n", c, c);442 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]443 // CHECK-FIXES-NOTSTRICT: std::println("Integers {:d} {:d} from char", c, c);444 // CHECK-FIXES-STRICT: std::println("Integers {} {} from char", static_cast<signed char>(c), static_cast<signed char>(c));445 446 const signed char sc = 'A';447 printf("Integers %d %hhd from signed char\n", sc, sc);448 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]449 // CHECK-FIXES: std::println("Integers {} {} from signed char", sc, sc);450 451 printf("Integers %i %hhi from signed char\n", sc, sc);452 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]453 // CHECK-FIXES: std::println("Integers {} {} from signed char", sc, sc);454 455 const unsigned char uc = 'A';456 printf("Integers %d %hhd from unsigned char\n", uc, uc);457 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]458 // CHECK-FIXES-NOTSTRICT: std::println("Integers {} {} from unsigned char", uc, uc);459 // CHECK-FIXES-STRICT: std::println("Integers {} {} from unsigned char", static_cast<signed char>(uc), static_cast<signed char>(uc));460 461 printf("Integers %i %hhi from unsigned char\n", uc, uc);462 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]463 // CHECK-FIXES-NOTSTRICT: std::println("Integers {} {} from unsigned char", uc, uc);464 // CHECK-FIXES-STRICT: std::println("Integers {} {} from unsigned char", static_cast<signed char>(uc), static_cast<signed char>(uc));465 466 const int8_t i8 = 42;467 printf("Integer %" PRIi8 " from int8_t\n", i8);468 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]469 // CHECK-FIXES: std::println("Integer {} from int8_t", i8);470 471 const int_fast8_t if8 = 42;472 printf("Integer %" PRIiFAST8 " from int_fast8_t\n", if8);473 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]474 // CHECK-FIXES: std::println("Integer {} from int_fast8_t", if8);475 476 const int_least8_t il8 = 42;477 printf("Integer %" PRIiFAST8 " from int_least8_t\n", il8);478 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]479 // CHECK-FIXES: std::println("Integer {} from int_least8_t", il8);480 481 const uint8_t u8 = 42U;482 const std::uint8_t su8 = u8;483 printf("Integers %" PRIi8 " and %" PRId8 " from uint8_t\n", u8, su8);484 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]485 // CHECK-FIXES-NOTSTRICT: std::println("Integers {} and {} from uint8_t", u8, su8);486 // CHECK-FIXES-STRICT: std::println("Integers {} and {} from uint8_t", static_cast<int8_t>(u8), static_cast<std::int8_t>(su8));487 488 const uint_fast8_t uf8 = 42U;489 printf("Integer %" PRIiFAST8 " from uint_fast8_t\n", uf8);490 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]491 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from uint_fast8_t", uf8);492 // CHECK-FIXES-STRICT: std::println("Integer {} from uint_fast8_t", static_cast<int_fast8_t>(uf8));493 494 const uint_least8_t ul8 = 42U;495 printf("Integer %" PRIiLEAST8 " from uint_least8_t\n", ul8);496 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]497 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from uint_least8_t", ul8);498 // CHECK-FIXES-STRICT: std::println("Integer {} from uint_least8_t", static_cast<int_least8_t>(ul8));499 500 const short s = 42;501 printf("Integer %hd from short\n", s);502 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]503 // CHECK-FIXES: std::println("Integer {} from short", s);504 505 printf("Integer %hi from short\n", s);506 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]507 // CHECK-FIXES: std::println("Integer {} from short", s);508 509 const unsigned short us = 42U;510 printf("Integer %hd from unsigned short\n", us);511 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]512 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from unsigned short", us);513 // CHECK-FIXES-STRICT: std::println("Integer {} from unsigned short", static_cast<short>(us));514 515 printf("Integer %hi from unsigned short\n", us);516 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]517 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from unsigned short", us);518 // CHECK-FIXES-STRICT: std::println("Integer {} from unsigned short", static_cast<short>(us));519 520 const int i = 42;521 printf("Integer %d from integer\n", i);522 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]523 // CHECK-FIXES: std::println("Integer {} from integer", i);524 525 printf("Integer %i from integer\n", i);526 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]527 // CHECK-FIXES: std::println("Integer {} from integer", i);528 529 const unsigned int ui = 42U;530 printf("Integer %d from unsigned integer\n", ui);531 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]532 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from unsigned integer", ui);533 // CHECK-FIXES-STRICT: std::println("Integer {} from unsigned integer", static_cast<int>(ui));534 535 printf("Integer %i from unsigned integer\n", ui);536 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]537 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from unsigned integer", ui);538 // CHECK-FIXES-STRICT: std::println("Integer {} from unsigned integer", static_cast<int>(ui));539 540 const long l = 42L;541 printf("Integer %ld from long\n", l);542 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]543 // CHECK-FIXES: std::println("Integer {} from long", l);544 545 printf("Integer %li from long\n", l);546 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]547 // CHECK-FIXES: std::println("Integer {} from long", l);548 549 const unsigned long ul = 42UL;550 printf("Integer %ld from unsigned long\n", ul);551 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]552 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from unsigned long", ul);553 // CHECK-FIXES-STRICT: std::println("Integer {} from unsigned long", static_cast<long>(ul));554 555 printf("Integer %li from unsigned long\n", ul);556 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]557 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from unsigned long", ul);558 // CHECK-FIXES-STRICT: std::println("Integer {} from unsigned long", static_cast<long>(ul));559 560 const long long ll = 42LL;561 printf("Integer %lld from long long\n", ll);562 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]563 // CHECK-FIXES: std::println("Integer {} from long long", ll);564 565 printf("Integer %lli from long long\n", ll);566 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]567 // CHECK-FIXES: std::println("Integer {} from long long", ll);568 569 const unsigned long long ull = 42ULL;570 printf("Integer %lld from unsigned long long\n", ull);571 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]572 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from unsigned long long", ull);573 // CHECK-FIXES-STRICT: std::println("Integer {} from unsigned long long", static_cast<long long>(ull));574 575 printf("Integer %lli from unsigned long long\n", ull);576 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]577 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from unsigned long long", ull);578 // CHECK-FIXES-STRICT: std::println("Integer {} from unsigned long long", static_cast<long long>(ull));579 580 const intmax_t im = 42;581 const std::intmax_t sim = im;582 printf("Integers %jd and %jd from intmax_t\n", im, sim);583 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]584 // CHECK-FIXES: std::println("Integers {} and {} from intmax_t", im, sim);585 586 printf("Integers %ji and %ji from intmax_t\n", im, sim);587 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]588 // CHECK-FIXES: std::println("Integers {} and {} from intmax_t", im, sim);589 590 const uintmax_t uim = 42;591 const std::uintmax_t suim = uim;592 printf("Integers %jd and %jd from uintmax_t\n", uim, suim);593 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]594 // CHECK-FIXES-NOTSTRICT: std::println("Integers {} and {} from uintmax_t", uim, suim);595 // CHECK-FIXES-STRICT: std::println("Integers {} and {} from uintmax_t", static_cast<intmax_t>(uim), static_cast<std::intmax_t>(suim));596 597 printf("Integer %ji from intmax_t\n", uim);598 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]599 // CHECK-FIXES-NOTSTRICT: std::println("Integer {} from intmax_t", uim);600 // CHECK-FIXES-STRICT: std::println("Integer {} from intmax_t", static_cast<intmax_t>(uim));601 602 const int ai[] = { 0, 1, 2, 3};603 const ptrdiff_t pd = &ai[3] - &ai[0];604 const std::ptrdiff_t spd = pd;605 printf("Integers %td and %td from ptrdiff_t\n", pd, spd);606 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]607 // CHECK-FIXES: std::println("Integers {} and {} from ptrdiff_t", pd, spd);608 609 printf("Integers %ti and %ti from ptrdiff_t\n", pd, spd);610 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]611 // CHECK-FIXES: std::println("Integers {} and {} from ptrdiff_t", pd, spd);612 613 const size_t z = 42UL;614 const std::size_t sz = z;615 printf("Integers %zd and %zd from size_t\n", z, sz);616 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]617 // CHECK-FIXES-NOTSTRICT: std::println("Integers {} and {} from size_t", z, sz);618 // CHECK-FIXES-STRICT: std::println("Integers {} and {} from size_t", static_cast<ssize_t>(z), static_cast<std::ssize_t>(sz));619}620 621void printf_integer_u()622{623 const bool b = true;624 // The "d" type is necessary here for compatibility with printf since625 // std::print will print booleans as "true" or "false".626 printf("Unsigned integer %u from bool\n", b);627 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]628 // CHECK-FIXES: std::println("Unsigned integer {:d} from bool", b);629 630 const char c = 'A';631 printf("Unsigned integer %hhu from char\n", c);632 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]633 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integer {:d} from char", c);634 // CHECK-FIXES-STRICT: std::println("Unsigned integer {} from char", static_cast<unsigned char>(c));635 636 const signed char sc = 'A';637 printf("Unsigned integer %hhu from signed char\n", sc);638 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]639 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integer {} from signed char", sc);640 // CHECK-FIXES-STRICT: std::println("Unsigned integer {} from signed char", static_cast<unsigned char>(sc));641 642 printf("Unsigned integer %u from signed char\n", sc);643 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]644 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integer {} from signed char", sc);645 // CHECK-FIXES-STRICT: std::println("Unsigned integer {} from signed char", static_cast<unsigned char>(sc));646 647 const unsigned char uc = 'A';648 printf("Unsigned integer %hhu from unsigned char\n", uc);649 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]650 // CHECK-FIXES: std::println("Unsigned integer {} from unsigned char", uc);651 652 printf("Unsigned integer %u from unsigned char\n", uc);653 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]654 // CHECK-FIXES: std::println("Unsigned integer {} from unsigned char", uc);655 656 const int8_t i8 = 42;657 printf("Unsigned integer %" PRIu8 " from int8_t\n", i8);658 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]659 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integer {} from int8_t", i8);660 // CHECK-FIXES-STRICT: std::println("Unsigned integer {} from int8_t", static_cast<uint8_t>(i8));661 662 const int_fast8_t if8 = 42;663 printf("Unsigned integer %" PRIuFAST8 " from int_fast8_t\n", if8);664 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]665 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integer {} from int_fast8_t", if8);666 // CHECK-FIXES-STRICT: std::println("Unsigned integer {} from int_fast8_t", static_cast<uint_fast8_t>(if8));667 668 const int_least8_t il8 = 42;669 printf("Unsigned integer %" PRIuFAST8 " from int_least8_t\n", il8);670 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]671 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integer {} from int_least8_t", il8);672 // CHECK-FIXES-STRICT: std::println("Unsigned integer {} from int_least8_t", static_cast<uint_least8_t>(il8));673 674 const uint8_t u8 = 42U;675 printf("Unsigned integer %" PRIu8 " from uint8_t\n", u8);676 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]677 // CHECK-FIXES: std::println("Unsigned integer {} from uint8_t", u8);678 679 const uint_fast8_t uf8 = 42U;680 printf("Unsigned integer %" PRIuFAST8 " from uint_fast8_t\n", uf8);681 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]682 // CHECK-FIXES: std::println("Unsigned integer {} from uint_fast8_t", uf8);683 684 const uint_least8_t ul8 = 42U;685 printf("Unsigned integer %" PRIuLEAST8 " from uint_least8_t\n", ul8);686 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]687 // CHECK-FIXES: std::println("Unsigned integer {} from uint_least8_t", ul8);688 689 const short s = 42;690 printf("Unsigned integer %hu from short\n", s);691 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]692 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integer {} from short", s);693 // CHECK-FIXES-STRICT: std::println("Unsigned integer {} from short", static_cast<unsigned short>(s));694 695 const unsigned short us = 42U;696 printf("Unsigned integer %hu from unsigned short\n", us);697 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]698 // CHECK-FIXES: std::println("Unsigned integer {} from unsigned short", us);699 700 const int i = 42;701 printf("Unsigned integer %u from signed integer\n", i);702 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]703 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integer {} from signed integer", i);704 // CHECK-FIXES-STRICT: std::println("Unsigned integer {} from signed integer", static_cast<unsigned int>(i));705 706 const unsigned int ui = 42U;707 printf("Unsigned integer %u from unsigned integer\n", ui);708 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]709 // CHECK-FIXES: std::println("Unsigned integer {} from unsigned integer", ui);710 711 const long l = 42L;712 printf("Unsigned integer %u from signed long\n", l);713 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]714 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integer {} from signed long", l);715 // CHECK-FIXES-STRICT: std::println("Unsigned integer {} from signed long", static_cast<unsigned long>(l));716 717 const unsigned long ul = 42UL;718 printf("Unsigned integer %lu from unsigned long\n", ul);719 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]720 // CHECK-FIXES: std::println("Unsigned integer {} from unsigned long", ul);721 722 const long long ll = 42LL;723 printf("Unsigned integer %llu from long long\n", ll);724 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]725 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integer {} from long long", ll);726 // CHECK-FIXES-STRICT: std::println("Unsigned integer {} from long long", static_cast<unsigned long long>(ll));727 728 const unsigned long long ull = 42ULL;729 printf("Unsigned integer %llu from unsigned long long\n", ull);730 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]731 // CHECK-FIXES: std::println("Unsigned integer {} from unsigned long long", ull);732 733 const intmax_t im = 42;734 const std::intmax_t sim = im;735 printf("Unsigned integers %ju and %ju from intmax_t\n", im, sim);736 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]737 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integers {} and {} from intmax_t", im, sim);738 // CHECK-FIXES-STRICT: std::println("Unsigned integers {} and {} from intmax_t", static_cast<uintmax_t>(im), static_cast<std::uintmax_t>(sim));739 740 const uintmax_t uim = 42U;741 const std::uintmax_t suim = uim;742 printf("Unsigned integers %ju and %ju from uintmax_t\n", uim, suim);743 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]744 // CHECK-FIXES: std::println("Unsigned integers {} and {} from uintmax_t", uim, suim);745 746 const int ai[] = { 0, 1, 2, 3};747 const ptrdiff_t pd = &ai[3] - &ai[0];748 const std::ptrdiff_t spd = pd;749 printf("Unsigned integers %tu and %tu from ptrdiff_t\n", pd, spd);750 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]751 // CHECK-FIXES-NOTSTRICT: std::println("Unsigned integers {} and {} from ptrdiff_t", pd, spd);752 // CHECK-FIXES-STRICT: std::println("Unsigned integers {} and {} from ptrdiff_t", static_cast<size_t>(pd), static_cast<std::size_t>(spd));753 754 const size_t z = 42U;755 const std::size_t sz = z;756 printf("Unsigned integers %zu and %zu from size_t\n", z, sz);757 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]758 // CHECK-FIXES: std::println("Unsigned integers {} and {} from size_t", z, sz);759}760 761// This checks that we get the argument offset right with the extra FILE * argument762void fprintf_integer() {763 fprintf(stderr, "Integer %d from integer\n", 42);764 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]765 // CHECK-FIXES: std::println(stderr, "Integer {} from integer", 42);766 767 fprintf(stderr, "Integer %i from integer\n", 65);768 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]769 // CHECK-FIXES: std::println(stderr, "Integer {} from integer", 65);770 771 fprintf(stderr, "Integer %i from char\n", 'A');772 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]773 // CHECK-FIXES-NOTSTRICT: std::println(stderr, "Integer {:d} from char", 'A');774 // CHECK-FIXES-STRICT: std::println(stderr, "Integer {} from char", static_cast<signed char>('A'));775 776 fprintf(stderr, "Integer %d from char\n", 'A');777 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]778 // CHECK-FIXES-NOTSTRICT: std::println(stderr, "Integer {:d} from char", 'A');779 // CHECK-FIXES-STRICT: std::println(stderr, "Integer {} from char", static_cast<signed char>('A'));780}781 782void printf_char() {783 const char c = 'A';784 printf("Char %c from char\n", c);785 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]786 // CHECK-FIXES: std::println("Char {} from char", c);787 788 const signed char sc = 'A';789 printf("Char %c from signed char\n", sc);790 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]791 // CHECK-FIXES: std::println("Char {:c} from signed char", sc);792 793 const unsigned char uc = 'A';794 printf("Char %c from unsigned char\n", uc);795 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]796 // CHECK-FIXES: std::println("Char {:c} from unsigned char", uc);797 798 const int i = 65;799 printf("Char %c from integer\n", i);800 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]801 // CHECK-FIXES: std::println("Char {:c} from integer", i);802 803 const unsigned int ui = 65;804 printf("Char %c from unsigned integer\n", ui);805 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]806 // CHECK-FIXES: std::println("Char {:c} from unsigned integer", ui);807 808 const unsigned long long ull = 65;809 printf("Char %c from unsigned long long\n", ull);810 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]811 // CHECK-FIXES: std::println("Char {:c} from unsigned long long", ull);812}813 814void printf_bases() {815 printf("Hex %lx\n", 42L);816 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]817 // CHECK-FIXES: std::println("Hex {:x}", 42L);818 819 printf("HEX %X\n", 42);820 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]821 // CHECK-FIXES: std::println("HEX {:X}", 42);822 823 printf("Oct %lo\n", 42L);824 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]825 // CHECK-FIXES: std::println("Oct {:o}", 42L);826}827 828void printf_alternative_forms() {829 printf("Hex %#lx\n", 42L);830 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]831 // CHECK-FIXES: std::println("Hex {:#x}", 42L);832 833 printf("HEX %#X\n", 42);834 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]835 // CHECK-FIXES: std::println("HEX {:#X}", 42);836 837 printf("Oct %#lo\n", 42L);838 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]839 // CHECK-FIXES: std::println("Oct {:#o}", 42L);840 841 printf("Double %#f %#F\n", -42.0, -42.0);842 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]843 // CHECK-FIXES: std::println("Double {:#f} {:#F}", -42.0, -42.0);844 845 printf("Double %#g %#G\n", -42.0, -42.0);846 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]847 // CHECK-FIXES: std::println("Double {:#g} {:#G}", -42.0, -42.0);848 849 printf("Double %#e %#E\n", -42.0, -42.0);850 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]851 // CHECK-FIXES: std::println("Double {:#e} {:#E}", -42.0, -42.0);852 853 printf("Double %#a %#A\n", -42.0, -42.0);854 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]855 // CHECK-FIXES: std::println("Double {:#a} {:#A}", -42.0, -42.0);856 857 // Characters don't have an alternate form858 printf("Char %#c\n", 'A');859 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]860 // CHECK-FIXES: std::println("Char {}", 'A');861 862 // Strings don't have an alternate form863 printf("Char %#c\n", 'A');864 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]865 // CHECK-FIXES: std::println("Char {}", 'A');866}867 868void printf_string() {869 printf("Hello %s after\n", "Goodbye");870 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]871 // CHECK-FIXES: std::println("Hello {} after", "Goodbye");872 873 // std::print can't print signed char strings.874 const signed char *sstring = reinterpret_cast<const signed char *>("ustring");875 printf("signed char string %s\n", sstring);876 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]877 // CHECK-FIXES: std::println("signed char string {}", reinterpret_cast<const char *>(sstring));878 879 // std::print can't print unsigned char strings.880 const unsigned char *ustring = reinterpret_cast<const unsigned char *>("ustring");881 printf("unsigned char string %s\n", ustring);882 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]883 // CHECK-FIXES: std::println("unsigned char string {}", reinterpret_cast<const char *>(ustring));884}885 886void printf_float() {887 // If the type is not specified then either f or e will be used depending on888 // whichever is shorter. This means that it is necessary to be specific to889 // maintain compatibility with printf.890 891 // TODO: Should we force a cast here, since printf will promote to double892 // automatically, but std::format will not, which could result in different893 // output?894 895 const float f = 42.0F;896 printf("Hello %f after\n", f);897 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]898 // CHECK-FIXES: std::println("Hello {:f} after", f);899 900 printf("Hello %g after\n", f);901 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]902 // CHECK-FIXES: std::println("Hello {:g} after", f);903 904 printf("Hello %e after\n", f);905 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]906 // CHECK-FIXES: std::println("Hello {:e} after", f);907}908 909void printf_double() {910 // If the type is not specified then either f or e will be used depending on911 // whichever is shorter. This means that it is necessary to be specific to912 // maintain compatibility with printf.913 914 const double d = 42.0;915 printf("Hello %f after\n", d);916 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]917 // CHECK-FIXES: std::println("Hello {:f} after", d);918 919 printf("Hello %g after\n", d);920 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]921 // CHECK-FIXES: std::println("Hello {:g} after", d);922 923 printf("Hello %e after\n", d);924 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]925 // CHECK-FIXES: std::println("Hello {:e} after", d);926}927 928void printf_long_double() {929 // If the type is not specified then either f or e will be used depending on930 // whichever is shorter. This means that it is necessary to be specific to931 // maintain compatibility with printf.932 933 const long double ld = 42.0L;934 printf("Hello %Lf after\n", ld);935 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]936 // CHECK-FIXES: std::println("Hello {:f} after", ld);937 938 printf("Hello %g after\n", ld);939 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]940 // CHECK-FIXES: std::println("Hello {:g} after", ld);941 942 printf("Hello %e after\n", ld);943 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]944 // CHECK-FIXES: std::println("Hello {:e} after", ld);945}946 947void printf_pointer() {948 int i;949 double j;950 printf("Int* %p %s %p\n", &i, "Double*", &j);951 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]952 // CHECK-FIXES: std::println("Int* {} {} {}", static_cast<const void *>(&i), "Double*", static_cast<const void *>(&j));953 954 printf("%p\n", nullptr);955 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]956 // CHECK-FIXES: std::println("{}", nullptr);957 958 const auto np = nullptr;959 printf("%p\n", np);960 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]961 // CHECK-FIXES: std::println("{}", np);962 963 // NULL isn't a pointer, so std::print needs some help.964 printf("%p\n", NULL);965 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]966 // CHECK-FIXES: std::println("{}", static_cast<const void *>(NULL));967 968 printf("%p\n", 42);969 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]970 // CHECK-FIXES: std::println("{}", static_cast<const void *>(42));971 972 // If we already have a void pointer then no cast is required.973 printf("%p\n", reinterpret_cast<const void *>(44));974 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]975 // CHECK-FIXES: std::println("{}", reinterpret_cast<const void *>(44));976 977 const void *p;978 printf("%p\n", p);979 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]980 // CHECK-FIXES: std::println("{}", p);981 982 // But a pointer to a pointer to void does need a cast983 const void **pp;984 printf("%p\n", pp);985 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]986 // CHECK-FIXES: std::println("{}", static_cast<const void *>(pp));987 988 printf("%p\n", printf_pointer);989 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]990 // CHECK-FIXES: std::println("{}", static_cast<const void *>(printf_pointer));991}992 993class AClass994{995 int member;996 997 void printf_this_pointer()998 {999 printf("%p\n", this);1000 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1001 // CHECK-FIXES: std::println("{}", static_cast<const void *>(this));1002 }1003 1004 void printf_pointer_to_member_function()1005 {1006 printf("%p\n", &AClass::printf_pointer_to_member_function);1007 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1008 // CHECK-FIXES: std::println("{}", static_cast<const void *>(&AClass::printf_pointer_to_member_function));1009 }1010 1011 void printf_pointer_to_member_variable()1012 {1013 printf("%p\n", &AClass::member);1014 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1015 // CHECK-FIXES: std::println("{}", static_cast<const void *>(&AClass::member));1016 }1017};1018 1019void printf_positional_arg() {1020 printf("%1$d", 42);1021 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1022 // CHECK-FIXES: std::print("{0}", 42);1023 1024 printf("before %1$d", 42);1025 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1026 // CHECK-FIXES: std::print("before {0}", 42);1027 1028 printf("%1$d after", 42);1029 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1030 // CHECK-FIXES: std::print("{0} after", 42);1031 1032 printf("before %1$d after", 42);1033 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1034 // CHECK-FIXES: std::print("before {0} after", 42);1035 1036 printf("before %2$d between %1$s after", "string", 42);1037 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1038 // CHECK-FIXES: std::print("before {1} between {0} after", "string", 42);1039}1040 1041// printf always defaults to right justification,, no matter what the type is of1042// the argument. std::format uses left justification by default for strings, and1043// right justification for numbers.1044void printf_right_justified() {1045 printf("Right-justified integer %4d after\n", 42);1046 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1047 // CHECK-FIXES: std::println("Right-justified integer {:4} after", 42);1048 1049 printf("Right-justified double %4f\n", 227.2);1050 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1051 // CHECK-FIXES: std::println("Right-justified double {:4f}", 227.2);1052 1053 printf("Right-justified double %4g\n", 227.4);1054 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1055 // CHECK-FIXES: std::println("Right-justified double {:4g}", 227.4);1056 1057 printf("Right-justified integer with field width argument %*d after\n", 5, 424242);1058 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1059 // CHECK-FIXES: std::println("Right-justified integer with field width argument {:{}} after", 424242, 5);1060 1061 printf("Right-justified integer with field width argument %2$*1$d after\n", 5, 424242);1062 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1063 // CHECK-FIXES: std::println("Right-justified integer with field width argument {1:{0}} after", 5, 424242);1064 1065 printf("Right-justified string %20s\n", "Hello");1066 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1067 // CHECK-FIXES: std::println("Right-justified string {:>20}", "Hello");1068 1069 printf("Right-justified string with field width argument %2$*1$s after\n", 20, "wibble");1070 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1071 // CHECK-FIXES: std::println("Right-justified string with field width argument {1:>{0}} after", 20, "wibble");1072}1073 1074// printf always requires - for left justification, no matter what the type is1075// of the argument. std::format uses left justification by default for strings,1076// and right justification for numbers.1077void printf_left_justified() {1078 printf("Left-justified integer %-4d\n", 42);1079 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1080 // CHECK-FIXES: std::println("Left-justified integer {:<4}", 42);1081 1082 printf("Left-justified integer %--4d\n", 42);1083 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1084 // CHECK-FIXES: std::println("Left-justified integer {:<4}", 42);1085 1086 printf("Left-justified double %-4f\n", 227.2);1087 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1088 // CHECK-FIXES: std::println("Left-justified double {:<4f}", 227.2);1089 1090 printf("Left-justified double %-4g\n", 227.4);1091 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1092 // CHECK-FIXES: std::println("Left-justified double {:<4g}", 227.4);1093 1094 printf("Left-justified integer with field width argument %-*d after\n", 5, 424242);1095 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1096 // CHECK-FIXES: std::println("Left-justified integer with field width argument {:<{}} after", 424242, 5);1097 1098 printf("Left-justified integer with field width argument %2$-*1$d after\n", 5, 424242);1099 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1100 // CHECK-FIXES: std::println("Left-justified integer with field width argument {1:<{0}} after", 5, 424242);1101 1102 printf("Left-justified string %-20s\n", "Hello");1103 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1104 // CHECK-FIXES: std::println("Left-justified string {:20}", "Hello");1105 1106 printf("Left-justified string with field width argument %2$-*1$s after\n", 5, "wibble");1107 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1108 // CHECK-FIXES: std::println("Left-justified string with field width argument {1:{0}} after", 5, "wibble");1109}1110 1111void printf_precision() {1112 printf("Hello %.3f\n", 3.14159);1113 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1114 // CHECK-FIXES: std::println("Hello {:.3f}", 3.14159);1115 1116 printf("Hello %10.3f\n", 3.14159);1117 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1118 // CHECK-FIXES: std::println("Hello {:10.3f}", 3.14159);1119 1120 printf("Hello %.*f after\n", 10, 3.14159265358979323846);1121 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1122 // CHECK-FIXES: std::println("Hello {:.{}f} after", 3.14159265358979323846, 10);1123 1124 printf("Hello %10.*f after\n", 3, 3.14159265358979323846);1125 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1126 // CHECK-FIXES: std::println("Hello {:10.{}f} after", 3.14159265358979323846, 3);1127 1128 printf("Hello %*.*f after\n", 10, 4, 3.14159265358979323846);1129 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1130 // CHECK-FIXES: std::println("Hello {:{}.{}f} after", 3.14159265358979323846, 10, 4);1131 1132 printf("Hello %1$.*2$f after\n", 3.14159265358979323846, 4);1133 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1134 // CHECK-FIXES: std::println("Hello {0:.{1}f} after", 3.14159265358979323846, 4);1135 1136 // Precision is ignored, but maintained on non-numeric arguments1137 printf("Hello %.5s\n", "Goodbye");1138 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1139 // CHECK-FIXES: std::println("Hello {:.5}", "Goodbye");1140 1141 printf("Hello %.5c\n", 'G');1142 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1143 // CHECK-FIXES: std::println("Hello {:.5}", 'G');1144}1145 1146void printf_field_width_and_precision(const std::string &s1, const std::string &s2, const std::string &s3)1147{1148 printf("width only:%*d width and precision:%*.*f precision only:%.*f\n", 3, 42, 4, 2, 3.14159265358979323846, 5, 2.718);1149 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1150 // CHECK-FIXES: std::println("width only:{:{}} width and precision:{:{}.{}f} precision only:{:.{}f}", 42, 3, 3.14159265358979323846, 4, 2, 2.718, 5);1151 1152 const unsigned int ui1 = 42, ui2 = 43, ui3 = 44;1153 printf("casts width only:%*d width and precision:%*.*d precision only:%.*d\n", 3, ui1, 4, 2, ui2, 5, ui3);1154 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1155 // CHECK-FIXES-NOTSTRICT: std::println("casts width only:{:{}} width and precision:{:{}.{}} precision only:{:.{}}", ui1, 3, ui2, 4, 2, ui3, 5);1156 // CHECK-FIXES-STRICT: std::println("casts width only:{:{}} width and precision:{:{}.{}} precision only:{:.{}}", static_cast<int>(ui1), 3, static_cast<int>(ui2), 4, 2, static_cast<int>(ui3), 5);1157 1158 printf("c_str removal width only:%*s width and precision:%*.*s precision only:%.*s\n", 3, s1.c_str(), 4, 2, s2.c_str(), 5, s3.c_str());1159 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1160 // CHECK-FIXES: std::println("c_str removal width only:{:>{}} width and precision:{:>{}.{}} precision only:{:.{}}", s1, 3, s2, 4, 2, s3, 5);1161 1162 const std::string *ps1 = &s1, *ps2 = &s2, *ps3 = &s3;1163 printf("c_str() removal pointer width only:%-*s width and precision:%-*.*s precision only:%-.*s\n", 3, ps1->c_str(), 4, 2, ps2->c_str(), 5, ps3->c_str());1164 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1165 // CHECK-FIXES: std::println("c_str() removal pointer width only:{:{}} width and precision:{:{}.{}} precision only:{:.{}}", *ps1, 3, *ps2, 4, 2, *ps3, 5);1166 1167 iterator<std::string> is1, is2, is3;1168 printf("c_str() removal iterator width only:%-*s width and precision:%-*.*s precision only:%-.*s\n", 3, is1->c_str(), 4, 2, is2->c_str(), 5, is3->c_str());1169 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1170 // CHECK-FIXES: std::println("c_str() removal iterator width only:{:{}} width and precision:{:{}.{}} precision only:{:.{}}", *is1, 3, *is2, 4, 2, *is3, 5);1171 1172 printf("width and precision positional:%1$*2$.*3$f after\n", 3.14159265358979323846, 4, 2);1173 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1174 // CHECK-FIXES: std::println("width and precision positional:{0:{1}.{2}f} after", 3.14159265358979323846, 4, 2);1175 1176 const int width = 10, precision = 3;1177 printf("width only:%3$*1$d width and precision:%4$*1$.*2$f precision only:%5$.*2$f\n", width, precision, 42, 3.1415926, 2.718);1178 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1179 // CHECK-FIXES: std::println("width only:{2:{0}} width and precision:{3:{0}.{1}f} precision only:{4:.{1}f}", width, precision, 42, 3.1415926, 2.718);1180 1181 printf("c_str removal width only:%3$*1$s width and precision:%4$*1$.*2$s precision only:%5$.*2$s\n", width, precision, s1.c_str(), s2.c_str(), s3.c_str());1182 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1183 // CHECK-FIXES: std::println("c_str removal width only:{2:>{0}} width and precision:{3:>{0}.{1}} precision only:{4:.{1}}", width, precision, s1, s2, s3);1184}1185 1186void fprintf_field_width_and_precision(const std::string &s1, const std::string &s2, const std::string &s3) {1187 fprintf(stderr, "width only:%*d width and precision:%*.*f precision only:%.*f\n", 3, 42, 4, 2, 3.14159265358979323846, 5, 2.718);1188 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]1189 // CHECK-FIXES: std::println(stderr, "width only:{:{}} width and precision:{:{}.{}f} precision only:{:.{}f}", 42, 3, 3.14159265358979323846, 4, 2, 2.718, 5);1190 1191 fprintf(stderr, "width and precision positional:%1$*2$.*3$f after\n", 3.14159265358979323846, 4, 2);1192 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]1193 // CHECK-FIXES: std::println(stderr, "width and precision positional:{0:{1}.{2}f} after", 3.14159265358979323846, 4, 2);1194 1195 fprintf(stderr, "c_str removal width only:%*s width and precision:%*.*s precision only:%.*s\n", 3, s1.c_str(), 4, 2, s2.c_str(), 5, s3.c_str());1196 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]1197 // CHECK-FIXES: std::println(stderr, "c_str removal width only:{:>{}} width and precision:{:>{}.{}} precision only:{:.{}}", s1, 3, s2, 4, 2, s3, 5);1198 1199 const std::string *ps1 = &s1, *ps2 = &s2, *ps3 = &s3;1200 fprintf(stderr, "c_str() removal pointer width only:%-*s width and precision:%-*.*s precision only:%-.*s\n", 3, ps1->c_str(), 4, 2, ps2->c_str(), 5, ps3->c_str());1201 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]1202 // CHECK-FIXES: std::println(stderr, "c_str() removal pointer width only:{:{}} width and precision:{:{}.{}} precision only:{:.{}}", *ps1, 3, *ps2, 4, 2, *ps3, 5);1203 1204 iterator<std::string> is1, is2, is3;1205 fprintf(stderr, "c_str() removal iterator width only:%-*s width and precision:%-*.*s precision only:%-.*s\n", 3, is1->c_str(), 4, 2, is2->c_str(), 5, is3->c_str());1206 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]1207 // CHECK-FIXES: std::println(stderr, "c_str() removal iterator width only:{:{}} width and precision:{:{}.{}} precision only:{:.{}}", *is1, 3, *is2, 4, 2, *is3, 5);1208 1209 const int width = 10, precision = 3;1210 fprintf(stderr, "width only:%3$*1$d width and precision:%4$*1$.*2$f precision only:%5$.*2$f\n", width, precision, 42, 3.1415926, 2.718);1211 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]1212 // CHECK-FIXES: std::println(stderr, "width only:{2:{0}} width and precision:{3:{0}.{1}f} precision only:{4:.{1}f}", width, precision, 42, 3.1415926, 2.718);1213 1214 fprintf(stderr, "c_str removal width only:%3$*1$s width and precision:%4$*1$.*2$s precision only:%5$.*2$s\n", width, precision, s1.c_str(), s2.c_str(), s3.c_str());1215 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'fprintf' [modernize-use-std-print]1216 // CHECK-FIXES: std::println(stderr, "c_str removal width only:{2:>{0}} width and precision:{3:>{0}.{1}} precision only:{4:.{1}}", width, precision, s1, s2, s3);1217}1218 1219void printf_alternative_form() {1220 printf("Wibble %#x\n", 42);1221 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1222 // CHECK-FIXES: std::println("Wibble {:#x}", 42);1223 1224 printf("Wibble %#20x\n", 42);1225 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1226 // CHECK-FIXES: std::println("Wibble {:#20x}", 42);1227 1228 printf("Wibble %#020x\n", 42);1229 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1230 // CHECK-FIXES: std::println("Wibble {:#020x}", 42);1231 1232 printf("Wibble %#-20x\n", 42);1233 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1234 // CHECK-FIXES: std::println("Wibble {:<#20x}", 42);1235}1236 1237void printf_leading_plus() {1238 printf("Positive integer %+d\n", 42);1239 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1240 // CHECK-FIXES: std::println("Positive integer {:+}", 42);1241 1242 printf("Positive double %+f\n", 42.2);1243 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1244 // CHECK-FIXES: std::println("Positive double {:+f}", 42.2);1245 1246 printf("Positive double %+g\n", 42.2);1247 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1248 // CHECK-FIXES: std::println("Positive double {:+g}", 42.2);1249 1250 // Ignore leading plus on strings to avoid potential runtime exception where1251 // printf would have just ignored it.1252 printf("Positive string %+s\n", "string");1253 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1254 // CHECK-FIXES: std::println("Positive string {}", "string");1255}1256 1257void printf_leading_space() {1258 printf("Spaced integer % d\n", 42);1259 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1260 // CHECK-FIXES: std::println("Spaced integer {: }", 42);1261 1262 printf("Spaced integer %- d\n", 42);1263 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1264 // CHECK-FIXES: std::println("Spaced integer {: }", 42);1265 1266 printf("Spaced double % f\n", 42.2);1267 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1268 // CHECK-FIXES: std::println("Spaced double {: f}", 42.2);1269 1270 printf("Spaced double % g\n", 42.2);1271 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1272 // CHECK-FIXES: std::println("Spaced double {: g}", 42.2);1273}1274 1275void printf_leading_zero() {1276 printf("Leading zero integer %03d\n", 42);1277 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1278 // CHECK-FIXES: std::println("Leading zero integer {:03}", 42);1279 1280 printf("Leading minus and zero integer %-03d minus ignored\n", 42);1281 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1282 // CHECK-FIXES: std::println("Leading minus and zero integer {:<03} minus ignored", 42);1283 1284 printf("Leading zero unsigned integer %03u\n", 42U);1285 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1286 // CHECK-FIXES: std::println("Leading zero unsigned integer {:03}", 42U);1287 1288 printf("Leading zero double %03f\n", 42.2);1289 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1290 // CHECK-FIXES: std::println("Leading zero double {:03f}", 42.2);1291 1292 printf("Leading zero double %03g\n", 42.2);1293 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1294 // CHECK-FIXES: std::println("Leading zero double {:03g}", 42.2);1295}1296 1297void printf_leading_plus_and_space() {1298 // printf prefers plus to space. {fmt} will throw if both are present.1299 printf("Spaced integer % +d\n", 42);1300 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1301 // CHECK-FIXES: std::println("Spaced integer {:+}", 42);1302 1303 printf("Spaced double %+ f\n", 42.2);1304 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1305 // CHECK-FIXES: std::println("Spaced double {:+f}", 42.2);1306 1307 printf("Spaced double % +g\n", 42.2);1308 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1309 // CHECK-FIXES: std::println("Spaced double {:+g}", 42.2);1310}1311 1312void printf_leading_zero_and_plus() {1313 printf("Leading zero integer %+03d\n", 42);1314 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1315 // CHECK-FIXES: std::println("Leading zero integer {:+03}", 42);1316 1317 printf("Leading zero double %0+3f\n", 42.2);1318 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1319 // CHECK-FIXES: std::println("Leading zero double {:+03f}", 42.2);1320 1321 printf("Leading zero double %0+3g\n", 42.2);1322 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1323 // CHECK-FIXES: std::println("Leading zero double {:+03g}", 42.2);1324}1325 1326void printf_leading_zero_and_space() {1327 printf("Leading zero and space integer %0 3d\n", 42);1328 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1329 // CHECK-FIXES: std::println("Leading zero and space integer {: 03}", 42);1330 1331 printf("Leading zero and space double %0 3f\n", 42.2);1332 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1333 // CHECK-FIXES: std::println("Leading zero and space double {: 03f}", 42.2);1334 1335 printf("Leading zero and space double %0 3g\n", 42.2);1336 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1337 // CHECK-FIXES: std::println("Leading zero and space double {: 03g}", 42.2);1338}1339 1340// add signed plained enum too1341enum PlainEnum { red };1342enum SignedPlainEnum { black = -42 };1343enum BoolEnum : unsigned int { yellow };1344enum CharEnum : char { purple };1345enum SCharEnum : signed char { aquamarine };1346enum UCharEnum : unsigned char { pink };1347enum ShortEnum : short { beige };1348enum UShortEnum : unsigned short { grey };1349enum IntEnum : int { green };1350enum UIntEnum : unsigned int { blue };1351enum LongEnum : long { magenta };1352enum ULongEnum : unsigned long { cyan };1353enum LongLongEnum : long long { taupe };1354enum ULongLongEnum : unsigned long long { brown };1355 1356void printf_enum_d() {1357 PlainEnum plain_enum;1358 printf("%d", plain_enum);1359 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1360 // CHECK-FIXES: std::print("{}", static_cast<int>(plain_enum));1361 1362 SignedPlainEnum splain_enum;1363 printf("%d", splain_enum);1364 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1365 // CHECK-FIXES: std::print("{}", static_cast<int>(splain_enum));1366 1367 BoolEnum bool_enum;1368 printf("%d", bool_enum);1369 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1370 // CHECK-FIXES: std::print("{}", static_cast<int>(bool_enum));1371 1372 CharEnum char_enum;1373 printf("%d", char_enum);1374 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1375 // CHECK-FIXES: std::print("{}", static_cast<signed char>(char_enum));1376 1377 SCharEnum schar_enum;1378 printf("%d", schar_enum);1379 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1380 // CHECK-FIXES: std::print("{}", static_cast<signed char>(schar_enum));1381 1382 UCharEnum uchar_enum;1383 printf("%d", uchar_enum);1384 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1385 // CHECK-FIXES: std::print("{}", static_cast<signed char>(uchar_enum));1386 1387 ShortEnum short_enum;1388 printf("%d", short_enum);1389 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1390 // CHECK-FIXES: std::print("{}", static_cast<short>(short_enum));1391 1392 UShortEnum ushort_enum;1393 printf("%d", ushort_enum);1394 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1395 // CHECK-FIXES: std::print("{}", static_cast<short>(ushort_enum));1396 1397 IntEnum int_enum;1398 printf("%d", int_enum);1399 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1400 // CHECK-FIXES: std::print("{}", static_cast<int>(int_enum));1401 1402 UIntEnum uint_enum;1403 printf("%d", uint_enum);1404 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1405 // CHECK-FIXES: std::print("{}", static_cast<int>(uint_enum));1406 1407 LongEnum long_enum;1408 printf("%d", long_enum);1409 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1410 // CHECK-FIXES: std::print("{}", static_cast<long>(long_enum));1411 1412 ULongEnum ulong_enum;1413 printf("%d", ulong_enum);1414 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1415 // CHECK-FIXES: std::print("{}", static_cast<long>(ulong_enum));1416 1417 LongLongEnum longlong_enum;1418 printf("%d", longlong_enum);1419 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1420 // CHECK-FIXES: std::print("{}", static_cast<long long>(longlong_enum));1421 1422 ULongLongEnum ulonglong_enum;1423 printf("%d", ulonglong_enum);1424 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1425 // CHECK-FIXES: std::print("{}", static_cast<long long>(ulonglong_enum));1426}1427 1428void printf_enum_u() {1429 PlainEnum plain_enum;1430 printf("%u", plain_enum);1431 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1432 // CHECK-FIXES: std::print("{}", static_cast<unsigned int>(plain_enum));1433 1434 SignedPlainEnum splain_enum;1435 printf("%u", splain_enum);1436 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1437 // CHECK-FIXES: std::print("{}", static_cast<unsigned int>(splain_enum));1438 1439 BoolEnum bool_enum;1440 printf("%u", bool_enum);1441 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1442 // CHECK-FIXES: std::print("{}", static_cast<unsigned int>(bool_enum));1443 1444 CharEnum char_enum;1445 printf("%u", char_enum);1446 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1447 // CHECK-FIXES: std::print("{}", static_cast<unsigned char>(char_enum));1448 1449 SCharEnum schar_enum;1450 printf("%u", schar_enum);1451 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1452 // CHECK-FIXES: std::print("{}", static_cast<unsigned char>(schar_enum));1453 1454 UCharEnum uchar_enum;1455 printf("%u", uchar_enum);1456 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1457 // CHECK-FIXES: std::print("{}", static_cast<unsigned char>(uchar_enum));1458 1459 ShortEnum short_enum;1460 printf("%u", short_enum);1461 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1462 // CHECK-FIXES: std::print("{}", static_cast<unsigned short>(short_enum));1463 1464 UShortEnum ushort_enum;1465 printf("%u", ushort_enum);1466 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1467 // CHECK-FIXES: std::print("{}", static_cast<unsigned short>(ushort_enum));1468 1469 IntEnum int_enum;1470 printf("%u", int_enum);1471 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1472 // CHECK-FIXES: std::print("{}", static_cast<unsigned int>(int_enum));1473 1474 UIntEnum uint_enum;1475 printf("%u", uint_enum);1476 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1477 // CHECK-FIXES: std::print("{}", static_cast<unsigned int>(uint_enum));1478 1479 LongEnum long_enum;1480 printf("%u", long_enum);1481 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1482 // CHECK-FIXES: std::print("{}", static_cast<unsigned long>(long_enum));1483 1484 ULongEnum ulong_enum;1485 printf("%u", ulong_enum);1486 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1487 // CHECK-FIXES: std::print("{}", static_cast<unsigned long>(ulong_enum));1488 1489 LongLongEnum longlong_enum;1490 printf("%u", longlong_enum);1491 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1492 // CHECK-FIXES: std::print("{}", static_cast<unsigned long long>(longlong_enum));1493 1494 ULongLongEnum ulonglong_enum;1495 printf("%u", ulonglong_enum);1496 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1497 // CHECK-FIXES: std::print("{}", static_cast<unsigned long long>(ulonglong_enum));1498}1499 1500void printf_string_function(const char *(*callback)()) {1501 printf("printf string from callback %s", callback());1502 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1503 // CHECK-FIXES: std::print("printf string from callback {}", callback());1504}1505 1506template <typename CharType>1507struct X1508{1509 const CharType *str() const;1510};1511 1512void printf_string_member_function(const X<char> &x, const X<const char> &cx) {1513 printf("printf string from member function %s", x.str());1514 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1515 // CHECK-FIXES: std::print("printf string from member function {}", x.str());1516 1517 printf("printf string from member function on const %s", cx.str());1518 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1519 // CHECK-FIXES: std::print("printf string from member function on const {}", cx.str());1520}1521 1522void printf_string_cstr(const std::string &s1, const std::string &s2) {1523 printf("printf string one c_str %s", s1.c_str());1524 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1525 // CHECK-FIXES: std::print("printf string one c_str {}", s1);1526 1527 printf("printf string two c_str %s %s\n", s1.c_str(), s2.data());1528 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1529 // CHECK-FIXES: std::println("printf string two c_str {} {}", s1, s2);1530}1531 1532void printf_not_char_string_cstr(const std::wstring &ws1) {1533 // This test is to check that we only remove1534 // std::basic_string<CharType>::c_str()/data() when CharType is char. I've1535 // been unable to come up with a genuine situation where someone would have1536 // actually successfully called those methods when this isn't the case without1537 // -Wformat warning, but it seems sensible to restrict removal regardless.1538 printf("printf bogus wstring c_str %s", ws1.c_str());1539 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1540 // CHECK-FIXES: std::print("printf bogus wstring c_str {}", ws1.c_str());1541}1542 1543void fprintf_string_cstr(const std::string &s1) {1544 fprintf(stderr, "fprintf string c_str %s", s1.c_str());1545 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'fprintf' [modernize-use-std-print]1546 // CHECK-FIXES: std::print(stderr, "fprintf string c_str {}", s1);1547}1548 1549void printf_string_pointer_cstr(const std::string *s1, const std::string *s2) {1550 printf("printf string pointer one c_str %s", s1->c_str());1551 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1552 // CHECK-FIXES: std::print("printf string pointer one c_str {}", *s1);1553 1554 printf("printf string pointer two c_str %s %s\n", s1->c_str(), s2->data());1555 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1556 // CHECK-FIXES: std::println("printf string pointer two c_str {} {}", *s1, *s2);1557}1558 1559void fprintf_string_pointer_cstr(const std::string *s1) {1560 fprintf(stderr, "fprintf string pointer c_str %s", s1->c_str());1561 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'fprintf' [modernize-use-std-print]1562 // CHECK-FIXES: std::print(stderr, "fprintf string pointer c_str {}", *s1);1563}1564 1565void printf_iterator_cstr(iterator<std::string> i1, iterator<std::string> i2)1566{1567 printf("printf iterator c_str %s %s\n", i1->c_str(), i2->data());1568 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1569 // CHECK-FIXES: std::println("printf iterator c_str {} {}", *i1, *i2);1570}1571 1572// Something that isn't std::string, so the calls to c_str() and data() must not1573// be removed even though the printf call will be replaced.1574struct S1575{1576 const char *c_str() const;1577 const char *data() const;1578};1579 1580void p(S s1, S *s2)1581{1582 printf("Not std::string %s %s", s1.c_str(), s2->c_str());1583 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1584 // CHECK-FIXES: std::print("Not std::string {} {}", s1.c_str(), s2->c_str());1585 1586 printf("Not std::string %s %s", s1.data(), s2->data());1587 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1588 // CHECK-FIXES: std::print("Not std::string {} {}", s1.data(), s2->data());1589}1590 1591// These need PRI and __PRI prefixes so that the check gets as far as looking1592// for where the macro comes from.1593#define PRI_FMT_MACRO "s"1594#define __PRI_FMT_MACRO "s"1595 1596void macro_expansion(const char *s)1597{1598 const uint64_t u64 = 42;1599 const uint32_t u32 = 32;1600 1601 printf("Replaceable macro at end %" PRIu64, u64);1602 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1603 // CHECK-FIXES: std::print("Replaceable macro at end {}", u64);1604 1605 printf("Replaceable macros in middle %" PRIu64 " %" PRIu32 "\n", u64, u32);1606 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'printf' [modernize-use-std-print]1607 // CHECK-FIXES: std::println("Replaceable macros in middle {} {}", u64, u32);1608 1609 printf("Unreplaceable macro at end %" PRI_FMT_MACRO, s);1610 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro 'PRI_FMT_MACRO' [modernize-use-std-print]1611 1612 printf(PRI_FMT_MACRO " Unreplaceable macro at beginning %s", s);1613 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro 'PRI_FMT_MACRO' [modernize-use-std-print]1614 1615 printf("Unreplacemable macro %" __PRI_FMT_MACRO " in the middle", s);1616 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro '__PRI_FMT_MACRO' [modernize-use-std-print]1617 1618 printf("First macro is replaceable %" PRIu64 " but second one is not %" PRI_FMT_MACRO, u64, s);1619 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro 'PRI_FMT_MACRO' [modernize-use-std-print]1620 1621 // Needs a PRI prefix so that we get as far as looking for where the macro comes from1622 printf(" macro from command line %" PRI_CMDLINE_MACRO, s);1623 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro 'PRI_CMDLINE_MACRO' [modernize-use-std-print]1624 1625 // Needs a __PRI prefix so that we get as far as looking for where the macro comes from1626 printf(" macro from command line %" __PRI_CMDLINE_MACRO, s);1627 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro '__PRI_CMDLINE_MACRO' [modernize-use-std-print]1628 1629 // We ought to be able to fix this since the macro surrounds the whole call1630 // and therefore can't change the format string independently. This is1631 // required to be able to fix calls inside Catch2 macros for example.1632#define SURROUND_ALL(x) x1633 SURROUND_ALL(printf("Macro surrounding entire invocation %" PRIu64, u64));1634 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1635 // CHECK-FIXES: SURROUND_ALL(std::print("Macro surrounding entire invocation {}", u64));1636 1637 // But having that surrounding macro shouldn't stop us ignoring an1638 // unreplaceable macro elsewhere.1639 SURROUND_ALL(printf("Macro surrounding entire invocation with unreplaceable macro %" PRI_FMT_MACRO, s));1640 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro 'PRI_FMT_MACRO' [modernize-use-std-print]1641 1642 // At the moment at least the check will replace occurrences where the1643 // function name is the result of expanding a macro.1644#define SURROUND_FUNCTION_NAME(x) x1645 SURROUND_FUNCTION_NAME(printf)("Hello %d", 4442);1646 // CHECK-MESSAGES: [[@LINE-1]]:26: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]1647 // CHECK-FIXES: std::print("Hello {}", 4442);1648 1649 // We can't safely fix occurrences where the macro may affect the format1650 // string differently in different builds.1651#define SURROUND_FORMAT(x) "!" x1652 printf(SURROUND_FORMAT("Hello %d"), 4443);1653 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: unable to use 'std::print' instead of 'printf' because format string contains unreplaceable macro 'SURROUND_FORMAT' [modernize-use-std-print]1654}1655