Skip to content
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

Add function annotations and fix Clang error #1056

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

j-walther
Copy link

Hi!

This PR changes a few things to accomodate the following new annotations:

  • function-arg-prefix[arg]
  • function-arg-ident-prefix[arg]
  • function-prefix
  • function-postfix
  • function-ident-prefix

Annotations

I've overhauled the list parsing to allow for some special characters to be escaped - as I've noted in the docs.

In addition to that I've introduced a new data type for annotations called Dict. It's a mapping of string indices to annotation values.
This was necessary to accomodate the function-arg-prefix[arg] and function-arg-ident-prefix[arg] annotations so that in a function like

fn foo(bar: *const usize) {}

annotations to arguments can simply be created by adding cbindgen:function-arg-prefix[bar]. I much prefer this over indexing by number since that might inadvertently add the annotation to another argument if they're ever shifted around.

As a part of these changes I've added the CFuncArg struct to replace the previous tuple. Initially this was because I placed the ident_prefix there but it turned out to be easier to just reuse the one on CDecl.

These annotations allow specifying SAL annotations, Compiler-specific attributes (e.g. Clang) and macros in general.

Clang errors

The body example added an inline void foo() to body.compat.c which caused a compiler error in conjunction with -Wnon-c-typedef-for-linkage since the generated structs were untagged. This effectively broke the automated test suite for me.

I've changed it so that if cbindgen is generating C++ with C compatibility and the user adds anything using [export.body] or [export.pre_body] cbindgen will enforce tags for those structs.

Function affixes

The remaining annotations provide similar benefits but for functions.

Outlook

This PR lays the foundation for solving #982. For more advanced use-cases the annotations defined in this PR should still be available but hidden behind an abstraction layer for e.g. NonNull parameters.

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.

1 participant