Skip to content

Commit 2c4bee9

Browse files
author
Nathan Hawes
authored
Merge pull request #8094 from nathawes/rdar31039915-index-local-params
[indexer] Index parameter definitions with a distinct external argument label too (but not their refs)
2 parents 5c01a65 + 7b75cf7 commit 2c4bee9

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/Demangling/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->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
@@ -56,7 +56,7 @@ printArtificialName(const swift::AbstractStorageDecl *ASD, AccessorKind AK, llvm
5656
}
5757

5858
static bool printDisplayName(const swift::ValueDecl *D, llvm::raw_ostream &OS) {
59-
if (!D->hasName()) {
59+
if (!D->hasName() && !isa<ParamDecl>(D)) {
6060
auto *FD = dyn_cast<FuncDecl>(D);
6161
if (!FD || FD->getAccessorKind() == AccessorKind::NotAccessor)
6262
return true;
@@ -401,22 +401,17 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
401401
return SrcMgr.getLineAndColumn(Loc, BufferID);
402402
}
403403

404-
bool shouldIndex(ValueDecl *D) const {
404+
bool shouldIndex(ValueDecl *D, bool IsRef) const {
405405
if (D->isImplicit())
406406
return false;
407-
if (isLocal(D))
407+
if (isLocalSymbol(D) && (!isa<ParamDecl>(D) || IsRef))
408408
return false;
409409
if (D->isPrivateStdlibDecl())
410410
return false;
411411

412412
return true;
413413
}
414414

415-
bool isLocal(ValueDecl *D) const {
416-
return D->getDeclContext()->getLocalContext() &&
417-
(!isa<ParamDecl>(D) || cast<ParamDecl>(D)->getArgumentNameLoc().isValid());
418-
}
419-
420415
void getModuleHash(SourceFileOrModule SFOrMod, llvm::raw_ostream &OS);
421416
llvm::hash_code hashModule(llvm::hash_code code, SourceFileOrModule SFOrMod);
422417
llvm::hash_code hashFileReference(llvm::hash_code code,
@@ -578,7 +573,7 @@ bool IndexSwiftASTWalker::startEntity(Decl *D, IndexSymbol &Info) {
578573
}
579574

580575
bool IndexSwiftASTWalker::startEntityDecl(ValueDecl *D) {
581-
if (!shouldIndex(D))
576+
if (!shouldIndex(D, /*IsRef=*/false))
582577
return false;
583578

584579
SourceLoc Loc = D->getLoc();
@@ -631,7 +626,7 @@ bool IndexSwiftASTWalker::startEntityDecl(ValueDecl *D) {
631626

632627
bool IndexSwiftASTWalker::reportRelatedRef(ValueDecl *D, SourceLoc Loc, bool isImplicit,
633628
SymbolRoleSet Relations, Decl *Related) {
634-
if (!shouldIndex(D))
629+
if (!shouldIndex(D, /*IsRef=*/true))
635630
return true;
636631

637632
IndexSymbol Info;
@@ -697,7 +692,7 @@ bool IndexSwiftASTWalker::reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet
697692
bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
698693
AccessorKind AccKind, bool IsRef,
699694
SourceLoc Loc) {
700-
if (!shouldIndex(D))
695+
if (!shouldIndex(D, IsRef))
701696
return true; // continue walking.
702697

703698
auto updateInfo = [this, D, AccKind](IndexSymbol &Info) {
@@ -765,7 +760,7 @@ bool IndexSwiftASTWalker::reportExtension(ExtensionDecl *D) {
765760
NominalTypeDecl *NTD = D->getExtendedType()->getAnyNominal();
766761
if (!NTD)
767762
return true;
768-
if (!shouldIndex(NTD))
763+
if (!shouldIndex(NTD, /*IsRef=*/false))
769764
return true;
770765

771766
IndexSymbol Info;
@@ -850,7 +845,7 @@ static bool hasUsefulRoleInSystemModule(SymbolRoleSet roles) {
850845

851846
bool IndexSwiftASTWalker::reportRef(ValueDecl *D, SourceLoc Loc,
852847
IndexSymbol &Info) {
853-
if (!shouldIndex(D))
848+
if (!shouldIndex(D, /*IsRef=*/true))
854849
return true; // keep walking
855850

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

lib/Index/IndexSymbol.cpp

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

211+
if (isLocalSymbol(D)) {
212+
info.Properties |= SymbolProperty::Local;
213+
}
214+
211215
return info;
212216
}
213217

@@ -227,3 +231,8 @@ SymbolSubKind index::getSubKindForAccessor(AccessorKind AK) {
227231

228232
llvm_unreachable("Unhandled AccessorKind in switch.");
229233
}
234+
235+
bool index::isLocalSymbol(const swift::Decl *D) {
236+
return D->getDeclContext()->getLocalContext() &&
237+
(!isa<ParamDecl>(D) || cast<ParamDecl>(D)->getArgumentNameLoc().isValid());
238+
}

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)