[ASTMatchers] forCallable should not erase binding on success by marco-antognini-sonarsource · Pull Request #89657 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-clang-tidy

@llvm/pr-subscribers-clang

Author: Marco Borgeaud (marco-antognini-sonarsource)

Changes

Do not erase Builder when the first check fails because it could succeed on the second stack frame.

The problem was that InnerMatcher.matches erases the bindings when it returns false. The appropriate solution is to pass a copy of the bindings, similar to what matchesFirstInRange does.


Full diff: https://github.com/llvm/llvm-project/pull/89657.diff

2 Files Affected:

diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index 8a2bbfff9e9e6b..bfa6b5fba830ee 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -8363,20 +8363,28 @@ AST_MATCHER_P(Stmt, forCallable, internal::Matcher, InnerMatcher) { const auto &CurNode = Stack.back(); Stack.pop_back(); if (const auto *FuncDeclNode = CurNode.get()) {

@@ -6020,6 +6051,105 @@ TEST(StatementMatcher, ForCallable) { binaryOperator(forCallable(blockDecl())))); }

+namespace { +class ForCallablePreservesBindingWithMultipleParentsTestCallback

+public:

+} + TEST(Matcher, ForEachOverriden) { const auto ForEachOverriddenInClass = [](const char *ClassName) { return cxxMethodDecl(ofClass(hasName(ClassName)), isVirtual(),