-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[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 1 commit
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 |
|---|---|---|
|
|
@@ -30,6 +30,17 @@ struct NotLengthExprForStringNode { | |
| IntegerLiteralSizeNode->getValue().getZExtValue(); | ||
| } | ||
|
|
||
| if (const auto *DeclRefNode = Node.get<DeclRefExpr>()) { | ||
| if (const auto *VD = dyn_cast<VarDecl>(DeclRefNode->getDecl())) { | ||
| if (VD->hasInit() && VD->getType().isConstQualified()) { | ||
| if (const auto *Init = dyn_cast<IntegerLiteral>(VD->getInit())) { | ||
| return StringLiteralNode->getLength() != | ||
| Init->getValue().getZExtValue(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
hjanuschka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (const auto *StrlenNode = Node.get<CallExpr>()) { | ||
| if (StrlenNode->getDirectCallee()->getName() != "strlen" || | ||
| StrlenNode->getNumArgs() != 1) { | ||
|
|
@@ -171,10 +182,64 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) { | |
| hasRHS(lengthExprForStringNode("needle"))))) | ||
| .bind("expr"), | ||
| this); | ||
|
|
||
| Finder->addMatcher( | ||
hjanuschka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cxxOperatorCallExpr( | ||
| hasAnyOperatorName("==", "!="), | ||
| anyOf( | ||
| hasOperands( | ||
| cxxMemberCallExpr( | ||
| argumentCountIs(2), hasArgument(0, ZeroLiteral), | ||
| hasArgument(1, lengthExprForStringNode("needle")), | ||
| callee( | ||
| cxxMethodDecl(hasName("substr"), | ||
| ofClass(OnClassWithStartsWithFunction)) | ||
| .bind("find_fun"))) | ||
| .bind("find_expr"), | ||
| expr().bind("needle")), | ||
| 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); | ||
hjanuschka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| bool UseStartsEndsWithCheck::isNegativeComparison(const Expr* ComparisonExpr) { | ||
hjanuschka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Handle direct != operator | ||
hjanuschka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (const auto *BO = llvm::dyn_cast<BinaryOperator>(ComparisonExpr)) { | ||
| return BO->getOpcode() == BO_NE; | ||
| } | ||
|
|
||
| // Handle operator!= call | ||
hjanuschka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (const auto *Op = llvm::dyn_cast<CXXOperatorCallExpr>(ComparisonExpr)) { | ||
| return Op->getOperator() == OO_ExclaimEqual; | ||
| } | ||
|
|
||
| // Handle rewritten !(expr == expr) | ||
| if (const auto *UO = llvm::dyn_cast<UnaryOperator>(ComparisonExpr)) { | ||
| if (UO->getOpcode() == UO_LNot) { | ||
| if (const auto *InnerBO = | ||
| llvm::dyn_cast<BinaryOperator>(UO->getSubExpr()->IgnoreParens())) { | ||
| return InnerBO->getOpcode() == BO_EQ; | ||
| } | ||
| if (const auto *InnerOp = | ||
| llvm::dyn_cast<CXXOperatorCallExpr>(UO->getSubExpr()->IgnoreParens())) { | ||
| return InnerOp->getOperator() == OO_EqualEqual; | ||
| } | ||
| } | ||
| } | ||
|
|
||
nicovank marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return false; | ||
| } | ||
|
|
||
| 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,40 +248,43 @@ 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()) | ||
hjanuschka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
|
|
||
| const bool Neg = ComparisonExpr->getOpcode() == BO_NE; | ||
| bool Neg = isNegativeComparison(ComparisonExpr); | ||
hjanuschka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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
Contributor
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.:
Contributor
Author
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?
Contributor
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.
Contributor
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 Diag = diag(ComparisonExpr->getBeginLoc(), | ||
| "use %0 instead of %1 %select{==|!=}2 ") | ||
| << ReplacementFunction->getName() << FindFun->getNameAsString() | ||
| << Neg; | ||
|
|
||
| // Remove possible '.+ [!=]= ' prefix. | ||
| Diagnostic << FixItHint::CreateRemoval(CharSourceRange::getCharRange( | ||
| // Remove everything before the function call. | ||
| Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( | ||
| ComparisonExpr->getBeginLoc(), FindExpr->getBeginLoc())); | ||
|
|
||
| // Replace method name by '(starts|ends)_with'. | ||
| // Remove possible arguments before search expression. | ||
| Diagnostic << FixItHint::CreateReplacement( | ||
| CharSourceRange::getCharRange(FindExpr->getExprLoc(), | ||
| SearchExpr->getBeginLoc()), | ||
| (ReplacementFunction->getName() + "(").str()); | ||
| // Rename the function to `starts_with` or `ends_with`. | ||
| Diag << FixItHint::CreateReplacement(FindExpr->getExprLoc(), | ||
| ReplacementFunction->getName()); | ||
|
|
||
| // Add possible negation '!'. | ||
| if (Neg) | ||
| Diagnostic << FixItHint::CreateInsertion(FindExpr->getBeginLoc(), "!"); | ||
| // Replace arguments and everything after the function call. | ||
| Diag << FixItHint::CreateReplacement( | ||
| CharSourceRange::getTokenRange(FindExpr->getArg(0)->getBeginLoc(), | ||
hjanuschka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ComparisonExpr->getEndLoc()), | ||
| (SearchExprText + ")").str()); | ||
|
|
||
| // Add negation if necessary. | ||
| if (Neg) { | ||
| Diag << FixItHint::CreateInsertion(FindExpr->getBeginLoc(), "!"); | ||
| } | ||
hjanuschka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } // namespace clang::tidy::modernize | ||
Uh oh!
There was an error while loading. Please reload this page.