Skip to content

Commit d0e44c8

Browse files
Merge pull request #672 from rolfbjarne/objcinterface-protocols
Add support for protocols in @interface definitions (ObjCInterfaceDecl).
2 parents ce6bd07 + f13935e commit d0e44c8

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

sources/libClangSharp/ClangSharp.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3446,6 +3446,15 @@ int clangsharp_Cursor_getNumProtocols(CXCursor C) {
34463446
if (const ObjCProtocolDecl* OCPD = dyn_cast<ObjCProtocolDecl>(D)) {
34473447
return OCPD->protocol_size();
34483448
}
3449+
3450+
if (const ObjCInterfaceDecl* OCID = dyn_cast<ObjCInterfaceDecl>(D)) {
3451+
unsigned n = 0;
3452+
3453+
for (auto protocol : OCID->protocols()) {
3454+
n++;
3455+
}
3456+
return n;
3457+
}
34493458
}
34503459

34513460
return -1;
@@ -3764,6 +3773,17 @@ CXCursor clangsharp_Cursor_getProtocol(CXCursor C, unsigned i) {
37643773
n++;
37653774
}
37663775
}
3776+
3777+
if (const ObjCInterfaceDecl* OCID = dyn_cast<ObjCInterfaceDecl>(D)) {
3778+
unsigned n = 0;
3779+
3780+
for (auto protocol : OCID->protocols()) {
3781+
if (n == i) {
3782+
return MakeCXCursor(protocol, getCursorTU(C));
3783+
}
3784+
n++;
3785+
}
3786+
}
37673787
}
37683788

37693789
return clang_getNullCursor();

tests/ClangSharp.UnitTests/ObjectiveCTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,33 @@ @interface MyClass (MyCategory)
6464
}
6565
}
6666

67+
[Test]
68+
public void ClassWithProtocols()
69+
{
70+
AssertNeedNewClangSharp();
71+
72+
var inputContents = $@"
73+
@protocol P1
74+
@end
75+
@protocol P2
76+
@end
77+
78+
@interface MyClass <P1, P2>
79+
@end
80+
";
81+
82+
using var translationUnit = CreateTranslationUnit(inputContents, "objective-c++");
83+
84+
var classes = translationUnit.TranslationUnitDecl.Decls.OfType<ObjCInterfaceDecl>().ToList();
85+
Assert.That(classes.Count, Is.GreaterThanOrEqualTo(1), $"At least one class");
86+
var myClass = classes.SingleOrDefault(v => v.Name == "MyClass")!;
87+
Assert.That(myClass, Is.Not.Null, "MyClass");
88+
var protocols = myClass.Protocols.ToList();
89+
Assert.That(protocols.Count, Is.EqualTo(2), "protocols.Count");
90+
Assert.That(protocols[0].Name, Is.EqualTo("P1"), "protocols[0].Name");
91+
Assert.That(protocols[1].Name, Is.EqualTo("P2"), "protocols[1].Name");
92+
}
93+
6794
[Test]
6895
public void Method_IsPropertyAccessor()
6996
{

0 commit comments

Comments
 (0)