[clang] 43cc4f1 - Ensure that we traverse non-op() method bodys of lambdas
Stephen Kelly via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 28 16:49:41 PST 2021
Author: Stephen Kelly
Date: 2021-01-29T00:49:28Z
New Revision: 43cc4f15008f8c700497d3d2b7020bfd29f5750f
URL: https://github.com/llvm/llvm-project/commit/43cc4f15008f8c700497d3d2b7020bfd29f5750f
DIFF: https://github.com/llvm/llvm-project/commit/43cc4f15008f8c700497d3d2b7020bfd29f5750f.diff
LOG: Ensure that we traverse non-op() method bodys of lambdas
Differential Revision: https://reviews.llvm.org/D95644
Added:
Modified:
clang/include/clang/AST/RecursiveASTVisitor.h
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index db2ef21f4364..7870cea198a7 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -2062,7 +2062,8 @@ bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
if (const CXXRecordDecl *RD = MD->getParent()) {
- if (RD->isLambda()) {
+ if (RD->isLambda() &&
+ declaresSameEntity(RD->getLambdaCallOperator(), MD)) {
VisitBody = VisitBody && getDerived().shouldVisitLambdaBody();
}
}
diff --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index 8004599e01a2..a3a09c426673 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -474,6 +474,42 @@ TEST(Matcher, CapturesThis) {
EXPECT_TRUE(notMatches("void f() { int z = 3; [&z](){}; }", HasCaptureThis));
}
+TEST(Matcher, MatchesMethodsOnLambda) {
+ StringRef Code = R"cpp(
+struct A {
+ ~A() {}
+};
+void foo()
+{
+ A a;
+ auto l = [a] { };
+ auto lCopy = l;
+ auto lPtrDecay = +[] { };
+ (void)lPtrDecay;
+}
+)cpp";
+
+ EXPECT_TRUE(matches(
+ Code, cxxConstructorDecl(
+ hasBody(compoundStmt()),
+ hasAncestor(lambdaExpr(hasAncestor(varDecl(hasName("l"))))),
+ isCopyConstructor())));
+ EXPECT_TRUE(matches(
+ Code, cxxConstructorDecl(
+ hasBody(compoundStmt()),
+ hasAncestor(lambdaExpr(hasAncestor(varDecl(hasName("l"))))),
+ isMoveConstructor())));
+ EXPECT_TRUE(matches(
+ Code, cxxDestructorDecl(
+ hasBody(compoundStmt()),
+ hasAncestor(lambdaExpr(hasAncestor(varDecl(hasName("l"))))))));
+ EXPECT_TRUE(matches(
+ Code, cxxConversionDecl(hasBody(compoundStmt(has(returnStmt(
+ hasReturnValue(implicitCastExpr()))))),
+ hasAncestor(lambdaExpr(hasAncestor(
+ varDecl(hasName("lPtrDecay"))))))));
+}
+
TEST(Matcher, isClassMessage) {
EXPECT_TRUE(matchesObjC(
"@interface NSString +(NSString *) stringWithFormat; @end "
More information about the cfe-commits
mailing list