[PATCH] D148318: [clang-tidy] Add `performance-avoid-endl` check
Piotr Zegar via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 14 07:41:30 PDT 2023
PiotrZSL added a comment.
Consider extending this check to suport also std::ends, maybe name it performance-avoid-endl-ends.
================
Comment at: clang-tools-extra/clang-tidy/performance/DontUseEndlCheck.cpp:26
+ callee(cxxMethodDecl(ofClass(
+ hasAnyName("::std::basic_ostream", "::std::basic_iostream")))),
+ hasDescendant(
----------------
unnecessary limit... (in my project we use custom stream class for logging).
```
Finder->addMatcher(cxxOperatorCallExpr(unless(isExpansionInSystemHeader()),
hasOverloadedOperatorName("<<"),
unless(isMacroExpansion()),
hasRHS(ignoringImplicit(declRefExpr(to(namedDecl(hasAnyName("endl", "ends")).bind("decl"))).bind("expr")))
```
something like this should be sufficient...
If you do not plan to remove restriction for basic_ostream, make it configurable.
And other problem is that some << operators does not need to be methods, they can be functions, in such case you may run into issues, but you could just read of type from expr... instead processing argument, or class.
Like ```cxxOperatorCallExpr(hasType(references(cxxRecordDecl(....```
================
Comment at: clang-tools-extra/clang-tidy/performance/DontUseEndlCheck.cpp:44
+ Diag << FixItHint::CreateReplacement(
+ CharSourceRange::getCharRange(EndlCall->getSourceRange()), "'\\n'");
+}
----------------
AMS21 wrote:
> This doesn't quite work and I'm not sure why or what would work. Any help would be appreciated.
>
> Report for this like
> ```cpp
> std::cout << std::endl;
> ```
> looks like this:
> ```
> std::cout << std::endl;
> ^~~~~
> '\n'
> ```
>
> So the start location is correct but the end is not.
Use getTokenRange
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D148318/new/
https://reviews.llvm.org/D148318
More information about the cfe-commits
mailing list