checkpatch: Adapt the braces check to Zephyr
scripts/checkpatch.pl was written originally for the Linux kernel, and its code reflects the kernel's coding style. In particular, it has checks for unneeded braces around single-statement if/else/for/while conditions. In Zephyr however, braces are always required, and so the checks needed modifying to verify the opposite condition. In order to enable the now-compatible checks, we also remove the --ignore BRACES statement in .checkpatch.conf. Limitations: the current code works well if there are not conditional statements (e.g. #if, #ifdef or #endif) next to the if/else/for/while conditions. This is rarely the case, but triggers with the Bluetooth controller in code like this: ``` #if defined(CONFIG_BT_PERIPHERAL) if (!lll->is_hdcd) #endif /* CONFIG_BT_PERIPHERAL */ { ``` ``` } else #endif /* CONFIG_BT_CTLR_PRIVACY */ { ``` ``` #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX) if (lll->cte_started) { radio_switch_complete(phy_s, 0, phy_s, 0); } else #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */ { ``` ``` #ifdef DUAL_BANK while ((FLASH_STM32_REGS(dev)->SR1 & FLASH_SR_QW) || (FLASH_STM32_REGS(dev)->SR2 & FLASH_SR_QW)) #else while (FLASH_STM32_REGS(dev)->SR1 & FLASH_SR_QW) #endif { ``` Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
This commit is contained in:
parent
0f948fdb1c
commit
3c420a4828
3 changed files with 32 additions and 60 deletions
|
@ -5,7 +5,6 @@
|
||||||
--min-conf-desc-length=1
|
--min-conf-desc-length=1
|
||||||
--typedefsfile=scripts/checkpatch/typedefsfile
|
--typedefsfile=scripts/checkpatch/typedefsfile
|
||||||
|
|
||||||
--ignore BRACES
|
|
||||||
--ignore PRINTK_WITHOUT_KERN_LEVEL
|
--ignore PRINTK_WITHOUT_KERN_LEVEL
|
||||||
--ignore SPLIT_STRING
|
--ignore SPLIT_STRING
|
||||||
--ignore VOLATILE
|
--ignore VOLATILE
|
||||||
|
|
|
@ -545,8 +545,7 @@ exceptions:
|
||||||
* The line length is 100 columns or fewer. In the documentation, longer lines
|
* The line length is 100 columns or fewer. In the documentation, longer lines
|
||||||
for URL references are an allowed exception.
|
for URL references are an allowed exception.
|
||||||
* Add braces to every ``if``, ``else``, ``do``, ``while``, ``for`` and
|
* Add braces to every ``if``, ``else``, ``do``, ``while``, ``for`` and
|
||||||
``switch`` body, even for single-line code blocks. Use the ``--ignore BRACES``
|
``switch`` body, even for single-line code blocks.
|
||||||
flag to make *checkpatch* stop complaining.
|
|
||||||
* Use spaces instead of tabs to align comments after declarations, as needed.
|
* Use spaces instead of tabs to align comments after declarations, as needed.
|
||||||
* Use C89-style single line comments, ``/* */``. The C99-style single line
|
* Use C89-style single line comments, ``/* */``. The C99-style single line
|
||||||
comment, ``//``, is not allowed.
|
comment, ``//``, is not allowed.
|
||||||
|
|
|
@ -5497,8 +5497,9 @@ sub process {
|
||||||
my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
|
my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
|
||||||
my $offset = statement_rawlines($whitespace) - 1;
|
my $offset = statement_rawlines($whitespace) - 1;
|
||||||
|
|
||||||
$allowed[$allow] = 0;
|
# always allow braces (i.e. require them)
|
||||||
#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
|
$allowed[$allow] = 1;
|
||||||
|
#print "COND<$cond> block<$block> whitespace<$whitespace> offset<$offset>\n";
|
||||||
|
|
||||||
# We have looked at and allowed this specific line.
|
# We have looked at and allowed this specific line.
|
||||||
$suppress_ifbraces{$ln + $offset} = 1;
|
$suppress_ifbraces{$ln + $offset} = 1;
|
||||||
|
@ -5507,46 +5508,37 @@ sub process {
|
||||||
$ln += statement_rawlines($block) - 1;
|
$ln += statement_rawlines($block) - 1;
|
||||||
|
|
||||||
substr($block, 0, length($cond), '');
|
substr($block, 0, length($cond), '');
|
||||||
|
#print "COND<$cond> block<$block>\n";
|
||||||
|
|
||||||
|
# Remove any 0x1C characters. The script replaces
|
||||||
|
# comments /* */ with those.
|
||||||
|
$block =~ tr/\x1C//d;
|
||||||
|
|
||||||
$seen++ if ($block =~ /^\s*{/);
|
$seen++ if ($block =~ /^\s*{/);
|
||||||
|
|
||||||
#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
|
|
||||||
if (statement_lines($cond) > 1) {
|
|
||||||
#print "APW: ALLOWED: cond<$cond>\n";
|
|
||||||
$allowed[$allow] = 1;
|
|
||||||
}
|
|
||||||
if ($block =~/\b(?:if|for|while)\b/) {
|
|
||||||
#print "APW: ALLOWED: block<$block>\n";
|
|
||||||
$allowed[$allow] = 1;
|
|
||||||
}
|
|
||||||
if (statement_block_size($block) > 1) {
|
|
||||||
#print "APW: ALLOWED: lines block<$block>\n";
|
|
||||||
$allowed[$allow] = 1;
|
|
||||||
}
|
|
||||||
$allow++;
|
$allow++;
|
||||||
}
|
}
|
||||||
if ($seen) {
|
my $sum_allowed = 0;
|
||||||
my $sum_allowed = 0;
|
foreach (@allowed) {
|
||||||
foreach (@allowed) {
|
$sum_allowed += $_;
|
||||||
$sum_allowed += $_;
|
}
|
||||||
}
|
#print "sum_allowed<$sum_allowed> seen<$seen> allow<$allow>\n";
|
||||||
if ($sum_allowed == 0) {
|
if ($sum_allowed == 0) {
|
||||||
WARN("BRACES",
|
WARN("BRACES",
|
||||||
"braces {} are not necessary for any arm of this statement\n" . $herectx);
|
"braces {} are not necessary for any arm of this statement\n" . $herectx);
|
||||||
} elsif ($sum_allowed != $allow &&
|
} elsif ($seen != $allow) {
|
||||||
$seen != $allow) {
|
WARN("BRACES",
|
||||||
CHK("BRACES",
|
"braces {} must be used on all arms of this statement\n" . $herectx);
|
||||||
"braces {} should be used on all arms of this statement\n" . $herectx);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!defined $suppress_ifbraces{$linenr - 1} &&
|
if (!defined $suppress_ifbraces{$linenr - 1} &&
|
||||||
$line =~ /\b(if|while|for|else)\b/) {
|
$line =~ /\b(if|while|for|else)\b/) {
|
||||||
my $allowed = 0;
|
my $allowed = 0;
|
||||||
|
# Check the pre-context for:
|
||||||
# Check the pre-context.
|
# '#': #if
|
||||||
if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
|
# '}': } while()
|
||||||
|
if (substr($line, 0, $-[0]) =~ /([\#\}]\s*)$/) {
|
||||||
#print "APW: ALLOWED: pre<$1>\n";
|
#print "APW: ALLOWED: pre<$1>\n";
|
||||||
$allowed = 1;
|
$allowed = 1;
|
||||||
}
|
}
|
||||||
|
@ -5556,39 +5548,21 @@ sub process {
|
||||||
|
|
||||||
# Check the condition.
|
# Check the condition.
|
||||||
my ($cond, $block) = @{$chunks[0]};
|
my ($cond, $block) = @{$chunks[0]};
|
||||||
#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
|
#print "level $level CHECKING<$linenr> cond<$cond> block<$block>\n";
|
||||||
if (defined $cond) {
|
if (defined $cond) {
|
||||||
substr($block, 0, length($cond), '');
|
substr($block, 0, length($cond), '');
|
||||||
}
|
}
|
||||||
if (statement_lines($cond) > 1) {
|
# Remove any 0x1C characters. The script replaces
|
||||||
#print "APW: ALLOWED: cond<$cond>\n";
|
# comments /* */ with those.
|
||||||
$allowed = 1;
|
$block =~ tr/\x1C//d;
|
||||||
}
|
#print sprintf '%v02X', $block;
|
||||||
if ($block =~/\b(?:if|for|while)\b/) {
|
#print "\n";
|
||||||
#print "APW: ALLOWED: block<$block>\n";
|
if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
|
||||||
$allowed = 1;
|
|
||||||
}
|
|
||||||
if (statement_block_size($block) > 1) {
|
|
||||||
#print "APW: ALLOWED: lines block<$block>\n";
|
|
||||||
$allowed = 1;
|
|
||||||
}
|
|
||||||
# Check the post-context.
|
|
||||||
if (defined $chunks[1]) {
|
|
||||||
my ($cond, $block) = @{$chunks[1]};
|
|
||||||
if (defined $cond) {
|
|
||||||
substr($block, 0, length($cond), '');
|
|
||||||
}
|
|
||||||
if ($block =~ /^\s*\{/) {
|
|
||||||
#print "APW: ALLOWED: chunk-1 block<$block>\n";
|
|
||||||
$allowed = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
|
|
||||||
my $cnt = statement_rawlines($block);
|
my $cnt = statement_rawlines($block);
|
||||||
my $herectx = get_stat_here($linenr, $cnt, $here);
|
my $herectx = get_stat_here($linenr, $cnt, $here);
|
||||||
|
|
||||||
WARN("BRACES",
|
WARN("BRACES",
|
||||||
"braces {} are not necessary for single statement blocks\n" . $herectx);
|
"braces {} are required around if/while/for/else\n" . $herectx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue