]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gcov.c
* gcc.dg/tree-ssa/pr77445-2.c: Quote brackets.
[thirdparty/gcc.git] / gcc / gcov.c
CommitLineData
86144b75
DE
1/* Gcov.c: prepend line execution counts and branch probabilities to a
2 source file.
cbe34bb5 3 Copyright (C) 1990-2017 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"
b04cd507 34#include "system.h"
4977bab6
ZW
35#include "coretypes.h"
36#include "tm.h"
ab87f8c8 37#include "intl.h"
2691e6d7 38#include "diagnostic.h"
5735c3ea 39#include "version.h"
fc8a650e 40#include "demangle.h"
86144b75 41
5735c3ea
JM
42#include <getopt.h>
43
24bc5921
ML
44#include <vector>
45#include <algorithm>
75cc66f2 46#include "md5.h"
24bc5921
ML
47
48using namespace std;
49
546d2adb 50#define IN_GCOV 1
86144b75 51#include "gcov-io.h"
d79f9ec9 52#include "gcov-io.c"
86144b75 53
1a9075e2 54/* The gcno file is generated by -ftest-coverage option. The gcda file is
4977bab6
ZW
55 generated by a program compiled with -fprofile-arcs. Their formats
56 are documented in gcov-io.h. */
86144b75
DE
57
58/* The functions in this file for creating and solution program flow graphs
4977bab6
ZW
59 are very similar to functions in the gcc source file profile.c. In
60 some places we make use of the knowledge of how profile.c works to
61 select particular algorithms here. */
86144b75 62
10adac51
XDL
63/* The code validates that the profile information read in corresponds
64 to the code currently being compiled. Rather than checking for
9696c529 65 identical files, the code below compares a checksum on the CFG
10adac51 66 (based on the order of basic blocks and the arcs in the CFG). If
9696c529
SB
67 the CFG checksum in the gcda file match the CFG checksum in the
68 gcno file, the profile data will be used. */
10adac51 69
86144b75
DE
70/* This is the size of the buffer used to read in source file lines. */
71
4977bab6
ZW
72struct function_info;
73struct block_info;
27283c73 74struct source_info;
86144b75 75
4977bab6 76/* Describes an arc between two basic blocks. */
86144b75 77
4977bab6
ZW
78typedef struct arc_info
79{
32dd366d 80 /* source and destination blocks. */
4977bab6
ZW
81 struct block_info *src;
82 struct block_info *dst;
86144b75 83
4977bab6
ZW
84 /* transition counts. */
85 gcov_type count;
3d7ca167
ZD
86 /* used in cycle search, so that we do not clobber original counts. */
87 gcov_type cs_count;
86144b75 88
86144b75
DE
89 unsigned int count_valid : 1;
90 unsigned int on_tree : 1;
91 unsigned int fake : 1;
92 unsigned int fall_through : 1;
86144b75 93
8919c0d9
NS
94 /* Arc to a catch handler. */
95 unsigned int is_throw : 1;
96
27283c73
NS
97 /* Arc is for a function that abnormally returns. */
98 unsigned int is_call_non_return : 1;
99
fa10beec 100 /* Arc is for catch/setjmp. */
27283c73
NS
101 unsigned int is_nonlocal_return : 1;
102
103 /* Is an unconditional branch. */
104 unsigned int is_unconditional : 1;
105
10b7602f
NS
106 /* Loop making arc. */
107 unsigned int cycle : 1;
108
4977bab6
ZW
109 /* Next branch on line. */
110 struct arc_info *line_next;
f55ade6e 111
4977bab6
ZW
112 /* Links to next arc on src and dst lists. */
113 struct arc_info *succ_next;
114 struct arc_info *pred_next;
115} arc_t;
86144b75 116
4977bab6
ZW
117/* Describes a basic block. Contains lists of arcs to successor and
118 predecessor blocks. */
86144b75 119
4977bab6 120typedef struct block_info
86144b75 121{
4977bab6
ZW
122 /* Chain of exit and entry arcs. */
123 arc_t *succ;
124 arc_t *pred;
125
32dd366d 126 /* Number of unprocessed exit and entry arcs. */
4977bab6
ZW
127 gcov_type num_succ;
128 gcov_type num_pred;
129
32dd366d 130 /* Block execution count. */
4977bab6 131 gcov_type count;
8919c0d9 132 unsigned flags : 12;
4977bab6
ZW
133 unsigned count_valid : 1;
134 unsigned valid_chain : 1;
135 unsigned invalid_chain : 1;
8919c0d9 136 unsigned exceptional : 1;
4977bab6 137
27283c73 138 /* Block is a call instrumenting site. */
10b7602f
NS
139 unsigned is_call_site : 1; /* Does the call. */
140 unsigned is_call_return : 1; /* Is the return. */
4977bab6 141
27283c73
NS
142 /* Block is a landing pad for longjmp or throw. */
143 unsigned is_nonlocal_return : 1;
144
145 union
146 {
147 struct
148 {
149 /* Array of line numbers and source files. source files are
150 introduced by a linenumber of zero, the next 'line number' is
151 the number of the source file. Always starts with a source
152 file. */
153 unsigned *encoding;
154 unsigned num;
155 } line; /* Valid until blocks are linked onto lines */
156 struct
157 {
10b7602f 158 /* Single line graph cycle workspace. Used for all-blocks
71c0e7fc 159 mode. */
10b7602f
NS
160 arc_t *arc;
161 unsigned ident;
162 } cycle; /* Used in all-blocks mode, after blocks are linked onto
71c0e7fc 163 lines. */
27283c73
NS
164 } u;
165
166 /* Temporary chain for solving graph, and for chaining blocks on one
167 line. */
4977bab6 168 struct block_info *chain;
f55ade6e 169
4977bab6 170} block_t;
86144b75 171
4977bab6 172/* Describes a single function. Contains an array of basic blocks. */
86144b75 173
4977bab6 174typedef struct function_info
8b219a76 175{
4977bab6
ZW
176 /* Name of function. */
177 char *name;
fc8a650e 178 char *demangled_name;
796621e8 179 unsigned ident;
10adac51
XDL
180 unsigned lineno_checksum;
181 unsigned cfg_checksum;
86144b75 182
8919c0d9
NS
183 /* The graph contains at least one fake incoming edge. */
184 unsigned has_catch : 1;
185
9696c529
SB
186 /* Array of basic blocks. Like in GCC, the entry block is
187 at blocks[0] and the exit block is at blocks[1]. */
188#define ENTRY_BLOCK (0)
189#define EXIT_BLOCK (1)
4977bab6
ZW
190 block_t *blocks;
191 unsigned num_blocks;
27283c73 192 unsigned blocks_executed;
4977bab6
ZW
193
194 /* Raw arc coverage counts. */
195 gcov_type *counts;
196 unsigned num_counts;
27283c73 197
1ce1b792 198 /* First line number & file. */
27283c73 199 unsigned line;
1ce1b792 200 unsigned src;
27283c73
NS
201
202 /* Next function in same source file. */
4457451f 203 struct function_info *next_file_fn;
f55ade6e 204
4977bab6
ZW
205 /* Next function. */
206 struct function_info *next;
207} function_t;
208
209/* Describes coverage of a file or function. */
210
211typedef struct coverage_info
8b219a76
NS
212{
213 int lines;
214 int lines_executed;
f55ade6e 215
8b219a76
NS
216 int branches;
217 int branches_executed;
218 int branches_taken;
f55ade6e 219
8b219a76
NS
220 int calls;
221 int calls_executed;
f55ade6e 222
8b219a76 223 char *name;
4977bab6 224} coverage_t;
8b219a76 225
4977bab6
ZW
226/* Describes a single line of source. Contains a chain of basic blocks
227 with code on it. */
86144b75 228
4977bab6
ZW
229typedef struct line_info
230{
24bc5921
ML
231 /* Return true when NEEDLE is one of basic blocks the line belongs to. */
232 bool has_block (block_t *needle);
233
4977bab6 234 gcov_type count; /* execution count */
27283c73
NS
235 union
236 {
f55ade6e 237 arc_t *branches; /* branches from blocks that end on this
27283c73 238 line. Used for branch-counts when not
71c0e7fc 239 all-blocks mode. */
27283c73 240 block_t *blocks; /* blocks which start on this line. Used
71c0e7fc 241 in all-blocks mode. */
27283c73 242 } u;
4977bab6 243 unsigned exists : 1;
8919c0d9 244 unsigned unexceptional : 1;
4977bab6 245} line_t;
86144b75 246
24bc5921
ML
247bool
248line_t::has_block (block_t *needle)
249{
250 for (block_t *n = u.blocks; n; n = n->chain)
251 if (n == needle)
252 return true;
253
254 return false;
255}
256
4977bab6
ZW
257/* Describes a file mentioned in the block graph. Contains an array
258 of line info. */
37b8715b 259
4977bab6
ZW
260typedef struct source_info
261{
eeabee0a 262 /* Canonical name of source file. */
4977bab6 263 char *name;
1a9075e2 264 time_t file_time;
37b8715b 265
32dd366d 266 /* Array of line information. */
4977bab6
ZW
267 line_t *lines;
268 unsigned num_lines;
86144b75 269
4977bab6 270 coverage_t coverage;
27283c73
NS
271
272 /* Functions in this source file. These are in ascending line
273 number order. */
274 function_t *functions;
4977bab6 275} source_t;
86144b75 276
eeabee0a
NS
277typedef struct name_map
278{
279 char *name; /* Source file name */
280 unsigned src; /* Source file */
281} name_map_t;
282
4977bab6 283/* Holds a list of function basic block graphs. */
86144b75 284
4977bab6 285static function_t *functions;
1ce1b792 286static function_t **fn_end = &functions;
86144b75 287
1ce1b792
NS
288static source_t *sources; /* Array of source files */
289static unsigned n_sources; /* Number of sources */
290static unsigned a_sources; /* Allocated sources */
1a9075e2 291
eeabee0a
NS
292static name_map_t *names; /* Mapping of file names to sources */
293static unsigned n_names; /* Number of names */
294static unsigned a_names; /* Allocated names */
295
27283c73
NS
296/* This holds data summary information. */
297
5366b186 298static unsigned object_runs;
27283c73
NS
299static unsigned program_count;
300
bdbdc4e1
NS
301static unsigned total_lines;
302static unsigned total_executed;
303
32dd366d 304/* Modification time of graph file. */
86144b75 305
4977bab6 306static time_t bbg_file_time;
86144b75 307
efbb59b2
SB
308/* Name of the notes (gcno) output file. The "bbg" prefix is for
309 historical reasons, when the notes file contained only the
310 basic block graph notes. */
86144b75 311
4977bab6 312static char *bbg_file_name;
86144b75 313
dd486eb2
NS
314/* Stamp of the bbg file */
315static unsigned bbg_stamp;
316
efbb59b2 317/* Name and file pointer of the input file for the count data (gcda). */
86144b75 318
4977bab6 319static char *da_file_name;
86144b75 320
80b3502b
NS
321/* Data file is missing. */
322
323static int no_data_file;
324
1a9075e2
TG
325/* If there is several input files, compute and display results after
326 reading all data files. This way if two or more gcda file refer to
327 the same source file (eg inline subprograms in a .h file), the
328 counts are added. */
329
330static int multiple_files = 0;
331
4977bab6 332/* Output branch probabilities. */
86144b75 333
4977bab6 334static int flag_branches = 0;
86144b75 335
f55ade6e 336/* Show unconditional branches too. */
27283c73
NS
337static int flag_unconditional = 0;
338
86144b75
DE
339/* Output a gcov file if this is true. This is on by default, and can
340 be turned off by the -n option. */
341
4977bab6 342static int flag_gcov_file = 1;
86144b75 343
acdb4da7
NS
344/* Output progress indication if this is true. This is off by default
345 and can be turned on by the -d option. */
346
347static int flag_display_progress = 0;
348
fc8a650e
SS
349/* Output *.gcov file in intermediate format used by 'lcov'. */
350
351static int flag_intermediate_format = 0;
352
353/* Output demangled function names. */
354
355static int flag_demangled_names = 0;
356
4977bab6
ZW
357/* For included files, make the gcov output file name include the name
358 of the input source file. For example, if x.h is included in a.c,
359 then the output file name is a.c##x.h.gcov instead of x.h.gcov. */
86144b75 360
4977bab6 361static int flag_long_names = 0;
86144b75 362
75cc66f2
ML
363/* For situations when a long name can potentially hit filesystem path limit,
364 let's calculate md5sum of the path and append it to a file name. */
365
366static int flag_hash_filenames = 0;
367
27283c73
NS
368/* Output count information for every basic block, not merely those
369 that contain line number information. */
370
371static int flag_all_blocks = 0;
372
86144b75
DE
373/* Output summary info for each function. */
374
4977bab6 375static int flag_function_summary = 0;
86144b75 376
4977bab6
ZW
377/* Object directory file prefix. This is the directory/file where the
378 graph and data files are looked for, if nonzero. */
86144b75
DE
379
380static char *object_directory = 0;
381
1bec9caa
NS
382/* Source directory prefix. This is removed from source pathnames
383 that match, when generating the output file name. */
384
385static char *source_prefix = 0;
386static size_t source_length = 0;
387
388/* Only show data for sources with relative pathnames. Absolute ones
389 usually indicate a system header file, which although it may
390 contain inline functions, is usually uninteresting. */
391static int flag_relative_only = 0;
392
37b8715b 393/* Preserve all pathname components. Needed when object files and
4977bab6
ZW
394 source files are in subdirectories. '/' is mangled as '#', '.' is
395 elided and '..' mangled to '^'. */
396
397static int flag_preserve_paths = 0;
37b8715b 398
8bfa6fc5 399/* Output the number of times a branch was taken as opposed to the percentage
32dd366d 400 of times it was taken. */
23190837 401
4977bab6 402static int flag_counts = 0;
8bfa6fc5 403
86144b75 404/* Forward declarations. */
f55ade6e
AJ
405static int process_args (int, char **);
406static void print_usage (int) ATTRIBUTE_NORETURN;
407static void print_version (void) ATTRIBUTE_NORETURN;
408static void process_file (const char *);
1a9075e2 409static void generate_results (const char *);
f55ade6e 410static void create_file_names (const char *);
eeabee0a
NS
411static int name_search (const void *, const void *);
412static int name_sort (const void *, const void *);
413static char *canonicalize_name (const char *);
1ce1b792
NS
414static unsigned find_source (const char *);
415static function_t *read_graph_file (void);
416static int read_count_file (function_t *);
f55ade6e 417static void solve_flow_graph (function_t *);
8919c0d9 418static void find_exception_blocks (function_t *);
f55ade6e
AJ
419static void add_branch_counts (coverage_t *, const arc_t *);
420static void add_line_counts (coverage_t *, function_t *);
bdbdc4e1 421static void executed_summary (unsigned, unsigned);
f55ade6e
AJ
422static void function_summary (const coverage_t *, const char *);
423static const char *format_gcov (gcov_type, gcov_type, int);
424static void accumulate_line_counts (source_t *);
c3284718 425static void output_gcov_file (const char *, source_t *);
f55ade6e
AJ
426static int output_branch_count (FILE *, int, const arc_t *);
427static void output_lines (FILE *, const source_t *);
428static char *make_gcov_file_name (const char *, const char *);
eeabee0a 429static char *mangle_name (const char *, char *);
f55ade6e 430static void release_structures (void);
5366b186 431static void release_function (function_t *);
f55ade6e 432extern int main (int, char **);
86144b75 433
24bc5921
ML
434/* Cycle detection!
435 There are a bajillion algorithms that do this. Boost's function is named
436 hawick_cycles, so I used the algorithm by K. A. Hawick and H. A. James in
437 "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
438 (url at <http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf>).
439
440 The basic algorithm is simple: effectively, we're finding all simple paths
441 in a subgraph (that shrinks every iteration). Duplicates are filtered by
442 "blocking" a path when a node is added to the path (this also prevents non-
443 simple paths)--the node is unblocked only when it participates in a cycle.
444 */
445
446typedef vector<arc_t *> arc_vector_t;
447typedef vector<const block_t *> block_vector_t;
448
449/* Enum with types of loop in CFG. */
450
451enum loop_type
452{
453 NO_LOOP = 0,
454 LOOP = 1,
455 NEGATIVE_LOOP = 3
456};
457
458/* Loop_type operator that merges two values: A and B. */
459
460inline loop_type& operator |= (loop_type& a, loop_type b)
461{
462 return a = static_cast<loop_type> (a | b);
463}
464
465/* Handle cycle identified by EDGES, where the function finds minimum cs_count
466 and subtract the value from all counts. The subtracted value is added
467 to COUNT. Returns type of loop. */
468
469static loop_type
470handle_cycle (const arc_vector_t &edges, int64_t &count)
471{
472 /* Find the minimum edge of the cycle, and reduce all nodes in the cycle by
473 that amount. */
4ab65208 474 int64_t cycle_count = INTTYPE_MAXIMUM (int64_t);
24bc5921
ML
475 for (unsigned i = 0; i < edges.size (); i++)
476 {
477 int64_t ecount = edges[i]->cs_count;
478 if (cycle_count > ecount)
479 cycle_count = ecount;
480 }
481 count += cycle_count;
482 for (unsigned i = 0; i < edges.size (); i++)
483 edges[i]->cs_count -= cycle_count;
484
485 return cycle_count < 0 ? NEGATIVE_LOOP : LOOP;
486}
487
488/* Unblock a block U from BLOCKED. Apart from that, iterate all blocks
489 blocked by U in BLOCK_LISTS. */
490
491static void
492unblock (const block_t *u, block_vector_t &blocked,
493 vector<block_vector_t > &block_lists)
494{
495 block_vector_t::iterator it = find (blocked.begin (), blocked.end (), u);
496 if (it == blocked.end ())
497 return;
498
499 unsigned index = it - blocked.begin ();
500 blocked.erase (it);
501
502 for (block_vector_t::iterator it2 = block_lists[index].begin ();
503 it2 != block_lists[index].end (); it2++)
504 unblock (*it2, blocked, block_lists);
505 for (unsigned j = 0; j < block_lists[index].size (); j++)
506 unblock (u, blocked, block_lists);
507
508 block_lists.erase (block_lists.begin () + index);
509}
510
511/* Find circuit going to block V, PATH is provisional seen cycle.
512 BLOCKED is vector of blocked vertices, BLOCK_LISTS contains vertices
513 blocked by a block. COUNT is accumulated count of the current LINE.
514 Returns what type of loop it contains. */
515
516static loop_type
517circuit (block_t *v, arc_vector_t &path, block_t *start,
518 block_vector_t &blocked, vector<block_vector_t> &block_lists,
519 line_t &linfo, int64_t &count)
520{
521 loop_type result = NO_LOOP;
522
523 /* Add v to the block list. */
524 gcc_assert (find (blocked.begin (), blocked.end (), v) == blocked.end ());
525 blocked.push_back (v);
526 block_lists.push_back (block_vector_t ());
527
528 for (arc_t *arc = v->succ; arc; arc = arc->succ_next)
529 {
530 block_t *w = arc->dst;
531 if (w < start || !linfo.has_block (w))
532 continue;
533
534 path.push_back (arc);
535 if (w == start)
536 /* Cycle has been found. */
537 result |= handle_cycle (path, count);
538 else if (find (blocked.begin (), blocked.end (), w) == blocked.end ())
539 result |= circuit (w, path, start, blocked, block_lists, linfo, count);
540
541 path.pop_back ();
542 }
543
544 if (result != NO_LOOP)
545 unblock (v, blocked, block_lists);
546 else
547 for (arc_t *arc = v->succ; arc; arc = arc->succ_next)
548 {
549 block_t *w = arc->dst;
550 if (w < start || !linfo.has_block (w))
551 continue;
552
553 size_t index
554 = find (blocked.begin (), blocked.end (), w) - blocked.begin ();
555 gcc_assert (index < blocked.size ());
556 block_vector_t &list = block_lists[index];
557 if (find (list.begin (), list.end (), v) == list.end ())
558 list.push_back (v);
559 }
560
561 return result;
562}
563
564/* Find cycles for a LINFO. If HANDLE_NEGATIVE_CYCLES is set and the line
565 contains a negative loop, then perform the same function once again. */
566
567static gcov_type
568get_cycles_count (line_t &linfo, bool handle_negative_cycles = true)
569{
570 /* Note that this algorithm works even if blocks aren't in sorted order.
571 Each iteration of the circuit detection is completely independent
572 (except for reducing counts, but that shouldn't matter anyways).
573 Therefore, operating on a permuted order (i.e., non-sorted) only
574 has the effect of permuting the output cycles. */
575
576 loop_type result = NO_LOOP;
577 gcov_type count = 0;
578 for (block_t *block = linfo.u.blocks; block; block = block->chain)
579 {
580 arc_vector_t path;
581 block_vector_t blocked;
582 vector<block_vector_t > block_lists;
583 result |= circuit (block, path, block, blocked, block_lists, linfo,
584 count);
585 }
586
587 /* If we have a negative cycle, repeat the find_cycles routine. */
588 if (result == NEGATIVE_LOOP && handle_negative_cycles)
589 count += get_cycles_count (linfo, false);
590
591 return count;
592}
593
86144b75 594int
f55ade6e 595main (int argc, char **argv)
86144b75 596{
4977bab6 597 int argno;
acdb4da7 598 int first_arg;
2691e6d7
JM
599 const char *p;
600
601 p = argv[0] + strlen (argv[0]);
602 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
603 --p;
604 progname = p;
605
606 xmalloc_set_program_name (progname);
f55ade6e 607
98a3dad4 608 /* Unlock the stdio streams. */
2653bb0c 609 unlock_std_streams ();
98a3dad4 610
191bf464 611 gcc_init_libintl ();
ab87f8c8 612
2691e6d7
JM
613 diagnostic_initialize (global_dc, 0);
614
e3536b82
TG
615 /* Handle response files. */
616 expandargv (&argc, &argv);
617
84ec53b6
NS
618 a_names = 10;
619 names = XNEWVEC (name_map_t, a_names);
620 a_sources = 10;
621 sources = XNEWVEC (source_t, a_sources);
176bf572 622
4977bab6
ZW
623 argno = process_args (argc, argv);
624 if (optind == argc)
625 print_usage (true);
86144b75 626
1a9075e2
TG
627 if (argc - argno > 1)
628 multiple_files = 1;
629
acdb4da7 630 first_arg = argno;
176bf572 631
4977bab6 632 for (; argno != argc; argno++)
acdb4da7
NS
633 {
634 if (flag_display_progress)
176bf572
ML
635 printf ("Processing file %d out of %d\n", argno - first_arg + 1,
636 argc - first_arg);
acdb4da7
NS
637 process_file (argv[argno]);
638 }
f55ade6e 639
1a9075e2
TG
640 generate_results (multiple_files ? NULL : argv[argc - 1]);
641
642 release_structures ();
f55ade6e 643
86144b75
DE
644 return 0;
645}
86144b75 646\f
5735c3ea
JM
647/* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
648 otherwise the output of --help. */
86144b75
DE
649
650static void
f55ade6e 651print_usage (int error_p)
86144b75 652{
5735c3ea
JM
653 FILE *file = error_p ? stderr : stdout;
654 int status = error_p ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
f55ade6e 655
eeabee0a 656 fnotice (file, "Usage: gcov [OPTION]... SOURCE|OBJ...\n\n");
5735c3ea
JM
657 fnotice (file, "Print code coverage information.\n\n");
658 fnotice (file, " -h, --help Print this help, then exit\n");
27283c73 659 fnotice (file, " -a, --all-blocks Show information for every basic block\n");
5735c3ea 660 fnotice (file, " -b, --branch-probabilities Include branch probabilities in output\n");
fc8a650e 661 fnotice (file, " -c, --branch-counts Output counts of branches taken\n\
5735c3ea 662 rather than percentages\n");
fc8a650e
SS
663 fnotice (file, " -d, --display-progress Display progress information\n");
664 fnotice (file, " -f, --function-summaries Output summaries for each function\n");
665 fnotice (file, " -i, --intermediate-format Output .gcov file in intermediate text format\n");
5735c3ea
JM
666 fnotice (file, " -l, --long-file-names Use long output file names for included\n\
667 source files\n");
fc8a650e
SS
668 fnotice (file, " -m, --demangled-names Output demangled function names\n");
669 fnotice (file, " -n, --no-output Do not create an output file\n");
37b8715b
NS
670 fnotice (file, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
671 fnotice (file, " -p, --preserve-paths Preserve all pathname components\n");
fc8a650e
SS
672 fnotice (file, " -r, --relative-only Only show data for relative sources\n");
673 fnotice (file, " -s, --source-prefix DIR Source prefix to elide\n");
27283c73 674 fnotice (file, " -u, --unconditional-branches Show unconditional branch counts too\n");
fc8a650e 675 fnotice (file, " -v, --version Print version number, then exit\n");
75cc66f2 676 fnotice (file, " -x, --hash-filenames Hash long pathnames\n");
5735c3ea 677 fnotice (file, "\nFor bug reporting instructions, please see:\n%s.\n",
a976603e 678 bug_report_url);
5735c3ea
JM
679 exit (status);
680}
681
682/* Print version information and exit. */
683
684static void
f55ade6e 685print_version (void)
5735c3ea 686{
2f41c1d6 687 fnotice (stdout, "gcov %s%s\n", pkgversion_string, version_string);
c1a9c369 688 fprintf (stdout, "Copyright %s 2017 Free Software Foundation, Inc.\n",
9f76f909 689 _("(C)"));
5735c3ea 690 fnotice (stdout,
9f76f909
KH
691 _("This is free software; see the source for copying conditions.\n"
692 "There is NO warranty; not even for MERCHANTABILITY or \n"
693 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
5735c3ea 694 exit (SUCCESS_EXIT_CODE);
86144b75
DE
695}
696
5735c3ea
JM
697static const struct option options[] =
698{
699 { "help", no_argument, NULL, 'h' },
700 { "version", no_argument, NULL, 'v' },
27283c73 701 { "all-blocks", no_argument, NULL, 'a' },
5735c3ea
JM
702 { "branch-probabilities", no_argument, NULL, 'b' },
703 { "branch-counts", no_argument, NULL, 'c' },
fc8a650e 704 { "intermediate-format", no_argument, NULL, 'i' },
5735c3ea
JM
705 { "no-output", no_argument, NULL, 'n' },
706 { "long-file-names", no_argument, NULL, 'l' },
707 { "function-summaries", no_argument, NULL, 'f' },
fc8a650e 708 { "demangled-names", no_argument, NULL, 'm' },
37b8715b 709 { "preserve-paths", no_argument, NULL, 'p' },
1bec9caa 710 { "relative-only", no_argument, NULL, 'r' },
37b8715b
NS
711 { "object-directory", required_argument, NULL, 'o' },
712 { "object-file", required_argument, NULL, 'o' },
1bec9caa 713 { "source-prefix", required_argument, NULL, 's' },
27283c73 714 { "unconditional-branches", no_argument, NULL, 'u' },
acdb4da7 715 { "display-progress", no_argument, NULL, 'd' },
75cc66f2 716 { "hash-filenames", no_argument, NULL, 'x' },
d90f9882 717 { 0, 0, 0, 0 }
5735c3ea
JM
718};
719
4977bab6 720/* Process args, return index to first non-arg. */
86144b75 721
4977bab6 722static int
f55ade6e 723process_args (int argc, char **argv)
86144b75 724{
5735c3ea 725 int opt;
86144b75 726
75cc66f2
ML
727 const char *opts = "abcdfhilmno:prs:uvx";
728 while ((opt = getopt_long (argc, argv, opts, options, NULL)) != -1)
86144b75 729 {
5735c3ea 730 switch (opt)
86144b75 731 {
27283c73
NS
732 case 'a':
733 flag_all_blocks = 1;
734 break;
5735c3ea 735 case 'b':
4977bab6 736 flag_branches = 1;
5735c3ea
JM
737 break;
738 case 'c':
4977bab6 739 flag_counts = 1;
5735c3ea 740 break;
27283c73
NS
741 case 'f':
742 flag_function_summary = 1;
5735c3ea 743 break;
27283c73
NS
744 case 'h':
745 print_usage (false);
746 /* print_usage will exit. */
5735c3ea 747 case 'l':
4977bab6 748 flag_long_names = 1;
5735c3ea 749 break;
fc8a650e
SS
750 case 'm':
751 flag_demangled_names = 1;
752 break;
27283c73
NS
753 case 'n':
754 flag_gcov_file = 0;
5735c3ea
JM
755 break;
756 case 'o':
757 object_directory = optarg;
758 break;
1bec9caa
NS
759 case 's':
760 source_prefix = optarg;
761 source_length = strlen (source_prefix);
762 break;
763 case 'r':
764 flag_relative_only = 1;
765 break;
37b8715b 766 case 'p':
4977bab6 767 flag_preserve_paths = 1;
37b8715b 768 break;
27283c73
NS
769 case 'u':
770 flag_unconditional = 1;
771 break;
fc8a650e
SS
772 case 'i':
773 flag_intermediate_format = 1;
774 flag_gcov_file = 1;
775 break;
acdb4da7
NS
776 case 'd':
777 flag_display_progress = 1;
778 break;
27283c73
NS
779 case 'v':
780 print_version ();
75cc66f2
ML
781 case 'x':
782 flag_hash_filenames = 1;
783 break;
27283c73 784 /* print_version will exit. */
5735c3ea
JM
785 default:
786 print_usage (true);
787 /* print_usage will exit. */
86144b75 788 }
86144b75
DE
789 }
790
4977bab6
ZW
791 return optind;
792}
793
fc8a650e
SS
794/* Get the name of the gcov file. The return value must be free'd.
795
796 It appends the '.gcov' extension to the *basename* of the file.
797 The resulting file name will be in PWD.
798
799 e.g.,
800 input: foo.da, output: foo.da.gcov
801 input: a/b/foo.cc, output: foo.cc.gcov */
802
803static char *
804get_gcov_intermediate_filename (const char *file_name)
805{
806 const char *gcov = ".gcov";
807 char *result;
808 const char *cptr;
809
810 /* Find the 'basename'. */
811 cptr = lbasename (file_name);
812
c3284718 813 result = XNEWVEC (char, strlen (cptr) + strlen (gcov) + 1);
fc8a650e
SS
814 sprintf (result, "%s%s", cptr, gcov);
815
816 return result;
817}
818
819/* Output the result in intermediate format used by 'lcov'.
820
821The intermediate format contains a single file named 'foo.cc.gcov',
822with no source code included. A sample output is
823
824file:foo.cc
825function:5,1,_Z3foov
826function:13,1,main
827function:19,1,_GLOBAL__sub_I__Z3foov
828function:19,1,_Z41__static_initialization_and_destruction_0ii
829lcount:5,1
830lcount:7,9
831lcount:9,8
832lcount:11,1
833file:/.../iostream
834lcount:74,1
835file:/.../basic_ios.h
836file:/.../ostream
837file:/.../ios_base.h
838function:157,0,_ZStorSt12_Ios_IostateS_
839lcount:157,0
840file:/.../char_traits.h
841function:258,0,_ZNSt11char_traitsIcE6lengthEPKc
842lcount:258,0
843...
844
845The default gcov outputs multiple files: 'foo.cc.gcov',
846'iostream.gcov', 'ios_base.h.gcov', etc. with source code
847included. Instead the intermediate format here outputs only a single
848file 'foo.cc.gcov' similar to the above example. */
849
850static void
851output_intermediate_file (FILE *gcov_file, source_t *src)
852{
853 unsigned line_num; /* current line number. */
854 const line_t *line; /* current line info ptr. */
855 function_t *fn; /* current function info ptr. */
856
857 fprintf (gcov_file, "file:%s\n", src->name); /* source file name */
858
4457451f 859 for (fn = src->functions; fn; fn = fn->next_file_fn)
fc8a650e
SS
860 {
861 /* function:<name>,<line_number>,<execution_count> */
862 fprintf (gcov_file, "function:%d,%s,%s\n", fn->line,
176bf572
ML
863 format_gcov (fn->blocks[0].count, 0, -1),
864 flag_demangled_names ? fn->demangled_name : fn->name);
fc8a650e
SS
865 }
866
867 for (line_num = 1, line = &src->lines[line_num];
868 line_num < src->num_lines;
869 line_num++, line++)
870 {
871 arc_t *arc;
872 if (line->exists)
176bf572
ML
873 fprintf (gcov_file, "lcount:%u,%s\n", line_num,
874 format_gcov (line->count, 0, -1));
fc8a650e
SS
875 if (flag_branches)
876 for (arc = line->u.branches; arc; arc = arc->line_next)
877 {
878 if (!arc->is_unconditional && !arc->is_call_non_return)
879 {
880 const char *branch_type;
881 /* branch:<line_num>,<branch_coverage_type>
882 branch_coverage_type
883 : notexec (Branch not executed)
884 : taken (Branch executed and taken)
885 : nottaken (Branch executed, but not taken)
886 */
887 if (arc->src->count)
888 branch_type = (arc->count > 0) ? "taken" : "nottaken";
889 else
890 branch_type = "notexec";
c3284718 891 fprintf (gcov_file, "branch:%d,%s\n", line_num, branch_type);
fc8a650e
SS
892 }
893 }
894 }
895}
896
eeabee0a 897/* Process a single input file. */
5735c3ea 898
4977bab6 899static void
f55ade6e 900process_file (const char *file_name)
4977bab6 901{
1ce1b792 902 function_t *fns;
f55ade6e 903
4977bab6 904 create_file_names (file_name);
1ce1b792
NS
905 fns = read_graph_file ();
906 if (!fns)
4977bab6 907 return;
176bf572 908
1ce1b792
NS
909 read_count_file (fns);
910 while (fns)
4977bab6 911 {
1ce1b792 912 function_t *fn = fns;
f55ade6e 913
1ce1b792
NS
914 fns = fn->next;
915 fn->next = NULL;
20d1af89 916 if (fn->counts || no_data_file)
5366b186 917 {
1ce1b792
NS
918 unsigned src = fn->src;
919 unsigned line = fn->line;
920 unsigned block_no;
921 function_t *probe, **prev;
922
923 /* Now insert it into the source file's list of
924 functions. Normally functions will be encountered in
925 ascending order, so a simple scan is quick. Note we're
926 building this list in reverse order. */
927 for (prev = &sources[src].functions;
4457451f 928 (probe = *prev); prev = &probe->next_file_fn)
1ce1b792
NS
929 if (probe->line <= line)
930 break;
4457451f 931 fn->next_file_fn = probe;
1ce1b792
NS
932 *prev = fn;
933
934 /* Mark last line in files touched by function. */
935 for (block_no = 0; block_no != fn->num_blocks; block_no++)
936 {
937 unsigned *enc = fn->blocks[block_no].u.line.encoding;
938 unsigned num = fn->blocks[block_no].u.line.num;
939
940 for (; num--; enc++)
941 if (!*enc)
942 {
943 if (enc[1] != src)
944 {
945 if (line >= sources[src].num_lines)
946 sources[src].num_lines = line + 1;
947 line = 0;
948 src = enc[1];
949 }
950 enc++;
951 num--;
952 }
953 else if (*enc > line)
954 line = *enc;
955 }
956 if (line >= sources[src].num_lines)
957 sources[src].num_lines = line + 1;
176bf572 958
5366b186 959 solve_flow_graph (fn);
8919c0d9
NS
960 if (fn->has_catch)
961 find_exception_blocks (fn);
1ce1b792
NS
962 *fn_end = fn;
963 fn_end = &fn->next;
5366b186
NS
964 }
965 else
1ce1b792
NS
966 /* The function was not in the executable -- some other
967 instance must have been selected. */
968 release_function (fn);
5366b186 969 }
1a9075e2
TG
970}
971
fc8a650e 972static void
c3284718 973output_gcov_file (const char *file_name, source_t *src)
fc8a650e
SS
974{
975 char *gcov_file_name = make_gcov_file_name (file_name, src->coverage.name);
976
977 if (src->coverage.lines)
978 {
979 FILE *gcov_file = fopen (gcov_file_name, "w");
980 if (gcov_file)
981 {
982 fnotice (stdout, "Creating '%s'\n", gcov_file_name);
983 output_lines (gcov_file, src);
984 if (ferror (gcov_file))
985 fnotice (stderr, "Error writing output file '%s'\n", gcov_file_name);
986 fclose (gcov_file);
987 }
988 else
989 fnotice (stderr, "Could not open output file '%s'\n", gcov_file_name);
990 }
991 else
992 {
993 unlink (gcov_file_name);
994 fnotice (stdout, "Removing '%s'\n", gcov_file_name);
995 }
996 free (gcov_file_name);
997}
998
1a9075e2
TG
999static void
1000generate_results (const char *file_name)
1001{
1ce1b792 1002 unsigned ix;
1a9075e2
TG
1003 source_t *src;
1004 function_t *fn;
fc8a650e
SS
1005 FILE *gcov_intermediate_file = NULL;
1006 char *gcov_intermediate_filename = NULL;
1a9075e2 1007
1ce1b792
NS
1008 for (ix = n_sources, src = sources; ix--; src++)
1009 if (src->num_lines)
1010 src->lines = XCNEWVEC (line_t, src->num_lines);
1011
4977bab6
ZW
1012 for (fn = functions; fn; fn = fn->next)
1013 {
1014 coverage_t coverage;
f55ade6e 1015
4977bab6 1016 memset (&coverage, 0, sizeof (coverage));
fc8a650e 1017 coverage.name = flag_demangled_names ? fn->demangled_name : fn->name;
4977bab6
ZW
1018 add_line_counts (flag_function_summary ? &coverage : NULL, fn);
1019 if (flag_function_summary)
1020 {
1021 function_summary (&coverage, "Function");
1022 fnotice (stdout, "\n");
1023 }
1024 }
f55ade6e 1025
eeabee0a
NS
1026 if (file_name)
1027 {
1028 name_map_t *name_map = (name_map_t *)bsearch
1029 (file_name, names, n_names, sizeof (*names), name_search);
1030 if (name_map)
1bec9caa 1031 file_name = sources[name_map->src].coverage.name;
eeabee0a
NS
1032 else
1033 file_name = canonicalize_name (file_name);
1034 }
fc8a650e
SS
1035
1036 if (flag_gcov_file && flag_intermediate_format)
1037 {
1038 /* Open the intermediate file. */
176bf572 1039 gcov_intermediate_filename = get_gcov_intermediate_filename (file_name);
fc8a650e
SS
1040 gcov_intermediate_file = fopen (gcov_intermediate_filename, "w");
1041 if (!gcov_intermediate_file)
176bf572
ML
1042 {
1043 fnotice (stderr, "Cannot open intermediate output file %s\n",
1044 gcov_intermediate_filename);
1045 return;
1046 }
fc8a650e
SS
1047 }
1048
1ce1b792 1049 for (ix = n_sources, src = sources; ix--; src++)
4977bab6 1050 {
1bec9caa
NS
1051 if (flag_relative_only)
1052 {
1053 /* Ignore this source, if it is an absolute path (after
1054 source prefix removal). */
1055 char first = src->coverage.name[0];
176bf572 1056
1bec9caa
NS
1057#if HAVE_DOS_BASED_FILE_SYSTEM
1058 if (first && src->coverage.name[1] == ':')
fe860eb5 1059 first = src->coverage.name[2];
1bec9caa 1060#endif
fe860eb5
KT
1061 if (IS_DIR_SEPARATOR (first))
1062 continue;
1bec9caa 1063 }
176bf572 1064
4977bab6
ZW
1065 accumulate_line_counts (src);
1066 function_summary (&src->coverage, "File");
bdbdc4e1
NS
1067 total_lines += src->coverage.lines;
1068 total_executed += src->coverage.lines_executed;
8f961b22 1069 if (flag_gcov_file)
4977bab6 1070 {
fc8a650e
SS
1071 if (flag_intermediate_format)
1072 /* Output the intermediate format without requiring source
1073 files. This outputs a section to a *single* file. */
1074 output_intermediate_file (gcov_intermediate_file, src);
1075 else
1076 output_gcov_file (file_name, src);
1077 fnotice (stdout, "\n");
1078 }
1079 }
8f961b22 1080
fc8a650e
SS
1081 if (flag_gcov_file && flag_intermediate_format)
1082 {
1083 /* Now we've finished writing the intermediate file. */
1084 fclose (gcov_intermediate_file);
1085 XDELETEVEC (gcov_intermediate_filename);
4977bab6 1086 }
bdbdc4e1
NS
1087
1088 if (!file_name)
1089 executed_summary (total_lines, total_executed);
86144b75
DE
1090}
1091
5366b186
NS
1092/* Release a function structure */
1093
1094static void
1095release_function (function_t *fn)
1096{
1097 unsigned ix;
1098 block_t *block;
1099
1100 for (ix = fn->num_blocks, block = fn->blocks; ix--; block++)
1101 {
1102 arc_t *arc, *arc_n;
1103
1104 for (arc = block->succ; arc; arc = arc_n)
1105 {
1106 arc_n = arc->succ_next;
1107 free (arc);
1108 }
1109 }
1110 free (fn->blocks);
1111 free (fn->counts);
fc8a650e
SS
1112 if (flag_demangled_names && fn->demangled_name != fn->name)
1113 free (fn->demangled_name);
1114 free (fn->name);
5366b186
NS
1115}
1116
4977bab6 1117/* Release all memory used. */
86144b75 1118
4977bab6 1119static void
f55ade6e 1120release_structures (void)
4977bab6 1121{
1ce1b792 1122 unsigned ix;
4977bab6 1123 function_t *fn;
f55ade6e 1124
1ce1b792 1125 for (ix = n_sources; ix--;)
eeabee0a
NS
1126 free (sources[ix].lines);
1127 free (sources);
176bf572 1128
eeabee0a
NS
1129 for (ix = n_names; ix--;)
1130 free (names[ix].name);
1131 free (names);
f55ade6e 1132
4977bab6
ZW
1133 while ((fn = functions))
1134 {
4977bab6 1135 functions = fn->next;
5366b186 1136 release_function (fn);
4977bab6
ZW
1137 }
1138}
1139
15882fe9
TG
1140/* Generate the names of the graph and data files. If OBJECT_DIRECTORY
1141 is not specified, these are named from FILE_NAME sans extension. If
1142 OBJECT_DIRECTORY is specified and is a directory, the files are in that
1143 directory, but named from the basename of the FILE_NAME, sans extension.
1144 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
1145 and the data files are named from that. */
86144b75
DE
1146
1147static void
f55ade6e 1148create_file_names (const char *file_name)
86144b75 1149{
86144b75 1150 char *cptr;
37b8715b 1151 char *name;
4977bab6 1152 int length = strlen (file_name);
37b8715b 1153 int base;
f55ade6e 1154
1a9075e2 1155 /* Free previous file names. */
04695783
JM
1156 free (bbg_file_name);
1157 free (da_file_name);
1a9075e2
TG
1158 da_file_name = bbg_file_name = NULL;
1159 bbg_file_time = 0;
1160 bbg_stamp = 0;
1161
37b8715b 1162 if (object_directory && object_directory[0])
86144b75 1163 {
37b8715b
NS
1164 struct stat status;
1165
1166 length += strlen (object_directory) + 2;
5ed6ace5 1167 name = XNEWVEC (char, length);
37b8715b 1168 name[0] = 0;
f55ade6e 1169
37b8715b
NS
1170 base = !stat (object_directory, &status) && S_ISDIR (status.st_mode);
1171 strcat (name, object_directory);
176bf572 1172 if (base && (!IS_DIR_SEPARATOR (name[strlen (name) - 1])))
37b8715b 1173 strcat (name, "/");
86144b75
DE
1174 }
1175 else
1176 {
5ed6ace5 1177 name = XNEWVEC (char, length + 1);
15882fe9
TG
1178 strcpy (name, file_name);
1179 base = 0;
86144b75 1180 }
f55ade6e 1181
37b8715b
NS
1182 if (base)
1183 {
f9da5064 1184 /* Append source file name. */
2f07423c
PO
1185 const char *cptr = lbasename (file_name);
1186 strcat (name, cptr ? cptr : file_name);
37b8715b 1187 }
f55ade6e 1188
4b7e68e7 1189 /* Remove the extension. */
258ef007 1190 cptr = strrchr (CONST_CAST (char *, lbasename (name)), '.');
86144b75 1191 if (cptr)
37b8715b 1192 *cptr = 0;
f55ade6e 1193
37b8715b 1194 length = strlen (name);
b8698a0f 1195
5ed6ace5 1196 bbg_file_name = XNEWVEC (char, length + strlen (GCOV_NOTE_SUFFIX) + 1);
4977bab6 1197 strcpy (bbg_file_name, name);
160e2e4f 1198 strcpy (bbg_file_name + length, GCOV_NOTE_SUFFIX);
37b8715b 1199
5ed6ace5 1200 da_file_name = XNEWVEC (char, length + strlen (GCOV_DATA_SUFFIX) + 1);
4977bab6
ZW
1201 strcpy (da_file_name, name);
1202 strcpy (da_file_name + length, GCOV_DATA_SUFFIX);
f55ade6e 1203
a14df7da 1204 free (name);
4977bab6 1205 return;
86144b75 1206}
86144b75 1207
eeabee0a
NS
1208/* A is a string and B is a pointer to name_map_t. Compare for file
1209 name orderability. */
1210
1211static int
1212name_search (const void *a_, const void *b_)
1213{
1214 const char *a = (const char *)a_;
1215 const name_map_t *b = (const name_map_t *)b_;
1216
1217#if HAVE_DOS_BASED_FILE_SYSTEM
1218 return strcasecmp (a, b->name);
1219#else
1220 return strcmp (a, b->name);
1221#endif
1222}
1223
1224/* A and B are a pointer to name_map_t. Compare for file name
1225 orderability. */
1226
1227static int
1228name_sort (const void *a_, const void *b_)
1229{
1230 const name_map_t *a = (const name_map_t *)a_;
1231 return name_search (a->name, b_);
1232}
1233
94de45d9
NS
1234/* Find or create a source file structure for FILE_NAME. Copies
1235 FILE_NAME on creation */
27283c73 1236
1ce1b792 1237static unsigned
f55ade6e 1238find_source (const char *file_name)
27283c73 1239{
eeabee0a
NS
1240 name_map_t *name_map;
1241 char *canon;
1242 unsigned idx;
1a9075e2 1243 struct stat status;
94de45d9
NS
1244
1245 if (!file_name)
1246 file_name = "<unknown>";
eeabee0a
NS
1247 name_map = (name_map_t *)bsearch
1248 (file_name, names, n_names, sizeof (*names), name_search);
1249 if (name_map)
1250 {
1251 idx = name_map->src;
1252 goto check_date;
1253 }
f55ade6e 1254
eeabee0a 1255 if (n_names + 2 > a_names)
1a9075e2 1256 {
eeabee0a
NS
1257 /* Extend the name map array -- we'll be inserting one or two
1258 entries. */
eeabee0a
NS
1259 a_names *= 2;
1260 name_map = XNEWVEC (name_map_t, a_names);
1261 memcpy (name_map, names, n_names * sizeof (*names));
1262 free (names);
1263 names = name_map;
1264 }
176bf572 1265
eeabee0a
NS
1266 /* Not found, try the canonical name. */
1267 canon = canonicalize_name (file_name);
176bf572
ML
1268 name_map = (name_map_t *) bsearch (canon, names, n_names, sizeof (*names),
1269 name_search);
eeabee0a
NS
1270 if (!name_map)
1271 {
1272 /* Not found with canonical name, create a new source. */
1273 source_t *src;
176bf572 1274
1ce1b792
NS
1275 if (n_sources == a_sources)
1276 {
1ce1b792
NS
1277 a_sources *= 2;
1278 src = XNEWVEC (source_t, a_sources);
1279 memcpy (src, sources, n_sources * sizeof (*sources));
1280 free (sources);
1281 sources = src;
1282 }
eeabee0a
NS
1283
1284 idx = n_sources;
1285
1286 name_map = &names[n_names++];
1287 name_map->name = canon;
1288 name_map->src = idx;
1289
1290 src = &sources[n_sources++];
1291 memset (src, 0, sizeof (*src));
1292 src->name = canon;
1a9075e2 1293 src->coverage.name = src->name;
1bec9caa
NS
1294 if (source_length
1295#if HAVE_DOS_BASED_FILE_SYSTEM
1296 /* You lose if separators don't match exactly in the
1297 prefix. */
1298 && !strncasecmp (source_prefix, src->coverage.name, source_length)
1299#else
1300 && !strncmp (source_prefix, src->coverage.name, source_length)
1301#endif
1302 && IS_DIR_SEPARATOR (src->coverage.name[source_length]))
1303 src->coverage.name += source_length + 1;
eeabee0a 1304 if (!stat (src->name, &status))
1a9075e2
TG
1305 src->file_time = status.st_mtime;
1306 }
eeabee0a
NS
1307 else
1308 idx = name_map->src;
1309
1310 if (name_search (file_name, name_map))
1311 {
1312 /* Append the non-canonical name. */
1313 name_map = &names[n_names++];
1314 name_map->name = xstrdup (file_name);
1315 name_map->src = idx;
1316 }
1a9075e2 1317
eeabee0a
NS
1318 /* Resort the name map. */
1319 qsort (names, n_names, sizeof (*names), name_sort);
1320
1321 check_date:
1322 if (sources[idx].file_time > bbg_file_time)
1a9075e2
TG
1323 {
1324 static int info_emitted;
1325
efbb59b2 1326 fnotice (stderr, "%s:source file is newer than notes file '%s'\n",
eeabee0a 1327 file_name, bbg_file_name);
1a9075e2
TG
1328 if (!info_emitted)
1329 {
1330 fnotice (stderr,
7106478c 1331 "(the message is displayed only once per source file)\n");
1a9075e2
TG
1332 info_emitted = 1;
1333 }
eeabee0a 1334 sources[idx].file_time = 0;
1a9075e2 1335 }
94de45d9 1336
eeabee0a 1337 return idx;
27283c73
NS
1338}
1339
efbb59b2 1340/* Read the notes file. Return list of functions read -- in reverse order. */
86144b75 1341
1ce1b792 1342static function_t *
f55ade6e 1343read_graph_file (void)
86144b75 1344{
94de45d9 1345 unsigned version;
4977bab6 1346 unsigned current_tag = 0;
1ce1b792
NS
1347 function_t *fn = NULL;
1348 function_t *fns = NULL;
1349 function_t **fns_end = &fns;
1350 unsigned src_idx = 0;
4977bab6 1351 unsigned ix;
7d63a2fa 1352 unsigned tag;
f55ade6e 1353
546d2adb 1354 if (!gcov_open (bbg_file_name, 1))
86144b75 1355 {
efbb59b2 1356 fnotice (stderr, "%s:cannot open notes file\n", bbg_file_name);
1ce1b792 1357 return fns;
86144b75 1358 }
546d2adb 1359 bbg_file_time = gcov_time ();
160e2e4f 1360 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC))
b7c9bf28 1361 {
efbb59b2 1362 fnotice (stderr, "%s:not a gcov notes file\n", bbg_file_name);
546d2adb 1363 gcov_close ();
1ce1b792 1364 return fns;
4977bab6 1365 }
b7c9bf28 1366
94de45d9
NS
1367 version = gcov_read_unsigned ();
1368 if (version != GCOV_VERSION)
4977bab6
ZW
1369 {
1370 char v[4], e[4];
f55ade6e 1371
330d2e2a
NS
1372 GCOV_UNSIGNED2STRING (v, version);
1373 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
1374
9e637a26 1375 fnotice (stderr, "%s:version '%.4s', prefer '%.4s'\n",
4977bab6
ZW
1376 bbg_file_name, v, e);
1377 }
dd486eb2
NS
1378 bbg_stamp = gcov_read_unsigned ();
1379
7d63a2fa 1380 while ((tag = gcov_read_unsigned ()))
4977bab6 1381 {
94de45d9 1382 unsigned length = gcov_read_unsigned ();
7d63a2fa 1383 gcov_position_t base = gcov_position ();
796621e8 1384
4977bab6 1385 if (tag == GCOV_TAG_FUNCTION)
b7c9bf28 1386 {
94de45d9 1387 char *function_name;
10adac51
XDL
1388 unsigned ident, lineno;
1389 unsigned lineno_checksum, cfg_checksum;
4977bab6 1390
796621e8 1391 ident = gcov_read_unsigned ();
10adac51
XDL
1392 lineno_checksum = gcov_read_unsigned ();
1393 cfg_checksum = gcov_read_unsigned ();
796621e8 1394 function_name = xstrdup (gcov_read_string ());
1ce1b792 1395 src_idx = find_source (gcov_read_string ());
94de45d9 1396 lineno = gcov_read_unsigned ();
f55ade6e 1397
5ed6ace5 1398 fn = XCNEW (function_t);
4977bab6 1399 fn->name = function_name;
176bf572
ML
1400 if (flag_demangled_names)
1401 {
1402 fn->demangled_name = cplus_demangle (fn->name, DMGL_PARAMS);
1403 if (!fn->demangled_name)
1404 fn->demangled_name = fn->name;
1405 }
796621e8 1406 fn->ident = ident;
10adac51
XDL
1407 fn->lineno_checksum = lineno_checksum;
1408 fn->cfg_checksum = cfg_checksum;
1ce1b792 1409 fn->src = src_idx;
27283c73 1410 fn->line = lineno;
4977bab6 1411
4457451f 1412 fn->next_file_fn = NULL;
1ce1b792
NS
1413 fn->next = NULL;
1414 *fns_end = fn;
1415 fns_end = &fn->next;
4977bab6 1416 current_tag = tag;
b7c9bf28 1417 }
4977bab6 1418 else if (fn && tag == GCOV_TAG_BLOCKS)
b7c9bf28 1419 {
4977bab6 1420 if (fn->blocks)
9e637a26 1421 fnotice (stderr, "%s:already seen blocks for '%s'\n",
4977bab6
ZW
1422 bbg_file_name, fn->name);
1423 else
b7c9bf28 1424 {
330d2e2a 1425 unsigned ix, num_blocks = GCOV_TAG_BLOCKS_NUM (length);
27283c73 1426 fn->num_blocks = num_blocks;
f55ade6e 1427
5ed6ace5 1428 fn->blocks = XCNEWVEC (block_t, fn->num_blocks);
27283c73 1429 for (ix = 0; ix != num_blocks; ix++)
94de45d9 1430 fn->blocks[ix].flags = gcov_read_unsigned ();
b7c9bf28 1431 }
4977bab6
ZW
1432 }
1433 else if (fn && tag == GCOV_TAG_ARCS)
1434 {
94de45d9 1435 unsigned src = gcov_read_unsigned ();
330d2e2a 1436 unsigned num_dests = GCOV_TAG_ARCS_NUM (length);
8919c0d9
NS
1437 block_t *src_blk = &fn->blocks[src];
1438 unsigned mark_catches = 0;
1439 struct arc_info *arc;
4977bab6 1440
94de45d9 1441 if (src >= fn->num_blocks || fn->blocks[src].succ)
4977bab6 1442 goto corrupt;
f55ade6e 1443
4977bab6 1444 while (num_dests--)
b7c9bf28 1445 {
94de45d9
NS
1446 unsigned dest = gcov_read_unsigned ();
1447 unsigned flags = gcov_read_unsigned ();
f55ade6e 1448
94de45d9 1449 if (dest >= fn->num_blocks)
4977bab6 1450 goto corrupt;
5ed6ace5 1451 arc = XCNEW (arc_t);
f55ade6e 1452
4977bab6 1453 arc->dst = &fn->blocks[dest];
8919c0d9 1454 arc->src = src_blk;
f55ade6e 1455
4977bab6
ZW
1456 arc->count = 0;
1457 arc->count_valid = 0;
1458 arc->on_tree = !!(flags & GCOV_ARC_ON_TREE);
1459 arc->fake = !!(flags & GCOV_ARC_FAKE);
1460 arc->fall_through = !!(flags & GCOV_ARC_FALLTHROUGH);
f55ade6e 1461
8919c0d9
NS
1462 arc->succ_next = src_blk->succ;
1463 src_blk->succ = arc;
1464 src_blk->num_succ++;
f55ade6e 1465
4977bab6
ZW
1466 arc->pred_next = fn->blocks[dest].pred;
1467 fn->blocks[dest].pred = arc;
1468 fn->blocks[dest].num_pred++;
1469
27283c73
NS
1470 if (arc->fake)
1471 {
1472 if (src)
1473 {
1474 /* Exceptional exit from this function, the
1475 source block must be a call. */
1476 fn->blocks[src].is_call_site = 1;
1477 arc->is_call_non_return = 1;
8919c0d9 1478 mark_catches = 1;
27283c73
NS
1479 }
1480 else
1481 {
1482 /* Non-local return from a callee of this
176bf572 1483 function. The destination block is a setjmp. */
27283c73
NS
1484 arc->is_nonlocal_return = 1;
1485 fn->blocks[dest].is_nonlocal_return = 1;
1486 }
1487 }
f55ade6e 1488
4977bab6
ZW
1489 if (!arc->on_tree)
1490 fn->num_counts++;
b7c9bf28 1491 }
176bf572 1492
8919c0d9
NS
1493 if (mark_catches)
1494 {
1495 /* We have a fake exit from this block. The other
1496 non-fall through exits must be to catch handlers.
1497 Mark them as catch arcs. */
1498
1499 for (arc = src_blk->succ; arc; arc = arc->succ_next)
1500 if (!arc->fake && !arc->fall_through)
1501 {
1502 arc->is_throw = 1;
1503 fn->has_catch = 1;
1504 }
1505 }
4977bab6
ZW
1506 }
1507 else if (fn && tag == GCOV_TAG_LINES)
1508 {
94de45d9 1509 unsigned blockno = gcov_read_unsigned ();
5ed6ace5 1510 unsigned *line_nos = XCNEWVEC (unsigned, length - 1);
4977bab6 1511
94de45d9 1512 if (blockno >= fn->num_blocks || fn->blocks[blockno].u.line.encoding)
4977bab6 1513 goto corrupt;
f55ade6e 1514
4977bab6 1515 for (ix = 0; ; )
b7c9bf28 1516 {
94de45d9 1517 unsigned lineno = gcov_read_unsigned ();
f55ade6e 1518
4977bab6 1519 if (lineno)
b7c9bf28 1520 {
4977bab6
ZW
1521 if (!ix)
1522 {
1523 line_nos[ix++] = 0;
1ce1b792 1524 line_nos[ix++] = src_idx;
4977bab6
ZW
1525 }
1526 line_nos[ix++] = lineno;
b7c9bf28 1527 }
4977bab6
ZW
1528 else
1529 {
94de45d9 1530 const char *file_name = gcov_read_string ();
f55ade6e 1531
4977bab6 1532 if (!file_name)
b7c9bf28 1533 break;
1ce1b792 1534 src_idx = find_source (file_name);
4977bab6 1535 line_nos[ix++] = 0;
1ce1b792 1536 line_nos[ix++] = src_idx;
4977bab6 1537 }
b7c9bf28 1538 }
f55ade6e 1539
27283c73
NS
1540 fn->blocks[blockno].u.line.encoding = line_nos;
1541 fn->blocks[blockno].u.line.num = ix;
4977bab6
ZW
1542 }
1543 else if (current_tag && !GCOV_TAG_IS_SUBTAG (current_tag, tag))
1544 {
1545 fn = NULL;
1546 current_tag = 0;
1547 }
474f141e 1548 gcov_sync (base, length);
94de45d9 1549 if (gcov_is_error ())
00cf2913
NS
1550 {
1551 corrupt:;
1552 fnotice (stderr, "%s:corrupted\n", bbg_file_name);
1ce1b792 1553 break;
00cf2913 1554 }
b7c9bf28 1555 }
546d2adb 1556 gcov_close ();
f55ade6e 1557
1ce1b792
NS
1558 if (!fns)
1559 fnotice (stderr, "%s:no functions found\n", bbg_file_name);
b7c9bf28 1560
1ce1b792 1561 return fns;
b7c9bf28 1562}
86144b75 1563
4977bab6 1564/* Reads profiles from the count file and attach to each
272d0bee 1565 function. Return nonzero if fatal error. */
86144b75 1566
4977bab6 1567static int
1ce1b792 1568read_count_file (function_t *fns)
86144b75 1569{
4977bab6 1570 unsigned ix;
94de45d9 1571 unsigned version;
7d63a2fa 1572 unsigned tag;
4977bab6 1573 function_t *fn = NULL;
7d63a2fa 1574 int error = 0;
4977bab6 1575
546d2adb 1576 if (!gcov_open (da_file_name, 1))
86144b75 1577 {
80b3502b
NS
1578 fnotice (stderr, "%s:cannot open data file, assuming not executed\n",
1579 da_file_name);
1580 no_data_file = 1;
1581 return 0;
4977bab6 1582 }
160e2e4f 1583 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
4977bab6
ZW
1584 {
1585 fnotice (stderr, "%s:not a gcov data file\n", da_file_name);
1586 cleanup:;
546d2adb 1587 gcov_close ();
4977bab6
ZW
1588 return 1;
1589 }
94de45d9
NS
1590 version = gcov_read_unsigned ();
1591 if (version != GCOV_VERSION)
4977bab6 1592 {
330d2e2a
NS
1593 char v[4], e[4];
1594
1595 GCOV_UNSIGNED2STRING (v, version);
1596 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
b8698a0f 1597
9e637a26 1598 fnotice (stderr, "%s:version '%.4s', prefer version '%.4s'\n",
330d2e2a 1599 da_file_name, v, e);
86144b75 1600 }
dd486eb2
NS
1601 tag = gcov_read_unsigned ();
1602 if (tag != bbg_stamp)
1603 {
efbb59b2 1604 fnotice (stderr, "%s:stamp mismatch with notes file\n", da_file_name);
dd486eb2
NS
1605 goto cleanup;
1606 }
f55ade6e 1607
7d63a2fa 1608 while ((tag = gcov_read_unsigned ()))
4977bab6 1609 {
94de45d9
NS
1610 unsigned length = gcov_read_unsigned ();
1611 unsigned long base = gcov_position ();
94de45d9 1612
5366b186 1613 if (tag == GCOV_TAG_PROGRAM_SUMMARY)
86144b75 1614 {
5366b186
NS
1615 struct gcov_summary summary;
1616 gcov_read_summary (&summary);
1617 object_runs += summary.ctrs[GCOV_COUNTER_ARCS].runs;
1618 program_count++;
1619 }
1620 else if (tag == GCOV_TAG_FUNCTION && !length)
1621 ; /* placeholder */
1622 else if (tag == GCOV_TAG_FUNCTION && length == GCOV_TAG_FUNCTION_LENGTH)
1623 {
1624 unsigned ident;
1625 struct function_info *fn_n;
4977bab6 1626
5366b186
NS
1627 /* Try to find the function in the list. To speed up the
1628 search, first start from the last function found. */
1629 ident = gcov_read_unsigned ();
1ce1b792 1630 fn_n = fns;
5366b186
NS
1631 for (fn = fn ? fn->next : NULL; ; fn = fn->next)
1632 {
1633 if (fn)
1634 ;
1635 else if ((fn = fn_n))
1636 fn_n = NULL;
1637 else
1638 {
1639 fnotice (stderr, "%s:unknown function '%u'\n",
1640 da_file_name, ident);
4977bab6 1641 break;
5366b186
NS
1642 }
1643 if (fn->ident == ident)
1644 break;
1645 }
4977bab6
ZW
1646
1647 if (!fn)
1648 ;
10adac51
XDL
1649 else if (gcov_read_unsigned () != fn->lineno_checksum
1650 || gcov_read_unsigned () != fn->cfg_checksum)
86144b75 1651 {
4977bab6 1652 mismatch:;
9e637a26 1653 fnotice (stderr, "%s:profile mismatch for '%s'\n",
94de45d9 1654 da_file_name, fn->name);
4977bab6
ZW
1655 goto cleanup;
1656 }
1657 }
cdb23767 1658 else if (tag == GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS) && fn)
4977bab6 1659 {
330d2e2a 1660 if (length != GCOV_TAG_COUNTER_LENGTH (fn->num_counts))
4977bab6 1661 goto mismatch;
f55ade6e 1662
4977bab6 1663 if (!fn->counts)
5ed6ace5 1664 fn->counts = XCNEWVEC (gcov_type, fn->num_counts);
f55ade6e 1665
4977bab6 1666 for (ix = 0; ix != fn->num_counts; ix++)
94de45d9
NS
1667 fn->counts[ix] += gcov_read_counter ();
1668 }
474f141e 1669 gcov_sync (base, length);
94de45d9 1670 if ((error = gcov_is_error ()))
00cf2913
NS
1671 {
1672 fnotice (stderr, error < 0 ? "%s:overflowed\n" : "%s:corrupted\n",
1673 da_file_name);
1674 goto cleanup;
1675 }
7d63a2fa 1676 }
f55ade6e 1677
546d2adb 1678 gcov_close ();
4977bab6 1679 return 0;
86144b75
DE
1680}
1681
4977bab6
ZW
1682/* Solve the flow graph. Propagate counts from the instrumented arcs
1683 to the blocks and the uninstrumented arcs. */
86144b75
DE
1684
1685static void
f55ade6e 1686solve_flow_graph (function_t *fn)
86144b75 1687{
4977bab6
ZW
1688 unsigned ix;
1689 arc_t *arc;
1690 gcov_type *count_ptr = fn->counts;
27283c73 1691 block_t *blk;
32dd366d
KH
1692 block_t *valid_blocks = NULL; /* valid, but unpropagated blocks. */
1693 block_t *invalid_blocks = NULL; /* invalid, but inferable blocks. */
f55ade6e 1694
1ce1b792
NS
1695 /* The arcs were built in reverse order. Fix that now. */
1696 for (ix = fn->num_blocks; ix--;)
1697 {
1698 arc_t *arc_p, *arc_n;
1699
1700 for (arc_p = NULL, arc = fn->blocks[ix].succ; arc;
1701 arc_p = arc, arc = arc_n)
1702 {
1703 arc_n = arc->succ_next;
1704 arc->succ_next = arc_p;
1705 }
1706 fn->blocks[ix].succ = arc_p;
1707
1708 for (arc_p = NULL, arc = fn->blocks[ix].pred; arc;
1709 arc_p = arc, arc = arc_n)
1710 {
1711 arc_n = arc->pred_next;
1712 arc->pred_next = arc_p;
1713 }
1714 fn->blocks[ix].pred = arc_p;
1715 }
1716
4977bab6 1717 if (fn->num_blocks < 2)
9e637a26 1718 fnotice (stderr, "%s:'%s' lacks entry and/or exit blocks\n",
4977bab6
ZW
1719 bbg_file_name, fn->name);
1720 else
86144b75 1721 {
9696c529 1722 if (fn->blocks[ENTRY_BLOCK].num_pred)
9e637a26 1723 fnotice (stderr, "%s:'%s' has arcs to entry block\n",
4977bab6 1724 bbg_file_name, fn->name);
86144b75 1725 else
4977bab6
ZW
1726 /* We can't deduce the entry block counts from the lack of
1727 predecessors. */
9696c529 1728 fn->blocks[ENTRY_BLOCK].num_pred = ~(unsigned)0;
f55ade6e 1729
9696c529 1730 if (fn->blocks[EXIT_BLOCK].num_succ)
9e637a26 1731 fnotice (stderr, "%s:'%s' has arcs from exit block\n",
4977bab6
ZW
1732 bbg_file_name, fn->name);
1733 else
1734 /* Likewise, we can't deduce exit block counts from the lack
1735 of its successors. */
9696c529 1736 fn->blocks[EXIT_BLOCK].num_succ = ~(unsigned)0;
86144b75
DE
1737 }
1738
4977bab6
ZW
1739 /* Propagate the measured counts, this must be done in the same
1740 order as the code in profile.c */
27283c73 1741 for (ix = 0, blk = fn->blocks; ix != fn->num_blocks; ix++, blk++)
86144b75 1742 {
4977bab6
ZW
1743 block_t const *prev_dst = NULL;
1744 int out_of_order = 0;
27283c73 1745 int non_fake_succ = 0;
f55ade6e 1746
27283c73 1747 for (arc = blk->succ; arc; arc = arc->succ_next)
86144b75 1748 {
27283c73
NS
1749 if (!arc->fake)
1750 non_fake_succ++;
f55ade6e 1751
4977bab6 1752 if (!arc->on_tree)
86144b75 1753 {
4977bab6
ZW
1754 if (count_ptr)
1755 arc->count = *count_ptr++;
1756 arc->count_valid = 1;
27283c73 1757 blk->num_succ--;
4977bab6 1758 arc->dst->num_pred--;
86144b75 1759 }
4977bab6
ZW
1760 if (prev_dst && prev_dst > arc->dst)
1761 out_of_order = 1;
1762 prev_dst = arc->dst;
86144b75 1763 }
27283c73
NS
1764 if (non_fake_succ == 1)
1765 {
1766 /* If there is only one non-fake exit, it is an
1767 unconditional branch. */
1768 for (arc = blk->succ; arc; arc = arc->succ_next)
1769 if (!arc->fake)
1770 {
1771 arc->is_unconditional = 1;
1772 /* If this block is instrumenting a call, it might be
e0bb17a8 1773 an artificial block. It is not artificial if it has
10b7602f
NS
1774 a non-fallthrough exit, or the destination of this
1775 arc has more than one entry. Mark the destination
1776 block as a return site, if none of those conditions
1777 hold. */
1778 if (blk->is_call_site && arc->fall_through
1779 && arc->dst->pred == arc && !arc->pred_next)
1780 arc->dst->is_call_return = 1;
27283c73
NS
1781 }
1782 }
f55ade6e 1783
4977bab6
ZW
1784 /* Sort the successor arcs into ascending dst order. profile.c
1785 normally produces arcs in the right order, but sometimes with
1786 one or two out of order. We're not using a particularly
32dd366d 1787 smart sort. */
4977bab6 1788 if (out_of_order)
86144b75 1789 {
27283c73 1790 arc_t *start = blk->succ;
4977bab6 1791 unsigned changes = 1;
f55ade6e 1792
4977bab6
ZW
1793 while (changes)
1794 {
1795 arc_t *arc, *arc_p, *arc_n;
f55ade6e 1796
4977bab6
ZW
1797 changes = 0;
1798 for (arc_p = NULL, arc = start; (arc_n = arc->succ_next);)
1799 {
1800 if (arc->dst > arc_n->dst)
1801 {
1802 changes = 1;
1803 if (arc_p)
1804 arc_p->succ_next = arc_n;
1805 else
1806 start = arc_n;
1807 arc->succ_next = arc_n->succ_next;
1808 arc_n->succ_next = arc;
1809 arc_p = arc_n;
1810 }
1811 else
1812 {
1813 arc_p = arc;
1814 arc = arc_n;
1815 }
1816 }
1817 }
27283c73 1818 blk->succ = start;
86144b75 1819 }
f55ade6e 1820
4977bab6
ZW
1821 /* Place it on the invalid chain, it will be ignored if that's
1822 wrong. */
27283c73
NS
1823 blk->invalid_chain = 1;
1824 blk->chain = invalid_blocks;
1825 invalid_blocks = blk;
4977bab6
ZW
1826 }
1827
1828 while (invalid_blocks || valid_blocks)
1829 {
4977bab6 1830 while ((blk = invalid_blocks))
86144b75 1831 {
4977bab6
ZW
1832 gcov_type total = 0;
1833 const arc_t *arc;
f55ade6e 1834
4977bab6
ZW
1835 invalid_blocks = blk->chain;
1836 blk->invalid_chain = 0;
1837 if (!blk->num_succ)
1838 for (arc = blk->succ; arc; arc = arc->succ_next)
1839 total += arc->count;
1840 else if (!blk->num_pred)
1841 for (arc = blk->pred; arc; arc = arc->pred_next)
1842 total += arc->count;
1843 else
1844 continue;
f55ade6e 1845
4977bab6
ZW
1846 blk->count = total;
1847 blk->count_valid = 1;
1848 blk->chain = valid_blocks;
1849 blk->valid_chain = 1;
1850 valid_blocks = blk;
86144b75 1851 }
4977bab6 1852 while ((blk = valid_blocks))
86144b75 1853 {
4977bab6
ZW
1854 gcov_type total;
1855 arc_t *arc, *inv_arc;
1856
1857 valid_blocks = blk->chain;
1858 blk->valid_chain = 0;
1859 if (blk->num_succ == 1)
1860 {
1861 block_t *dst;
f55ade6e 1862
4977bab6
ZW
1863 total = blk->count;
1864 inv_arc = NULL;
1865 for (arc = blk->succ; arc; arc = arc->succ_next)
1866 {
1867 total -= arc->count;
1868 if (!arc->count_valid)
1869 inv_arc = arc;
1870 }
1871 dst = inv_arc->dst;
1872 inv_arc->count_valid = 1;
1873 inv_arc->count = total;
1874 blk->num_succ--;
1875 dst->num_pred--;
1876 if (dst->count_valid)
1877 {
1878 if (dst->num_pred == 1 && !dst->valid_chain)
1879 {
1880 dst->chain = valid_blocks;
1881 dst->valid_chain = 1;
1882 valid_blocks = dst;
1883 }
1884 }
1885 else
1886 {
1887 if (!dst->num_pred && !dst->invalid_chain)
1888 {
1889 dst->chain = invalid_blocks;
1890 dst->invalid_chain = 1;
1891 invalid_blocks = dst;
1892 }
1893 }
1894 }
1895 if (blk->num_pred == 1)
1896 {
1897 block_t *src;
f55ade6e 1898
4977bab6
ZW
1899 total = blk->count;
1900 inv_arc = NULL;
1901 for (arc = blk->pred; arc; arc = arc->pred_next)
1902 {
1903 total -= arc->count;
1904 if (!arc->count_valid)
1905 inv_arc = arc;
1906 }
1907 src = inv_arc->src;
1908 inv_arc->count_valid = 1;
1909 inv_arc->count = total;
1910 blk->num_pred--;
1911 src->num_succ--;
1912 if (src->count_valid)
1913 {
1914 if (src->num_succ == 1 && !src->valid_chain)
1915 {
1916 src->chain = valid_blocks;
1917 src->valid_chain = 1;
1918 valid_blocks = src;
1919 }
1920 }
1921 else
1922 {
1923 if (!src->num_succ && !src->invalid_chain)
1924 {
1925 src->chain = invalid_blocks;
1926 src->invalid_chain = 1;
1927 invalid_blocks = src;
1928 }
1929 }
1930 }
86144b75
DE
1931 }
1932 }
f55ade6e 1933
4977bab6
ZW
1934 /* If the graph has been correctly solved, every block will have a
1935 valid count. */
1936 for (ix = 0; ix < fn->num_blocks; ix++)
1937 if (!fn->blocks[ix].count_valid)
1938 {
9e637a26 1939 fnotice (stderr, "%s:graph is unsolvable for '%s'\n",
4977bab6
ZW
1940 bbg_file_name, fn->name);
1941 break;
1942 }
86144b75 1943}
4977bab6 1944
8919c0d9
NS
1945/* Mark all the blocks only reachable via an incoming catch. */
1946
1947static void
1948find_exception_blocks (function_t *fn)
1949{
1950 unsigned ix;
1951 block_t **queue = XALLOCAVEC (block_t *, fn->num_blocks);
1952
1953 /* First mark all blocks as exceptional. */
1954 for (ix = fn->num_blocks; ix--;)
1955 fn->blocks[ix].exceptional = 1;
1956
1957 /* Now mark all the blocks reachable via non-fake edges */
1958 queue[0] = fn->blocks;
1959 queue[0]->exceptional = 0;
1960 for (ix = 1; ix;)
1961 {
1962 block_t *block = queue[--ix];
1963 const arc_t *arc;
176bf572 1964
8919c0d9
NS
1965 for (arc = block->succ; arc; arc = arc->succ_next)
1966 if (!arc->fake && !arc->is_throw && arc->dst->exceptional)
1967 {
1968 arc->dst->exceptional = 0;
1969 queue[ix++] = arc->dst;
1970 }
1971 }
1972}
86144b75 1973\f
86144b75 1974
4977bab6 1975/* Increment totals in COVERAGE according to arc ARC. */
8b219a76
NS
1976
1977static void
f55ade6e 1978add_branch_counts (coverage_t *coverage, const arc_t *arc)
8b219a76 1979{
27283c73 1980 if (arc->is_call_non_return)
8b219a76 1981 {
4977bab6
ZW
1982 coverage->calls++;
1983 if (arc->src->count)
1984 coverage->calls_executed++;
8b219a76 1985 }
27283c73 1986 else if (!arc->is_unconditional)
8b219a76 1987 {
4977bab6
ZW
1988 coverage->branches++;
1989 if (arc->src->count)
1990 coverage->branches_executed++;
1991 if (arc->count)
1992 coverage->branches_taken++;
86144b75
DE
1993 }
1994}
1995
9696c529 1996/* Format a GCOV_TYPE integer as either a percent ratio, or absolute
37b8715b
NS
1997 count. If dp >= 0, format TOP/BOTTOM * 100 to DP decimal places.
1998 If DP is zero, no decimal point is printed. Only print 100% when
1999 TOP==BOTTOM and only print 0% when TOP=0. If dp < 0, then simply
2000 format TOP. Return pointer to a static string. */
2001
2002static char const *
f55ade6e 2003format_gcov (gcov_type top, gcov_type bottom, int dp)
37b8715b
NS
2004{
2005 static char buffer[20];
f55ade6e 2006
37b8715b
NS
2007 if (dp >= 0)
2008 {
2009 float ratio = bottom ? (float)top / bottom : 0;
2010 int ix;
2011 unsigned limit = 100;
2012 unsigned percent;
f55ade6e 2013
37b8715b
NS
2014 for (ix = dp; ix--; )
2015 limit *= 10;
f55ade6e 2016
d19202ba
NS
2017 percent = (unsigned) (ratio * limit + (float)0.5);
2018 if (percent <= 0 && top)
37b8715b 2019 percent = 1;
d19202ba 2020 else if (percent >= limit && top != bottom)
37b8715b
NS
2021 percent = limit - 1;
2022 ix = sprintf (buffer, "%.*u%%", dp + 1, percent);
2023 if (dp)
2024 {
2025 dp++;
2026 do
2027 {
2028 buffer[ix+1] = buffer[ix];
2029 ix--;
2030 }
2031 while (dp--);
2032 buffer[ix + 1] = '.';
2033 }
2034 }
2035 else
16998094 2036 sprintf (buffer, "%" PRId64, (int64_t)top);
f55ade6e 2037
37b8715b
NS
2038 return buffer;
2039}
2040
bdbdc4e1 2041/* Summary of execution */
86144b75
DE
2042
2043static void
bdbdc4e1 2044executed_summary (unsigned lines, unsigned executed)
86144b75 2045{
bdbdc4e1 2046 if (lines)
4977bab6 2047 fnotice (stdout, "Lines executed:%s of %d\n",
bdbdc4e1 2048 format_gcov (executed, lines, 2), lines);
86144b75 2049 else
1457ebf9 2050 fnotice (stdout, "No executable lines\n");
bdbdc4e1 2051}
176bf572 2052
bdbdc4e1
NS
2053/* Output summary info for a function or file. */
2054
2055static void
2056function_summary (const coverage_t *coverage, const char *title)
2057{
2058 fnotice (stdout, "%s '%s'\n", title, coverage->name);
2059 executed_summary (coverage->lines, coverage->lines_executed);
86144b75 2060
4977bab6 2061 if (flag_branches)
86144b75 2062 {
4977bab6 2063 if (coverage->branches)
86144b75 2064 {
4977bab6
ZW
2065 fnotice (stdout, "Branches executed:%s of %d\n",
2066 format_gcov (coverage->branches_executed,
2067 coverage->branches, 2),
2068 coverage->branches);
2069 fnotice (stdout, "Taken at least once:%s of %d\n",
2070 format_gcov (coverage->branches_taken,
2071 coverage->branches, 2),
2072 coverage->branches);
86144b75
DE
2073 }
2074 else
4977bab6
ZW
2075 fnotice (stdout, "No branches\n");
2076 if (coverage->calls)
2077 fnotice (stdout, "Calls executed:%s of %d\n",
2078 format_gcov (coverage->calls_executed, coverage->calls, 2),
2079 coverage->calls);
86144b75 2080 else
4977bab6 2081 fnotice (stdout, "No calls\n");
8b219a76
NS
2082 }
2083}
2084
eeabee0a
NS
2085/* Canonicalize the filename NAME by canonicalizing directory
2086 separators, eliding . components and resolving .. components
2087 appropriately. Always returns a unique string. */
2088
2089static char *
2090canonicalize_name (const char *name)
2091{
2092 /* The canonical name cannot be longer than the incoming name. */
2093 char *result = XNEWVEC (char, strlen (name) + 1);
2094 const char *base = name, *probe;
2095 char *ptr = result;
2096 char *dd_base;
2097 int slash = 0;
2098
2099#if HAVE_DOS_BASED_FILE_SYSTEM
2100 if (base[0] && base[1] == ':')
2101 {
2102 result[0] = base[0];
2103 result[1] = ':';
2104 base += 2;
2105 ptr += 2;
2106 }
2107#endif
2108 for (dd_base = ptr; *base; base = probe)
2109 {
2110 size_t len;
176bf572 2111
eeabee0a
NS
2112 for (probe = base; *probe; probe++)
2113 if (IS_DIR_SEPARATOR (*probe))
2114 break;
2115
2116 len = probe - base;
2117 if (len == 1 && base[0] == '.')
2118 /* Elide a '.' directory */
2119 ;
2120 else if (len == 2 && base[0] == '.' && base[1] == '.')
2121 {
2122 /* '..', we can only elide it and the previous directory, if
2123 we're not a symlink. */
8c121ccb
NS
2124 struct stat ATTRIBUTE_UNUSED buf;
2125
eeabee0a 2126 *ptr = 0;
8c121ccb
NS
2127 if (dd_base == ptr
2128#if defined (S_ISLNK)
2129 /* S_ISLNK is not POSIX.1-1996. */
2130 || stat (result, &buf) || S_ISLNK (buf.st_mode)
2131#endif
176bf572 2132 )
eeabee0a
NS
2133 {
2134 /* Cannot elide, or unreadable or a symlink. */
2135 dd_base = ptr + 2 + slash;
2136 goto regular;
2137 }
2138 while (ptr != dd_base && *ptr != '/')
2139 ptr--;
2140 slash = ptr != result;
2141 }
2142 else
2143 {
2144 regular:
2145 /* Regular pathname component. */
2146 if (slash)
2147 *ptr++ = '/';
2148 memcpy (ptr, base, len);
2149 ptr += len;
2150 slash = 1;
2151 }
2152
2153 for (; IS_DIR_SEPARATOR (*probe); probe++)
2154 continue;
2155 }
2156 *ptr = 0;
2157
2158 return result;
2159}
2160
75cc66f2
ML
2161/* Print hex representation of 16 bytes from SUM and write it to BUFFER. */
2162
2163static void
2164md5sum_to_hex (const char *sum, char *buffer)
2165{
2166 for (unsigned i = 0; i < 16; i++)
2167 sprintf (buffer + (2 * i), "%02x", sum[i]);
2168}
2169
eeabee0a
NS
2170/* Generate an output file name. INPUT_NAME is the canonicalized main
2171 input file and SRC_NAME is the canonicalized file name.
2172 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
8b219a76
NS
2173 long_output_names we prepend the processed name of the input file
2174 to each output name (except when the current source file is the
2175 input file, so you don't get a double concatenation). The two
eeabee0a
NS
2176 components are separated by '##'. With preserve_paths we create a
2177 filename from all path components of the source file, replacing '/'
2178 with '#', and .. with '^', without it we simply take the basename
2179 component. (Remember, the canonicalized name will already have
2180 elided '.' components and converted \\ separators.) */
8b219a76
NS
2181
2182static char *
f55ade6e 2183make_gcov_file_name (const char *input_name, const char *src_name)
8b219a76 2184{
eeabee0a
NS
2185 char *ptr;
2186 char *result;
f55ade6e 2187
1a9075e2 2188 if (flag_long_names && input_name && strcmp (src_name, input_name))
8b219a76
NS
2189 {
2190 /* Generate the input filename part. */
eeabee0a 2191 result = XNEWVEC (char, strlen (input_name) + strlen (src_name) + 10);
176bf572 2192
eeabee0a
NS
2193 ptr = result;
2194 ptr = mangle_name (input_name, ptr);
2195 ptr[0] = ptr[1] = '#';
2196 ptr += 2;
8b219a76 2197 }
1a9075e2
TG
2198 else
2199 {
eeabee0a
NS
2200 result = XNEWVEC (char, strlen (src_name) + 10);
2201 ptr = result;
1a9075e2 2202 }
f55ade6e 2203
eeabee0a
NS
2204 ptr = mangle_name (src_name, ptr);
2205 strcpy (ptr, ".gcov");
176bf572 2206
75cc66f2
ML
2207 /* When hashing filenames, we shorten them by only using the filename
2208 component and appending a hash of the full (mangled) pathname. */
2209 if (flag_hash_filenames)
2210 {
2211 md5_ctx ctx;
2212 char md5sum[16];
2213 char md5sum_hex[33];
2214
2215 md5_init_ctx (&ctx);
2216 md5_process_bytes (result, strlen (result), &ctx);
2217 md5_finish_ctx (&ctx, md5sum);
2218 md5sum_to_hex (md5sum, md5sum_hex);
2219 free (result);
2220
2221 result = XNEWVEC (char, strlen (src_name) + 50);
2222 ptr = result;
2223 ptr = mangle_name (src_name, ptr);
2224 ptr[0] = ptr[1] = '#';
2225 ptr += 2;
2226 memcpy (ptr, md5sum_hex, 32);
2227 ptr += 32;
2228 strcpy (ptr, ".gcov");
2229 }
2230
eeabee0a
NS
2231 return result;
2232}
f55ade6e 2233
eeabee0a
NS
2234static char *
2235mangle_name (char const *base, char *ptr)
2236{
2237 size_t len;
176bf572 2238
eeabee0a
NS
2239 /* Generate the source filename part. */
2240 if (!flag_preserve_paths)
2241 {
2242 base = lbasename (base);
2243 len = strlen (base);
2244 memcpy (ptr, base, len);
2245 ptr += len;
2246 }
2247 else
8b219a76 2248 {
eeabee0a 2249 /* Convert '/' to '#', convert '..' to '^',
2f07423c 2250 convert ':' to '~' on DOS based file system. */
eeabee0a 2251 const char *probe;
f55ade6e 2252
eeabee0a
NS
2253#if HAVE_DOS_BASED_FILE_SYSTEM
2254 if (base[0] && base[1] == ':')
2f07423c 2255 {
eeabee0a
NS
2256 ptr[0] = base[0];
2257 ptr[1] = '~';
2258 ptr += 2;
2259 base += 2;
2260 }
2f07423c 2261#endif
eeabee0a
NS
2262 for (; *base; base = probe)
2263 {
2264 size_t len;
2265
2266 for (probe = base; *probe; probe++)
2267 if (*probe == '/')
2268 break;
2269 len = probe - base;
2270 if (len == 2 && base[0] == '.' && base[1] == '.')
2271 *ptr++ = '^';
2272 else
2f07423c 2273 {
eeabee0a
NS
2274 memcpy (ptr, base, len);
2275 ptr += len;
2f07423c 2276 }
eeabee0a 2277 if (*probe)
ba78087b 2278 {
eeabee0a
NS
2279 *ptr++ = '#';
2280 probe++;
ba78087b 2281 }
f55ade6e 2282 }
86144b75 2283 }
176bf572 2284
eeabee0a 2285 return ptr;
86144b75
DE
2286}
2287
4977bab6 2288/* Scan through the bb_data for each line in the block, increment
8b219a76
NS
2289 the line number execution count indicated by the execution count of
2290 the appropriate basic block. */
86144b75
DE
2291
2292static void
f55ade6e 2293add_line_counts (coverage_t *coverage, function_t *fn)
86144b75 2294{
4977bab6 2295 unsigned ix;
beb235f8 2296 line_t *line = NULL; /* This is propagated from one iteration to the
4977bab6
ZW
2297 next. */
2298
32dd366d 2299 /* Scan each basic block. */
4977bab6 2300 for (ix = 0; ix != fn->num_blocks; ix++)
86144b75 2301 {
27283c73 2302 block_t *block = &fn->blocks[ix];
4977bab6
ZW
2303 unsigned *encoding;
2304 const source_t *src = NULL;
2305 unsigned jx;
2306
27283c73
NS
2307 if (block->count && ix && ix + 1 != fn->num_blocks)
2308 fn->blocks_executed++;
2309 for (jx = 0, encoding = block->u.line.encoding;
2310 jx != block->u.line.num; jx++, encoding++)
4977bab6
ZW
2311 if (!*encoding)
2312 {
1ce1b792 2313 src = &sources[*++encoding];
4977bab6
ZW
2314 jx++;
2315 }
2316 else
2317 {
2318 line = &src->lines[*encoding];
2319
2320 if (coverage)
2321 {
2322 if (!line->exists)
2323 coverage->lines++;
27283c73 2324 if (!line->count && block->count)
4977bab6
ZW
2325 coverage->lines_executed++;
2326 }
2327 line->exists = 1;
8919c0d9
NS
2328 if (!block->exceptional)
2329 line->unexceptional = 1;
4977bab6
ZW
2330 line->count += block->count;
2331 }
27283c73 2332 free (block->u.line.encoding);
10b7602f
NS
2333 block->u.cycle.arc = NULL;
2334 block->u.cycle.ident = ~0U;
f55ade6e 2335
27283c73
NS
2336 if (!ix || ix + 1 == fn->num_blocks)
2337 /* Entry or exit block */;
2338 else if (flag_all_blocks)
86144b75 2339 {
1ce1b792
NS
2340 line_t *block_line = line;
2341
2342 if (!block_line)
2343 block_line = &sources[fn->src].lines[fn->line];
f55ade6e 2344
10b7602f
NS
2345 block->chain = block_line->u.blocks;
2346 block_line->u.blocks = block;
27283c73
NS
2347 }
2348 else if (flag_branches)
2349 {
2350 arc_t *arc;
2351
4977bab6 2352 for (arc = block->succ; arc; arc = arc->succ_next)
86144b75 2353 {
27283c73
NS
2354 arc->line_next = line->u.branches;
2355 line->u.branches = arc;
2356 if (coverage && !arc->is_unconditional)
4977bab6 2357 add_branch_counts (coverage, arc);
86144b75 2358 }
8b219a76
NS
2359 }
2360 }
4977bab6 2361 if (!line)
9e637a26 2362 fnotice (stderr, "%s:no lines for '%s'\n", bbg_file_name, fn->name);
4977bab6
ZW
2363}
2364
32dd366d 2365/* Accumulate the line counts of a file. */
4977bab6
ZW
2366
2367static void
f55ade6e 2368accumulate_line_counts (source_t *src)
4977bab6
ZW
2369{
2370 line_t *line;
27283c73 2371 function_t *fn, *fn_p, *fn_n;
4977bab6 2372 unsigned ix;
27283c73
NS
2373
2374 /* Reverse the function order. */
176bf572 2375 for (fn = src->functions, fn_p = NULL; fn; fn_p = fn, fn = fn_n)
27283c73 2376 {
4457451f
ML
2377 fn_n = fn->next_file_fn;
2378 fn->next_file_fn = fn_p;
27283c73
NS
2379 }
2380 src->functions = fn_p;
f55ade6e 2381
4977bab6 2382 for (ix = src->num_lines, line = src->lines; ix--; line++)
8b219a76 2383 {
27283c73 2384 if (!flag_all_blocks)
8b219a76 2385 {
27283c73 2386 arc_t *arc, *arc_p, *arc_n;
f55ade6e 2387
27283c73
NS
2388 /* Total and reverse the branch information. */
2389 for (arc = line->u.branches, arc_p = NULL; arc;
2390 arc_p = arc, arc = arc_n)
2391 {
2392 arc_n = arc->line_next;
2393 arc->line_next = arc_p;
f55ade6e 2394
27283c73
NS
2395 add_branch_counts (&src->coverage, arc);
2396 }
2397 line->u.branches = arc_p;
8b219a76 2398 }
27283c73
NS
2399 else if (line->u.blocks)
2400 {
2401 /* The user expects the line count to be the number of times
2402 a line has been executed. Simply summing the block count
2403 will give an artificially high number. The Right Thing
10b7602f
NS
2404 is to sum the entry counts to the graph of blocks on this
2405 line, then find the elementary cycles of the local graph
2406 and add the transition counts of those cycles. */
27283c73 2407 block_t *block, *block_p, *block_n;
27283c73 2408 gcov_type count = 0;
f55ade6e 2409
f9da5064 2410 /* Reverse the block information. */
27283c73
NS
2411 for (block = line->u.blocks, block_p = NULL; block;
2412 block_p = block, block = block_n)
2413 {
2414 block_n = block->chain;
2415 block->chain = block_p;
10b7602f 2416 block->u.cycle.ident = ix;
27283c73
NS
2417 }
2418 line->u.blocks = block_p;
f55ade6e 2419
10b7602f
NS
2420 /* Sum the entry arcs. */
2421 for (block = line->u.blocks; block; block = block->chain)
2422 {
2423 arc_t *arc;
86144b75 2424
10b7602f 2425 for (arc = block->pred; arc; arc = arc->pred_next)
24bc5921
ML
2426 if (flag_branches)
2427 add_branch_counts (&src->coverage, arc);
10b7602f
NS
2428 }
2429
24bc5921 2430 /* Cycle detection. */
10b7602f 2431 for (block = line->u.blocks; block; block = block->chain)
27283c73 2432 {
24bc5921
ML
2433 for (arc_t *arc = block->pred; arc; arc = arc->pred_next)
2434 if (!line->has_block (arc->src))
2435 count += arc->count;
2436 for (arc_t *arc = block->succ; arc; arc = arc->succ_next)
2437 arc->cs_count = arc->count;
27283c73 2438 }
10b7602f 2439
24bc5921
ML
2440 /* Now, add the count of loops entirely on this line. */
2441 count += get_cycles_count (*line);
27283c73
NS
2442 line->count = count;
2443 }
f55ade6e 2444
4977bab6 2445 if (line->exists)
8b219a76 2446 {
4977bab6
ZW
2447 src->coverage.lines++;
2448 if (line->count)
2449 src->coverage.lines_executed++;
8b219a76 2450 }
8b219a76
NS
2451 }
2452}
86144b75 2453
61ada8ae 2454/* Output information about ARC number IX. Returns nonzero if
27283c73
NS
2455 anything is output. */
2456
2457static int
f55ade6e 2458output_branch_count (FILE *gcov_file, int ix, const arc_t *arc)
27283c73 2459{
27283c73
NS
2460 if (arc->is_call_non_return)
2461 {
2462 if (arc->src->count)
2463 {
2464 fnotice (gcov_file, "call %2d returned %s\n", ix,
2465 format_gcov (arc->src->count - arc->count,
2466 arc->src->count, -flag_counts));
2467 }
2468 else
2469 fnotice (gcov_file, "call %2d never executed\n", ix);
2470 }
2471 else if (!arc->is_unconditional)
2472 {
2473 if (arc->src->count)
2474 fnotice (gcov_file, "branch %2d taken %s%s\n", ix,
2475 format_gcov (arc->count, arc->src->count, -flag_counts),
8919c0d9
NS
2476 arc->fall_through ? " (fallthrough)"
2477 : arc->is_throw ? " (throw)" : "");
27283c73
NS
2478 else
2479 fnotice (gcov_file, "branch %2d never executed\n", ix);
2480 }
10b7602f 2481 else if (flag_unconditional && !arc->dst->is_call_return)
27283c73
NS
2482 {
2483 if (arc->src->count)
2484 fnotice (gcov_file, "unconditional %2d taken %s\n", ix,
2485 format_gcov (arc->count, arc->src->count, -flag_counts));
2486 else
2487 fnotice (gcov_file, "unconditional %2d never executed\n", ix);
2488 }
2489 else
2490 return 0;
2491 return 1;
27283c73
NS
2492}
2493
8f961b22
NS
2494static const char *
2495read_line (FILE *file)
2496{
2497 static char *string;
2498 static size_t string_len;
2499 size_t pos = 0;
2500 char *ptr;
2501
2502 if (!string_len)
2503 {
2504 string_len = 200;
2505 string = XNEWVEC (char, string_len);
2506 }
2507
2508 while ((ptr = fgets (string + pos, string_len - pos, file)))
2509 {
2510 size_t len = strlen (string + pos);
2511
2512 if (string[pos + len - 1] == '\n')
2513 {
2514 string[pos + len - 1] = 0;
2515 return string;
2516 }
2517 pos += len;
92b05e72
JJ
2518 string = XRESIZEVEC (char, string, string_len * 2);
2519 string_len *= 2;
8f961b22 2520 }
176bf572 2521
8f961b22
NS
2522 return pos ? string : NULL;
2523}
2524
8b219a76
NS
2525/* Read in the source file one line at a time, and output that line to
2526 the gcov file preceded by its execution count and other
2527 information. */
86144b75 2528
8b219a76 2529static void
f55ade6e 2530output_lines (FILE *gcov_file, const source_t *src)
8b219a76
NS
2531{
2532 FILE *source_file;
f55ade6e 2533 unsigned line_num; /* current line number. */
4977bab6 2534 const line_t *line; /* current line info ptr. */
8f961b22 2535 const char *retval = ""; /* status of source file reading. */
f5d39c3d 2536 function_t *fn = NULL;
4977bab6 2537
1bec9caa 2538 fprintf (gcov_file, "%9s:%5d:Source:%s\n", "-", 0, src->coverage.name);
1a9075e2
TG
2539 if (!multiple_files)
2540 {
2541 fprintf (gcov_file, "%9s:%5d:Graph:%s\n", "-", 0, bbg_file_name);
2542 fprintf (gcov_file, "%9s:%5d:Data:%s\n", "-", 0,
2543 no_data_file ? "-" : da_file_name);
5366b186 2544 fprintf (gcov_file, "%9s:%5d:Runs:%u\n", "-", 0, object_runs);
1a9075e2 2545 }
27283c73 2546 fprintf (gcov_file, "%9s:%5d:Programs:%u\n", "-", 0, program_count);
f55ade6e 2547
4977bab6 2548 source_file = fopen (src->name, "r");
8b219a76
NS
2549 if (!source_file)
2550 {
1bec9caa 2551 fnotice (stderr, "Cannot open source file %s\n", src->name);
8b219a76
NS
2552 retval = NULL;
2553 }
1a9075e2
TG
2554 else if (src->file_time == 0)
2555 fprintf (gcov_file, "%9s:%5d:Source is newer than graph\n", "-", 0);
8b219a76 2556
f5d39c3d
NS
2557 if (flag_branches)
2558 fn = src->functions;
2559
4977bab6
ZW
2560 for (line_num = 1, line = &src->lines[line_num];
2561 line_num < src->num_lines; line_num++, line++)
8b219a76 2562 {
4457451f 2563 for (; fn && fn->line == line_num; fn = fn->next_file_fn)
27283c73 2564 {
9696c529
SB
2565 arc_t *arc = fn->blocks[EXIT_BLOCK].pred;
2566 gcov_type return_count = fn->blocks[EXIT_BLOCK].count;
2567 gcov_type called_count = fn->blocks[ENTRY_BLOCK].count;
b8698a0f 2568
27283c73
NS
2569 for (; arc; arc = arc->pred_next)
2570 if (arc->fake)
2571 return_count -= arc->count;
b8698a0f 2572
fc8a650e
SS
2573 fprintf (gcov_file, "function %s", flag_demangled_names ?
2574 fn->demangled_name : fn->name);
27283c73 2575 fprintf (gcov_file, " called %s",
9696c529 2576 format_gcov (called_count, 0, -1));
27283c73 2577 fprintf (gcov_file, " returned %s",
9696c529 2578 format_gcov (return_count, called_count, 0));
27283c73
NS
2579 fprintf (gcov_file, " blocks executed %s",
2580 format_gcov (fn->blocks_executed, fn->num_blocks - 2, 0));
2581 fprintf (gcov_file, "\n");
2582 }
f55ade6e 2583
8f961b22
NS
2584 if (retval)
2585 retval = read_line (source_file);
2586
8b219a76 2587 /* For lines which don't exist in the .bb file, print '-' before
f55ade6e 2588 the source line. For lines which exist but were never
8f961b22
NS
2589 executed, print '#####' or '=====' before the source line.
2590 Otherwise, print the execution count before the source line.
2591 There are 16 spaces of indentation added before the source
2592 line so that tabs won't be messed up. */
2593 fprintf (gcov_file, "%9s:%5u:%s\n",
8919c0d9
NS
2594 !line->exists ? "-" : line->count
2595 ? format_gcov (line->count, 0, -1)
8f961b22
NS
2596 : line->unexceptional ? "#####" : "=====", line_num,
2597 retval ? retval : "/*EOF*/");
27283c73
NS
2598
2599 if (flag_all_blocks)
8b219a76 2600 {
27283c73 2601 block_t *block;
10b7602f 2602 arc_t *arc;
27283c73 2603 int ix, jx;
f55ade6e 2604
27283c73
NS
2605 for (ix = jx = 0, block = line->u.blocks; block;
2606 block = block->chain)
37b8715b 2607 {
10b7602f 2608 if (!block->is_call_return)
27283c73 2609 fprintf (gcov_file, "%9s:%5u-block %2d\n",
8919c0d9
NS
2610 !line->exists ? "-" : block->count
2611 ? format_gcov (block->count, 0, -1)
2612 : block->exceptional ? "%%%%%" : "$$$$$",
10b7602f 2613 line_num, ix++);
27283c73
NS
2614 if (flag_branches)
2615 for (arc = block->succ; arc; arc = arc->succ_next)
2616 jx += output_branch_count (gcov_file, jx, arc);
86144b75 2617 }
8b219a76 2618 }
27283c73
NS
2619 else if (flag_branches)
2620 {
2621 int ix;
2622 arc_t *arc;
f55ade6e 2623
27283c73
NS
2624 for (ix = 0, arc = line->u.branches; arc; arc = arc->line_next)
2625 ix += output_branch_count (gcov_file, ix, arc);
2626 }
8b219a76 2627 }
f55ade6e 2628
8b219a76
NS
2629 /* Handle all remaining source lines. There may be lines after the
2630 last line of code. */
2631 if (retval)
2632 {
8f961b22
NS
2633 for (; (retval = read_line (source_file)); line_num++)
2634 fprintf (gcov_file, "%9s:%5u:%s\n", "-", line_num, retval);
8b219a76 2635 }
f55ade6e 2636
8b219a76
NS
2637 if (source_file)
2638 fclose (source_file);
2639}