1 | #!/usr/bin/perl |
---|
2 | # ================================================================= # |
---|
3 | # # |
---|
4 | # File : postcompile.pl # |
---|
5 | # Purpose : filter gcc shadow spam # |
---|
6 | # # |
---|
7 | # Coded by Ralf Westram (coder@reallysoft.de) in September 2007 # |
---|
8 | # Institute of Microbiology (Technical University Munich) # |
---|
9 | # http://www.arb-home.de/ # |
---|
10 | # # |
---|
11 | # ================================================================= # |
---|
12 | |
---|
13 | use strict; |
---|
14 | use warnings; |
---|
15 | |
---|
16 | # Note: g++ must be called with -fmessage-length=0 |
---|
17 | |
---|
18 | my $show_supressed_lines = 0; |
---|
19 | my $show_filtered_lines = 0; |
---|
20 | |
---|
21 | # regexps for whole line: |
---|
22 | my $reg_file = qr/^([^:]+):([0-9]+):(([0-9]+):)?\s/; # finds all messages |
---|
23 | my $reg_file_noline = qr/^([^:]+):\s/; # finds all messages w/o linenumber (if not matched $reg_file) |
---|
24 | my $reg_included = qr/^In\sfile\sincluded\sfrom\s(.*)[,:]/; |
---|
25 | my $reg_included2 = qr/^\s+from\s(.*)[,:]/; |
---|
26 | my $reg_location = qr/^[^:]+:\sIn\sfunction\s/; |
---|
27 | my $reg_location2 = qr/^[^:]+:\sAt\stop\slevel:/; |
---|
28 | my $reg_clang_dirt = qr/^ANALYZE:\s/; |
---|
29 | |
---|
30 | # regexps for messages: |
---|
31 | my $reg_is_error = qr/^error:\s/i; |
---|
32 | my $reg_is_warning = qr/^warning:\s/i; |
---|
33 | my $reg_is_note = qr/^note:\s/i; |
---|
34 | my $reg_is_instantiated = qr/^\s\s+instantiated\sfrom\s/; |
---|
35 | my $reg_is_required = qr/^\s\s+required\s(from|by)\s/; |
---|
36 | |
---|
37 | # regexps for warning messages (for part behind 'warning: ') |
---|
38 | my $reg_shadow_warning = qr/^declaration\sof\s.*\sshadows\s/; |
---|
39 | my $reg_shadow_location = qr/^shadowed\s/; |
---|
40 | |
---|
41 | # filter unwanted -Weffc++ warnings |
---|
42 | my $filter_Weffpp = 1; |
---|
43 | my @reg_Weffpp = ( |
---|
44 | qr/only\sdefines\sprivate\sconstructors\sand\shas\sno\sfriends/, # unwanted warning about singleton-class where the only instance exists as a static member of itself |
---|
45 | qr/^base\sclass\s.*has\s*(a|accessible)\snon-virtual\sdestructor/, |
---|
46 | qr/\sshould\sbe\sinitialized\sin\sthe\smember\sinitialization\slist/, |
---|
47 | qr/boost::icl::(insert|add)_iterator<ContainerT>.*should\sreturn/, # filter boost-iterator postfix operators warnings |
---|
48 | qr/^\s\sbut\sdoes\snot\soverride/, # belongs to reg_Weffpp_copyable |
---|
49 | qr/^\s\sor\s\'operator=/, # belongs to reg_Weffpp_copyable |
---|
50 | ); |
---|
51 | |
---|
52 | my $filter_Weffpp_copyable = 0; # 1 = filter copy-ctor/op=-warning, 0 = check for Noncopyable and warn |
---|
53 | my $reg_Weffpp_copyable = qr/'(class|struct)\s([A-Za-z_0-9:]+).*'\shas\spointer\sdata\smembers/; # occurs also if derived from 'Noncopyable' |
---|
54 | |
---|
55 | # regexps for files: |
---|
56 | my $reg_user_include = qr/^\/usr\/include\//; |
---|
57 | my $reg_HEADERLIBS_include = qr/\/HEADERLIBS\//; |
---|
58 | |
---|
59 | my $stop_after_first_error = 0; |
---|
60 | my $hide_warnings = 0; |
---|
61 | |
---|
62 | # ---------------------------------------- |
---|
63 | |
---|
64 | sub getModtime($) { |
---|
65 | my ($fileOrDir) = @_; |
---|
66 | my $modtime = (stat($fileOrDir))[9]; |
---|
67 | return $modtime; |
---|
68 | } |
---|
69 | |
---|
70 | my %derived_from_NC = (); # key=classname, value=1/0 |
---|
71 | my $NC_save_name = $ENV{ARBHOME}.'/SOURCE_TOOLS/postcompile.sav'; |
---|
72 | my $NC_loaded = 0; |
---|
73 | my $NC_loaded_timestamp; |
---|
74 | my $NC_need_save = 0; |
---|
75 | |
---|
76 | sub load_from_NC() { |
---|
77 | if (-f $NC_save_name) { |
---|
78 | $NC_loaded_timestamp = getModtime($NC_save_name); |
---|
79 | my $age = $NC_loaded_timestamp - time; |
---|
80 | if ($age<3*60) { # never load data older than 3 min |
---|
81 | open(NC,'<'.$NC_save_name); |
---|
82 | foreach (<NC>) { |
---|
83 | chomp; |
---|
84 | if (/^([01]),/o) { |
---|
85 | $derived_from_NC{$'} = $1; |
---|
86 | } |
---|
87 | } |
---|
88 | close(NC); |
---|
89 | } |
---|
90 | else { |
---|
91 | $NC_loaded_timestamp = 0; # epoch |
---|
92 | } |
---|
93 | } |
---|
94 | else { |
---|
95 | $NC_loaded_timestamp = 0; # epoch |
---|
96 | } |
---|
97 | $NC_loaded = 1; |
---|
98 | } |
---|
99 | |
---|
100 | sub save_from_NC() { |
---|
101 | if ($NC_need_save==1) { |
---|
102 | my $mt = 0; |
---|
103 | if (-f $NC_save_name) { $mt = getModtime($NC_save_name); } |
---|
104 | if ($mt>$NC_loaded_timestamp) { # changed on disk |
---|
105 | load_from_NC(); # does simple merge |
---|
106 | } |
---|
107 | my $NC_save_name_private = $NC_save_name.'.'.$$; |
---|
108 | open(NC,'>'.$NC_save_name_private); |
---|
109 | foreach (sort keys %derived_from_NC) { |
---|
110 | print NC $derived_from_NC{$_}.','.$_."\n"; |
---|
111 | } |
---|
112 | close(NC); |
---|
113 | |
---|
114 | rename($NC_save_name_private,$NC_save_name) || die "can't rename '$NC_save_name_private' to '$NC_save_name' (Reason: $!)"; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | sub advice_derived_from_Noncopyable($$$) { |
---|
119 | # Note: you can silence the Noncopyable-warning by |
---|
120 | # adding a comment containing 'Noncopyable' behind the 'class'-line |
---|
121 | my ($classname,$file,$linenr) = @_; |
---|
122 | |
---|
123 | if ($NC_loaded==0) { load_from_NC(); } |
---|
124 | my $is_a_NC = $derived_from_NC{$classname}; |
---|
125 | if (defined $is_a_NC) { |
---|
126 | return 0; # do not warn twice |
---|
127 | } |
---|
128 | |
---|
129 | if (not -f $file) { |
---|
130 | die "no such file '$file'"; |
---|
131 | } |
---|
132 | |
---|
133 | my $uq_classname = $classname; |
---|
134 | while ($uq_classname =~ /::/o) { $uq_classname = $'; } # skip namespace prefixes |
---|
135 | |
---|
136 | open(FILE,'<'.$file) || die "can't read '$file' (Reason: $!)"; |
---|
137 | my $line; |
---|
138 | my $line_count = 0; |
---|
139 | LINE: while (defined($line=<FILE>)) { |
---|
140 | $line_count++; |
---|
141 | if ($line_count==$linenr) { |
---|
142 | if ($line =~ /(class|struct)\s+($uq_classname|$classname)(.*)Noncopyable/) { |
---|
143 | my $prefix = $3; |
---|
144 | if (not $prefix =~ /\/\//) { # if we have a comment, assume it mentions that the class is derived from a Noncopyable |
---|
145 | if (not $prefix =~ /virtual/) { |
---|
146 | print $file.':'.$linenr.': inheritance from Noncopyable should be virtual'."\n"; |
---|
147 | } |
---|
148 | } |
---|
149 | $is_a_NC = 1; |
---|
150 | } |
---|
151 | else { $is_a_NC = 0; } |
---|
152 | last LINE; |
---|
153 | } |
---|
154 | } |
---|
155 | close(FILE); |
---|
156 | $derived_from_NC{$classname} = $is_a_NC; |
---|
157 | $NC_need_save = 1; |
---|
158 | |
---|
159 | return 1-$is_a_NC; |
---|
160 | } |
---|
161 | |
---|
162 | # results for Weffpp_warning_wanted(): |
---|
163 | my $WANTED = 0; |
---|
164 | my $WANTED_NO_RELATED = 1; |
---|
165 | my $UNWANTED = 2; |
---|
166 | |
---|
167 | sub Weffpp_warning_wanted($$\$) { |
---|
168 | my ($file,$line,$warning_text_r) = @_; |
---|
169 | |
---|
170 | if ($filter_Weffpp==1) { |
---|
171 | foreach my $reg (@reg_Weffpp) { |
---|
172 | return $UNWANTED if ($$warning_text_r =~ $reg); |
---|
173 | } |
---|
174 | if ($$warning_text_r =~ $reg_Weffpp_copyable) { |
---|
175 | my $classname = $2; |
---|
176 | return $UNWANTED if ($filter_Weffpp_copyable==1); |
---|
177 | return $UNWANTED if (advice_derived_from_Noncopyable($classname,$file,$line)==0); |
---|
178 | $$warning_text_r = $$warning_text_r.' (and is neighter derived from Noncopyable nor defines copy-ctor and op=)'; |
---|
179 | return $WANTED_NO_RELATED; |
---|
180 | } |
---|
181 | } |
---|
182 | return $WANTED; |
---|
183 | } |
---|
184 | |
---|
185 | sub warning($\@) { |
---|
186 | my ($msg,$out_r) = @_; |
---|
187 | push @$out_r, '[postcompile]: '.$msg; |
---|
188 | } |
---|
189 | |
---|
190 | my $shadow_warning = undef; |
---|
191 | |
---|
192 | sub store_shadow($\@) { |
---|
193 | my ($warn,$out_r) = @_; |
---|
194 | if (defined $shadow_warning) { |
---|
195 | warning('unprocessed shadow_warning:', @$out_r); |
---|
196 | push @$out_r, $shadow_warning; |
---|
197 | } |
---|
198 | $shadow_warning = $warn; |
---|
199 | } |
---|
200 | |
---|
201 | my $last_pushed_related = 0; |
---|
202 | |
---|
203 | sub push_loc_and_related($$\@\@) { |
---|
204 | my ($location_info,$message,$related_r,$out_r) = @_; |
---|
205 | if (defined $location_info) { |
---|
206 | push @$out_r, $location_info; |
---|
207 | $location_info = undef; |
---|
208 | } |
---|
209 | push @$out_r, $message; |
---|
210 | $last_pushed_related = scalar(@$related_r); |
---|
211 | foreach (@$related_r) { push @$out_r, $_; } |
---|
212 | } |
---|
213 | |
---|
214 | sub drop_last_pushed_relateds(\@) { |
---|
215 | my ($out_r) = @_; |
---|
216 | if ($last_pushed_related>0) { |
---|
217 | my $before_related = scalar(@$out_r) - $last_pushed_related - 1; |
---|
218 | $before_related>=0 || die "impossible (out_r-elements=".scalar(@$out_r)."; last_pushed_related=$last_pushed_related)"; |
---|
219 | @$out_r = @$out_r[0 .. $before_related]; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | sub included_from_here($) { |
---|
224 | my ($loc) = @_; |
---|
225 | return $loc.': included from here'; |
---|
226 | } |
---|
227 | |
---|
228 | sub suppress($\@) { |
---|
229 | my ($curr,$out_r) = @_; |
---|
230 | if ($show_supressed_lines==1) { |
---|
231 | warning('suppressed: '.$curr,@$out_r); |
---|
232 | } |
---|
233 | return undef; |
---|
234 | } |
---|
235 | |
---|
236 | sub is_system_or_builtin($) { |
---|
237 | my ($file) = @_; |
---|
238 | return (($file =~ $reg_user_include) or ($file eq '<built-in>') or ($file =~ $reg_HEADERLIBS_include)); |
---|
239 | } |
---|
240 | |
---|
241 | sub suppress_shadow_warning_for($) { |
---|
242 | my ($file) = @_; |
---|
243 | return is_system_or_builtin($file); |
---|
244 | } |
---|
245 | |
---|
246 | sub parse_input(\@) { |
---|
247 | my ($out_r) = @_; |
---|
248 | my @related = (); |
---|
249 | my $location_info = undef; |
---|
250 | |
---|
251 | my @warnout = (); |
---|
252 | my @errout = (); |
---|
253 | |
---|
254 | my $did_show_previous = 0; |
---|
255 | my $is_error = 0; |
---|
256 | my $curr_out_r = \@warnout; |
---|
257 | |
---|
258 | LINE: while (defined($_=<STDIN>)) { |
---|
259 | chomp; |
---|
260 | my $filter_current = 0; |
---|
261 | |
---|
262 | if ($_ =~ $reg_file) { |
---|
263 | my ($file,$line,$msg) = ($1,$2,$'); |
---|
264 | |
---|
265 | if ($msg =~ $reg_is_warning) { |
---|
266 | my $warn_text = $'; |
---|
267 | if ($warn_text =~ $reg_shadow_warning) { |
---|
268 | if (not $' =~ /this/) { # don't store this warnings (no location follows) |
---|
269 | store_shadow($_,@warnout); |
---|
270 | $_ = suppress($_,@warnout); |
---|
271 | } |
---|
272 | elsif (suppress_shadow_warning_for($file)) { |
---|
273 | $_ = suppress($_,@warnout); |
---|
274 | # $location_info = undef; |
---|
275 | } |
---|
276 | } |
---|
277 | elsif ($warn_text =~ $reg_shadow_location) { |
---|
278 | if (not defined $shadow_warning) { warning('no shadow_warning seen',@warnout); } |
---|
279 | else { |
---|
280 | if (suppress_shadow_warning_for($file)) { |
---|
281 | # don't warn about /usr/include or <built-in> shadowing |
---|
282 | $_ = suppress($_,@warnout); |
---|
283 | @related = (); |
---|
284 | $location_info = undef; |
---|
285 | } |
---|
286 | else { |
---|
287 | if (defined $location_info) { |
---|
288 | push @warnout, $location_info; |
---|
289 | $location_info = undef; |
---|
290 | } |
---|
291 | push @warnout, $shadow_warning; |
---|
292 | } |
---|
293 | $shadow_warning = undef; |
---|
294 | } |
---|
295 | } |
---|
296 | else { |
---|
297 | my $warn_is = Weffpp_warning_wanted($file,$line,$warn_text); |
---|
298 | if ($warn_is == $UNWANTED) { |
---|
299 | $filter_current = 1; # ignore this warning |
---|
300 | } |
---|
301 | elsif ($warn_is == $WANTED_NO_RELATED) { |
---|
302 | @related = (); # drop related messages |
---|
303 | } |
---|
304 | # rebuild warning (Weffpp_warning_wanted might modify the message) |
---|
305 | $_ = $file.':'.$line.': warning: '.$warn_text; |
---|
306 | } |
---|
307 | $is_error = 0; |
---|
308 | $curr_out_r = \@warnout; |
---|
309 | } |
---|
310 | elsif ($msg =~ $reg_is_error) { |
---|
311 | $is_error = 1; |
---|
312 | $curr_out_r = \@errout; |
---|
313 | } |
---|
314 | elsif ($msg =~ $reg_is_instantiated) { |
---|
315 | push @related, $_; |
---|
316 | $_ = suppress($_,@warnout); |
---|
317 | } |
---|
318 | elsif ($msg =~ $reg_is_required) { |
---|
319 | push @related, $_; |
---|
320 | $_ = suppress($_,@warnout); |
---|
321 | } |
---|
322 | elsif ($msg =~ $reg_is_note) { |
---|
323 | if ($did_show_previous==0) { |
---|
324 | $_ = suppress($_,@warnout); |
---|
325 | } |
---|
326 | else { |
---|
327 | if ($msg =~ /in\sexpansion\sof\smacro/o) { |
---|
328 | drop_last_pushed_relateds(@$curr_out_r); |
---|
329 | } |
---|
330 | } |
---|
331 | } |
---|
332 | } |
---|
333 | elsif ($_ =~ $reg_location or $_ =~ $reg_location2) { |
---|
334 | $location_info = $_; |
---|
335 | $_ = suppress($_,@warnout); |
---|
336 | } |
---|
337 | elsif ($_ =~ $reg_included) { |
---|
338 | push @related, included_from_here($1); |
---|
339 | $_ = suppress($_,@warnout); |
---|
340 | } |
---|
341 | elsif ($_ =~ $reg_clang_dirt) { |
---|
342 | $_ = undef; |
---|
343 | } |
---|
344 | elsif ($_ =~ $reg_file_noline) { |
---|
345 | if (/^cc1plus:.*error/) { |
---|
346 | ; # display normally |
---|
347 | } |
---|
348 | else { |
---|
349 | push @related, included_from_here($1); |
---|
350 | $_ = suppress($_,@warnout); |
---|
351 | } |
---|
352 | } |
---|
353 | elsif (@related) { |
---|
354 | if ($_ =~ $reg_included2) { |
---|
355 | push @related, included_from_here($1); |
---|
356 | $_ = suppress($_,@warnout); |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | if (defined $_) { |
---|
361 | if ($filter_current==0) { |
---|
362 | push_loc_and_related($location_info,$_,@related,@$curr_out_r); |
---|
363 | $did_show_previous = 1; |
---|
364 | |
---|
365 | if (($is_error==1) and ($stop_after_first_error==1)) { |
---|
366 | @warnout = (); # drop warnings |
---|
367 | last LINE; |
---|
368 | } |
---|
369 | } |
---|
370 | else { |
---|
371 | die "can't filter errors (Error=$_)" if ($is_error==1); |
---|
372 | if ($show_filtered_lines==1) { warning('filtered: '.$_, @$curr_out_r); } |
---|
373 | $did_show_previous = 0; |
---|
374 | } |
---|
375 | @related = (); |
---|
376 | } |
---|
377 | } |
---|
378 | |
---|
379 | @$out_r = @errout; |
---|
380 | if ($hide_warnings==0) { push @$out_r, @warnout; } |
---|
381 | } |
---|
382 | |
---|
383 | sub main() { |
---|
384 | my $args = scalar(@ARGV); |
---|
385 | my $pass_through = 0; |
---|
386 | while ($args>0) { |
---|
387 | my $arg = shift(@ARGV); |
---|
388 | if ($arg eq '--no-warnings') { $hide_warnings = 1; } |
---|
389 | elsif ($arg eq '--only-first-error') { $stop_after_first_error = 1; } |
---|
390 | elsif ($arg eq '--original') { $pass_through = 1; } |
---|
391 | elsif ($arg eq '--show-useless-Weff++') { $filter_Weffpp = 0; } |
---|
392 | elsif ($arg eq '--hide-Noncopyable-advices') { $filter_Weffpp_copyable = 1; } |
---|
393 | else { |
---|
394 | die "Usage: postcompile.pl [--no-warnings] [--only-first-error] [--filter-Weff++] [--hide-Noncopyable-advices] [--original]\n"; |
---|
395 | } |
---|
396 | $args--; |
---|
397 | } |
---|
398 | |
---|
399 | eval { |
---|
400 | if ($pass_through==1) { |
---|
401 | while (defined($_=<STDIN>)) { print $_; } |
---|
402 | } |
---|
403 | else { |
---|
404 | my @out = (); |
---|
405 | parse_input(@out); |
---|
406 | store_shadow(undef,@out); |
---|
407 | foreach (@out) { print "$_\n"; } |
---|
408 | } |
---|
409 | }; |
---|
410 | my $err = $@; |
---|
411 | save_from_NC(); |
---|
412 | if ($err) { die $err; } |
---|
413 | } |
---|
414 | main(); |
---|