[ast-matcher] Fixed a crash when traverse lambda expr with invalid captures (#108689)

Fixes: #106444
This commit is contained in:
Congcong Cai
2024-09-17 20:12:01 +08:00
committed by GitHub
parent d0438d2d08
commit 07e0b8a771
3 changed files with 18 additions and 2 deletions

View File

@@ -506,6 +506,8 @@ AST Matchers
- Fixed an ordering issue with the `hasOperands` matcher occurring when setting a
binding in the first matcher and using it in the second matcher.
- Fixed a crash when traverse lambda expr with invalid captures. (#GH106444)
clang-format
------------

View File

@@ -285,12 +285,13 @@ public:
ScopedIncrement ScopedDepth(&CurrentDepth);
for (unsigned I = 0, N = Node->capture_size(); I != N; ++I) {
const auto *C = Node->capture_begin() + I;
const LambdaCapture *C = Node->capture_begin() + I;
if (!C->isExplicit())
continue;
if (Node->isInitCapture(C) && !match(*C->getCapturedVar()))
return false;
if (!match(*Node->capture_init_begin()[I]))
const Expr *CIE = Node->capture_init_begin()[I];
if (CIE != nullptr && !match(*CIE))
return false;
}

View File

@@ -5052,6 +5052,19 @@ TEST(ForEachConstructorInitializer, MatchesInitializers) {
cxxConstructorDecl(forEachConstructorInitializer(cxxCtorInitializer()))));
}
TEST(LambdaCapture, InvalidLambdaCapture) {
// not crash
EXPECT_FALSE(matches(
R"(int main() {
struct A { A()=default; A(A const&)=delete; };
A a; [a]() -> void {}();
return 0;
})",
traverse(TK_IgnoreUnlessSpelledInSource,
lambdaExpr(has(lambdaCapture()))),
langCxx11OrLater()));
}
TEST(ForEachLambdaCapture, MatchesCaptures) {
EXPECT_TRUE(matches(
"int main() { int x, y; auto f = [x, y]() { return x + y; }; }",