This revision was automatically updated to reflect the committed changes. Closed by commit rG2ce765eb2f4f: [clang-tidy] Don't split \r\n in modernize-use-std-print check (authored by mikecrowe, committed by PiotrZSL).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D154788/new/ https://reviews.llvm.org/D154788 Files: clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp +++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp @@ -44,6 +44,16 @@ // 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) { Index: clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst +++ clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst @@ -70,8 +70,10 @@ `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 *``. Index: clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp =================================================================== --- clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp +++ clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp @@ -623,8 +623,11 @@ 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,
Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp +++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print.cpp @@ -44,6 +44,16 @@ // 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) { Index: clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst +++ clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst @@ -70,8 +70,10 @@ `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 *``. Index: clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp =================================================================== --- clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp +++ clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp @@ -623,8 +623,11 @@ 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,
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits