xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format-custom.cpp (revision 666d224248707f373577b5b049b5b0229100006c)
1af79372dSMike Crowe // RUN: %check_clang_tidy -check-suffixes=,STRICT                       \
2af79372dSMike Crowe // RUN:   -std=c++20 %s modernize-use-std-format %t --                  \
3af79372dSMike Crowe // RUN:   -config="{CheckOptions: {                                     \
4af79372dSMike Crowe // RUN:              modernize-use-std-format.StrictMode: true,         \
50e62d5cfSMike Crowe // RUN:              modernize-use-std-format.StrFormatLikeFunctions: '::strprintf; mynamespace::strprintf2; bad_format_type_strprintf', \
6af79372dSMike Crowe // RUN:              modernize-use-std-format.ReplacementFormatFunction: 'fmt::format', \
7af79372dSMike Crowe // RUN:              modernize-use-std-format.FormatHeader: '<fmt/core.h>' \
8af79372dSMike Crowe // RUN:            }}"                                                  \
9af79372dSMike Crowe // RUN:   -- -isystem %clang_tidy_headers
10af79372dSMike Crowe // RUN: %check_clang_tidy -check-suffixes=,NOTSTRICT                    \
11af79372dSMike Crowe // RUN:   -std=c++20 %s modernize-use-std-format %t --                  \
12af79372dSMike Crowe // RUN:   -config="{CheckOptions: {                                     \
130e62d5cfSMike Crowe // RUN:              modernize-use-std-format.StrFormatLikeFunctions: '::strprintf; mynamespace::strprintf2; bad_format_type_strprintf', \
14af79372dSMike Crowe // RUN:              modernize-use-std-format.ReplacementFormatFunction: 'fmt::format', \
15af79372dSMike Crowe // RUN:              modernize-use-std-format.FormatHeader: '<fmt/core.h>' \
16af79372dSMike Crowe // RUN:            }}"                                                  \
17af79372dSMike Crowe // RUN:   -- -isystem %clang_tidy_headers
18af79372dSMike Crowe 
19af79372dSMike Crowe #include <cstdio>
20af79372dSMike Crowe #include <string>
21af79372dSMike Crowe // CHECK-FIXES: #include <fmt/core.h>
22af79372dSMike Crowe 
23af79372dSMike Crowe std::string strprintf(const char *, ...);
24af79372dSMike Crowe 
25af79372dSMike Crowe namespace mynamespace {
26af79372dSMike Crowe   std::string strprintf2(const char *, ...);
27af79372dSMike Crowe }
28af79372dSMike Crowe 
29af79372dSMike Crowe std::string strprintf_test(const std::string &name, double value) {
30af79372dSMike Crowe   return strprintf("'%s'='%f'\n", name.c_str(), value);
31af79372dSMike Crowe   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'fmt::format' instead of 'strprintf' [modernize-use-std-format]
32af79372dSMike Crowe   // CHECK-FIXES: return fmt::format("'{}'='{:f}'\n", name, value);
33af79372dSMike Crowe 
34af79372dSMike Crowe   return mynamespace::strprintf2("'%s'='%f'\n", name.c_str(), value);
35af79372dSMike Crowe   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'fmt::format' instead of 'strprintf2' [modernize-use-std-format]
36af79372dSMike Crowe   // CHECK-FIXES: return fmt::format("'{}'='{:f}'\n", name, value);
37af79372dSMike Crowe }
38af79372dSMike Crowe 
39af79372dSMike Crowe std::string StrFormat_strict_conversion() {
40af79372dSMike Crowe   const unsigned char uc = 'A';
41af79372dSMike Crowe   return strprintf("Integer %hhd from unsigned char\n", uc);
42af79372dSMike Crowe   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'fmt::format' instead of 'strprintf' [modernize-use-std-format]
43af79372dSMike Crowe   // CHECK-FIXES-NOTSTRICT: return fmt::format("Integer {} from unsigned char\n", uc);
44af79372dSMike Crowe   // CHECK-FIXES-STRICT: return fmt::format("Integer {} from unsigned char\n", static_cast<signed char>(uc));
45af79372dSMike Crowe }
46af79372dSMike Crowe 
47af79372dSMike Crowe // Ensure that MatchesAnyListedNameMatcher::NameMatcher::match() can cope with a
48af79372dSMike Crowe // NamedDecl that has no name when we're trying to match unqualified_strprintf.
49af79372dSMike Crowe std::string A(const std::string &in)
50af79372dSMike Crowe {
51af79372dSMike Crowe     return "_" + in;
52af79372dSMike Crowe }
530e62d5cfSMike Crowe 
540e62d5cfSMike Crowe // Issue #92896: Ensure that the check doesn't assert if the argument is
550e62d5cfSMike Crowe // promoted to something that isn't a string.
560e62d5cfSMike Crowe struct S {
570e62d5cfSMike Crowe   S(...);
580e62d5cfSMike Crowe };
590e62d5cfSMike Crowe std::string bad_format_type_strprintf(const S &, ...);
600e62d5cfSMike Crowe 
610e62d5cfSMike Crowe std::string unsupported_format_parameter_type()
620e62d5cfSMike Crowe {
630e62d5cfSMike Crowe   // No fixes here because the format parameter of the function called is not a
640e62d5cfSMike Crowe   // string.
650e62d5cfSMike Crowe   return bad_format_type_strprintf("");
66*666d2242SMike Crowe // CHECK-MESSAGES: [[@LINE-1]]:10: warning: unable to use 'fmt::format' instead of 'bad_format_type_strprintf' because first argument is not a narrow string literal [modernize-use-std-format]
670e62d5cfSMike Crowe }
68