Skip to content

[5.7][CursorInfo] Add ObjC location to generated symbol graph #42356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/SymbolGraphGen/Symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/Basic/Unicode.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/Basic/SourceManager.h"
#include "AvailabilityMixin.h"
#include "JSON.h"
#include "Symbol.h"
Expand Down Expand Up @@ -409,6 +410,30 @@ void Symbol::serializeAccessLevelMixin(llvm::json::OStream &OS) const {
}

void Symbol::serializeLocationMixin(llvm::json::OStream &OS) const {
if (ClangNode ClangN = VD->getClangNode()) {
if (!Graph->Walker.Options.IncludeClangDocs)
return;

if (auto *ClangD = ClangN.getAsDecl()) {
clang::SourceManager &ClangSM =
ClangD->getASTContext().getSourceManager();

clang::PresumedLoc Loc = ClangSM.getPresumedLoc(ClangD->getLocation());
if (Loc.isValid()) {
// TODO: We should use a common function to fill in the location
// information for both cursor info and symbol graph gen, then also
// include position here.
OS.attributeObject("location", [&](){
SmallString<1024> FileURI("file://");
FileURI.append(Loc.getFilename());
OS.attribute("uri", FileURI.str());
});
}
}

return;
}

auto Loc = VD->getLoc(/*SerializedOK=*/true);
if (Loc.isInvalid()) {
return;
Expand Down
17 changes: 17 additions & 0 deletions test/SourceKit/CursorInfo/cursor_symbol_graph_objc.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// REQUIRES: objc_interop

// TODO: Add some way to specify extra options to symbolgraph-extract and then
// split this test into parts under the SymbolGraph directory. The SK
// tests should just check that we run the generation on 1. the correct
// symbol and 2. with the correct options.

// RUN: %empty-directory(%t)
// RUN: split-file --leading-lines %s %t

Expand All @@ -10,6 +15,9 @@ func test(s: ObjCStruct) {
// 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
someFunc()

// 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
funcUnderDirective()

// 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
_ = s.noDoc

Expand Down Expand Up @@ -100,6 +108,9 @@ func test(s: ObjCStruct) {
// CHECK-FUNC: "displayName": "Function",
// CHECK-FUNC: "identifier": "swift.func"
// CHECK-FUNC: },
// CHECK-FUNC: "location": {
// CHECK-FUNC: "uri": "file://{{.*}}mod{{\\\\|/}}M.h"
// CHECK-FUNC: },
// CHECK-FUNC: "names": {
// CHECK-FUNC: "subHeading": [
// CHECK-FUNC: {
Expand Down Expand Up @@ -266,3 +277,9 @@ struct ObjCStruct {
// CHECK-MIXED-DOC-NEXT: ]
// CHECK-MIXED-DOC-NEXT: }
};

#line 10 "other.h"
void funcUnderDirective(void);
// CHECK-DIRECTIVE-FUNC: "location": {
// CHECK-DIRECTIVE-FUNC: "uri": "file://{{.*}}other.h"
// CHECK-DIRECTIVE-FUNC: }