Skip to content

Commit e7d5603

Browse files
authored
Merge pull request #37133 from ahoppen/pr/serialize-text-module-symbolgraph
[SymbolGraph] Fix crasher when retrieving cursor info of method defined in ObjC
2 parents 1e544b0 + 5d3fb8f commit e7d5603

File tree

6 files changed

+57
-34
lines changed

6 files changed

+57
-34
lines changed

lib/SymbolGraphGen/JSON.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
// Adds Symbol Graph JSON serialization to other types.
1313
//===----------------------------------------------------------------------===//
1414

15+
#include "swift/AST/ASTContext.h"
1516
#include "swift/AST/Decl.h"
17+
#include "swift/AST/FileUnit.h"
1618
#include "swift/AST/GenericParamList.h"
1719
#include "swift/AST/Module.h"
1820
#include "swift/AST/Type.h"
1921
#include "swift/AST/USRGeneration.h"
22+
#include "swift/ClangImporter/ClangModule.h"
23+
#include "swift/Serialization/SerializedModuleLoader.h"
2024
#include "JSON.h"
2125

2226
void swift::symbolgraphgen::serialize(const llvm::VersionTuple &VT,
@@ -128,6 +132,39 @@ void swift::symbolgraphgen::serialize(const swift::GenericTypeParamType *Param,
128132
});
129133
}
130134

135+
void swift::symbolgraphgen::serialize(const ModuleDecl &Module,
136+
llvm::json::OStream &OS,
137+
llvm::Triple Target) {
138+
auto *MainFile = Module.getFiles().front();
139+
switch (MainFile->getKind()) {
140+
case FileUnitKind::Builtin:
141+
llvm_unreachable("Unexpected module kind: Builtin");
142+
case FileUnitKind::DWARFModule:
143+
llvm_unreachable("Unexpected module kind: DWARFModule");
144+
case FileUnitKind::Synthesized:
145+
llvm_unreachable("Unexpected module kind: Synthesized");
146+
break;
147+
case FileUnitKind::Source:
148+
serialize(Module.getASTContext().LangOpts.Target, OS);
149+
break;
150+
case FileUnitKind::SerializedAST: {
151+
auto SerializedAST = cast<SerializedASTFile>(MainFile);
152+
auto Target = llvm::Triple(SerializedAST->getTargetTriple());
153+
serialize(Target, OS);
154+
break;
155+
}
156+
case FileUnitKind::ClangModule: {
157+
auto ClangModule = cast<ClangModuleUnit>(MainFile);
158+
if (const auto *Overlay = ClangModule->getOverlayModule()) {
159+
serialize(*Overlay, OS, Target);
160+
} else {
161+
serialize(Target, OS);
162+
}
163+
break;
164+
}
165+
}
166+
}
167+
131168
void
132169
swift::symbolgraphgen::filterGenericParams(
133170
TypeArrayView<GenericTypeParamType> GenericParams,

lib/SymbolGraphGen/JSON.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void serialize(const llvm::Triple &T, llvm::json::OStream &OS);
4343
void serialize(const ExtensionDecl *Extension, llvm::json::OStream &OS);
4444
void serialize(const Requirement &Req, llvm::json::OStream &OS);
4545
void serialize(const swift::GenericTypeParamType *Param, llvm::json::OStream &OS);
46+
void serialize(const ModuleDecl &M, llvm::json::OStream &OS, llvm::Triple Target);
4647

4748
void filterGenericParams(
4849
TypeArrayView<GenericTypeParamType> GenericParams,

lib/SymbolGraphGen/SymbolGraph.cpp

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
#include "swift/AST/ProtocolConformance.h"
1818
#include "swift/AST/USRGeneration.h"
1919
#include "swift/Basic/Version.h"
20-
#include "swift/ClangImporter/ClangModule.h"
2120
#include "swift/Sema/IDETypeChecking.h"
22-
#include "swift/Serialization/SerializedModuleLoader.h"
2321

2422
#include "DeclarationFragmentPrinter.h"
2523
#include "FormatVersion.h"
@@ -530,38 +528,7 @@ void SymbolGraph::serialize(llvm::json::OStream &OS) {
530528
}
531529
AttributeRAII Platform("platform", OS);
532530

533-
auto *MainFile = M.getFiles().front();
534-
switch (MainFile->getKind()) {
535-
case FileUnitKind::Builtin:
536-
llvm_unreachable("Unexpected module kind: Builtin");
537-
case FileUnitKind::DWARFModule:
538-
llvm_unreachable("Unexpected module kind: DWARFModule");
539-
case FileUnitKind::Synthesized:
540-
llvm_unreachable("Unexpected module kind: Synthesized");
541-
break;
542-
case FileUnitKind::Source:
543-
symbolgraphgen::serialize(M.getASTContext().LangOpts.Target, OS);
544-
break;
545-
case FileUnitKind::SerializedAST: {
546-
auto SerializedAST = cast<SerializedASTFile>(MainFile);
547-
auto Target = llvm::Triple(SerializedAST->getTargetTriple());
548-
symbolgraphgen::serialize(Target, OS);
549-
break;
550-
}
551-
case FileUnitKind::ClangModule: {
552-
auto ClangModule = cast<ClangModuleUnit>(MainFile);
553-
if (const auto *Overlay = ClangModule->getOverlayModule()) {
554-
auto &OverlayMainFile =
555-
Overlay->getMainFile(FileUnitKind::SerializedAST);
556-
auto SerializedAST = cast<SerializedASTFile>(OverlayMainFile);
557-
auto Target = llvm::Triple(SerializedAST.getTargetTriple());
558-
symbolgraphgen::serialize(Target, OS);
559-
} else {
560-
symbolgraphgen::serialize(Walker.Options.Target, OS);
561-
}
562-
break;
563-
}
564-
}
531+
symbolgraphgen::serialize(M, OS, Walker.Options.Target);
565532
});
566533

567534
if (ModuleVersion) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@interface MyObjcType
2+
- (void)foo;
3+
@end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
framework module MixedFramework {
2+
umbrella header "MixedFramework.h"
3+
4+
export *
5+
module * { export * }
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %empty-directory(%t.mcp)
2+
3+
public class MySwiftType {
4+
public func bar(x: MyObjcType) {
5+
// Check that we don't crash
6+
// RUN: %sourcekitd-test -req=cursor -pos=%(line + 1):11 -req-opts=retrieve_symbol_graph=1 %s -- -module-name MixedFramework -F %S/Inputs/mixed_framework -import-underlying-module -module-cache-path %t.mcp %s
7+
x.foo()
8+
}
9+
}

0 commit comments

Comments
 (0)