[clang] 3357e48 - [clang/APINotes] Fix assertion crash in addObjCMethod for protocol DesignatedInit methods (#183799)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 2 07:42:35 PST 2026
Author: Med Ismail Bennani
Date: 2026-03-02T07:42:30-08:00
New Revision: 3357e487cf0ee7bb9a565cb79ac7ea65f12583b0
URL: https://github.com/llvm/llvm-project/commit/3357e487cf0ee7bb9a565cb79ac7ea65f12583b0
DIFF: https://github.com/llvm/llvm-project/commit/3357e487cf0ee7bb9a565cb79ac7ea65f12583b0.diff
LOG: [clang/APINotes] Fix assertion crash in addObjCMethod for protocol DesignatedInit methods (#183799)
Added:
clang/test/APINotes/objc_designated_init_protocol.m
Modified:
clang/lib/APINotes/APINotesWriter.cpp
clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/APINotes/SomeKit.apinotes
clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/Headers/SomeKit.h
Removed:
################################################################################
diff --git a/clang/lib/APINotes/APINotesWriter.cpp b/clang/lib/APINotes/APINotesWriter.cpp
index 47ed93a567c0e..390aea57f3915 100644
--- a/clang/lib/APINotes/APINotesWriter.cpp
+++ b/clang/lib/APINotes/APINotesWriter.cpp
@@ -50,6 +50,9 @@ class APINotesWriter::Implementation {
/// Indexed by context ID, provides the parent context ID.
llvm::DenseMap<uint32_t, uint32_t> ParentContexts;
+ /// Mapping from context IDs to the kind of context.
+ llvm::DenseMap<unsigned, uint8_t> ContextKinds;
+
/// Mapping from context IDs to the identifier ID holding the name.
llvm::DenseMap<unsigned, unsigned> ContextNames;
@@ -1461,6 +1464,7 @@ ContextID APINotesWriter::addContext(std::optional<ContextID> ParentCtxID,
Implementation->ContextNames[NextID] = NameID;
Implementation->ParentContexts[NextID] = RawParentCtxID;
+ Implementation->ContextKinds[NextID] = static_cast<uint8_t>(Kind);
}
// Add this version information.
@@ -1505,7 +1509,7 @@ void APINotesWriter::addObjCMethod(ContextID CtxID, ObjCSelectorRef Selector,
assert(Implementation->ParentContexts.contains(CtxID.Value));
uint32_t ParentCtxID = Implementation->ParentContexts[CtxID.Value];
ContextTableKey CtxKey(ParentCtxID,
- static_cast<uint8_t>(ContextKind::ObjCClass),
+ Implementation->ContextKinds[CtxID.Value],
Implementation->ContextNames[CtxID.Value]);
assert(Implementation->Contexts.contains(CtxKey));
auto &VersionedVec = Implementation->Contexts[CtxKey].second;
diff --git a/clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/APINotes/SomeKit.apinotes b/clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/APINotes/SomeKit.apinotes
index 817af123fc77b..5f3116924491b 100644
--- a/clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/APINotes/SomeKit.apinotes
+++ b/clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/APINotes/SomeKit.apinotes
@@ -45,6 +45,12 @@ Classes:
- Name: intPropertyToMangle
PropertyKind: Instance
Type: 'double *'
+Protocols:
+ - Name: InitializableProtocolDUMP
+ Methods:
+ - Selector: "initWithValue:"
+ MethodKind: Instance
+ DesignatedInit: true
Functions:
- Name: global_int_fun
ResultType: 'char *'
diff --git a/clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/Headers/SomeKit.h b/clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/Headers/SomeKit.h
index 1a192f5432fd1..06390b9ea1d4e 100644
--- a/clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/Headers/SomeKit.h
+++ b/clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/Headers/SomeKit.h
@@ -57,4 +57,8 @@ __attribute__((objc_root_class))
@property (class, nonatomic, readwrite, retain) A *implicitGetSetClass;
@end
+ at protocol InitializableProtocolDUMP
+- (instancetype)initWithValue:(int)value;
+ at end
+
#endif
diff --git a/clang/test/APINotes/objc_designated_init_protocol.m b/clang/test/APINotes/objc_designated_init_protocol.m
new file mode 100644
index 0000000000000..b999cc27b3377
--- /dev/null
+++ b/clang/test/APINotes/objc_designated_init_protocol.m
@@ -0,0 +1,17 @@
+// RUN: rm -rf %t && mkdir -p %t
+// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fapinotes-modules -Wno-private-module -fsyntax-only -I %S/Inputs/Headers -F %S/Inputs/Frameworks %s
+
+// Regression test for APINotesWriter::addObjCMethod asserting when a protocol
+// method is annotated with DesignatedInit: true in an .apinotes file.
+// The writer was reconstructing the ContextTableKey with a hardcoded
+// ContextKind::ObjCClass, causing the Contexts lookup to fail when the
+// context was actually a protocol. Importing the module is sufficient to
+// trigger the writer path that previously crashed.
+
+// expected-no-diagnostics
+
+#import <SomeKit/SomeKit.h>
+
+id<InitializableProtocolDUMP> useProtocol(id<InitializableProtocolDUMP> p) {
+ return [p initWithValue:0];
+}
More information about the cfe-commits
mailing list