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