https://github.com/ian-twilightcoder updated https://github.com/llvm/llvm-project/pull/246933
>From 12ad7b9c90f9c09dd8394aaf848f3214dbea2998 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-methods.m | 211 ++++++++++++++++++++++++++ 6 files changed, 249 insertions(+), 9 deletions(-) create mode 100644 clang/test/Modules/lsv-objc-methods.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-methods.m b/clang/test/Modules/lsv-objc-methods.m new file mode 100644 index 00000000000000..8aae10d382c17c --- /dev/null +++ b/clang/test/Modules/lsv-objc-methods.m @@ -0,0 +1,211 @@ +// 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=MethodTest %t/module.modulemap -o %t/MethodTest-no-lsv.pcm +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -I %t -fmodule-file=MethodTest=%t/MethodTest-no-lsv.pcm %t/base.m -verify +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -I %t -fmodule-file=MethodTest=%t/MethodTest-no-lsv.pcm %t/extras.m -verify +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -I %t -fmodule-file=MethodTest=%t/MethodTest-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=MethodTest %t/module.modulemap -o %t/MethodTest-lsv.pcm +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-map-file=%t/module.modulemap -fmodules-local-submodule-visibility -I %t -fmodule-file=MethodTest=%t/MethodTest-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=MethodTest=%t/MethodTest-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=MethodTest=%t/MethodTest-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=MethodTest=%t/MethodTest-lsv.pcm -fmodule-file=M=%t/M.pcm %t/B.m -verify + +//--- module.modulemap +module MethodTest { + 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 +- (void)interfaceMethod; ++ (void)interfaceClassMethod; +@end + +@interface MyObject (MyObject) +- (void)categoryMethod; ++ (void)categoryClassMethod; +@end + +//--- MyObject+Extras.h +#import <MyObject.h> + +@interface MyObject (Category) +- (void)extraCategoryMethod; ++ (void)extraCategoryClassMethod; +@end + +@interface MyObject () +- (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]; +} + +static inline void test_typed_class_A(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; + [MyObject extensionClassMethod]; +} + +//--- 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]:1 {{receiver is instance of class declared here}} + [object extensionMethod]; // expected-warning {{instance method '-extensionMethod' not found}} + // [email protected]:1 {{receiver is instance of class declared here}} +} + +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}} +} + +//--- base.m +#import <MyObject.h> + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; // expected-warning {{instance method '-extraCategoryMethod' not found}} + // [email protected]:1 {{receiver is instance of class declared here}} + [object extensionMethod]; // expected-warning {{instance method '-extensionMethod' not found}} + // [email protected]:1 {{receiver is instance of class declared here}} +} + +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}} +} + +//--- extras.m +// expected-no-diagnostics +#import <MyObject+Extras.h> + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; + [object extensionMethod]; +} + +void test_typed_class(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; + [MyObject extensionClassMethod]; +} + +//--- module.m +// expected-no-diagnostics +@import MethodTest; + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; + [object extensionMethod]; +} + +void test_typed_class(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; + [MyObject extensionClassMethod]; +} + +//--- M.m +// expected-no-diagnostics +@import M; + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; + [object extensionMethod]; +} + +void test_typed_class(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; + [MyObject extensionClassMethod]; +} + +//--- A.m +// expected-no-diagnostics +#import <A.h> + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; + [object extensionMethod]; +} + +void test_typed_class(void) { + [MyObject interfaceClassMethod]; + [MyObject categoryClassMethod]; + [MyObject extraCategoryClassMethod]; + [MyObject extensionClassMethod]; +} + +//--- B.m +#import <B.h> + +void test_typed_instance(MyObject *object) { + [object interfaceMethod]; + [object categoryMethod]; + [object extraCategoryMethod]; // expected-warning {{instance method '-extraCategoryMethod' not found}} + // [email protected]:1 {{receiver is instance of class declared here}} + [object extensionMethod]; // expected-warning {{instance method '-extensionMethod' not found}} + // [email protected]:1 {{receiver is instance of class declared here}} +} + +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}} +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
