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