-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ASTMatchers] fix captureVars assertion failure on capturesVariables #76619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ASTMatchers] fix captureVars assertion failure on capturesVariables #76619
Conversation
@llvm/pr-subscribers-clang Author: Ding Fei (danix800) ChangesFixes #76425 Full diff: https://github.com/llvm/llvm-project/pull/76619.diff 2 Files Affected:
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 82a26356c58f55..91c33e4b1163e6 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4817,6 +4817,8 @@ AST_MATCHER_P(LambdaExpr, hasAnyCapture, internal::Matcher<LambdaCapture>,
/// capturesVar(hasName("x")) matches `x` and `x = 1`.
AST_MATCHER_P(LambdaCapture, capturesVar, internal::Matcher<ValueDecl>,
InnerMatcher) {
+ if (!Node.capturesVariable())
+ return false;
auto *capturedVar = Node.getCapturedVar();
return capturedVar && InnerMatcher.matches(*capturedVar, Finder, Builder);
}
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
index 8f0dd5602307c5..eb493f9c3050ac 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2308,6 +2308,8 @@ TEST_P(ASTMatchersTest, LambdaCaptureTest_BindsToCaptureOfVarDecl) {
matches("int main() { int cc; auto f = [=](){ return cc; }; }", matcher));
EXPECT_TRUE(
matches("int main() { int cc; auto f = [&](){ return cc; }; }", matcher));
+ EXPECT_TRUE(matches(
+ "void f(int a) { int cc[a]; auto f = [&](){ return cc;}; }", matcher));
}
TEST_P(ASTMatchersTest, LambdaCaptureTest_BindsToCaptureWithInitializer) {
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the fix, can you add more details to your summary. The summary is what usually goes into the git log. We would like those to be as descriptive as possible to avoid having to do extra digging to understand the change at a high level.
b26fa2a
to
50c0ccd
Compare
Thanks for reminding. Summary updated, please take another look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but please add a release note so users know about the bug fix.
Matcher 'capturesVar' should check for capturesVariables() before calling getCaptureVar() since it asserts this LambdaCapture does capture a variable. Fixes llvm#76425
50c0ccd
to
0ca1a2b
Compare
Release note added. |
Matcher
capturesVar
should check forcapturesVariables()
before callinggetCaptureVar()
since it asserts thisLambdaCapture
does capture a variable.Fixes #76425