Skip to content

Commit dbb9749

Browse files
authored
[ASTMatchers] fix captureVars assertion failure on capturesVariables (#76619)
Matcher `capturesVar` should check for `capturesVariables()` before calling `getCaptureVar()` since it asserts this `LambdaCapture` does capture a variable. Fixes #76425
1 parent 51089e3 commit dbb9749

File tree

3 files changed

+5
-0
lines changed

3 files changed

+5
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ AST Matchers
633633
- Add ``isExplicitObjectMemberFunction``.
634634
- Fixed ``forEachArgumentWithParam`` and ``forEachArgumentWithParamType`` to
635635
not skip the explicit object parameter for operator calls.
636+
- Fixed captureVars assertion failure if not capturesVariables. (#GH76425)
636637

637638
clang-format
638639
------------

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4961,6 +4961,8 @@ AST_MATCHER_P(LambdaExpr, hasAnyCapture, internal::Matcher<LambdaCapture>,
49614961
/// capturesVar(hasName("x")) matches `x` and `x = 1`.
49624962
AST_MATCHER_P(LambdaCapture, capturesVar, internal::Matcher<ValueDecl>,
49634963
InnerMatcher) {
4964+
if (!Node.capturesVariable())
4965+
return false;
49644966
auto *capturedVar = Node.getCapturedVar();
49654967
return capturedVar && InnerMatcher.matches(*capturedVar, Finder, Builder);
49664968
}

clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,6 +2321,8 @@ TEST_P(ASTMatchersTest, LambdaCaptureTest_BindsToCaptureOfVarDecl) {
23212321
matches("int main() { int cc; auto f = [=](){ return cc; }; }", matcher));
23222322
EXPECT_TRUE(
23232323
matches("int main() { int cc; auto f = [&](){ return cc; }; }", matcher));
2324+
EXPECT_TRUE(matches(
2325+
"void f(int a) { int cc[a]; auto f = [&](){ return cc;}; }", matcher));
23242326
}
23252327

23262328
TEST_P(ASTMatchersTest, LambdaCaptureTest_BindsToCaptureWithInitializer) {

0 commit comments

Comments
 (0)