@@ -210,8 +210,6 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
210
210
return false ;
211
211
}
212
212
213
-
214
-
215
213
public:
216
214
IndexSwiftASTWalker (IndexDataConsumer &IdxConsumer, ASTContext &Ctx,
217
215
unsigned BufferID)
@@ -327,14 +325,18 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
327
325
328
326
}
329
327
328
+ Expr *getContainingExpr (size_t index) {
329
+ if (ExprStack.size () > index)
330
+ return ExprStack.end ()[-(index + 1 )];
331
+ return nullptr ;
332
+ }
333
+
330
334
Expr *getCurrentExpr () {
331
335
return ExprStack.empty () ? nullptr : ExprStack.back ();
332
336
}
333
337
334
338
Expr *getParentExpr () {
335
- if (ExprStack.size () >= 2 )
336
- return ExprStack.end ()[-2 ];
337
- return nullptr ;
339
+ return getContainingExpr (1 );
338
340
}
339
341
340
342
@@ -376,8 +378,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
376
378
bool initIndexSymbol (ExtensionDecl *D, ValueDecl *ExtendedD, SourceLoc Loc,
377
379
IndexSymbol &Info);
378
380
bool initFuncDeclIndexSymbol (FuncDecl *D, IndexSymbol &Info);
379
- bool initFuncRefIndexSymbol (Expr *CurrentE, Expr *ParentE, ValueDecl *D,
380
- SourceLoc Loc, IndexSymbol &Info);
381
+ bool initFuncRefIndexSymbol (ValueDecl *D, SourceLoc Loc, IndexSymbol &Info);
381
382
bool initVarRefIndexSymbols (Expr *CurrentE, ValueDecl *D, SourceLoc Loc,
382
383
IndexSymbol &Info);
383
384
@@ -685,7 +686,7 @@ bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
685
686
// AbstractStorageDecl.
686
687
assert (getParentDecl () == D);
687
688
auto PreviousTop = EntitiesStack.pop_back_val ();
688
- bool initFailed = initFuncRefIndexSymbol (getCurrentExpr (), getParentExpr (), D, Loc, Info);
689
+ bool initFailed = initFuncRefIndexSymbol (D, Loc, Info);
689
690
EntitiesStack.push_back (PreviousTop);
690
691
691
692
if (initFailed)
@@ -821,7 +822,7 @@ bool IndexSwiftASTWalker::reportRef(ValueDecl *D, SourceLoc Loc,
821
822
return true ; // keep walking
822
823
823
824
if (isa<AbstractFunctionDecl>(D)) {
824
- if (initFuncRefIndexSymbol (getCurrentExpr (), getParentExpr (), D, Loc, Info))
825
+ if (initFuncRefIndexSymbol (D, Loc, Info))
825
826
return true ;
826
827
} else if (isa<AbstractStorageDecl>(D)) {
827
828
if (initVarRefIndexSymbols (getCurrentExpr (), D, Loc, Info))
@@ -1021,33 +1022,49 @@ static bool isDynamicCall(Expr *BaseE, ValueDecl *D) {
1021
1022
return true ;
1022
1023
}
1023
1024
1024
- bool IndexSwiftASTWalker::initFuncRefIndexSymbol (Expr *CurrentE, Expr *ParentE,
1025
- ValueDecl *D, SourceLoc Loc,
1025
+ static bool isBeingCalled (Expr *Target, Expr *Parent, Expr *GrandParent) {
1026
+ if (!Target || !Parent || !isa<ApplyExpr>(Parent))
1027
+ return false ;
1028
+
1029
+ if (!isa<SelfApplyExpr>(Parent))
1030
+ return cast<ApplyExpr>(Parent)->getFn () == Target;
1031
+
1032
+ return GrandParent && isa<CallExpr>(GrandParent) &&
1033
+ cast<CallExpr>(GrandParent)->getFn () == Parent;
1034
+ }
1035
+
1036
+ bool IndexSwiftASTWalker::initFuncRefIndexSymbol (ValueDecl *D, SourceLoc Loc,
1026
1037
IndexSymbol &Info) {
1027
1038
1028
1039
if (initIndexSymbol (D, Loc, /* IsRef=*/ true , Info))
1029
1040
return true ;
1030
1041
1042
+ Expr *CurrentE = getCurrentExpr ();
1031
1043
if (!CurrentE)
1032
1044
return false ;
1033
1045
1046
+ Expr *ParentE = getParentExpr ();
1047
+
1034
1048
// FIXME: the below check maintains existing indexing behavior with
1035
1049
// pseudo/accessor output but seems incorrect. E.g otherGlobal in:
1036
1050
// let global = otherGlobal
1037
1051
// will not have a parent expression so no accessor call is reported
1038
- if (!ParentE)
1052
+ if (isa<AbstractStorageDecl>(D) && !ParentE)
1039
1053
return true ;
1040
1054
1041
- Info.roles |= (unsigned )SymbolRole::Call;
1055
+ if (!isa<AbstractStorageDecl>(D) &&
1056
+ !isBeingCalled (CurrentE, ParentE, getContainingExpr (2 )))
1057
+ return false ;
1042
1058
1043
- Decl *ParentD = getParentDecl ();
1044
- if (ParentD && isa<ValueDecl>(ParentD)) {
1045
- if (addRelation (Info, (SymbolRoleSet) SymbolRole::RelationCalledBy, cast<ValueDecl>(ParentD)))
1046
- return true ;
1059
+
1060
+ Info.roles |= (unsigned )SymbolRole::Call;
1061
+ if (auto *Caller = dyn_cast_or_null<AbstractFunctionDecl>(getParentDecl ())) {
1062
+ if (addRelation (Info, (SymbolRoleSet) SymbolRole::RelationCalledBy, Caller))
1063
+ return true ;
1047
1064
}
1048
1065
1049
1066
Expr *BaseE = nullptr ;
1050
- if (auto DotE = dyn_cast <DotSyntaxCallExpr>(ParentE))
1067
+ if (auto DotE = dyn_cast_or_null <DotSyntaxCallExpr>(ParentE))
1051
1068
BaseE = DotE->getBase ();
1052
1069
else if (auto MembE = dyn_cast<MemberRefExpr>(CurrentE))
1053
1070
BaseE = MembE->getBase ();
0 commit comments