Skip to content

Commit 402e7c1

Browse files
committed
embed.fnc: Add ability to customize ARGS_ASSERT macros
This enhances embed.fnc entries to contain arbitrary assert() calls, which will be added to the corresponding generated ARGS_ASSERT macros. The next commit will use it.
1 parent 2be71f5 commit 402e7c1

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

embed.fnc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@
158158
:
159159
: For a numeric argument, you may specify that it can't be 0 by using 'NZ'
160160
:
161+
: You can specify your own checking beyond these by adding any number of
162+
: assert() calls to en entry after the final argument. Whatever you specify
163+
: will be added to the ARGS_ASSERT macro for the entry. When doing so, weigh
164+
: that doing it here will make it less visible to a maintainer than keeping
165+
: it in the function it applies to
166+
:
161167
: regen/embed.pl may automatically add further checking for any argument as
162168
: it deems desirable. You can override this by specifying 'NOCHECK'
163169
:

regen/HeaderParser.pm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,11 @@ sub tidy_embed_fnc_entry {
746746
$line =~ s/\s+\z//; # No trailing white space
747747
($line)= expand($line); # No tabs
748748

749+
# Remove any assertions, and save them. This must be done before the
750+
# split because the assertions can contain '|'
751+
$line =~ s/ ( assert \s* \( .* ) \z //x;
752+
my $assertions = $1;
753+
749754
# Split into fields
750755
my ($flags, $ret, $name, @args)= split /\s*\|\s*/, $line;
751756

@@ -795,6 +800,28 @@ sub tidy_embed_fnc_entry {
795800
$head .= "|$arg";
796801
$head .= "\\\n" . (" " x 32) if $ix < $#args;
797802
}
803+
804+
my @assertions;
805+
if ($assertions) {
806+
# Put each assertion into a separate array element
807+
@assertions = split / \s* assert \s* \( /x, $assertions;
808+
shift @assertions; # The split leaves an empty first element
809+
810+
# Trim each assertion, including any trailing semicolon
811+
foreach my $this_assertion (@assertions) {
812+
$this_assertion =~ s/ ^ \s+ //x;
813+
$this_assertion =~ s/ \s+ \z //x;
814+
$this_assertion =~ s/ ; \z //x;
815+
816+
# Restore split delimitter
817+
$this_assertion = "assert($this_assertion";
818+
819+
# Each assertion is on a separate line (for now, anyway)
820+
$head .= "\\\n" . (" " x 32);
821+
$head .= $this_assertion;
822+
}
823+
}
824+
798825
$line= $head . "\n";
799826
800827
# Make all lines in this entry the same length; minimum 72
@@ -816,6 +843,7 @@ sub tidy_embed_fnc_entry {
816843
return_type => $ret,
817844
name => $name,
818845
args => \@args,
846+
assertions => \@assertions,
819847
);
820848
821849
$line =~ s/\s+\z/\n/;

regen/embed.pl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ sub generate_proto_h {
121121
$ind .= " " x ($level-1) if $level>1;
122122
my $inner_ind= $ind ? " " : " ";
123123

124-
my ($flags,$retval,$plain_func,$args) = @{$embed}{qw(flags return_type name args)};
124+
my ($flags, $retval, $plain_func, $args, $assertions ) =
125+
@{$embed}{qw(flags return_type name args assertions)};
125126
if ($flags =~ / ( [^ AabCDdEefFGhIiMmNnOoPpRrSsTUuvWXx;] ) /xx) {
126127
die_at_end "flag $1 is not legal (for function $plain_func)";
127128
}
@@ -311,6 +312,9 @@ sub generate_proto_h {
311312
}
312313
$ret .= " comma_pDEPTH" if $has_depth;
313314
$ret .= ")";
315+
316+
push @asserts, $assertions->@* if $assertions;
317+
314318
my @attrs;
315319
if ( $flags =~ /r/ ) {
316320
push @attrs, "__attribute__noreturn__";

0 commit comments

Comments
 (0)