1// RUN: %check_clang_tidy \
2// RUN: -std=c++23 %s modernize-use-std-print %t -- \
3// RUN: -config="{CheckOptions: {StrictMode: true}}" \
4// RUN: -- -isystem %clang_tidy_headers
5// RUN: %check_clang_tidy \
6// RUN: -std=c++23 %s modernize-use-std-print %t -- \
7// RUN: -config="{CheckOptions: {StrictMode: false}}" \
8// RUN: -- -isystem %clang_tidy_headers
9
10#include <cstdio>
11#include <string.h>
12
13namespace absl
14{
15// Use const char * for the format since the real type is hard to mock up.
16template <typename... Args>
17int PrintF(const char *format, const Args&... args);
18
19template <typename... Args>
20int FPrintF(FILE* output, const char *format, const Args&... args);
21}
22
23void printf_simple() {
24 absl::PrintF(format: "Hello %s %d", args: "world", args: 42);
25 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'PrintF' [modernize-use-std-print]
26 // CHECK-FIXES: std::print("Hello {} {}", "world", 42);
27}
28
29void printf_newline() {
30 absl::PrintF(format: "Hello %s %d\n", args: "world", args: 42);
31 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'PrintF' [modernize-use-std-print]
32 // CHECK-FIXES: std::println("Hello {} {}", "world", 42);
33
34 using namespace absl;
35 PrintF(format: "Hello %s %d\n", args: "world", args: 42);
36 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'PrintF' [modernize-use-std-print]
37 // CHECK-FIXES: std::println("Hello {} {}", "world", 42);
38}
39
40// absl uses the type of the argument rather than the format string, so unsigned
41// types will be printed as unsigned even if the format string indicates signed
42// and vice-versa. This is exactly what std::print will do too, so no casts are
43// required.
44void printf_no_casts_in_strict_mode() {
45 using namespace absl;
46
47 const unsigned short us = 42U;
48 PrintF(format: "Integer %hd from unsigned short\n", args: us);
49 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'PrintF' [modernize-use-std-print]
50 // CHECK-FIXES: std::println("Integer {} from unsigned short", us);
51
52 const short s = 42;
53 PrintF(format: "Unsigned integer %hu from short\n", args: s);
54 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'PrintF' [modernize-use-std-print]
55 // CHECK-FIXES: std::println("Unsigned integer {} from short", s);
56}
57
58int printf_uses_return_value(int i) {
59 using namespace absl;
60
61 return PrintF(format: "return value %d\n", args: i);
62}
63
64void fprintf_simple(FILE *fp) {
65 absl::FPrintF(output: fp, format: "Hello %s %d", args: "world", args: 42);
66 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'FPrintF' [modernize-use-std-print]
67 // CHECK-FIXES: std::print(fp, "Hello {} {}", "world", 42);
68}
69
70void fprintf_newline(FILE *fp) {
71 absl::FPrintF(output: fp, format: "Hello %s %d\n", args: "world", args: 42);
72 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'FPrintF' [modernize-use-std-print]
73 // CHECK-FIXES: std::println(fp, "Hello {} {}", "world", 42);
74
75 using namespace absl;
76 FPrintF(output: fp, format: "Hello %s %d\n", args: "world", args: 42);
77 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'FPrintF' [modernize-use-std-print]
78 // CHECK-FIXES: std::println(fp, "Hello {} {}", "world", 42);
79}
80
81void fprintf_no_casts_in_strict_mode(FILE *fp) {
82 using namespace absl;
83
84 const unsigned short us = 42U;
85 FPrintF(output: fp, format: "Integer %hd from unsigned short\n", args: us);
86 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'FPrintF' [modernize-use-std-print]
87 // CHECK-FIXES: std::println(fp, "Integer {} from unsigned short", us);
88
89 const short s = 42;
90 FPrintF(output: fp, format: "Unsigned integer %hu from short\n", args: s);
91 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'FPrintF' [modernize-use-std-print]
92 // CHECK-FIXES: std::println(fp, "Unsigned integer {} from short", s);
93}
94
95int fprintf_uses_return_value(FILE *fp, int i) {
96 using namespace absl;
97
98 return FPrintF(output: fp, format: "return value %d\n", args: i);
99}
100

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-absl.cpp