Skip to contentSkip to conversation

tracing: Fix clang -Wint-in-bool-context warnings in IF_ASSIGN macro

PATCH Superseded Nathan Chancellor 4 participants 5 messages Sep 25, 2019 – Sep 26, 2019 1 message not in the archive Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: Fix clang -Wint-in-bool-context warnings in IF_ASSIGN macro

After r372664 in clang, the IF_ASSIGN macro causes a couple hundred warnings along the lines of:

kernel/trace/trace_output.c:1331:2: warning: converting the enum constant to a boolean [-Wint-in-bool-context] kernel/trace/trace.h:409:3: note: expanded from macro 'trace_assign_type'
IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, ^
kernel/trace/trace.h:371:14: note: expanded from macro 'IF_ASSIGN'

                WARN_ON(id && (entry)->type != id);     \
                           ^

264 warnings generated.

Add the implicit '!= 0' to the WARN_ON statement to fix the warnings.

---
 kernel/trace/trace.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/trace/trace.h +5 -5
index 26b0a08f3c7d..f801d154ff6a 100644
@@ -365,11 +365,11 @@ static inline struct trace_array *top_trace_array(void)
 	__builtin_types_compatible_p(typeof(var), type *)
 
 #undef IF_ASSIGN
-#define IF_ASSIGN(var, entry, etype, id)		\
-	if (FTRACE_CMP_TYPE(var, etype)) {		\
-		var = (typeof(var))(entry);		\
-		WARN_ON(id && (entry)->type != id);	\
-		break;					\
+#define IF_ASSIGN(var, entry, etype, id)			\
+	if (FTRACE_CMP_TYPE(var, etype)) {			\
+		var = (typeof(var))(entry);			\
+		WARN_ON(id != 0 && (entry)->type != id);	\
+		break;						\
 	}
 
 /* Will cause compile errors if type is not found. */
Signature · 1 line
2.23.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Nathan Chancellor

After r372664 in clang, the IF_ASSIGN macro causes a couple hundred warnings along the lines of:

kernel/trace/trace_output.c:1331:2: warning: converting the enum constant to a boolean [-Wint-in-bool-context] kernel/trace/trace.h:409:3: note: expanded from macro 'trace_assign_type'
IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, ^
kernel/trace/trace.h:371:14: note: expanded from macro 'IF_ASSIGN'

                WARN_ON(id && (entry)->type != id);     \
                           ^
Quoted 7 lines

264 warnings generated.

Add the implicit '!= 0' to the WARN_ON statement to fix the warnings.

I can't think of a case that this warning is a bug (maybe David can explain more), but seems like a small fix that can stop a big spew of warnings, and IIUC this is the lone instance we see in the kernel. In that case, I prefer a tiny change to outright disabling the warning in case it does find interesting cases later.

Quoted 28 lines
---
 kernel/trace/trace.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/trace/trace.h +5 -6
index 26b0a08f3c7d..f801d154ff6a 100644
@@ -365,11 +365,11 @@ static inline struct trace_array *top_trace_array(void)
        __builtin_types_compatible_p(typeof(var), type *)
 
 #undef IF_ASSIGN
-#define IF_ASSIGN(var, entry, etype, id)               \
-       if (FTRACE_CMP_TYPE(var, etype)) {              \
-               var = (typeof(var))(entry);             \
-               WARN_ON(id && (entry)->type != id);     \
-               break;                                  \
+#define IF_ASSIGN(var, entry, etype, id)                       \
+       if (FTRACE_CMP_TYPE(var, etype)) {                      \
+               var = (typeof(var))(entry);                     \
+               WARN_ON(id != 0 && (entry)->type != id);        \
+               break;                                          \
        }
 
 /* Will cause compile errors if type is not found. */
--

2.23.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw

GCC C frontend does not warn, GCC C++ FE does. https://godbolt.org/z/_sczyM

So I (we?) think there is something weird in gcc frontends.

Quoted 1 line
Quoted 1 line

I can't think of a case that this warning is a bug (maybe David can

explain more),

In this case or generally? General bug example:

if (state == A || B)

(should be if (state == A || state == B))

Since this is just one occurrence and I recommend to just land this small fix.

st 25. 9. 2019 o 19:41 Nick Desaulniers <[email protected]> napísal(a):

Quoted 64 lines
Quoted 13 lines · Nathan Chancellor

After r372664 in clang, the IF_ASSIGN macro causes a couple hundred warnings along the lines of:

kernel/trace/trace_output.c:1331:2: warning: converting the enum constant to a boolean [-Wint-in-bool-context] kernel/trace/trace.h:409:3: note: expanded from macro 'trace_assign_type'
IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, ^
kernel/trace/trace.h:371:14: note: expanded from macro 'IF_ASSIGN'

                WARN_ON(id && (entry)->type != id);     \
                           ^
Quoted 7 lines

264 warnings generated.

Add the implicit '!= 0' to the WARN_ON statement to fix the warnings.

I can't think of a case that this warning is a bug (maybe David can explain more), but seems like a small fix that can stop a big spew of warnings, and IIUC this is the lone instance we see in the kernel. In that case, I prefer a tiny change to outright disabling the warning in case it does find interesting cases later.

Quoted 26 lines
---
 kernel/trace/trace.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/trace/trace.h +5 -6
index 26b0a08f3c7d..f801d154ff6a 100644
@@ -365,11 +365,11 @@ static inline struct trace_array *top_trace_array(void)
        __builtin_types_compatible_p(typeof(var), type *)
 
 #undef IF_ASSIGN
-#define IF_ASSIGN(var, entry, etype, id)               \
-       if (FTRACE_CMP_TYPE(var, etype)) {              \
-               var = (typeof(var))(entry);             \
-               WARN_ON(id && (entry)->type != id);     \
-               break;                                  \
+#define IF_ASSIGN(var, entry, etype, id)                       \
+       if (FTRACE_CMP_TYPE(var, etype)) {                      \
+               var = (typeof(var))(entry);                     \
+               WARN_ON(id != 0 && (entry)->type != id);        \
+               break;                                          \
        }
 
 /* Will cause compile errors if type is not found. */
--
Quoted 2 lines

2.23.0

--
Thanks,
~Nick Desaulniers

A message that is not in the archive (sent off-list or not yet received): CAOrgDVMqOqKtY-9FNV5iHWmz6GFqsH=ugwYp77Zwfr3Niw0ebg@mail.gmail.com

maintainer
#4 lore raw
Quoted 7 lines · On Thu, 26 Sep 2019 01:37:29 +0200 Dávid Bolvanský <[email protected]>…

GCC C frontend does not warn, GCC C++ FE does. https://godbolt.org/z/_sczyM

So I (we?) think there is something weird in gcc frontends.

Quoted 1 line
Quoted 1 line

I can't think of a case that this warning is a bug (maybe David can

explain more),

Quoted 9 lines

In this case or generally? General bug example:

if (state == A || B)

(should be if (state == A || state == B))

Since this is just one occurrence and I recommend to just land this small fix.

Can we add the above comment to the commit log. I was stuck on wondering what was wrong with the original code, and was ignoring the patch because I couldn't see what was wrong.

-- Steve

#5 lore raw

PATCH v2 1/1 tracing: Fix clang -Wint-in-bool-context warnings in IF_ASSIGN macro

In reply to Nathan Chancellor

After r372664 in clang, the IF_ASSIGN macro causes a couple hundred warnings along the lines of:

kernel/trace/trace_output.c:1331:2: warning: converting the enum constant to a boolean [-Wint-in-bool-context] kernel/trace/trace.h:409:3: note: expanded from macro 'trace_assign_type'
IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, ^
kernel/trace/trace.h:371:14: note: expanded from macro 'IF_ASSIGN'

                WARN_ON(id && (entry)->type != id);     \
                           ^

264 warnings generated.

This warning can catch issues with constructs like:

    if (state == A || B)

where the developer really meant:

    if (state == A || state == B)

This is currently the only occurrence of the warning in the kernel tree across defconfig, allyesconfig, allmodconfig for arm32, arm64, and x86_64. Add the implicit '!= 0' to the WARN_ON statement to fix the warnings and find potential issues in the future.

---

v1 -> v2:

* Update commit message to give context behind the warning and explain this is currently the only occurrence of this warning in the tree.
* Add Nick's Reviewed-by tag.

 kernel/trace/trace.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/trace/trace.h +5 -5
index 26b0a08f3c7d..f801d154ff6a 100644
@@ -365,11 +365,11 @@ static inline struct trace_array *top_trace_array(void)
 	__builtin_types_compatible_p(typeof(var), type *)
 
 #undef IF_ASSIGN
-#define IF_ASSIGN(var, entry, etype, id)		\
-	if (FTRACE_CMP_TYPE(var, etype)) {		\
-		var = (typeof(var))(entry);		\
-		WARN_ON(id && (entry)->type != id);	\
-		break;					\
+#define IF_ASSIGN(var, entry, etype, id)			\
+	if (FTRACE_CMP_TYPE(var, etype)) {			\
+		var = (typeof(var))(entry);			\
+		WARN_ON(id != 0 && (entry)->type != id);	\
+		break;						\
 	}
 
 /* Will cause compile errors if type is not found. */
Signature · 1 line
2.23.0