feature request: add option to map nonull to ptr::NonNull
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.
We have other code for reading attributes see warn_unused_result in the tests.
I don't expect this to be that much harder.
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: https://github.com/rust-lang/rust-bindgen/issues/1876 (the _Nonnull attribute / @property(nonnull) works exactly the same way).
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.