Skip to content

Commit 48ea4ac

Browse files
committed
[SourceKit] Avoid reporting parent locations for internal only parameter names. rdar://30702790
1 parent b7b4be8 commit 48ea4ac

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

test/SourceKit/CursorInfo/cursor_label.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ class C1 {
22
init(cc: Int) {}
33
func foo(aa : Int) {}
44
subscript(aa : Int, bb: Int)-> Int { get { return 0 } set {}}
5+
func foo(_ aa : Int) {}
6+
init(_ cc: Int) {}
7+
subscript(_ aa : Int)-> Int { get { return 0 } set {}}
58
}
69
let c = C1(cc: 1)
710
c.foo(aa : 1)
811

912
// RUN: %sourcekitd-test -req=cursor -pos=2:9 %s -- %s | %FileCheck %s -check-prefix=CHECK1
1013
// RUN: %sourcekitd-test -req=cursor -pos=3:13 %s -- %s | %FileCheck %s -check-prefix=CHECK2
1114
// RUN: %sourcekitd-test -req=cursor -pos=4:24 %s -- %s | %FileCheck %s -check-prefix=CHECK3
15+
// RUN: %sourcekitd-test -req=cursor -pos=5:15 %s -- %s | %FileCheck %s -check-prefix=CHECK-NONE
16+
// RUN: %sourcekitd-test -req=cursor -pos=6:11 %s -- %s | %FileCheck %s -check-prefix=CHECK-NONE
17+
// RUN: %sourcekitd-test -req=cursor -pos=7:16 %s -- %s | %FileCheck %s -check-prefix=CHECK-NONE
1218

1319
// CHECK1: PARENT OFFSET: 13
1420
// CHECK2: PARENT OFFSET: 37
1521
// CHECK3: PARENT OFFSET: 56
22+
// CHECK-NONE-NOT: PARENT OFFSET:

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,16 @@ static bool passCursorInfoForModule(ModuleEntity Mod,
613613
return false;
614614
}
615615

616-
static Optional<unsigned> getParamParentNameOffset(const ValueDecl *VD) {
616+
static Optional<unsigned>
617+
getParamParentNameOffset(const ValueDecl *VD, SourceLoc Cursor) {
618+
if (Cursor.isInvalid())
619+
return None;
617620
SourceLoc Loc;
618621
if (auto PD = dyn_cast<ParamDecl>(VD)) {
622+
623+
// Avoid returning parent loc for internal-only names.
624+
if (PD->getArgumentNameLoc().isValid() && PD->getNameLoc() == Cursor)
625+
return None;
619626
auto *DC = PD->getDeclContext();
620627
switch (DC->getContextKind()) {
621628
case DeclContextKind::SubscriptDecl:
@@ -642,6 +649,7 @@ static bool passCursorInfoForDecl(const ValueDecl *VD,
642649
const Type ContainerTy,
643650
bool IsRef,
644651
Optional<unsigned> OrigBufferID,
652+
SourceLoc CursorLoc,
645653
SwiftLangSupport &Lang,
646654
const CompilerInvocation &Invok,
647655
ArrayRef<ImmutableTextSnapshotRef> PreviousASTSnaps,
@@ -875,7 +883,7 @@ static bool passCursorInfoForDecl(const ValueDecl *VD,
875883
Info.LocalizationKey = LocalizationKey;
876884
Info.IsSystem = IsSystem;
877885
Info.TypeInterface = StringRef();
878-
Info.ParentNameOffset = getParamParentNameOffset(VD);
886+
Info.ParentNameOffset = getParamParentNameOffset(VD, CursorLoc);
879887
Receiver(Info);
880888
return false;
881889
}
@@ -1152,7 +1160,7 @@ static void resolveCursor(SwiftLangSupport &Lang,
11521160
bool Failed = passCursorInfoForDecl(VD, MainModule,
11531161
SemaTok.ContainerType,
11541162
SemaTok.ContainerType,
1155-
SemaTok.IsRef, BufferID, Lang,
1163+
SemaTok.IsRef, BufferID, Loc, Lang,
11561164
CompInvok, getPreviousASTSnaps(),
11571165
Receiver);
11581166
if (Failed) {
@@ -1406,7 +1414,7 @@ void SwiftLangSupport::getCursorInfo(
14061414
// it's not necessary.
14071415
passCursorInfoForDecl(
14081416
Entity.Dcl, /*MainModule*/ nullptr, Type(), Type(), Entity.IsRef,
1409-
/*OrigBufferID=*/None, *this, Invok, {}, Receiver);
1417+
/*OrigBufferID=*/None, SourceLoc(), *this, Invok, {}, Receiver);
14101418
}
14111419
} else {
14121420
Receiver({});
@@ -1593,8 +1601,8 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
15931601
}
15941602
bool Failed =
15951603
passCursorInfoForDecl(VD, MainModule, selfTy, Type(),
1596-
/*IsRef=*/false, BufferID, Lang, CompInvok,
1597-
PreviousASTSnaps, Receiver);
1604+
/*IsRef=*/false, BufferID, SourceLoc(), Lang,
1605+
CompInvok, PreviousASTSnaps, Receiver);
15981606
if (Failed) {
15991607
if (!PreviousASTSnaps.empty()) {
16001608
// Attempt again using the up-to-date AST.

0 commit comments

Comments
 (0)