Skip to content

Commit 9ed2be7

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 9ed2be7

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

scripts/checkpatch.pl

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

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

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

0 commit comments

Comments
 (0)