Files
TheRock/compiler/amd-llvm/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

130 lines
5.6 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ContainerContainsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang::tidy::readability {
void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) {
const auto Literal0 = integerLiteral(equals(0));
const auto Literal1 = integerLiteral(equals(1));
const auto ClassWithContains = cxxRecordDecl(
hasMethod(cxxMethodDecl(isConst(), parameterCountIs(1), isPublic(),
unless(isDeleted()), returns(booleanType()),
hasAnyName("contains", "Contains"))
.bind("contains_fun")));
const auto CountCall =
cxxMemberCallExpr(argumentCountIs(1),
callee(cxxMethodDecl(hasAnyName("count", "Count"),
ofClass(ClassWithContains))))
.bind("call");
const auto FindCall =
// Either one argument, or assume the second argument is the position to
// start searching from.
cxxMemberCallExpr(
anyOf(argumentCountIs(1),
allOf(argumentCountIs(2), hasArgument(1, Literal0))),
callee(cxxMethodDecl(hasAnyName("find", "Find"),
ofClass(ClassWithContains))))
.bind("call");
const auto EndCall = cxxMemberCallExpr(
argumentCountIs(0), callee(cxxMethodDecl(hasAnyName("end", "End"),
ofClass(ClassWithContains))));
const auto StringNpos = anyOf(declRefExpr(to(varDecl(hasName("npos")))),
memberExpr(member(hasName("npos"))));
Finder->addMatcher(
traverse(TK_AsIs,
implicitCastExpr(hasImplicitDestinationType(booleanType()),
hasSourceExpression(CountCall))
.bind("positiveComparison")),
this);
const auto PositiveComparison =
anyOf(allOf(hasOperatorName("!="), hasOperands(CountCall, Literal0)),
allOf(hasLHS(CountCall), hasOperatorName(">"), hasRHS(Literal0)),
allOf(hasLHS(Literal0), hasOperatorName("<"), hasRHS(CountCall)),
allOf(hasLHS(CountCall), hasOperatorName(">="), hasRHS(Literal1)),
allOf(hasLHS(Literal1), hasOperatorName("<="), hasRHS(CountCall)),
allOf(hasOperatorName("!="),
hasOperands(FindCall, anyOf(EndCall, StringNpos))));
const auto NegativeComparison =
anyOf(allOf(hasOperatorName("=="), hasOperands(CountCall, Literal0)),
allOf(hasLHS(CountCall), hasOperatorName("<="), hasRHS(Literal0)),
allOf(hasLHS(Literal0), hasOperatorName(">="), hasRHS(CountCall)),
allOf(hasLHS(CountCall), hasOperatorName("<"), hasRHS(Literal1)),
allOf(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall)),
allOf(hasOperatorName("=="),
hasOperands(FindCall, anyOf(EndCall, StringNpos))));
Finder->addMatcher(
binaryOperation(
anyOf(allOf(PositiveComparison, expr().bind("positiveComparison")),
allOf(NegativeComparison, expr().bind("negativeComparison")))),
this);
}
void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
// Extract the information about the match
const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>("call");
const auto *PositiveComparison =
Result.Nodes.getNodeAs<Expr>("positiveComparison");
const auto *NegativeComparison =
Result.Nodes.getNodeAs<Expr>("negativeComparison");
assert((!PositiveComparison || !NegativeComparison) &&
"only one of PositiveComparison or NegativeComparison should be set");
const bool Negated = NegativeComparison != nullptr;
const auto *Comparison = Negated ? NegativeComparison : PositiveComparison;
const StringRef ContainsFunName =
Result.Nodes.getNodeAs<CXXMethodDecl>("contains_fun")->getName();
const Expr *SearchExpr = Call->getArg(0)->IgnoreParenImpCasts();
// Diagnose the issue.
auto Diag = diag(Call->getExprLoc(), "use '%0' to check for membership")
<< ContainsFunName;
// Don't fix it if it's in a macro invocation. Leave fixing it to the user.
const SourceLocation FuncCallLoc = Comparison->getEndLoc();
if (!FuncCallLoc.isValid() || FuncCallLoc.isMacroID())
return;
const StringRef SearchExprText = Lexer::getSourceText(
CharSourceRange::getTokenRange(SearchExpr->getSourceRange()),
*Result.SourceManager, Result.Context->getLangOpts());
// Remove everything before the function call.
Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
Comparison->getBeginLoc(), Call->getBeginLoc()));
// Rename the function to `contains`.
Diag << FixItHint::CreateReplacement(Call->getExprLoc(), ContainsFunName);
// Replace arguments and everything after the function call.
Diag << FixItHint::CreateReplacement(
CharSourceRange::getTokenRange(Call->getArg(0)->getBeginLoc(),
Comparison->getEndLoc()),
(SearchExprText + ")").str());
// Add negation if necessary.
if (Negated)
Diag << FixItHint::CreateInsertion(Call->getBeginLoc(), "!");
}
} // namespace clang::tidy::readability