[clang-tools-extra] [clang-tidy] add default error message for performance-avoid-endl (PR #107867)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 9 16:21:17 PDT 2024
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/107867
>From 31b01c0cd3fc809e5451853b820d021dd43d16c7 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Mon, 9 Sep 2024 22:32:48 +0800
Subject: [PATCH 1/2] [clang-tidy] add default error message for
performance-avoid-endl
use ::std::endl as default message when matched expr does not have valid source text
---
.../clang-tidy/performance/AvoidEndlCheck.cpp | 13 ++++++-------
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
.../clang-tidy/checkers/performance/avoid-endl.cpp | 11 +++++++++++
3 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp b/clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp
index 8ecaa41754fb2b..4cfe1ca49f317c 100644
--- a/clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp
@@ -46,14 +46,13 @@ void AvoidEndlCheck::check(const MatchFinder::MatchResult &Result) {
// Handle the more common streaming '... << std::endl' case
const CharSourceRange TokenRange =
CharSourceRange::getTokenRange(Expression->getSourceRange());
- const StringRef SourceText = Lexer::getSourceText(
+ StringRef SourceText = Lexer::getSourceText(
TokenRange, *Result.SourceManager, Result.Context->getLangOpts());
-
- auto Diag = diag(Expression->getBeginLoc(),
- "do not use '%0' with streams; use '\\n' instead")
- << SourceText;
-
- Diag << FixItHint::CreateReplacement(TokenRange, "'\\n'");
+ if (SourceText.empty())
+ SourceText = "::std::endl";
+ diag(Expression->getBeginLoc(),
+ "do not use '%0' with streams; use '\\n' instead")
+ << SourceText << FixItHint::CreateReplacement(TokenRange, "'\\n'");
} else {
// Handle the less common function call 'std::endl(...)' case
const auto *CallExpression = llvm::cast<CallExpr>(Expression);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 8d028f8863cb7a..6768e243add997 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -120,6 +120,10 @@ Changes in existing checks
<clang-tidy/checks/modernize/use-std-print>` check to support replacing
member function calls too.
+- Improved :doc:`performance-avoid-endl
+ <clang-tidy/checks/performance/avoid-endl>` check by fixing incorrect
+ message.
+
- Improved :doc:`readablility-implicit-bool-conversion
<clang-tidy/checks/readability/implicit-bool-conversion>` check
by adding the option `UseUpperCaseLiteralSuffix` to select the
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/avoid-endl.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/avoid-endl.cpp
index 462b88773d231c..dd2d61a78d7965 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/avoid-endl.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/avoid-endl.cpp
@@ -225,3 +225,14 @@ void bad_custom_stream() {
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
// CHECK-FIXES: logger << '\n';
}
+
+namespace gh107859 {
+
+#define ENDL std::endl;
+
+void bad_macro() {
+ std::cout << ENDL;
+ // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use '::std::endl' with streams; use '\n' instead [performance-avoid-endl]
+}
+
+} // namespace gh107859
>From 0af236e834483ee29189a4e3fa566647b8dea5a3 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 10 Sep 2024 07:21:10 +0800
Subject: [PATCH 2/2] Update
clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp
---
clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp b/clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp
index 4cfe1ca49f317c..bc12b9c73c0d3c 100644
--- a/clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp
@@ -49,7 +49,7 @@ void AvoidEndlCheck::check(const MatchFinder::MatchResult &Result) {
StringRef SourceText = Lexer::getSourceText(
TokenRange, *Result.SourceManager, Result.Context->getLangOpts());
if (SourceText.empty())
- SourceText = "::std::endl";
+ SourceText = "std::endl";
diag(Expression->getBeginLoc(),
"do not use '%0' with streams; use '\\n' instead")
<< SourceText << FixItHint::CreateReplacement(TokenRange, "'\\n'");
More information about the cfe-commits
mailing list