-
Notifications
You must be signed in to change notification settings - Fork 32
Core: Add LV_NODISCARD for C API functions with non-discardable results #265
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
Conversation
hartwork
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kaixiong I like the idea. I think I found three cases that slipped through the cracks:
# git grep 'LV_API.*_new' | grep -v LV_NODISCARD
libvisual/libvisual/lv_param_value.h:LV_API VisParamValue *visual_param_value_new (VisParamType type, void *value);
libvisual/libvisual/lv_time.h:LV_API VisTimer *visual_timer_new (void);
# git grep 'LV_API.*clone' | grep -v LV_NODISCARD
libvisual/libvisual/lv_palette.h:LV_API VisPalette *visual_palette_clone (VisPalette *self);What do you think?
| #define __PRETTY_FUNCTION__ __FUNCTION__ | ||
| #endif | ||
|
|
||
| #if __cplusplus >= 201703L |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kaixiong could it be that this needs addition of defined(__cplusplus) && ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hartwork, I was wondering about this. GCC and Clang compile it but I'm not sure if this is standard. Any idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kaixiong let me first spell out the questions that I think we need to answer and then try to give a potential answer. For the questions:
- Is this code used with a C compiler or only C++? Does need it to work from plain C?
- Can we safely compare an undefined macro against an integer value?
- Does the
__cplusplusvalue reflect reality (more below)?
Potential answers:
- Seems like yes, we need to support both C and C++ here.
- It is defined according to https://stackoverflow.com/questions/5085392/what-is-the-value-of-an-undefined-constant-used-in-if so it seems to become a question of style and/or clarity.
- MSVC is said to not have reality in
__cplusplus. The post lays out some options. Another one. I think we do want to support MSVC?
PS: https://sourceforge.net/p/predef/wiki/Standards/ does not seem to have any additional insights.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hartwork Looks like Question 1 and 2 are effectively the same question as the C/C++ preprocessor both default to 0 for undefined constants (apart from the handling of true and false).
Question 3 is addressed in #269 but does not ultimately matter for this PR because the code falls back to compiler extensions (_Check_return_) immediately below this.
…re not to be discarded.
This PR adds the define
LV_NODISCARDfor marking C API functions whose return results should not be discarded. I have added them to API functions that return allocated objects.The define uses compiler-specific function attributes to achieve this when building C code.
For C++,
[[nodiscard]]already exists from C++17 onwards. C++20 has a variant that accepts a string literal for explaining the rationale that could be displayed in compiler messages. I can't see how to achieve the same for C unfortunately.Use
LV_NODISCARDonly for C API functions.[[nodiscard]]for C++ functions.