================
@@ -0,0 +1,129 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "SubstrSelfAssignmentCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+#include <optional>
+#include <string>
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::performance {
+
+SubstrSelfAssignmentCheck::SubstrSelfAssignmentCheck(StringRef Name,
+                                                     ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      StringLikeClasses(utils::options::parseStringList(
+          Options.get("StringLikeClasses", "::std::basic_string"))) {}
+
+void SubstrSelfAssignmentCheck::storeOptions(
+    ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "StringLikeClasses",
+                utils::options::serializeStringList(StringLikeClasses));
+}
+
+void SubstrSelfAssignmentCheck::registerMatchers(MatchFinder *Finder) {
+  const auto VarRef =
+      ignoringParens(declRefExpr(to(varDecl().bind("var"))).bind("lhs"));
+
+  const auto StringClass =
+      cxxRecordDecl(hasAnyName(StringLikeClasses)).bind("string-class");
+
+  // The static member 'npos' of the matched string class. A bare name
+  // comparison is not enough: a local variable or parameter that happens to
+  // be called 'npos' carries a real count, which has no single-'erase'
+  // rewrite.
+  const auto NposDecl =
+      varDecl(hasName("npos"),
+              hasDeclContext(cxxRecordDecl(equalsBoundNode("string-class"))));
+
+  const auto Npos =
+      expr(ignoringParenImpCasts(anyOf(declRefExpr(hasDeclaration(NposDecl)),
+                                       memberExpr(hasDeclaration(NposDecl)))));
+
+  const auto SubstrCall =
+      cxxMemberCallExpr(
+          callee(cxxMethodDecl(hasName("substr"), ofClass(StringClass))),
+          on(ignoringParens(declRefExpr(to(varDecl(equalsBoundNode("var")))))),
+          optionally(hasArgument(1, Npos.bind("npos-count"))))
+          .bind("substr");
+
+  // Match: s = s.substr(...), except in unevaluated contexts such as
+  // decltype or sizeof, where no temporary is ever materialized.
+  Finder->addMatcher(
+      cxxOperatorCallExpr(
+          hasOperatorName("="), hasArgument(0, VarRef),
+          hasArgument(1, SubstrCall),
+          unless(anyOf(hasAncestor(typeLoc()),
+                       hasAncestor(expr(matchers::hasUnevaluatedContext())))))
+          .bind("assign"),
+      this);
+}
+
+void SubstrSelfAssignmentCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *AssignExpr =
+      Result.Nodes.getNodeAs<CXXOperatorCallExpr>("assign");
+  const auto *LHS = Result.Nodes.getNodeAs<DeclRefExpr>("lhs");
+  const auto *SubstrExpr = Result.Nodes.getNodeAs<CXXMemberCallExpr>("substr");
+  const SourceManager &SM = *Result.SourceManager;
+  const LangOptions &LangOpts = Result.Context->getLangOpts();
+
+  // Count only explicitly-written arguments (exclude CXXDefaultArgExpr).
+  unsigned NumExplicitArgs = 0;
+  for (const Expr *Arg : SubstrExpr->arguments())
+    if (!isa<CXXDefaultArgExpr>(Arg))
+      ++NumExplicitArgs;
----------------
zwuis wrote:

Use `llvm::count_if`.

https://github.com/llvm/llvm-project/pull/209657
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to