David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 1 | //===- unittest/Tooling/RecursiveASTVisitorTests/LambdaExpr.cpp -----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "TestVisitor.h" |
| 10 | #include <stack> |
| 11 | |
| 12 | using namespace clang; |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | class LambdaExprVisitor : public ExpectedLocationVisitor<LambdaExprVisitor> { |
| 17 | public: |
| 18 | bool VisitLambdaExpr(LambdaExpr *Lambda) { |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 19 | PendingBodies.push(Lambda->getBody()); |
| 20 | PendingClasses.push(Lambda->getLambdaClass()); |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 21 | Match("", Lambda->getIntroducerRange().getBegin()); |
| 22 | return true; |
| 23 | } |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 24 | /// For each call to VisitLambdaExpr, we expect a subsequent call to visit |
| 25 | /// the body (and maybe the lambda class, which is implicit). |
| 26 | bool VisitStmt(Stmt *S) { |
| 27 | if (!PendingBodies.empty() && S == PendingBodies.top()) |
| 28 | PendingBodies.pop(); |
| 29 | return true; |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 30 | } |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 31 | bool VisitDecl(Decl *D) { |
| 32 | if (!PendingClasses.empty() && D == PendingClasses.top()) |
| 33 | PendingClasses.pop(); |
| 34 | return true; |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 35 | } |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 36 | /// Determine whether parts of lambdas (VisitLambdaExpr) were later traversed. |
| 37 | bool allBodiesHaveBeenTraversed() const { return PendingBodies.empty(); } |
| 38 | bool allClassesHaveBeenTraversed() const { return PendingClasses.empty(); } |
| 39 | |
| 40 | bool VisitImplicitCode = false; |
| 41 | bool shouldVisitImplicitCode() const { return VisitImplicitCode; } |
| 42 | |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 43 | private: |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 44 | std::stack<Stmt *> PendingBodies; |
| 45 | std::stack<Decl *> PendingClasses; |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | TEST(RecursiveASTVisitor, VisitsLambdaExpr) { |
| 49 | LambdaExprVisitor Visitor; |
| 50 | Visitor.ExpectMatch("", 1, 12); |
| 51 | EXPECT_TRUE(Visitor.runOver("void f() { []{ return; }(); }", |
| 52 | LambdaExprVisitor::Lang_CXX11)); |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 53 | EXPECT_TRUE(Visitor.allBodiesHaveBeenTraversed()); |
| 54 | EXPECT_FALSE(Visitor.allClassesHaveBeenTraversed()); |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 57 | TEST(RecursiveASTVisitor, LambdaInLambda) { |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 58 | LambdaExprVisitor Visitor; |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 59 | Visitor.ExpectMatch("", 1, 12); |
| 60 | Visitor.ExpectMatch("", 1, 16); |
| 61 | EXPECT_TRUE(Visitor.runOver("void f() { []{ []{ return; }; }(); }", |
| 62 | LambdaExprVisitor::Lang_CXX11)); |
| 63 | EXPECT_TRUE(Visitor.allBodiesHaveBeenTraversed()); |
| 64 | EXPECT_FALSE(Visitor.allClassesHaveBeenTraversed()); |
| 65 | } |
| 66 | |
Sam McCall | 32ef520 | 2019-01-14 17:16:00 +0000 | [diff] [blame] | 67 | TEST(RecursiveASTVisitor, TopLevelLambda) { |
| 68 | LambdaExprVisitor Visitor; |
| 69 | Visitor.VisitImplicitCode = true; |
| 70 | Visitor.ExpectMatch("", 1, 10); |
| 71 | Visitor.ExpectMatch("", 1, 14); |
| 72 | EXPECT_TRUE(Visitor.runOver("auto x = []{ [] {}; };", |
| 73 | LambdaExprVisitor::Lang_CXX11)); |
| 74 | EXPECT_TRUE(Visitor.allBodiesHaveBeenTraversed()); |
| 75 | EXPECT_TRUE(Visitor.allClassesHaveBeenTraversed()); |
| 76 | } |
| 77 | |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 78 | TEST(RecursiveASTVisitor, VisitsLambdaExprAndImplicitClass) { |
| 79 | LambdaExprVisitor Visitor; |
| 80 | Visitor.VisitImplicitCode = true; |
| 81 | Visitor.ExpectMatch("", 1, 12); |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 82 | EXPECT_TRUE(Visitor.runOver("void f() { []{ return; }(); }", |
| 83 | LambdaExprVisitor::Lang_CXX11)); |
| 84 | EXPECT_TRUE(Visitor.allBodiesHaveBeenTraversed()); |
Sam McCall | e60151c | 2019-01-14 10:31:42 +0000 | [diff] [blame] | 85 | EXPECT_TRUE(Visitor.allClassesHaveBeenTraversed()); |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST(RecursiveASTVisitor, VisitsAttributedLambdaExpr) { |
Sunil Srivastava | f4038e7 | 2019-07-19 21:38:34 +0000 | [diff] [blame^] | 89 | if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).isPS4()) |
| 90 | return; // PS4 does not support fastcall. |
David Zarzycki | d488daa | 2018-04-19 18:19:02 +0000 | [diff] [blame] | 91 | LambdaExprVisitor Visitor; |
| 92 | Visitor.ExpectMatch("", 1, 12); |
| 93 | EXPECT_TRUE(Visitor.runOver( |
| 94 | "void f() { [] () __attribute__ (( fastcall )) { return; }(); }", |
| 95 | LambdaExprVisitor::Lang_CXX14)); |
| 96 | } |
| 97 | |
| 98 | } // end anonymous namespace |