-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang-tidy] Enhance modernize-use-starts-ends-with to handle substr patterns #116033
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
Changes from all commits
23b4bcd
f2dc9b2
de9d062
f948b16
79838fc
dcb89af
115a760
3bed9be
522d4b3
e03d67e
26ebe24
01bc74c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,17 @@ | |
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::modernize { | ||
|
||
static bool isNegativeComparison(const Expr *ComparisonExpr) { | ||
if (const auto *BO = llvm::dyn_cast<BinaryOperator>(ComparisonExpr)) | ||
return BO->getOpcode() == BO_NE; | ||
|
||
if (const auto *Op = llvm::dyn_cast<CXXOperatorCallExpr>(ComparisonExpr)) | ||
return Op->getOperator() == OO_ExclaimEqual; | ||
|
||
return false; | ||
} | ||
|
||
struct NotLengthExprForStringNode { | ||
NotLengthExprForStringNode(std::string ID, DynTypedNode Node, | ||
ASTContext *Context) | ||
|
@@ -171,10 +182,26 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) { | |
hasRHS(lengthExprForStringNode("needle"))))) | ||
.bind("expr"), | ||
this); | ||
|
||
// Case 6: X.substr(0, LEN(Y)) [!=]= Y -> starts_with. | ||
Finder->addMatcher( | ||
cxxOperatorCallExpr( | ||
hasAnyOperatorName("==", "!="), | ||
hasOperands( | ||
expr().bind("needle"), | ||
cxxMemberCallExpr( | ||
argumentCountIs(2), hasArgument(0, ZeroLiteral), | ||
hasArgument(1, lengthExprForStringNode("needle")), | ||
callee(cxxMethodDecl(hasName("substr"), | ||
ofClass(OnClassWithStartsWithFunction)) | ||
.bind("find_fun"))) | ||
.bind("find_expr"))) | ||
.bind("expr"), | ||
this); | ||
} | ||
|
||
void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) { | ||
const auto *ComparisonExpr = Result.Nodes.getNodeAs<BinaryOperator>("expr"); | ||
const auto *ComparisonExpr = Result.Nodes.getNodeAs<Expr>("expr"); | ||
const auto *FindExpr = Result.Nodes.getNodeAs<CXXMemberCallExpr>("find_expr"); | ||
const auto *FindFun = Result.Nodes.getNodeAs<CXXMethodDecl>("find_fun"); | ||
const auto *SearchExpr = Result.Nodes.getNodeAs<Expr>("needle"); | ||
|
@@ -183,39 +210,42 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) { | |
const auto *EndsWithFunction = | ||
Result.Nodes.getNodeAs<CXXMethodDecl>("ends_with_fun"); | ||
assert(bool(StartsWithFunction) != bool(EndsWithFunction)); | ||
|
||
const CXXMethodDecl *ReplacementFunction = | ||
StartsWithFunction ? StartsWithFunction : EndsWithFunction; | ||
|
||
if (ComparisonExpr->getBeginLoc().isMacroID()) | ||
if (ComparisonExpr->getBeginLoc().isMacroID() || | ||
FindExpr->getBeginLoc().isMacroID()) | ||
return; | ||
|
||
const bool Neg = ComparisonExpr->getOpcode() == BO_NE; | ||
// Make sure FindExpr->getArg(0) can be used to make a range in the FitItHint. | ||
if (FindExpr->getNumArgs() == 0) | ||
hjanuschka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
|
||
auto Diagnostic = | ||
diag(FindExpr->getExprLoc(), "use %0 instead of %1() %select{==|!=}2 0") | ||
<< ReplacementFunction->getName() << FindFun->getName() << Neg; | ||
// Retrieve the source text of the search expression. | ||
const auto SearchExprText = Lexer::getSourceText( | ||
CharSourceRange::getTokenRange(SearchExpr->getSourceRange()), | ||
*Result.SourceManager, Result.Context->getLangOpts()); | ||
Comment on lines
+225
to
+228
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Getting the source text is a bit unfortunate, as this will expand macros, but I don't think it can be avoided because it is needed to be able to swap the operator sides. E.g.: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so we can keep it?
shouldn't that skip macros? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are probably edge cases which I don't expect to be present in real code but maybe I'll be proven wrong. I also think grabbing source text of either the haystack or needle is necessary here because of the possible swap. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Only at the checked positions. E.g., the needle may still be a macro (as per the test you've added from my comment). |
||
|
||
// Remove possible arguments after search expression and ' [!=]= .+' suffix. | ||
Diagnostic << FixItHint::CreateReplacement( | ||
CharSourceRange::getTokenRange( | ||
Lexer::getLocForEndOfToken(SearchExpr->getEndLoc(), 0, | ||
*Result.SourceManager, getLangOpts()), | ||
ComparisonExpr->getEndLoc()), | ||
")"); | ||
auto Diagnostic = diag(FindExpr->getExprLoc(), "use %0 instead of %1") | ||
<< ReplacementFunction->getName() << FindFun->getName(); | ||
|
||
// Remove possible '.+ [!=]= ' prefix. | ||
// Remove everything before the function call. | ||
Diagnostic << FixItHint::CreateRemoval(CharSourceRange::getCharRange( | ||
ComparisonExpr->getBeginLoc(), FindExpr->getBeginLoc())); | ||
|
||
// Replace method name by '(starts|ends)_with'. | ||
// Remove possible arguments before search expression. | ||
// Rename the function to `starts_with` or `ends_with`. | ||
Diagnostic << FixItHint::CreateReplacement(FindExpr->getExprLoc(), | ||
ReplacementFunction->getName()); | ||
|
||
// Replace arguments and everything after the function call. | ||
Diagnostic << FixItHint::CreateReplacement( | ||
CharSourceRange::getCharRange(FindExpr->getExprLoc(), | ||
SearchExpr->getBeginLoc()), | ||
(ReplacementFunction->getName() + "(").str()); | ||
CharSourceRange::getTokenRange(FindExpr->getArg(0)->getBeginLoc(), | ||
hjanuschka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ComparisonExpr->getEndLoc()), | ||
(SearchExprText + ")").str()); | ||
|
||
// Add possible negation '!'. | ||
if (Neg) | ||
// Add negation if necessary. | ||
if (isNegativeComparison(ComparisonExpr)) | ||
Diagnostic << FixItHint::CreateInsertion(FindExpr->getBeginLoc(), "!"); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.