]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gcov.c
PR c++/61339 - add mismatch between struct and class [-Wmismatched-tags] to non-bugs
[thirdparty/gcc.git] / gcc / gcov.c
CommitLineData
86144b75
DE
1/* Gcov.c: prepend line execution counts and branch probabilities to a
2 source file.
a5544970 3 Copyright (C) 1990-2019 Free Software Foundation, Inc.
86144b75 4 Contributed by James E. Wilson of Cygnus Support.
1d300e19 5 Mangled by Bob Manson of Cygnus Support.
4977bab6 6 Mangled further by Nathan Sidwell <nathan@codesourcery.com>
86144b75
DE
7
8Gcov is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
9dcd6f09 10the Free Software Foundation; either version 3, or (at your option)
86144b75
DE
11any later version.
12
13Gcov is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with Gcov; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
86144b75 21
86144b75
DE
22/* ??? Print a list of the ten blocks with the highest execution counts,
23 and list the line numbers corresponding to those blocks. Also, perhaps
24 list the line numbers with the highest execution counts, only printing
25 the first if there are several which are all listed in the same block. */
26
27/* ??? Should have an option to print the number of basic blocks, and the
28 percent of them that are covered. */
29
4977bab6
ZW
30/* Need an option to show individual block counts, and show
31 probabilities of fall through arcs. */
86144b75 32
1d300e19 33#include "config.h"
6aed7ed0
DA
34#define INCLUDE_ALGORITHM
35#define INCLUDE_VECTOR
28f4a4a8 36#define INCLUDE_STRING
136ca74e
ML
37#define INCLUDE_MAP
38#define INCLUDE_SET
b04cd507 39#include "system.h"
4977bab6
ZW
40#include "coretypes.h"
41#include "tm.h"
ab87f8c8 42#include "intl.h"
2691e6d7 43#include "diagnostic.h"
5735c3ea 44#include "version.h"
fc8a650e 45#include "demangle.h"
28f4a4a8 46#include "color-macros.h"
c8fda30f
ML
47#include "pretty-print.h"
48#include "json.h"
86144b75 49
c8fda30f 50#include <zlib.h>
5735c3ea
JM
51#include <getopt.h>
52
75cc66f2 53#include "md5.h"
24bc5921
ML
54
55using namespace std;
56
546d2adb 57#define IN_GCOV 1
86144b75 58#include "gcov-io.h"
d79f9ec9 59#include "gcov-io.c"
86144b75 60
1a9075e2 61/* The gcno file is generated by -ftest-coverage option. The gcda file is
4977bab6
ZW
62 generated by a program compiled with -fprofile-arcs. Their formats
63 are documented in gcov-io.h. */
86144b75
DE
64
65/* The functions in this file for creating and solution program flow graphs
4977bab6
ZW
66 are very similar to functions in the gcc source file profile.c. In
67 some places we make use of the knowledge of how profile.c works to
68 select particular algorithms here. */
86144b75 69
10adac51
XDL
70/* The code validates that the profile information read in corresponds
71 to the code currently being compiled. Rather than checking for
9696c529 72 identical files, the code below compares a checksum on the CFG
10adac51 73 (based on the order of basic blocks and the arcs in the CFG). If
9696c529
SB
74 the CFG checksum in the gcda file match the CFG checksum in the
75 gcno file, the profile data will be used. */
10adac51 76
86144b75
DE
77/* This is the size of the buffer used to read in source file lines. */
78
99b1c316
MS
79class function_info;
80class block_info;
81class source_info;
86144b75 82
4977bab6 83/* Describes an arc between two basic blocks. */
86144b75 84
232c80f2 85struct arc_info
4977bab6 86{
32dd366d 87 /* source and destination blocks. */
99b1c316
MS
88 class block_info *src;
89 class block_info *dst;
86144b75 90
4977bab6
ZW
91 /* transition counts. */
92 gcov_type count;
3d7ca167
ZD
93 /* used in cycle search, so that we do not clobber original counts. */
94 gcov_type cs_count;
86144b75 95
86144b75
DE
96 unsigned int count_valid : 1;
97 unsigned int on_tree : 1;
98 unsigned int fake : 1;
99 unsigned int fall_through : 1;
86144b75 100
8919c0d9
NS
101 /* Arc to a catch handler. */
102 unsigned int is_throw : 1;
103
27283c73
NS
104 /* Arc is for a function that abnormally returns. */
105 unsigned int is_call_non_return : 1;
106
fa10beec 107 /* Arc is for catch/setjmp. */
27283c73
NS
108 unsigned int is_nonlocal_return : 1;
109
110 /* Is an unconditional branch. */
111 unsigned int is_unconditional : 1;
112
10b7602f
NS
113 /* Loop making arc. */
114 unsigned int cycle : 1;
115
4977bab6
ZW
116 /* Links to next arc on src and dst lists. */
117 struct arc_info *succ_next;
118 struct arc_info *pred_next;
232c80f2 119};
86144b75 120
0790260e
ML
121/* Describes which locations (lines and files) are associated with
122 a basic block. */
123
6c1dae73 124class block_location_info
0790260e 125{
6c1dae73 126public:
0790260e
ML
127 block_location_info (unsigned _source_file_idx):
128 source_file_idx (_source_file_idx)
129 {}
130
131 unsigned source_file_idx;
132 vector<unsigned> lines;
133};
134
4977bab6
ZW
135/* Describes a basic block. Contains lists of arcs to successor and
136 predecessor blocks. */
86144b75 137
6c1dae73 138class block_info
86144b75 139{
6c1dae73 140public:
a82be9d5
ML
141 /* Constructor. */
142 block_info ();
143
4977bab6 144 /* Chain of exit and entry arcs. */
232c80f2
ML
145 arc_info *succ;
146 arc_info *pred;
4977bab6 147
32dd366d 148 /* Number of unprocessed exit and entry arcs. */
4977bab6
ZW
149 gcov_type num_succ;
150 gcov_type num_pred;
151
9b9d6370
ML
152 unsigned id;
153
32dd366d 154 /* Block execution count. */
4977bab6
ZW
155 gcov_type count;
156 unsigned count_valid : 1;
157 unsigned valid_chain : 1;
158 unsigned invalid_chain : 1;
8919c0d9 159 unsigned exceptional : 1;
4977bab6 160
27283c73 161 /* Block is a call instrumenting site. */
10b7602f
NS
162 unsigned is_call_site : 1; /* Does the call. */
163 unsigned is_call_return : 1; /* Is the return. */
4977bab6 164
27283c73
NS
165 /* Block is a landing pad for longjmp or throw. */
166 unsigned is_nonlocal_return : 1;
167
0790260e
ML
168 vector<block_location_info> locations;
169
170 struct
27283c73 171 {
0790260e
ML
172 /* Single line graph cycle workspace. Used for all-blocks
173 mode. */
232c80f2 174 arc_info *arc;
0790260e
ML
175 unsigned ident;
176 } cycle; /* Used in all-blocks mode, after blocks are linked onto
177 lines. */
27283c73
NS
178
179 /* Temporary chain for solving graph, and for chaining blocks on one
180 line. */
99b1c316 181 class block_info *chain;
f55ade6e 182
bea002e9 183};
86144b75 184
a82be9d5
ML
185block_info::block_info (): succ (NULL), pred (NULL), num_succ (0), num_pred (0),
186 id (0), count (0), count_valid (0), valid_chain (0), invalid_chain (0),
187 exceptional (0), is_call_site (0), is_call_return (0), is_nonlocal_return (0),
188 locations (), chain (NULL)
189{
190 cycle.arc = NULL;
191}
192
136ca74e
ML
193/* Describes a single line of source. Contains a chain of basic blocks
194 with code on it. */
195
6c1dae73 196class line_info
136ca74e 197{
6c1dae73 198public:
136ca74e
ML
199 /* Default constructor. */
200 line_info ();
201
202 /* Return true when NEEDLE is one of basic blocks the line belongs to. */
bea002e9 203 bool has_block (block_info *needle);
136ca74e
ML
204
205 /* Execution count. */
206 gcov_type count;
207
208 /* Branches from blocks that end on this line. */
232c80f2 209 vector<arc_info *> branches;
136ca74e
ML
210
211 /* blocks which start on this line. Used in all-blocks mode. */
bea002e9 212 vector<block_info *> blocks;
136ca74e
ML
213
214 unsigned exists : 1;
215 unsigned unexceptional : 1;
216 unsigned has_unexecuted_block : 1;
217};
218
219line_info::line_info (): count (0), branches (), blocks (), exists (false),
220 unexceptional (0), has_unexecuted_block (0)
221{
222}
223
224bool
bea002e9 225line_info::has_block (block_info *needle)
136ca74e
ML
226{
227 return std::find (blocks.begin (), blocks.end (), needle) != blocks.end ();
228}
229
c8fda30f
ML
230/* Output demangled function names. */
231
232static int flag_demangled_names = 0;
233
4977bab6 234/* Describes a single function. Contains an array of basic blocks. */
86144b75 235
6c1dae73 236class function_info
8b219a76 237{
6c1dae73 238public:
0790260e
ML
239 function_info ();
240 ~function_info ();
241
136ca74e
ML
242 /* Return true when line N belongs to the function in source file SRC_IDX.
243 The line must be defined in body of the function, can't be inlined. */
244 bool group_line_p (unsigned n, unsigned src_idx);
245
1e81a283
ML
246 /* Function filter based on function_info::artificial variable. */
247
248 static inline bool
249 is_artificial (function_info *fn)
250 {
251 return fn->artificial;
252 }
253
4977bab6 254 /* Name of function. */
c8fda30f
ML
255 char *m_name;
256 char *m_demangled_name;
796621e8 257 unsigned ident;
10adac51
XDL
258 unsigned lineno_checksum;
259 unsigned cfg_checksum;
86144b75 260
8919c0d9
NS
261 /* The graph contains at least one fake incoming edge. */
262 unsigned has_catch : 1;
263
136ca74e
ML
264 /* True when the function is artificial and does not exist
265 in a source file. */
266 unsigned artificial : 1;
267
268 /* True when multiple functions start at a line in a source file. */
269 unsigned is_group : 1;
270
9696c529
SB
271 /* Array of basic blocks. Like in GCC, the entry block is
272 at blocks[0] and the exit block is at blocks[1]. */
273#define ENTRY_BLOCK (0)
274#define EXIT_BLOCK (1)
bea002e9 275 vector<block_info> blocks;
27283c73 276 unsigned blocks_executed;
4977bab6
ZW
277
278 /* Raw arc coverage counts. */
4464b977 279 vector<gcov_type> counts;
27283c73 280
136ca74e
ML
281 /* First line number. */
282 unsigned start_line;
283
284 /* First line column. */
285 unsigned start_column;
286
287 /* Last line number. */
288 unsigned end_line;
289
b8154717
ML
290 /* Last line column. */
291 unsigned end_column;
292
136ca74e 293 /* Index of source file where the function is defined. */
1ce1b792 294 unsigned src;
27283c73 295
136ca74e
ML
296 /* Vector of line information. */
297 vector<line_info> lines;
f55ade6e 298
4977bab6 299 /* Next function. */
99b1c316 300 class function_info *next;
c8fda30f
ML
301
302 /* Get demangled name of a function. The demangled name
303 is converted when it is used for the first time. */
304 char *get_demangled_name ()
305 {
306 if (m_demangled_name == NULL)
307 {
308 m_demangled_name = cplus_demangle (m_name, DMGL_PARAMS);
309 if (!m_demangled_name)
310 m_demangled_name = m_name;
311 }
312
313 return m_demangled_name;
314 }
315
316 /* Get name of the function based on flag_demangled_names. */
317 char *get_name ()
318 {
319 return flag_demangled_names ? get_demangled_name () : m_name;
320 }
321
322 /* Return number of basic blocks (without entry and exit block). */
323 unsigned get_block_count ()
324 {
325 return blocks.size () - 2;
326 }
cb8758b2 327};
4977bab6 328
136ca74e
ML
329/* Function info comparer that will sort functions according to starting
330 line. */
331
332struct function_line_start_cmp
333{
334 inline bool operator() (const function_info *lhs,
335 const function_info *rhs)
336 {
337 return (lhs->start_line == rhs->start_line
338 ? lhs->start_column < rhs->start_column
339 : lhs->start_line < rhs->start_line);
340 }
341};
342
4977bab6
ZW
343/* Describes coverage of a file or function. */
344
a1b5dd18 345struct coverage_info
8b219a76
NS
346{
347 int lines;
348 int lines_executed;
f55ade6e 349
8b219a76
NS
350 int branches;
351 int branches_executed;
352 int branches_taken;
f55ade6e 353
8b219a76
NS
354 int calls;
355 int calls_executed;
f55ade6e 356
8b219a76 357 char *name;
a1b5dd18 358};
8b219a76 359
4977bab6
ZW
360/* Describes a file mentioned in the block graph. Contains an array
361 of line info. */
37b8715b 362
6c1dae73 363class source_info
4977bab6 364{
6c1dae73 365public:
c7432e76
ML
366 /* Default constructor. */
367 source_info ();
368
59688303
ML
369 vector<function_info *> *get_functions_at_location (unsigned line_num) const;
370
371 /* Register a new function. */
372 void add_function (function_info *fn);
136ca74e
ML
373
374 /* Index of the source_info in sources vector. */
375 unsigned index;
376
eeabee0a 377 /* Canonical name of source file. */
4977bab6 378 char *name;
1a9075e2 379 time_t file_time;
37b8715b 380
c7432e76 381 /* Vector of line information. */
4695d816 382 vector<line_info> lines;
86144b75 383
a1b5dd18 384 coverage_info coverage;
27283c73 385
d6683f89
ML
386 /* Maximum line count in the source file. */
387 unsigned int maximum_count;
388
27283c73
NS
389 /* Functions in this source file. These are in ascending line
390 number order. */
59688303
ML
391 vector<function_info *> functions;
392
393 /* Line number to functions map. */
394 vector<vector<function_info *> *> line_to_function_map;
c7432e76
ML
395};
396
136ca74e 397source_info::source_info (): index (0), name (NULL), file_time (),
d6683f89 398 lines (), coverage (), maximum_count (0), functions ()
c7432e76
ML
399{
400}
86144b75 401
59688303
ML
402/* Register a new function. */
403void
404source_info::add_function (function_info *fn)
136ca74e 405{
59688303 406 functions.push_back (fn);
136ca74e 407
59688303
ML
408 if (fn->start_line >= line_to_function_map.size ())
409 line_to_function_map.resize (fn->start_line + 1);
410
411 vector<function_info *> **slot = &line_to_function_map[fn->start_line];
412 if (*slot == NULL)
413 *slot = new vector<function_info *> ();
136ca74e 414
59688303
ML
415 (*slot)->push_back (fn);
416}
417
418vector<function_info *> *
419source_info::get_functions_at_location (unsigned line_num) const
420{
421 if (line_num >= line_to_function_map.size ())
422 return NULL;
136ca74e 423
59688303
ML
424 vector<function_info *> *slot = line_to_function_map[line_num];
425 if (slot != NULL)
426 std::sort (slot->begin (), slot->end (), function_line_start_cmp ());
427
428 return slot;
136ca74e
ML
429}
430
8a3f457f 431class name_map
eeabee0a 432{
8a3f457f
ML
433public:
434 name_map ()
435 {
436 }
437
438 name_map (char *_name, unsigned _src): name (_name), src (_src)
439 {
440 }
441
442 bool operator== (const name_map &rhs) const
443 {
444#if HAVE_DOS_BASED_FILE_SYSTEM
445 return strcasecmp (this->name, rhs.name) == 0;
446#else
447 return strcmp (this->name, rhs.name) == 0;
448#endif
449 }
450
451 bool operator< (const name_map &rhs) const
452 {
453#if HAVE_DOS_BASED_FILE_SYSTEM
454 return strcasecmp (this->name, rhs.name) < 0;
455#else
456 return strcmp (this->name, rhs.name) < 0;
457#endif
458 }
459
460 const char *name; /* Source file name */
eeabee0a 461 unsigned src; /* Source file */
8a3f457f 462};
eeabee0a 463
211bea6b 464/* Vector of all functions. */
cb8758b2 465static vector<function_info *> functions;
86144b75 466
59688303
ML
467/* Function ident to function_info * map. */
468static map<unsigned, function_info *> ident_to_fn;
469
c7432e76
ML
470/* Vector of source files. */
471static vector<source_info> sources;
1a9075e2 472
8a3f457f
ML
473/* Mapping of file names to sources */
474static vector<name_map> names;
eeabee0a 475
7a583153
ML
476/* Record all processed files in order to warn about
477 a file being read multiple times. */
478static vector<char *> processed_files;
479
27283c73
NS
480/* This holds data summary information. */
481
5366b186 482static unsigned object_runs;
27283c73 483
bdbdc4e1
NS
484static unsigned total_lines;
485static unsigned total_executed;
486
32dd366d 487/* Modification time of graph file. */
86144b75 488
4977bab6 489static time_t bbg_file_time;
86144b75 490
efbb59b2
SB
491/* Name of the notes (gcno) output file. The "bbg" prefix is for
492 historical reasons, when the notes file contained only the
493 basic block graph notes. */
86144b75 494
4977bab6 495static char *bbg_file_name;
86144b75 496
dd486eb2
NS
497/* Stamp of the bbg file */
498static unsigned bbg_stamp;
499
93814e2d
ML
500/* Supports has_unexecuted_blocks functionality. */
501static unsigned bbg_supports_has_unexecuted_blocks;
502
c74bd3fb
ML
503/* Working directory in which a TU was compiled. */
504static const char *bbg_cwd;
505
efbb59b2 506/* Name and file pointer of the input file for the count data (gcda). */
86144b75 507
4977bab6 508static char *da_file_name;
86144b75 509
80b3502b
NS
510/* Data file is missing. */
511
512static int no_data_file;
513
1a9075e2
TG
514/* If there is several input files, compute and display results after
515 reading all data files. This way if two or more gcda file refer to
516 the same source file (eg inline subprograms in a .h file), the
517 counts are added. */
518
519static int multiple_files = 0;
520
4977bab6 521/* Output branch probabilities. */
86144b75 522
4977bab6 523static int flag_branches = 0;
86144b75 524
f55ade6e 525/* Show unconditional branches too. */
27283c73
NS
526static int flag_unconditional = 0;
527
86144b75
DE
528/* Output a gcov file if this is true. This is on by default, and can
529 be turned off by the -n option. */
530
4977bab6 531static int flag_gcov_file = 1;
86144b75 532
feb4589d
ML
533/* Output to stdout instead to a gcov file. */
534
535static int flag_use_stdout = 0;
536
acdb4da7
NS
537/* Output progress indication if this is true. This is off by default
538 and can be turned on by the -d option. */
539
540static int flag_display_progress = 0;
541
c8fda30f 542/* Output *.gcov file in JSON intermediate format used by consumers. */
fc8a650e 543
c8fda30f 544static int flag_json_format = 0;
fc8a650e 545
4977bab6
ZW
546/* For included files, make the gcov output file name include the name
547 of the input source file. For example, if x.h is included in a.c,
548 then the output file name is a.c##x.h.gcov instead of x.h.gcov. */
86144b75 549
4977bab6 550static int flag_long_names = 0;
86144b75 551
75cc66f2
ML
552/* For situations when a long name can potentially hit filesystem path limit,
553 let's calculate md5sum of the path and append it to a file name. */
554
555static int flag_hash_filenames = 0;
556
9b9d6370
ML
557/* Print verbose informations. */
558
559static int flag_verbose = 0;
560
28f4a4a8
ML
561/* Print colored output. */
562
563static int flag_use_colors = 0;
564
d6683f89
ML
565/* Use perf-like colors to indicate hot lines. */
566
567static int flag_use_hotness_colors = 0;
568
27283c73
NS
569/* Output count information for every basic block, not merely those
570 that contain line number information. */
571
572static int flag_all_blocks = 0;
573
29a4ef18
ML
574/* Output human readable numbers. */
575
576static int flag_human_readable_numbers = 0;
577
86144b75
DE
578/* Output summary info for each function. */
579
4977bab6 580static int flag_function_summary = 0;
86144b75 581
4977bab6
ZW
582/* Object directory file prefix. This is the directory/file where the
583 graph and data files are looked for, if nonzero. */
86144b75
DE
584
585static char *object_directory = 0;
586
1bec9caa
NS
587/* Source directory prefix. This is removed from source pathnames
588 that match, when generating the output file name. */
589
590static char *source_prefix = 0;
591static size_t source_length = 0;
592
593/* Only show data for sources with relative pathnames. Absolute ones
594 usually indicate a system header file, which although it may
595 contain inline functions, is usually uninteresting. */
596static int flag_relative_only = 0;
597
37b8715b 598/* Preserve all pathname components. Needed when object files and
4977bab6
ZW
599 source files are in subdirectories. '/' is mangled as '#', '.' is
600 elided and '..' mangled to '^'. */
601
602static int flag_preserve_paths = 0;
37b8715b 603
8bfa6fc5 604/* Output the number of times a branch was taken as opposed to the percentage
32dd366d 605 of times it was taken. */
23190837 606
4977bab6 607static int flag_counts = 0;
8bfa6fc5 608
86144b75 609/* Forward declarations. */
f55ade6e
AJ
610static int process_args (int, char **);
611static void print_usage (int) ATTRIBUTE_NORETURN;
612static void print_version (void) ATTRIBUTE_NORETURN;
613static void process_file (const char *);
ca498a11 614static void process_all_functions (void);
1a9075e2 615static void generate_results (const char *);
f55ade6e 616static void create_file_names (const char *);
eeabee0a 617static char *canonicalize_name (const char *);
1ce1b792 618static unsigned find_source (const char *);
211bea6b
ML
619static void read_graph_file (void);
620static int read_count_file (void);
cb8758b2
ML
621static void solve_flow_graph (function_info *);
622static void find_exception_blocks (function_info *);
a1b5dd18
ML
623static void add_branch_counts (coverage_info *, const arc_info *);
624static void add_line_counts (coverage_info *, function_info *);
bdbdc4e1 625static void executed_summary (unsigned, unsigned);
e4b52fca
ML
626static void function_summary (const coverage_info *);
627static void file_summary (const coverage_info *);
f55ade6e 628static const char *format_gcov (gcov_type, gcov_type, int);
c7432e76
ML
629static void accumulate_line_counts (source_info *);
630static void output_gcov_file (const char *, source_info *);
232c80f2 631static int output_branch_count (FILE *, int, const arc_info *);
c7432e76 632static void output_lines (FILE *, const source_info *);
f55ade6e 633static char *make_gcov_file_name (const char *, const char *);
eeabee0a 634static char *mangle_name (const char *, char *);
f55ade6e
AJ
635static void release_structures (void);
636extern int main (int, char **);
86144b75 637
c8fda30f 638function_info::function_info (): m_name (NULL), m_demangled_name (NULL),
4e8bfd79 639 ident (0), lineno_checksum (0), cfg_checksum (0), has_catch (0),
136ca74e 640 artificial (0), is_group (0),
4464b977 641 blocks (), blocks_executed (0), counts (),
b8154717
ML
642 start_line (0), start_column (0), end_line (0), end_column (0),
643 src (0), lines (), next (NULL)
0790260e 644{
0790260e
ML
645}
646
647function_info::~function_info ()
648{
649 for (int i = blocks.size () - 1; i >= 0; i--)
650 {
232c80f2 651 arc_info *arc, *arc_n;
0790260e
ML
652
653 for (arc = blocks[i].succ; arc; arc = arc_n)
654 {
655 arc_n = arc->succ_next;
656 free (arc);
657 }
658 }
c8fda30f
ML
659 if (m_demangled_name != m_name)
660 free (m_demangled_name);
661 free (m_name);
0790260e
ML
662}
663
136ca74e
ML
664bool function_info::group_line_p (unsigned n, unsigned src_idx)
665{
666 return is_group && src == src_idx && start_line <= n && n <= end_line;
667}
668
24bc5921
ML
669/* Cycle detection!
670 There are a bajillion algorithms that do this. Boost's function is named
671 hawick_cycles, so I used the algorithm by K. A. Hawick and H. A. James in
672 "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
673 (url at <http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf>).
674
675 The basic algorithm is simple: effectively, we're finding all simple paths
676 in a subgraph (that shrinks every iteration). Duplicates are filtered by
677 "blocking" a path when a node is added to the path (this also prevents non-
678 simple paths)--the node is unblocked only when it participates in a cycle.
679 */
680
232c80f2 681typedef vector<arc_info *> arc_vector_t;
bea002e9 682typedef vector<const block_info *> block_vector_t;
24bc5921 683
24bc5921
ML
684/* Handle cycle identified by EDGES, where the function finds minimum cs_count
685 and subtract the value from all counts. The subtracted value is added
686 to COUNT. Returns type of loop. */
687
4af3b0ea 688static void
24bc5921
ML
689handle_cycle (const arc_vector_t &edges, int64_t &count)
690{
691 /* Find the minimum edge of the cycle, and reduce all nodes in the cycle by
692 that amount. */
4ab65208 693 int64_t cycle_count = INTTYPE_MAXIMUM (int64_t);
24bc5921
ML
694 for (unsigned i = 0; i < edges.size (); i++)
695 {
696 int64_t ecount = edges[i]->cs_count;
697 if (cycle_count > ecount)
698 cycle_count = ecount;
699 }
700 count += cycle_count;
701 for (unsigned i = 0; i < edges.size (); i++)
702 edges[i]->cs_count -= cycle_count;
703
2e9ff3bb 704 gcc_assert (cycle_count > 0);
24bc5921
ML
705}
706
707/* Unblock a block U from BLOCKED. Apart from that, iterate all blocks
708 blocked by U in BLOCK_LISTS. */
709
710static void
bea002e9 711unblock (const block_info *u, block_vector_t &blocked,
24bc5921
ML
712 vector<block_vector_t > &block_lists)
713{
714 block_vector_t::iterator it = find (blocked.begin (), blocked.end (), u);
715 if (it == blocked.end ())
716 return;
717
718 unsigned index = it - blocked.begin ();
719 blocked.erase (it);
720
363a0690 721 block_vector_t to_unblock (block_lists[index]);
24bc5921
ML
722
723 block_lists.erase (block_lists.begin () + index);
363a0690
ML
724
725 for (block_vector_t::iterator it = to_unblock.begin ();
726 it != to_unblock.end (); it++)
727 unblock (*it, blocked, block_lists);
24bc5921
ML
728}
729
2e9ff3bb
ML
730/* Return true when PATH contains a zero cycle arc count. */
731
732static bool
733path_contains_zero_cycle_arc (arc_vector_t &path)
734{
735 for (unsigned i = 0; i < path.size (); i++)
736 if (path[i]->cs_count == 0)
737 return true;
738 return false;
739}
740
24bc5921
ML
741/* Find circuit going to block V, PATH is provisional seen cycle.
742 BLOCKED is vector of blocked vertices, BLOCK_LISTS contains vertices
743 blocked by a block. COUNT is accumulated count of the current LINE.
744 Returns what type of loop it contains. */
745
4af3b0ea 746static bool
bea002e9 747circuit (block_info *v, arc_vector_t &path, block_info *start,
24bc5921 748 block_vector_t &blocked, vector<block_vector_t> &block_lists,
4695d816 749 line_info &linfo, int64_t &count)
24bc5921 750{
4af3b0ea 751 bool loop_found = false;
24bc5921
ML
752
753 /* Add v to the block list. */
754 gcc_assert (find (blocked.begin (), blocked.end (), v) == blocked.end ());
755 blocked.push_back (v);
756 block_lists.push_back (block_vector_t ());
757
232c80f2 758 for (arc_info *arc = v->succ; arc; arc = arc->succ_next)
24bc5921 759 {
bea002e9 760 block_info *w = arc->dst;
2e9ff3bb
ML
761 if (w < start
762 || arc->cs_count == 0
763 || !linfo.has_block (w))
24bc5921
ML
764 continue;
765
766 path.push_back (arc);
767 if (w == start)
4af3b0ea
ML
768 {
769 /* Cycle has been found. */
770 handle_cycle (path, count);
771 loop_found = true;
772 }
2e9ff3bb
ML
773 else if (!path_contains_zero_cycle_arc (path)
774 && find (blocked.begin (), blocked.end (), w) == blocked.end ())
4af3b0ea
ML
775 loop_found |= circuit (w, path, start, blocked, block_lists, linfo,
776 count);
24bc5921
ML
777
778 path.pop_back ();
779 }
780
4af3b0ea 781 if (loop_found)
24bc5921
ML
782 unblock (v, blocked, block_lists);
783 else
232c80f2 784 for (arc_info *arc = v->succ; arc; arc = arc->succ_next)
24bc5921 785 {
bea002e9 786 block_info *w = arc->dst;
2e9ff3bb
ML
787 if (w < start
788 || arc->cs_count == 0
789 || !linfo.has_block (w))
24bc5921
ML
790 continue;
791
792 size_t index
793 = find (blocked.begin (), blocked.end (), w) - blocked.begin ();
794 gcc_assert (index < blocked.size ());
795 block_vector_t &list = block_lists[index];
796 if (find (list.begin (), list.end (), v) == list.end ())
797 list.push_back (v);
798 }
799
4af3b0ea 800 return loop_found;
24bc5921
ML
801}
802
4af3b0ea 803/* Find cycles for a LINFO. */
24bc5921
ML
804
805static gcov_type
4af3b0ea 806get_cycles_count (line_info &linfo)
24bc5921
ML
807{
808 /* Note that this algorithm works even if blocks aren't in sorted order.
809 Each iteration of the circuit detection is completely independent
810 (except for reducing counts, but that shouldn't matter anyways).
811 Therefore, operating on a permuted order (i.e., non-sorted) only
812 has the effect of permuting the output cycles. */
813
4af3b0ea 814 bool loop_found = false;
24bc5921 815 gcov_type count = 0;
bea002e9 816 for (vector<block_info *>::iterator it = linfo.blocks.begin ();
4695d816 817 it != linfo.blocks.end (); it++)
24bc5921
ML
818 {
819 arc_vector_t path;
820 block_vector_t blocked;
821 vector<block_vector_t > block_lists;
4af3b0ea
ML
822 loop_found |= circuit (*it, path, *it, blocked, block_lists, linfo,
823 count);
24bc5921
ML
824 }
825
24bc5921
ML
826 return count;
827}
828
86144b75 829int
f55ade6e 830main (int argc, char **argv)
86144b75 831{
4977bab6 832 int argno;
acdb4da7 833 int first_arg;
2691e6d7
JM
834 const char *p;
835
836 p = argv[0] + strlen (argv[0]);
837 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
838 --p;
839 progname = p;
840
841 xmalloc_set_program_name (progname);
f55ade6e 842
98a3dad4 843 /* Unlock the stdio streams. */
2653bb0c 844 unlock_std_streams ();
98a3dad4 845
191bf464 846 gcc_init_libintl ();
ab87f8c8 847
2691e6d7
JM
848 diagnostic_initialize (global_dc, 0);
849
e3536b82
TG
850 /* Handle response files. */
851 expandargv (&argc, &argv);
852
4977bab6
ZW
853 argno = process_args (argc, argv);
854 if (optind == argc)
855 print_usage (true);
86144b75 856
1a9075e2
TG
857 if (argc - argno > 1)
858 multiple_files = 1;
859
acdb4da7 860 first_arg = argno;
176bf572 861
4977bab6 862 for (; argno != argc; argno++)
acdb4da7
NS
863 {
864 if (flag_display_progress)
176bf572
ML
865 printf ("Processing file %d out of %d\n", argno - first_arg + 1,
866 argc - first_arg);
acdb4da7 867 process_file (argv[argno]);
1a9075e2 868
c8fda30f 869 if (flag_json_format || argno == argc - 1)
e89ce41d 870 {
ca498a11 871 process_all_functions ();
e89ce41d
ML
872 generate_results (argv[argno]);
873 release_structures ();
874 }
875 }
f55ade6e 876
86144b75
DE
877 return 0;
878}
86144b75 879\f
5735c3ea
JM
880/* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
881 otherwise the output of --help. */
86144b75
DE
882
883static void
f55ade6e 884print_usage (int error_p)
86144b75 885{
5735c3ea
JM
886 FILE *file = error_p ? stderr : stdout;
887 int status = error_p ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
f55ade6e 888
541e0d55 889 fnotice (file, "Usage: gcov [OPTION...] SOURCE|OBJ...\n\n");
5735c3ea 890 fnotice (file, "Print code coverage information.\n\n");
27283c73 891 fnotice (file, " -a, --all-blocks Show information for every basic block\n");
5735c3ea 892 fnotice (file, " -b, --branch-probabilities Include branch probabilities in output\n");
fc8a650e 893 fnotice (file, " -c, --branch-counts Output counts of branches taken\n\
5735c3ea 894 rather than percentages\n");
fc8a650e
SS
895 fnotice (file, " -d, --display-progress Display progress information\n");
896 fnotice (file, " -f, --function-summaries Output summaries for each function\n");
2f360676 897 fnotice (file, " -h, --help Print this help, then exit\n");
e4b52fca 898 fnotice (file, " -i, --json-format Output JSON intermediate format into .gcov.json.gz file\n");
29a4ef18 899 fnotice (file, " -j, --human-readable Output human readable numbers\n");
28f4a4a8 900 fnotice (file, " -k, --use-colors Emit colored output\n");
5735c3ea
JM
901 fnotice (file, " -l, --long-file-names Use long output file names for included\n\
902 source files\n");
fc8a650e
SS
903 fnotice (file, " -m, --demangled-names Output demangled function names\n");
904 fnotice (file, " -n, --no-output Do not create an output file\n");
37b8715b
NS
905 fnotice (file, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
906 fnotice (file, " -p, --preserve-paths Preserve all pathname components\n");
d6683f89 907 fnotice (file, " -q, --use-hotness-colors Emit perf-like colored output for hot lines\n");
fc8a650e
SS
908 fnotice (file, " -r, --relative-only Only show data for relative sources\n");
909 fnotice (file, " -s, --source-prefix DIR Source prefix to elide\n");
feb4589d 910 fnotice (file, " -t, --stdout Output to stdout instead of a file\n");
27283c73 911 fnotice (file, " -u, --unconditional-branches Show unconditional branch counts too\n");
fc8a650e 912 fnotice (file, " -v, --version Print version number, then exit\n");
9b9d6370 913 fnotice (file, " -w, --verbose Print verbose informations\n");
75cc66f2 914 fnotice (file, " -x, --hash-filenames Hash long pathnames\n");
5735c3ea 915 fnotice (file, "\nFor bug reporting instructions, please see:\n%s.\n",
a976603e 916 bug_report_url);
5735c3ea
JM
917 exit (status);
918}
919
920/* Print version information and exit. */
921
922static void
f55ade6e 923print_version (void)
5735c3ea 924{
2f41c1d6 925 fnotice (stdout, "gcov %s%s\n", pkgversion_string, version_string);
3f27508c 926 fprintf (stdout, "Copyright %s 2019 Free Software Foundation, Inc.\n",
9f76f909 927 _("(C)"));
5735c3ea 928 fnotice (stdout,
9f76f909
KH
929 _("This is free software; see the source for copying conditions.\n"
930 "There is NO warranty; not even for MERCHANTABILITY or \n"
931 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
5735c3ea 932 exit (SUCCESS_EXIT_CODE);
86144b75
DE
933}
934
5735c3ea
JM
935static const struct option options[] =
936{
937 { "help", no_argument, NULL, 'h' },
938 { "version", no_argument, NULL, 'v' },
9b9d6370 939 { "verbose", no_argument, NULL, 'w' },
27283c73 940 { "all-blocks", no_argument, NULL, 'a' },
5735c3ea
JM
941 { "branch-probabilities", no_argument, NULL, 'b' },
942 { "branch-counts", no_argument, NULL, 'c' },
c8fda30f 943 { "json-format", no_argument, NULL, 'i' },
29a4ef18 944 { "human-readable", no_argument, NULL, 'j' },
5735c3ea
JM
945 { "no-output", no_argument, NULL, 'n' },
946 { "long-file-names", no_argument, NULL, 'l' },
947 { "function-summaries", no_argument, NULL, 'f' },
fc8a650e 948 { "demangled-names", no_argument, NULL, 'm' },
37b8715b 949 { "preserve-paths", no_argument, NULL, 'p' },
1bec9caa 950 { "relative-only", no_argument, NULL, 'r' },
37b8715b
NS
951 { "object-directory", required_argument, NULL, 'o' },
952 { "object-file", required_argument, NULL, 'o' },
1bec9caa 953 { "source-prefix", required_argument, NULL, 's' },
feb4589d 954 { "stdout", no_argument, NULL, 't' },
27283c73 955 { "unconditional-branches", no_argument, NULL, 'u' },
acdb4da7 956 { "display-progress", no_argument, NULL, 'd' },
75cc66f2 957 { "hash-filenames", no_argument, NULL, 'x' },
28f4a4a8 958 { "use-colors", no_argument, NULL, 'k' },
d6683f89 959 { "use-hotness-colors", no_argument, NULL, 'q' },
d90f9882 960 { 0, 0, 0, 0 }
5735c3ea
JM
961};
962
4977bab6 963/* Process args, return index to first non-arg. */
86144b75 964
4977bab6 965static int
f55ade6e 966process_args (int argc, char **argv)
86144b75 967{
5735c3ea 968 int opt;
86144b75 969
d6683f89 970 const char *opts = "abcdfhijklmno:pqrs:tuvwx";
75cc66f2 971 while ((opt = getopt_long (argc, argv, opts, options, NULL)) != -1)
86144b75 972 {
5735c3ea 973 switch (opt)
86144b75 974 {
27283c73
NS
975 case 'a':
976 flag_all_blocks = 1;
977 break;
5735c3ea 978 case 'b':
4977bab6 979 flag_branches = 1;
5735c3ea
JM
980 break;
981 case 'c':
4977bab6 982 flag_counts = 1;
5735c3ea 983 break;
27283c73
NS
984 case 'f':
985 flag_function_summary = 1;
5735c3ea 986 break;
27283c73
NS
987 case 'h':
988 print_usage (false);
989 /* print_usage will exit. */
5735c3ea 990 case 'l':
4977bab6 991 flag_long_names = 1;
5735c3ea 992 break;
29a4ef18
ML
993 case 'j':
994 flag_human_readable_numbers = 1;
995 break;
28f4a4a8
ML
996 case 'k':
997 flag_use_colors = 1;
998 break;
d6683f89
ML
999 case 'q':
1000 flag_use_hotness_colors = 1;
1001 break;
fc8a650e
SS
1002 case 'm':
1003 flag_demangled_names = 1;
1004 break;
27283c73
NS
1005 case 'n':
1006 flag_gcov_file = 0;
5735c3ea
JM
1007 break;
1008 case 'o':
1009 object_directory = optarg;
1010 break;
1bec9caa
NS
1011 case 's':
1012 source_prefix = optarg;
1013 source_length = strlen (source_prefix);
1014 break;
1015 case 'r':
1016 flag_relative_only = 1;
1017 break;
37b8715b 1018 case 'p':
4977bab6 1019 flag_preserve_paths = 1;
37b8715b 1020 break;
27283c73
NS
1021 case 'u':
1022 flag_unconditional = 1;
1023 break;
fc8a650e 1024 case 'i':
c8fda30f
ML
1025 flag_json_format = 1;
1026 flag_gcov_file = 1;
1027 break;
1028 case 'd':
1029 flag_display_progress = 1;
1030 break;
75cc66f2
ML
1031 case 'x':
1032 flag_hash_filenames = 1;
1033 break;
9b9d6370
ML
1034 case 'w':
1035 flag_verbose = 1;
1036 break;
feb4589d
ML
1037 case 't':
1038 flag_use_stdout = 1;
1039 break;
ec383304
ML
1040 case 'v':
1041 print_version ();
27283c73 1042 /* print_version will exit. */
5735c3ea
JM
1043 default:
1044 print_usage (true);
1045 /* print_usage will exit. */
86144b75 1046 }
86144b75
DE
1047 }
1048
4977bab6
ZW
1049 return optind;
1050}
1051
52eba779
ML
1052/* Output intermediate LINE sitting on LINE_NUM to JSON OBJECT.
1053 Add FUNCTION_NAME to the LINE. */
136ca74e
ML
1054
1055static void
c8fda30f 1056output_intermediate_json_line (json::array *object,
52eba779
ML
1057 line_info *line, unsigned line_num,
1058 const char *function_name)
136ca74e
ML
1059{
1060 if (!line->exists)
1061 return;
1062
c8fda30f
ML
1063 json::object *lineo = new json::object ();
1064 lineo->set ("line_number", new json::number (line_num));
52eba779
ML
1065 if (function_name != NULL)
1066 lineo->set ("function_name", new json::string (function_name));
c8fda30f
ML
1067 lineo->set ("count", new json::number (line->count));
1068 lineo->set ("unexecuted_block",
1069 new json::literal (line->has_unexecuted_block));
1070
1071 json::array *branches = new json::array ();
1072 lineo->set ("branches", branches);
136ca74e 1073
232c80f2 1074 vector<arc_info *>::const_iterator it;
136ca74e
ML
1075 if (flag_branches)
1076 for (it = line->branches.begin (); it != line->branches.end ();
1077 it++)
1078 {
1079 if (!(*it)->is_unconditional && !(*it)->is_call_non_return)
1080 {
c8fda30f
ML
1081 json::object *branch = new json::object ();
1082 branch->set ("count", new json::number ((*it)->count));
1083 branch->set ("throw", new json::literal ((*it)->is_throw));
1084 branch->set ("fallthrough",
1085 new json::literal ((*it)->fall_through));
1086 branches->append (branch);
136ca74e
ML
1087 }
1088 }
c8fda30f
ML
1089
1090 object->append (lineo);
136ca74e
ML
1091}
1092
e89ce41d
ML
1093/* Get the name of the gcov file. The return value must be free'd.
1094
1095 It appends the '.gcov' extension to the *basename* of the file.
1096 The resulting file name will be in PWD.
1097
1098 e.g.,
1099 input: foo.da, output: foo.da.gcov
1100 input: a/b/foo.cc, output: foo.cc.gcov */
1101
1102static char *
1103get_gcov_intermediate_filename (const char *file_name)
1104{
c8fda30f 1105 const char *gcov = ".gcov.json.gz";
e89ce41d
ML
1106 char *result;
1107 const char *cptr;
1108
1109 /* Find the 'basename'. */
1110 cptr = lbasename (file_name);
1111
1112 result = XNEWVEC (char, strlen (cptr) + strlen (gcov) + 1);
1113 sprintf (result, "%s%s", cptr, gcov);
1114
1115 return result;
1116}
1117
c8fda30f
ML
1118/* Output the result in JSON intermediate format.
1119 Source info SRC is dumped into JSON_FILES which is JSON array. */
fc8a650e
SS
1120
1121static void
c8fda30f 1122output_json_intermediate_file (json::array *json_files, source_info *src)
fc8a650e 1123{
c8fda30f
ML
1124 json::object *root = new json::object ();
1125 json_files->append (root);
1126
1127 root->set ("file", new json::string (src->name));
1128
1129 json::array *functions = new json::array ();
1130 root->set ("functions", functions);
fc8a650e 1131
136ca74e
ML
1132 std::sort (src->functions.begin (), src->functions.end (),
1133 function_line_start_cmp ());
cb8758b2 1134 for (vector<function_info *>::iterator it = src->functions.begin ();
136ca74e 1135 it != src->functions.end (); it++)
fc8a650e 1136 {
c8fda30f
ML
1137 json::object *function = new json::object ();
1138 function->set ("name", new json::string ((*it)->m_name));
1139 function->set ("demangled_name",
1140 new json::string ((*it)->get_demangled_name ()));
1141 function->set ("start_line", new json::number ((*it)->start_line));
b8154717 1142 function->set ("start_column", new json::number ((*it)->start_column));
c8fda30f 1143 function->set ("end_line", new json::number ((*it)->end_line));
b8154717 1144 function->set ("end_column", new json::number ((*it)->end_column));
c8fda30f
ML
1145 function->set ("blocks",
1146 new json::number ((*it)->get_block_count ()));
1147 function->set ("blocks_executed",
1148 new json::number ((*it)->blocks_executed));
1149 function->set ("execution_count",
1150 new json::number ((*it)->blocks[0].count));
1151
1152 functions->append (function);
fc8a650e
SS
1153 }
1154
c8fda30f
ML
1155 json::array *lineso = new json::array ();
1156 root->set ("lines", lineso);
1157
52eba779
ML
1158 function_info *last_non_group_fn = NULL;
1159
e89ce41d 1160 for (unsigned line_num = 1; line_num <= src->lines.size (); line_num++)
fc8a650e 1161 {
59688303 1162 vector<function_info *> *fns = src->get_functions_at_location (line_num);
136ca74e 1163
59688303
ML
1164 if (fns != NULL)
1165 /* Print first group functions that begin on the line. */
1166 for (vector<function_info *>::iterator it2 = fns->begin ();
1167 it2 != fns->end (); it2++)
1168 {
52eba779
ML
1169 if (!(*it2)->is_group)
1170 last_non_group_fn = *it2;
1171
59688303
ML
1172 vector<line_info> &lines = (*it2)->lines;
1173 for (unsigned i = 0; i < lines.size (); i++)
1174 {
1175 line_info *line = &lines[i];
52eba779
ML
1176 output_intermediate_json_line (lineso, line, line_num + i,
1177 (*it2)->m_name);
59688303
ML
1178 }
1179 }
136ca74e
ML
1180
1181 /* Follow with lines associated with the source file. */
dda107df 1182 if (line_num < src->lines.size ())
52eba779
ML
1183 output_intermediate_json_line (lineso, &src->lines[line_num], line_num,
1184 (last_non_group_fn != NULL
1185 ? last_non_group_fn->m_name : NULL));
fc8a650e
SS
1186 }
1187}
1188
136ca74e
ML
1189/* Function start pair. */
1190struct function_start
1191{
1192 unsigned source_file_idx;
1193 unsigned start_line;
1194};
1195
1196/* Traits class for function start hash maps below. */
1197
1198struct function_start_pair_hash : typed_noop_remove <function_start>
1199{
1200 typedef function_start value_type;
1201 typedef function_start compare_type;
1202
1203 static hashval_t
1204 hash (const function_start &ref)
1205 {
1206 inchash::hash hstate (0);
1207 hstate.add_int (ref.source_file_idx);
1208 hstate.add_int (ref.start_line);
1209 return hstate.end ();
1210 }
1211
1212 static bool
1213 equal (const function_start &ref1, const function_start &ref2)
1214 {
1215 return (ref1.source_file_idx == ref2.source_file_idx
1216 && ref1.start_line == ref2.start_line);
1217 }
1218
1219 static void
1220 mark_deleted (function_start &ref)
1221 {
1222 ref.start_line = ~1U;
1223 }
1224
1225 static void
1226 mark_empty (function_start &ref)
1227 {
1228 ref.start_line = ~2U;
1229 }
1230
1231 static bool
1232 is_deleted (const function_start &ref)
1233 {
1234 return ref.start_line == ~1U;
1235 }
1236
1237 static bool
1238 is_empty (const function_start &ref)
1239 {
1240 return ref.start_line == ~2U;
1241 }
1242};
1243
eeabee0a 1244/* Process a single input file. */
5735c3ea 1245
4977bab6 1246static void
f55ade6e 1247process_file (const char *file_name)
4977bab6 1248{
4977bab6 1249 create_file_names (file_name);
7a583153
ML
1250
1251 for (unsigned i = 0; i < processed_files.size (); i++)
1252 if (strcmp (da_file_name, processed_files[i]) == 0)
1253 {
1254 fnotice (stderr, "'%s' file is already processed\n",
1255 file_name);
1256 return;
1257 }
1258
1259 processed_files.push_back (xstrdup (da_file_name));
1260
211bea6b 1261 read_graph_file ();
211bea6b 1262 read_count_file ();
ca498a11
ML
1263}
1264
1265/* Process all functions in all files. */
136ca74e 1266
ca498a11
ML
1267static void
1268process_all_functions (void)
1269{
cb8758b2 1270 hash_map<function_start_pair_hash, function_info *> fn_map;
136ca74e
ML
1271
1272 /* Identify group functions. */
cb8758b2 1273 for (vector<function_info *>::iterator it = functions.begin ();
211bea6b
ML
1274 it != functions.end (); it++)
1275 if (!(*it)->artificial)
136ca74e
ML
1276 {
1277 function_start needle;
211bea6b
ML
1278 needle.source_file_idx = (*it)->src;
1279 needle.start_line = (*it)->start_line;
136ca74e 1280
cb8758b2 1281 function_info **slot = fn_map.get (needle);
136ca74e
ML
1282 if (slot)
1283 {
136ca74e 1284 (*slot)->is_group = 1;
211bea6b 1285 (*it)->is_group = 1;
136ca74e
ML
1286 }
1287 else
211bea6b 1288 fn_map.put (needle, *it);
136ca74e
ML
1289 }
1290
1e81a283
ML
1291 /* Remove all artificial function. */
1292 functions.erase (remove_if (functions.begin (), functions.end (),
1293 function_info::is_artificial), functions.end ());
1294
cb8758b2 1295 for (vector<function_info *>::iterator it = functions.begin ();
211bea6b 1296 it != functions.end (); it++)
4977bab6 1297 {
cb8758b2 1298 function_info *fn = *it;
1e81a283 1299 unsigned src = fn->src;
f55ade6e 1300
4464b977 1301 if (!fn->counts.empty () || no_data_file)
5366b186 1302 {
1e81a283 1303 source_info *s = &sources[src];
59688303 1304 s->add_function (fn);
1ce1b792 1305
1e81a283
ML
1306 /* Mark last line in files touched by function. */
1307 for (unsigned block_no = 0; block_no != fn->blocks.size ();
1308 block_no++)
1ce1b792 1309 {
bea002e9 1310 block_info *block = &fn->blocks[block_no];
1e81a283 1311 for (unsigned i = 0; i < block->locations.size (); i++)
136ca74e 1312 {
1e81a283
ML
1313 /* Sort lines of locations. */
1314 sort (block->locations[i].lines.begin (),
1315 block->locations[i].lines.end ());
1316
1317 if (!block->locations[i].lines.empty ())
0790260e 1318 {
1e81a283
ML
1319 s = &sources[block->locations[i].source_file_idx];
1320 unsigned last_line
1321 = block->locations[i].lines.back ();
136ca74e 1322
1e81a283
ML
1323 /* Record new lines for the function. */
1324 if (last_line >= s->lines.size ())
136ca74e
ML
1325 {
1326 s = &sources[block->locations[i].source_file_idx];
1327 unsigned last_line
1328 = block->locations[i].lines.back ();
1329
1330 /* Record new lines for the function. */
1331 if (last_line >= s->lines.size ())
1332 {
1333 /* Record new lines for a source file. */
1334 s->lines.resize (last_line + 1);
1335 }
1336 }
0790260e
ML
1337 }
1338 }
1ce1b792 1339 }
1e81a283
ML
1340
1341 /* Allocate lines for group function, following start_line
1342 and end_line information of the function. */
1343 if (fn->is_group)
1344 fn->lines.resize (fn->end_line - fn->start_line + 1);
1345
1e81a283
ML
1346 solve_flow_graph (fn);
1347 if (fn->has_catch)
1348 find_exception_blocks (fn);
5366b186
NS
1349 }
1350 else
211bea6b
ML
1351 {
1352 /* The function was not in the executable -- some other
1353 instance must have been selected. */
1354 }
5366b186 1355 }
1a9075e2
TG
1356}
1357
fc8a650e 1358static void
c7432e76 1359output_gcov_file (const char *file_name, source_info *src)
fc8a650e
SS
1360{
1361 char *gcov_file_name = make_gcov_file_name (file_name, src->coverage.name);
1362
1363 if (src->coverage.lines)
1364 {
1365 FILE *gcov_file = fopen (gcov_file_name, "w");
1366 if (gcov_file)
e89ce41d
ML
1367 {
1368 fnotice (stdout, "Creating '%s'\n", gcov_file_name);
1369 output_lines (gcov_file, src);
1370 if (ferror (gcov_file))
1371 fnotice (stderr, "Error writing output file '%s'\n",
1372 gcov_file_name);
1373 fclose (gcov_file);
1374 }
fc8a650e 1375 else
e89ce41d 1376 fnotice (stderr, "Could not open output file '%s'\n", gcov_file_name);
fc8a650e
SS
1377 }
1378 else
1379 {
1380 unlink (gcov_file_name);
1381 fnotice (stdout, "Removing '%s'\n", gcov_file_name);
1382 }
1383 free (gcov_file_name);
1384}
1385
1a9075e2
TG
1386static void
1387generate_results (const char *file_name)
1388{
c8fda30f 1389 char *gcov_intermediate_filename;
1a9075e2 1390
cb8758b2 1391 for (vector<function_info *>::iterator it = functions.begin ();
211bea6b 1392 it != functions.end (); it++)
4977bab6 1393 {
cb8758b2 1394 function_info *fn = *it;
a1b5dd18 1395 coverage_info coverage;
f55ade6e 1396
4977bab6 1397 memset (&coverage, 0, sizeof (coverage));
c8fda30f 1398 coverage.name = fn->get_name ();
4977bab6
ZW
1399 add_line_counts (flag_function_summary ? &coverage : NULL, fn);
1400 if (flag_function_summary)
1401 {
e4b52fca 1402 function_summary (&coverage);
4977bab6
ZW
1403 fnotice (stdout, "\n");
1404 }
1405 }
f55ade6e 1406
8a3f457f
ML
1407 name_map needle;
1408
eeabee0a
NS
1409 if (file_name)
1410 {
8a3f457f
ML
1411 needle.name = file_name;
1412 vector<name_map>::iterator it = std::find (names.begin (), names.end (),
1413 needle);
1414 if (it != names.end ())
1415 file_name = sources[it->src].coverage.name;
eeabee0a
NS
1416 else
1417 file_name = canonicalize_name (file_name);
1418 }
fc8a650e 1419
c8fda30f
ML
1420 gcov_intermediate_filename = get_gcov_intermediate_filename (file_name);
1421
1422 json::object *root = new json::object ();
1423 root->set ("format_version", new json::string ("1"));
1424 root->set ("gcc_version", new json::string (version_string));
0ffa4bc2
ML
1425
1426 if (bbg_cwd != NULL)
1427 root->set ("current_working_directory", new json::string (bbg_cwd));
99603e90 1428 root->set ("data_file", new json::string (file_name));
c8fda30f
ML
1429
1430 json::array *json_files = new json::array ();
1431 root->set ("files", json_files);
e89ce41d 1432
c7432e76
ML
1433 for (vector<source_info>::iterator it = sources.begin ();
1434 it != sources.end (); it++)
4977bab6 1435 {
c7432e76 1436 source_info *src = &(*it);
1bec9caa
NS
1437 if (flag_relative_only)
1438 {
1439 /* Ignore this source, if it is an absolute path (after
1440 source prefix removal). */
1441 char first = src->coverage.name[0];
176bf572 1442
1bec9caa
NS
1443#if HAVE_DOS_BASED_FILE_SYSTEM
1444 if (first && src->coverage.name[1] == ':')
fe860eb5 1445 first = src->coverage.name[2];
1bec9caa 1446#endif
fe860eb5
KT
1447 if (IS_DIR_SEPARATOR (first))
1448 continue;
1bec9caa 1449 }
176bf572 1450
4977bab6 1451 accumulate_line_counts (src);
feb4589d
ML
1452
1453 if (!flag_use_stdout)
e4b52fca 1454 file_summary (&src->coverage);
bdbdc4e1
NS
1455 total_lines += src->coverage.lines;
1456 total_executed += src->coverage.lines_executed;
8f961b22 1457 if (flag_gcov_file)
4977bab6 1458 {
c8fda30f
ML
1459 if (flag_json_format)
1460 output_json_intermediate_file (json_files, src);
e89ce41d 1461 else
feb4589d
ML
1462 {
1463 if (flag_use_stdout)
1464 {
1465 if (src->coverage.lines)
1466 output_lines (stdout, src);
1467 }
1468 else
1469 {
1470 output_gcov_file (file_name, src);
1471 fnotice (stdout, "\n");
1472 }
1473 }
e89ce41d
ML
1474 }
1475 }
1476
c8fda30f 1477 if (flag_gcov_file && flag_json_format)
e89ce41d 1478 {
c8fda30f
ML
1479 if (flag_use_stdout)
1480 {
1481 root->dump (stdout);
1482 printf ("\n");
1483 }
1484 else
1485 {
1486 pretty_printer pp;
1487 root->print (&pp);
1488 pp_formatted_text (&pp);
1489
1490 gzFile output = gzopen (gcov_intermediate_filename, "w");
1491 if (output == NULL)
1492 {
1493 fnotice (stderr, "Cannot open JSON output file %s\n",
1494 gcov_intermediate_filename);
1495 return;
1496 }
1497
1498 if (gzputs (output, pp_formatted_text (&pp)) == EOF
1499 || gzclose (output))
1500 {
1501 fnotice (stderr, "Error writing JSON output file %s\n",
1502 gcov_intermediate_filename);
1503 return;
1504 }
1505 }
fc8a650e 1506 }
8f961b22 1507
bdbdc4e1
NS
1508 if (!file_name)
1509 executed_summary (total_lines, total_executed);
86144b75
DE
1510}
1511
4977bab6 1512/* Release all memory used. */
86144b75 1513
4977bab6 1514static void
f55ade6e 1515release_structures (void)
4977bab6 1516{
cb8758b2 1517 for (vector<function_info *>::iterator it = functions.begin ();
211bea6b
ML
1518 it != functions.end (); it++)
1519 delete (*it);
f55ade6e 1520
e89ce41d
ML
1521 sources.resize (0);
1522 names.resize (0);
211bea6b 1523 functions.resize (0);
59688303 1524 ident_to_fn.clear ();
4977bab6
ZW
1525}
1526
15882fe9
TG
1527/* Generate the names of the graph and data files. If OBJECT_DIRECTORY
1528 is not specified, these are named from FILE_NAME sans extension. If
1529 OBJECT_DIRECTORY is specified and is a directory, the files are in that
1530 directory, but named from the basename of the FILE_NAME, sans extension.
1531 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
1532 and the data files are named from that. */
86144b75
DE
1533
1534static void
f55ade6e 1535create_file_names (const char *file_name)
86144b75 1536{
86144b75 1537 char *cptr;
37b8715b 1538 char *name;
4977bab6 1539 int length = strlen (file_name);
37b8715b 1540 int base;
f55ade6e 1541
1a9075e2 1542 /* Free previous file names. */
04695783
JM
1543 free (bbg_file_name);
1544 free (da_file_name);
1a9075e2
TG
1545 da_file_name = bbg_file_name = NULL;
1546 bbg_file_time = 0;
1547 bbg_stamp = 0;
1548
37b8715b 1549 if (object_directory && object_directory[0])
86144b75 1550 {
37b8715b
NS
1551 struct stat status;
1552
1553 length += strlen (object_directory) + 2;
5ed6ace5 1554 name = XNEWVEC (char, length);
37b8715b 1555 name[0] = 0;
f55ade6e 1556
37b8715b
NS
1557 base = !stat (object_directory, &status) && S_ISDIR (status.st_mode);
1558 strcat (name, object_directory);
176bf572 1559 if (base && (!IS_DIR_SEPARATOR (name[strlen (name) - 1])))
37b8715b 1560 strcat (name, "/");
86144b75
DE
1561 }
1562 else
1563 {
5ed6ace5 1564 name = XNEWVEC (char, length + 1);
15882fe9
TG
1565 strcpy (name, file_name);
1566 base = 0;
86144b75 1567 }
f55ade6e 1568
37b8715b
NS
1569 if (base)
1570 {
f9da5064 1571 /* Append source file name. */
2f07423c
PO
1572 const char *cptr = lbasename (file_name);
1573 strcat (name, cptr ? cptr : file_name);
37b8715b 1574 }
f55ade6e 1575
4b7e68e7 1576 /* Remove the extension. */
258ef007 1577 cptr = strrchr (CONST_CAST (char *, lbasename (name)), '.');
86144b75 1578 if (cptr)
37b8715b 1579 *cptr = 0;
f55ade6e 1580
37b8715b 1581 length = strlen (name);
b8698a0f 1582
5ed6ace5 1583 bbg_file_name = XNEWVEC (char, length + strlen (GCOV_NOTE_SUFFIX) + 1);
4977bab6 1584 strcpy (bbg_file_name, name);
160e2e4f 1585 strcpy (bbg_file_name + length, GCOV_NOTE_SUFFIX);
37b8715b 1586
5ed6ace5 1587 da_file_name = XNEWVEC (char, length + strlen (GCOV_DATA_SUFFIX) + 1);
4977bab6
ZW
1588 strcpy (da_file_name, name);
1589 strcpy (da_file_name + length, GCOV_DATA_SUFFIX);
f55ade6e 1590
a14df7da 1591 free (name);
4977bab6 1592 return;
86144b75 1593}
86144b75 1594
94de45d9
NS
1595/* Find or create a source file structure for FILE_NAME. Copies
1596 FILE_NAME on creation */
27283c73 1597
1ce1b792 1598static unsigned
f55ade6e 1599find_source (const char *file_name)
27283c73 1600{
eeabee0a
NS
1601 char *canon;
1602 unsigned idx;
1a9075e2 1603 struct stat status;
94de45d9
NS
1604
1605 if (!file_name)
1606 file_name = "<unknown>";
f55ade6e 1607
8a3f457f
ML
1608 name_map needle;
1609 needle.name = file_name;
1610
1611 vector<name_map>::iterator it = std::find (names.begin (), names.end (),
1612 needle);
1613 if (it != names.end ())
1a9075e2 1614 {
8a3f457f
ML
1615 idx = it->src;
1616 goto check_date;
eeabee0a 1617 }
176bf572 1618
eeabee0a
NS
1619 /* Not found, try the canonical name. */
1620 canon = canonicalize_name (file_name);
8a3f457f
ML
1621 needle.name = canon;
1622 it = std::find (names.begin (), names.end (), needle);
1623 if (it == names.end ())
eeabee0a
NS
1624 {
1625 /* Not found with canonical name, create a new source. */
c7432e76 1626 source_info *src;
eeabee0a 1627
c7432e76 1628 idx = sources.size ();
8a3f457f
ML
1629 needle = name_map (canon, idx);
1630 names.push_back (needle);
eeabee0a 1631
c7432e76
ML
1632 sources.push_back (source_info ());
1633 src = &sources.back ();
eeabee0a 1634 src->name = canon;
1a9075e2 1635 src->coverage.name = src->name;
136ca74e 1636 src->index = idx;
1bec9caa
NS
1637 if (source_length
1638#if HAVE_DOS_BASED_FILE_SYSTEM
1639 /* You lose if separators don't match exactly in the
1640 prefix. */
1641 && !strncasecmp (source_prefix, src->coverage.name, source_length)
1642#else
1643 && !strncmp (source_prefix, src->coverage.name, source_length)
1644#endif
1645 && IS_DIR_SEPARATOR (src->coverage.name[source_length]))
1646 src->coverage.name += source_length + 1;
eeabee0a 1647 if (!stat (src->name, &status))
1a9075e2
TG
1648 src->file_time = status.st_mtime;
1649 }
eeabee0a 1650 else
8a3f457f 1651 idx = it->src;
eeabee0a 1652
8a3f457f
ML
1653 needle.name = file_name;
1654 if (std::find (names.begin (), names.end (), needle) == names.end ())
eeabee0a
NS
1655 {
1656 /* Append the non-canonical name. */
8a3f457f 1657 names.push_back (name_map (xstrdup (file_name), idx));
eeabee0a 1658 }
1a9075e2 1659
eeabee0a 1660 /* Resort the name map. */
8a3f457f 1661 std::sort (names.begin (), names.end ());
6e49961c 1662
eeabee0a
NS
1663 check_date:
1664 if (sources[idx].file_time > bbg_file_time)
1a9075e2
TG
1665 {
1666 static int info_emitted;
1667
efbb59b2 1668 fnotice (stderr, "%s:source file is newer than notes file '%s'\n",
eeabee0a 1669 file_name, bbg_file_name);
1a9075e2
TG
1670 if (!info_emitted)
1671 {
1672 fnotice (stderr,
7106478c 1673 "(the message is displayed only once per source file)\n");
1a9075e2
TG
1674 info_emitted = 1;
1675 }
eeabee0a 1676 sources[idx].file_time = 0;
1a9075e2 1677 }
94de45d9 1678
eeabee0a 1679 return idx;
27283c73
NS
1680}
1681
211bea6b 1682/* Read the notes file. Save functions to FUNCTIONS global vector. */
86144b75 1683
211bea6b 1684static void
f55ade6e 1685read_graph_file (void)
86144b75 1686{
94de45d9 1687 unsigned version;
4977bab6 1688 unsigned current_tag = 0;
7d63a2fa 1689 unsigned tag;
f55ade6e 1690
546d2adb 1691 if (!gcov_open (bbg_file_name, 1))
86144b75 1692 {
efbb59b2 1693 fnotice (stderr, "%s:cannot open notes file\n", bbg_file_name);
211bea6b 1694 return;
86144b75 1695 }
546d2adb 1696 bbg_file_time = gcov_time ();
160e2e4f 1697 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC))
b7c9bf28 1698 {
efbb59b2 1699 fnotice (stderr, "%s:not a gcov notes file\n", bbg_file_name);
546d2adb 1700 gcov_close ();
211bea6b 1701 return;
4977bab6 1702 }
b7c9bf28 1703
94de45d9
NS
1704 version = gcov_read_unsigned ();
1705 if (version != GCOV_VERSION)
4977bab6
ZW
1706 {
1707 char v[4], e[4];
f55ade6e 1708
330d2e2a
NS
1709 GCOV_UNSIGNED2STRING (v, version);
1710 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
1711
9e637a26 1712 fnotice (stderr, "%s:version '%.4s', prefer '%.4s'\n",
4977bab6
ZW
1713 bbg_file_name, v, e);
1714 }
dd486eb2 1715 bbg_stamp = gcov_read_unsigned ();
c74bd3fb 1716 bbg_cwd = xstrdup (gcov_read_string ());
93814e2d 1717 bbg_supports_has_unexecuted_blocks = gcov_read_unsigned ();
dd486eb2 1718
cb8758b2 1719 function_info *fn = NULL;
7d63a2fa 1720 while ((tag = gcov_read_unsigned ()))
4977bab6 1721 {
94de45d9 1722 unsigned length = gcov_read_unsigned ();
7d63a2fa 1723 gcov_position_t base = gcov_position ();
796621e8 1724
4977bab6 1725 if (tag == GCOV_TAG_FUNCTION)
b7c9bf28 1726 {
94de45d9 1727 char *function_name;
136ca74e 1728 unsigned ident;
10adac51 1729 unsigned lineno_checksum, cfg_checksum;
4977bab6 1730
796621e8 1731 ident = gcov_read_unsigned ();
10adac51
XDL
1732 lineno_checksum = gcov_read_unsigned ();
1733 cfg_checksum = gcov_read_unsigned ();
796621e8 1734 function_name = xstrdup (gcov_read_string ());
136ca74e 1735 unsigned artificial = gcov_read_unsigned ();
0790260e 1736 unsigned src_idx = find_source (gcov_read_string ());
136ca74e
ML
1737 unsigned start_line = gcov_read_unsigned ();
1738 unsigned start_column = gcov_read_unsigned ();
1739 unsigned end_line = gcov_read_unsigned ();
b8154717 1740 unsigned end_column = gcov_read_unsigned ();
f55ade6e 1741
cb8758b2 1742 fn = new function_info ();
211bea6b 1743 functions.push_back (fn);
59688303
ML
1744 ident_to_fn[ident] = fn;
1745
c8fda30f 1746 fn->m_name = function_name;
796621e8 1747 fn->ident = ident;
10adac51
XDL
1748 fn->lineno_checksum = lineno_checksum;
1749 fn->cfg_checksum = cfg_checksum;
1ce1b792 1750 fn->src = src_idx;
136ca74e
ML
1751 fn->start_line = start_line;
1752 fn->start_column = start_column;
1753 fn->end_line = end_line;
b8154717 1754 fn->end_column = end_column;
136ca74e 1755 fn->artificial = artificial;
4977bab6 1756
4977bab6 1757 current_tag = tag;
b7c9bf28 1758 }
4977bab6 1759 else if (fn && tag == GCOV_TAG_BLOCKS)
b7c9bf28 1760 {
0790260e 1761 if (!fn->blocks.empty ())
9e637a26 1762 fnotice (stderr, "%s:already seen blocks for '%s'\n",
c8fda30f 1763 bbg_file_name, fn->get_name ());
4977bab6 1764 else
0790260e 1765 fn->blocks.resize (gcov_read_unsigned ());
4977bab6
ZW
1766 }
1767 else if (fn && tag == GCOV_TAG_ARCS)
1768 {
94de45d9 1769 unsigned src = gcov_read_unsigned ();
9b9d6370 1770 fn->blocks[src].id = src;
330d2e2a 1771 unsigned num_dests = GCOV_TAG_ARCS_NUM (length);
bea002e9 1772 block_info *src_blk = &fn->blocks[src];
8919c0d9
NS
1773 unsigned mark_catches = 0;
1774 struct arc_info *arc;
4977bab6 1775
0790260e 1776 if (src >= fn->blocks.size () || fn->blocks[src].succ)
4977bab6 1777 goto corrupt;
f55ade6e 1778
4977bab6 1779 while (num_dests--)
b7c9bf28 1780 {
94de45d9
NS
1781 unsigned dest = gcov_read_unsigned ();
1782 unsigned flags = gcov_read_unsigned ();
f55ade6e 1783
0790260e 1784 if (dest >= fn->blocks.size ())
4977bab6 1785 goto corrupt;
232c80f2 1786 arc = XCNEW (arc_info);
f55ade6e 1787
4977bab6 1788 arc->dst = &fn->blocks[dest];
8919c0d9 1789 arc->src = src_blk;
f55ade6e 1790
4977bab6
ZW
1791 arc->count = 0;
1792 arc->count_valid = 0;
1793 arc->on_tree = !!(flags & GCOV_ARC_ON_TREE);
1794 arc->fake = !!(flags & GCOV_ARC_FAKE);
1795 arc->fall_through = !!(flags & GCOV_ARC_FALLTHROUGH);
f55ade6e 1796
8919c0d9
NS
1797 arc->succ_next = src_blk->succ;
1798 src_blk->succ = arc;
1799 src_blk->num_succ++;
f55ade6e 1800
4977bab6
ZW
1801 arc->pred_next = fn->blocks[dest].pred;
1802 fn->blocks[dest].pred = arc;
1803 fn->blocks[dest].num_pred++;
1804
27283c73
NS
1805 if (arc->fake)
1806 {
1807 if (src)
1808 {
1809 /* Exceptional exit from this function, the
1810 source block must be a call. */
1811 fn->blocks[src].is_call_site = 1;
1812 arc->is_call_non_return = 1;
8919c0d9 1813 mark_catches = 1;
27283c73
NS
1814 }
1815 else
1816 {
1817 /* Non-local return from a callee of this
176bf572 1818 function. The destination block is a setjmp. */
27283c73
NS
1819 arc->is_nonlocal_return = 1;
1820 fn->blocks[dest].is_nonlocal_return = 1;
1821 }
1822 }
f55ade6e 1823
4977bab6 1824 if (!arc->on_tree)
4464b977 1825 fn->counts.push_back (0);
b7c9bf28 1826 }
176bf572 1827
8919c0d9
NS
1828 if (mark_catches)
1829 {
1830 /* We have a fake exit from this block. The other
1831 non-fall through exits must be to catch handlers.
1832 Mark them as catch arcs. */
1833
1834 for (arc = src_blk->succ; arc; arc = arc->succ_next)
1835 if (!arc->fake && !arc->fall_through)
1836 {
1837 arc->is_throw = 1;
1838 fn->has_catch = 1;
1839 }
1840 }
4977bab6
ZW
1841 }
1842 else if (fn && tag == GCOV_TAG_LINES)
1843 {
94de45d9 1844 unsigned blockno = gcov_read_unsigned ();
bea002e9 1845 block_info *block = &fn->blocks[blockno];
4977bab6 1846
0790260e 1847 if (blockno >= fn->blocks.size ())
4977bab6 1848 goto corrupt;
f55ade6e 1849
0790260e 1850 while (true)
b7c9bf28 1851 {
94de45d9 1852 unsigned lineno = gcov_read_unsigned ();
f55ade6e 1853
4977bab6 1854 if (lineno)
0790260e 1855 block->locations.back ().lines.push_back (lineno);
4977bab6
ZW
1856 else
1857 {
94de45d9 1858 const char *file_name = gcov_read_string ();
f55ade6e 1859
4977bab6 1860 if (!file_name)
b7c9bf28 1861 break;
0790260e
ML
1862 block->locations.push_back (block_location_info
1863 (find_source (file_name)));
4977bab6 1864 }
b7c9bf28 1865 }
4977bab6
ZW
1866 }
1867 else if (current_tag && !GCOV_TAG_IS_SUBTAG (current_tag, tag))
1868 {
1869 fn = NULL;
1870 current_tag = 0;
1871 }
474f141e 1872 gcov_sync (base, length);
94de45d9 1873 if (gcov_is_error ())
00cf2913
NS
1874 {
1875 corrupt:;
1876 fnotice (stderr, "%s:corrupted\n", bbg_file_name);
1ce1b792 1877 break;
00cf2913 1878 }
b7c9bf28 1879 }
546d2adb 1880 gcov_close ();
f55ade6e 1881
211bea6b 1882 if (functions.empty ())
1ce1b792 1883 fnotice (stderr, "%s:no functions found\n", bbg_file_name);
b7c9bf28 1884}
86144b75 1885
4977bab6 1886/* Reads profiles from the count file and attach to each
272d0bee 1887 function. Return nonzero if fatal error. */
86144b75 1888
4977bab6 1889static int
211bea6b 1890read_count_file (void)
86144b75 1891{
4977bab6 1892 unsigned ix;
94de45d9 1893 unsigned version;
7d63a2fa 1894 unsigned tag;
cb8758b2 1895 function_info *fn = NULL;
7d63a2fa 1896 int error = 0;
59688303 1897 map<unsigned, function_info *>::iterator it;
4977bab6 1898
546d2adb 1899 if (!gcov_open (da_file_name, 1))
86144b75 1900 {
80b3502b
NS
1901 fnotice (stderr, "%s:cannot open data file, assuming not executed\n",
1902 da_file_name);
1903 no_data_file = 1;
1904 return 0;
4977bab6 1905 }
160e2e4f 1906 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
4977bab6
ZW
1907 {
1908 fnotice (stderr, "%s:not a gcov data file\n", da_file_name);
1909 cleanup:;
546d2adb 1910 gcov_close ();
4977bab6
ZW
1911 return 1;
1912 }
94de45d9
NS
1913 version = gcov_read_unsigned ();
1914 if (version != GCOV_VERSION)
4977bab6 1915 {
330d2e2a
NS
1916 char v[4], e[4];
1917
1918 GCOV_UNSIGNED2STRING (v, version);
1919 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
b8698a0f 1920
9e637a26 1921 fnotice (stderr, "%s:version '%.4s', prefer version '%.4s'\n",
330d2e2a 1922 da_file_name, v, e);
86144b75 1923 }
dd486eb2
NS
1924 tag = gcov_read_unsigned ();
1925 if (tag != bbg_stamp)
1926 {
efbb59b2 1927 fnotice (stderr, "%s:stamp mismatch with notes file\n", da_file_name);
dd486eb2
NS
1928 goto cleanup;
1929 }
f55ade6e 1930
7d63a2fa 1931 while ((tag = gcov_read_unsigned ()))
4977bab6 1932 {
94de45d9
NS
1933 unsigned length = gcov_read_unsigned ();
1934 unsigned long base = gcov_position ();
94de45d9 1935
512cc015 1936 if (tag == GCOV_TAG_OBJECT_SUMMARY)
86144b75 1937 {
5366b186
NS
1938 struct gcov_summary summary;
1939 gcov_read_summary (&summary);
512cc015 1940 object_runs = summary.runs;
5366b186
NS
1941 }
1942 else if (tag == GCOV_TAG_FUNCTION && !length)
1943 ; /* placeholder */
1944 else if (tag == GCOV_TAG_FUNCTION && length == GCOV_TAG_FUNCTION_LENGTH)
1945 {
1946 unsigned ident;
5366b186 1947 ident = gcov_read_unsigned ();
211bea6b 1948 fn = NULL;
59688303
ML
1949 it = ident_to_fn.find (ident);
1950 if (it != ident_to_fn.end ())
1951 fn = it->second;
4977bab6
ZW
1952
1953 if (!fn)
1954 ;
10adac51
XDL
1955 else if (gcov_read_unsigned () != fn->lineno_checksum
1956 || gcov_read_unsigned () != fn->cfg_checksum)
86144b75 1957 {
4977bab6 1958 mismatch:;
9e637a26 1959 fnotice (stderr, "%s:profile mismatch for '%s'\n",
c8fda30f 1960 da_file_name, fn->get_name ());
4977bab6
ZW
1961 goto cleanup;
1962 }
1963 }
cdb23767 1964 else if (tag == GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS) && fn)
4977bab6 1965 {
4464b977 1966 if (length != GCOV_TAG_COUNTER_LENGTH (fn->counts.size ()))
4977bab6 1967 goto mismatch;
f55ade6e 1968
4464b977 1969 for (ix = 0; ix != fn->counts.size (); ix++)
94de45d9
NS
1970 fn->counts[ix] += gcov_read_counter ();
1971 }
474f141e 1972 gcov_sync (base, length);
94de45d9 1973 if ((error = gcov_is_error ()))
00cf2913 1974 {
324ff1a0
JJ
1975 fnotice (stderr,
1976 error < 0
1977 ? N_("%s:overflowed\n")
1978 : N_("%s:corrupted\n"),
00cf2913
NS
1979 da_file_name);
1980 goto cleanup;
1981 }
7d63a2fa 1982 }
f55ade6e 1983
546d2adb 1984 gcov_close ();
4977bab6 1985 return 0;
86144b75
DE
1986}
1987
4977bab6
ZW
1988/* Solve the flow graph. Propagate counts from the instrumented arcs
1989 to the blocks and the uninstrumented arcs. */
86144b75
DE
1990
1991static void
cb8758b2 1992solve_flow_graph (function_info *fn)
86144b75 1993{
4977bab6 1994 unsigned ix;
232c80f2 1995 arc_info *arc;
4464b977 1996 gcov_type *count_ptr = &fn->counts.front ();
bea002e9
ML
1997 block_info *blk;
1998 block_info *valid_blocks = NULL; /* valid, but unpropagated blocks. */
1999 block_info *invalid_blocks = NULL; /* invalid, but inferable blocks. */
f55ade6e 2000
1ce1b792 2001 /* The arcs were built in reverse order. Fix that now. */
0790260e 2002 for (ix = fn->blocks.size (); ix--;)
1ce1b792 2003 {
232c80f2 2004 arc_info *arc_p, *arc_n;
1ce1b792
NS
2005
2006 for (arc_p = NULL, arc = fn->blocks[ix].succ; arc;
2007 arc_p = arc, arc = arc_n)
2008 {
2009 arc_n = arc->succ_next;
2010 arc->succ_next = arc_p;
2011 }
2012 fn->blocks[ix].succ = arc_p;
2013
2014 for (arc_p = NULL, arc = fn->blocks[ix].pred; arc;
2015 arc_p = arc, arc = arc_n)
2016 {
2017 arc_n = arc->pred_next;
2018 arc->pred_next = arc_p;
2019 }
2020 fn->blocks[ix].pred = arc_p;
2021 }
2022
0790260e 2023 if (fn->blocks.size () < 2)
9e637a26 2024 fnotice (stderr, "%s:'%s' lacks entry and/or exit blocks\n",
c8fda30f 2025 bbg_file_name, fn->get_name ());
4977bab6 2026 else
86144b75 2027 {
9696c529 2028 if (fn->blocks[ENTRY_BLOCK].num_pred)
9e637a26 2029 fnotice (stderr, "%s:'%s' has arcs to entry block\n",
c8fda30f 2030 bbg_file_name, fn->get_name ());
86144b75 2031 else
4977bab6
ZW
2032 /* We can't deduce the entry block counts from the lack of
2033 predecessors. */
9696c529 2034 fn->blocks[ENTRY_BLOCK].num_pred = ~(unsigned)0;
f55ade6e 2035
9696c529 2036 if (fn->blocks[EXIT_BLOCK].num_succ)
9e637a26 2037 fnotice (stderr, "%s:'%s' has arcs from exit block\n",
c8fda30f 2038 bbg_file_name, fn->get_name ());
4977bab6
ZW
2039 else
2040 /* Likewise, we can't deduce exit block counts from the lack
2041 of its successors. */
9696c529 2042 fn->blocks[EXIT_BLOCK].num_succ = ~(unsigned)0;
86144b75
DE
2043 }
2044
4977bab6
ZW
2045 /* Propagate the measured counts, this must be done in the same
2046 order as the code in profile.c */
0790260e 2047 for (unsigned i = 0; i < fn->blocks.size (); i++)
86144b75 2048 {
0790260e 2049 blk = &fn->blocks[i];
bea002e9 2050 block_info const *prev_dst = NULL;
4977bab6 2051 int out_of_order = 0;
27283c73 2052 int non_fake_succ = 0;
f55ade6e 2053
27283c73 2054 for (arc = blk->succ; arc; arc = arc->succ_next)
86144b75 2055 {
27283c73
NS
2056 if (!arc->fake)
2057 non_fake_succ++;
f55ade6e 2058
4977bab6 2059 if (!arc->on_tree)
86144b75 2060 {
4977bab6
ZW
2061 if (count_ptr)
2062 arc->count = *count_ptr++;
2063 arc->count_valid = 1;
27283c73 2064 blk->num_succ--;
4977bab6 2065 arc->dst->num_pred--;
86144b75 2066 }
4977bab6
ZW
2067 if (prev_dst && prev_dst > arc->dst)
2068 out_of_order = 1;
2069 prev_dst = arc->dst;
86144b75 2070 }
27283c73
NS
2071 if (non_fake_succ == 1)
2072 {
2073 /* If there is only one non-fake exit, it is an
2074 unconditional branch. */
2075 for (arc = blk->succ; arc; arc = arc->succ_next)
2076 if (!arc->fake)
2077 {
2078 arc->is_unconditional = 1;
2079 /* If this block is instrumenting a call, it might be
e0bb17a8 2080 an artificial block. It is not artificial if it has
10b7602f
NS
2081 a non-fallthrough exit, or the destination of this
2082 arc has more than one entry. Mark the destination
2083 block as a return site, if none of those conditions
2084 hold. */
2085 if (blk->is_call_site && arc->fall_through
2086 && arc->dst->pred == arc && !arc->pred_next)
2087 arc->dst->is_call_return = 1;
27283c73
NS
2088 }
2089 }
f55ade6e 2090
4977bab6
ZW
2091 /* Sort the successor arcs into ascending dst order. profile.c
2092 normally produces arcs in the right order, but sometimes with
2093 one or two out of order. We're not using a particularly
32dd366d 2094 smart sort. */
4977bab6 2095 if (out_of_order)
86144b75 2096 {
232c80f2 2097 arc_info *start = blk->succ;
4977bab6 2098 unsigned changes = 1;
f55ade6e 2099
4977bab6
ZW
2100 while (changes)
2101 {
232c80f2 2102 arc_info *arc, *arc_p, *arc_n;
f55ade6e 2103
4977bab6
ZW
2104 changes = 0;
2105 for (arc_p = NULL, arc = start; (arc_n = arc->succ_next);)
2106 {
2107 if (arc->dst > arc_n->dst)
2108 {
2109 changes = 1;
2110 if (arc_p)
2111 arc_p->succ_next = arc_n;
2112 else
2113 start = arc_n;
2114 arc->succ_next = arc_n->succ_next;
2115 arc_n->succ_next = arc;
2116 arc_p = arc_n;
2117 }
2118 else
2119 {
2120 arc_p = arc;
2121 arc = arc_n;
2122 }
2123 }
2124 }
27283c73 2125 blk->succ = start;
86144b75 2126 }
f55ade6e 2127
4977bab6
ZW
2128 /* Place it on the invalid chain, it will be ignored if that's
2129 wrong. */
27283c73
NS
2130 blk->invalid_chain = 1;
2131 blk->chain = invalid_blocks;
2132 invalid_blocks = blk;
4977bab6
ZW
2133 }
2134
2135 while (invalid_blocks || valid_blocks)
2136 {
4977bab6 2137 while ((blk = invalid_blocks))
86144b75 2138 {
4977bab6 2139 gcov_type total = 0;
232c80f2 2140 const arc_info *arc;
f55ade6e 2141
4977bab6
ZW
2142 invalid_blocks = blk->chain;
2143 blk->invalid_chain = 0;
2144 if (!blk->num_succ)
2145 for (arc = blk->succ; arc; arc = arc->succ_next)
2146 total += arc->count;
2147 else if (!blk->num_pred)
2148 for (arc = blk->pred; arc; arc = arc->pred_next)
2149 total += arc->count;
2150 else
2151 continue;
f55ade6e 2152
4977bab6
ZW
2153 blk->count = total;
2154 blk->count_valid = 1;
2155 blk->chain = valid_blocks;
2156 blk->valid_chain = 1;
2157 valid_blocks = blk;
86144b75 2158 }
4977bab6 2159 while ((blk = valid_blocks))
86144b75 2160 {
4977bab6 2161 gcov_type total;
232c80f2 2162 arc_info *arc, *inv_arc;
4977bab6
ZW
2163
2164 valid_blocks = blk->chain;
2165 blk->valid_chain = 0;
2166 if (blk->num_succ == 1)
2167 {
bea002e9 2168 block_info *dst;
f55ade6e 2169
4977bab6
ZW
2170 total = blk->count;
2171 inv_arc = NULL;
2172 for (arc = blk->succ; arc; arc = arc->succ_next)
2173 {
2174 total -= arc->count;
2175 if (!arc->count_valid)
2176 inv_arc = arc;
2177 }
2178 dst = inv_arc->dst;
2179 inv_arc->count_valid = 1;
2180 inv_arc->count = total;
2181 blk->num_succ--;
2182 dst->num_pred--;
2183 if (dst->count_valid)
2184 {
2185 if (dst->num_pred == 1 && !dst->valid_chain)
2186 {
2187 dst->chain = valid_blocks;
2188 dst->valid_chain = 1;
2189 valid_blocks = dst;
2190 }
2191 }
2192 else
2193 {
2194 if (!dst->num_pred && !dst->invalid_chain)
2195 {
2196 dst->chain = invalid_blocks;
2197 dst->invalid_chain = 1;
2198 invalid_blocks = dst;
2199 }
2200 }
2201 }
2202 if (blk->num_pred == 1)
2203 {
bea002e9 2204 block_info *src;
f55ade6e 2205
4977bab6
ZW
2206 total = blk->count;
2207 inv_arc = NULL;
2208 for (arc = blk->pred; arc; arc = arc->pred_next)
2209 {
2210 total -= arc->count;
2211 if (!arc->count_valid)
2212 inv_arc = arc;
2213 }
2214 src = inv_arc->src;
2215 inv_arc->count_valid = 1;
2216 inv_arc->count = total;
2217 blk->num_pred--;
2218 src->num_succ--;
2219 if (src->count_valid)
2220 {
2221 if (src->num_succ == 1 && !src->valid_chain)
2222 {
2223 src->chain = valid_blocks;
2224 src->valid_chain = 1;
2225 valid_blocks = src;
2226 }
2227 }
2228 else
2229 {
2230 if (!src->num_succ && !src->invalid_chain)
2231 {
2232 src->chain = invalid_blocks;
2233 src->invalid_chain = 1;
2234 invalid_blocks = src;
2235 }
2236 }
2237 }
86144b75
DE
2238 }
2239 }
f55ade6e 2240
4977bab6
ZW
2241 /* If the graph has been correctly solved, every block will have a
2242 valid count. */
0790260e
ML
2243 for (unsigned i = 0; ix < fn->blocks.size (); i++)
2244 if (!fn->blocks[i].count_valid)
4977bab6 2245 {
9e637a26 2246 fnotice (stderr, "%s:graph is unsolvable for '%s'\n",
c8fda30f 2247 bbg_file_name, fn->get_name ());
4977bab6
ZW
2248 break;
2249 }
86144b75 2250}
4977bab6 2251
8919c0d9
NS
2252/* Mark all the blocks only reachable via an incoming catch. */
2253
2254static void
cb8758b2 2255find_exception_blocks (function_info *fn)
8919c0d9
NS
2256{
2257 unsigned ix;
bea002e9 2258 block_info **queue = XALLOCAVEC (block_info *, fn->blocks.size ());
8919c0d9
NS
2259
2260 /* First mark all blocks as exceptional. */
0790260e 2261 for (ix = fn->blocks.size (); ix--;)
8919c0d9
NS
2262 fn->blocks[ix].exceptional = 1;
2263
2264 /* Now mark all the blocks reachable via non-fake edges */
0790260e 2265 queue[0] = &fn->blocks[0];
8919c0d9
NS
2266 queue[0]->exceptional = 0;
2267 for (ix = 1; ix;)
2268 {
bea002e9 2269 block_info *block = queue[--ix];
232c80f2 2270 const arc_info *arc;
176bf572 2271
8919c0d9
NS
2272 for (arc = block->succ; arc; arc = arc->succ_next)
2273 if (!arc->fake && !arc->is_throw && arc->dst->exceptional)
2274 {
2275 arc->dst->exceptional = 0;
2276 queue[ix++] = arc->dst;
2277 }
2278 }
2279}
86144b75 2280\f
86144b75 2281
4977bab6 2282/* Increment totals in COVERAGE according to arc ARC. */
8b219a76
NS
2283
2284static void
a1b5dd18 2285add_branch_counts (coverage_info *coverage, const arc_info *arc)
8b219a76 2286{
27283c73 2287 if (arc->is_call_non_return)
8b219a76 2288 {
4977bab6
ZW
2289 coverage->calls++;
2290 if (arc->src->count)
2291 coverage->calls_executed++;
8b219a76 2292 }
27283c73 2293 else if (!arc->is_unconditional)
8b219a76 2294 {
4977bab6
ZW
2295 coverage->branches++;
2296 if (arc->src->count)
2297 coverage->branches_executed++;
2298 if (arc->count)
2299 coverage->branches_taken++;
86144b75
DE
2300 }
2301}
2302
29a4ef18
ML
2303/* Format COUNT, if flag_human_readable_numbers is set, return it human
2304 readable format. */
2305
2306static char const *
2307format_count (gcov_type count)
2308{
2309 static char buffer[64];
2310 const char *units = " kMGTPEZY";
2311
2312 if (count < 1000 || !flag_human_readable_numbers)
2313 {
2314 sprintf (buffer, "%" PRId64, count);
2315 return buffer;
2316 }
2317
2318 unsigned i;
2319 gcov_type divisor = 1;
2320 for (i = 0; units[i+1]; i++, divisor *= 1000)
2321 {
2322 if (count + divisor / 2 < 1000 * divisor)
2323 break;
2324 }
d1c772b9
ML
2325 float r = 1.0f * count / divisor;
2326 sprintf (buffer, "%.1f%c", r, units[i]);
29a4ef18
ML
2327 return buffer;
2328}
2329
9696c529 2330/* Format a GCOV_TYPE integer as either a percent ratio, or absolute
977667e6
ML
2331 count. If DECIMAL_PLACES >= 0, format TOP/BOTTOM * 100 to DECIMAL_PLACES.
2332 If DECIMAL_PLACES is zero, no decimal point is printed. Only print 100% when
2333 TOP==BOTTOM and only print 0% when TOP=0. If DECIMAL_PLACES < 0, then simply
37b8715b
NS
2334 format TOP. Return pointer to a static string. */
2335
2336static char const *
977667e6 2337format_gcov (gcov_type top, gcov_type bottom, int decimal_places)
37b8715b
NS
2338{
2339 static char buffer[20];
f55ade6e 2340
977667e6 2341 if (decimal_places >= 0)
07ea19a7 2342 {
977667e6 2343 float ratio = bottom ? 100.0f * top / bottom: 0;
07ea19a7 2344
977667e6
ML
2345 /* Round up to 1% if there's a small non-zero value. */
2346 if (ratio > 0.0f && ratio < 0.5f && decimal_places == 0)
2347 ratio = 1.0f;
2348 sprintf (buffer, "%.*f%%", decimal_places, ratio);
37b8715b
NS
2349 }
2350 else
29a4ef18 2351 return format_count (top);
f55ade6e 2352
37b8715b
NS
2353 return buffer;
2354}
2355
bdbdc4e1 2356/* Summary of execution */
86144b75
DE
2357
2358static void
bdbdc4e1 2359executed_summary (unsigned lines, unsigned executed)
86144b75 2360{
bdbdc4e1 2361 if (lines)
4977bab6 2362 fnotice (stdout, "Lines executed:%s of %d\n",
bdbdc4e1 2363 format_gcov (executed, lines, 2), lines);
86144b75 2364 else
1457ebf9 2365 fnotice (stdout, "No executable lines\n");
bdbdc4e1 2366}
176bf572 2367
e4b52fca 2368/* Output summary info for a function. */
bdbdc4e1
NS
2369
2370static void
e4b52fca 2371function_summary (const coverage_info *coverage)
bdbdc4e1 2372{
e4b52fca
ML
2373 fnotice (stdout, "%s '%s'\n", "Function", coverage->name);
2374 executed_summary (coverage->lines, coverage->lines_executed);
2375}
2376
2377/* Output summary info for a file. */
2378
2379static void
2380file_summary (const coverage_info *coverage)
2381{
2382 fnotice (stdout, "%s '%s'\n", "File", coverage->name);
bdbdc4e1 2383 executed_summary (coverage->lines, coverage->lines_executed);
86144b75 2384
4977bab6 2385 if (flag_branches)
86144b75 2386 {
4977bab6 2387 if (coverage->branches)
86144b75 2388 {
4977bab6
ZW
2389 fnotice (stdout, "Branches executed:%s of %d\n",
2390 format_gcov (coverage->branches_executed,
2391 coverage->branches, 2),
2392 coverage->branches);
2393 fnotice (stdout, "Taken at least once:%s of %d\n",
2394 format_gcov (coverage->branches_taken,
2395 coverage->branches, 2),
2396 coverage->branches);
86144b75
DE
2397 }
2398 else
4977bab6
ZW
2399 fnotice (stdout, "No branches\n");
2400 if (coverage->calls)
2401 fnotice (stdout, "Calls executed:%s of %d\n",
2402 format_gcov (coverage->calls_executed, coverage->calls, 2),
2403 coverage->calls);
86144b75 2404 else
4977bab6 2405 fnotice (stdout, "No calls\n");
8b219a76
NS
2406 }
2407}
2408
eeabee0a
NS
2409/* Canonicalize the filename NAME by canonicalizing directory
2410 separators, eliding . components and resolving .. components
2411 appropriately. Always returns a unique string. */
2412
2413static char *
2414canonicalize_name (const char *name)
2415{
2416 /* The canonical name cannot be longer than the incoming name. */
2417 char *result = XNEWVEC (char, strlen (name) + 1);
2418 const char *base = name, *probe;
2419 char *ptr = result;
2420 char *dd_base;
2421 int slash = 0;
2422
2423#if HAVE_DOS_BASED_FILE_SYSTEM
2424 if (base[0] && base[1] == ':')
2425 {
2426 result[0] = base[0];
2427 result[1] = ':';
2428 base += 2;
2429 ptr += 2;
2430 }
2431#endif
2432 for (dd_base = ptr; *base; base = probe)
2433 {
2434 size_t len;
176bf572 2435
eeabee0a
NS
2436 for (probe = base; *probe; probe++)
2437 if (IS_DIR_SEPARATOR (*probe))
2438 break;
2439
2440 len = probe - base;
2441 if (len == 1 && base[0] == '.')
2442 /* Elide a '.' directory */
2443 ;
2444 else if (len == 2 && base[0] == '.' && base[1] == '.')
2445 {
2446 /* '..', we can only elide it and the previous directory, if
2447 we're not a symlink. */
8c121ccb
NS
2448 struct stat ATTRIBUTE_UNUSED buf;
2449
eeabee0a 2450 *ptr = 0;
8c121ccb
NS
2451 if (dd_base == ptr
2452#if defined (S_ISLNK)
2453 /* S_ISLNK is not POSIX.1-1996. */
2454 || stat (result, &buf) || S_ISLNK (buf.st_mode)
2455#endif
176bf572 2456 )
eeabee0a
NS
2457 {
2458 /* Cannot elide, or unreadable or a symlink. */
2459 dd_base = ptr + 2 + slash;
2460 goto regular;
2461 }
2462 while (ptr != dd_base && *ptr != '/')
2463 ptr--;
2464 slash = ptr != result;
2465 }
2466 else
2467 {
2468 regular:
2469 /* Regular pathname component. */
2470 if (slash)
2471 *ptr++ = '/';
2472 memcpy (ptr, base, len);
2473 ptr += len;
2474 slash = 1;
2475 }
2476
2477 for (; IS_DIR_SEPARATOR (*probe); probe++)
2478 continue;
2479 }
2480 *ptr = 0;
2481
2482 return result;
2483}
2484
75cc66f2
ML
2485/* Print hex representation of 16 bytes from SUM and write it to BUFFER. */
2486
2487static void
2488md5sum_to_hex (const char *sum, char *buffer)
2489{
2490 for (unsigned i = 0; i < 16; i++)
62a76702 2491 sprintf (buffer + (2 * i), "%02x", (unsigned char)sum[i]);
75cc66f2
ML
2492}
2493
eeabee0a
NS
2494/* Generate an output file name. INPUT_NAME is the canonicalized main
2495 input file and SRC_NAME is the canonicalized file name.
2496 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
8b219a76
NS
2497 long_output_names we prepend the processed name of the input file
2498 to each output name (except when the current source file is the
2499 input file, so you don't get a double concatenation). The two
eeabee0a
NS
2500 components are separated by '##'. With preserve_paths we create a
2501 filename from all path components of the source file, replacing '/'
2502 with '#', and .. with '^', without it we simply take the basename
2503 component. (Remember, the canonicalized name will already have
2504 elided '.' components and converted \\ separators.) */
8b219a76
NS
2505
2506static char *
f55ade6e 2507make_gcov_file_name (const char *input_name, const char *src_name)
8b219a76 2508{
eeabee0a
NS
2509 char *ptr;
2510 char *result;
f55ade6e 2511
1a9075e2 2512 if (flag_long_names && input_name && strcmp (src_name, input_name))
8b219a76
NS
2513 {
2514 /* Generate the input filename part. */
eeabee0a 2515 result = XNEWVEC (char, strlen (input_name) + strlen (src_name) + 10);
176bf572 2516
eeabee0a
NS
2517 ptr = result;
2518 ptr = mangle_name (input_name, ptr);
2519 ptr[0] = ptr[1] = '#';
2520 ptr += 2;
8b219a76 2521 }
1a9075e2
TG
2522 else
2523 {
eeabee0a
NS
2524 result = XNEWVEC (char, strlen (src_name) + 10);
2525 ptr = result;
1a9075e2 2526 }
f55ade6e 2527
eeabee0a
NS
2528 ptr = mangle_name (src_name, ptr);
2529 strcpy (ptr, ".gcov");
176bf572 2530
75cc66f2
ML
2531 /* When hashing filenames, we shorten them by only using the filename
2532 component and appending a hash of the full (mangled) pathname. */
2533 if (flag_hash_filenames)
2534 {
2535 md5_ctx ctx;
2536 char md5sum[16];
2537 char md5sum_hex[33];
2538
2539 md5_init_ctx (&ctx);
62a76702 2540 md5_process_bytes (src_name, strlen (src_name), &ctx);
75cc66f2
ML
2541 md5_finish_ctx (&ctx, md5sum);
2542 md5sum_to_hex (md5sum, md5sum_hex);
2543 free (result);
2544
2545 result = XNEWVEC (char, strlen (src_name) + 50);
2546 ptr = result;
2547 ptr = mangle_name (src_name, ptr);
2548 ptr[0] = ptr[1] = '#';
2549 ptr += 2;
2550 memcpy (ptr, md5sum_hex, 32);
2551 ptr += 32;
2552 strcpy (ptr, ".gcov");
2553 }
2554
eeabee0a
NS
2555 return result;
2556}
f55ade6e 2557
12502bf2
ML
2558/* Mangle BASE name, copy it at the beginning of PTR buffer and
2559 return address of the \0 character of the buffer. */
2560
eeabee0a
NS
2561static char *
2562mangle_name (char const *base, char *ptr)
2563{
2564 size_t len;
176bf572 2565
eeabee0a
NS
2566 /* Generate the source filename part. */
2567 if (!flag_preserve_paths)
12502bf2 2568 base = lbasename (base);
eeabee0a 2569 else
12502bf2
ML
2570 base = mangle_path (base);
2571
2572 len = strlen (base);
2573 memcpy (ptr, base, len);
2574 ptr += len;
176bf572 2575
eeabee0a 2576 return ptr;
86144b75
DE
2577}
2578
4977bab6 2579/* Scan through the bb_data for each line in the block, increment
8b219a76
NS
2580 the line number execution count indicated by the execution count of
2581 the appropriate basic block. */
86144b75
DE
2582
2583static void
a1b5dd18 2584add_line_counts (coverage_info *coverage, function_info *fn)
86144b75 2585{
6e49961c 2586 bool has_any_line = false;
32dd366d 2587 /* Scan each basic block. */
6e49961c 2588 for (unsigned ix = 0; ix != fn->blocks.size (); ix++)
86144b75 2589 {
4695d816 2590 line_info *line = NULL;
bea002e9 2591 block_info *block = &fn->blocks[ix];
0790260e 2592 if (block->count && ix && ix + 1 != fn->blocks.size ())
27283c73 2593 fn->blocks_executed++;
0790260e
ML
2594 for (unsigned i = 0; i < block->locations.size (); i++)
2595 {
136ca74e 2596 unsigned src_idx = block->locations[i].source_file_idx;
0790260e 2597 vector<unsigned> &lines = block->locations[i].lines;
136ca74e
ML
2598
2599 block->cycle.arc = NULL;
2600 block->cycle.ident = ~0U;
2601
0790260e
ML
2602 for (unsigned j = 0; j < lines.size (); j++)
2603 {
136ca74e
ML
2604 unsigned ln = lines[j];
2605
2606 /* Line belongs to a function that is in a group. */
2607 if (fn->group_line_p (ln, src_idx))
0790260e 2608 {
136ca74e
ML
2609 gcc_assert (lines[j] - fn->start_line < fn->lines.size ());
2610 line = &(fn->lines[lines[j] - fn->start_line]);
2611 line->exists = 1;
2612 if (!block->exceptional)
2613 {
2614 line->unexceptional = 1;
2615 if (block->count == 0)
2616 line->has_unexecuted_block = 1;
2617 }
2618 line->count += block->count;
0790260e 2619 }
136ca74e 2620 else
cdb07de7 2621 {
136ca74e
ML
2622 gcc_assert (ln < sources[src_idx].lines.size ());
2623 line = &(sources[src_idx].lines[ln]);
2624 if (coverage)
2625 {
2626 if (!line->exists)
2627 coverage->lines++;
2628 if (!line->count && block->count)
2629 coverage->lines_executed++;
2630 }
2631 line->exists = 1;
2632 if (!block->exceptional)
2633 {
2634 line->unexceptional = 1;
2635 if (block->count == 0)
2636 line->has_unexecuted_block = 1;
2637 }
2638 line->count += block->count;
cdb07de7 2639 }
0790260e 2640 }
f55ade6e 2641
136ca74e 2642 has_any_line = true;
f55ade6e 2643
136ca74e
ML
2644 if (!ix || ix + 1 == fn->blocks.size ())
2645 /* Entry or exit block. */;
2646 else if (line != NULL)
86144b75 2647 {
136ca74e 2648 line->blocks.push_back (block);
6e49961c 2649
136ca74e 2650 if (flag_branches)
6e49961c 2651 {
232c80f2 2652 arc_info *arc;
136ca74e
ML
2653
2654 for (arc = block->succ; arc; arc = arc->succ_next)
2655 line->branches.push_back (arc);
6e49961c 2656 }
86144b75 2657 }
8b219a76
NS
2658 }
2659 }
6e49961c
ML
2660
2661 if (!has_any_line)
c8fda30f
ML
2662 fnotice (stderr, "%s:no lines for '%s'\n", bbg_file_name,
2663 fn->get_name ());
4977bab6
ZW
2664}
2665
136ca74e
ML
2666/* Accumulate info for LINE that belongs to SRC source file. If ADD_COVERAGE
2667 is set to true, update source file summary. */
2668
2669static void accumulate_line_info (line_info *line, source_info *src,
2670 bool add_coverage)
2671{
2672 if (add_coverage)
2673 for (vector<arc_info *>::iterator it = line->branches.begin ();
2674 it != line->branches.end (); it++)
2675 add_branch_counts (&src->coverage, *it);
2676
2677 if (!line->blocks.empty ())
2678 {
2679 /* The user expects the line count to be the number of times
2680 a line has been executed. Simply summing the block count
2681 will give an artificially high number. The Right Thing
2682 is to sum the entry counts to the graph of blocks on this
2683 line, then find the elementary cycles of the local graph
2684 and add the transition counts of those cycles. */
2685 gcov_type count = 0;
2686
2687 /* Cycle detection. */
bea002e9 2688 for (vector<block_info *>::iterator it = line->blocks.begin ();
136ca74e
ML
2689 it != line->blocks.end (); it++)
2690 {
232c80f2 2691 for (arc_info *arc = (*it)->pred; arc; arc = arc->pred_next)
136ca74e
ML
2692 if (!line->has_block (arc->src))
2693 count += arc->count;
232c80f2 2694 for (arc_info *arc = (*it)->succ; arc; arc = arc->succ_next)
136ca74e
ML
2695 arc->cs_count = arc->count;
2696 }
2697
2698 /* Now, add the count of loops entirely on this line. */
2699 count += get_cycles_count (*line);
2700 line->count = count;
d6683f89
ML
2701
2702 if (line->count > src->maximum_count)
2703 src->maximum_count = line->count;
136ca74e
ML
2704 }
2705
2706 if (line->exists && add_coverage)
2707 {
2708 src->coverage.lines++;
2709 if (line->count)
2710 src->coverage.lines_executed++;
2711 }
2712}
2713
32dd366d 2714/* Accumulate the line counts of a file. */
4977bab6
ZW
2715
2716static void
c7432e76 2717accumulate_line_counts (source_info *src)
4977bab6 2718{
136ca74e 2719 /* First work on group functions. */
cb8758b2 2720 for (vector<function_info *>::iterator it = src->functions.begin ();
136ca74e 2721 it != src->functions.end (); it++)
27283c73 2722 {
136ca74e
ML
2723 function_info *fn = *it;
2724
2725 if (fn->src != src->index || !fn->is_group)
2726 continue;
2727
2728 for (vector<line_info>::iterator it2 = fn->lines.begin ();
2729 it2 != fn->lines.end (); it2++)
2730 {
2731 line_info *line = &(*it2);
2732 accumulate_line_info (line, src, false);
2733 }
27283c73 2734 }
f55ade6e 2735
136ca74e
ML
2736 /* Work on global lines that line in source file SRC. */
2737 for (vector<line_info>::iterator it = src->lines.begin ();
2738 it != src->lines.end (); it++)
2739 accumulate_line_info (&(*it), src, true);
86144b75 2740
136ca74e
ML
2741 /* If not using intermediate mode, sum lines of group functions and
2742 add them to lines that live in a source file. */
c8fda30f 2743 if (!flag_json_format)
cb8758b2 2744 for (vector<function_info *>::iterator it = src->functions.begin ();
136ca74e
ML
2745 it != src->functions.end (); it++)
2746 {
2747 function_info *fn = *it;
10b7602f 2748
136ca74e
ML
2749 if (fn->src != src->index || !fn->is_group)
2750 continue;
10b7602f 2751
136ca74e
ML
2752 for (unsigned i = 0; i < fn->lines.size (); i++)
2753 {
2754 line_info *fn_line = &fn->lines[i];
2755 if (fn_line->exists)
2756 {
2757 unsigned ln = fn->start_line + i;
2758 line_info *src_line = &src->lines[ln];
f55ade6e 2759
136ca74e
ML
2760 if (!src_line->exists)
2761 src->coverage.lines++;
2762 if (!src_line->count && fn_line->count)
2763 src->coverage.lines_executed++;
c7432e76 2764
136ca74e
ML
2765 src_line->count += fn_line->count;
2766 src_line->exists = 1;
2767
2768 if (fn_line->has_unexecuted_block)
2769 src_line->has_unexecuted_block = 1;
2770
2771 if (fn_line->unexceptional)
2772 src_line->unexceptional = 1;
2773 }
2774 }
2775 }
8b219a76 2776}
86144b75 2777
61ada8ae 2778/* Output information about ARC number IX. Returns nonzero if
27283c73
NS
2779 anything is output. */
2780
2781static int
232c80f2 2782output_branch_count (FILE *gcov_file, int ix, const arc_info *arc)
27283c73 2783{
27283c73
NS
2784 if (arc->is_call_non_return)
2785 {
2786 if (arc->src->count)
2787 {
2788 fnotice (gcov_file, "call %2d returned %s\n", ix,
2789 format_gcov (arc->src->count - arc->count,
2790 arc->src->count, -flag_counts));
2791 }
2792 else
2793 fnotice (gcov_file, "call %2d never executed\n", ix);
2794 }
2795 else if (!arc->is_unconditional)
2796 {
2797 if (arc->src->count)
9b9d6370 2798 fnotice (gcov_file, "branch %2d taken %s%s", ix,
27283c73 2799 format_gcov (arc->count, arc->src->count, -flag_counts),
8919c0d9
NS
2800 arc->fall_through ? " (fallthrough)"
2801 : arc->is_throw ? " (throw)" : "");
27283c73 2802 else
9b9d6370
ML
2803 fnotice (gcov_file, "branch %2d never executed", ix);
2804
2805 if (flag_verbose)
2806 fnotice (gcov_file, " (BB %d)", arc->dst->id);
2807
2808 fnotice (gcov_file, "\n");
27283c73 2809 }
10b7602f 2810 else if (flag_unconditional && !arc->dst->is_call_return)
27283c73
NS
2811 {
2812 if (arc->src->count)
2813 fnotice (gcov_file, "unconditional %2d taken %s\n", ix,
2814 format_gcov (arc->count, arc->src->count, -flag_counts));
2815 else
2816 fnotice (gcov_file, "unconditional %2d never executed\n", ix);
2817 }
2818 else
2819 return 0;
2820 return 1;
27283c73
NS
2821}
2822
8f961b22
NS
2823static const char *
2824read_line (FILE *file)
2825{
2826 static char *string;
2827 static size_t string_len;
2828 size_t pos = 0;
2829 char *ptr;
2830
2831 if (!string_len)
2832 {
2833 string_len = 200;
2834 string = XNEWVEC (char, string_len);
2835 }
2836
2837 while ((ptr = fgets (string + pos, string_len - pos, file)))
2838 {
2839 size_t len = strlen (string + pos);
2840
62a76702 2841 if (len && string[pos + len - 1] == '\n')
8f961b22
NS
2842 {
2843 string[pos + len - 1] = 0;
2844 return string;
2845 }
2846 pos += len;
62a76702
BE
2847 /* If the file contains NUL characters or an incomplete
2848 last line, which can happen more than once in one run,
2849 we have to avoid doubling the STRING_LEN unnecessarily. */
2850 if (pos > string_len / 2)
2851 {
2852 string_len *= 2;
2853 string = XRESIZEVEC (char, string, string_len);
2854 }
8f961b22 2855 }
176bf572 2856
8f961b22
NS
2857 return pos ? string : NULL;
2858}
2859
28f4a4a8
ML
2860/* Pad string S with spaces from left to have total width equal to 9. */
2861
2862static void
2863pad_count_string (string &s)
2864{
2865 if (s.size () < 9)
2866 s.insert (0, 9 - s.size (), ' ');
2867}
2868
2869/* Print GCOV line beginning to F stream. If EXISTS is set to true, the
2870 line exists in source file. UNEXCEPTIONAL indicated that it's not in
2871 an exceptional statement. The output is printed for LINE_NUM of given
2872 COUNT of executions. EXCEPTIONAL_STRING and UNEXCEPTIONAL_STRING are
2873 used to indicate non-executed blocks. */
2874
2875static void
2876output_line_beginning (FILE *f, bool exists, bool unexceptional,
cdb07de7 2877 bool has_unexecuted_block,
28f4a4a8
ML
2878 gcov_type count, unsigned line_num,
2879 const char *exceptional_string,
d6683f89
ML
2880 const char *unexceptional_string,
2881 unsigned int maximum_count)
28f4a4a8
ML
2882{
2883 string s;
2884 if (exists)
2885 {
2886 if (count > 0)
2887 {
2888 s = format_gcov (count, 0, -1);
93814e2d
ML
2889 if (has_unexecuted_block
2890 && bbg_supports_has_unexecuted_blocks)
cdb07de7
ML
2891 {
2892 if (flag_use_colors)
2893 {
2894 pad_count_string (s);
136ca74e
ML
2895 s.insert (0, SGR_SEQ (COLOR_BG_MAGENTA
2896 COLOR_SEPARATOR COLOR_FG_WHITE));
cdb07de7
ML
2897 s += SGR_RESET;
2898 }
2899 else
2900 s += "*";
2901 }
28f4a4a8
ML
2902 pad_count_string (s);
2903 }
2904 else
2905 {
2906 if (flag_use_colors)
2907 {
2908 s = "0";
2909 pad_count_string (s);
2910 if (unexceptional)
2911 s.insert (0, SGR_SEQ (COLOR_BG_RED
2912 COLOR_SEPARATOR COLOR_FG_WHITE));
2913 else
2914 s.insert (0, SGR_SEQ (COLOR_BG_CYAN
2915 COLOR_SEPARATOR COLOR_FG_WHITE));
2916 s += SGR_RESET;
2917 }
2918 else
2919 {
2920 s = unexceptional ? unexceptional_string : exceptional_string;
2921 pad_count_string (s);
2922 }
2923 }
2924 }
2925 else
2926 {
2927 s = "-";
2928 pad_count_string (s);
2929 }
2930
d6683f89
ML
2931 /* Format line number in output. */
2932 char buffer[16];
2933 sprintf (buffer, "%5u", line_num);
2934 string linestr (buffer);
2935
2936 if (flag_use_hotness_colors && maximum_count)
2937 {
2938 if (count * 2 > maximum_count) /* > 50%. */
2939 linestr.insert (0, SGR_SEQ (COLOR_BG_RED));
2940 else if (count * 5 > maximum_count) /* > 20%. */
2941 linestr.insert (0, SGR_SEQ (COLOR_BG_YELLOW));
2942 else if (count * 10 > maximum_count) /* > 10%. */
2943 linestr.insert (0, SGR_SEQ (COLOR_BG_GREEN));
2944 linestr += SGR_RESET;
2945 }
2946
2947 fprintf (f, "%s:%s", s.c_str (), linestr.c_str ());
28f4a4a8
ML
2948}
2949
136ca74e
ML
2950static void
2951print_source_line (FILE *f, const vector<const char *> &source_lines,
2952 unsigned line)
2953{
2954 gcc_assert (line >= 1);
2955 gcc_assert (line <= source_lines.size ());
2956
2957 fprintf (f, ":%s\n", source_lines[line - 1]);
2958}
2959
2960/* Output line details for LINE and print it to F file. LINE lives on
2961 LINE_NUM. */
2962
2963static void
2964output_line_details (FILE *f, const line_info *line, unsigned line_num)
2965{
2966 if (flag_all_blocks)
2967 {
232c80f2 2968 arc_info *arc;
136ca74e
ML
2969 int ix, jx;
2970
2971 ix = jx = 0;
bea002e9 2972 for (vector<block_info *>::const_iterator it = line->blocks.begin ();
136ca74e
ML
2973 it != line->blocks.end (); it++)
2974 {
2975 if (!(*it)->is_call_return)
2976 {
2977 output_line_beginning (f, line->exists,
2978 (*it)->exceptional, false,
2979 (*it)->count, line_num,
d6683f89 2980 "%%%%%", "$$$$$", 0);
136ca74e
ML
2981 fprintf (f, "-block %2d", ix++);
2982 if (flag_verbose)
2983 fprintf (f, " (BB %u)", (*it)->id);
2984 fprintf (f, "\n");
2985 }
2986 if (flag_branches)
2987 for (arc = (*it)->succ; arc; arc = arc->succ_next)
2988 jx += output_branch_count (f, jx, arc);
2989 }
2990 }
2991 else if (flag_branches)
2992 {
2993 int ix;
2994
2995 ix = 0;
232c80f2 2996 for (vector<arc_info *>::const_iterator it = line->branches.begin ();
136ca74e
ML
2997 it != line->branches.end (); it++)
2998 ix += output_branch_count (f, ix, (*it));
2999 }
3000}
3001
3002/* Output detail statistics about function FN to file F. */
3003
3004static void
c8fda30f 3005output_function_details (FILE *f, function_info *fn)
136ca74e
ML
3006{
3007 if (!flag_branches)
3008 return;
3009
232c80f2 3010 arc_info *arc = fn->blocks[EXIT_BLOCK].pred;
136ca74e
ML
3011 gcov_type return_count = fn->blocks[EXIT_BLOCK].count;
3012 gcov_type called_count = fn->blocks[ENTRY_BLOCK].count;
3013
3014 for (; arc; arc = arc->pred_next)
3015 if (arc->fake)
3016 return_count -= arc->count;
3017
c8fda30f 3018 fprintf (f, "function %s", fn->get_name ());
136ca74e
ML
3019 fprintf (f, " called %s",
3020 format_gcov (called_count, 0, -1));
3021 fprintf (f, " returned %s",
3022 format_gcov (return_count, called_count, 0));
3023 fprintf (f, " blocks executed %s",
c8fda30f 3024 format_gcov (fn->blocks_executed, fn->get_block_count (), 0));
136ca74e
ML
3025 fprintf (f, "\n");
3026}
3027
8b219a76
NS
3028/* Read in the source file one line at a time, and output that line to
3029 the gcov file preceded by its execution count and other
3030 information. */
86144b75 3031
8b219a76 3032static void
c7432e76 3033output_lines (FILE *gcov_file, const source_info *src)
8b219a76 3034{
28f4a4a8 3035#define DEFAULT_LINE_START " -: 0:"
136ca74e 3036#define FN_SEPARATOR "------------------\n"
28f4a4a8 3037
8b219a76 3038 FILE *source_file;
136ca74e 3039 const char *retval;
4977bab6 3040
e595522a
ML
3041 /* Print colorization legend. */
3042 if (flag_use_colors)
3043 fprintf (gcov_file, "%s",
3044 DEFAULT_LINE_START "Colorization: profile count: " \
3045 SGR_SEQ (COLOR_BG_CYAN) "zero coverage (exceptional)" SGR_RESET \
3046 " " \
3047 SGR_SEQ (COLOR_BG_RED) "zero coverage (unexceptional)" SGR_RESET \
3048 " " \
3049 SGR_SEQ (COLOR_BG_MAGENTA) "unexecuted block" SGR_RESET "\n");
3050
d6683f89 3051 if (flag_use_hotness_colors)
e595522a
ML
3052 fprintf (gcov_file, "%s",
3053 DEFAULT_LINE_START "Colorization: line numbers: hotness: " \
d6683f89
ML
3054 SGR_SEQ (COLOR_BG_RED) "> 50%" SGR_RESET " " \
3055 SGR_SEQ (COLOR_BG_YELLOW) "> 20%" SGR_RESET " " \
3056 SGR_SEQ (COLOR_BG_GREEN) "> 10%" SGR_RESET "\n");
3057
28f4a4a8 3058 fprintf (gcov_file, DEFAULT_LINE_START "Source:%s\n", src->coverage.name);
1a9075e2
TG
3059 if (!multiple_files)
3060 {
28f4a4a8
ML
3061 fprintf (gcov_file, DEFAULT_LINE_START "Graph:%s\n", bbg_file_name);
3062 fprintf (gcov_file, DEFAULT_LINE_START "Data:%s\n",
1a9075e2 3063 no_data_file ? "-" : da_file_name);
28f4a4a8 3064 fprintf (gcov_file, DEFAULT_LINE_START "Runs:%u\n", object_runs);
1a9075e2 3065 }
f55ade6e 3066
4977bab6 3067 source_file = fopen (src->name, "r");
8b219a76 3068 if (!source_file)
136ca74e 3069 fnotice (stderr, "Cannot open source file %s\n", src->name);
1a9075e2 3070 else if (src->file_time == 0)
28f4a4a8 3071 fprintf (gcov_file, DEFAULT_LINE_START "Source is newer than graph\n");
8b219a76 3072
136ca74e
ML
3073 vector<const char *> source_lines;
3074 if (source_file)
3075 while ((retval = read_line (source_file)) != NULL)
3076 source_lines.push_back (xstrdup (retval));
f5d39c3d 3077
136ca74e 3078 unsigned line_start_group = 0;
59688303 3079 vector<function_info *> *fns;
136ca74e
ML
3080
3081 for (unsigned line_num = 1; line_num <= source_lines.size (); line_num++)
8b219a76 3082 {
136ca74e 3083 if (line_num >= src->lines.size ())
27283c73 3084 {
136ca74e
ML
3085 fprintf (gcov_file, "%9s:%5u", "-", line_num);
3086 print_source_line (gcov_file, source_lines, line_num);
3087 continue;
27283c73 3088 }
f55ade6e 3089
136ca74e
ML
3090 const line_info *line = &src->lines[line_num];
3091
3092 if (line_start_group == 0)
3093 {
3094 fns = src->get_functions_at_location (line_num);
59688303 3095 if (fns != NULL && fns->size () > 1)
e59b5e24
ML
3096 {
3097 /* It's possible to have functions that partially overlap,
3098 thus take the maximum end_line of functions starting
3099 at LINE_NUM. */
59688303
ML
3100 for (unsigned i = 0; i < fns->size (); i++)
3101 if ((*fns)[i]->end_line > line_start_group)
3102 line_start_group = (*fns)[i]->end_line;
e59b5e24 3103 }
59688303 3104 else if (fns != NULL && fns->size () == 1)
136ca74e 3105 {
59688303 3106 function_info *fn = (*fns)[0];
136ca74e
ML
3107 output_function_details (gcov_file, fn);
3108 }
3109 }
8f961b22 3110
8b219a76 3111 /* For lines which don't exist in the .bb file, print '-' before
f55ade6e 3112 the source line. For lines which exist but were never
8f961b22
NS
3113 executed, print '#####' or '=====' before the source line.
3114 Otherwise, print the execution count before the source line.
3115 There are 16 spaces of indentation added before the source
3116 line so that tabs won't be messed up. */
28f4a4a8 3117 output_line_beginning (gcov_file, line->exists, line->unexceptional,
136ca74e 3118 line->has_unexecuted_block, line->count,
d6683f89 3119 line_num, "=====", "#####", src->maximum_count);
27283c73 3120
136ca74e
ML
3121 print_source_line (gcov_file, source_lines, line_num);
3122 output_line_details (gcov_file, line, line_num);
f55ade6e 3123
136ca74e
ML
3124 if (line_start_group == line_num)
3125 {
59688303
ML
3126 for (vector<function_info *>::iterator it = fns->begin ();
3127 it != fns->end (); it++)
37b8715b 3128 {
136ca74e
ML
3129 function_info *fn = *it;
3130 vector<line_info> &lines = fn->lines;
3131
3132 fprintf (gcov_file, FN_SEPARATOR);
3133
c8fda30f 3134 string fn_name = fn->get_name ();
136ca74e 3135 if (flag_use_colors)
9b9d6370 3136 {
136ca74e
ML
3137 fn_name.insert (0, SGR_SEQ (COLOR_FG_CYAN));
3138 fn_name += SGR_RESET;
3139 }
3140
3141 fprintf (gcov_file, "%s:\n", fn_name.c_str ());
3142
3143 output_function_details (gcov_file, fn);
3144
3145 /* Print all lines covered by the function. */
3146 for (unsigned i = 0; i < lines.size (); i++)
3147 {
3148 line_info *line = &lines[i];
3149 unsigned l = fn->start_line + i;
3150
3151 /* For lines which don't exist in the .bb file, print '-'
3152 before the source line. For lines which exist but
3153 were never executed, print '#####' or '=====' before
3154 the source line. Otherwise, print the execution count
3155 before the source line.
3156 There are 16 spaces of indentation added before the source
3157 line so that tabs won't be messed up. */
28f4a4a8 3158 output_line_beginning (gcov_file, line->exists,
136ca74e
ML
3159 line->unexceptional,
3160 line->has_unexecuted_block,
3161 line->count,
d6683f89
ML
3162 l, "=====", "#####",
3163 src->maximum_count);
136ca74e
ML
3164
3165 print_source_line (gcov_file, source_lines, l);
3166 output_line_details (gcov_file, line, l);
9b9d6370 3167 }
86144b75 3168 }
f55ade6e 3169
136ca74e
ML
3170 fprintf (gcov_file, FN_SEPARATOR);
3171 line_start_group = 0;
27283c73 3172 }
8b219a76 3173 }
f55ade6e 3174
8b219a76
NS
3175 if (source_file)
3176 fclose (source_file);
3177}