Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] add default error message for performance-avoid-endl #107867

9 changes: 5 additions & 4 deletions clang-tools-extra/clang-tidy/performance/AvoidEndlCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ 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());

if (SourceText.empty())
SourceText = "std::endl";
auto Diag = diag(Expression->getBeginLoc(),
"do not use '%0' with streams; use '\\n' instead")
<< SourceText;

Diag << FixItHint::CreateReplacement(TokenRange, "'\\n'");
if (TokenRange.isValid())
Diag << FixItHint::CreateReplacement(TokenRange, "'\\n'");
HerrCai0907 marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Handle the less common function call 'std::endl(...)' case
const auto *CallExpression = llvm::cast<CallExpr>(Expression);
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

HerrCai0907 marked this conversation as resolved.
Show resolved Hide resolved
- Improved :doc:`readablility-implicit-bool-conversion
<clang-tidy/checks/readability/implicit-bool-conversion>` check
by adding the option `UseUpperCaseLiteralSuffix` to select the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
SimplyDanny marked this conversation as resolved.
Show resolved Hide resolved
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
}

} // namespace gh107859
Loading