Skip to content

Commit 50c0ccd

Browse files
committed
[ASTMatchers] fix captureVars assertion failure on capturesVariables
Matcher 'capturesVar' should check for capturesVariables() before calling getCaptureVar() since it asserts this LambdaCapture does capture a variable. Fixes #76425
1 parent 3d688d4 commit 50c0ccd

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4817,6 +4817,8 @@ AST_MATCHER_P(LambdaExpr, hasAnyCapture, internal::Matcher<LambdaCapture>,
48174817
/// capturesVar(hasName("x")) matches `x` and `x = 1`.
48184818
AST_MATCHER_P(LambdaCapture, capturesVar, internal::Matcher<ValueDecl>,
48194819
InnerMatcher) {
4820+
if (!Node.capturesVariable())
4821+
return false;
48204822
auto *capturedVar = Node.getCapturedVar();
48214823
return capturedVar && InnerMatcher.matches(*capturedVar, Finder, Builder);
48224824
}

clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,8 @@ TEST_P(ASTMatchersTest, LambdaCaptureTest_BindsToCaptureOfVarDecl) {
23082308
matches("int main() { int cc; auto f = [=](){ return cc; }; }", matcher));
23092309
EXPECT_TRUE(
23102310
matches("int main() { int cc; auto f = [&](){ return cc; }; }", matcher));
2311+
EXPECT_TRUE(matches(
2312+
"void f(int a) { int cc[a]; auto f = [&](){ return cc;}; }", matcher));
23112313
}
23122314

23132315
TEST_P(ASTMatchersTest, LambdaCaptureTest_BindsToCaptureWithInitializer) {

0 commit comments

Comments
 (0)