[clang-tidy] Add modernize-printf-to-std-print check
authorMike Crowe <mac@mcrowe.com>
Mon, 26 Jun 2023 05:44:14 +0000 (05:44 +0000)
committerPiotr Zegar <me@piotrzegar.pl>
Mon, 26 Jun 2023 09:05:06 +0000 (09:05 +0000)
commitec89cb9a81529fd41fb37b8e62203a2e9f23bd54
tree999ba543330f030beca8b59f573ab40f563ca957
parent3b85be3df23cfb8fb4d1f0656eac3214cf400c72
[clang-tidy] Add modernize-printf-to-std-print check

Add FormatStringConverter utility class that is capable of converting
printf-style format strings into std::print-style format strings along
with recording a set of casts to wrap the arguments as required and
removing now-unnecessary calls to std::string::c_str() and
std::string::data()

Use FormatStringConverter to implement a new clang-tidy check that is
capable of converting calls to printf, fprintf, absl::PrintF,
absl::FPrintF, or any functions configured by an option to calls to
std::print and std::println, or other functions configured by options.

In other words, the check turns:

 fprintf(stderr, "The %s is %3d\n", description.c_str(), value);

into:

 std::println(stderr, "The {} is {:3}", description, value);

if it can.

std::print and std::println can do almost anything that standard printf
can, but the conversion has some some limitations that are described in
the documentation. If conversion is not possible then the call remains
unchanged.

Depends on D153716

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D149280
20 files changed:
clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.cpp [new file with mode: 0644]
clang-tools-extra/clang-tidy/modernize/UseStdPrintCheck.h [new file with mode: 0644]
clang-tools-extra/clang-tidy/utils/CMakeLists.txt
clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp [new file with mode: 0644]
clang-tools-extra/clang-tidy/utils/FormatStringConverter.h [new file with mode: 0644]
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/list.rst
clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/cstddef [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/cstdint [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/cstdio [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/inttypes.h [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/stdio.h
clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string.h
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-absl.cpp [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-fmt.cpp [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp [new file with mode: 0644]