From 235f140b4c6ab8abdb34007952a8983c91fa8c22 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Fri, 18 Jul 2025 09:04:16 -0600 Subject: [PATCH] [SymbolGraphGen] check implicit clang decls for having extension parents (#83143) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves rdar://151911998 This PR addresses an uncommon situation in the symbol graph when importing Objective-C symbols into Swift. When a class conforms to a protocol that includes an initializer, that initializer is automatically generated for that class in the AST. This initializer has the same USR as the original protocol symbol, but is housed in a different DeclContext to indicate the membership. Right now, we catch this situation when the protocol conformance is declared on the class definition: There's a branch to check for "implicit" decls with an underlying Clang symbol, and creates a synthesized USR if that symbols DeclContext points to a type. However, when the protocol conformance is added in a category extension, the DeclContext for the generated initializer points to the extension, causing the symbol to bypass that check and get added to the symbol graph with a duplicated USR. This PR adds a check to look for ExtensionDecls as the DeclContext so that the symbol can correctly receive a synthesized USR. One of the tests in this PR (`SymbolGraph/ClangImporter/ObjCInitializer.swift`) tests a similar situation where this "implicit decl with a Clang node" is created: Some initializers in Objective-C get imported into Swift twice, with differently-adapted parameter names. This is covered by the original code, but i wanted to leave the test in because i broke this case in my initial investigation! 😅 The other test (`SymbolGraph/ClangImporter/ProtocolInitializer.swift`) tests the new behavior that is fixed by this PR. --- lib/SymbolGraphGen/SymbolGraphASTWalker.cpp | 13 +++++--- .../ClangImporter/ObjCInitializer.swift | 28 ++++++++++++++++ .../ClangImporter/ProtocolInitializer.swift | 32 +++++++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 test/SymbolGraph/ClangImporter/ObjCInitializer.swift create mode 100644 test/SymbolGraph/ClangImporter/ProtocolInitializer.swift diff --git a/lib/SymbolGraphGen/SymbolGraphASTWalker.cpp b/lib/SymbolGraphGen/SymbolGraphASTWalker.cpp index d95185f2fea..09b5c9430c6 100644 --- a/lib/SymbolGraphGen/SymbolGraphASTWalker.cpp +++ b/lib/SymbolGraphGen/SymbolGraphASTWalker.cpp @@ -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); } } @@ -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(VD->getDeclContext())) { + if (VD->isImplicit() && VD->getClangDecl()) { + const NominalTypeDecl *Parent = dyn_cast_or_null(VD->getDeclContext()); + if (!Parent) { + if (const auto *ParentExt = dyn_cast_or_null(VD->getDeclContext())) + Parent = ParentExt->getExtendedNominal(); + } + if (Parent) { SG->recordNode(Symbol(SG, VD, Parent)); return true; } diff --git a/test/SymbolGraph/ClangImporter/ObjCInitializer.swift b/test/SymbolGraph/ClangImporter/ObjCInitializer.swift new file mode 100644 index 00000000000..77dce000ee3 --- /dev/null +++ b/test/SymbolGraph/ClangImporter/ObjCInitializer.swift @@ -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 + +// 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 diff --git a/test/SymbolGraph/ClangImporter/ProtocolInitializer.swift b/test/SymbolGraph/ClangImporter/ProtocolInitializer.swift new file mode 100644 index 00000000000..c52423be030 --- /dev/null +++ b/test/SymbolGraph/ClangImporter/ProtocolInitializer.swift @@ -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 + +@optional +- (nullable id)initWithSomeNumber:(NSInteger)number; + +@end + +@interface MyClass : NSObject +@end + +@interface MyClass () +@end