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

feature request: add option to map nonull to ptr::NonNull #1791

Open
Dushistov opened this issue May 20, 2020 · 3 comments
Open

feature request: add option to map nonull to ptr::NonNull #1791

Dushistov opened this issue May 20, 2020 · 3 comments

Comments

@Dushistov
Copy link
Contributor

There is supported by clang extension to mark pointers as nonnull

Input C/C++ Header

#if defined(__clang__)
#define C4NONNULL __attribute((nonnull))
#else
#define C4NONNULL /**/
#endif

void f(int *p C4NONNULL);

It would be nice to have option to automatically convert types with such attribute to std::ptr::NonNull,
this repr(transparent) type so this would be valid transformation.
I deal with big enough C API where a lot of argument types have such attribute,
and this automatic conversation will help.

@emilio
Copy link
Contributor

emilio commented May 21, 2020

We have other code for reading attributes see warn_unused_result in the tests.

I don't expect this to be that much harder.

@madsmtm
Copy link

madsmtm commented Dec 10, 2021

I've looked at this a bit, and would like to try implementing it, but I think I'm gonna need some help with it:

From what I can tell we need to use clang_Type_getNullability. As I understand it, that only works on types whose kind are CXType_Attributed. These are not exposed unless we specify CXTranslationUnit_IncludeAttributedTypes in clang_parseTranslationUnit.

All of these things require libclang 8.0, so my first question is how would we do the version checks? Do we need to somehow weakly link clang_Type_getNullability (weak linking is not yet really supported by Rust), or use cargo features, or is there another better way?

Next, I was considering parsing that into a new TypeKind::Attributed(inner), kind of like TypeKind::Pointer, but adding it as a new type seems like a big change for what should be a small feature. Maybe you know of a better way to handle this?

Lastly, I'm having problems with getting the "inner" type, neither ty.pointee_type() nor ty.canonical_type() returns the right thing.

Thanks in advance!

Related: #1876 (the _Nonnull attribute / @property(nonnull) works exactly the same way).

@emilio
Copy link
Contributor

emilio commented Feb 18, 2022

Sorry for the lag replying here.

All of these things require libclang 8.0, so my first question is how would we do the version checks? Do we need to somehow weakly link clang_Type_getNullability (weak linking is not yet really supported by Rust), or use cargo features, or is there another better way?

In terms of the version checks, yeah, you can do something like:

if clang::clang_Type_getNullability::is_loaded() {
  // Add extra flag to translation unit flags, use the function.
}

Regarding the inner type, it seems the right function to use is clang_Type_getModifiedType.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants