1 # Copyright (C) 1997, 2001 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 # For now find gcov in the same directory as $GCC_UNDER_TEST.
25 if { ![is_remote host] && [string match "*/*" [lindex $GCC_UNDER_TEST 0]] } {
26 set GCOV [file dirname [lindex $GCC_UNDER_TEST 0]]/gcov
31 # Proc to delete the working files created by the compiler for gcov.
33 proc clean-gcov { testcase } {
34 set basename [file tail $testcase]
35 set base [file rootname $basename]
36 remote_file host delete $base.bb $base.bbg $base.da $basename.gcov
40 # verify-lines -- check that line counts are as expected
42 # TESTCASE is the name of the test.
43 # FILE is the name of the gcov output file.
45 proc verify-lines { testcase file } {
49 set output [grep $file ".*count\\(\[0-9\]+\\)" line]
50 #send_user "output:$output\n"
51 foreach line $output {
52 verbose "Processing count line: $line" 3
53 #send_user "line:$line\n"
54 if [regexp "(\[0-9\]+) *(\[0-9\]+).*count\\((\[0-9\]+)\\)" "$line" all n is shouldbe] {
55 #send_user "n $n:is $is:shouldbe $shouldbe\n"
58 set lmessage "$n:no data available for this line"
61 } elseif { $is != $shouldbe } {
63 set lmessage "$n:is $is:should be $shouldbe"
69 set lmessage "can't parse $line (in wrong place?)"
74 return [list $failed $lmessage]
78 # verify-branches -- check that branch percentages are as expected
80 # TESTCASE is the name of the test.
81 # FILE is the name of the gcov output file.
83 # Checks are based on comments in the source file. This means to look for
84 # branch percentages 10 or 90, 20 or 80, and # 70 or 30:
85 # /* branch(10, 20, 70) */
86 # This means that all specified percentages should have been seen by now:
88 # All specified percentages must also be seen by the next branch(n) or
89 # by the end of the file.
91 # Each check depends on the compiler having generated the expected
92 # branch instructions. Don't check for branches that might be
93 # optimized away or replaced with predicated instructions.
95 proc verify-branches { testcase file } {
101 set fd [open $file r]
102 while { [gets $fd line] >= 0 } {
103 if [regexp "branch" $line] {
104 verbose "Processing branch line: $line" 3
105 if [regexp "branch\\((\[0-9 \]+)\\)" "$line" all new_shouldbe] {
106 # All percentages in the current list should have been seen.
107 if {[llength $shouldbe] != 0} {
108 if { $failed == 0 } {
109 set bmessage "expected branch percentages not found: $shouldbe"
114 set shouldbe $new_shouldbe
115 # Record the percentages to check for. Replace percentage
116 # n > 50 with 100-n, since block ordering affects the
117 # direction of a branch.
118 for {set i 0} {$i < [llength $shouldbe]} {incr i} {
119 set num [lindex $shouldbe $i]
121 set shouldbe [lreplace $shouldbe $i $i [expr 100 - $num]]
124 } elseif [regexp "branch \[0-9\]+ taken = (-\[0-9\]+)%" "$line" all taken] {
125 # Percentages should never be negative.
126 if { $failed == 0 } {
127 set bmessage "negative percentage: $taken"
130 } elseif [regexp "branch \[0-9\]+ taken = (\[0-9\]+)%" "$line" all taken] {
131 # Percentages should never be greater than 100.
133 if { $failed == 0 } {
134 set bmessage "percentage greater than 100: $taken"
139 set taken [expr 100 - $taken]
141 # If this percentage is one to check for then remove it
142 # from the list. It's normal to ignore some reports.
143 set i [lsearch $shouldbe $taken]
145 set shouldbe [lreplace $shouldbe $i $i]
147 } elseif [regexp "branch\\(end\\)" "$line"] {
148 # All percentages in the list should have been seen by now.
149 if {[llength $shouldbe] != 0} {
150 if { $failed == 0 } {
151 set bmessage "expected branch percentages not found: $shouldbe"
159 # All percentages in the list should have been seen.
160 if {[llength $shouldbe] != 0} {
161 if { $failed == 0 } {
162 set bmessage "expected branch percentages not found: $shouldbe"
167 return [list $failed $bmessage]
171 # verify-calls -- check that call return percentages are as expected
173 # TESTCASE is the name of the test.
174 # FILE is the name of the gcov output file.
176 # Checks are based on comments in the source file. This means to look for
177 # call return percentages 50, 20, 33:
178 # /* returns(50, 20, 33) */
179 # This means that all specified percentages should have been seen by now:
181 # All specified percentages must also be seen by the next returns(n) or
182 # by the end of the file.
184 # Each check depends on the compiler having generated the expected
185 # call instructions. Don't check for calls that are inserted by the
186 # compiler or that might be inlined.
188 proc verify-calls { testcase file } {
194 set fd [open $file r]
195 while { [gets $fd line] >= 0 } {
196 if [regexp "returns" $line] {
197 verbose "Processing returns line: $line" 3
198 if [regexp "returns\\((\[0-9 \]+)\\)" "$line" all new_shouldbe] {
199 # All percentages in the current list should have been seen.
200 if {[llength $shouldbe] != 0} {
201 if { $failed == 0 } {
202 set cmessage "expected return percentages not found: $shouldbe"
207 # Record the percentages to check for.
208 set shouldbe $new_shouldbe
209 } elseif [regexp "call \[0-9\]+ returns = (-\[0-9\]+)%" "$line" all returns] {
210 # Percentages should never be negative.
211 if { $failed == 0 } {
212 set cmessage "negative percentage: $returns"
215 } elseif [regexp "call \[0-9\]+ returns = (\[0-9\]+)%" "$line" all returns] {
216 # For branches we check that percentages are not greater than
217 # 100 but call return percentages can be, as for setjmp(), so
218 # don't count that as an error.
220 # If this percentage is one to check for then remove it
221 # from the list. It's normal to ignore some reports.
222 set i [lsearch $shouldbe $returns]
224 set shouldbe [lreplace $shouldbe $i $i]
226 } elseif [regexp "returns\\(end\\)" "$line"] {
227 # All percentages in the list should have been seen by now.
228 if {[llength $shouldbe] != 0} {
229 if { $failed == 0 } {
230 set cmessage "expected return percentages not found: $shouldbe"
238 # All percentages in the list should have been seen.
239 if {[llength $shouldbe] != 0} {
240 if { $failed == 0 } {
241 set cmessage "expected return percentages not found: $shouldbe"
246 return [list $failed $cmessage]
249 # Called by dg-final to run gcov and analyze the results.
251 # ARGS is the options to pass to gcov followed by the name of the
254 proc run-gcov { args } {
258 # Extract the test name from the arguments.
259 set testcase [lindex $args end]
261 verbose "Running $GCOV $testcase" 2
262 set testcase [remote_download host $testcase];
263 set result [remote_exec host $GCOV $args];
264 if { [lindex $result 0] != 0 } {
265 fail "$subdir/$testcase gcov failed: [lindex $result 1]"
270 # Get the gcov output file after making sure it exists.
271 set files [glob -nocomplain $testcase.gcov]
272 if { $files == "" } {
273 fail "$subdir/$testcase gcov failed: $testcase.gcov does not exist"
277 remote_upload host $testcase.gcov $testcase.gcov;
279 # Check that line execution counts are as expected.
280 set loutput [verify-lines $testcase $testcase.gcov]
281 set lfailed [lindex $loutput 0]
282 set lmessage [lindex $loutput 1]
284 # If we asked for branch and call information, check that it is correct.
285 if [regexp -- "-b" $args] {
286 set boutput [verify-branches $testcase $testcase.gcov]
287 set bfailed [lindex $boutput 0]
288 set bmessage [lindex $boutput 1]
289 set coutput [verify-calls $testcase $testcase.gcov]
290 set cfailed [lindex $coutput 0]
291 set cmessage [lindex $coutput 1]
301 # Report whether the gcov test passed or failed. If there were
302 # multiple failures then the message is a summary.
303 set tfailed [expr $lfailed + [expr $bfailed + $cfailed]]
304 if { $tfailed > 0 } {
305 if { $tfailed == 1 } {
306 set vmessage "$lmessage$bmessage$cmessage"
307 } elseif { $bfailed == 0 && $cfailed == 0 } {
308 set vmessage "$lfailed failures in line counts"
309 } elseif { $lfailed == 0 && $cfailed == 0 } {
310 set vmessage "$bfailed failures in branch percentages"
311 } elseif { $lfailed == 0 && $bfailed == 0 } {
312 set vmessage "$cfailed failures in return percentages"
314 set vmessage "$lfailed failures in line counts, $bfailed in branch percentages, $cfailed in return percentages"
316 fail "$subdir/$testcase gcov: $vmessage"
318 pass "$subdir/$testcase gcov"
322 # Initialize harness.
325 # Delete old .da files.
326 set files [glob -nocomplain gcov-*.da];
327 if { $files != "" } {
328 eval "remote_file build delete $files";
332 dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/gcov-*.c]] "" ""