[clang-tools-extra] [clang-tidy] Add llvm-use-vector-utils (PR #177722)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 24 00:30:29 PST 2026
================
@@ -0,0 +1,126 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "UseVectorUtilsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::llvm_check {
+
+UseVectorUtilsCheck::UseVectorUtilsCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ Inserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM),
+ areDiagsSelfContained()) {}
+
+void UseVectorUtilsCheck::registerPPCallbacks(const SourceManager &SM,
+ Preprocessor *PP,
+ Preprocessor *ModuleExpanderPP) {
+ Inserter.registerPreprocessor(PP);
+}
+
+void UseVectorUtilsCheck::registerMatchers(MatchFinder *Finder) {
+ // Match `llvm::to_vector(llvm::map_range(X, F))`.
+ Finder->addMatcher(
+ callExpr(callee(functionDecl(hasName("::llvm::to_vector"))),
+ hasArgument(0, callExpr(callee(functionDecl(
+ hasName("::llvm::map_range"))))
+ .bind("inner_call")))
+ .bind("map_range_call"),
+ this);
+
+ // Match `llvm::to_vector(llvm::make_filter_range(X, Pred))`.
+ Finder->addMatcher(
+ callExpr(callee(functionDecl(hasName("::llvm::to_vector"))),
+ hasArgument(0, callExpr(callee(functionDecl(hasName(
+ "::llvm::make_filter_range"))))
+ .bind("inner_call")))
+ .bind("filter_range_call"),
+ this);
+}
+
+void UseVectorUtilsCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *MapRangeCall = Result.Nodes.getNodeAs<CallExpr>("map_range_call");
+ const auto *FilterRangeCall =
+ Result.Nodes.getNodeAs<CallExpr>("filter_range_call");
+ if (!MapRangeCall && !FilterRangeCall)
+ return;
+
+ const auto *InnerCall = Result.Nodes.getNodeAs<CallExpr>("inner_call");
+ assert(InnerCall && "inner_call must be bound if map_range_call or "
+ "filter_range_call matched");
+ // Only handle the 2-argument overloads of `map_range`/`make_filter_range`, to
+ // future-proof against additional overloads.
+ if (InnerCall->getNumArgs() != 2)
+ return;
+
+ const CallExpr *OuterCall = MapRangeCall ? MapRangeCall : FilterRangeCall;
+
+ const SourceManager &SM = *Result.SourceManager;
+ const LangOptions &LangOpts = getLangOpts();
+
+ // Determine the base replacement function name.
+ const StringRef ReplacementFuncBase =
+ MapRangeCall ? "llvm::map_to_vector" : "llvm::filter_to_vector";
+ const StringRef InnerFuncName =
+ MapRangeCall ? "llvm::map_range" : "llvm::make_filter_range";
----------------
vbvictor wrote:
nit: we can store replacements in map if plan to add more in the future
https://github.com/llvm/llvm-project/pull/177722
More information about the cfe-commits
mailing list