Skip to content

Add support for C/C++ attribute annotate #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2024

Conversation

fredrikhr
Copy link
Contributor

Adds support to emit information about __attribute__((annotate("ANNOTATION"))) in the generated Code and XML.

The GNU attribute annotate or its clang equivalent can appear and might convey interesting information that may be useful in the generated bindings. ClangSharpPInvokeGenerator today has only very limited support for a very few attributes, but this annotate attribute can be a simple catch-all allowing developers to at least have some mechanism of transferring information from the C/C++ header to the generated code.

In my particular scenario I need to do post-processing on the generated bindings and the annotation attribute seemed like a good way to transfer knowledge from the C++ header to the generated XML. I simply overrode a macro that includes the attribute in a struct type declaration, and using the changes made in this PR I could now see the emitted attribute in my C# source and as an <attribute> element in the generated XML. In my example I use it as a marker to change the generated struct into a ref struct, because that's what I needed in that particular case.

Similarly to NativeTypeNameAttribute the generator will emit a synthetically generated attribute NativeAnnotation if at least one annotate attribute was encountered. Otherwise, this new attribute is emitted under the same conditions under which NativeTypeNameAttribute is emitted.

In C/C++ the annotate attribute has one mandatory string literal argument. That literal is emitted as the Annotation property on the NativeAnnotation attribute in C#. libClangSharp already supports the annotate attribute, so this PR simply changes the P/Invoke generator to actually do something when an annotate attribute is reported by Clang.

Comment on lines 805 to 808
if (!generator._hasAnnotateAttr)
{
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is potentially problematic for scenarios where the developer generates helper types independently of any header that provides annotations.

This is done in various downstream tools where the tool is invoked once per header file in a library (rather than just once with all headers being processed by that single invocation).

I think it would be better to generate it by default and have a check for --exclude NativeAnnotationAttribute to support users who don't want it generated instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I implemented this in c62fb62

@tannergooding
Copy link
Member

Rest of the changes look good to me

@fredrikhr fredrikhr force-pushed the attribute-annotate branch from 936e300 to c62fb62 Compare May 25, 2024 12:18
@tannergooding tannergooding merged commit f0501f7 into dotnet:main May 28, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants