1// RUN: %check_clang_tidy -std=c++23 %s modernize-use-std-print %t -- \
2// RUN: -config="{CheckOptions: \
3// RUN: { \
4// RUN: modernize-use-std-print.PrintfLikeFunctions: 'unqualified_printf;::myprintf; mynamespace::myprintf2', \
5// RUN: modernize-use-std-print.FprintfLikeFunctions: '::myfprintf; mynamespace::myfprintf2' \
6// RUN: } \
7// RUN: }" \
8// RUN: -- -isystem %clang_tidy_headers
9
10#include <cstdio>
11#include <string>
12
13int myprintf(const char *, ...);
14int myfprintf(FILE *fp, const char *, ...);
15
16namespace mynamespace {
17int myprintf2(const char *, ...);
18int myfprintf2(FILE *fp, const char *, ...);
19}
20
21void printf_simple() {
22 myprintf("Hello %s %d", "world", 42);
23 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'myprintf' [modernize-use-std-print]
24 // CHECK-FIXES: std::print("Hello {} {}", "world", 42);
25}
26
27void printf_newline() {
28 myprintf("Hello %s %d\n", "world", 42);
29 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'myprintf' [modernize-use-std-print]
30 // CHECK-FIXES: std::println("Hello {} {}", "world", 42);
31
32 mynamespace::myprintf2("Hello %s %d\n", "world", 42);
33 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'myprintf2' [modernize-use-std-print]
34 // CHECK-FIXES: std::println("Hello {} {}", "world", 42);
35
36 using mynamespace::myprintf2;
37 myprintf2("Hello %s %d\n", "world", 42);
38 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::println' instead of 'myprintf2' [modernize-use-std-print]
39 // CHECK-FIXES: std::println("Hello {} {}", "world", 42);
40
41 // When using custom options leave printf alone
42 printf(format: "Hello %s %d\n", "world", 42);
43}
44
45int printf_uses_return_value(int i) {
46 return myprintf("return value %d\n", i);
47 // CHECK-MESSAGES-NOT: [[@LINE-1]]:10: warning: use 'std::println' instead of 'myprintf' [modernize-use-std-print]
48 // CHECK-FIXES-NOT: std::println("return value {}", i);
49}
50
51void 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
58void 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, format: "Hello %s %d\n", "world", 42);
75}
76
77int fprintf_uses_return_value(int i) {
78 return myfprintf(stderr, "return value %d\n", i);
79 // CHECK-MESSAGES-NOT: [[@LINE-1]]:10: warning: use 'std::println' instead of 'myprintf' [modernize-use-std-print]
80 // CHECK-FIXES-NOT: std::println(stderr, "return value {}", i);
81}
82
83// Ensure that MatchesAnyListedNameMatcher::NameMatcher::match() can cope with a
84// NamedDecl that has no name when we're trying to match unqualified_printf.
85void no_name(const std::string &in)
86{
87 "A" + in;
88}
89

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