-
Notifications
You must be signed in to change notification settings - Fork 819
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
Adding _Nonnull and _Nullable attributes to mujoco.h
APIs
#309
Comments
Great idea. Just to add, for GCC there are some differences, nuances, will share below. Isn't _Nullable is different from attribute((nonnull)), they're actually the opposite. void f(const char * str) attribute((nonnull)); void f(const char * str) Someone investigated this problem and saw it removed NULL checks http://www.rkoucha.fr/tech_corner/nonnull_gcc_attribute.html wget2 removed the nonnull attribute due to the optimizer removing checks against NULL too |
Thanks for the reminder! Since @kbayes's deb14dc, the API reference docs are now autogenerated, which removes my main concern for these decorations: that they will clutter the API and decrease readibility.
|
Can I look into this @yuvaltassa ? Are there any additional considerations beyond the one mentioned above that might be helpful for me to start? |
You could use a macro, to only enable the attribute nonnull on a specific build, just to get the warnings that attribute nonnull found. I wouldn't put it on a real build that was ever run. gcc with -fanalyze -02 should give some other NULL warnings if it finds some :) |
@varshneydevansh sure, go ahead and see where you get with this! Looking forward to continuing the discussion over a PR 🙂 |
Thank you, @yuvaltassa, I will do this soon as I fell into a little health trouble. Once I am fit, I will create a PR. =) |
So, I sat down an hour ago and being able to understand what we need to do -
I think I need to add something like this in the file also - #ifdef __clang__
#define _Nullable _Nullable
#define _Nonnull _Nonnull
#else
#define _Nullable
#define _Nonnull __attribute__((nonnull))
#endif |
|
https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Function-Attributes.html gcc follows a different format. Useful when not all parameters can't be NULL. I give an example below with only 3 of the 5 parameters specified as never NULL extern void * Anyway, ensure to only do it on a build for warnings, as otherwise all your if(NULL != ptr) checks will be optimized out by the compiler. |
So should I add something like this in the #ifdef __clang__
#define _Nonnull _Nonnull
#define _Nullable _Nullable
#elif defined(__GNUC__)
#define _Nonnull __attribute__((nonnull))
#define _Nullable
#else
#define _Nonnull
#define _Nullable
#endif I am also a little unclear about the reference in for the other decorators, which might be useful for us - The above snippet reference is defining Furthermore, GCC does not have an equivalent of |
Does that _Nonnull compile with latest GCC? I don't recall it being supported within the parameter list. I thought always need to specify the argument number, you can use my marco : #define NONNULL_ARG_NUM(x, y) attribute((nonnull(x, y))) |
I have added the macros in the file //-------------------------- nullability check ----------------------------------------------------
#ifdef __clang__
#define NONNULL_ARG(x) _Nonnull
#define NULLABLE _Nullable
#elif defined(__GNUC__)
#define NONNULL_ARG(x) __attribute__((nonnull(x)))
#define NULLABLE
#else
#define NONNULL_ARG(x)
#define NULLABLE
#endif
and the changes to the function I am doing like this - example - // load model from file and check for errors
m = mj_loadXML("hello.xml", NULL, error, 1000); in mujoco.h - // Parse XML file in MJCF or URDF format, compile it, return low-level model.
// If vfs is not NULL, look up files in vfs before reading from disk.
// If error is not NULL, it must have size error_sz.
MJAPI mjModel* mj_loadXML(const char* NONNULL_ARG(1) filename, const mjVFS* NULLABLE vfs, char* NULLABLE error, int error_sz);
|
Fine, I made some mistakes with the above approach and did modify a lot of function definitions in the mujoco.h file with the above approach. After, a bit of more dig up, I think this is what we are looking for and now correcting my mistake- define V8_NONNULL(...) attribute((nonnull(VA_ARGS)))
#ifdef __clang__
#define NONNULL_ARG _Nonnull
#define NULLABLE_ARG _Nullable
#define NONNULL_FUNC(...) /* NOT SUPPORTED */
#elif defined(__GNUC__)
#define NONNULL_ARG /* NOT SUPPORTED */
#define NULLABLE_ARG /* NOT SUPPORTED */
#define NONNULL_FUNC(...) __attribute__((nonnull(__VA_ARGS__)))
#else
#define NONNULL_ARG /* NOT SUPPORTED */
#define NULLABLE_ARG /* NOT SUPPORTED */
#define NONNULL_FUNC(...) /* NOT SUPPORTED */
#endif
// Parse XML file in MJCF or URDF format, compile it, return low-level model.
// If vfs is not NULL, look up files in vfs before reading from disk.
// If error is not NULL, it must have size error_sz.
MJAPI mjModel* mj_loadXML(const char* NONNULL_ARG filename, const mjVFS* NULLABLE_ARG vfs, char* NULLABLE_ARG error, int error_sz) NONNULL_FUNC(1);
|
I am a little lost with the modification for the https://mujoco.readthedocs.io/en/latest/APIreference/APIfunctions.html?highlight=mj_multiRay#ray-collisions I am creating the PR so that I can get a better feedback. =) |
varshneydevansh, How about adding this comment, so it's clear it's for the optimizer : /* _Nonnull allows the compiler to opimize out all the NULL pointer checks */ |
Can someone edit the title of this PR so it is the correct spelling of _Nonnull? |
mujoco.h
APIsmujoco.h
APIs
Is your feature request related to a problem? Please describe.
When using MuJoCo API, it is a bit harder to parse either automatically or from documentation when a parameter can be NULL or not. For example,
mj_step
requiresmjData* d
to be nonnull, whilemj_copyData
can take NULLmjData* dest
. Similarly,mjv_makeScene
can take NULLconst mjModel* m
. There seems to be no systematic way to understand which parameter can be NULL which cannot.Describe the solution you'd like
New compilers, such as clang starts to support _Nullable attribute: https://clang.llvm.org/docs/AttributeReference.html#nullable
These can be
#define
away on unsupported compilers (For example, on GCC, you can define that as__attribute__((nonnull))
: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-AttributesDescribe alternatives you've considered
This information could be annotated in the comment like how we annotated length of array in public struct. It doesn't provide the said compiler check.
Additional context
These nullability attributes can be helpful when generating interface glue code to MuJoCo library. For example, swift-mujoco right now assumes all fields to be nonnull and have to manually implement selected APIs that can be NULL. If nullability attribute added, these can be automatically generated like the rest of APIs.
The text was updated successfully, but these errors were encountered: