[clang-tools-extra] [clang-tidy] Add readability-use-span-first-last check (PR #118074)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 30 03:11:10 PST 2024


================
@@ -0,0 +1,110 @@
+//===--- UseSpanFirstLastCheck.cpp - clang-tidy-----------------*- 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 "UseSpanFirstLastCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+void UseSpanFirstLastCheck::registerMatchers(MatchFinder *Finder) {
+  if (!getLangOpts().CPlusPlus20)
+    return;
+
+  // Match span::subspan calls
+  const auto HasSpanType =
+      hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
+          classTemplateSpecializationDecl(hasName("::std::span"))))));
+
+  Finder->addMatcher(cxxMemberCallExpr(callee(memberExpr(hasDeclaration(
+                                           cxxMethodDecl(hasName("subspan"))))),
+                                       on(expr(HasSpanType)))
+                         .bind("subspan"),
+                     this);
+}
+
+void UseSpanFirstLastCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>("subspan");
+  if (!Call)
+    return;
+
+  handleSubspanCall(Result, Call);
+}
+
+void UseSpanFirstLastCheck::handleSubspanCall(
+    const MatchFinder::MatchResult &Result, const CXXMemberCallExpr *Call) {
+  unsigned NumArgs = Call->getNumArgs();
+  if (NumArgs == 0 || NumArgs > 2)
+    return;
+
+  const Expr *Offset = Call->getArg(0);
+  const Expr *Count = NumArgs > 1 ? Call->getArg(1) : nullptr;
+  auto &Context = *Result.Context;
+
+  // Check if this is subspan(0, n) -> first(n)
+  bool IsZeroOffset = false;
+  const Expr *OffsetE = Offset->IgnoreImpCasts();
+  if (const auto *IL = dyn_cast<IntegerLiteral>(OffsetE)) {
+    IsZeroOffset = IL->getValue() == 0;
+  }
+
+  // Check if this is subspan(size() - n) -> last(n)
+  bool IsSizeMinusN = false;
+  const Expr *SizeMinusArg = nullptr;
+  if (const auto *BO = dyn_cast<BinaryOperator>(OffsetE)) {
+    if (BO->getOpcode() == BO_Sub) {
+      if (const auto *SizeCall = dyn_cast<CXXMemberCallExpr>(BO->getLHS())) {
+        if (SizeCall->getMethodDecl()->getName() == "size") {
+          IsSizeMinusN = true;
+          SizeMinusArg = BO->getRHS();
+        }
+      }
+    }
+  }
+
+  // Build replacement text
+  std::string Replacement;
----------------
5chmidti wrote:

This replacement should be possible to do without the need to go through the lexer, and just replace the needed parts instead. As in, replace from `substr` until before the `n` with `first(/last(` (though that would be problematic with a macro that does `#define K s.size() - 3`, because the `3` would be inside the macro (location wise))...

https://github.com/llvm/llvm-project/pull/118074


More information about the cfe-commits mailing list