[ast-matcher] Fixed a crash when traverse lambda expr with invalid captures (#108689)
Fixes: #106444
This commit is contained in:
@@ -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
|
||||
------------
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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; }; }",
|
||||
|
||||
Reference in New Issue
Block a user