xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-member.cpp (revision bbcb625798514f1cd6ef04818381d38ea26b23e5)
1 // RUN: %check_clang_tidy %s modernize-use-std-print %t -- \
2 // RUN:   -config="{CheckOptions: \
3 // RUN:             { \
4 // RUN:               modernize-use-std-print.PrintfLikeFunctions: 'MyClass::printf', \
5 // RUN:               modernize-use-std-print.FprintfLikeFunctions: 'MyClass::fprintf', \
6 // RUN:               modernize-use-std-print.ReplacementPrintFunction: 'print', \
7 // RUN:               modernize-use-std-print.ReplacementPrintlnFunction: 'println', \
8 // RUN:             } \
9 // RUN:            }" \
10 // RUN:   -- -isystem %clang_tidy_headers
11 
12 #include <cstdio>
13 #include <string.h>
14 
15 struct MyStruct {};
16 
17 struct MyClass
18 {
19   template <typename... Args>
20   void printf(const char *fmt, Args &&...);
21   template <typename... Args>
22   int fprintf(MyStruct *param1, const char *fmt, Args &&...);
23 };
24 
25 void printf_simple(MyClass &myclass, MyClass *pmyclass) {
26   myclass.printf("printf dot %d", 42);
27   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'print' instead of 'printf' [modernize-use-std-print]
28   // CHECK-FIXES: myclass.print("printf dot {}", 42);
29 
30   pmyclass->printf("printf pointer %d", 43);
31   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'print' instead of 'printf' [modernize-use-std-print]
32   // CHECK-FIXES: pmyclass->print("printf pointer {}", 43);
33 
34   (*pmyclass).printf("printf deref pointer %d", 44);
35   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'print' instead of 'printf' [modernize-use-std-print]
36   // CHECK-FIXES: (*pmyclass).print("printf deref pointer {}", 44);
37 }
38 
39 void printf_newline(MyClass &myclass, MyClass *pmyclass) {
40   myclass.printf("printf dot newline %c\n", 'A');
41   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'println' instead of 'printf' [modernize-use-std-print]
42   // CHECK-FIXES: myclass.println("printf dot newline {}", 'A');
43 
44   pmyclass->printf("printf pointer newline %c\n", 'B');
45   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'println' instead of 'printf' [modernize-use-std-print]
46   // CHECK-FIXES: pmyclass->println("printf pointer newline {}", 'B');
47 }
48 
49 void fprintf_simple(MyStruct *mystruct, MyClass &myclass, MyClass *pmyclass) {
50   myclass.fprintf(mystruct, "fprintf dot %d", 142);
51   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'print' instead of 'fprintf' [modernize-use-std-print]
52   // CHECK-FIXES: myclass.print(mystruct, "fprintf dot {}", 142);
53 
54   pmyclass->fprintf(mystruct, "fprintf pointer %d", 143);
55   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'print' instead of 'fprintf' [modernize-use-std-print]
56   // CHECK-FIXES: pmyclass->print(mystruct, "fprintf pointer {}", 143);
57 
58   (*pmyclass).fprintf(mystruct, "fprintf deref pointer %d", 144);
59   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'print' instead of 'fprintf' [modernize-use-std-print]
60   // CHECK-FIXES: (*pmyclass).print(mystruct, "fprintf deref pointer {}", 144);
61 }
62 
63 struct MyDerivedClass : public MyClass {};
64 
65 void printf_derived(MyDerivedClass &derived)
66 {
67   derived.printf("printf on derived class %d", 42);
68   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'print' instead of 'printf' [modernize-use-std-print]
69   // CHECK-FIXES: derived.print("printf on derived class {}", 42);
70 }
71