Skip to content

Conversation

@kaixiong
Copy link
Member

@kaixiong kaixiong commented Feb 8, 2023

This PR adds the define LV_NODISCARD for 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_NODISCARD only for C API functions. [[nodiscard]] for C++ functions.

@kaixiong kaixiong added this to the 0.5.0_alpha1 milestone Feb 8, 2023
@kaixiong kaixiong requested a review from hartwork February 8, 2023 14:06
Copy link
Member

@hartwork hartwork left a 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
Copy link
Member

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) && ?

Copy link
Member Author

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?

Copy link
Member

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:

  1. Is this code used with a C compiler or only C++? Does need it to work from plain C?
  2. Can we safely compare an undefined macro against an integer value?
  3. Does the __cplusplus value reflect reality (more below)?

Potential answers:

  1. Seems like yes, we need to support both C and C++ here.
  2. 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.
  3. 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?

Copy link
Member Author

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.

@kaixiong kaixiong merged commit af4bc3b into master Feb 13, 2023
@kaixiong kaixiong deleted the nodiscard branch February 13, 2023 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants