Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/Abstractions/FieldDesc.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.

using System;
using System.Collections.Generic;
using ClangSharp.Interop;

namespace ClangSharp.Abstractions;
Expand All @@ -9,6 +10,7 @@ internal struct FieldDesc
{
public AccessSpecifier AccessSpecifier { get; set; }
public string? NativeTypeName { get; set; }
public IEnumerable<string>? CppAttributes { get; set; }
public string EscapedName { get; set; }
public string ParentName { get; set; }
public int? Offset { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ public void BeginField(in FieldDesc desc)
AddNativeTypeNameAttribute(desc.NativeTypeName);
}

if (desc.CppAttributes is not null)
{
AddCppAttributes(desc.CppAttributes);
}

if (desc.Location is { } location)
{
WriteSourceLocation(location, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ private void VisitFieldDecl(FieldDecl fieldDecl)
var desc = new FieldDesc {
AccessSpecifier = accessSpecifier,
NativeTypeName = nativeTypeName,
CppAttributes = _config.GenerateCppAttributes
? fieldDecl.Attrs.Select(x => EscapeString(x.Spelling))
: null,
EscapedName = escapedName,
ParentName = GetRemappedCursorName(parent),
Offset = offset,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests;

/// <summary>
/// Regression test for https://github.com/dotnet/ClangSharp/issues/444.
/// Under <c>generate-cpp-attributes</c>, a C++ attribute (e.g. a SAL <c>_Field_size_full_</c>
/// annotation surfaced as an <c>annotate</c> attribute) on a struct field was not emitted as a
/// <c>[CppAttributeList("...")]</c>, even though the same attribute on a parameter was. Fields now
/// emit the attribute the same way parameters do.
/// </summary>
[Platform("win")]
public sealed class SalFieldAttributeTest : PInvokeGeneratorTest
{
private const string InputContents = @"#define _Field_size_full_(x) __attribute__((annotate(""_Field_size_full_("" #x "")"")))

struct MyStruct
{
int count;
_Field_size_full_(count) int* data;
};
";

[Test]
public Task FieldSalAnnotationGeneratesCppAttributeList()
{
var expectedOutputContents = @"namespace ClangSharp.Test
{
public unsafe partial struct MyStruct
{
public int count;

[CppAttributeList(""_Field_size_full_(count)"")]
[NativeAnnotation(""_Field_size_full_(count)"")]
public int* data;
}
}
";

var expectedDiagnostics = new[] {
new Diagnostic(DiagnosticLevel.Warning, "Function like macro definition records are not supported: '_Field_size_full_'. Generated bindings may be incomplete.", "Line 1, Column 9 in ClangUnsavedFile.h")
};

return ValidateGeneratedCSharpLatestWindowsBindingsAsync(InputContents, expectedOutputContents, additionalConfigOptions: PInvokeGeneratorConfigurationOptions.GenerateCppAttributes, expectedDiagnostics: expectedDiagnostics);
}
}
Loading