Skip to content

Commit

Permalink
cstyle: understand basic top-level macro invocations
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Rob Norris <[email protected]>
Closes openzfs#16840
  • Loading branch information
robn authored and behlendorf committed Dec 6, 2024
1 parent 7742e29 commit 0e87150
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion scripts/cstyle.pl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ ($$)
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;
Expand Down Expand Up @@ -395,12 +396,18 @@ ($$)
}
}

# 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;
Expand Down Expand Up @@ -700,6 +707,14 @@ ($$)
"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;
}
Expand Down

0 comments on commit 0e87150

Please sign in to comment.