Skip to content

[SymbolGraphGen] check implicit clang decls for having extension parents #83143

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
Jul 18, 2025
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
13 changes: 8 additions & 5 deletions lib/SymbolGraphGen/SymbolGraphASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ bool SymbolGraphASTWalker::walkToDeclPre(Decl *D, CharSourceRange Range) {

// We also might establish some synthesized members because we
// extended an external type.
if (ExtendedNominal->getModuleContext() != &M) {
if (!areModulesEqual(ExtendedNominal->getModuleContext(), &M)) {
ExtendedSG->recordConformanceSynthesizedMemberRelationships(Source);
}
}
Expand Down Expand Up @@ -341,10 +341,13 @@ bool SymbolGraphASTWalker::walkToDeclPre(Decl *D, CharSourceRange Range) {
// symbol, regardless of which class it's actually appearing on. To prevent
// multiple of these symbols colliding with each other, treat them as
// synthesized symbols and use their parent type as the base type.
if (VD->isImplicit() && VD->hasClangNode() &&
VD->getClangNode().getAsDecl()) {
if (const auto *Parent =
dyn_cast_or_null<NominalTypeDecl>(VD->getDeclContext())) {
if (VD->isImplicit() && VD->getClangDecl()) {
const NominalTypeDecl *Parent = dyn_cast_or_null<NominalTypeDecl>(VD->getDeclContext());
if (!Parent) {
if (const auto *ParentExt = dyn_cast_or_null<ExtensionDecl>(VD->getDeclContext()))
Parent = ParentExt->getExtendedNominal();
}
if (Parent) {
SG->recordNode(Symbol(SG, VD, Parent));
return true;
}
Expand Down
28 changes: 28 additions & 0 deletions test/SymbolGraph/ClangImporter/ObjCInitializer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// REQUIRES: objc_interop

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

// RUN: %target-swift-symbolgraph-extract -sdk %clang-importer-sdk -module-name ObjCInitializer -Fsystem %t -output-dir %t -pretty-print -v
// RUN: %FileCheck %s --input-file %t/ObjCInitializer.symbols.json
// RUN: %FileCheck %s --input-file %t/ObjCInitializer.symbols.json --check-prefix SYNTH

//--- ObjCInitializer.framework/Modules/module.modulemap
framework module ObjCInitializer [system] {
header "ObjCInitializer.h"
}

//--- ObjCInitializer.framework/Headers/ObjCInitializer.h
@import Foundation;

@interface NSScrubberLayoutAttributes : NSObject <NSCopying>

// This initializer gets imported twice - once as `init(forItemAt:)` and once as
// `init(forItemAtIndex:)`. Make sure one of them gets a synthesized USR
// CHECK: "precise": "c:objc(cs)NSScrubberLayoutAttributes(cm)layoutAttributesForItemAtIndex:"
// CHECK-NOT: "precise": "c:objc(cs)NSScrubberLayoutAttributes(cm)layoutAttributesForItemAtIndex:"
// SYNTH: "precise": "c:objc(cs)NSScrubberLayoutAttributes(cm)layoutAttributesForItemAtIndex:::SYNTHESIZED::c:objc(cs)NSScrubberLayoutAttributes",
// SYNTH-NOT: "precise": "c:objc(cs)NSScrubberLayoutAttributes(cm)layoutAttributesForItemAtIndex:::SYNTHESIZED::c:objc(cs)NSScrubberLayoutAttributes",
+ (instancetype)layoutAttributesForItemAtIndex:(NSInteger)index;

@end
32 changes: 32 additions & 0 deletions test/SymbolGraph/ClangImporter/ProtocolInitializer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// REQUIRES: objc_interop

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

// RUN: %target-swift-symbolgraph-extract -sdk %clang-importer-sdk -module-name ProtocolInitializer -I %t/ProtocolInitializer -output-dir %t -pretty-print -v
// RUN: %FileCheck %s --input-file %t/ProtocolInitializer.symbols.json

// the initializer's base USR should only appear as the source of one relationship
// CHECK: "source": "c:objc(pl)MyProtocol(im)initWithSomeNumber:"
// CHECK-NOT: "source": "c:objc(pl)MyProtocol(im)initWithSomeNumber:"

//--- ProtocolInitializer/module.modulemap
module ProtocolInitializer {
header "ProtocolInitializer.h"
}

//--- ProtocolInitializer/ProtocolInitializer.h
@import Foundation;

@protocol MyProtocol <NSObject>

@optional
- (nullable id)initWithSomeNumber:(NSInteger)number;

@end

@interface MyClass : NSObject
@end

@interface MyClass () <MyProtocol>
@end