Skip to content

Commit 1d97379

Browse files
authored
Merge pull request #42353 from bnbarham/add-objc-loc
[CursorInfo] Add ObjC location to generated symbol graph
2 parents d6cc8e8 + 680bf2e commit 1d97379

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/SymbolGraphGen/Symbol.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/Basic/Unicode.h"
2222
#include "clang/AST/ASTContext.h"
2323
#include "clang/AST/Decl.h"
24+
#include "clang/Basic/SourceManager.h"
2425
#include "AvailabilityMixin.h"
2526
#include "JSON.h"
2627
#include "Symbol.h"
@@ -409,6 +410,30 @@ void Symbol::serializeAccessLevelMixin(llvm::json::OStream &OS) const {
409410
}
410411

411412
void Symbol::serializeLocationMixin(llvm::json::OStream &OS) const {
413+
if (ClangNode ClangN = VD->getClangNode()) {
414+
if (!Graph->Walker.Options.IncludeClangDocs)
415+
return;
416+
417+
if (auto *ClangD = ClangN.getAsDecl()) {
418+
clang::SourceManager &ClangSM =
419+
ClangD->getASTContext().getSourceManager();
420+
421+
clang::PresumedLoc Loc = ClangSM.getPresumedLoc(ClangD->getLocation());
422+
if (Loc.isValid()) {
423+
// TODO: We should use a common function to fill in the location
424+
// information for both cursor info and symbol graph gen, then also
425+
// include position here.
426+
OS.attributeObject("location", [&](){
427+
SmallString<1024> FileURI("file://");
428+
FileURI.append(Loc.getFilename());
429+
OS.attribute("uri", FileURI.str());
430+
});
431+
}
432+
}
433+
434+
return;
435+
}
436+
412437
auto Loc = VD->getLoc(/*SerializedOK=*/true);
413438
if (Loc.isInvalid()) {
414439
return;

test/SourceKit/CursorInfo/cursor_symbol_graph_objc.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// REQUIRES: objc_interop
22

3+
// TODO: Add some way to specify extra options to symbolgraph-extract and then
4+
// split this test into parts under the SymbolGraph directory. The SK
5+
// tests should just check that we run the generation on 1. the correct
6+
// symbol and 2. with the correct options.
7+
38
// RUN: %empty-directory(%t)
49
// RUN: split-file --leading-lines %s %t
510

@@ -10,6 +15,9 @@ func test(s: ObjCStruct) {
1015
// RUN: %sourcekitd-test -req=cursor -pos=%(line+1):3 -req-opts=retrieve_symbol_graph=1 %t/use.swift -- -I %t/mod -target %target-triple %t/use.swift | %FileCheck -check-prefix=CHECK-FUNC %s
1116
someFunc()
1217

18+
// RUN: %sourcekitd-test -req=cursor -pos=%(line+1):3 -req-opts=retrieve_symbol_graph=1 %t/use.swift -- -I %t/mod -target %target-triple %t/use.swift | %FileCheck -check-prefix=CHECK-DIRECTIVE-FUNC %s
19+
funcUnderDirective()
20+
1321
// RUN: %sourcekitd-test -req=cursor -pos=%(line+1):9 -req-opts=retrieve_symbol_graph=1 %t/use.swift -- -I %t/mod -target %target-triple %t/use.swift | %FileCheck -check-prefix=CHECK-NO %s
1422
_ = s.noDoc
1523

@@ -100,6 +108,9 @@ func test(s: ObjCStruct) {
100108
// CHECK-FUNC: "displayName": "Function",
101109
// CHECK-FUNC: "identifier": "swift.func"
102110
// CHECK-FUNC: },
111+
// CHECK-FUNC: "location": {
112+
// CHECK-FUNC: "uri": "file://{{.*}}mod{{\\\\|/}}M.h"
113+
// CHECK-FUNC: },
103114
// CHECK-FUNC: "names": {
104115
// CHECK-FUNC: "subHeading": [
105116
// CHECK-FUNC: {
@@ -266,3 +277,9 @@ struct ObjCStruct {
266277
// CHECK-MIXED-DOC-NEXT: ]
267278
// CHECK-MIXED-DOC-NEXT: }
268279
};
280+
281+
#line 10 "other.h"
282+
void funcUnderDirective(void);
283+
// CHECK-DIRECTIVE-FUNC: "location": {
284+
// CHECK-DIRECTIVE-FUNC: "uri": "file://{{.*}}other.h"
285+
// CHECK-DIRECTIVE-FUNC: }

0 commit comments

Comments
 (0)