https://github.com/ian-twilightcoder updated https://github.com/llvm/llvm-project/pull/246933
>From 193badab1edb7f9a72abd2030cb5c716242c8260 Mon Sep 17 00:00:00 2001 From: Ian Anderson <[email protected]> Date: Sat, 28 Nov 2026 21:40:06 -0800 Subject: [PATCH] [Modules][ObjC] Objective-C categories aren't visible in local submodule visibility Objective-C categories are never visible under local submodule visibility because ObjCInterfaceDecl::isVisible{Category|Extension} doesn't check Sema::isVisible. In fact they can't check Sema because that's at a higher level. Instead add an isDeclVisible to ASTContext, and have Sema set a callback on ASTContext to let it call through to Sema::isVisible. Co-Authored-By: Claude Opus 5.5 <[email protected]> rdar://82149585 --- clang/include/clang/AST/ASTContext.h | 17 ++ clang/include/clang/AST/DeclObjC.h | 9 - clang/lib/AST/ASTContext.cpp | 6 + clang/lib/AST/DeclObjC.cpp | 9 + clang/lib/Sema/Sema.cpp | 6 + clang/test/Modules/lsv-objc.m | 376 +++++++++++++++++++++++++++ 6 files changed, 414 insertions(+), 9 deletions(-) create mode 100644 clang/test/Modules/lsv-objc.m diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 9ad16898b32563..ed251fd28ca0d9 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -47,6 +47,7 @@ #include "llvm/ADT/StringSet.h" #include "llvm/ADT/TinyPtrVector.h" #include "llvm/Support/TypeSize.h" +#include <functional> #include <optional> namespace llvm { @@ -851,6 +852,8 @@ class ASTContext : public RefCountedBase<ASTContext> { /// Keeps track of the deallocated DeclListNodes for future reuse. DeclListNode *ListNodeFreeList = nullptr; + std::function<bool(const NamedDecl *)> DeclVisibilityCallback; + public: IdentifierTable &Idents; SelectorTable &Selectors; @@ -1495,6 +1498,20 @@ class ASTContext : public RefCountedBase<ASTContext> { return ExternalSource; } + /// Set the callback that isDeclVisible() uses for declarations that are not + /// unconditionally visible. + void + setDeclVisibilityCallback(std::function<bool(const NamedDecl *)> Callback) { + DeclVisibilityCallback = std::move(Callback); + } + + /// Determine whether \p D is visible. + /// + /// This is for AST-level queries that filter declarations by visibility + /// without going through name lookup, such as finding the visible + /// categories of an Objective-C class. + bool isDeclVisible(const NamedDecl *D) const; + /// Attach an AST mutation listener to the AST context. /// /// The AST mutation listener provides the ability to track modifications to diff --git a/clang/include/clang/AST/DeclObjC.h b/clang/include/clang/AST/DeclObjC.h index 6b9aac57e6a500..301c7e2ae4ca08 100644 --- a/clang/include/clang/AST/DeclObjC.h +++ b/clang/include/clang/AST/DeclObjC.h @@ -2946,15 +2946,6 @@ ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() { return *this; } -inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) { - return !Cat->isInvalidDecl() && Cat->isUnconditionallyVisible(); -} - -inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) { - return !Cat->isInvalidDecl() && Cat->IsClassExtension() && - Cat->isUnconditionallyVisible(); -} - inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) { return !Cat->isInvalidDecl() && Cat->IsClassExtension(); } diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index e30510684a6a2a..89f103bebfecc6 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -1018,6 +1018,12 @@ ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) { ExternalSource = std::move(Source); } +bool ASTContext::isDeclVisible(const NamedDecl *D) const { + if (D->isUnconditionallyVisible()) + return true; + return DeclVisibilityCallback && DeclVisibilityCallback(D); +} + void ASTContext::PrintStats() const { llvm::errs() << "\n*** AST Context Stats:\n"; llvm::errs() << " " << Types.size() << " types total.\n"; diff --git a/clang/lib/AST/DeclObjC.cpp b/clang/lib/AST/DeclObjC.cpp index 1adf6c9e048a97..88dc296759524c 100644 --- a/clang/lib/AST/DeclObjC.cpp +++ b/clang/lib/AST/DeclObjC.cpp @@ -316,6 +316,15 @@ ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration( void ObjCInterfaceDecl::anchor() {} +bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) { + return !Cat->isInvalidDecl() && Cat->getASTContext().isDeclVisible(Cat); +} + +bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) { + return !Cat->isInvalidDecl() && Cat->IsClassExtension() && + Cat->getASTContext().isDeclVisible(Cat); +} + ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const { // If this particular declaration has a type parameter list, return it. if (ObjCTypeParamList *written = getTypeParamListAsWritten()) diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 0bd387f2c88d71..000e9e0470d881 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -345,6 +345,9 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, // Tell diagnostics how to render things from the AST library. Diags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument, &Context); + Context.setDeclVisibilityCallback( + [this](const NamedDecl *D) { return isVisible(D); }); + // This evaluation context exists to ensure that there's always at least one // valid evaluation context available. It is never removed from the // evaluation stack. @@ -625,6 +628,9 @@ Sema::~Sema() { = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) ExternalSema->ForgetSema(); + // Clear the ASTContext's callback to us. + Context.setDeclVisibilityCallback(nullptr); + // Delete cached satisfactions. for (auto [_, Node] : SatisfactionCache) delete Node; diff --git a/clang/test/Modules/lsv-objc.m b/clang/test/Modules/lsv-objc.m new file mode 100644 index 00000000000000..e1168627e602f3 --- /dev/null +++ b/clang/test/Modules/lsv-objc.m @@ -0,0 +1,376 @@ +// RUN: rm -rf %t +// RUN: split-file %s %t + +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -I %t -x objective-c -emit-module -fmodule-name=MyLibrary %t/module.modulemap -o %t/MyLibrary-no-lsv.pcm +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -I %t -fmodule-file=MyLibrary=%t/MyLibrary-no-lsv.pcm %t/base.m -verify +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -I %t -fmodule-file=MyLibrary=%t/MyLibrary-no-lsv.pcm %t/extras.m -verify +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -I %t -fmodule-file=MyLibrary=%t/MyLibrary-no-lsv.pcm %t/module.m -verify + +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -fmodules-local-submodule-visibility -I %t -x objective-c -emit-module -fmodule-name=MyLibrary %t/module.modulemap -o %t/MyLibrary-lsv.pcm +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -fmodules-local-submodule-visibility -I %t -fmodule-file=MyLibrary=%t/MyLibrary-lsv.pcm -x objective-c -emit-module -fmodule-name=M %t/module.modulemap -o %t/M.pcm -verify +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -fmodules-local-submodule-visibility -I %t -fmodule-file=MyLibrary=%t/MyLibrary-lsv.pcm -fmodule-file=M=%t/M.pcm %t/M.m -verify +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -fmodules-local-submodule-visibility -I %t -fmodule-file=MyLibrary=%t/MyLibrary-lsv.pcm -fmodule-file=M=%t/M.pcm %t/A.m -verify +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -fmodules-local-submodule-visibility -I %t -fmodule-file=MyLibrary=%t/MyLibrary-lsv.pcm -fmodule-file=M=%t/M.pcm %t/B.m -verify + +//--- module.modulemap +module MyLibrary { + module MyObject { + header "MyObject.h" + export * + } + module Extras { + header "MyObject+Extras.h" + export * + } +} + +module M { + // Imports MyObject+Extras.h + module A { + header "A.h" + export * + } + // Only imports MyObject.h. Without LSV, the extras will + // leak in from submodule A. But with LSV, only the base + // methods will be visible, as would be the case when + // B.h was included by itself without modules. + module B { + header "B.h" + export * + } +} + +//--- MyObject.h +@interface MyObject { +@public + int interfaceIvar; +} +@property int interfaceProperty; +@property (class) int interfaceClassProperty; +@property (readonly) int redeclaredProperty; +- (void)interfaceMethod; ++ (void)interfaceClassMethod; +@end + +@interface MyObject (MyObject) +@property int categoryProperty; +@property (class) int categoryClassProperty; +- (void)categoryMethod; ++ (void)categoryClassMethod; +@end + +//--- MyObject+Extras.h +#import <MyObject.h> + +@interface MyObject (Category) +@property int extraCategoryProperty; +@property (class) int extraCategoryClassProperty; +- (void)extraCategoryMethod; ++ (void)extraCategoryClassMethod; +@end + +@interface MyObject () { +@public + int extensionIvar; +} +@property int extensionProperty; +@property (class) int extensionClassProperty; +@property (readwrite) int redeclaredProperty; +- (instancetype)initWithInt:(int)value __attribute__((objc_designated_initializer)); +- (void)extensionMethod; ++ (void)extensionClassMethod; +@end + +//--- A.h +#import <MyObject+Extras.h> + +static inline void test_typed_instance_A(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; + [object extensionMethod]; + object.interfaceProperty = 0; + object.categoryProperty = 0; + object.extraCategoryProperty = 0; + object.extensionProperty = 0; + object.redeclaredProperty = 0; + object->interfaceIvar = 0; + object->extensionIvar = 0; +} + +static inline void test_typed_class_A(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; + [MyObject extensionClassMethod]; + MyObject.interfaceClassProperty = 0; + MyObject.categoryClassProperty = 0; + MyObject.extraCategoryClassProperty = 0; + MyObject.extensionClassProperty = 0; +} + +//--- B.h +#import <MyObject.h> + +static inline void test_typed_instance_B(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; // expected-warning {{instance method '-extraCategoryMethod' not found}} + // [email protected]:* {{receiver is instance of class declared here}} + [object extensionMethod]; // expected-warning {{instance method '-extensionMethod' not found}} + // [email protected]:* {{receiver is instance of class declared here}} + object.interfaceProperty = 0; + object.categoryProperty = 0; + object->interfaceIvar = 0; +} + +static inline void test_typed_class_B(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; // expected-warning {{class method '+extraCategoryClassMethod' not found}} + [MyObject extensionClassMethod]; // expected-warning {{class method '+extensionClassMethod' not found}} + MyObject.interfaceClassProperty = 0; + MyObject.categoryClassProperty = 0; +} + +//--- base.m +#import <MyObject.h> + +@interface Subclass : MyObject +- (instancetype)initWithSubclassInt:(int)value __attribute__((objc_designated_initializer)); // #initWithSubclassInt +@end + +@implementation Subclass +- (instancetype)initWithSubclassInt:(int)value { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}} + // expected-note@#initWithSubclassInt {{method marked as designated initializer of the class here}} + return [super initWithInt:value]; // expected-warning {{instance method '-initWithInt:' not found}} + // [email protected]:* {{receiver is instance of class declared here}} +} +@end + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; // expected-warning {{instance method '-extraCategoryMethod' not found}} + // [email protected]:* {{receiver is instance of class declared here}} + [object extensionMethod]; // expected-warning {{instance method '-extensionMethod' not found}} + // [email protected]:* {{receiver is instance of class declared here}} + object.interfaceProperty = 0; + object.categoryProperty = 0; + object.extraCategoryProperty = 0; // expected-error {{property 'extraCategoryProperty' not found on object of type 'MyObject *'}} + // [email protected]:* {{'categoryProperty' declared here}} + object.extensionProperty = 0; // expected-error {{property 'extensionProperty' not found on object of type 'MyObject *'}} + object.redeclaredProperty = 0; // expected-error {{assignment to readonly property}} + object->interfaceIvar = 0; + object->extensionIvar = 0; // expected-error {{'MyObject' does not have a member named 'extensionIvar'}} +} + +void test_typed_class(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; // expected-warning {{class method '+extraCategoryClassMethod' not found}} + [MyObject extensionClassMethod]; // expected-warning {{class method '+extensionClassMethod' not found}} + MyObject.interfaceClassProperty = 0; + MyObject.categoryClassProperty = 0; + MyObject.extraCategoryClassProperty = 0; // expected-error {{property 'extraCategoryClassProperty' not found on object of type 'MyObject'}} + MyObject.extensionClassProperty = 0; // expected-error {{property 'extensionClassProperty' not found on object of type 'MyObject'}} +} + +//--- extras.m +#import <MyObject+Extras.h> + +@interface Subclass : MyObject +- (instancetype)initWithSubclassInt:(int)value __attribute__((objc_designated_initializer)); +@end + +@implementation Subclass // expected-warning {{method override for the designated initializer of the superclass '-initWithInt:' not found}} +// expected-note@MyObject+Extras.h:* {{method marked as designated initializer of the class here}} +- (instancetype)initWithSubclassInt:(int)value { + return [super initWithInt:value]; +} +@end + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; + [object extensionMethod]; + object.interfaceProperty = 0; + object.categoryProperty = 0; + object.extraCategoryProperty = 0; + object.extensionProperty = 0; + object.redeclaredProperty = 0; + object->interfaceIvar = 0; + object->extensionIvar = 0; +} + +void test_typed_class(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; + [MyObject extensionClassMethod]; + MyObject.interfaceClassProperty = 0; + MyObject.categoryClassProperty = 0; + MyObject.extraCategoryClassProperty = 0; + MyObject.extensionClassProperty = 0; +} + +//--- module.m +@import MyLibrary; + +@interface Subclass : MyObject +- (instancetype)initWithSubclassInt:(int)value __attribute__((objc_designated_initializer)); +@end + +@implementation Subclass // expected-warning {{method override for the designated initializer of the superclass '-initWithInt:' not found}} +// expected-note@MyObject+Extras.h:* {{method marked as designated initializer of the class here}} +- (instancetype)initWithSubclassInt:(int)value { + return [super initWithInt:value]; +} +@end + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; + [object extensionMethod]; + object.interfaceProperty = 0; + object.categoryProperty = 0; + object.extraCategoryProperty = 0; + object.extensionProperty = 0; + object.redeclaredProperty = 0; + object->interfaceIvar = 0; + object->extensionIvar = 0; +} + +void test_typed_class(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; + [MyObject extensionClassMethod]; + MyObject.interfaceClassProperty = 0; + MyObject.categoryClassProperty = 0; + MyObject.extraCategoryClassProperty = 0; + MyObject.extensionClassProperty = 0; +} + +//--- M.m +@import M; + +@interface Subclass : MyObject +- (instancetype)initWithSubclassInt:(int)value __attribute__((objc_designated_initializer)); +@end + +@implementation Subclass // expected-warning {{method override for the designated initializer of the superclass '-initWithInt:' not found}} +// expected-note@MyObject+Extras.h:* {{method marked as designated initializer of the class here}} +- (instancetype)initWithSubclassInt:(int)value { + return [super initWithInt:value]; +} +@end + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; + [object extensionMethod]; + object.interfaceProperty = 0; + object.categoryProperty = 0; + object.extraCategoryProperty = 0; + object.extensionProperty = 0; + object.redeclaredProperty = 0; + object->interfaceIvar = 0; + object->extensionIvar = 0; +} + +void test_typed_class(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; + [MyObject extensionClassMethod]; + MyObject.interfaceClassProperty = 0; + MyObject.categoryClassProperty = 0; + MyObject.extraCategoryClassProperty = 0; + MyObject.extensionClassProperty = 0; +} + +//--- A.m +#import <A.h> + +@interface Subclass : MyObject +- (instancetype)initWithSubclassInt:(int)value __attribute__((objc_designated_initializer)); +@end + +@implementation Subclass // expected-warning {{method override for the designated initializer of the superclass '-initWithInt:' not found}} +// expected-note@MyObject+Extras.h:* {{method marked as designated initializer of the class here}} +- (instancetype)initWithSubclassInt:(int)value { + return [super initWithInt:value]; +} +@end + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; + [object extensionMethod]; + object.interfaceProperty = 0; + object.categoryProperty = 0; + object.extraCategoryProperty = 0; + object.extensionProperty = 0; + object.redeclaredProperty = 0; + object->interfaceIvar = 0; + object->extensionIvar = 0; +} + +void test_typed_class(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; + [MyObject extensionClassMethod]; + MyObject.interfaceClassProperty = 0; + MyObject.categoryClassProperty = 0; + MyObject.extraCategoryClassProperty = 0; + MyObject.extensionClassProperty = 0; +} + +//--- B.m +#import <B.h> + +@interface Subclass : MyObject +- (instancetype)initWithSubclassInt:(int)value __attribute__((objc_designated_initializer)); // #initWithSubclassInt +@end + +@implementation Subclass +- (instancetype)initWithSubclassInt:(int)value { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}} + // expected-note@#initWithSubclassInt {{method marked as designated initializer of the class here}} + return [super initWithInt:value]; // expected-warning {{instance method '-initWithInt:' not found}} + // [email protected]:* {{receiver is instance of class declared here}} +} +@end + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; // expected-warning {{instance method '-extraCategoryMethod' not found}} + // [email protected]:* {{receiver is instance of class declared here}} + [object extensionMethod]; // expected-warning {{instance method '-extensionMethod' not found}} + // [email protected]:* {{receiver is instance of class declared here}} + object.interfaceProperty = 0; + object.categoryProperty = 0; + object.extraCategoryProperty = 0; // expected-error {{property 'extraCategoryProperty' not found on object of type 'MyObject *'}} + // [email protected]:* {{'categoryProperty' declared here}} + object.extensionProperty = 0; // expected-error {{property 'extensionProperty' not found on object of type 'MyObject *'}} + object.redeclaredProperty = 0; // expected-error {{assignment to readonly property}} + object->interfaceIvar = 0; + object->extensionIvar = 0; // expected-error {{'MyObject' does not have a member named 'extensionIvar'}} +} + +void test_typed_class(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; // expected-warning {{class method '+extraCategoryClassMethod' not found}} + [MyObject extensionClassMethod]; // expected-warning {{class method '+extensionClassMethod' not found}} + MyObject.interfaceClassProperty = 0; + MyObject.categoryClassProperty = 0; + MyObject.extraCategoryClassProperty = 0; // expected-error {{property 'extraCategoryClassProperty' not found on object of type 'MyObject'}} + MyObject.extensionClassProperty = 0; // expected-error {{property 'extensionClassProperty' not found on object of type 'MyObject'}} +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
