Skip to content

Commit fa748a3

Browse files
Merge pull request #655 from rolfbjarne/objccategorydecl-gettypeparamlist
Check 'ObjCCategoryDecl->getTypeParamList()' for null before trying to use the result.
2 parents e925bfe + 6571c92 commit fa748a3

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

sources/libClangSharp/ClangSharp.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3069,7 +3069,11 @@ int clangsharp_Cursor_getNumArguments(CXCursor C) {
30693069
}
30703070

30713071
if (const ObjCCategoryDecl* OCCD = dyn_cast<ObjCCategoryDecl>(D)) {
3072-
return OCCD->getTypeParamList()->size();
3072+
ObjCTypeParamList* typeParamList = OCCD->getTypeParamList();
3073+
if (typeParamList != nullptr) {
3074+
return typeParamList->size();
3075+
}
3076+
return 0;
30733077
}
30743078
}
30753079

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
2+
3+
using System.Linq;
4+
using NUnit.Framework;
5+
6+
namespace ClangSharp.UnitTests;
7+
8+
[Platform("macosx")]
9+
public sealed class ObjectiveCTest : TranslationUnitTest
10+
{
11+
[Test]
12+
[Ignore("TODO: this needs a new version of libClangSharp published.")]
13+
public void Category_TypeParamList()
14+
{
15+
var inputContents = $@"
16+
@interface MyClass
17+
@end
18+
@interface MyClass (MyCategory)
19+
@end
20+
";
21+
22+
using var translationUnit = CreateTranslationUnit(inputContents, "objective-c++");
23+
24+
var categories = translationUnit.TranslationUnitDecl.Decls.OfType<ObjCCategoryDecl>().ToList ();
25+
Assert.That (categories.Count, Is.EqualTo (1), "Count");
26+
foreach (var c in categories) {
27+
Assert.That(c.TypeParamList.Count, Is.EqualTo (0), "TypeParamList.Count");
28+
}
29+
}
30+
}

tests/ClangSharp.UnitTests/TranslationUnitTest.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
22

3+
using System;
4+
using System.Collections.Generic;
35
using System.Text;
46
using ClangSharp.Interop;
57
using NUnit.Framework;
@@ -19,19 +21,24 @@ public abstract class TranslationUnitTest
1921
protected static readonly string[] DefaultClangCommandLineArgs =
2022
[
2123
"-std=c++17", // The input files should be compiled for C++ 17
22-
"-xc++", // The input files are C++
2324
"-Wno-pragma-once-outside-header" // We are processing files which may be header files
2425
];
2526

26-
protected static TranslationUnit CreateTranslationUnit(string inputContents)
27+
protected static TranslationUnit CreateTranslationUnit(string inputContents, string language = "c++" /* input files are C++ by default */)
2728
{
2829
Assert.That(DefaultInputFileName, Does.Exist);
2930

3031
using var unsavedFile = CXUnsavedFile.Create(DefaultInputFileName, inputContents);
3132
var unsavedFiles = new CXUnsavedFile[] { unsavedFile };
3233

3334
var index = CXIndex.Create();
34-
var translationUnit = CXTranslationUnit.Parse(index, DefaultInputFileName, DefaultClangCommandLineArgs, unsavedFiles, DefaultTranslationUnitFlags);
35+
var arguments = new List<string>(DefaultClangCommandLineArgs)
36+
{
37+
"-x",
38+
language,
39+
};
40+
var errorCode = CXTranslationUnit.TryParse(index, DefaultInputFileName, arguments.ToArray(), unsavedFiles, DefaultTranslationUnitFlags, out var translationUnit);
41+
Assert.That(errorCode, Is.EqualTo(CXErrorCode.CXError_Success), $"Failed to create {nameof(CXTranslationUnit)} (error code: {errorCode})");
3542

3643
if (translationUnit.NumDiagnostics != 0)
3744
{

0 commit comments

Comments
 (0)