CHECK: Macro argument reuse
Hey Outreachy Mentors,
I am working on the folowing check reported by checkpatch.pl:
CHECK: Macro argument reuse 'dir' - possible side-effects? #11: FILE: drivers/staging/greybus/audio_helper.c:11:
+#define gbaudio_dapm_for_each_direction(dir) \
+ for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
+ (dir)++)
I understand that the macro definition has input argument dir and this argument is being used inside the definition. This can cause problems if dir is an expression with side effect.
I am trying to re-write this macro as following:
#define gbaudio_dapm_for_each_direction(dir) \
do { \
snd_soc_dapm_direction dir_ = (dir); \
for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \
(dir_)++) \
} while (0)This causes a new warning saying,
WARNING: suspect code indent for conditional statements
I am uncertain about whether I am going in the right direction, and I need some guidance to continue.
Thank you!
Regards,
Khadija
Quoted 24 lines · Khadija Kamran
Hey Outreachy Mentors,
I am working on the folowing check reported by checkpatch.pl:
CHECK: Macro argument reuse 'dir' - possible side-effects? #11: FILE:
drivers/staging/greybus/audio_helper.c:11:(quoted diff) +3
-0+#define gbaudio_dapm_for_each_direction(dir) \ + for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ + (dir)++)I understand that the macro definition has input argument dir and this argument is being used inside the definition. This can cause problems if dir is an expression with side effect.
I am trying to re-write this macro as following:
#define gbaudio_dapm_for_each_direction(dir) \ do { \ snd_soc_dapm_direction dir_ = (dir); \ for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \ (dir_)++) \ } while (0)
Quoted 4 lines
This causes a new warning saying,
WARNING: suspect code indent for conditional statements
I am uncertain about whether I am going in the right direction, and I need some guidance to continue.
Hi Khadija,
There are a few things worth discussing here, let's start with an easy
one: line length. Normally a for-loop has a first line that follows this pattern:
for (<INIT>; <CONDITIONAL>; <INCREMENT>)
... and while it is sometimes necessary to split a long for-loop line across multiple lines, you should split the line as you did, along the semi-colons between the INIT, CONDITIONAL, and INCREMENT blocks. However, if you can fit everything on one 80 characters-or-less[1] line you shouldn't split the for-loop. If one were to choose a single character macro parameter, e.g. 'X', it should be possible to write this in a single line, here is a quick untested example:
Quoted 1 line
Quoted 1 line
Quoted 1 line
drivers/staging/greybus/au renamed from drivers/staging/greybus/audio_helper.c +0 -0
dio_helper.c
index 223987616e07..16860d451285 100644
drivers/staging/greybus/audio_helper.c +0 -0
@@ -8,9 +8,8 @@
#include <sound/soc-dapm.h>
#include "audio_helper.h"
-#define gbaudio_dapm_for_each_direction(dir) \
- for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
- (dir)++)
+#define gbaudio_dapm_for_each_direction(X) \
+ for ((X) = SND_SOC_DAPM_DIR_IN; (X) <= SND_SOC_DAPM_DIR_OUT; (X)++)
static void gbaudio_dapm_link_dai_widget(struct snd_soc_dapm_widget *dai_w,
struct snd_soc_card *card)
Quoted 1 line
Quoted 1 line
Quoted 1 line
However, line length isn't what likely caused you to start working on this macro; the "Macro reuse" warning from scripts/checkpatch.pl was the problem. Looking at the original macro code, I didn't see anything obviously wrong so I took a look at the checkpatch.pl tool itself to see what it was looking for that caused this warning/check notice. Searching for the "Macro argument reuse" string I saw the following code snippet:
Quoted 1 line
Quoted 1 line
Quoted 1 line
my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
if ($use_cnt > 1) {
CHK("MACRO_ARG_REUSE",
"Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
}
Quoted 1 line
Quoted 1 line
Quoted 1 line
... which basically means that anytime a macro parameter is used multiple times in the macro it will cause checkpatch.pl to emit this notice. As long as we carefully review the macro to ensure it is correct, we can ignore this checkpatch.pl warning. While checkpatch.pl can be used as a source of code improvement suggestions, it is important to note that in many cases checkpatch.pl can, and should, be ignored after verifying the code is correct and valid; checkpatch.pl is not a perfect tool.
Finally, and perhaps most importantly, the code snippet you shared while trying to fix this checkpatch.pl warning has a serious bug which I think is worth discussing. I'm going to copy-and-paste it here for easier reference:
Quoted 6 lines
#define gbaudio_dapm_for_each_direction(dir) \
do { \
snd_soc_dapm_direction dir_ = (dir); \
for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \
(dir_)++) \
} while (0)While you are correct in that it is normally considered a good practice to wrap preprocessor macros in a do-while loop, especially if adding a local variable to the macro, in this particular case it breaks the macro. If we look at the original macro we see that it is a single line, the first line of a for-loop:
Quoted 3 lines
#define gbaudio_dapm_for_each_direction(dir) \
for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
(dir)++)... and if we look at how it is used in gbaudio_dapm_free_widget() we can see how the macro is used as a shortcut, or template, for writing a for-loop:
Quoted 1 line
Quoted 1 line
Quoted 1 line
static void gbaudio_dapm_free_widget(struct snd_soc_dapm_widget *w) {
/* ... snip ... */
gbaudio_dapm_for_each_direction(dir) {
snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
gbaudio_dapm_free_path(p);
}/* ... snip ... */
}
Quoted 1 line
Quoted 1 line
Quoted 1 line
If we expand this macro we can see how it would behave as a normal for-loop:
Quoted 1 line
Quoted 1 line
Quoted 1 line
for (dir = SND_SOC_DAPM_DIR_IN; dir <= SND_SOC_DAPM_DIR_OUT; dir++) {
snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p) gbaudio_dapm_free_path(p);
}
Quoted 1 line
Quoted 1 line
Quoted 1 line
However, if we wrap the macro in a do-while loop this shortcut/template falls apart as the for-loop no longer works. As an exercise, you can do the substitution manually in the code and see what it looks like; this is exactly what the preprocessor does, so when in doubt you can always do this to double check your macro code. While looking at the expanded macro, I think you'll also find that the for-loop's initializer block is not handled properly; it's not as serious a bug in that it likely won't be flagged by checkpatch.pl or the compiler, but it might result in bad behaviors when running and will likely be difficult to debug!
I hope this was helpful, but if you have any questions about what I've written above don't hesitate to ask. Good luck!
-Paul
[1] Not too long ago the 80 character line limit was relaxed to 100 characters, but there are still some subsystem maintainers that still enforce an 80 character limit (full disclosure: I'm one of them). When in doubt, stick to 80 characters unless you have previous experience with the subsystem.
Signature · 1 line
paul-moore.com
Quoted 83 lines · Paul Moore
Quoted 24 lines · Khadija Kamran
Hey Outreachy Mentors,
I am working on the folowing check reported by checkpatch.pl:
CHECK: Macro argument reuse 'dir' - possible side-effects? #11: FILE:
drivers/staging/greybus/audio_helper.c:11:(quoted diff) +3
-0+#define gbaudio_dapm_for_each_direction(dir) \ + for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ + (dir)++)I understand that the macro definition has input argument dir and this argument is being used inside the definition. This can cause problems if dir is an expression with side effect.
I am trying to re-write this macro as following:
#define gbaudio_dapm_for_each_direction(dir) \ do { \ snd_soc_dapm_direction dir_ = (dir); \ for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \ (dir_)++) \ } while (0)Quoted 4 lines
This causes a new warning saying,
WARNING: suspect code indent for conditional statements
I am uncertain about whether I am going in the right direction, and I need some guidance to continue.Hi Khadija,
There are a few things worth discussing here, let's start with an easy
one: line length. Normally a for-loop has a first line that follows this pattern:for (<INIT>; <CONDITIONAL>; <INCREMENT>)
... and while it is sometimes necessary to split a long for-loop line across multiple lines, you should split the line as you did, along the semi-colons between the INIT, CONDITIONAL, and INCREMENT blocks. However, if you can fit everything on one 80 characters-or-less[1] line you shouldn't split the for-loop. If one were to choose a single character macro parameter, e.g. 'X', it should be possible to write this in a single line, here is a quick untested example:
Quoted 1 line
Quoted 1 line
Quoted 1 line
drivers/staging/greybus/au renamed from drivers/staging/greybus/audio_helper.c +0
-0
dio_helper.c
index223987616e07..16860d451285100644drivers/staging/greybus/audio_helper.c +2
-3@@ -8,9 +8,8 @@#include <sound/soc-dapm.h> #include "audio_helper.h" -#define gbaudio_dapm_for_each_direction (dir) \ - for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ - (dir)++) +#define gbaudio_dapm_for_each_direction (X ) \ + for ((X) = SND_SOC_DAPM_DIR_IN; (X) <= SND_SOC_DAPM_DIR_OUT; (X)++)static void gbaudio_dapm_link_dai_widget (struct snd_soc_dapm_widget *dai_w,struct snd_soc_card *card)Quoted 1 line
Quoted 1 line
Quoted 1 line
However, line length isn't what likely caused you to start working on this macro; the "Macro reuse" warning from
scripts/checkpatch.plwas the problem. Looking at the original macro code, I didn't see anything obviously wrong so I took a look at the checkpatch.pl tool itself to see what it was looking for that caused this warning/check notice. Searching for the "Macro argument reuse" string I saw the following code snippet:Quoted 1 line
Quoted 1 line
Quoted 1 line
my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
if ($use_cnt > 1) {
CHK("MACRO_ARG_REUSE","Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");}
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 8 lines
... which basically means that anytime a macro parameter is used multiple times in the macro it will cause checkpatch.pl to emit this notice. As long as we carefully review the macro to ensure it is correct, we can ignore this checkpatch.pl warning. While checkpatch.pl can be used as a source of code improvement suggestions, it is important to note that in many cases checkpatch.pl can, and should, be ignored after verifying the code is correct and valid; checkpatch.pl is not a perfect tool.
The explanation seems not completely clear. The point is that the parameter of the macro is used as the left side of an assignment. Often the left side of an assignment is just a variable that would not have any side effects, like x in x = 3. But there is nothing that forces that. For example the left side of an assignment could be a[foo()] where each call to foo() would have some side effects (print something, update some variables, etc). To check for such concerns, you would have to look at the places where the macro is used, not the definition.
julia
Quoted 78 lines
Finally, and perhaps most importantly, the code snippet you shared while trying to fix this checkpatch.pl warning has a serious bug which I think is worth discussing. I'm going to copy-and-paste it here for easier reference:
Quoted 6 lines
#define gbaudio_dapm_for_each_direction(dir) \ do { \ snd_soc_dapm_direction dir_ = (dir); \ for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \ (dir_)++) \ } while (0)While you are correct in that it is normally considered a good practice to wrap preprocessor macros in a do-while loop, especially if adding a local variable to the macro, in this particular case it breaks the macro. If we look at the original macro we see that it is a single line, the first line of a for-loop:
Quoted 3 lines
#define gbaudio_dapm_for_each_direction(dir) \ for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ (dir)++)... and if we look at how it is used in
gbaudio_dapm_free_widget()we can see how the macro is used as a shortcut, or template, for writing a for-loop:Quoted 1 line
Quoted 1 line
Quoted 1 line
static void
gbaudio_dapm_free_widget(struct snd_soc_dapm_widget *w){/* ... snip ... */
gbaudio_dapm_for_each_direction(dir) { snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p) gbaudio_dapm_free_path(p); }/* ... snip ... */
}
Quoted 1 line
Quoted 1 line
Quoted 1 line
If we expand this macro we can see how it would behave as a normal for-loop:
Quoted 1 line
Quoted 1 line
Quoted 1 line
for (dir =
SND_SOC_DAPM_DIR_IN; dir <=SND_SOC_DAPM_DIR_OUT; dir++) {
snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)gbaudio_dapm_free_path(p);
}Quoted 1 line
Quoted 1 line
Quoted 1 line
However, if we wrap the macro in a do-while loop this shortcut/template falls apart as the for-loop no longer works. As an exercise, you can do the substitution manually in the code and see what it looks like; this is exactly what the preprocessor does, so when in doubt you can always do this to double check your macro code. While looking at the expanded macro, I think you'll also find that the for-loop's initializer block is not handled properly; it's not as serious a bug in that it likely won't be flagged by checkpatch.pl or the compiler, but it might result in bad behaviors when running and will likely be difficult to debug!
I hope this was helpful, but if you have any questions about what I've written above don't hesitate to ask. Good luck!
-Paul
[1] Not too long ago the 80 character line limit was relaxed to 100 characters, but there are still some subsystem maintainers that still enforce an 80 character limit (full disclosure: I'm one of them). When in doubt, stick to 80 characters unless you have previous experience with the subsystem.
--
paul-moore.com
Quoted 65 lines · Paul Moore
Quoted 24 lines · Khadija Kamran
Hey Outreachy Mentors,
I am working on the folowing check reported by checkpatch.pl:
CHECK: Macro argument reuse 'dir' - possible side-effects? #11: FILE:
drivers/staging/greybus/audio_helper.c:11:(quoted diff) +3
-0+#define gbaudio_dapm_for_each_direction(dir) \ + for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ + (dir)++)I understand that the macro definition has input argument dir and this argument is being used inside the definition. This can cause problems if dir is an expression with side effect.
I am trying to re-write this macro as following:
#define gbaudio_dapm_for_each_direction(dir) \ do { \ snd_soc_dapm_direction dir_ = (dir); \ for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \ (dir_)++) \ } while (0)Quoted 4 lines
This causes a new warning saying,
WARNING: suspect code indent for conditional statements
I am uncertain about whether I am going in the right direction, and I need some guidance to continue.Hi Khadija,
There are a few things worth discussing here, let's start with an easy
one: line length. Normally a for-loop has a first line that follows this pattern:for (<INIT>; <CONDITIONAL>; <INCREMENT>)
... and while it is sometimes necessary to split a long for-loop line across multiple lines, you should split the line as you did, along the semi-colons between the INIT, CONDITIONAL, and INCREMENT blocks. However, if you can fit everything on one 80 characters-or-less[1] line you shouldn't split the for-loop. If one were to choose a single character macro parameter, e.g. 'X', it should be possible to write this in a single line, here is a quick untested example:
Quoted 1 line
Quoted 1 line
Quoted 1 line
drivers/staging/greybus/au renamed from drivers/staging/greybus/audio_helper.c +0
-0
dio_helper.c
index223987616e07..16860d451285100644drivers/staging/greybus/audio_helper.c +2
-3@@ -8,9 +8,8 @@#include <sound/soc-dapm.h> #include "audio_helper.h" -#define gbaudio_dapm_for_each_direction (dir) \ - for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ - (dir)++) +#define gbaudio_dapm_for_each_direction (X ) \ + for ((X) = SND_SOC_DAPM_DIR_IN; (X) <= SND_SOC_DAPM_DIR_OUT; (X)++)static void gbaudio_dapm_link_dai_widget (struct snd_soc_dapm_widget *dai_w,struct snd_soc_card *card)
Quoted 2 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Okay understood.
Quoted 12 lines
However, line length isn't what likely caused you to start working on this macro; the "Macro reuse" warning from
scripts/checkpatch.plwas the problem. Looking at the original macro code, I didn't see anything obviously wrong so I took a look at the checkpatch.pl tool itself to see what it was looking for that caused this warning/check notice. Searching for the "Macro argument reuse" string I saw the following code snippet:Quoted 1 line
Quoted 1 line
Quoted 1 line
my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
if ($use_cnt > 1) {
CHK("MACRO_ARG_REUSE",
Quoted 8 lines
"Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");}
Quoted 1 line
Quoted 1 line
Quoted 1 line
... which basically means that anytime a macro parameter is used multiple times in the macro it will cause checkpatch.pl to emit this notice. As long as we carefully review the macro to ensure it is correct, we can ignore this checkpatch.pl warning.
Paul,
How can I ensure it is correct? Won't it still cause a problem if we use any argument with side-effect.
Quoted 57 lines
While
checkpatch.pl can be used as a source of code improvement suggestions, it is important to note that in many cases checkpatch.pl can, and should, be ignored after verifying the code is correct and valid; checkpatch.pl is not a perfect tool.Finally, and perhaps most importantly, the code snippet you shared while trying to fix this checkpatch.pl warning has a serious bug which I think is worth discussing. I'm going to copy-and-paste it here for easier reference:
Quoted 6 lines
#define gbaudio_dapm_for_each_direction(dir) \ do { \ snd_soc_dapm_direction dir_ = (dir); \ for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \ (dir_)++) \ } while (0)While you are correct in that it is normally considered a good practice to wrap preprocessor macros in a do-while loop, especially if adding a local variable to the macro, in this particular case it breaks the macro. If we look at the original macro we see that it is a single line, the first line of a for-loop:
Quoted 3 lines
#define gbaudio_dapm_for_each_direction(dir) \ for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ (dir)++)... and if we look at how it is used in
gbaudio_dapm_free_widget()we can see how the macro is used as a shortcut, or template, for writing a for-loop:Quoted 1 line
Quoted 1 line
Quoted 1 line
static void
gbaudio_dapm_free_widget(struct snd_soc_dapm_widget *w){/* ... snip ... */
gbaudio_dapm_for_each_direction(dir) { snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p) gbaudio_dapm_free_path(p); }/* ... snip ... */
}
Quoted 1 line
Quoted 1 line
Quoted 1 line
If we expand this macro we can see how it would behave as a normal for-loop:
Quoted 1 line
Quoted 1 line
Quoted 1 line
for (dir =
SND_SOC_DAPM_DIR_IN; dir <=SND_SOC_DAPM_DIR_OUT; dir++) {
snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)gbaudio_dapm_free_path(p);
}Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 5 lines
However, if we wrap the macro in a do-while loop this shortcut/template falls apart as the for-loop no longer works. As an exercise, you can do the substitution manually in the code and see what it looks like; this is exactly what the preprocessor does, so when in doubt you can always do this to double check your macro code.
I can clearly see my mistake here. Thank you for explaining this.
Quoted 6 lines
While looking at the expanded macro, I think you'll also find that the for-loop's initializer block is not handled properly; it's not as serious a bug in that it likely won't be flagged by checkpatch.pl or the compiler, but it might result in bad behaviors when running and will likely be difficult to debug!
I am sorry, just wanted to confirm, are you talking about this:
Quoted 7 lines
Quoted 2 lines
for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \ (dir_)++) \I hope this was helpful, but if you have any questions about what I've written above don't hesitate to ask. Good luck!
Thank you for taking your time out to write a detailed explanation. This is great help. :)
Regards,
Khadija
Quoted 10 lines
-Paul
[1] Not too long ago the 80 character line limit was relaxed to 100 characters, but there are still some subsystem maintainers that still enforce an 80 character limit (full disclosure: I'm one of them). When in doubt, stick to 80 characters unless you have previous experience with the subsystem.
Signature · 1 line
paul-moore.com
Quoted 105 lines · Julia Lawall
Quoted 83 lines · Paul Moore
Quoted 24 lines · Khadija Kamran
Hey Outreachy Mentors,
I am working on the folowing check reported by checkpatch.pl:
CHECK: Macro argument reuse 'dir' - possible side-effects? #11: FILE:
drivers/staging/greybus/audio_helper.c:11:(quoted diff) +3
-0+#define gbaudio_dapm_for_each_direction(dir) \ + for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ + (dir)++)I understand that the macro definition has input argument dir and this argument is being used inside the definition. This can cause problems if dir is an expression with side effect.
I am trying to re-write this macro as following:
#define gbaudio_dapm_for_each_direction(dir) \ do { \ snd_soc_dapm_direction dir_ = (dir); \ for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \ (dir_)++) \ } while (0)Quoted 4 lines
This causes a new warning saying,
WARNING: suspect code indent for conditional statements
I am uncertain about whether I am going in the right direction, and I need some guidance to continue.Hi Khadija,
There are a few things worth discussing here, let's start with an easy
one: line length. Normally a for-loop has a first line that follows this pattern:for (<INIT>; <CONDITIONAL>; <INCREMENT>)
... and while it is sometimes necessary to split a long for-loop line across multiple lines, you should split the line as you did, along the semi-colons between the INIT, CONDITIONAL, and INCREMENT blocks. However, if you can fit everything on one 80 characters-or-less[1] line you shouldn't split the for-loop. If one were to choose a single character macro parameter, e.g. 'X', it should be possible to write this in a single line, here is a quick untested example:
Quoted 1 line
Quoted 1 line
Quoted 1 line
drivers/staging/greybus/au renamed from drivers/staging/greybus/audio_helper.c +0
-0
dio_helper.c
index223987616e07..16860d451285100644drivers/staging/greybus/audio_helper.c +2
-3@@ -8,9 +8,8 @@#include <sound/soc-dapm.h> #include "audio_helper.h" -#define gbaudio_dapm_for_each_direction (dir) \ - for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ - (dir)++) +#define gbaudio_dapm_for_each_direction (X ) \ + for ((X) = SND_SOC_DAPM_DIR_IN; (X) <= SND_SOC_DAPM_DIR_OUT; (X)++)static void gbaudio_dapm_link_dai_widget (struct snd_soc_dapm_widget *dai_w,struct snd_soc_card *card)Quoted 1 line
Quoted 1 line
Quoted 1 line
However, line length isn't what likely caused you to start working on this macro; the "Macro reuse" warning from
scripts/checkpatch.plwas the problem. Looking at the original macro code, I didn't see anything obviously wrong so I took a look at the checkpatch.pl tool itself to see what it was looking for that caused this warning/check notice. Searching for the "Macro argument reuse" string I saw the following code snippet:Quoted 1 line
Quoted 1 line
Quoted 1 line
my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
if ($use_cnt > 1) {
CHK("MACRO_ARG_REUSE","Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");}
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 8 lines
... which basically means that anytime a macro parameter is used multiple times in the macro it will cause checkpatch.pl to emit this notice. As long as we carefully review the macro to ensure it is correct, we can ignore this checkpatch.pl warning. While checkpatch.pl can be used as a source of code improvement suggestions, it is important to note that in many cases checkpatch.pl can, and should, be ignored after verifying the code is correct and valid; checkpatch.pl is not a perfect tool.
The explanation seems not completely clear. The point is that the parameter of the macro is used as the left side of an assignment. Often the left side of an assignment is just a variable that would not have any side effects, like x in x = 3. But there is nothing that forces that. For example the left side of an assignment could be a[foo()] where each call to
foo()would have some side effects (print something, update some variables, etc). To check for such concerns, you would have to look at the places where the macro is used, not the definition.
Quoted 2 lines
julia
Hey Julia,
So everytime this CHECK is reported, instead of changing the macro definition, it is better to check that wherever the macro is used, it should not be passed an argument with side-effects?
Regards,
Khadija
Quoted 79 lines
Quoted 79 lines
Finally, and perhaps most importantly, the code snippet you shared while trying to fix this checkpatch.pl warning has a serious bug which I think is worth discussing. I'm going to copy-and-paste it here for easier reference:
Quoted 6 lines
#define gbaudio_dapm_for_each_direction(dir) \ do { \ snd_soc_dapm_direction dir_ = (dir); \ for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \ (dir_)++) \ } while (0)While you are correct in that it is normally considered a good practice to wrap preprocessor macros in a do-while loop, especially if adding a local variable to the macro, in this particular case it breaks the macro. If we look at the original macro we see that it is a single line, the first line of a for-loop:
Quoted 3 lines
#define gbaudio_dapm_for_each_direction(dir) \ for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ (dir)++)... and if we look at how it is used in
gbaudio_dapm_free_widget()we can see how the macro is used as a shortcut, or template, for writing a for-loop:Quoted 1 line
Quoted 1 line
Quoted 1 line
static void
gbaudio_dapm_free_widget(struct snd_soc_dapm_widget *w){/* ... snip ... */
gbaudio_dapm_for_each_direction(dir) { snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p) gbaudio_dapm_free_path(p); }/* ... snip ... */
}
Quoted 1 line
Quoted 1 line
Quoted 1 line
If we expand this macro we can see how it would behave as a normal for-loop:
Quoted 1 line
Quoted 1 line
Quoted 1 line
for (dir =
SND_SOC_DAPM_DIR_IN; dir <=SND_SOC_DAPM_DIR_OUT; dir++) {
snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)gbaudio_dapm_free_path(p);
}Quoted 1 line
Quoted 1 line
Quoted 1 line
However, if we wrap the macro in a do-while loop this shortcut/template falls apart as the for-loop no longer works. As an exercise, you can do the substitution manually in the code and see what it looks like; this is exactly what the preprocessor does, so when in doubt you can always do this to double check your macro code. While looking at the expanded macro, I think you'll also find that the for-loop's initializer block is not handled properly; it's not as serious a bug in that it likely won't be flagged by checkpatch.pl or the compiler, but it might result in bad behaviors when running and will likely be difficult to debug!
I hope this was helpful, but if you have any questions about what I've written above don't hesitate to ask. Good luck!
-Paul
[1] Not too long ago the 80 character line limit was relaxed to 100 characters, but there are still some subsystem maintainers that still enforce an 80 character limit (full disclosure: I'm one of them). When in doubt, stick to 80 characters unless you have previous experience with the subsystem.
--
paul-moore.com
Quoted 3 lines · Khadija Kamran
Quoted 2 lines · Paul Moore
On Thu, Mar 23, 2023 at 5:00 PM Khadija Kamran
<[email protected]> wrote:
...
Quoted 16 lines
Quoted 16 lines
However, line length isn't what likely caused you to start working on this macro; the "Macro reuse" warning from
scripts/checkpatch.plwas the problem. Looking at the original macro code, I didn't see anything obviously wrong so I took a look at the checkpatch.pl tool itself to see what it was looking for that caused this warning/check notice. Searching for the "Macro argument reuse" string I saw the following code snippet:Quoted 1 line
Quoted 1 line
Quoted 1 line
my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
if ($use_cnt > 1) {
CHK("MACRO_ARG_REUSE","Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");}
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 4 lines
... which basically means that anytime a macro parameter is used multiple times in the macro it will cause checkpatch.pl to emit this notice. As long as we carefully review the macro to ensure it is correct, we can ignore this checkpatch.pl warning.
Paul,
How can I ensure it is correct? Won't it still cause a problem if we use any argument with side-effect.
Julia provided a more thorough explanation, if you haven't read her email in this thread, please do. My advice above was to review the macro carefully, and careful review includes examining where and how the macro is used, as that can have a significant impact. If you are ever in doubt, do the macro substitution manually and I believe you will find the problems much easier to identify.
Quoted 11 lines
Quoted 6 lines
While looking at the expanded macro, I think you'll also find that the for-loop's initializer block is not handled properly; it's not as serious a bug in that it likely won't be flagged by checkpatch.pl or the compiler, but it might result in bad behaviors when running and will likely be difficult to debug!
I am sorry, just wanted to confirm, are you talking about this:
Quoted 2 lines
Quoted 2 lines
for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \ (dir_)++) \
Yes, that's correct. The original macro would have set 'dir_' to 'SND_SOC_DAPM_DIR_IN' at the start of the for-loop.
Quoted 6 lines
Quoted 3 lines
I hope this was helpful, but if you have any questions about what I've written above don't hesitate to ask. Good luck!
Thank you for taking your time out to write a detailed explanation. This is great help. :)
I'm happy to hear that. I think all of us here are able to do what we do because people took the time to explain things so we could understand; please remember that when you are an experienced developer and someone asks you for help ;)
Good luck!
Signature · 1 line
paul-moore.com
Quoted 33 lines · Paul Moore
Quoted 3 lines · Khadija Kamran
Quoted 2 lines · Paul Moore
On Thu, Mar 23, 2023 at 5:00 PM Khadija Kamran
<[email protected]> wrote:...
Quoted 16 lines
Quoted 16 lines
However, line length isn't what likely caused you to start working on this macro; the "Macro reuse" warning from
scripts/checkpatch.plwas the problem. Looking at the original macro code, I didn't see anything obviously wrong so I took a look at the checkpatch.pl tool itself to see what it was looking for that caused this warning/check notice. Searching for the "Macro argument reuse" string I saw the following code snippet:Quoted 1 line
Quoted 1 line
Quoted 1 line
my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
if ($use_cnt > 1) {
CHK("MACRO_ARG_REUSE","Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");}
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 4 lines
... which basically means that anytime a macro parameter is used multiple times in the macro it will cause checkpatch.pl to emit this notice. As long as we carefully review the macro to ensure it is correct, we can ignore this checkpatch.pl warning.
Paul,
How can I ensure it is correct? Won't it still cause a problem if we use any argument with side-effect.
Quoted 7 lines
Julia provided a more thorough explanation, if you haven't read her email in this thread, please do. My advice above was to review the macro carefully, and careful review includes examining where and how the macro is used, as that can have a significant impact. If you are ever in doubt, do the macro substitution manually and I believe you will find the problems much easier to identify.
Hey Paul,
Okay, I understand.
Thank you.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
While looking at the expanded macro, I think you'll also find that the for-loop's initializer block is not handled properly; it's not as serious a bug in that it likely won't be flagged by checkpatch.pl or the compiler, but it might result in bad behaviors when running and will likely be difficult to debug!
Quoted 8 lines
Quoted 4 lines
I am sorry, just wanted to confirm, are you talking about this:
Quoted 2 lines
Quoted 2 lines
for (; (dir_) <= SND_SOC_DAPM_DIR_OUT; \ (dir_)++) \Yes, that's correct. The original macro would have set '
dir_' to 'SND_SOC_DAPM_DIR_IN' at the start of the for-loop.
Okay understood.
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
I hope this was helpful, but if you have any questions about what I've written above don't hesitate to ask. Good luck!
Quoted 8 lines
Quoted 2 lines
Thank you for taking your time out to write a detailed explanation. This is great help. :)
I'm happy to hear that. I think all of us here are able to do what we do because people took the time to explain things so we could understand; please remember that when you are an experienced developer and someone asks you for help ;)
I will always keep it in mind. :")
Thank you Paul :)
Regards,
Khadija
Quoted 4 lines
Good luck!
Signature · 1 line
paul-moore.com