[clang-tools-extra] [clang-tidy] Add modernize-use-span-param check (PR #182027)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 19 11:48:44 PST 2026
================
@@ -0,0 +1,190 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "UseSpanParamCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+// Methods on std::vector that only read data (compatible with std::span).
+static bool isReadOnlyVectorMethod(StringRef Name) {
+ return Name == "operator[]" || Name == "at" || Name == "data" ||
+ Name == "size" || Name == "empty" || Name == "begin" ||
+ Name == "end" || Name == "cbegin" || Name == "cend" ||
+ Name == "rbegin" || Name == "rend" || Name == "crbegin" ||
+ Name == "crend" || Name == "front" || Name == "back";
+}
+
+// Check if all uses of the parameter in the function body are read-only.
+static bool allUsesAreReadOnly(const ParmVarDecl *Param,
+ const FunctionDecl *Func, ASTContext &Context) {
+ const Stmt *Body = Func->getBody();
+ if (!Body)
+ return false;
+
+ const auto Refs = match(
+ findAll(declRefExpr(to(equalsNode(Param))).bind("ref")), *Body, Context);
+
+ for (const auto &Ref : Refs) {
+ const auto *DRE = Ref.getNodeAs<DeclRefExpr>("ref");
+ if (!DRE)
+ return false;
+
+ // Walk up through implicit casts to find the "real" parent.
+ const Expr *Current = DRE;
+ while (true) {
+ const auto Parents = Context.getParents(*Current);
+ if (Parents.empty())
+ return false;
+ const auto &Parent = Parents[0];
+ if (const auto *ICE = Parent.get<ImplicitCastExpr>()) {
+ Current = ICE;
+ continue;
+ }
----------------
localspook wrote:
Should we also skip past `ParenExpr`s?
https://github.com/llvm/llvm-project/pull/182027
More information about the cfe-commits
mailing list