Skip to content

Commit 5d3fb8f

Browse files
committed
[SymbolGraph] Fix crasher when retrieving cursor info of method defined in ObjC
In a mixed Objective-C / Swift module, we have a Clang module overlay that’s a Source file, not a serialized AST as is currently assumed. That assumption caused a crash when retrieving the symbol graph as part of a cursor info request to SourceKit, which was invoked on a method defined in the Objective-C part of the module. To fix the crash, recursively use the same logic that already exists to serialize a module to also serialize the clang overlay module since that function alreayd correctly handles the distinction between source files and serialized ASTs. Resolves rdar://76951147
1 parent 1bd6086 commit 5d3fb8f

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"
@@ -524,38 +522,7 @@ void SymbolGraph::serialize(llvm::json::OStream &OS) {
524522
}
525523
AttributeRAII Platform("platform", OS);
526524

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

561528
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)