@@ -590,6 +590,8 @@ static void extendDepthMap(
590
590
Expr *expr,
591
591
llvm::DenseMap<Expr *, std::pair<unsigned , Expr *>> &depthMap) {
592
592
class RecordingTraversal : public ASTWalker {
593
+ SmallVector<ClosureExpr *, 4 > Closures;
594
+
593
595
public:
594
596
llvm::DenseMap<Expr *, std::pair<unsigned , Expr *>> &DepthMap;
595
597
unsigned Depth = 0 ;
@@ -601,13 +603,37 @@ static void extendDepthMap(
601
603
std::pair<bool , Expr *> walkToExprPre (Expr *E) override {
602
604
DepthMap[E] = {Depth, Parent.getAsExpr ()};
603
605
++Depth;
606
+
607
+ if (auto CE = dyn_cast<ClosureExpr>(E))
608
+ Closures.push_back (CE);
609
+
604
610
return { true , E };
605
611
}
606
612
607
613
Expr *walkToExprPost (Expr *E) override {
614
+ if (auto CE = dyn_cast<ClosureExpr>(E)) {
615
+ assert (Closures.back () == CE);
616
+ Closures.pop_back ();
617
+ }
618
+
608
619
--Depth;
609
620
return E;
610
621
}
622
+
623
+ std::pair<bool , Stmt *> walkToStmtPre (Stmt *S) override {
624
+ if (auto RS = dyn_cast<ReturnStmt>(S)) {
625
+ // For return statements, treat the parent of the return expression
626
+ // as the closure itself.
627
+ if (RS->hasResult () && !Closures.empty ()) {
628
+ llvm::SaveAndRestore<ParentTy> SavedParent (Parent, Closures.back ());
629
+ auto E = RS->getResult ();
630
+ E->walk (*this );
631
+ return { false , S };
632
+ }
633
+ }
634
+
635
+ return { true , S };
636
+ }
611
637
};
612
638
613
639
RecordingTraversal traversal (depthMap);
0 commit comments