File: llvm2lcov.sh

package info (click to toggle)
lcov 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,464 kB
  • sloc: perl: 27,911; sh: 7,320; xml: 6,982; python: 1,152; makefile: 597; cpp: 520; ansic: 176
file content (346 lines) | stat: -rwxr-xr-x 9,011 bytes parent folder | download
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
#!/bin/bash
set +x

if [[ "x" == ${LCOV_HOME}x ]] ; then
    if [ -f ../../bin/lcov ] ; then
        LCOV_HOME=../..
    fi
fi

source ../common.tst

rm -rf test *.profraw *.profdata *.json *.info report

clean_cover

if [[ 1 == $CLEAN_ONLY ]] ; then
    exit 0
fi

LCOV_OPTS="--branch-coverage $PARALLEL $PROFILE"

clang++ -fprofile-instr-generate -fcoverage-mapping -fcoverage-mcdc -o test main.cpp
if [ $? != 0 ] ; then
    echo "clang++ exec failed"
    exit 1
fi
./test
llvm-profdata merge --sparse *.profraw -o test.profdata
if [ $? != 0 ] ; then
    echo "llvm-profdata failed"
    exit 1
fi
llvm-cov export -format=text -instr-profile=test.profdata ./test > test.json
if [ $? != 0 ] ; then
    echo "llvm-cov failed"
    exit 1
fi

# disable function, branch and mcdc coverage
$COVER $LLVM2LCOV_TOOL --rc function_coverage=0 -o test.info test.json
if [ $? != 0 ] ; then
    echo "llvm2lcov failed"
    exit 1
fi

# disable mcdc coverage
$COVER $LLVM2LCOV_TOOL --branch -o test.info test.json
if [ $? != 0 ] ; then
    echo "llvm2lcov failed"
    exit 1
fi

# disable branch coverage
$COVER $LLVM2LCOV_TOOL --mcdc -o test.info test.json
if [ $? != 0 ] ; then
    echo "llvm2lcov failed"
    exit 1
fi

$COVER $LLVM2LCOV_TOOL --branch --mcdc -o test.info test.json
if [ $? != 0 ] ; then
    echo "llvm2lcov failed"
    exit 1
fi

# should be valid data to generate HTML
$COVER $GENHTML_TOOL --flat --branch --mcdc -o report test.info
if [ $? != 0 ] ; then
    echo "genhtml failed"
    exit 1
fi

# run again, excluding 'main.cpp'
$COVER $LLVM2LCOV_TOOL --branch --mcdc -o test.excl.info test.json --exclude '*/main.cpp'
if [ $? != 0 ] ; then
    echo "llvm2lcov --exclude failed"
    exit 1
fi

# should be 3 functions
N=`grep -c "FNA:" test.info`
if [ 3 != "$N" ] ; then
    echo "wrong number of functions"
    exit 1
fi

# look for expected location and function hit counts:
for d in \
    'FNL:[0-9],20,25' \
    'FNA:[0-9],2,_Z3fooc' \
    'FNL:[0-9],27,72' \
    'FNA:[0-9],1,main' \
    'FNL:[0-9],2,4' \
    'FNA:[0-9],1,main.cpp:_ZL3barv' \
    ; do
    grep -E $d test.info
    if [ 0 != $? ] ; then
        echo "did not find expected function data $d"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
done

# lines main.cpp:(31-42) should be hit
for line in $(seq 31 42) ; \
    do \
    grep -E "DA:$line,1" test.info
    if [ 0 != $? ] ; then
        echo "did not find expected hit on function line $line"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
done

# lines main.cpp:14, 45-48 should be 'not hit
for line in 14 45 46 47 48 ; do
    grep "DA:$line,0" test.info
    if [ 0 != $? ] ; then
        echo "did not find expected zero hit on function line $line"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
done

# lines main.cpp:30, 43, 51, 65 should be 'not instrumented
for line in 30 43 51 65 ; do
    grep "DA:$line" test.info
    if [ 0 == $? ] ; then
        echo "find unexpected instrumented line $line"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
done

# check lines total number
grep -E "LF:55$" test.info
if [ $? != 0 ] ; then
    echo "unexpected total number of lines"
    if [ 0 == $KEEP_GOING ] ; then
        exit 1
    fi
fi
# check lines hit number
grep -E "LH:50$" test.info
if [ $? != 0 ] ; then
    echo "unexpected hit number of lines"
    if [ 0 == $KEEP_GOING ] ; then
        exit 1
    fi
fi

# check that branches have right <branch> expressions
line=41
N=`grep -c "BRDA:$line," test.info`
if [ 2 != "$N" ] ; then
    echo "did not find expected branches on line $line"
    if [ 0 == $KEEP_GOING ] ; then
        exit 1
    fi
fi
grep "BRDA:$line,0,(i <= 0) == True,1" test.info
if [ 0 != $? ] ; then
    echo "did not find expected 'BRDA' entry on line $line"
    if [ 0 == $KEEP_GOING ] ; then
        exit 1
    fi
fi
grep "BRDA:$line,0,(i <= 0) == False,0" test.info
if [ 0 != $? ] ; then
    echo "did not find expected 'BRDA' entry on line $line"
    if [ 0 == $KEEP_GOING ] ; then
        exit 1
    fi
fi

# check that branches defined inside macros are instrumented right
# lines main.cpp:33, 36, 39, 44 should contain branches defined inside macros
for line in 33 36 39 44 ; do
    grep -E "BRDA:$line," test.info
    if [ 0 != $? ] ; then
        echo "did not find expected branches on line $line"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
done

# check branches total number
grep -E "BRF:56$" test.info
if [ $? != 0 ] ; then
    echo "unexpected total number of branches"
    if [ 0 == $KEEP_GOING ] ; then
        exit 1
    fi
fi
# check branches hit number
grep -E "BRH:35$" test.info
if [ $? != 0 ] ; then
    echo "unexpected hit number of branches"
    if [ 0 == $KEEP_GOING ] ; then
        exit 1
    fi
fi

# LLVM/21 and later generate JSON data files in the new format.
# So, these files should be processed differently.
IFS='.' read -r -a LLVM_VER <<< `clang -dumpversion`
if [ "${LLVM_VER[0]}" -ge 21 ] ; then
    # line main.cpp:70 should contain 2 groups of MC/DC entries
    line=70
    MCDC_1=`grep -c "MCDC:$line,2," test.info`
    MCDC_2=`grep -c "MCDC:$line,3," test.info`
    if [ 4 != "$MCDC_1" ] || [ 6 != "$MCDC_2" ] ; then
        echo "did not find expected MC/DC entries on line $line"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
    # check that MC/DC entries have right <expressions>
    N=`grep -c "MCDC:40,2,[tf],0,1,'i <= 0' in 'BOOL(i > 0) ||        i <= 0)'" test.info`
    if [ 2 != "$N" ] ; then
        echo "did not find expected MC/DC entries on line 40"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
    # check MC/DC defined in macros
    grep -E "MCDC:" test.excl.info
    if [ 0 == $? ] ; then
        echo "find unexpected MC/DC"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
    for line in 33 36 39 ; do
        grep -E "MCDC:$line,[23],[tf]" test.info
        if [ 0 != $? ] ; then
            echo "did not find expected MC/DC on line $line"
            if [ 0 == $KEEP_GOING ] ; then
                exit 1
            fi
        fi
    done
    # check MC/DC total number
    grep -E "MCF:40$" test.info
    if [ $? != 0 ] ; then
        echo "unexpected total number of MC/DC entries"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
    # check MC/DC hit number
    grep -E "MCH:10$" test.info
    if [ $? != 0 ] ; then
        echo "unexpected hit number of MC/DC entries"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
else
    # line main.cpp:70 should contain 2 groups of MC/DC entries
    line=70
    MCDC_1=`grep -c "MCDC:$line,2," test.info`
    MCDC_2=`grep -c "MCDC:$line,3," test.info`
    if [ 4 != "$MCDC_1" ] || [ 6 != "$MCDC_2" ] ; then
        echo "did not find expected MC/DC entries on line $line"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
    # check that MC/DC entries have right <expressions>
    N=`grep -c "MCDC:63,2,[tf],1,1,'i < 1' in 'a\[i\] && i < 1'" test.info`
    if [ 2 != "$N" ] ; then
        echo "did not find expected MC/DC entries on line 63"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
    # check MC/DC defined in macros
    grep -E "MCDC:6,3,[tf]" test.excl.info
    if [ 0 != $? ] ; then
        echo "did not find expected MC/DC"
        if [ 0 == $KEEP_GOING ] ; then
        exit 1
        fi
    fi
    for m in \
        "MCDC:6,2,[tf]" \
        "MCDC:15,2,[tf]" \
        ; do
        grep -E $m test.info
        if [ 0 != $? ] ; then
            echo "did not find expected MC/DC"
            if [ 0 == $KEEP_GOING ] ; then
            exit 1
            fi
        fi
    done
    # check MC/DC total number
    grep -E "MCF:34$" test.info
    if [ $? != 0 ] ; then
        echo "unexpected total number of MC/DC entries"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
    # check MC/DC hit number
    grep -E "MCH:10$" test.info
    if [ $? != 0 ] ; then
        echo "unexpected hit number of MC/DC entries"
        if [ 0 == $KEEP_GOING ] ; then
            exit 1
        fi
    fi
fi

# generate help message
$COVER ${EXEC_COVER} $LLVM2LCOV_TOOL --help
if [ 0 != $? ] ; then
    echo "llvm2lcov help failed"
    if [ 0 == $KEEP_GOING ] ; then
        exit 1
    fi
fi

# incorrect option
$COVER ${EXEC_COVER} $LLVM2LCOV_TOOL --unsupported
$COVER $LLVM2LCOV_TOOL --unsupported -o test.info test.json
if [ 0 == $? ] ; then
    echo "did not see incorrect option"
    if [ 0 == $KEEP_GOING ] ; then
        exit 1
    fi
fi

echo "Tests passed"

if [ "x$COVER" != "x" ] && [ $LOCAL_COVERAGE == 1 ]; then
    cover ${COVER_DB}
    $PERL2LCOV_TOOL -o ${COVER_DB}/perlcov.info ${COVER_DB} --ignore-errors inconsistent
    $GENHTML_TOOL -o ${COVER_DB}/report ${COVER_DB}/perlcov.info --flat --show-navigation --branch --ignore-errors inconsistent
fi