1
1

- This version canhandle command line parsing

- If -d is specified, it assumes that the next parameter is 
      a valid subdirectory path.
    - If -f is specified, then it generates the statistic for tehse
      files. Each -f is pared with one file only. So multiple files require 
      multiple -f's
    - -p "%" can be given to spew out the files which have a coverage
      below this percentage

This commit was SVN r5426.
Этот коммит содержится в:
Prabhanjan Kambadur 2005-04-19 00:35:59 +00:00
родитель c094c02310
Коммит 213da2abd0

Просмотреть файл

@ -24,143 +24,222 @@
#
use strict;
use Switch;
my $num_arguments = scalar(@ARGV);
my $percentage;
#global variables
my $index = 0;
my $num_args = scalar(@ARGV);
my $percentage = 0.0;
my $ret1 = 0;
my $ret2 = 0;
my $dir_list_given = 0;
my $req_list_given = 0;
if ($num_arguments == 0) {
print "WARN: All files with coverage of 0% will be reported.\n Please use -p percentage to specify something else\n";
$percentage = 0.00;
$ret1 = open(DIR_FILE, "> dir_list.txt");
$ret2 = open(REQ_FILE, "> req_list.txt");
if ($ret1 < 0 || $ret2 < 0) {
print "ERROR: opening dir_list.txt or req_list.txt\n";
exit(3);
}
# check the error condition
if (0 == ($num_args % 2)) {
} else {
if ($num_arguments == 2) {
$percentage = $ARGV[1];
} else {
print "ERROR:Please enter the number below which you want to list all the files\n";
print "ERROR: the number of arguments has to be even\n";
exit(3);
}
# process the arguments
while($num_args > 0) {
switch ($ARGV[$index]) {
case "-d" {
print DIR_FILE $ARGV[$index+1];
$index += 2;
$num_args -= 2;
$dir_list_given = 1;
}
case "-f" {
my $filename = `find . -name $ARGV[$index+1]`;
print REQ_FILE $filename;
$index += 2;
$num_args -= 2;
$req_list_given = 1;
}
case "-p" {
$percentage = $ARGV[$index];
$index += 2;
$num_args -= 2;
}
else {
print "ERROR: Incorrect command line option\n";
exit(3);
}
}
}
close(DIR_FILE);
close(REQ_FILE);
# when nothing is specified, everything defaults to just "src"
if (0 == $dir_list_given && 0 == $req_list_given) {
print "WARN: No directory or file has been requested\ndefaulting to src directory\n";
system("echo src > dir_list.txt");
$dir_list_given = 1;
}
#Now, we have both the requested file list and the directory list
#So simply call the procedure generate_stats with the required arguments
if (1 == $dir_list_given) {
get_file_list("./dir_list.txt",
"touched_files.txt",
"untouched_files.txt");
generate_stats("touched_files.txt", # file_list
"coverage_stats.txt",# generic coverage numbers
"percent_stats.txt", # files below a certain %
$percentage, # percentage below which we report
1); # 1 to report
}
if (1 == $req_list_given) {
generate_stats("req_list.txt", # file_list
"req_stats.txt",# generic coverage
"", # no percentage coverage required
0, # not required
0); # 0 to not report percentages
}
sub get_file_list {
my ($input, $touched, $untouched) = @_;
my $ret = open(DIRFILES, "< $input");
if ($ret < 0) {
print "ERROR: could not open directory listing\n";
exit(3);
}
}
system("rm -f coverage_stats.txt zero_coverage.txt touched_files.txt untouched_files.txt");
open (COVERAGE_STATS, "> coverage_stats.txt");
open (ZERO_COVERAGE, "> percent_coverage.txt");
print COVERAGE_STATS "#Index Filename Directory Usage(%)\n";
print COVERAGE_STATS "#======================================================================================================\n";
print ZERO_COVERAGE "#Index Filename Directory Usage(%)\n";
print ZERO_COVERAGE "#======================================================================================================\n";
close(COVERAGE_STATS);
close(ZERO_COVERAGE);
my $k = 0;
my $l = 0;
# Get the list of files for which stats are required
my $ret = open(DIRFILES, "< ./dir_list");
if ($ret < 0) {
print "ERROR: could not open directory listing\n";
exit(3);
}
while(<DIRFILES>) {
chomp();
my $c_files = `find $_ -name \"*.c\"`;
my $cc_files = `find $_ -name \"*.cc\"`;
$cc_files =~ s/\.cc//g;
$c_files =~ s/\.c//g;
$c_files = $c_files . $cc_files;
my @C_FILES = split(/\n/, $c_files);
my $da_files = `find $_ -name \"*.da\"`;
$da_files =~ s/\.da//g;
my @DA_FILES = split(/\n/, $da_files);
open (TEMP1, "> temp1");
open (TEMP2, "> temp2");
print TEMP1 $c_files;
print TEMP2 $da_files;
close(TEMP1);
close(TEMP2);
# Now do the manual diff
open(TEMP1, "< temp1");
open(UNTOUCHED_FILES, ">> untouched_files.txt");
open(TOUCHED_FILES, ">> touched_files.txt");
while(<TEMP1>) {
my $c_file = $_;
my $found = 0;
open(TEMP2, "< temp2");
while(<TEMP2>) {
if ($c_file eq $_) {
$found = 1;
}
}
while(<DIRFILES>) {
chomp();
my $c_files = `find $_ -name \"*.c\"`;
my $cc_files = `find $_ -name \"*.cc\"`;
$cc_files =~ s/\.cc//g;
$c_files =~ s/\.c//g;
$c_files = $c_files . $cc_files;
my @C_FILES = split(/\n/, $c_files);
my $da_files = `find $_ -name \"*.da\"`;
$da_files =~ s/\.da//g;
my @DA_FILES = split(/\n/, $da_files);
open (TEMP1, "> temp1");
open (TEMP2, "> temp2");
print TEMP1 $c_files;
print TEMP2 $da_files;
close(TEMP1);
close(TEMP2);
if ($found == 0) {
print UNTOUCHED_FILES $c_file;
} else {
chomp($c_file);
$c_file = $c_file . ".c\n";
print TOUCHED_FILES $c_file;
}
}
close(UNTOUCHED_FILES);
close(TOUCHED_FILES);
close(TEMP1);
system("rm temp1 temp2");
}
close(DIRFILES);
#Now to print the stats of all the files which are touched
system("sort touched_files.txt -o temp; uniq temp touched_files.txt");
system("sort untouched_files.txt -o temp; uniq temp untouched_files.txt");
open (TOUCHED_FILES, "< touched_files.txt");
open (COVERAGE_STATS, ">> coverage_stats.txt");
open (ZERO_COVERAGE, ">> percent_coverage.txt");
my $average = 0.0;
my $num_files = `wc -l touched_files.txt`;
while (<TOUCHED_FILES>) {
#generate the gcov file for this particular file
#1. Get the directory name and filename seperately
#2. Invoke gcov on the file
#3. Print the statistic onto a file
chomp();
my $full_name = $_;
my $file_name = `basename $full_name`;
my $dir_name = `dirname $full_name`;
chomp($dir_name);
chomp($file_name);
open(RESULT, "cd $dir_name; gcov $file_name 2> /dev/null |");
while (<RESULT>) {
if (/Creating/) {}
else {
#Now we are doing the right line. Search for this file
if (/$file_name/) {
s/^([0-9]+\.[0-9]+\%)\.*/$1/;
my $val = $1;
$average += $val;
$k++;
my $print_string = sprintf("%4d %40s %40s %3.2f\n", $k, $file_name, $dir_name, $val);
if ($val <= $percentage) {
$l++;
my $zero_string = sprintf("%4d %40s %40s %3.2f\n", $l, $file_name, $dir_name, $val);
print ZERO_COVERAGE $zero_string;
# Now do the manual diff
open(TEMP1, "< temp1");
open(UNTOUCHED_FILES, ">> $touched");
open(TOUCHED_FILES, ">> $untouched");
while(<TEMP1>) {
my $c_file = $_;
my $found = 0;
open(TEMP2, "< temp2");
while(<TEMP2>) {
if ($c_file eq $_) {
$found = 1;
}
print COVERAGE_STATS $print_string;
}
}
close(TEMP2);
if ($found == 0) {
print UNTOUCHED_FILES $c_file;
} else {
chomp($c_file);
$c_file = $c_file . ".c\n";
print TOUCHED_FILES $c_file;
}
}
close(UNTOUCHED_FILES);
close(TOUCHED_FILES);
close(TEMP1);
system("rm temp1 temp2");
}
close(DIRFILES);
#Now to print the stats of all the files which are touched
system("sort $touched -o temp; uniq temp $touched");
system("sort $untouched -o temp; uniq temp $untouched");
}
# This is the function which generates the statistics and dumps it out
# to a file. Details are pretty straightforward at this point
sub generate_stats {
my ($input_file, $coverage_file, $percent_file, $percentage, $calculate) = @_;
my $k = 0;
my $l = 0;
open (INPUT, "< $input_file");
open (COVERAGE, "> $coverage_file");
if ($calculate == 1) {
open (PERCENT, "> percent_coverage.txt");
}
print COVERAGE "#Index Filename Directory Usage(%)\n";
print COVERAGE "#======================================================================================================\n";
if ($calculate == 1) {
print PERCENT "#Index Filename Directory Usage(%)\n";
print PERCENT "#======================================================================================================\n";
}
my $average = 0.0;
my $num_files = `wc -l touched_files.txt`;
while (<INPUT>) {
#generate the gcov file for this particular file
#1. Get the directory name and filename seperately
#2. Invoke gcov on the file
#3. Print the statistic onto a file
chomp();
my $full_name = $_;
my $file_name = `basename $full_name`;
my $dir_name = `dirname $full_name`;
chomp($dir_name);
chomp($file_name);
open(RESULT, "cd $dir_name; gcov $file_name 2> /dev/null |");
while (<RESULT>) {
if (/Creating/) {}
else {
#Now we are doing the right line. Search for this file
if (/$file_name/) {
s/^([0-9]+\.[0-9]+\%)\.*/$1/;
my $val = $1;
$average += $val;
$k++;
my $print_string = sprintf("%4d %40s %40s %3.2f\n", $k, $file_name, $dir_name, $val);
if ($calculate == 1) {
if ($val <= $percentage) {
$l++;
my $zero_string = sprintf("%4d %40s %40s %3.2f\n", $l, $file_name, $dir_name, $val);
print PERCENT $zero_string;
}
}
print COVERAGE $print_string;
}
}
}
close(RESULT);
}
print COVERAGE "==============================================================\n";
print COVERAGE "Average coverage was: ", $average/$num_files, " \n";
print COVERAGE "==============================================================\n";
close(INPUT);
close(COVERAGE);
if ($calculate == 1) {
close(PERCENT);
}
close(RESULT);
}
print COVERAGE_STATS "==============================================================\n";
print COVERAGE_STATS "Average coverage was: ", $average/$num_files, " \n";
print COVERAGE_STATS "==============================================================\n";
close(TOUCHED_FILES);
close(COVERAGE_STATS);
close(ZERO_COVERAGE);