1cdf0e10cSrcweir#!/usr/bin/perl -w
2*bb113e63SAndrew Rist# *************************************************************
3*bb113e63SAndrew Rist#
4*bb113e63SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
5*bb113e63SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
6*bb113e63SAndrew Rist#  distributed with this work for additional information
7*bb113e63SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
8*bb113e63SAndrew Rist#  to you under the Apache License, Version 2.0 (the
9*bb113e63SAndrew Rist#  "License"); you may not use this file except in compliance
10*bb113e63SAndrew Rist#  with the License.  You may obtain a copy of the License at
11*bb113e63SAndrew Rist#
12*bb113e63SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
13*bb113e63SAndrew Rist#
14*bb113e63SAndrew Rist#  Unless required by applicable law or agreed to in writing,
15*bb113e63SAndrew Rist#  software distributed under the License is distributed on an
16*bb113e63SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17*bb113e63SAndrew Rist#  KIND, either express or implied.  See the License for the
18*bb113e63SAndrew Rist#  specific language governing permissions and limitations
19*bb113e63SAndrew Rist#  under the License.
20*bb113e63SAndrew Rist#
21*bb113e63SAndrew Rist# *************************************************************
22cdf0e10cSrcweir#
23cdf0e10cSrcweir# $Id: gcov_resultinterpreter.pl,v 1.3 2005-11-02 17:24:12 kz Exp $
24cdf0e10cSrcweir#
25cdf0e10cSrcweir
26cdf0e10cSrcweir# GCOV_RESULTINTERPRETER
27cdf0e10cSrcweir#
28cdf0e10cSrcweir# Helper, to interpret the result
29cdf0e10cSrcweir#
30cdf0e10cSrcweir# Q: Why perl?
31cdf0e10cSrcweir# A: regexp ;-)
32cdf0e10cSrcweir#
33cdf0e10cSrcweir
34cdf0e10cSrcweiruse strict;
35cdf0e10cSrcweiruse File::Basename;
36cdf0e10cSrcweiruse Getopt::Long;
37cdf0e10cSrcweir
38cdf0e10cSrcweirour $version_info = 'gcov helper $Revision: 1.3 $ ';
39cdf0e10cSrcweir
40cdf0e10cSrcweirour $help;                    # Help option flag
41cdf0e10cSrcweirour $version;                 # Version option flag
42cdf0e10cSrcweir# our $infile;
43cdf0e10cSrcweir
44cdf0e10cSrcweirour $usedFunctions;     # show all functions, which have a value > 0
45cdf0e10cSrcweirour $nonusedFunctions;  # show all functions, which have a value == 0
46cdf0e10cSrcweirour $nPercent;          # show all functions, which have a value > $nPercent
47cdf0e10cSrcweirour $complete;          # show all functions, which have a value == 100
48cdf0e10cSrcweirour $incomplete;       # show all functions, which have a value > 0 && < 100
49cdf0e10cSrcweir
50cdf0e10cSrcweir# Prototypes
51cdf0e10cSrcweirsub print_usage(*);
52cdf0e10cSrcweirsub read_gcov_function_file($);
53cdf0e10cSrcweir
54cdf0e10cSrcweir# Parse command line options
55cdf0e10cSrcweirif (!GetOptions(
56cdf0e10cSrcweir                "usedfunctions" => \$usedFunctions,
57cdf0e10cSrcweir                "nonusedfunctions" => \$nonusedFunctions,
58cdf0e10cSrcweir                "percent=s" => \$nPercent,
59cdf0e10cSrcweir                "complete" => \$complete,
60cdf0e10cSrcweir                "incomplete" => \$incomplete,
61cdf0e10cSrcweir                "help"   => \$help,
62cdf0e10cSrcweir                "version" => \$version
63cdf0e10cSrcweir                ))
64cdf0e10cSrcweir{
65cdf0e10cSrcweir    print_usage(*STDERR);
66cdf0e10cSrcweir    exit(1);
67cdf0e10cSrcweir}
68cdf0e10cSrcweir
69cdf0e10cSrcweir# Check for help option
70cdf0e10cSrcweirif ($help)
71cdf0e10cSrcweir{
72cdf0e10cSrcweir    print_usage(*STDOUT);
73cdf0e10cSrcweir    exit(0);
74cdf0e10cSrcweir}
75cdf0e10cSrcweir
76cdf0e10cSrcweir# Check for version option
77cdf0e10cSrcweirif ($version)
78cdf0e10cSrcweir{
79cdf0e10cSrcweir    print("$version_info\n");
80cdf0e10cSrcweir    exit(0);
81cdf0e10cSrcweir}
82cdf0e10cSrcweir
83cdf0e10cSrcweir# check if enough parameters
84cdf0e10cSrcweirif ($#ARGV < 0)
85cdf0e10cSrcweir{
86cdf0e10cSrcweir    print("No input filename specified\n");
87cdf0e10cSrcweir    print_usage(*STDERR);
88cdf0e10cSrcweir    exit(1);
89cdf0e10cSrcweir}
90cdf0e10cSrcweir
91cdf0e10cSrcweirif ($complete)
92cdf0e10cSrcweir{
93cdf0e10cSrcweir    $nPercent = 100.00;
94cdf0e10cSrcweir}
95cdf0e10cSrcweir# ------------------------------------------------------------------------------
96cdf0e10cSrcweir
97cdf0e10cSrcweirmy %list = read_gcov_function_file($ARGV[0]);
98cdf0e10cSrcweir
99cdf0e10cSrcweirmy $key;
100cdf0e10cSrcweirmy $value;
101cdf0e10cSrcweir
102cdf0e10cSrcweirwhile (($key, $value) = each %list)
103cdf0e10cSrcweir{
104cdf0e10cSrcweir    # print "function: $key = $value\n";
105cdf0e10cSrcweir    if ($nonusedFunctions)
106cdf0e10cSrcweir    {
107cdf0e10cSrcweir        if ($value <= 0.00)
108cdf0e10cSrcweir        {
109cdf0e10cSrcweir            print "$key\n";
110cdf0e10cSrcweir        }
111cdf0e10cSrcweir    }
112cdf0e10cSrcweir    elsif ($usedFunctions)
113cdf0e10cSrcweir    {
114cdf0e10cSrcweir        if ($value != 0.00)
115cdf0e10cSrcweir        {
116cdf0e10cSrcweir            print "$key, $value\n";
117cdf0e10cSrcweir        }
118cdf0e10cSrcweir    }
119cdf0e10cSrcweir    elsif ($nPercent)
120cdf0e10cSrcweir    {
121cdf0e10cSrcweir        if ($value >= $nPercent)
122cdf0e10cSrcweir        {
123cdf0e10cSrcweir            print "$key, $value\n";
124cdf0e10cSrcweir        }
125cdf0e10cSrcweir    }
126cdf0e10cSrcweir    elsif ($incomplete)
127cdf0e10cSrcweir    {
128cdf0e10cSrcweir        if ($value > 0.00 && $value < 100.00)
129cdf0e10cSrcweir        {
130cdf0e10cSrcweir            print "$key, $value\n";
131cdf0e10cSrcweir        }
132cdf0e10cSrcweir    }
133cdf0e10cSrcweir    else
134cdf0e10cSrcweir    {
135cdf0e10cSrcweir        print "$key, $value\n";
136cdf0e10cSrcweir    }
137cdf0e10cSrcweir}
138cdf0e10cSrcweir
139cdf0e10cSrcweir# --------------------------------------------------------------------------------
140cdf0e10cSrcweir# Read the gcov function (gcov -f) file
141cdf0e10cSrcweir# and compare line by line with the export function list
142cdf0e10cSrcweir# so we get a list of functions, which are only exported, and not all stuff.
143cdf0e10cSrcweir
144cdf0e10cSrcweirsub read_gcov_function_file($)
145cdf0e10cSrcweir{
146cdf0e10cSrcweir    local *INPUT_HANDLE;
147cdf0e10cSrcweir    my $file = $_[0];
148cdf0e10cSrcweir    my %list;
149cdf0e10cSrcweir    my $line = "";
150cdf0e10cSrcweir
151cdf0e10cSrcweir    open(INPUT_HANDLE, $file)
152cdf0e10cSrcweir        or die("ERROR: cannot open $file!\n");
153cdf0e10cSrcweir
154cdf0e10cSrcweir    while ($line = <INPUT_HANDLE>)
155cdf0e10cSrcweir    {
156cdf0e10cSrcweir        chomp($line);
157cdf0e10cSrcweir        # sample line (for reg exp:)
158cdf0e10cSrcweir        # 100.00 rtl_ustr_toDouble
159cdf0e10cSrcweir        if ($line =~ /^(.*) (\w+)$/ )
160cdf0e10cSrcweir        {
161cdf0e10cSrcweir            my $percent = $1;
162cdf0e10cSrcweir            my $value = $2;
163cdf0e10cSrcweir
164cdf0e10cSrcweir            $list{$value} = $percent;
165cdf0e10cSrcweir        }
166cdf0e10cSrcweir    }
167cdf0e10cSrcweir    close(INPUT_HANDLE);
168cdf0e10cSrcweir    return %list;
169cdf0e10cSrcweir}
170cdf0e10cSrcweir
171cdf0e10cSrcweir# ----------------------------------------------------------------------------
172cdf0e10cSrcweirsub print_usage(*)
173cdf0e10cSrcweir{
174cdf0e10cSrcweir    local *HANDLE = $_[0];
175cdf0e10cSrcweir    my $tool_name = basename($0);
176cdf0e10cSrcweir
177cdf0e10cSrcweir    print(HANDLE <<END_OF_USAGE);
178cdf0e10cSrcweir
179cdf0e10cSrcweirUsage: $tool_name [OPTIONS] INPUTFILE
180cdf0e10cSrcweir
181cdf0e10cSrcweir    -u, --usedFunctions     show all functions, which have a value > 0
182cdf0e10cSrcweir    -n, --nonusedFunctions  show all functions, which have a value == 0
183cdf0e10cSrcweir    -p, --percent           show all functions, which have a value > percent
184cdf0e10cSrcweir    -c, --complete          show all functions, which have a value == 100
185cdf0e10cSrcweir    -i, --incomplete        show all functions, which have a value > 0 && < 100
186cdf0e10cSrcweir
187cdf0e10cSrcweir    -h, --help              Print this help, then exit
188cdf0e10cSrcweir    -v, --version           Print version number, then exit
189cdf0e10cSrcweir
190cdf0e10cSrcweirEND_OF_USAGE
191cdf0e10cSrcweir    ;
192cdf0e10cSrcweir}
193