1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-07 17:18:11 +03:00
samba-mirror/pidl/lib/Parse/Pidl/Base.pm
Douglas Bagnall a78f69cb7d pidl: optionally annotate output for debug purposes
It can sometimes be hard to tell which bit of pidl generated which bit
of C. This commit wants to help.

If the PIDL_DEVELOPER environment variable is set (via waf
--pidl-developer or some other means), pidl will annotate *most* C
indicating which lines were generated by which bits of pidl. It looks
something like this:

_PUBLIC_ enum ndr_err_code ndr_push_auth_session_info(struct ndr_push *ndr, int ndr_flags, const struct auth_session_info *r)
{  //:PIDL: Parse::Pidl::Samba4::NDR::Parser::ParseTypePushFunction  lib/Parse/Pidl/Samba4/NDR/Parser.pm:3079
	NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);  //:PIDL: Parse::Pidl::Samba4::NDR::Parser::ParseStructPush  lib/Parse/Pidl/Samba4/NDR/Parser.pm:604
	if (ndr_flags & NDR_SCALARS) {
		NDR_CHECK(ndr_push_align(ndr, 5));  //:PIDL: Parse::Pidl::Samba4::NDR::Parser::ParseStructPushPrimitives  lib/Parse/Pidl/Samba4/NDR/Parser.pm:1448
		NDR_CHECK(ndr_push_unique_ptr(ndr, r->security_token));  //:PIDL: Parse::Pidl::Samba4::NDR::Parser::ParsePtrPush  lib/Parse/Pidl/Samba4/NDR/Parser.pm:604
		NDR_CHECK(ndr_push_unique_ptr(ndr, r->unix_token));
		NDR_CHECK(ndr_push_unique_ptr(ndr, r->info));
		NDR_CHECK(ndr_push_unique_ptr(ndr, r->unix_info));
		NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, 0));
		/* [ignore] 'torture' */  //:PIDL: Parse::Pidl::Samba4::NDR::Parser::ParseElementPushLevel  lib/Parse/Pidl/Samba4/NDR/Parser.pm:729
		NDR_CHECK(ndr_push_DATA_BLOB(ndr, NDR_SCALARS, r->session_key));  //:PIDL: Parse::Pidl::Samba4::NDR::Parser::ParseDataPush  lib/Parse/Pidl/Samba4/NDR/Parser.pm:604
		NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, 0));  //:PIDL: Parse::Pidl::Samba4::NDR::Parser::ParsePtrPush  lib/Parse/Pidl/Samba4/NDR/Parser.pm:604
		/* [ignore] 'credentials' */  //:PIDL: Parse::Pidl::Samba4::NDR::Parser::ParseElementPushLevel  lib/Parse/Pidl/Samba4/NDR/Parser.pm:729

The comments starting with '//:PIDL:' have the function name, the filename,
and line number. The comment follows the ordinary output, and uses the '//'
style so as not to interfere with multiline /* */ comments if they happen
to exist.

A '//:PIDL:' comment is added whenever the pidl function or indentation
level changes, and very occasionally at other places if pidl runs for a
while without either of these things happening.

This does not affect pidl parsers that do not inherit from Parse::Pidl::Base,
and is careful to have no performance impact on non-debug generation.

This may help with semi-automated flow analysis.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2019-12-04 05:10:31 +00:00

100 lines
1.7 KiB
Perl

# Superclass for IDL structure generators
# GPL3
package Parse::Pidl::Base;
use strict;
use warnings;
use Parse::Pidl qw(fatal warning error);
use vars qw($VERSION);
$VERSION = '0.01';
sub indent {
my $self = shift;
$self->{tabs} .= "\t";
}
sub deindent {
my $self = shift;
$self->{tabs} = substr($self->{tabs}, 1);
}
sub pidl {
my ($self, $txt) = @_;
if ($txt) {
if ($txt !~ /^#/) {
$self->{res} .= $self->{tabs};
}
$self->{res} .= $txt;
}
$self->{res} .= "\n";
}
sub pidl_hdr {
my ($self, $txt) = @_;
$self->{res_hdr} .= "$txt\n";
}
sub pidl_both {
my ($self, $txt) = @_;
$self->{res} .= "$txt\n";
$self->{res_hdr} .= "$txt\n";
}
# When the PIDL_DEVELOPER env flag is set, we overwrite $self->pidl()
# and $self->pidl_hdr() to annotate the output with location
# information.
sub pidl_dev_msg {
my $self = shift;
my ($pkg, $file, $line, $sub) = caller(2);
# minimise the path
if ($file =~ m{/pidl/(lib/.+|pidl)$}) {
$file = $1;
}
my $state = $self->{dev_state} // ['uninitialised', 0, ''];
my ($ploc, $pline, $ptabs) = @$state;
my $loc = "$sub $file";
if ($loc ne $ploc or
abs($line - $pline) > 20 or
$self->{tabs} ne $ptabs) {
$self->{dev_state} = [$loc, $line, $self->{tabs}];
return " //<PIDL> $loc:$line";
}
return '';
}
if ($ENV{PIDL_DEVELOPER}) {
undef &pidl;
undef &pidl_hdr;
*Parse::Pidl::Base::pidl = sub {
my ($self, $txt) = @_;
if ($txt) {
if ($txt !~ /^#/) {
$self->{res} .= $self->{tabs};
}
$self->{res} .= $txt;
}
$self->{res} .= $self->pidl_dev_msg;
$self->{res} .= "\n";
};
*Parse::Pidl::Base::pidl_hdr = sub {
my ($self, $txt) = @_;
$txt .= $self->pidl_dev_msg;
$self->{res_hdr} .= "$txt\n";
}
}
1;