Skip to content

Commit 7b75cf7

Browse files
author
Nathan Hawes
committed
[indexer] Index parameter definitions with a distinct external argument label too (but not their refs)
Parameter defs with a separate external argument label are marked with the 'Local' symbol property. This ensures the indexer has enough information for clients to match up a function's argument labels with its child parameter definitions. Resolves rdar://problem/31039915.
1 parent bd7acf1 commit 7b75cf7

File tree

6 files changed

+62
-19
lines changed

6 files changed

+62
-19
lines changed

include/swift/Index/IndexSymbol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct IndexSymbol : IndexRelation {
7979

8080
SymbolInfo getSymbolInfoForDecl(const Decl *D);
8181
SymbolSubKind getSubKindForAccessor(AccessorKind AK);
82+
bool isLocalSymbol(const Decl *D);
8283

8384
using clang::index::printSymbolProperties;
8485

lib/AST/ASTMangler.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/ASTVisitor.h"
2020
#include "swift/AST/Initializer.h"
2121
#include "swift/AST/Module.h"
22+
#include "swift/AST/ParameterList.h"
2223
#include "swift/AST/ProtocolConformance.h"
2324
#include "swift/AST/Mangle.h"
2425
#include "swift/Basic/ManglingUtils.h"
@@ -375,6 +376,29 @@ static bool isInPrivateOrLocalContext(const ValueDecl *D) {
375376
return isInPrivateOrLocalContext(nominal);
376377
}
377378

379+
static unsigned getUnnamedParamIndex(const Decl *D) {
380+
unsigned UnnamedIndex = 0;
381+
ArrayRef<ParameterList *> ParamLists;
382+
383+
if (auto AFD = dyn_cast<AbstractFunctionDecl>(D->getDeclContext())) {
384+
ParamLists = AFD->getParameterLists();
385+
} else if (auto ACE = dyn_cast<AbstractClosureExpr>(D->getDeclContext())) {
386+
ParamLists = ACE->getParameterLists();
387+
} else {
388+
llvm_unreachable("unhandled param context");
389+
}
390+
for (auto ParamList : ParamLists) {
391+
for (auto Param : *ParamList) {
392+
if (!Param->hasName()) {
393+
if (Param == D)
394+
return UnnamedIndex;
395+
++UnnamedIndex;
396+
}
397+
}
398+
}
399+
llvm_unreachable("param not found");
400+
}
401+
378402
void ASTMangler::appendDeclName(const ValueDecl *decl) {
379403
if (decl->getName().isOperator()) {
380404
appendIdentifier(translateOperator(decl->getName().str()));
@@ -394,6 +418,10 @@ void ASTMangler::appendDeclName(const ValueDecl *decl) {
394418
}
395419

396420
if (decl->getDeclContext()->isLocalContext()) {
421+
if (isa<ParamDecl>(decl) && !decl->hasName()) {
422+
// Mangle unnamed params with their ordering.
423+
return appendOperator("L", Index(getUnnamedParamIndex(decl)));
424+
}
397425
// Mangle local declarations with a numeric discriminator.
398426
return appendOperator("L", Index(decl->getLocalDiscriminator()));
399427
}

lib/AST/USRGeneration.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ static bool ShouldUseObjCUSR(const Decl *D) {
144144
bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
145145
using namespace Mangle;
146146

147-
if (!D->hasName() && (!isa<FuncDecl>(D) || cast<FuncDecl>(D)->getAccessorKind() == AccessorKind::NotAccessor))
147+
if (!D->hasName() && !isa<ParamDecl>(D) &&
148+
(!isa<FuncDecl>(D) ||
149+
cast<FuncDecl>(D)->getAccessorKind() == AccessorKind::NotAccessor))
148150
return true; // Ignore.
149151
if (D->getModuleContext()->isBuiltinModule())
150152
return true; // Ignore.

lib/Index/Index.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ printArtificialName(const swift::AbstractStorageDecl *ASD, AccessorKind AK, llvm
5151
}
5252

5353
static bool printDisplayName(const swift::ValueDecl *D, llvm::raw_ostream &OS) {
54-
if (!D->hasName()) {
54+
if (!D->hasName() && !isa<ParamDecl>(D)) {
5555
auto *FD = dyn_cast<FuncDecl>(D);
5656
if (!FD || FD->getAccessorKind() == AccessorKind::NotAccessor)
5757
return true;
@@ -396,22 +396,17 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
396396
return SrcMgr.getLineAndColumn(Loc, BufferID);
397397
}
398398

399-
bool shouldIndex(ValueDecl *D) const {
399+
bool shouldIndex(ValueDecl *D, bool IsRef) const {
400400
if (D->isImplicit())
401401
return false;
402-
if (isLocal(D))
402+
if (isLocalSymbol(D) && (!isa<ParamDecl>(D) || IsRef))
403403
return false;
404404
if (D->isPrivateStdlibDecl())
405405
return false;
406406

407407
return true;
408408
}
409409

410-
bool isLocal(ValueDecl *D) const {
411-
return D->getDeclContext()->getLocalContext() &&
412-
(!isa<ParamDecl>(D) || cast<ParamDecl>(D)->getArgumentNameLoc().isValid());
413-
}
414-
415410
void getModuleHash(SourceFileOrModule SFOrMod, llvm::raw_ostream &OS);
416411
llvm::hash_code hashModule(llvm::hash_code code, SourceFileOrModule SFOrMod);
417412
llvm::hash_code hashFileReference(llvm::hash_code code,
@@ -573,7 +568,7 @@ bool IndexSwiftASTWalker::startEntity(Decl *D, IndexSymbol &Info) {
573568
}
574569

575570
bool IndexSwiftASTWalker::startEntityDecl(ValueDecl *D) {
576-
if (!shouldIndex(D))
571+
if (!shouldIndex(D, /*IsRef=*/false))
577572
return false;
578573

579574
SourceLoc Loc = D->getLoc();
@@ -626,7 +621,7 @@ bool IndexSwiftASTWalker::startEntityDecl(ValueDecl *D) {
626621

627622
bool IndexSwiftASTWalker::reportRelatedRef(ValueDecl *D, SourceLoc Loc, bool isImplicit,
628623
SymbolRoleSet Relations, Decl *Related) {
629-
if (!shouldIndex(D))
624+
if (!shouldIndex(D, /*IsRef=*/true))
630625
return true;
631626

632627
IndexSymbol Info;
@@ -692,7 +687,7 @@ bool IndexSwiftASTWalker::reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet
692687
bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
693688
AccessorKind AccKind, bool IsRef,
694689
SourceLoc Loc) {
695-
if (!shouldIndex(D))
690+
if (!shouldIndex(D, IsRef))
696691
return true; // continue walking.
697692

698693
auto updateInfo = [this, D, AccKind](IndexSymbol &Info) {
@@ -760,7 +755,7 @@ bool IndexSwiftASTWalker::reportExtension(ExtensionDecl *D) {
760755
NominalTypeDecl *NTD = D->getExtendedType()->getAnyNominal();
761756
if (!NTD)
762757
return true;
763-
if (!shouldIndex(NTD))
758+
if (!shouldIndex(NTD, /*IsRef=*/false))
764759
return true;
765760

766761
IndexSymbol Info;
@@ -845,7 +840,7 @@ static bool hasUsefulRoleInSystemModule(SymbolRoleSet roles) {
845840

846841
bool IndexSwiftASTWalker::reportRef(ValueDecl *D, SourceLoc Loc,
847842
IndexSymbol &Info) {
848-
if (!shouldIndex(D))
843+
if (!shouldIndex(D, /*IsRef=*/true))
849844
return true; // keep walking
850845

851846
if (isa<AbstractFunctionDecl>(D)) {

lib/Index/IndexSymbol.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ SymbolInfo index::getSymbolInfoForDecl(const Decl *D) {
205205
break;
206206
}
207207

208+
if (isLocalSymbol(D)) {
209+
info.Properties |= SymbolProperty::Local;
210+
}
211+
208212
return info;
209213
}
210214

@@ -224,3 +228,8 @@ SymbolSubKind index::getSubKindForAccessor(AccessorKind AK) {
224228

225229
llvm_unreachable("Unhandled AccessorKind in switch.");
226230
}
231+
232+
bool index::isLocalSymbol(const swift::Decl *D) {
233+
return D->getDeclContext()->getLocalContext() &&
234+
(!isa<ParamDecl>(D) || cast<ParamDecl>(D)->getArgumentNameLoc().isValid());
235+
}

test/Index/kinds.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,23 @@ class AClass {
4444
// CHECK: [[@LINE-1]]:7 | class/Swift | AClass | s:14swift_ide_test6AClassC | Def | rel: 0
4545

4646
// InstanceMethod + Parameters
47-
func instanceMethod(a: Int, b b: Int, _ c: Int, d _: Int, _: Int) {}
47+
func instanceMethod(a: Int, b b: Int, _ c: Int, d _: Int, _: Int) {
4848
// CHECK: [[@LINE-1]]:8 | instance-method/Swift | instanceMethod(a:b:_:d:_:) | s:14swift_ide_test6AClassC14instanceMethodySi1a_Si1bS2i1dSitF | Def,RelChild | rel: 1
4949
// CHECK-NEXT: RelChild | class/Swift | AClass | s:14swift_ide_test6AClassC
5050
// CHECK: [[@LINE-3]]:23 | param/Swift | a | s:14swift_ide_test6AClassC14instanceMethodySi1a_Si1bS2i1dSitFAEL_Siv | Def,RelChild | rel: 1
5151
// CHECK-NEXT: RelChild | instance-method/Swift | instanceMethod(a:b:_:d:_:) | s:14swift_ide_test6AClassC14instanceMethodySi1a_Si1bS2i1dSitF
52-
// CHECK-NOT: [[@LINE-5]]:33 | param/Swift | b | s:{{.*}} | Def,RelChild | rel: 1
53-
// CHECK-NOT: [[@LINE-6]]:43 | param/Swift | c | s:{{.*}} | Def,RelChild | rel: 1
54-
// CHECK-NOT: [[@LINE-7]]:53 | param/Swift | d | s:{{.*}} | Def,RelChild | rel: 1
55-
// CHECK-NOT: [[@LINE-8]]:61 | param/Swift | _ | s:{{.*}} | Def,RelChild | rel: 1
52+
// CHECK: [[@LINE-5]]:33 | param(local)/Swift | b | s:{{.*}} | Def,RelChild | rel: 1
53+
// CHECK: [[@LINE-6]]:43 | param(local)/Swift | c | s:{{.*}} | Def,RelChild | rel: 1
54+
// CHECK: [[@LINE-7]]:53 | param(local)/Swift | _ | s:{{.*}} | Def,RelChild | rel: 1
55+
// CHECK: [[@LINE-8]]:61 | param/Swift | _ | s:{{.*}} | Def,RelChild | rel: 1
56+
57+
_ = a
58+
// CHECK: [[@LINE-1]]:9 | param/Swift | a | s:{{.*}} | Ref,Read,RelCont | rel: 1
59+
_ = b
60+
// CHECK-NOT: [[@LINE-1]]:9 | param(local)/Swift | b | s:{{.*}} | Ref,Read,RelCont | rel: 1
61+
_ = c
62+
// CHECK-NOT: [[@LINE-1]]:9 | param(local)/Swift | c | s:{{.*}} | Ref,Read,RelCont | rel: 1
63+
}
5664

5765
// ClassMethod
5866
class func classMethod() {}

0 commit comments

Comments
 (0)