Skip to content

Commit 4958645

Browse files
committed
Check 'ObjCCategoryDecl->getTypeParamList()' for null before trying to use the result.
1 parent 30d6d6b commit 4958645

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

sources/libClangSharp/ClangSharp.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3069,7 +3069,10 @@ 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)
3074+
return typeParamList->size();
3075+
return 0;
30733076
}
30743077
}
30753078

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
public void Category_TypeParamList()
13+
{
14+
var inputContents = $@"
15+
@interface MyClass
16+
@end
17+
@interface MyClass (MyCategory)
18+
@end
19+
";
20+
21+
using var translationUnit = CreateTranslationUnit(inputContents, "objective-c++");
22+
23+
var categories = translationUnit.TranslationUnitDecl.Decls.OfType<ObjCCategoryDecl>().ToList ();
24+
Assert.That (categories.Count, Is.EqualTo (1), "Count");
25+
foreach (var c in categories) {
26+
Assert.That(c.TypeParamList.Count, Is.EqualTo (0), "TypeParamList.Count");
27+
}
28+
}
29+
}

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)