Skip to content

Commit 7b34e4b

Browse files
committed
checkpatch: add 2 new rules to enforce capitilization in macro constants
- Detects #define statements with lowercase macro names - Detects enum labels starting with lowercase letters Fixes #95679 Signed-off-by: Anas Nashif <[email protected]>
1 parent 911b3da commit 7b34e4b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

scripts/checkpatch.pl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6551,6 +6551,40 @@ sub process {
65516551
}
65526552
}
65536553

6554+
# check for macro names defining constants - should be capitalized
6555+
if ($line =~ /^\+\s*#\s*define\s+([a-z][a-zA-Z0-9_]*)\s+/) {
6556+
my $macro_name = $1;
6557+
# Skip function-like macros (those with parentheses)
6558+
if ($line !~ /^\+\s*#\s*define\s+$macro_name\s*\(/) {
6559+
# Skip common exceptions
6560+
if ($macro_name !~ /^(?:asm|__asm__|volatile|__volatile__|inline|__inline__|restrict|__restrict__|typeof|__typeof__)$/) {
6561+
WARN("MACRO_CASE",
6562+
"Macro name '$macro_name' should be capitalized (names of macros defining constants should be capitalized)\n" . $herecurr);
6563+
}
6564+
}
6565+
}
6566+
6567+
# check for enum labels - should be capitalized
6568+
if ($line =~ /^\+\s*([a-z][a-zA-Z0-9_]*)\s*[,=}]/) {
6569+
my $enum_name = $1;
6570+
my $in_enum = 0;
6571+
# Look back to see if we're inside an enum
6572+
for (my $ln = $linenr - 1; $ln >= $linenr - 20 && $ln >= 0; $ln--) {
6573+
my $prevrawline = $rawlines[$ln];
6574+
if ($prevrawline =~ /enum\s+\w*\s*\{/) {
6575+
$in_enum = 1;
6576+
last;
6577+
}
6578+
if ($prevrawline =~ /\}[^;]*;/) {
6579+
last;
6580+
}
6581+
}
6582+
if ($in_enum) {
6583+
WARN("ENUM_CASE",
6584+
"Enum label '$enum_name' should be capitalized (names of labels in enums should be capitalized)\n" . $herecurr);
6585+
}
6586+
}
6587+
65546588
# check for feature test macros that request C library API extensions, violating rules A.4 and A.5
65556589

65566590
if ($line =~ /#\s*define\s+$api_defines/) {

0 commit comments

Comments
 (0)