Skip to content

[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

Merged

Conversation

danix800
Copy link
Member

@danix800 danix800 commented Dec 30, 2023

Matcher capturesVar should check for capturesVariables() before calling getCaptureVar() since it asserts this LambdaCapture does capture a variable.

Fixes #76425

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Dec 30, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 30, 2023

@llvm/pr-subscribers-clang

Author: Ding Fei (danix800)

Changes

Fixes #76425


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

2 Files Affected:

  • (modified) clang/include/clang/ASTMatchers/ASTMatchers.h (+2)
  • (modified) clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp (+2)
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) {

@danix800 danix800 requested review from shafik and cor3ntin December 30, 2023 16:38
Copy link
Collaborator

@shafik shafik left a 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.

@danix800 danix800 force-pushed the fix/ast-matcher-captureVars-assertion-failure branch from b26fa2a to 50c0ccd Compare January 9, 2024 10:25
@danix800
Copy link
Member Author

danix800 commented Jan 9, 2024

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.

Thanks for reminding. Summary updated, please take another look.

@danix800 danix800 requested a review from shafik January 9, 2024 10:28
Copy link
Collaborator

@AaronBallman AaronBallman left a 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
@danix800 danix800 force-pushed the fix/ast-matcher-captureVars-assertion-failure branch from 50c0ccd to 0ca1a2b Compare April 9, 2024 04:06
@danix800
Copy link
Member Author

danix800 commented Apr 9, 2024

LGTM but please add a release note so users know about the bug fix.

Release note added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ast_matcher capturesVar assertion failed in debug mode
4 participants