[PATCH] D149280: [clang-tidy] Add modernize-printf-to-std-print check

Mike Crowe via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 26 12:09:24 PDT 2023


mikecrowe created this revision.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
mikecrowe requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Add FormatStringConverter utility class that is capable of converting
printf-style format strings into std::format-style format strings along
with recording a set of casts to wrap the arguments as required.

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

In other words, the check turns:

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

into:

std::print(stderr, "The {} is {:3}\n", answer, value);

if it can.

std::print 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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149280

Files:
  clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
  clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
  clang-tools-extra/clang-tidy/modernize/PrintfToStdPrintCheck.cpp
  clang-tools-extra/clang-tidy/modernize/PrintfToStdPrintCheck.h
  clang-tools-extra/clang-tidy/utils/CMakeLists.txt
  clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
  clang-tools-extra/clang-tidy/utils/FormatStringConverter.h
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/modernize/printf-to-std-print.rst
  clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/cstdio
  clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/stdio.h
  clang-tools-extra/test/clang-tidy/checkers/modernize/printf-to-fmt-print-convert.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/printf-to-std-print-convert-absl.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/printf-to-std-print-convert-custom.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/printf-to-std-print-convert.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149280.517262.patch
Type: text/x-patch
Size: 96366 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230426/57d8511e/attachment-0001.bin>


More information about the cfe-commits mailing list