summaryrefslogtreecommitdiffstats
path: root/bindings/utility-scripts/error-analyzer.pl
blob: 60b91c1f6959c3889e22399e84fefe39cbbf7f11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell

use strict;
use File::Find ();
use Data::Dumper;

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;

sub unique {
    my @in = @_;
    my @ret = ();

    for my $x (@in) {
        push @ret, $x if (! grep /$x/, @ret);
    }
    return @ret;
}

my $functions = {};

my $p = $ARGV[0];

# Traverse desired filesystems
-d $p && File::Find::find({wanted => \&wanted}, $p);

foreach my $function (keys %$functions) {
    potential_errors($function);
}

foreach my $name (sort (keys %$functions)) {
    my $record = $functions->{$name};
    next if $record->{'return-type'} !~ /\bg?int\b/ || $record->{'return-type'} =~ /\bstatic\b/;
    my @derr = @{$record->{'errors'}};
    my @inherr = @{$record->{'inherited-errors'}[0]};
    my $path = $record->{'file'};
    print "$name ";
    my %temp = ();
    @temp{@inherr} = ();
    for (@derr) {
        delete $temp{$_};
        print "$_ ";
    }
    if (keys %temp) {
        foreach (keys %temp) {
            print "$_ ";
        }
    }
    print "\n";
}

exit;

sub potential_errors {
    my $function = shift;


    return ([],[[],[]]) if ! exists $functions->{$function};
    my $record = $functions->{$function};

    return ([],[[],[]]) if $record->{'return-type'} !~ /\bg?int\b/ || $record->{'recursing'};

    if (! exists $record->{'inherited-errors'}) {
        my @inheritederrors;
        my @froms;
        $record->{'recursing'} = 1;

        foreach my $call (@{$record->{'calls'}}) {
            my ($err,$inh) = potential_errors($call);
            my ($suberr,$subfrom) = @$inh;

            if (@$err || @$suberr) {
                push @froms, $call;
                push @inheritederrors, (@$err, @$suberr);
            }
        }
        $record->{'inherited-errors'} = [[ unique(@inheritederrors) ],[@froms]];
        delete $record->{'recursing'};
    }
    return ($record->{'errors'},$record->{'inherited-errors'});
}
    

sub parse_file {
    my $file = shift;
    my $path = shift;
    my $lastline;
    my $curfunction;
    my $curtype;
    my @curerrors;
    my @curcalls;
    my $infunction = 0;
    open FD, "<$file";
    while (<FD>) {

        MATCHING: {
            if ($infunction) {
                if (/^\}/) {
                    #print "finished funcctions $curfunction\n";
                    $functions->{$curfunction} = { name => $curfunction, 'return-type' => $curtype, 'errors' => [ unique(@curerrors) ], 'calls' => [ @curcalls], 'file' => $path};
                    $infunction = 0;
                    last MATCHING;
                }
                while (/(?:\breturn\b|=).*?([A-Za-z_]+)\(/g) {
                    push @curcalls, $1;
                }
                pos = 0;
                while (/(LASSO_[A-Z_]*_ERROR_[A-Z_]*|LASSO_ERROR_[A-Z_]*)/g) {
                    push @curerrors, $1;
                }
                last MATCHING;
            }
            if (/^([a-z_]+)\([^;]*$/) {
                $curfunction = $1;
                chop $lastline;
                $curtype = $lastline;
                @curerrors = ();
                @curcalls = ();
                last MATCHING;
            }
            if ($curfunction && /^\{/) {
                $infunction = 1;
                last MATCHING;
            }
        }
        $lastline = $_;

    }
    close FD;
}

sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);

    parse_file($_,$File::Find::name) if ($_ =~ /^.*\.c$/s && $File::Find::name !~ /^.*\.svn.*/);
}