[clang-tools-extra] [clang-tidy] Support C++26 placeholder bindings (PR #207604)
Yanzuo Liu via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 28 02:34:24 PDT 2026
================
@@ -0,0 +1,76 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "UsePlaceholderBindingCheck.h"
+#include "../utils/DeclRefExprUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+namespace {
+AST_POLYMORPHIC_MATCHER(isInMacro,
+ AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl)) {
+ return Node.getBeginLoc().isMacroID() || Node.getEndLoc().isMacroID();
+}
+
+AST_MATCHER(VarDecl, hasAutomaticStorageDurationAndNoSpecifiers) {
+ return !Node.hasAttrs() && Node.getStorageClass() == SC_None &&
+ Node.getTSCSpec() == TSCS_unspecified;
+}
+} // namespace
+
+void UsePlaceholderBindingCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ cStyleCastExpr(
+ unless(isInMacro()), hasType(voidType()),
+ hasParent(stmt(anyOf(compoundStmt(), switchCase()))),
+ hasSourceExpression(ignoringParens(declRefExpr(
+ unless(isInMacro()),
+ to(bindingDecl(forDecomposition(varDecl(
+ unless(isInMacro()),
+ hasAutomaticStorageDurationAndNoSpecifiers(),
+ hasAncestor(compoundStmt().bind("scope")))))
+ .bind("binding"))))))
+ .bind("cast"),
+ this);
+}
+
+void UsePlaceholderBindingCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *Cast = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
+ const auto *Binding = Result.Nodes.getNodeAs<BindingDecl>("binding");
+ const auto *Scope = Result.Nodes.getNodeAs<CompoundStmt>("scope");
+ ASTContext &Context = *Result.Context;
+
+ if (Binding->isParameterPack() ||
+ Binding->isPlaceholderVar(Context.getLangOpts()))
----------------
zwuis wrote:
Move this to a custom AST matcher.
https://github.com/llvm/llvm-project/pull/207604
More information about the cfe-commits
mailing list