summaryrefslogtreecommitdiffstats
path: root/src/util/def-check.pl
blob: f4d195b35d3c20f410f777affa3f95ead8aace78 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/athena/bin/perl
eval 'exec /usr/athena/bin/perl -S $0 ${1+"$@"}'
	if $running_under_some_shell;

@convC = ();
@convK = ();
@convD = ();

open H, "<$ARGV[0]" || die "aaaa! $!";
open D, "<$ARGV[1]";

LINE:
while (! eof H) {
    $_ = <H>;
    chop;
    # get calling convention info for function decls
    # what about function pointer typedefs?
    # need to verify unhandled syntax actually triggers a report, not ignored
    # blank lines
    if (/^[ \t]*$/) {
        next LINE;
    }
  Top:
    # drop preprocessor directives
    if (/^ *#/) {
        next LINE;
    }
    if (/^ *\?==/) {
        next LINE;
    }
    s/#.*$//;
    if (/^} *$/) {
        next LINE;
    }
    # strip comments
  Cloop1:
    if (/\/\*./) {
	s;/\*[^*]*;/*;;
	s;/\*\*([^/]);/*$1;;
	s;/\*\*$;/*;;
	s;/\*\*/; ;g;
	goto Cloop1;
    }
    # multi-line comments?
    if (/\/\*$/) {
	$_ .= "\n";
	$len1 = length;
	$_ .= <H>;
	chop if $len1 < length;
	goto Cloop1 if /\/\*./;
    }
    # blank lines
    if (/^[ \t]*$/) {
        next LINE;
    }
    if (/ *extern "C" {/) {
        next LINE;
    }
    # elide struct definitions
  Struct1:
    if (/{[^}]*}/) {
	s/{[^}]*}/ /g;
	goto Struct1;
    }
    # multi-line defs
    if (/{/) {
	$_ .= "\n";
	$len1 = length;
	$_ .= <H>;
	chop if $len1 < length;
	goto Struct1;
    }
  Semi:
    unless (/;/) {
	$_ .= "\n";
	$len1 = length;
	$_ .= <H>;
	chop if $len1 < length;
	s/\n/ /g;
	s/[ \t]+/ /g;
	s/^[ \t]*//;
	goto Top;
    }
    if (/^typedef[^;]*;/) {
	s/^typedef[^;]*;//g;
	goto Semi;
    }
    if (/^struct[^\(\)]*;/) {
	s/^struct[^\(\)]*;//g;
	goto Semi;
    }
    # should just have simple decls now; split lines at semicolons
    s/ *;[ \t]*$//;
    s/ *;/\n/g;
    if (/^[ \t]*$/) {
        next LINE;
    }
    s/[ \t]*$//;
    goto Notfunct unless /\(.*\)/;
    # here, is probably function decl
    # strip simple arg list - parens, no parens inside; discard, iterate.
    # the iteration should deal with function pointer args.
  Striparg:
    if (/ *\([^\(\)]*\)/) {
	s/ *\([^\(\)]*\)//g;
	goto Striparg;
    }
    # replace return type etc with one token indicating calling convention
    if (/CALLCONV/) {
	if (/KRB5_CALLCONV_C/) {
	    s/^.*KRB5_CALLCONV_C *//;
	    push @convC, $_;
	} elsif (/KRB5_CALLCONV/) {
	    s/^.*KRB5_CALLCONV *//;
	    push @convK, $_;
	} else {
	    die horribly;
	}
	goto Hadcallc;
    }
    # deal with no CALLCONV indicator
    s/^.* (\w+) *$/$1/;
    push @convD, $_;
  Hadcallc:
    goto Skipnotf;
  Notfunct:
    # probably a variable
    s/^/VARIABLE_DECL /;
  Skipnotf:
    # toss blank lines
    if (/^[ \t]*$/) {
        next LINE;
    }
}

print join("\n\t", "Using default calling convention:", sort(@convD));
print join("\n\t", "\nUsing KRB5_CALLCONV:", sort(@convK));
print join("\n\t", "\nUsing KRB5_C_CALLCONV:", sort(@convC));
print "\n";

%conv = ();
map { $conv{$_} = "default"; } @convD;
map { $conv{$_} = "KRB5"; } @convK;
map { $conv{$_} = "KRB5_C"; } @convC;

LINE2:
while (! eof D) {
    $_ = <D>;
    chop;
    #
    if (/^;/) {
        $printit = 0;
        next LINE2;
    }
    if (/^[ \t]*$/) {
        $printit = 0;
        next LINE2;
    }
    if (/^EXPORTS/) {
        $printit = 0;
        next LINE2;
    }
    s/[ \t]*//g;
    my($xconv);
    if (/!CALLCONV/) {
	$xconv = "KRB5_C";
    } else {
	$xconv = "KRB5";
    }
    s/;.*$//;
    if (!defined($conv{$_})) {
	print "No calling convention specified for $_!\n";
    } elsif ($conv{$_} != $xconv) {
	print "Function $_ should have calling convention '$xconv', but has '$conv{$_}' instead.\n";
    } else {
#	print "Function $_ is okay.\n";
    }
}

#print "Calling conventions defined for: ", keys(%conv);