cstyle: understand basic top-level macro invocations

We quite often invoke macros outside of functions, usually to generate
functions or data. cstyle inteprets these as function headers, which at
least have opinions for indenting.

This introduces a separate state for top-level macro invocations, and
excludes it from matching functions. For the moment, most of the
existing rules will continue to apply, but this gives us a way to add or
removes rules targeting macros specifically.

Sponsored-by: https://despairlabs.com/sponsor/
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <robn@despairlabs.com>
Closes #16840
This commit is contained in:
Rob Norris 2024-12-04 17:48:38 +11:00 committed by Brian Behlendorf
parent 7742e29387
commit 0e87150b6c

View File

@ -211,6 +211,7 @@ my $next_in_cpp = 0;
my $in_comment = 0;
my $comment_done = 0;
my $in_warlock_comment = 0;
my $in_macro_call = 0;
my $in_function = 0;
my $in_function_header = 0;
my $function_header_full_indent = 0;
@ -395,12 +396,18 @@ line: while (<$filehandle>) {
}
}
# If this looks like a top-level macro invocation, remember it so we
# don't mistake it for a function declaration below.
if (/^[A-Za-z_][A-Za-z_0-9]*\(/) {
$in_macro_call = 1;
}
#
# If this matches something of form "foo(", it's probably a function
# definition, unless it ends with ") bar;", in which case it's a declaration
# that uses a macro to generate the type.
#
if (/^\w+\(/ && !/\) \w+;/) {
if (!$in_macro_call && /^\w+\(/ && !/\) \w+;/) {
$in_function_header = 1;
if (/\($/) {
$function_header_full_indent = 1;
@ -700,6 +707,14 @@ line: while (<$filehandle>) {
"else and right brace should be on same line");
}
}
# Macro invocations end with a closing paren, and possibly a semicolon.
# We do this check down here to make sure all the regular checks are
# applied to calls that appear entirely on a single line.
if ($in_macro_call && /\);?$/) {
$in_macro_call = 0;
}
$prev = $line;
}