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_suppressed_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\s(function|member\sfunction|instantiation)\s/; |
---|
27 | my $reg_location2 = qr/^[^:]+:\sAt\stop\slevel:/; |
---|
28 | my $reg_location3 = qr/^At\sglobal\sscope:/; |
---|
29 | my $reg_clang_dirt = qr/^ANALYZE:\s/; |
---|
30 | |
---|
31 | # regexps for messages: |
---|
32 | my $reg_is_error = qr/^error:\s/i; |
---|
33 | my $reg_is_warning = qr/^warning:\s/i; |
---|
34 | my $reg_is_note = qr/^note:\s/i; |
---|
35 | my $reg_is_instantiated = qr/^\s\s+instantiated\sfrom\s/; |
---|
36 | my $reg_is_required = qr/^\s\s+required\s(from|by)\s/; |
---|
37 | |
---|
38 | # regexps for warning messages (for part behind 'warning: ') |
---|
39 | my $reg_shadow_warning = qr/^declaration\sof\s.*\sshadows\s/; |
---|
40 | my $reg_shadow_location = qr/^shadowed\s/; |
---|
41 | |
---|
42 | # filter unwanted -Weffc++ warnings |
---|
43 | my $filter_Weffpp = 1; |
---|
44 | my @reg_Weffpp = ( |
---|
45 | 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 |
---|
46 | qr/^base\sclass\s.*has\s*(a|accessible)\snon-virtual\sdestructor/, |
---|
47 | qr/\sshould\sbe\sinitialized\sin\sthe\smember\sinitialization\slist/, |
---|
48 | qr/boost::icl::(insert|add)_iterator<ContainerT>.*should\sreturn/, # filter boost-iterator postfix operators warnings |
---|
49 | qr/^\s\sbut\sdoes\snot\soverride/, # belongs to reg_Weffpp_copyable |
---|
50 | qr/^\s\sor\s\'operator=/, # belongs to reg_Weffpp_copyable |
---|
51 | ); |
---|
52 | |
---|
53 | my $filter_Weffpp_copyable = 0; # 1 = filter copy-ctor/op=-warning, 0 = check for Noncopyable and warn |
---|
54 | my $reg_Weffpp_copyable = qr/'(class|struct)\s([A-Za-z_0-9:]+).*'\shas\spointer\sdata\smembers/; # occurs also if derived from 'Noncopyable' |
---|
55 | |
---|
56 | # regexps for files: |
---|
57 | my $reg_user_include = qr/^\/usr\/include\//; |
---|
58 | my $reg_HEADERLIBS_include = qr/\/HEADERLIBS\//; |
---|
59 | |
---|
60 | my $stop_after_first_error = 0; |
---|
61 | my $hide_warnings = 0; |
---|
62 | |
---|
63 | my $dump_loop_optimization = 0; |
---|
64 | my $check_loop_optimization = 0; |
---|
65 | my $checked_file = undef; # contains name of sourcefile (if it shall be checked) |
---|
66 | |
---|
67 | my $ARBHOME = $ENV{ARBHOME}; |
---|
68 | my $exitcode = 0; |
---|
69 | |
---|
70 | # ---------------------------------------- |
---|
71 | |
---|
72 | sub detect_needLoopCheck($) { |
---|
73 | my ($source) = @_; |
---|
74 | if ($check_loop_optimization) { |
---|
75 | use Cwd; |
---|
76 | my $currDir = Cwd::getcwd(); |
---|
77 | my $relDir = undef; |
---|
78 | if ($currDir =~ /^$ARBHOME/) { $relDir = $'; } |
---|
79 | if (not defined $relDir) { die "Can't detect ARBHOME-relative working directory"; } |
---|
80 | $relDir =~ s/^\/*//og; |
---|
81 | my $relfile = $relDir.'/'.$source; |
---|
82 | my $checked_list = $ARBHOME.'/SOURCE_TOOLS/vectorized.source'; |
---|
83 | |
---|
84 | open(LIST,'<'.$checked_list) || die "can't read '$checked_list' (Reason: $!)"; |
---|
85 | SEARCH: foreach (<LIST>) { |
---|
86 | if (not /^\s*\#/) { # ignore comments |
---|
87 | if (not /^\s*$/) { # ignore empty lines |
---|
88 | chomp($_); |
---|
89 | if ($relfile eq $_) { |
---|
90 | $checked_file = $source; |
---|
91 | last SEARCH; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | close(LIST); |
---|
97 | if (not defined $checked_file) { |
---|
98 | $check_loop_optimization = undef; |
---|
99 | } |
---|
100 | elsif ($dump_loop_optimization==2) { |
---|
101 | $dump_loop_optimization = 0; # do not dump candidates, if file is already checked |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | sub getModtime($) { |
---|
107 | my ($fileOrDir) = @_; |
---|
108 | my $modtime = (stat($fileOrDir))[9]; |
---|
109 | return $modtime; |
---|
110 | } |
---|
111 | |
---|
112 | my %derived_from_NC = (); # key=classname, value=1/0 |
---|
113 | my $NC_save_name = $ARBHOME.'/SOURCE_TOOLS/postcompile.sav'; |
---|
114 | my $NC_loaded = 0; |
---|
115 | my $NC_loaded_timestamp; |
---|
116 | my $NC_need_save = 0; |
---|
117 | |
---|
118 | sub load_from_NC() { |
---|
119 | if (-f $NC_save_name) { |
---|
120 | $NC_loaded_timestamp = getModtime($NC_save_name); |
---|
121 | my $age = $NC_loaded_timestamp - time; |
---|
122 | if ($age<3*60) { # never load data older than 3 min |
---|
123 | open(NC,'<'.$NC_save_name); |
---|
124 | foreach (<NC>) { |
---|
125 | chomp; |
---|
126 | if (/^([01]),/o) { |
---|
127 | $derived_from_NC{$'} = $1; |
---|
128 | } |
---|
129 | } |
---|
130 | close(NC); |
---|
131 | } |
---|
132 | else { |
---|
133 | $NC_loaded_timestamp = 0; # epoch |
---|
134 | } |
---|
135 | } |
---|
136 | else { |
---|
137 | $NC_loaded_timestamp = 0; # epoch |
---|
138 | } |
---|
139 | $NC_loaded = 1; |
---|
140 | } |
---|
141 | |
---|
142 | sub save_from_NC() { |
---|
143 | if ($NC_need_save==1) { |
---|
144 | my $mt = 0; |
---|
145 | if (-f $NC_save_name) { $mt = getModtime($NC_save_name); } |
---|
146 | if ($mt>$NC_loaded_timestamp) { # changed on disk |
---|
147 | load_from_NC(); # does simple merge |
---|
148 | } |
---|
149 | my $NC_save_name_private = $NC_save_name.'.'.$$; |
---|
150 | open(NC,'>'.$NC_save_name_private); |
---|
151 | foreach (sort keys %derived_from_NC) { |
---|
152 | print NC $derived_from_NC{$_}.','.$_."\n"; |
---|
153 | } |
---|
154 | close(NC); |
---|
155 | |
---|
156 | rename($NC_save_name_private,$NC_save_name) || die "can't rename '$NC_save_name_private' to '$NC_save_name' (Reason: $!)"; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | sub advice_derived_from_Noncopyable($$$) { |
---|
161 | # Note: you can silence the Noncopyable-warning by |
---|
162 | # adding a comment containing 'Noncopyable' behind the 'class'-line |
---|
163 | my ($classname,$file,$linenr) = @_; |
---|
164 | |
---|
165 | if ($NC_loaded==0) { load_from_NC(); } |
---|
166 | my $is_a_NC = $derived_from_NC{$classname}; |
---|
167 | if (defined $is_a_NC) { |
---|
168 | return 0; # do not warn twice |
---|
169 | } |
---|
170 | |
---|
171 | if (not -f $file) { |
---|
172 | die "no such file '$file'"; |
---|
173 | } |
---|
174 | |
---|
175 | my $uq_classname = $classname; |
---|
176 | while ($uq_classname =~ /::/o) { $uq_classname = $'; } # skip namespace prefixes |
---|
177 | |
---|
178 | open(FILE,'<'.$file) || die "can't read '$file' (Reason: $!)"; |
---|
179 | my $line; |
---|
180 | my $line_count = 0; |
---|
181 | LINE: while (defined($line=<FILE>)) { |
---|
182 | $line_count++; |
---|
183 | if ($line_count==$linenr) { |
---|
184 | if ($line =~ /(class|struct)\s+($uq_classname|$classname)(.*)Noncopyable/) { |
---|
185 | my $prefix = $3; |
---|
186 | if (not $prefix =~ /\/\//) { # if we have a comment, assume it mentions that the class is derived from a Noncopyable |
---|
187 | if (not $prefix =~ /virtual/) { |
---|
188 | print $file.':'.$linenr.': inheritance from Noncopyable should be virtual'."\n"; |
---|
189 | } |
---|
190 | } |
---|
191 | $is_a_NC = 1; |
---|
192 | } |
---|
193 | else { $is_a_NC = 0; } |
---|
194 | last LINE; |
---|
195 | } |
---|
196 | } |
---|
197 | close(FILE); |
---|
198 | $derived_from_NC{$classname} = $is_a_NC; |
---|
199 | $NC_need_save = 1; |
---|
200 | |
---|
201 | return 1-$is_a_NC; |
---|
202 | } |
---|
203 | |
---|
204 | # results for Weffpp_warning_wanted(): |
---|
205 | my $WANTED = 0; |
---|
206 | my $WANTED_NO_RELATED = 1; |
---|
207 | my $UNWANTED = 2; |
---|
208 | |
---|
209 | sub Weffpp_warning_wanted($$\$) { |
---|
210 | my ($file,$line,$warning_text_r) = @_; |
---|
211 | |
---|
212 | if ($filter_Weffpp==1) { |
---|
213 | foreach my $reg (@reg_Weffpp) { |
---|
214 | return $UNWANTED if ($$warning_text_r =~ $reg); |
---|
215 | } |
---|
216 | if ($$warning_text_r =~ $reg_Weffpp_copyable) { |
---|
217 | my $classname = $2; |
---|
218 | return $UNWANTED if ($filter_Weffpp_copyable==1); |
---|
219 | return $UNWANTED if (advice_derived_from_Noncopyable($classname,$file,$line)==0); |
---|
220 | $$warning_text_r = $$warning_text_r.' (and is neighter derived from Noncopyable nor defines copy-ctor and op=)'; |
---|
221 | return $WANTED_NO_RELATED; |
---|
222 | } |
---|
223 | } |
---|
224 | return $WANTED; |
---|
225 | } |
---|
226 | |
---|
227 | sub warning($\@) { |
---|
228 | my ($msg,$out_r) = @_; |
---|
229 | push @$out_r, '[postcompile/'.$$.']: '.$msg; |
---|
230 | } |
---|
231 | |
---|
232 | my $shadow_warning = undef; |
---|
233 | |
---|
234 | sub store_shadow($\@) { |
---|
235 | my ($warn,$out_r) = @_; |
---|
236 | if (defined $shadow_warning) { |
---|
237 | warning('unprocessed shadow_warning:', @$out_r); |
---|
238 | push @$out_r, $shadow_warning; |
---|
239 | } |
---|
240 | $shadow_warning = $warn; |
---|
241 | } |
---|
242 | |
---|
243 | my $last_pushed_related = 0; |
---|
244 | |
---|
245 | sub push_loc_and_related($$\@\@) { |
---|
246 | my ($location_info,$message,$related_r,$out_r) = @_; |
---|
247 | if (defined $location_info) { |
---|
248 | push @$out_r, $location_info; |
---|
249 | } |
---|
250 | push @$out_r, $message; |
---|
251 | $last_pushed_related = scalar(@$related_r); |
---|
252 | |
---|
253 | # show related info (include-notes behind rest) |
---|
254 | foreach (@$related_r) { if (not /included\sfrom/) { push @$out_r, $_; } } |
---|
255 | foreach (@$related_r) { if (/included\sfrom/) { push @$out_r, $_; } } |
---|
256 | } |
---|
257 | |
---|
258 | sub drop_last_pushed_relateds(\@) { |
---|
259 | my ($out_r) = @_; |
---|
260 | if ($last_pushed_related>0) { |
---|
261 | my $before_related = scalar(@$out_r) - $last_pushed_related - 1; |
---|
262 | $before_related>=0 || die "impossible (out_r-elements=".scalar(@$out_r)."; last_pushed_related=$last_pushed_related)"; |
---|
263 | @$out_r = @$out_r[0 .. $before_related]; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | sub included_from_here($) { |
---|
268 | my ($loc) = @_; |
---|
269 | return $loc.': included from here'; |
---|
270 | } |
---|
271 | |
---|
272 | sub suppress($\@$) { |
---|
273 | my ($curr,$out_r,$as) = @_; |
---|
274 | if ($show_suppressed_lines==1) { |
---|
275 | warning('suppressed['.$as.']: '.$curr,@$out_r); |
---|
276 | } |
---|
277 | return undef; |
---|
278 | } |
---|
279 | |
---|
280 | sub is_system_or_builtin($) { |
---|
281 | my ($file) = @_; |
---|
282 | return (($file =~ $reg_user_include) or ($file eq '<built-in>') or ($file =~ $reg_HEADERLIBS_include)); |
---|
283 | } |
---|
284 | |
---|
285 | sub suppress_shadow_warning_for($) { |
---|
286 | my ($file) = @_; |
---|
287 | return is_system_or_builtin($file); |
---|
288 | } |
---|
289 | |
---|
290 | sub compare_message_location($$) { |
---|
291 | my ($a,$b) = @_; |
---|
292 | # $a and $b are refs to array [filename location message] |
---|
293 | my $cmp = $$a[0] cmp $$b[0]; # compare filenames |
---|
294 | if ($cmp==0) { |
---|
295 | my ($la,$lb) = (undef,undef); |
---|
296 | if ($$a[1] =~ /^[0-9]+/o) { $la = $&; } |
---|
297 | if ($$b[1] =~ /^[0-9]+/o) { $lb = $&; } |
---|
298 | $cmp = $la <=> $lb; # compare line numbers |
---|
299 | } |
---|
300 | return $cmp; |
---|
301 | } |
---|
302 | sub compare_located_messages($$) { |
---|
303 | my ($a,$b) = @_; |
---|
304 | # $a and $b are refs to array [filename location message] |
---|
305 | my $cmp = compare_message_location($a,$b); |
---|
306 | if ($cmp==0) { $cmp = $$a[2] cmp $$b[2]; } # compare message |
---|
307 | return $cmp; |
---|
308 | } |
---|
309 | |
---|
310 | sub grep_loop_comments($\@) { |
---|
311 | my ($source,$hits_r) = @_; |
---|
312 | open(SRC,'<'.$source) || die "can't read '$source' (Reason: $!)"; |
---|
313 | my $line; |
---|
314 | my $linenr = 1; |
---|
315 | while (defined($line=<SRC>)) { |
---|
316 | if ($line =~ /(LOOP_VECTORIZED|UNCHECKED_LOOP)/o) { |
---|
317 | push @$hits_r, [ $source, $linenr, $&.$' ]; |
---|
318 | } |
---|
319 | $linenr++; |
---|
320 | } |
---|
321 | close(SRC); |
---|
322 | } |
---|
323 | |
---|
324 | sub parse_input(\@) { |
---|
325 | my ($out_r) = @_; |
---|
326 | my @related = (); |
---|
327 | my $location_info = undef; |
---|
328 | |
---|
329 | my @warnout = (); |
---|
330 | my @errout = (); |
---|
331 | my @loopnote = (); # contains notes about loop-optimizations (refs to array [file line msg]) |
---|
332 | |
---|
333 | my $did_show_previous = 0; |
---|
334 | my $is_error = 0; |
---|
335 | my $curr_out_r = \@warnout; |
---|
336 | |
---|
337 | LINE: while (defined($_=<STDIN>)) { |
---|
338 | chomp; |
---|
339 | my $filter_current = 0; |
---|
340 | |
---|
341 | if ($_ =~ $reg_file) { |
---|
342 | my ($file,$line,$msg) = ($1,$2,$'); |
---|
343 | |
---|
344 | if ($msg =~ $reg_is_warning) { |
---|
345 | my $warn_text = $'; |
---|
346 | if ($warn_text =~ $reg_shadow_warning) { |
---|
347 | if (not $' =~ /this/) { # don't store this warnings (no location follows) |
---|
348 | store_shadow($_,@warnout); |
---|
349 | $_ = suppress($_,@warnout, 'shadow-this'); |
---|
350 | } |
---|
351 | elsif (suppress_shadow_warning_for($file)) { |
---|
352 | $_ = suppress($_,@warnout, 'shadow'); |
---|
353 | # $location_info = undef; |
---|
354 | } |
---|
355 | } |
---|
356 | elsif ($warn_text =~ $reg_shadow_location) { |
---|
357 | if (not defined $shadow_warning) { warning('no shadow_warning seen',@warnout); } |
---|
358 | else { |
---|
359 | if (suppress_shadow_warning_for($file)) { |
---|
360 | # don't warn about /usr/include or <built-in> shadowing |
---|
361 | $_ = suppress($_,@warnout, 'shadowed'); |
---|
362 | @related = (); |
---|
363 | $location_info = undef; |
---|
364 | } |
---|
365 | else { |
---|
366 | if (defined $location_info) { |
---|
367 | push @warnout, $location_info; |
---|
368 | $location_info = undef; |
---|
369 | } |
---|
370 | push @warnout, $shadow_warning; |
---|
371 | } |
---|
372 | $shadow_warning = undef; |
---|
373 | } |
---|
374 | } |
---|
375 | else { |
---|
376 | my $warn_is = Weffpp_warning_wanted($file,$line,$warn_text); |
---|
377 | if ($warn_is == $UNWANTED) { |
---|
378 | $filter_current = 1; # ignore this warning |
---|
379 | } |
---|
380 | elsif ($warn_is == $WANTED_NO_RELATED) { |
---|
381 | @related = (); # drop related messages |
---|
382 | } |
---|
383 | # rebuild warning (Weffpp_warning_wanted might modify the message) |
---|
384 | $_ = $file.':'.$line.': warning: '.$warn_text; |
---|
385 | } |
---|
386 | $is_error = 0; |
---|
387 | $curr_out_r = \@warnout; |
---|
388 | } |
---|
389 | elsif ($msg =~ $reg_is_error) { |
---|
390 | $is_error = 1; |
---|
391 | $curr_out_r = \@errout; |
---|
392 | } |
---|
393 | elsif ($msg =~ $reg_is_instantiated) { |
---|
394 | push @related, $_; |
---|
395 | $_ = suppress($_,@warnout, 'instanciated'); |
---|
396 | } |
---|
397 | elsif ($msg =~ $reg_is_required) { |
---|
398 | push @related, $_; |
---|
399 | $_ = suppress($_,@warnout, 'required'); |
---|
400 | } |
---|
401 | elsif ($msg =~ $reg_is_note) { |
---|
402 | my $note_after = 1; # 0->message follows note; 1->note follows message |
---|
403 | |
---|
404 | my $note = $'; |
---|
405 | if ($note =~ /loop/o) { |
---|
406 | push @loopnote, [ $file, $line, $msg ]; |
---|
407 | $_ = suppress($_,@warnout, 'loop-note'); |
---|
408 | } |
---|
409 | else { |
---|
410 | if ($note_after==1) { |
---|
411 | if ($did_show_previous==0) { |
---|
412 | $_ = suppress($_,@warnout, 'note-of-nonshown'); |
---|
413 | } |
---|
414 | else { |
---|
415 | if ($msg =~ /in\sexpansion\sof\smacro/o) { |
---|
416 | drop_last_pushed_relateds(@$curr_out_r); |
---|
417 | } |
---|
418 | } |
---|
419 | } |
---|
420 | else { |
---|
421 | # note leads message -> store in related |
---|
422 | push @related, $_; |
---|
423 | $_ = suppress($_,@warnout, 'note'); |
---|
424 | } |
---|
425 | } |
---|
426 | } |
---|
427 | } |
---|
428 | elsif ($_ =~ $reg_location or $_ =~ $reg_location2 or $_ =~ $reg_location3) { |
---|
429 | $location_info = $_; |
---|
430 | $_ = suppress($_,@warnout, 'location'); |
---|
431 | } |
---|
432 | elsif ($_ =~ $reg_included) { |
---|
433 | push @related, included_from_here($1); |
---|
434 | $_ = suppress($_,@warnout, 'included'); |
---|
435 | } |
---|
436 | elsif ($_ =~ $reg_clang_dirt) { |
---|
437 | $_ = undef; |
---|
438 | } |
---|
439 | elsif ($_ =~ $reg_file_noline) { |
---|
440 | if (/^cc1plus:.*error/) { |
---|
441 | ; # display normally |
---|
442 | } |
---|
443 | else { |
---|
444 | # push @related, included_from_here($1); |
---|
445 | push @related, $_; |
---|
446 | $_ = suppress($_,@warnout, 'file-level-comment'); |
---|
447 | } |
---|
448 | } |
---|
449 | elsif (@related) { |
---|
450 | if ($_ =~ $reg_included2) { |
---|
451 | push @related, included_from_here($1); |
---|
452 | $_ = suppress($_,@warnout, 'included2'); |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|
456 | if (defined $_) { |
---|
457 | if ($filter_current==0) { |
---|
458 | if ($show_suppressed_lines==1) { warning('passing: '.$_, @warnout); } |
---|
459 | push_loc_and_related($location_info,$_,@related,@$curr_out_r); |
---|
460 | $did_show_previous = 1; |
---|
461 | |
---|
462 | if (($is_error==1) and ($stop_after_first_error==1)) { |
---|
463 | @warnout = (); # drop warnings |
---|
464 | last LINE; |
---|
465 | } |
---|
466 | } |
---|
467 | else { |
---|
468 | die "can't filter errors (Error=$_)" if ($is_error==1); |
---|
469 | if ($show_filtered_lines==1) { warning('filtered: '.$_, @$curr_out_r); } |
---|
470 | $did_show_previous = 0; |
---|
471 | } |
---|
472 | $location_info = undef; |
---|
473 | @related = (); |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | @$out_r = @errout; |
---|
478 | if ($hide_warnings==0) { push @$out_r, @warnout; } |
---|
479 | |
---|
480 | if ($dump_loop_optimization or $check_loop_optimization) { |
---|
481 | my @loopmsg = (); |
---|
482 | foreach my $vref (@loopnote) { |
---|
483 | my ($file,$line,$msg) = @$vref; |
---|
484 | my $boring = 0; |
---|
485 | if ($msg =~ /completely\sunrolled/o) { $boring = 1; } |
---|
486 | if (not $boring) { push @loopmsg, $vref; } |
---|
487 | } |
---|
488 | |
---|
489 | # sort messages and count + replace duplicates |
---|
490 | @loopmsg = sort { compare_located_messages($a,$b); } @loopmsg; |
---|
491 | { |
---|
492 | my @undup_loopmsg = (); |
---|
493 | my $seenDups = 0; |
---|
494 | push @loopmsg, [ 'x', 0, '' ]; # forces proper counter on last message |
---|
495 | foreach (@loopmsg) { |
---|
496 | my $prev = pop @undup_loopmsg; |
---|
497 | if (defined $prev) { |
---|
498 | if (compare_located_messages($prev,$_)!=0) { |
---|
499 | if ($seenDups>0) { $$prev[2] = $$prev[2].' ['.($seenDups+1).'x]'; } # append counter |
---|
500 | push @undup_loopmsg, $prev; |
---|
501 | $seenDups = 0; |
---|
502 | } |
---|
503 | else { $seenDups++; } |
---|
504 | } |
---|
505 | push @undup_loopmsg, $_; |
---|
506 | } |
---|
507 | pop @undup_loopmsg; |
---|
508 | @loopmsg = @undup_loopmsg; |
---|
509 | } |
---|
510 | |
---|
511 | if ($dump_loop_optimization) { |
---|
512 | if (scalar(@loopmsg)) { |
---|
513 | push @$out_r, "---------------------------------------- dump_loop_optimization"; |
---|
514 | foreach my $vref (@loopmsg) { |
---|
515 | my ($file,$line,$msg) = @$vref; |
---|
516 | if ($dump_loop_optimization==1 or $msg =~ /vectorized/o) { |
---|
517 | push @$out_r, "$file:$line: $msg"; |
---|
518 | } |
---|
519 | } |
---|
520 | push @$out_r, "---------------------------------------- dump_loop_optimization [end]"; |
---|
521 | } |
---|
522 | } |
---|
523 | if ($check_loop_optimization) { |
---|
524 | my @comments = (); |
---|
525 | grep_loop_comments($checked_file, @comments); |
---|
526 | |
---|
527 | my $errors = 0; |
---|
528 | foreach my $cref (@comments) { |
---|
529 | my $loc = $$cref[0].':'.$$cref[1]; |
---|
530 | my $cmsg = $$cref[2]; |
---|
531 | |
---|
532 | my $was_vectorized = 0; |
---|
533 | foreach my $vref (@loopmsg) { |
---|
534 | if (compare_message_location($cref,$vref)==0) { |
---|
535 | my $vmsg = $$vref[2]; |
---|
536 | if ($vmsg =~ /vectorized/o) { |
---|
537 | # push @$out_r, "$loc: Note: vectorized message='$vmsg'"; |
---|
538 | if ($vmsg =~ /\[([0-9]+)x\]/o) { # vectorized multiple times (e.g. in template) |
---|
539 | $was_vectorized = $1; |
---|
540 | } |
---|
541 | else { |
---|
542 | $was_vectorized = 1; |
---|
543 | } |
---|
544 | } |
---|
545 | } |
---|
546 | } |
---|
547 | if ($cmsg =~ /^LOOP_VECTORIZED/o) { |
---|
548 | my $rest = $'; |
---|
549 | if (not $was_vectorized) { |
---|
550 | push @$out_r, "$loc: Error: loop vectorization failed"; |
---|
551 | $errors++; |
---|
552 | } |
---|
553 | else { |
---|
554 | if ($rest =~ /^=([0-9]+|\*)/o) { |
---|
555 | my $expect_multiple = $1; |
---|
556 | if ($expect_multiple ne '*') { # allow any number of instantiations |
---|
557 | if ($expect_multiple!=$was_vectorized) { |
---|
558 | push @$out_r, "$loc: Error: vectorization count mismatch (specified=$expect_multiple, found=$was_vectorized)"; |
---|
559 | $errors++; |
---|
560 | } |
---|
561 | } |
---|
562 | } |
---|
563 | elsif ($was_vectorized>1) { |
---|
564 | push @$out_r, "$loc: Warning: loop gets vectorized $was_vectorized times"; |
---|
565 | push @$out_r, "$loc: Note: Comment with LOOP_VECTORIZED=$was_vectorized to force check of number of instantiations"; |
---|
566 | push @$out_r, "$loc: Note: Comment with LOOP_VECTORIZED=* to hide this warning"; |
---|
567 | } |
---|
568 | } |
---|
569 | } |
---|
570 | } |
---|
571 | |
---|
572 | foreach my $vref (@loopmsg) { |
---|
573 | my $vmsg = $$vref[2]; |
---|
574 | if ($vmsg =~ /vectorized/o) { |
---|
575 | my $loc = $$vref[0].':'.$$vref[1]; |
---|
576 | my $has_loop_comment = undef; |
---|
577 | foreach my $cref (@comments) { |
---|
578 | if (compare_message_location($cref,$vref)==0) { |
---|
579 | $has_loop_comment = $$cref[2]; |
---|
580 | } |
---|
581 | } |
---|
582 | if (not defined $has_loop_comment) { |
---|
583 | push @$out_r, "$loc: Warning: Unchecked optimized loop"; |
---|
584 | push @$out_r, "$loc: Note: Comment with LOOP_VECTORIZED to force check"; |
---|
585 | push @$out_r, "$loc: Note: Comment with UNCHECKED_LOOP to hide this warning"; |
---|
586 | } |
---|
587 | } |
---|
588 | } |
---|
589 | if ($errors) { $exitcode = 1; } |
---|
590 | } |
---|
591 | } |
---|
592 | } |
---|
593 | |
---|
594 | sub die_usage($) { |
---|
595 | my ($err) = @_; |
---|
596 | print "Usage: postcompile.pl [Options] sourcefile\n"; |
---|
597 | print "Used as compilation output filter for C/C++\n"; |
---|
598 | print "Options:\n"; |
---|
599 | print " --no-warnings hide warnings (plus related messages)\n"; |
---|
600 | print " --only-first-error show only first error\n"; |
---|
601 | print " --original pass-through\n"; |
---|
602 | print " --show-useless-Weff++ do not suppress useless -Weff++ warnings\n"; |
---|
603 | print " --hide-Noncopyable-advices do not advice about using Noncopyable\n"; |
---|
604 | print " --dump-loop-optimization dump (most) notes related to loop vectorization\n"; |
---|
605 | print " --loop-optimization-candi show candidates for loop vectorization-check\n"; |
---|
606 | print " --check-loop-optimization if sourcefile is listed in \$ARBHOME/SOURCE_TOOLS/vectorized.source,\n"; |
---|
607 | print " => check commented loops have been vectorized\n"; |
---|
608 | |
---|
609 | if (defined $err) { |
---|
610 | die "Error in postcompile.pl: $err"; |
---|
611 | } |
---|
612 | } |
---|
613 | |
---|
614 | sub main() { |
---|
615 | my $args = scalar(@ARGV); |
---|
616 | my $pass_through = 0; |
---|
617 | my $sourcefile = undef; |
---|
618 | while ($args>0) { |
---|
619 | my $arg = shift(@ARGV); |
---|
620 | if ($arg eq '--no-warnings') { $hide_warnings = 1; } |
---|
621 | elsif ($arg eq '--only-first-error') { $stop_after_first_error = 1; } |
---|
622 | elsif ($arg eq '--original') { $pass_through = 1; } |
---|
623 | elsif ($arg eq '--show-useless-Weff++') { $filter_Weffpp = 0; } |
---|
624 | elsif ($arg eq '--hide-Noncopyable-advices') { $filter_Weffpp_copyable = 1; } |
---|
625 | elsif ($arg eq '--dump-loop-optimization') { $dump_loop_optimization = 1; } |
---|
626 | elsif ($arg eq '--loop-optimization-candi') { $dump_loop_optimization = 2; } |
---|
627 | elsif ($arg eq '--check-loop-optimization') { $check_loop_optimization = 1; } |
---|
628 | elsif (not defined $sourcefile) { $sourcefile = $arg; } |
---|
629 | else { |
---|
630 | die_usage("Unknown argument '$arg'"); |
---|
631 | } |
---|
632 | $args--; |
---|
633 | } |
---|
634 | |
---|
635 | defined $sourcefile || die_usage("missing argument 'sourcefile'"); |
---|
636 | if (not -f $sourcefile) { die "Unknown sourcefile '$sourcefile'"; } |
---|
637 | |
---|
638 | eval { |
---|
639 | if ($pass_through==1) { |
---|
640 | while (defined($_=<STDIN>)) { print $_; } |
---|
641 | } |
---|
642 | else { |
---|
643 | my @out = (); |
---|
644 | detect_needLoopCheck($sourcefile); |
---|
645 | parse_input(@out); |
---|
646 | store_shadow(undef,@out); |
---|
647 | foreach (@out) { print "$_\n"; } |
---|
648 | } |
---|
649 | }; |
---|
650 | my $err = $@; |
---|
651 | save_from_NC(); |
---|
652 | if ($err) { die $err; } |
---|
653 | } |
---|
654 | main(); |
---|
655 | exit $exitcode; |
---|