1 # Copyright (C) 1997-2013 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 3 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 GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
17 # Verify various kinds of gcov output: line counts, branch percentages,
18 # and call return percentages. None of this is language-specific.
23 # clean-gcov -- delete the working files the compiler creates for gcov
25 # TESTCASE is the name of the test.
27 proc clean-gcov { testcase } {
28 set basename [file tail $testcase]
29 set base [file rootname $basename]
30 remote_file host delete $base.gcno $base.gcda \
31 $basename.gcov $base.h.gcov
35 # verify-lines -- check that line counts are as expected
37 # TESTNAME is the name of the test, including unique flags.
38 # TESTCASE is the name of the test file.
39 # FILE is the name of the gcov output file.
41 proc verify-lines { testname testcase file } {
42 #send_user "verify-lines\n"
47 while { [gets $fd line] >= 0 } {
48 # We want to match both "-" and "#####" as count as well as numbers,
49 # since we want to detect lines that shouldn't be marked as covered.
50 if [regexp "^ *(\[^:]*): *(\[0-9\\-#]+):.*count\\((\[0-9\\-#=]+)\\)(.*)" \
51 "$line" all is n shouldbe rest] {
52 if [regexp "^ *{(.*)}" $rest all xfailed] {
53 switch [dg-process-target $xfailed] {
55 "F" { setup_xfail "*-*-*" }
59 fail "$testname line $n: no data available"
61 } elseif { $is != $shouldbe } {
62 fail "$testname line $n: is $is:should be $shouldbe"
65 pass "$testname count for line $n"
74 # verify-branches -- check that branch percentages are as expected
76 # TESTNAME is the name of the test, including unique flags.
77 # TESTCASE is the name of the test file.
78 # FILE is the name of the gcov output file.
80 # Checks are based on comments in the source file. This means to look for
81 # branch percentages 10 or 90, 20 or 80, and # 70 or 30:
82 # /* branch(10, 20, 70) */
83 # This means that all specified percentages should have been seen by now:
85 # All specified percentages must also be seen by the next branch(n) or
86 # by the end of the file.
88 # Each check depends on the compiler having generated the expected
89 # branch instructions. Don't check for branches that might be
90 # optimized away or replaced with predicated instructions.
92 proc verify-branches { testname testcase file } {
93 #send_user "verify-branches\n"
99 while { [gets $fd line] >= 0 } {
100 regexp "^\[^:\]+: *(\[0-9\]+):" "$line" all n
101 if [regexp "branch" $line] {
102 verbose "Processing branch line $n: $line" 3
103 if [regexp "branch\\((\[0-9 \]+)\\)" "$line" all new_shouldbe] {
104 # All percentages in the current list should have been seen.
105 if {[llength $shouldbe] != 0} {
106 fail "$testname line $n: expected branch percentages not found: $shouldbe"
110 set shouldbe $new_shouldbe
111 #send_user "$n: looking for: $shouldbe\n"
112 # Record the percentages to check for. Replace percentage
113 # n > 50 with 100-n, since block ordering affects the
114 # direction of a branch.
115 for {set i 0} {$i < [llength $shouldbe]} {incr i} {
116 set num [lindex $shouldbe $i]
118 set shouldbe [lreplace $shouldbe $i $i [expr 100 - $num]]
121 } elseif [regexp "branch +\[0-9\]+ taken (-\[0-9\]+)%" "$line" \
123 # Percentages should never be negative.
124 fail "$testname line $n: negative percentage: $taken"
126 } elseif [regexp "branch +\[0-9\]+ taken (\[0-9\]+)%" "$line" \
128 #send_user "$n: taken = $taken\n"
129 # Percentages should never be greater than 100.
131 fail "$testname line $n: branch percentage greater than 100: $taken"
135 set taken [expr 100 - $taken]
137 # If this percentage is one to check for then remove it
138 # from the list. It's normal to ignore some reports.
139 set i [lsearch $shouldbe $taken]
141 set shouldbe [lreplace $shouldbe $i $i]
143 } elseif [regexp "branch\\(end\\)" "$line"] {
144 # All percentages in the list should have been seen by now.
145 if {[llength $shouldbe] != 0} {
146 fail "$testname line n: expected branch percentages not found: $shouldbe"
153 # All percentages in the list should have been seen.
154 if {[llength $shouldbe] != 0} {
155 fail "$testname line $n: expected branch percentages not found: $shouldbe"
163 # verify-calls -- check that call return percentages are as expected
165 # TESTNAME is the name of the test, including unique flags.
166 # TESTCASE is the name of the test file.
167 # FILE is the name of the gcov output file.
169 # Checks are based on comments in the source file. This means to look for
170 # call return percentages 50, 20, 33:
171 # /* returns(50, 20, 33) */
172 # This means that all specified percentages should have been seen by now:
174 # All specified percentages must also be seen by the next returns(n) or
175 # by the end of the file.
177 # Each check depends on the compiler having generated the expected
178 # call instructions. Don't check for calls that are inserted by the
179 # compiler or that might be inlined.
181 proc verify-calls { testname testcase file } {
182 #send_user "verify-calls\n"
186 set fd [open $file r]
188 while { [gets $fd line] >= 0 } {
189 regexp "^\[^:\]+: *(\[0-9\]+):" "$line" all n
190 if [regexp "return" $line] {
191 verbose "Processing returns line $n: $line" 3
192 if [regexp "returns\\((\[0-9 \]+)\\)" "$line" all new_shouldbe] {
193 # All percentages in the current list should have been seen.
194 if {[llength $shouldbe] != 0} {
195 fail "$testname line $n: expected return percentages not found: $shouldbe"
199 # Record the percentages to check for.
200 set shouldbe $new_shouldbe
201 } elseif [regexp "call +\[0-9\]+ returned (-\[0-9\]+)%" "$line" \
203 # Percentages should never be negative.
204 fail "$testname line $n: negative percentage: $returns"
206 } elseif [regexp "call +\[0-9\]+ returned (\[0-9\]+)%" "$line" \
208 # For branches we check that percentages are not greater than
209 # 100 but call return percentages can be, as for setjmp(), so
210 # don't count that as an error.
212 # If this percentage is one to check for then remove it
213 # from the list. It's normal to ignore some reports.
214 set i [lsearch $shouldbe $returns]
216 set shouldbe [lreplace $shouldbe $i $i]
218 } elseif [regexp "returns\\(end\\)" "$line"] {
219 # All percentages in the list should have been seen by now.
220 if {[llength $shouldbe] != 0} {
221 fail "$testname line $n: expected return percentages not found: $shouldbe"
228 # All percentages in the list should have been seen.
229 if {[llength $shouldbe] != 0} {
230 fail "$testname line $n: expected return percentages not found: $shouldbe"
237 # Called by dg-final to run gcov and analyze the results.
239 # ARGS consists of the optional strings "branches" and/or "calls",
240 # (indicating that these things should be verified) followed by a
241 # list of arguments to provide to gcov, including the name of the
244 proc run-gcov { args } {
249 set gcov_verify_calls 0
250 set gcov_verify_branches 0
254 if { $a == "calls" } {
255 set gcov_verify_calls 1
256 } elseif { $a == "branches" } {
257 set gcov_verify_branches 1
258 } elseif { $gcov_args == "" } {
261 switch [dg-process-target $a] {
263 "F" { set xfailed 1 }
268 set testname [testname-for-summary]
270 # Extract the test file name from the arguments.
271 set testcase [lindex $gcov_args end]
273 verbose "Running $GCOV $testcase" 2
274 set testcase [remote_download host $testcase]
275 set result [remote_exec host $GCOV $gcov_args]
276 if { [lindex $result 0] != 0 } {
280 fail "$testname gcov failed: [lindex $result 1]"
285 # Get the gcov output file after making sure it exists.
286 set files [glob -nocomplain $testcase.gcov]
287 if { $files == "" } {
291 fail "$testname gcov failed: $testcase.gov does not exist"
295 remote_upload host $testcase.gcov $testcase.gcov
297 # Check that line execution counts are as expected.
298 set lfailed [verify-lines $testname $testcase $testcase.gcov]
300 # If requested via the .x file, check that branch and call information
302 if { $gcov_verify_branches } {
303 set bfailed [verify-branches $testname $testcase $testcase.gcov]
307 if { $gcov_verify_calls } {
308 set cfailed [verify-calls $testname $testcase $testcase.gcov]
313 # Report whether the gcov test passed or failed. If there were
314 # multiple failures then the message is a summary.
315 set tfailed [expr $lfailed + $bfailed + $cfailed]
319 if { $tfailed > 0 } {
320 fail "$testname gcov: $lfailed failures in line counts, $bfailed in branch percentages, $cfailed in return percentages"
322 pass "$testname gcov"