Skip to content

Commit

Permalink
[clang-tidy] Don't split \r\n in modernize-use-std-print check
Browse files Browse the repository at this point in the history
When given:
 printf("Hello\r\n");

it's clearer to leave the CRLF intact and convert this to:
 std::print("Hello\r\n");

than to remove the trailing newline and convert it to:
 std::println("Hello\r");

Update the documentation to match, and clarify the situations for using
println vs print which weren't previously explained.

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D154788
  • Loading branch information
mikecrowe authored and PiotrZSL committed Jul 11, 2023
1 parent d7e79bd commit 2ce765e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 4 additions & 1 deletion clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,11 @@ void FormatStringConverter::finalizeFormatText() {
PrintfFormatString.size() - PrintfFormatStringPos));
PrintfFormatStringPos = PrintfFormatString.size();

// It's clearer to convert printf("Hello\r\n"); to std::print("Hello\r\n")
// than to std::println("Hello\r");
if (StringRef(StandardFormatString).ends_with("\\n") &&
!StringRef(StandardFormatString).ends_with("\\\\n")) {
!StringRef(StandardFormatString).ends_with("\\\\n") &&
!StringRef(StandardFormatString).ends_with("\\r\\n")) {
UsePrintNewlineFunction = true;
FormatStringNeededRewriting = true;
StandardFormatString.erase(StandardFormatString.end() - 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ If the call is deemed suitable for conversion then:
`FprintfLikeFunctions` are replaced with the function specified by the
`ReplacementPrintlnFunction` option if the format string ends with ``\n``
or `ReplacementPrintFunction` otherwise.
- the format string is rewritten to use the ``std::formatter`` language and
a ``\n`` is removed from the end.
- the format string is rewritten to use the ``std::formatter`` language. If
a ``\n`` is found at the end of the format string not preceded by ``r``
then it is removed and `ReplacementPrintlnFunction` is used rather than
`ReplacementPrintFunction`.
- any arguments that corresponded to ``%p`` specifiers that
``std::formatter`` wouldn't accept are wrapped in a ``static_cast``
to ``const void *``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ void printf_deceptive_newline() {
// CHECK-FIXES: std::println("Hello");
}

void printf_crlf_newline() {
printf("Hello\r\n");
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]
// CHECK-FIXES: std::print("Hello\r\n");

printf("Hello\r\\n");
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: use 'std::print' instead of 'printf' [modernize-use-std-print]
// CHECK-FIXES: std::print("Hello\r\\n");
}

// std::print returns nothing, so any callers that use the return
// value cannot be automatically translated.
int printf_uses_return_value(int choice) {
Expand Down

0 comments on commit 2ce765e

Please sign in to comment.