From 903895ea5f5f4bd21f654f84c17b4d294ac65049 Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Wed, 4 Dec 2024 17:48:38 +1100 Subject: [PATCH] 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 Signed-off-by: Rob Norris Closes #16840 --- scripts/cstyle.pl | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/scripts/cstyle.pl b/scripts/cstyle.pl index d47fd3362..680ce9766 100755 --- a/scripts/cstyle.pl +++ b/scripts/cstyle.pl @@ -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; }