From 810536251de30c0577e12d142ac7751ed404f07e Mon Sep 17 00:00:00 2001 From: Vignesh Pandian Date: Mon, 23 Jun 2025 14:54:30 +0530 Subject: [PATCH] scripts: checkpatch: False alarm warning for macros. Remove false alarm warning of `braces {} are required around if/while/for/else` for macro definitions with if/while/for/else. Signed-off-by: Vignesh Pandian --- scripts/checkpatch.pl | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 25b09073bdb..8f964bc654b 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -5560,7 +5560,42 @@ sub process { $block =~ tr/\x1C//d; #print sprintf '%v02X', $block; #print "\n"; - if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) { + + # Detect if the line is part of a macro + my $is_macro = 0; + + # Check if the current line is a single-line macro + if ($lines[$linenr] =~ /^\+\s*#\s*define\b/) { + $is_macro = 1; + } else { + # Dynamically check upward for multi-line macro + my $i = $linenr - 1; + while ($i >= 0) { + my $line = $lines[$i]; + last unless defined $line; + + # Stop at non-added/context lines + last if $line !~ /^[ +]/; + + # If this is a macro definition line, we're inside a macro + if ($line =~ /^\+\s*#\s*define\b/) { + $is_macro = 1; + last; + } + + # Check if previous line ends with backslash (i.e., continuation) + if ($i > 0) { + my $prev_line = $lines[$i - 1]; + last if !defined($prev_line) || $prev_line !~ /\\\s*$/; + } else { + last; + } + + $i--; + } + } + + if ($level == 0 && $block !~ /^\s*\{/ && !$allowed && !$is_macro) { my $cnt = statement_rawlines($block); my $herectx = get_stat_here($linenr, $cnt, $here);