2

I was going through the WARN_ON_ONCE macro definition. I have doubt regarding the following line, what is the use of !! before condition. If we remove !! then also same will be stored in __ret_warn_once.

int __ret_warn_once = !!(condition);

What will happen when compiler executing the following source line.

static bool __section(.data.unlikely) __warned;
user3693586
  • 1,227
  • 5
  • 18
  • 40
  • Hint: think about what happens if `condition` has the value `7`. I am sure there have been previous questions about the `!!` construct but unfortunately one can't search for it. – Nate Eldredge Dec 01 '19 at 12:42
  • "what is the use of `!!` before condition." - Have you checked this question https://stackoverflow.com/questions/14751973/what-is-in-c? As for `__warned` variable, it is used for implement "ONCE" semantic: the warning message is printed only on the first condition's violation; all futher violations won't emit a message. – Tsyvarev Dec 01 '19 at 12:43
  • @Tsyvarev , yes understood, its to convert non-zero value to 1 and zero to 0. So final value will be either 0 or 1, which can be treated as boolean. Not sure why the result was stored in int variable(`__ret_warn_once`), if we think it for more portability then at [line 136](https://github.com/torvalds/linux/blob/v4.14/include/asm-generic/bug.h#L136) the logical `&&` was used with a bool variable(`__warned`). So we can declare `__ret_warn_once` also as bool. – user3693586 Dec 02 '19 at 18:55
  • So the real question is "why variable `__ret_warn_once` is declared as `int` instead of `bool`", am I right? If so, this is very unobvious interpretation of your question post. And citing the line with **declaration** of `__warned` variable is very unobvious way for ask about of **usage** that variable in the following expression. Please, edit you question post to the form, when it would contain an **actual** question. BTW, your current question post doesn't contain a question statement at all. – Tsyvarev Dec 02 '19 at 21:28
  • @Tsyvarev , My initial doubhts are about `!!` and `static bool __section(.data.unlikely) __warned;`. After referring the links shared in the above comments, understood that `!!` is to convert the values to either 1 or 0. I am trying to understand the WARN_ON_ONCE macro definition, so when I check the next lilne, which is if condition, I got the question why can't `__ret_warn_once` can be declared as bool. – user3693586 Dec 03 '19 at 03:53
  • Please, update([edit]) the question post with your **actual** question. Note, that question post should be understandable **without reading the comments**. – Tsyvarev Dec 03 '19 at 09:40
  • @Tsyvarev, edited the question, initial doubt was on `!!` and `__warned`, but while understanding the `!!`, got doubt about usage of `__ret_warn_once` as int. updated the three points in the question. Any reason behind using `__ret_warn_once` as int? – user3693586 Dec 03 '19 at 15:36

0 Answers0