-
Notifications
You must be signed in to change notification settings - Fork 836
/
Copy pathcover_test.pl
executable file
·697 lines (606 loc) · 16.6 KB
/
cover_test.pl
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#!/usr/bin/perl -w
use strict;
use FileHandle;
use Getopt::Std;
use IO::File;
use POSIX qw(strftime);
format HEADER =
cover_test.pl -f diff.log -d ./
Syntax: cover_test.pl -option value
-h - Give help screen
-f - diff file [required]
-d - directory [optional]
.
my $debug =0;
my ($diff, $directory,$log);
&do_opt();
if(! defined $diff){
&crit_record("pls input diff file");
}
if(!defined $directory){
$directory ="./";
}
if(defined $log){
print "pls check the output in [$directory/$log]\n";
my $fd =POSIX::open( "$directory/$log", &POSIX::O_CREAT | &POSIX::O_WRONLY, 0640);
POSIX::dup2($fd, 1);
POSIX::dup2($fd, 2);
}
&record("directory=[$directory]; diff=[$diff];");
#interpret the diff file
my($hd_diff) =&getFileHandle($diff,"r");
if (! defined $hd_diff){
&crit_record("diff [$diff] init fail, pls check it!");
}
&record("begin to interpret diff file!");
my $reg_diff = 'diff --git [ab]/(.+?)[\s\n]';
my $reg_add = '^\+(.*)';
my @reg_old = ('^-');
my @reg_del = ('^\+') ;
my @reg_file = ('\.h$','\.cc$','\.c$','\.ic$');
my $type_gcno = "gcno";
my $type_gcov = "gcov";
my $reg_cnt = '^@@.*?\d*,\d*.*?(\d*),(\d*) @@';
my $reg_gcov = '^#####:\s*(\d+):(.*)';
my $reg_hgcov_file = '\.h\.gcov';
my $reg_file_prefix = '(.*)\..*';
my $line;
my %src_file = ();
my ($ref_raw_line, $raw_line, $key);
my ($bcnt, $ecnt) =(0,0);
my $delta =-1;
my $current =0;
my $in_block =0;
while($line = $hd_diff->getline()){
# find the file
$ref_raw_line = &ppgrep($line, $reg_diff, 1);
if(defined $ref_raw_line){
$in_block = 0;
$delta = -1;
$raw_line = $ref_raw_line->[0];
$raw_line = "$directory/$raw_line";
&debug_record("diff file has: $raw_line");
my @add_lines = ();
$key = $raw_line;
$src_file{$key} = \@add_lines;
$bcnt = 0;
$ecnt = 0;
next;
}
$ref_raw_line = &ppgrep($line, $reg_cnt, 2);
if(defined $ref_raw_line){
$in_block = 1;
$delta = -1;
my $c1 = $ref_raw_line->[0];
my $c2 = $ref_raw_line->[1];
$bcnt = $c1;
$ecnt = $c1+$c2-1;
$current = $c1-1;
print("\tdiff file has: $key: begin: $bcnt end: $ecnt\n");
next;
}
if ($in_block == 0){
next;
}
my $is_old = &filterLine(\@reg_old, $line);
if ($is_old == 0){
next;
}
$current = $current+1;
#find the lines with head of '+'
$ref_raw_line = &ppgrep($line, $reg_add, 1);
if(defined $ref_raw_line){
my $new_ref_array = $src_file{$key};
&update_ecnt_by_bcnt($new_ref_array, $bcnt);
$raw_line = $ref_raw_line->[0];
$delta = $delta+1;
#del the line match the array:reg_del
#my $is_del = &filterLine(\@reg_del,$raw_line);
#if( $is_del == 0){
# next;
#}
#del the backspace
$raw_line = &delbackspace($raw_line);
if (! defined $raw_line){
next;
}
&debug_record("diff file lines: $raw_line");
my $tmp_ref_array = $src_file{$key};
my @st_line = ($raw_line,$bcnt, $bcnt+$delta);
push(@$tmp_ref_array, \@st_line);
next;
}
elsif (!defined $ref_raw_line)
{
$bcnt = $current+1;
$delta = -1;
}
}
#-------------------------------print the currect line scope---------------------------------
my $value;
my $repeat = -1;
print "\n";
&record("begin to amend diff file[amend the bcnt, ecnt]!");
while(($key, $value) = each %src_file){
$repeat = -1;
foreach my $ref_correct(@$value){
my ($line_correct, $bcnt_correct, $ecnt_correct) =@$ref_correct;
&debug_record("\tline:$line_correct|bcnt=$bcnt_correct|ecnt=$ecnt_correct\n");
if ($repeat == $bcnt_correct){
next;
}
else{
$repeat =$bcnt_correct;
print("\tdiff file has: $key: begin: $bcnt_correct end: $ecnt_correct\n");
}
}
}
print "\n";
#-------------------------------filter the not c/c++/h source file---------------------------------
my @filtered_file = ();
my @valid_file = ();
my $all_file_cnt = 0;
while(($key, $value) = each %src_file){
$all_file_cnt++;
# filter the file
my $ret = &filterLine(\@reg_file, $key);
if($ret == 0){
push(@valid_file, $key);
next;
}
else{
push(@filtered_file, $key);
}
}
my $novalid_cnt = scalar(@filtered_file);
my $valid_cnt = $all_file_cnt - $novalid_cnt;
&record("The diff log include : [$all_file_cnt\t] files");
&record("The valid source file: [$valid_cnt\t]; listing as below:\n");
my $add_lines_cnt;
foreach my $file(@valid_file){
$value = $src_file{$file};
$add_lines_cnt =scalar(@$value);
print "\t\t\t lines:[$add_lines_cnt\t];file:[$file]\n";
}
&record("The filtered file: [$novalid_cnt\t]; listing as below:\n");
foreach my $file(@filtered_file){
$value = $src_file{$file};
$add_lines_cnt =scalar(@$value);
print "\t\t\t lines:[$add_lines_cnt\t];file:[$file]\n";
}
#---------------------------------begin to check-----------------------------------------------------
&record("begin to check files:");
my ($file_name, $file_gcno,$file_name_gcno, $file_gcov, $file_name_gcov);
foreach my $file(@valid_file){
print("\nfile: $file-------------------------------------------------------\n");
if (-e $file){
#find the gcno file
#
$file_name = &getFileName($file);
#----------------debug
if( $debug and $file_name ne "mysqld.cc"){
#next;
}
#----------------debug
$file_name_gcno = &genTargetFileName($file_name, $type_gcno);
$file_gcno = &find_gcno($file_name_gcno,$directory);
&record("file=[$file];gcno=[$file_name_gcno]");
if(!defined $file_gcno or ! -e $file_gcno){
&warn_record("file [$file_name_gcno] not exists!");
$file_name_gcno = &genTargetFileNameFor51($file_name, $type_gcno);
$file_gcno = &find_gcno($file_name_gcno,$directory);
&record("file=[$file];gcno=[$file_name_gcno]");
if(!defined $file_gcno or ! -e $file_gcno){
&warn_record("file [$file_name_gcno] not exists!");
}
}
#gen the gcov file
$file_name_gcov = &genTargetFileName($file_name, $type_gcov);
$file_gcov = "$directory/$file_name_gcov";
&debug_record("file_gcov: $file_gcov");
my $ret = &gengcov($file, $file_gcno);
&record("file=[$file];gcov=[$file_gcov]");
if ($ret != 0){
&warn_record("file [$file_name_gcno--->$file_gcov] gen gcov error!");
#next;
}
if( ! -e $file_gcov){
&warn_record("file [$file_name_gcov] not exists, skip it!");
my $reg_h_file = &isgrep($file_gcov, $reg_hgcov_file);
if( $reg_h_file == 0){
next;
}else{
my $str1="##### file [$file_name_gcov] not exists, exit cover test!!";
&sp_warn_record($str1,"");
exit(1);
}
}
&record("......begin to scan gcov file [$file_gcov]......");
my $ref_diff_lines =$src_file{$file};
&compare($ref_diff_lines, $file_gcov);
}
else{
&warn_record("file [$file] not exists!");
next;
}
}
#------------------------------------------------------------
#correct the end line number
#------------------------------------------------------------
sub update_ecnt_by_bcnt(){
my($ref_array, $bcnt) = @_;
if(defined $ref_array){
foreach my $ref(@$ref_array){
my($tmp_line, $tmp_bcnt, $tmp_ecnt) =@$ref;
if ($tmp_bcnt == $bcnt){
@$ref =($tmp_line, $tmp_bcnt, $tmp_ecnt+1);
}
}
}
}
#------------------------------------------------------------
#find the file
#------------------------------------------------------------
sub find_gcno(){
my($file_name_gcno, $dir) =@_;
&debug_record("find_gcno: find $dir -name '$file_name_gcno'");
my @result = `find $dir -name '$file_name_gcno'`;
if(defined $result[0]){
chomp($result[0]);
return $result[0];
}
else{
return undef;
}
}
#----------------------------------------------------
#append the file type
#----------------------------------------------------
sub genTargetFileName(){
my($file_name, $type) = @_;
my $type_file = "$file_name.$type";
return $type_file;
}
sub genTargetFileNameFor51(){
my($file_name, $type) = @_;
my $ref_file_prefix = &ppgrep($file_name,$reg_file_prefix,1);
my $file_prefix = $ref_file_prefix->[0];
return "$file_prefix.$type";
}
#-----------------------------------------------------
#get the filename from string
#-----------------------------------------------------
sub getFileName(){
my($full_path) = @_;
my $file_name;
if($full_path =~ /.*\/(.*?)$/){
$file_name = $1;
}
else{
$file_name = $full_path;
}
return $file_name;
}
#------------------------------------------------------------------------
#
#------------------------------------------------------------------------
sub compare(){
my ($ref_diff_lines, $file_gcov) =@_;
my $hd_gcov = &getFileHandle($file_gcov, "r");
my $reg = "^#####:";
my $reg_no_cover = "\/\/no cover line";
my $reg_no_cover_begin = "\/\/no cover begin";
my $reg_no_cover_end = "\/\/no cover end";
my $no_cover_cnt = 0;
my $in_no_cover = 0;
my $bcnt;
my $ecnt;
my $sr_cnt;
my $cov_ret;
while(my $line = $hd_gcov->getline()){
my $parse_line = $line;
#&debug_record("the gcno lines: $parse_line");
#step 1: del backspace
$parse_line = &delbackspace($parse_line);
if( !defined $parse_line){
next;
}
# no cover line
$cov_ret = &isgrep($parse_line, $reg_no_cover);
if($cov_ret == 0){
$no_cover_cnt++;
next;
}
$cov_ret = &isgrep($parse_line, $reg_no_cover_begin);
if( $cov_ret == 0 and $in_no_cover == 0){
$no_cover_cnt++;
$in_no_cover=1;
next;
}
$cov_ret = &isgrep($parse_line, $reg_no_cover_end);
if ($cov_ret ==0 and $in_no_cover ==1){
$no_cover_cnt++;
$in_no_cover=0;
next;
}
if($in_no_cover == 1){
$no_cover_cnt++;
next;
}
#&debug_record("the gcno lines: $parse_line");
#step 2: grep #####
$parse_line = &ppgrep($parse_line, $reg_gcov, 2);
if( !defined $parse_line){
next;
}
$sr_cnt = $parse_line->[0];
$parse_line = $parse_line->[1];
#&debug_record("the gcno source lines: $line");
#step 3: del backspace
$parse_line = &delbackspace($parse_line);
if(!defined $parse_line){
next;
}
#&debug_record("the gcno delbak lines: $parse_line");
#step 4: del the no meaning line
my $ret = &ismeaning($parse_line);
if( $ret != 0){
next;
}
#&debug_record("the gcno is meaning lines: $parse_line");
#print "$line\n" ;
foreach my $diff_line(@$ref_diff_lines){
$ret = &isMatchStr($parse_line, $sr_cnt, $diff_line);
&debug_record("begin: $line end: $diff_line : result is $ret");
if($ret == 0){
print "\t\t\t$line";
last;
}
}
}
if($no_cover_cnt>0)
{
my $str2 ="";
my $str1 = "filter the no cover lines: $no_cover_cnt";
if($in_no_cover != 0){
$str2="##### crit: no cover comment is not pairing, pls check!";
}
&sp_warn_record($str1, $str2);
if($in_no_cover != 0){
exit(1);
}
}
}
#------------------------------------------------------------------
#judge it is meaning line
#------------------------------------------------------------------
sub ismeaning(){
my($str) = @_;
if( $str =~ /[0-9]|[a-z]|[A-Z]/){
return 0;
}
else{
return 1;
}
}
#------------------------------------------------------------------
#
#------------------------------------------------------------------
sub isMatchStr(){
my($parse_line, $sr_cnt, $diff_line) = @_;
my ($bcnt, $ecnt);
($diff_line, $bcnt, $ecnt) = @$diff_line;
my $ret = &isfullstr($parse_line, $diff_line);
if ($ret == 0){
if($bcnt != 0 or $ecnt != 0){
if( $sr_cnt >= $bcnt and $sr_cnt <= $ecnt){
return 0;
}
else{
return 1;
}
}
else{
return 0;
}
}
return 1;
}
#------------------------------------------------------------------
#
#------------------------------------------------------------------
sub issubstr(){
my($full, $str) = @_;
my $pos = index($full, $str);
if( defined $pos and $pos >= 0){
return 0;
}
return 1;
}
#------------------------------------------------------------------
#
#------------------------------------------------------------------
sub isfullstr(){
my($full, $str) = @_;
if ($full eq $str){
return 0;
}
return 1;
}
#------------------------------------------------------------------
#generate the gcov file
#------------------------------------------------------------------
sub gengcov(){
my ($file, $file_gcno) =@_;
if(!defined $file_gcno){
return 1;
}
my $ret = system("gcov $file -o $file_gcno >/dev/null ");
&debug_record("system gcov: gcov $file -o $file_gcno >/dev/null");
$ret = $ret>>8;
if($ret == 0){
return 0;
}
else{
return 1;
}
}
#-------------------------------------------------------
#replace the file type
#-------------------------------------------------------
sub replaceFileType(){
my ($str, $target) = @_;
my $newstr = $str;
if($str =~ /(.*)\..*?$/){
return "$1.$target";
}
return undef;
}
#-------------------------------------------------------
#truncate the backspace on the head and tail
#-------------------------------------------------------
sub delbackspace(){
my($str) =@_;
if( !defined $str or $str eq ""){
return undef;
}
$str =~s/^\s+//g;
$str =~s/\$\s+//g;
if ($str eq ""){
return undef;
}
return $str;
}
#------------------------------------------------------
#judge the str is in array
#------------------------------------------------------
sub isStrInArray(){
my (@array, $line) =@_;
foreach my $tmp(@array){
if($line eq $tmp){
return 0;
}
}
return 1;
}
#-------------------------------------------------------
#justify is success to grep the line according to regex
#-------------------------------------------------------
sub isgrep(){
my ($line, $reg) =@_;
if($line =~ /$reg/){
return 0;
}
else{
return 1;
}
}
#------------------------------------------------------
#grep the result
#------------------------------------------------------
sub ppgrep(){
my($line, $reg, $cnt) = @_;
my @ret = ();
if($line =~ /$reg/i){
if ($cnt >=1){
push(@ret, $1);
}
if ($cnt >=2){
push(@ret, $2);
}
if ($cnt >=3){
push(@ret, $3);
}
if ($cnt >=4){
return undef;
}
return \@ret;
}
else{
return undef;
}
}
#----------------------------------------------------------
#filter the lines according to reg array
#----------------------------------------------------------
sub filterLine(){
my($ref_reg, $line) = @_;
my @arr_filter = @$ref_reg;
foreach my $reg(@arr_filter){
my $ret =&isgrep($line,$reg);
if( $ret == 0){
return 0;
}
}
return 1;
}
#------------------------------------------
#record the information
#------------------------------------------
sub record(){
my($string) = @_;
my $current_time = strftime("%Y-%m-%d %H:%M:%S", localtime(time));
print("$current_time: info: $string\n");
}
#------------------------------------------
#record the crit
#------------------------------------------
sub crit_record(){
my($string) = @_;
my $current_time = strftime("%Y-%m-%d %H:%M:%S", localtime(time));
print("$current_time: crit: $string\n");
exit(1);
}
#------------------------------------------
#record the crit
#------------------------------------------
sub debug_record(){
my($string) = @_;
if( $debug ==1){
my $current_time = strftime("%Y-%m-%d %H:%M:%S", localtime(time));
print("$current_time: debug: $string\n");
}
}
#------------------------------------------
#record the warn
#------------------------------------------
sub warn_record(){
my($string) = @_;
my $current_time = strftime("%Y-%m-%d %H:%M:%S", localtime(time));
print("$current_time: warn: $string\n");
}
sub sp_warn_record(){
my($str1, $str2) = @_;
print("\t\t\t===================================================================\n");
print("\t\t\t= $str1\n");
print("\t\t\t= $str2\n");
print("\t\t\t===================================================================\n");
}
#---------------------------------------------------------------------
#init the file handle
#---------------------------------------------------------------------
sub getFileHandle{
my($file, $mode)=@_;
my ($hd);
$hd = new FileHandle($file,"$mode");
return $hd;
}
#---------------------------------------------------------------------------------
#split the input parameters
#---------------------------------------------------------------------------------
sub do_opt{
use vars '$opt_h','$opt_H','$opt_f','$opt_d','$opt_l';
my $returncode = getopts('hH:f:d:l:');
&do_help() if(defined $opt_h or defined $opt_H or $returncode!=1);
$diff = $opt_f if(defined $opt_f);
$directory = $opt_d if(defined $opt_d);
$log = $opt_l if(defined $opt_l);
}
#----------------------------------------------------------------------------------
#give help information and exit;
#---------------------------------------------------------------------------------
sub do_help{
$~ = "HEADER";
write;
exit(1);
}