summaryrefslogtreecommitdiff
path: root/core/java12/com/vladium/emma/report/lcov/ReportGenerator.java
blob: 08a6310a3e232403632b6b1100a454def176b10c (plain)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Copyright 2009 Google Inc. All Rights Reserved.
 * Derived from code Copyright (C) 2003 Vladimir Roubtsov.
 *
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this
 * distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html
 *
 * $Id$
 */

package com.vladium.emma.report.lcov;

import com.vladium.emma.EMMARuntimeException;
import com.vladium.emma.IAppErrorCodes;
import com.vladium.emma.data.ClassDescriptor;
import com.vladium.emma.data.ICoverageData;
import com.vladium.emma.data.IMetaData;
import com.vladium.emma.report.AbstractReportGenerator;
import com.vladium.emma.report.AllItem;
import com.vladium.emma.report.ClassItem;
import com.vladium.emma.report.IItem;
import com.vladium.emma.report.ItemComparator;
import com.vladium.emma.report.MethodItem;
import com.vladium.emma.report.PackageItem;
import com.vladium.emma.report.SourcePathCache;
import com.vladium.emma.report.SrcFileItem;
import com.vladium.util.Descriptors;
import com.vladium.util.Files;
import com.vladium.util.IProperties;
import com.vladium.util.IntObjectMap;
import com.vladium.util.asserts.$assert;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * @author Vlad Roubtsov, (C) 2003
 * @author Tim Baverstock, (C) 2009
 *
 * Generates LCOV format files:
 *    http://manpages.ubuntu.com/manpages/karmic/man1/geninfo.1.html
 */
public final class ReportGenerator extends AbstractReportGenerator
                                   implements IAppErrorCodes
{
    public String getType()
    {
        return TYPE;
    }

    /**
     * Queue-based visitor, starts with the root, node visits enqueue child
     * nodes.
     */
    public void process(final IMetaData mdata,
                        final ICoverageData cdata,
                        final SourcePathCache cache,
                        final IProperties properties)
        throws EMMARuntimeException
    {
        initialize(mdata, cdata, cache, properties);

        long start = 0;
        long end;
        final boolean trace1 = m_log.atTRACE1();

        if (trace1)
        {
            start = System.currentTimeMillis();
        }

        m_queue = new LinkedList();
        for (m_queue.add(m_view.getRoot()); !m_queue.isEmpty(); )
        {
            final IItem head = (IItem) m_queue.removeFirst();
            head.accept(this, null);
        }
        close();

        if (trace1)
        {
            end = System.currentTimeMillis();
            m_log.trace1("process", "[" + getType() + "] report generated in "
                         + (end - start) + " ms");
        }
    }

    public void cleanup()
    {
        m_queue = null;
        close();
        super.cleanup();
    }


    /**
    * Visitor for top-level node; opens output file, enqueues packages.
    */
    public Object visit(final AllItem item, final Object ctx)
    {
        File outFile = m_settings.getOutFile();
        if (outFile == null)
        {
            outFile = new File("coverage.lcov");
            m_settings.setOutFile(outFile);
        }

        final File fullOutFile = Files.newFile(m_settings.getOutDir(), outFile);

        m_log.info("writing [" + getType() + "] report to ["
                   + fullOutFile.getAbsolutePath() + "] ...");

        openOutFile(fullOutFile, m_settings.getOutEncoding(), true);

        // Enqueue packages
        final ItemComparator order =
                m_typeSortComparators[PackageItem.getTypeMetadata().getTypeID()];
        for (Iterator packages = item.getChildren(order); packages.hasNext(); )
        {
            final IItem pkg = (IItem) packages.next();
            m_queue.addLast(pkg);
        }

        return ctx;
    }

    /**
     * Visitor for packages; enqueues source files contained by the package.
     */
    public Object visit(final PackageItem item, final Object ctx)
    {
        if (m_verbose)
        {
            m_log.verbose("  report: processing package [" + item.getName() + "] ...");
        }

        // Enqueue source files
        int id = m_srcView
                 ? SrcFileItem.getTypeMetadata().getTypeID()
                 : ClassItem.getTypeMetadata().getTypeID();
        final ItemComparator order = m_typeSortComparators[id];
        for (Iterator srcORclsFiles = item.getChildren(order);
             srcORclsFiles.hasNext();
            )
        {
            final IItem srcORcls = (IItem) srcORclsFiles.next();
            m_queue.addLast(srcORcls);
        }

        return ctx;
    }

    /**
     * Visitor for source files: doesn't use the enqueue mechanism to examine
     * deeper nodes because it writes the 'end_of_record' decoration here.
     */
    public Object visit (final SrcFileItem item, final Object ctx)
    {
        row("SF:".concat(item.getFullVMName()));

        // TODO: Enqueue ClassItems, then an 'end_of_record' object

        emitFileCoverage(item);

        row("end_of_record");
        return ctx;
    }

    /** Issue a coverage report for all lines in the file, and for each
     * function in the file.
     */
    private void emitFileCoverage(final SrcFileItem item)
    {
        if ($assert.ENABLED)
        {
            $assert.ASSERT(item != null, "null input: item");
        }

        final String fileName = item.getFullVMName();

        final String packageVMName = ((PackageItem) item.getParent()).getVMName();

        if (!m_hasLineNumberInfo)
        {
            m_log.info("source file '"
                       + Descriptors.combineVMName(packageVMName, fileName)
                       + "' has no line number information");
        }
        boolean success = false;

        try
        {
            // For each class in the file, for each method in the class,
            // examine the execution blocks in the method until one with
            // coverage is found. Report coverage or non-coverage on the
            // strength of that one block (much as for now, a line is 'covered'
            // if it's partially covered).

            // TODO: Intertwingle method records and line records

            {
                final ItemComparator order = m_typeSortComparators[
                        ClassItem.getTypeMetadata().getTypeID()];
                int clsIndex = 0;
                for (Iterator classes = item.getChildren(order);
                     classes.hasNext();
                     ++clsIndex)
                {
                    final ClassItem cls = (ClassItem) classes.next();

                    final String className = cls.getName();

                    ClassDescriptor cdesc = cls.getClassDescriptor();

                    // [methodid][blocksinmethod]
                    boolean[][] ccoverage = cls.getCoverage();

                    final ItemComparator order2 = m_typeSortComparators[
                            MethodItem.getTypeMetadata().getTypeID()];
                    for (Iterator methods = cls.getChildren(order2); methods.hasNext(); )
                    {
                        final MethodItem method = (MethodItem) methods.next();
                        String mname = method.getName();
                        final int methodID = method.getID();

                        boolean covered = false;
                        if (ccoverage != null)
                        {
                            if ($assert.ENABLED)
                            {
                                $assert.ASSERT(ccoverage.length > methodID, "index bounds");
                                $assert.ASSERT(ccoverage[methodID] != null, "null: coverage");
                                $assert.ASSERT(ccoverage[methodID].length > 0, "empty array");
                            }
                            covered = ccoverage[methodID][0];
                        }

                        row("FN:" + method.getFirstLine() + "," + className + "::" + mname);
                        row("FNDA:" + (covered ? 1 : 0) + "," + className + "::" + mname);
                    }
                }
            }

            // For each line in the file, emit a DA.

            {
                final int unitsType = m_settings.getUnitsType();
                // line num:int -> SrcFileItem.LineCoverageData
                IntObjectMap lineCoverageMap = null;
                int[] lineCoverageKeys = null;

                lineCoverageMap = item.getLineCoverage();
                $assert.ASSERT(lineCoverageMap != null, "null: lineCoverageMap");
                lineCoverageKeys = lineCoverageMap.keys();
                java.util.Arrays.sort(lineCoverageKeys);

                for (int i = 0; i < lineCoverageKeys.length; ++i)
                {
                    int l = lineCoverageKeys[i];
                    final SrcFileItem.LineCoverageData lCoverageData =
                            (SrcFileItem.LineCoverageData) lineCoverageMap.get(l);

                    if ($assert.ENABLED)
                    {
                        $assert.ASSERT(lCoverageData != null, "lCoverage is null");
                    }
                    switch (lCoverageData.m_coverageStatus)
                    {
                        case SrcFileItem.LineCoverageData.LINE_COVERAGE_ZERO:
                            row("DA:" + l + ",0");
                            break;

                        case SrcFileItem.LineCoverageData.LINE_COVERAGE_PARTIAL:
                            // TODO: Add partial coverage support to LCOV
                            row("DA:" + l + ",1");
                            break;

                        case SrcFileItem.LineCoverageData.LINE_COVERAGE_COMPLETE:
                            row("DA:" + l + ",1");
                            break;

                        default:
                            $assert.ASSERT(false, "invalid line coverage status: "
                                           + lCoverageData.m_coverageStatus);

                    } // end of switch
                }
            }

            success = true;
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.out);
            success = false;
        }

        if (!success)
        {
            m_log.info("[source file '"
                       + Descriptors.combineVMName(packageVMName, fileName)
                       + "' not found in sourcepath]");
        }
    }

    public Object visit (final ClassItem item, final Object ctx)
    {
        return ctx;
    }

    private void row(final StringBuffer str)
    {
        if ($assert.ENABLED)
        {
            $assert.ASSERT(str != null, "str = null");
        }

        try
        {
            m_out.write(str.toString());
            m_out.newLine();
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException(IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }
    }

    private void row(final String str)
    {
        if ($assert.ENABLED)
        {
            $assert.ASSERT(str != null, "str = null");
        }

        try
        {
            m_out.write(str);
            m_out.newLine();
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException(IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }
    }

    private void close()
    {
        if (m_out != null)
        {
            try
            {
                m_out.flush();
                m_out.close();
            }
            catch (IOException ioe)
            {
                throw new EMMARuntimeException(IAppErrorCodes.REPORT_IO_FAILURE, ioe);
            }
            finally
            {
                m_out = null;
            }
        }
    }

    private void openOutFile(final File file, final String encoding, final boolean mkdirs)
    {
        try
        {
            if (mkdirs)
            {
                final File parent = file.getParentFile();
                if (parent != null)
                {
                    parent.mkdirs();
                }
            }
            file.delete();
            if (file.exists())
            {
                throw new EMMARuntimeException("Failed to delete " + file);
            }
            m_out = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(file), encoding),
                    IO_BUF_SIZE);
        }
        catch (UnsupportedEncodingException uee)
        {
            throw new EMMARuntimeException(uee);
        }
        catch (IOException fnfe) // FileNotFoundException
        {
            // note: in J2SDK 1.3 FileOutputStream constructor's throws clause
            // was narrowed to FileNotFoundException:
            throw new EMMARuntimeException(fnfe);
        }
    }

    private LinkedList /* IITem */ m_queue;
    private BufferedWriter m_out;

    private static final String TYPE = "lcov";

    private static final int IO_BUF_SIZE = 32 * 1024;
}