xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp (revision 43fa7ea9df3a7b3bdc9d0129bcc3bed4db52dba0)
1 // RUN: %check_clang_tidy -std=c++23 %s modernize-use-std-print %t -- \
2 // RUN:   -config="{CheckOptions: \
3 // RUN:             [ \
4 // RUN:              { \
5 // RUN:               key: modernize-use-std-print.PrintfLikeFunctions, \
6 // RUN:               value: '::myprintf; mynamespace::myprintf2' \
7 // RUN:              }, \
8 // RUN:              { \
9 // RUN:               key: modernize-use-std-print.FprintfLikeFunctions, \
10 // RUN:               value: '::myfprintf; mynamespace::myfprintf2' \
11 // RUN:              } \
12 // RUN:             ] \
13 // RUN:            }" \
14 // RUN:   -- -isystem %clang_tidy_headers
15 
16 #include <cstdio>
17 #include <string.h>
18 
19 int myprintf(const char *, ...);
20 int myfprintf(FILE *fp, const char *, ...);
21 
22 namespace mynamespace {
23 int myprintf2(const char *, ...);
24 int myfprintf2(FILE *fp, const char *, ...);
25 }
26 
27 void printf_simple() {
28   myprintf("Hello %s %d", "world", 42);
29   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'myprintf' [modernize-use-std-print]
30   // CHECK-FIXES: std::print("Hello {} {}", "world", 42);
31 }
32 
33 void printf_newline() {
34   myprintf("Hello %s %d\n", "world", 42);
35   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'myprintf' [modernize-use-std-print]
36   // CHECK-FIXES: std::println("Hello {} {}", "world", 42);
37 
38   mynamespace::myprintf2("Hello %s %d\n", "world", 42);
39   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'myprintf2' [modernize-use-std-print]
40   // CHECK-FIXES: std::println("Hello {} {}", "world", 42);
41 
42   using mynamespace::myprintf2;
43   myprintf2("Hello %s %d\n", "world", 42);
44   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'myprintf2' [modernize-use-std-print]
45   // CHECK-FIXES: std::println("Hello {} {}", "world", 42);
46 
47   // When using custom options leave printf alone
48   printf("Hello %s %d\n", "world", 42);
49 }
50 
51 void fprintf_simple(FILE *fp)
52 {
53   myfprintf(stderr, "Hello %s %d", "world", 42);
54   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'myfprintf' [modernize-use-std-print]
55   // CHECK-FIXES: std::print(stderr, "Hello {} {}", "world", 42);
56 }
57 
58 void fprintf_newline(FILE *fp)
59 {
60   myfprintf(stderr, "Hello %s %d\n", "world", 42);
61   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'myfprintf' [modernize-use-std-print]
62   // CHECK-FIXES: std::println(stderr, "Hello {} {}", "world", 42);
63 
64   mynamespace::myfprintf2(stderr, "Hello %s %d\n", "world", 42);
65   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'myfprintf2' [modernize-use-std-print]
66   // CHECK-FIXES: std::println(stderr, "Hello {} {}", "world", 42);
67 
68   using mynamespace::myfprintf2;
69   myfprintf2(stderr, "Hello %s %d\n", "world", 42);
70   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'myfprintf2' [modernize-use-std-print]
71   // CHECK-FIXES: std::println(stderr, "Hello {} {}", "world", 42);
72 
73   // When using custom options leave fprintf alone
74   fprintf(stderr, "Hello %s %d\n", "world", 42);
75 }
76