PR tree-optimization/78496
[official-gcc.git] / gcc / gcov.c
bloba5aa4aadcac5a358006140303edd390667405b3e
1 /* Gcov.c: prepend line execution counts and branch probabilities to a
2 source file.
3 Copyright (C) 1990-2017 Free Software Foundation, Inc.
4 Contributed by James E. Wilson of Cygnus Support.
5 Mangled by Bob Manson of Cygnus Support.
6 Mangled further by Nathan Sidwell <nathan@codesourcery.com>
8 Gcov is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 Gcov is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Gcov; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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. */
27 /* ??? Should have an option to print the number of basic blocks, and the
28 percent of them that are covered. */
30 /* Need an option to show individual block counts, and show
31 probabilities of fall through arcs. */
33 #include "config.h"
34 #define INCLUDE_ALGORITHM
35 #define INCLUDE_VECTOR
36 #include "system.h"
37 #include "coretypes.h"
38 #include "tm.h"
39 #include "intl.h"
40 #include "diagnostic.h"
41 #include "version.h"
42 #include "demangle.h"
44 #include <getopt.h>
46 #include "md5.h"
48 using namespace std;
50 #define IN_GCOV 1
51 #include "gcov-io.h"
52 #include "gcov-io.c"
54 /* The gcno file is generated by -ftest-coverage option. The gcda file is
55 generated by a program compiled with -fprofile-arcs. Their formats
56 are documented in gcov-io.h. */
58 /* The functions in this file for creating and solution program flow graphs
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. */
63 /* The code validates that the profile information read in corresponds
64 to the code currently being compiled. Rather than checking for
65 identical files, the code below compares a checksum on the CFG
66 (based on the order of basic blocks and the arcs in the CFG). If
67 the CFG checksum in the gcda file match the CFG checksum in the
68 gcno file, the profile data will be used. */
70 /* This is the size of the buffer used to read in source file lines. */
72 struct function_info;
73 struct block_info;
74 struct source_info;
76 /* Describes an arc between two basic blocks. */
78 typedef struct arc_info
80 /* source and destination blocks. */
81 struct block_info *src;
82 struct block_info *dst;
84 /* transition counts. */
85 gcov_type count;
86 /* used in cycle search, so that we do not clobber original counts. */
87 gcov_type cs_count;
89 unsigned int count_valid : 1;
90 unsigned int on_tree : 1;
91 unsigned int fake : 1;
92 unsigned int fall_through : 1;
94 /* Arc to a catch handler. */
95 unsigned int is_throw : 1;
97 /* Arc is for a function that abnormally returns. */
98 unsigned int is_call_non_return : 1;
100 /* Arc is for catch/setjmp. */
101 unsigned int is_nonlocal_return : 1;
103 /* Is an unconditional branch. */
104 unsigned int is_unconditional : 1;
106 /* Loop making arc. */
107 unsigned int cycle : 1;
109 /* Next branch on line. */
110 struct arc_info *line_next;
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;
117 /* Describes which locations (lines and files) are associated with
118 a basic block. */
120 struct block_location_info
122 block_location_info (unsigned _source_file_idx):
123 source_file_idx (_source_file_idx)
126 unsigned source_file_idx;
127 vector<unsigned> lines;
130 /* Describes a basic block. Contains lists of arcs to successor and
131 predecessor blocks. */
133 typedef struct block_info
135 /* Chain of exit and entry arcs. */
136 arc_t *succ;
137 arc_t *pred;
139 /* Number of unprocessed exit and entry arcs. */
140 gcov_type num_succ;
141 gcov_type num_pred;
143 unsigned id;
145 /* Block execution count. */
146 gcov_type count;
147 unsigned count_valid : 1;
148 unsigned valid_chain : 1;
149 unsigned invalid_chain : 1;
150 unsigned exceptional : 1;
152 /* Block is a call instrumenting site. */
153 unsigned is_call_site : 1; /* Does the call. */
154 unsigned is_call_return : 1; /* Is the return. */
156 /* Block is a landing pad for longjmp or throw. */
157 unsigned is_nonlocal_return : 1;
159 vector<block_location_info> locations;
161 struct
163 /* Single line graph cycle workspace. Used for all-blocks
164 mode. */
165 arc_t *arc;
166 unsigned ident;
167 } cycle; /* Used in all-blocks mode, after blocks are linked onto
168 lines. */
170 /* Temporary chain for solving graph, and for chaining blocks on one
171 line. */
172 struct block_info *chain;
174 } block_t;
176 /* Describes a single function. Contains an array of basic blocks. */
178 typedef struct function_info
180 function_info ();
181 ~function_info ();
183 /* Name of function. */
184 char *name;
185 char *demangled_name;
186 unsigned ident;
187 unsigned lineno_checksum;
188 unsigned cfg_checksum;
190 /* The graph contains at least one fake incoming edge. */
191 unsigned has_catch : 1;
193 /* Array of basic blocks. Like in GCC, the entry block is
194 at blocks[0] and the exit block is at blocks[1]. */
195 #define ENTRY_BLOCK (0)
196 #define EXIT_BLOCK (1)
197 vector<block_t> blocks;
198 unsigned blocks_executed;
200 /* Raw arc coverage counts. */
201 gcov_type *counts;
202 unsigned num_counts;
204 /* First line number & file. */
205 unsigned line;
206 unsigned src;
208 /* Next function in same source file. */
209 struct function_info *next_file_fn;
211 /* Next function. */
212 struct function_info *next;
213 } function_t;
215 /* Describes coverage of a file or function. */
217 typedef struct coverage_info
219 int lines;
220 int lines_executed;
222 int branches;
223 int branches_executed;
224 int branches_taken;
226 int calls;
227 int calls_executed;
229 char *name;
230 } coverage_t;
232 /* Describes a single line of source. Contains a chain of basic blocks
233 with code on it. */
235 typedef struct line_info
237 /* Return true when NEEDLE is one of basic blocks the line belongs to. */
238 bool has_block (block_t *needle);
240 gcov_type count; /* execution count */
241 arc_t *branches; /* branches from blocks that end on this line. */
242 block_t *blocks; /* blocks which start on this line.
243 Used in all-blocks mode. */
244 unsigned exists : 1;
245 unsigned unexceptional : 1;
246 } line_t;
248 bool
249 line_t::has_block (block_t *needle)
251 for (block_t *n = blocks; n; n = n->chain)
252 if (n == needle)
253 return true;
255 return false;
258 /* Describes a file mentioned in the block graph. Contains an array
259 of line info. */
261 typedef struct source_info
263 /* Canonical name of source file. */
264 char *name;
265 time_t file_time;
267 /* Array of line information. */
268 line_t *lines;
269 unsigned num_lines;
271 coverage_t coverage;
273 /* Functions in this source file. These are in ascending line
274 number order. */
275 function_t *functions;
276 } source_t;
278 typedef struct name_map
280 char *name; /* Source file name */
281 unsigned src; /* Source file */
282 } name_map_t;
284 /* Holds a list of function basic block graphs. */
286 static function_t *functions;
287 static function_t **fn_end = &functions;
289 static source_t *sources; /* Array of source files */
290 static unsigned n_sources; /* Number of sources */
291 static unsigned a_sources; /* Allocated sources */
293 static name_map_t *names; /* Mapping of file names to sources */
294 static unsigned n_names; /* Number of names */
295 static unsigned a_names; /* Allocated names */
297 /* This holds data summary information. */
299 static unsigned object_runs;
300 static unsigned program_count;
302 static unsigned total_lines;
303 static unsigned total_executed;
305 /* Modification time of graph file. */
307 static time_t bbg_file_time;
309 /* Name of the notes (gcno) output file. The "bbg" prefix is for
310 historical reasons, when the notes file contained only the
311 basic block graph notes. */
313 static char *bbg_file_name;
315 /* Stamp of the bbg file */
316 static unsigned bbg_stamp;
318 /* Name and file pointer of the input file for the count data (gcda). */
320 static char *da_file_name;
322 /* Data file is missing. */
324 static int no_data_file;
326 /* If there is several input files, compute and display results after
327 reading all data files. This way if two or more gcda file refer to
328 the same source file (eg inline subprograms in a .h file), the
329 counts are added. */
331 static int multiple_files = 0;
333 /* Output branch probabilities. */
335 static int flag_branches = 0;
337 /* Show unconditional branches too. */
338 static int flag_unconditional = 0;
340 /* Output a gcov file if this is true. This is on by default, and can
341 be turned off by the -n option. */
343 static int flag_gcov_file = 1;
345 /* Output progress indication if this is true. This is off by default
346 and can be turned on by the -d option. */
348 static int flag_display_progress = 0;
350 /* Output *.gcov file in intermediate format used by 'lcov'. */
352 static int flag_intermediate_format = 0;
354 /* Output demangled function names. */
356 static int flag_demangled_names = 0;
358 /* For included files, make the gcov output file name include the name
359 of the input source file. For example, if x.h is included in a.c,
360 then the output file name is a.c##x.h.gcov instead of x.h.gcov. */
362 static int flag_long_names = 0;
364 /* For situations when a long name can potentially hit filesystem path limit,
365 let's calculate md5sum of the path and append it to a file name. */
367 static int flag_hash_filenames = 0;
369 /* Print verbose informations. */
371 static int flag_verbose = 0;
373 /* Output count information for every basic block, not merely those
374 that contain line number information. */
376 static int flag_all_blocks = 0;
378 /* Output summary info for each function. */
380 static int flag_function_summary = 0;
382 /* Object directory file prefix. This is the directory/file where the
383 graph and data files are looked for, if nonzero. */
385 static char *object_directory = 0;
387 /* Source directory prefix. This is removed from source pathnames
388 that match, when generating the output file name. */
390 static char *source_prefix = 0;
391 static size_t source_length = 0;
393 /* Only show data for sources with relative pathnames. Absolute ones
394 usually indicate a system header file, which although it may
395 contain inline functions, is usually uninteresting. */
396 static int flag_relative_only = 0;
398 /* Preserve all pathname components. Needed when object files and
399 source files are in subdirectories. '/' is mangled as '#', '.' is
400 elided and '..' mangled to '^'. */
402 static int flag_preserve_paths = 0;
404 /* Output the number of times a branch was taken as opposed to the percentage
405 of times it was taken. */
407 static int flag_counts = 0;
409 /* Forward declarations. */
410 static int process_args (int, char **);
411 static void print_usage (int) ATTRIBUTE_NORETURN;
412 static void print_version (void) ATTRIBUTE_NORETURN;
413 static void process_file (const char *);
414 static void generate_results (const char *);
415 static void create_file_names (const char *);
416 static int name_search (const void *, const void *);
417 static int name_sort (const void *, const void *);
418 static char *canonicalize_name (const char *);
419 static unsigned find_source (const char *);
420 static function_t *read_graph_file (void);
421 static int read_count_file (function_t *);
422 static void solve_flow_graph (function_t *);
423 static void find_exception_blocks (function_t *);
424 static void add_branch_counts (coverage_t *, const arc_t *);
425 static void add_line_counts (coverage_t *, function_t *);
426 static void executed_summary (unsigned, unsigned);
427 static void function_summary (const coverage_t *, const char *);
428 static const char *format_gcov (gcov_type, gcov_type, int);
429 static void accumulate_line_counts (source_t *);
430 static void output_gcov_file (const char *, source_t *);
431 static int output_branch_count (FILE *, int, const arc_t *);
432 static void output_lines (FILE *, const source_t *);
433 static char *make_gcov_file_name (const char *, const char *);
434 static char *mangle_name (const char *, char *);
435 static void release_structures (void);
436 extern int main (int, char **);
438 function_info::function_info (): name (NULL), demangled_name (NULL),
439 ident (0), lineno_checksum (0), cfg_checksum (0), has_catch (0),
440 blocks (), blocks_executed (0), counts (NULL), num_counts (0),
441 line (0), src (0), next_file_fn (NULL), next (NULL)
445 function_info::~function_info ()
447 for (int i = blocks.size () - 1; i >= 0; i--)
449 arc_t *arc, *arc_n;
451 for (arc = blocks[i].succ; arc; arc = arc_n)
453 arc_n = arc->succ_next;
454 free (arc);
457 free (counts);
458 if (flag_demangled_names && demangled_name != name)
459 free (demangled_name);
460 free (name);
463 /* Cycle detection!
464 There are a bajillion algorithms that do this. Boost's function is named
465 hawick_cycles, so I used the algorithm by K. A. Hawick and H. A. James in
466 "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
467 (url at <http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf>).
469 The basic algorithm is simple: effectively, we're finding all simple paths
470 in a subgraph (that shrinks every iteration). Duplicates are filtered by
471 "blocking" a path when a node is added to the path (this also prevents non-
472 simple paths)--the node is unblocked only when it participates in a cycle.
475 typedef vector<arc_t *> arc_vector_t;
476 typedef vector<const block_t *> block_vector_t;
478 /* Enum with types of loop in CFG. */
480 enum loop_type
482 NO_LOOP = 0,
483 LOOP = 1,
484 NEGATIVE_LOOP = 3
487 /* Loop_type operator that merges two values: A and B. */
489 inline loop_type& operator |= (loop_type& a, loop_type b)
491 return a = static_cast<loop_type> (a | b);
494 /* Handle cycle identified by EDGES, where the function finds minimum cs_count
495 and subtract the value from all counts. The subtracted value is added
496 to COUNT. Returns type of loop. */
498 static loop_type
499 handle_cycle (const arc_vector_t &edges, int64_t &count)
501 /* Find the minimum edge of the cycle, and reduce all nodes in the cycle by
502 that amount. */
503 int64_t cycle_count = INTTYPE_MAXIMUM (int64_t);
504 for (unsigned i = 0; i < edges.size (); i++)
506 int64_t ecount = edges[i]->cs_count;
507 if (cycle_count > ecount)
508 cycle_count = ecount;
510 count += cycle_count;
511 for (unsigned i = 0; i < edges.size (); i++)
512 edges[i]->cs_count -= cycle_count;
514 return cycle_count < 0 ? NEGATIVE_LOOP : LOOP;
517 /* Unblock a block U from BLOCKED. Apart from that, iterate all blocks
518 blocked by U in BLOCK_LISTS. */
520 static void
521 unblock (const block_t *u, block_vector_t &blocked,
522 vector<block_vector_t > &block_lists)
524 block_vector_t::iterator it = find (blocked.begin (), blocked.end (), u);
525 if (it == blocked.end ())
526 return;
528 unsigned index = it - blocked.begin ();
529 blocked.erase (it);
531 for (block_vector_t::iterator it2 = block_lists[index].begin ();
532 it2 != block_lists[index].end (); it2++)
533 unblock (*it2, blocked, block_lists);
534 for (unsigned j = 0; j < block_lists[index].size (); j++)
535 unblock (u, blocked, block_lists);
537 block_lists.erase (block_lists.begin () + index);
540 /* Find circuit going to block V, PATH is provisional seen cycle.
541 BLOCKED is vector of blocked vertices, BLOCK_LISTS contains vertices
542 blocked by a block. COUNT is accumulated count of the current LINE.
543 Returns what type of loop it contains. */
545 static loop_type
546 circuit (block_t *v, arc_vector_t &path, block_t *start,
547 block_vector_t &blocked, vector<block_vector_t> &block_lists,
548 line_t &linfo, int64_t &count)
550 loop_type result = NO_LOOP;
552 /* Add v to the block list. */
553 gcc_assert (find (blocked.begin (), blocked.end (), v) == blocked.end ());
554 blocked.push_back (v);
555 block_lists.push_back (block_vector_t ());
557 for (arc_t *arc = v->succ; arc; arc = arc->succ_next)
559 block_t *w = arc->dst;
560 if (w < start || !linfo.has_block (w))
561 continue;
563 path.push_back (arc);
564 if (w == start)
565 /* Cycle has been found. */
566 result |= handle_cycle (path, count);
567 else if (find (blocked.begin (), blocked.end (), w) == blocked.end ())
568 result |= circuit (w, path, start, blocked, block_lists, linfo, count);
570 path.pop_back ();
573 if (result != NO_LOOP)
574 unblock (v, blocked, block_lists);
575 else
576 for (arc_t *arc = v->succ; arc; arc = arc->succ_next)
578 block_t *w = arc->dst;
579 if (w < start || !linfo.has_block (w))
580 continue;
582 size_t index
583 = find (blocked.begin (), blocked.end (), w) - blocked.begin ();
584 gcc_assert (index < blocked.size ());
585 block_vector_t &list = block_lists[index];
586 if (find (list.begin (), list.end (), v) == list.end ())
587 list.push_back (v);
590 return result;
593 /* Find cycles for a LINFO. If HANDLE_NEGATIVE_CYCLES is set and the line
594 contains a negative loop, then perform the same function once again. */
596 static gcov_type
597 get_cycles_count (line_t &linfo, bool handle_negative_cycles = true)
599 /* Note that this algorithm works even if blocks aren't in sorted order.
600 Each iteration of the circuit detection is completely independent
601 (except for reducing counts, but that shouldn't matter anyways).
602 Therefore, operating on a permuted order (i.e., non-sorted) only
603 has the effect of permuting the output cycles. */
605 loop_type result = NO_LOOP;
606 gcov_type count = 0;
607 for (block_t *block = linfo.blocks; block; block = block->chain)
609 arc_vector_t path;
610 block_vector_t blocked;
611 vector<block_vector_t > block_lists;
612 result |= circuit (block, path, block, blocked, block_lists, linfo,
613 count);
616 /* If we have a negative cycle, repeat the find_cycles routine. */
617 if (result == NEGATIVE_LOOP && handle_negative_cycles)
618 count += get_cycles_count (linfo, false);
620 return count;
624 main (int argc, char **argv)
626 int argno;
627 int first_arg;
628 const char *p;
630 p = argv[0] + strlen (argv[0]);
631 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
632 --p;
633 progname = p;
635 xmalloc_set_program_name (progname);
637 /* Unlock the stdio streams. */
638 unlock_std_streams ();
640 gcc_init_libintl ();
642 diagnostic_initialize (global_dc, 0);
644 /* Handle response files. */
645 expandargv (&argc, &argv);
647 a_names = 10;
648 names = XNEWVEC (name_map_t, a_names);
649 a_sources = 10;
650 sources = XNEWVEC (source_t, a_sources);
652 argno = process_args (argc, argv);
653 if (optind == argc)
654 print_usage (true);
656 if (argc - argno > 1)
657 multiple_files = 1;
659 first_arg = argno;
661 for (; argno != argc; argno++)
663 if (flag_display_progress)
664 printf ("Processing file %d out of %d\n", argno - first_arg + 1,
665 argc - first_arg);
666 process_file (argv[argno]);
669 generate_results (multiple_files ? NULL : argv[argc - 1]);
671 release_structures ();
673 return 0;
676 /* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
677 otherwise the output of --help. */
679 static void
680 print_usage (int error_p)
682 FILE *file = error_p ? stderr : stdout;
683 int status = error_p ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
685 fnotice (file, "Usage: gcov [OPTION...] SOURCE|OBJ...\n\n");
686 fnotice (file, "Print code coverage information.\n\n");
687 fnotice (file, " -a, --all-blocks Show information for every basic block\n");
688 fnotice (file, " -b, --branch-probabilities Include branch probabilities in output\n");
689 fnotice (file, " -c, --branch-counts Output counts of branches taken\n\
690 rather than percentages\n");
691 fnotice (file, " -d, --display-progress Display progress information\n");
692 fnotice (file, " -f, --function-summaries Output summaries for each function\n");
693 fnotice (file, " -h, --help Print this help, then exit\n");
694 fnotice (file, " -i, --intermediate-format Output .gcov file in intermediate text format\n");
695 fnotice (file, " -l, --long-file-names Use long output file names for included\n\
696 source files\n");
697 fnotice (file, " -m, --demangled-names Output demangled function names\n");
698 fnotice (file, " -n, --no-output Do not create an output file\n");
699 fnotice (file, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
700 fnotice (file, " -p, --preserve-paths Preserve all pathname components\n");
701 fnotice (file, " -r, --relative-only Only show data for relative sources\n");
702 fnotice (file, " -s, --source-prefix DIR Source prefix to elide\n");
703 fnotice (file, " -u, --unconditional-branches Show unconditional branch counts too\n");
704 fnotice (file, " -v, --version Print version number, then exit\n");
705 fnotice (file, " -w, --verbose Print verbose informations\n");
706 fnotice (file, " -x, --hash-filenames Hash long pathnames\n");
707 fnotice (file, "\nFor bug reporting instructions, please see:\n%s.\n",
708 bug_report_url);
709 exit (status);
712 /* Print version information and exit. */
714 static void
715 print_version (void)
717 fnotice (stdout, "gcov %s%s\n", pkgversion_string, version_string);
718 fprintf (stdout, "Copyright %s 2017 Free Software Foundation, Inc.\n",
719 _("(C)"));
720 fnotice (stdout,
721 _("This is free software; see the source for copying conditions.\n"
722 "There is NO warranty; not even for MERCHANTABILITY or \n"
723 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
724 exit (SUCCESS_EXIT_CODE);
727 static const struct option options[] =
729 { "help", no_argument, NULL, 'h' },
730 { "version", no_argument, NULL, 'v' },
731 { "verbose", no_argument, NULL, 'w' },
732 { "all-blocks", no_argument, NULL, 'a' },
733 { "branch-probabilities", no_argument, NULL, 'b' },
734 { "branch-counts", no_argument, NULL, 'c' },
735 { "intermediate-format", no_argument, NULL, 'i' },
736 { "no-output", no_argument, NULL, 'n' },
737 { "long-file-names", no_argument, NULL, 'l' },
738 { "function-summaries", no_argument, NULL, 'f' },
739 { "demangled-names", no_argument, NULL, 'm' },
740 { "preserve-paths", no_argument, NULL, 'p' },
741 { "relative-only", no_argument, NULL, 'r' },
742 { "object-directory", required_argument, NULL, 'o' },
743 { "object-file", required_argument, NULL, 'o' },
744 { "source-prefix", required_argument, NULL, 's' },
745 { "unconditional-branches", no_argument, NULL, 'u' },
746 { "display-progress", no_argument, NULL, 'd' },
747 { "hash-filenames", no_argument, NULL, 'x' },
748 { 0, 0, 0, 0 }
751 /* Process args, return index to first non-arg. */
753 static int
754 process_args (int argc, char **argv)
756 int opt;
758 const char *opts = "abcdfhilmno:prs:uvwx";
759 while ((opt = getopt_long (argc, argv, opts, options, NULL)) != -1)
761 switch (opt)
763 case 'a':
764 flag_all_blocks = 1;
765 break;
766 case 'b':
767 flag_branches = 1;
768 break;
769 case 'c':
770 flag_counts = 1;
771 break;
772 case 'f':
773 flag_function_summary = 1;
774 break;
775 case 'h':
776 print_usage (false);
777 /* print_usage will exit. */
778 case 'l':
779 flag_long_names = 1;
780 break;
781 case 'm':
782 flag_demangled_names = 1;
783 break;
784 case 'n':
785 flag_gcov_file = 0;
786 break;
787 case 'o':
788 object_directory = optarg;
789 break;
790 case 's':
791 source_prefix = optarg;
792 source_length = strlen (source_prefix);
793 break;
794 case 'r':
795 flag_relative_only = 1;
796 break;
797 case 'p':
798 flag_preserve_paths = 1;
799 break;
800 case 'u':
801 flag_unconditional = 1;
802 break;
803 case 'i':
804 flag_intermediate_format = 1;
805 flag_gcov_file = 1;
806 break;
807 case 'd':
808 flag_display_progress = 1;
809 break;
810 case 'x':
811 flag_hash_filenames = 1;
812 break;
813 case 'w':
814 flag_verbose = 1;
815 break;
816 case 'v':
817 print_version ();
818 /* print_version will exit. */
819 default:
820 print_usage (true);
821 /* print_usage will exit. */
825 return optind;
828 /* Output the result in intermediate format used by 'lcov'.
830 The intermediate format contains a single file named 'foo.cc.gcov',
831 with no source code included. A sample output is
833 file:foo.cc
834 function:5,1,_Z3foov
835 function:13,1,main
836 function:19,1,_GLOBAL__sub_I__Z3foov
837 function:19,1,_Z41__static_initialization_and_destruction_0ii
838 lcount:5,1
839 lcount:7,9
840 lcount:9,8
841 lcount:11,1
842 file:/.../iostream
843 lcount:74,1
844 file:/.../basic_ios.h
845 file:/.../ostream
846 file:/.../ios_base.h
847 function:157,0,_ZStorSt12_Ios_IostateS_
848 lcount:157,0
849 file:/.../char_traits.h
850 function:258,0,_ZNSt11char_traitsIcE6lengthEPKc
851 lcount:258,0
854 The default gcov outputs multiple files: 'foo.cc.gcov',
855 'iostream.gcov', 'ios_base.h.gcov', etc. with source code
856 included. Instead the intermediate format here outputs only a single
857 file 'foo.cc.gcov' similar to the above example. */
859 static void
860 output_intermediate_file (FILE *gcov_file, source_t *src)
862 unsigned line_num; /* current line number. */
863 const line_t *line; /* current line info ptr. */
864 function_t *fn; /* current function info ptr. */
866 fprintf (gcov_file, "file:%s\n", src->name); /* source file name */
868 for (fn = src->functions; fn; fn = fn->next_file_fn)
870 /* function:<name>,<line_number>,<execution_count> */
871 fprintf (gcov_file, "function:%d,%s,%s\n", fn->line,
872 format_gcov (fn->blocks[0].count, 0, -1),
873 flag_demangled_names ? fn->demangled_name : fn->name);
876 for (line_num = 1, line = &src->lines[line_num];
877 line_num < src->num_lines;
878 line_num++, line++)
880 arc_t *arc;
881 if (line->exists)
882 fprintf (gcov_file, "lcount:%u,%s\n", line_num,
883 format_gcov (line->count, 0, -1));
884 if (flag_branches)
885 for (arc = line->branches; arc; arc = arc->line_next)
887 if (!arc->is_unconditional && !arc->is_call_non_return)
889 const char *branch_type;
890 /* branch:<line_num>,<branch_coverage_type>
891 branch_coverage_type
892 : notexec (Branch not executed)
893 : taken (Branch executed and taken)
894 : nottaken (Branch executed, but not taken)
896 if (arc->src->count)
897 branch_type = (arc->count > 0) ? "taken" : "nottaken";
898 else
899 branch_type = "notexec";
900 fprintf (gcov_file, "branch:%d,%s\n", line_num, branch_type);
906 /* Process a single input file. */
908 static void
909 process_file (const char *file_name)
911 function_t *fns;
913 create_file_names (file_name);
914 fns = read_graph_file ();
915 if (!fns)
916 return;
918 read_count_file (fns);
919 while (fns)
921 function_t *fn = fns;
923 fns = fn->next;
924 fn->next = NULL;
925 if (fn->counts || no_data_file)
927 unsigned src = fn->src;
928 unsigned line = fn->line;
929 unsigned block_no;
930 function_t *probe, **prev;
932 /* Now insert it into the source file's list of
933 functions. Normally functions will be encountered in
934 ascending order, so a simple scan is quick. Note we're
935 building this list in reverse order. */
936 for (prev = &sources[src].functions;
937 (probe = *prev); prev = &probe->next_file_fn)
938 if (probe->line <= line)
939 break;
940 fn->next_file_fn = probe;
941 *prev = fn;
943 /* Mark last line in files touched by function. */
944 for (block_no = 0; block_no != fn->blocks.size (); block_no++)
946 block_t *block = &fn->blocks[block_no];
947 for (unsigned i = 0; i < block->locations.size (); i++)
949 unsigned s = block->locations[i].source_file_idx;
951 /* Sort lines of locations. */
952 sort (block->locations[i].lines.begin (),
953 block->locations[i].lines.end ());
955 if (!block->locations[i].lines.empty ())
957 unsigned last_line
958 = block->locations[i].lines.back () + 1;
959 if (last_line > sources[s].num_lines)
960 sources[s].num_lines = last_line;
965 solve_flow_graph (fn);
966 if (fn->has_catch)
967 find_exception_blocks (fn);
968 *fn_end = fn;
969 fn_end = &fn->next;
971 else
972 /* The function was not in the executable -- some other
973 instance must have been selected. */
974 delete fn;
978 static void
979 output_gcov_file (const char *file_name, source_t *src)
981 char *gcov_file_name = make_gcov_file_name (file_name, src->coverage.name);
983 if (src->coverage.lines)
985 FILE *gcov_file = fopen (gcov_file_name, "w");
986 if (gcov_file)
988 fnotice (stdout, "Creating '%s'\n", gcov_file_name);
990 if (flag_intermediate_format)
991 output_intermediate_file (gcov_file, src);
992 else
993 output_lines (gcov_file, src);
994 if (ferror (gcov_file))
995 fnotice (stderr, "Error writing output file '%s'\n", gcov_file_name);
996 fclose (gcov_file);
998 else
999 fnotice (stderr, "Could not open output file '%s'\n", gcov_file_name);
1001 else
1003 unlink (gcov_file_name);
1004 fnotice (stdout, "Removing '%s'\n", gcov_file_name);
1006 free (gcov_file_name);
1009 static void
1010 generate_results (const char *file_name)
1012 unsigned ix;
1013 source_t *src;
1014 function_t *fn;
1016 for (ix = n_sources, src = sources; ix--; src++)
1017 if (src->num_lines)
1018 src->lines = XCNEWVEC (line_t, src->num_lines);
1020 for (fn = functions; fn; fn = fn->next)
1022 coverage_t coverage;
1024 memset (&coverage, 0, sizeof (coverage));
1025 coverage.name = flag_demangled_names ? fn->demangled_name : fn->name;
1026 add_line_counts (flag_function_summary ? &coverage : NULL, fn);
1027 if (flag_function_summary)
1029 function_summary (&coverage, "Function");
1030 fnotice (stdout, "\n");
1034 if (file_name)
1036 name_map_t *name_map = (name_map_t *)bsearch
1037 (file_name, names, n_names, sizeof (*names), name_search);
1038 if (name_map)
1039 file_name = sources[name_map->src].coverage.name;
1040 else
1041 file_name = canonicalize_name (file_name);
1044 for (ix = n_sources, src = sources; ix--; src++)
1046 if (flag_relative_only)
1048 /* Ignore this source, if it is an absolute path (after
1049 source prefix removal). */
1050 char first = src->coverage.name[0];
1052 #if HAVE_DOS_BASED_FILE_SYSTEM
1053 if (first && src->coverage.name[1] == ':')
1054 first = src->coverage.name[2];
1055 #endif
1056 if (IS_DIR_SEPARATOR (first))
1057 continue;
1060 accumulate_line_counts (src);
1061 function_summary (&src->coverage, "File");
1062 total_lines += src->coverage.lines;
1063 total_executed += src->coverage.lines_executed;
1064 if (flag_gcov_file)
1066 output_gcov_file (file_name, src);
1067 fnotice (stdout, "\n");
1071 if (!file_name)
1072 executed_summary (total_lines, total_executed);
1075 /* Release all memory used. */
1077 static void
1078 release_structures (void)
1080 unsigned ix;
1081 function_t *fn;
1083 for (ix = n_sources; ix--;)
1084 free (sources[ix].lines);
1085 free (sources);
1087 for (ix = n_names; ix--;)
1088 free (names[ix].name);
1089 free (names);
1091 while ((fn = functions))
1093 functions = fn->next;
1094 delete fn;
1098 /* Generate the names of the graph and data files. If OBJECT_DIRECTORY
1099 is not specified, these are named from FILE_NAME sans extension. If
1100 OBJECT_DIRECTORY is specified and is a directory, the files are in that
1101 directory, but named from the basename of the FILE_NAME, sans extension.
1102 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
1103 and the data files are named from that. */
1105 static void
1106 create_file_names (const char *file_name)
1108 char *cptr;
1109 char *name;
1110 int length = strlen (file_name);
1111 int base;
1113 /* Free previous file names. */
1114 free (bbg_file_name);
1115 free (da_file_name);
1116 da_file_name = bbg_file_name = NULL;
1117 bbg_file_time = 0;
1118 bbg_stamp = 0;
1120 if (object_directory && object_directory[0])
1122 struct stat status;
1124 length += strlen (object_directory) + 2;
1125 name = XNEWVEC (char, length);
1126 name[0] = 0;
1128 base = !stat (object_directory, &status) && S_ISDIR (status.st_mode);
1129 strcat (name, object_directory);
1130 if (base && (!IS_DIR_SEPARATOR (name[strlen (name) - 1])))
1131 strcat (name, "/");
1133 else
1135 name = XNEWVEC (char, length + 1);
1136 strcpy (name, file_name);
1137 base = 0;
1140 if (base)
1142 /* Append source file name. */
1143 const char *cptr = lbasename (file_name);
1144 strcat (name, cptr ? cptr : file_name);
1147 /* Remove the extension. */
1148 cptr = strrchr (CONST_CAST (char *, lbasename (name)), '.');
1149 if (cptr)
1150 *cptr = 0;
1152 length = strlen (name);
1154 bbg_file_name = XNEWVEC (char, length + strlen (GCOV_NOTE_SUFFIX) + 1);
1155 strcpy (bbg_file_name, name);
1156 strcpy (bbg_file_name + length, GCOV_NOTE_SUFFIX);
1158 da_file_name = XNEWVEC (char, length + strlen (GCOV_DATA_SUFFIX) + 1);
1159 strcpy (da_file_name, name);
1160 strcpy (da_file_name + length, GCOV_DATA_SUFFIX);
1162 free (name);
1163 return;
1166 /* A is a string and B is a pointer to name_map_t. Compare for file
1167 name orderability. */
1169 static int
1170 name_search (const void *a_, const void *b_)
1172 const char *a = (const char *)a_;
1173 const name_map_t *b = (const name_map_t *)b_;
1175 #if HAVE_DOS_BASED_FILE_SYSTEM
1176 return strcasecmp (a, b->name);
1177 #else
1178 return strcmp (a, b->name);
1179 #endif
1182 /* A and B are a pointer to name_map_t. Compare for file name
1183 orderability. */
1185 static int
1186 name_sort (const void *a_, const void *b_)
1188 const name_map_t *a = (const name_map_t *)a_;
1189 return name_search (a->name, b_);
1192 /* Find or create a source file structure for FILE_NAME. Copies
1193 FILE_NAME on creation */
1195 static unsigned
1196 find_source (const char *file_name)
1198 name_map_t *name_map;
1199 char *canon;
1200 unsigned idx;
1201 struct stat status;
1203 if (!file_name)
1204 file_name = "<unknown>";
1205 name_map = (name_map_t *)bsearch
1206 (file_name, names, n_names, sizeof (*names), name_search);
1207 if (name_map)
1209 idx = name_map->src;
1210 goto check_date;
1213 if (n_names + 2 > a_names)
1215 /* Extend the name map array -- we'll be inserting one or two
1216 entries. */
1217 a_names *= 2;
1218 name_map = XNEWVEC (name_map_t, a_names);
1219 memcpy (name_map, names, n_names * sizeof (*names));
1220 free (names);
1221 names = name_map;
1224 /* Not found, try the canonical name. */
1225 canon = canonicalize_name (file_name);
1226 name_map = (name_map_t *) bsearch (canon, names, n_names, sizeof (*names),
1227 name_search);
1228 if (!name_map)
1230 /* Not found with canonical name, create a new source. */
1231 source_t *src;
1233 if (n_sources == a_sources)
1235 a_sources *= 2;
1236 src = XNEWVEC (source_t, a_sources);
1237 memcpy (src, sources, n_sources * sizeof (*sources));
1238 free (sources);
1239 sources = src;
1242 idx = n_sources;
1244 name_map = &names[n_names++];
1245 name_map->name = canon;
1246 name_map->src = idx;
1248 src = &sources[n_sources++];
1249 memset (src, 0, sizeof (*src));
1250 src->name = canon;
1251 src->coverage.name = src->name;
1252 if (source_length
1253 #if HAVE_DOS_BASED_FILE_SYSTEM
1254 /* You lose if separators don't match exactly in the
1255 prefix. */
1256 && !strncasecmp (source_prefix, src->coverage.name, source_length)
1257 #else
1258 && !strncmp (source_prefix, src->coverage.name, source_length)
1259 #endif
1260 && IS_DIR_SEPARATOR (src->coverage.name[source_length]))
1261 src->coverage.name += source_length + 1;
1262 if (!stat (src->name, &status))
1263 src->file_time = status.st_mtime;
1265 else
1266 idx = name_map->src;
1268 if (name_search (file_name, name_map))
1270 /* Append the non-canonical name. */
1271 name_map = &names[n_names++];
1272 name_map->name = xstrdup (file_name);
1273 name_map->src = idx;
1276 /* Resort the name map. */
1277 qsort (names, n_names, sizeof (*names), name_sort);
1279 check_date:
1280 if (sources[idx].file_time > bbg_file_time)
1282 static int info_emitted;
1284 fnotice (stderr, "%s:source file is newer than notes file '%s'\n",
1285 file_name, bbg_file_name);
1286 if (!info_emitted)
1288 fnotice (stderr,
1289 "(the message is displayed only once per source file)\n");
1290 info_emitted = 1;
1292 sources[idx].file_time = 0;
1295 return idx;
1298 /* Read the notes file. Return list of functions read -- in reverse order. */
1300 static function_t *
1301 read_graph_file (void)
1303 unsigned version;
1304 unsigned current_tag = 0;
1305 function_t *fn = NULL;
1306 function_t *fns = NULL;
1307 function_t **fns_end = &fns;
1308 unsigned tag;
1310 if (!gcov_open (bbg_file_name, 1))
1312 fnotice (stderr, "%s:cannot open notes file\n", bbg_file_name);
1313 return fns;
1315 bbg_file_time = gcov_time ();
1316 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC))
1318 fnotice (stderr, "%s:not a gcov notes file\n", bbg_file_name);
1319 gcov_close ();
1320 return fns;
1323 version = gcov_read_unsigned ();
1324 if (version != GCOV_VERSION)
1326 char v[4], e[4];
1328 GCOV_UNSIGNED2STRING (v, version);
1329 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
1331 fnotice (stderr, "%s:version '%.4s', prefer '%.4s'\n",
1332 bbg_file_name, v, e);
1334 bbg_stamp = gcov_read_unsigned ();
1336 while ((tag = gcov_read_unsigned ()))
1338 unsigned length = gcov_read_unsigned ();
1339 gcov_position_t base = gcov_position ();
1341 if (tag == GCOV_TAG_FUNCTION)
1343 char *function_name;
1344 unsigned ident, lineno;
1345 unsigned lineno_checksum, cfg_checksum;
1347 ident = gcov_read_unsigned ();
1348 lineno_checksum = gcov_read_unsigned ();
1349 cfg_checksum = gcov_read_unsigned ();
1350 function_name = xstrdup (gcov_read_string ());
1351 unsigned src_idx = find_source (gcov_read_string ());
1352 lineno = gcov_read_unsigned ();
1354 fn = new function_t;
1355 fn->name = function_name;
1356 if (flag_demangled_names)
1358 fn->demangled_name = cplus_demangle (fn->name, DMGL_PARAMS);
1359 if (!fn->demangled_name)
1360 fn->demangled_name = fn->name;
1362 fn->ident = ident;
1363 fn->lineno_checksum = lineno_checksum;
1364 fn->cfg_checksum = cfg_checksum;
1365 fn->src = src_idx;
1366 fn->line = lineno;
1368 fn->next_file_fn = NULL;
1369 fn->next = NULL;
1370 *fns_end = fn;
1371 fns_end = &fn->next;
1372 current_tag = tag;
1374 else if (fn && tag == GCOV_TAG_BLOCKS)
1376 if (!fn->blocks.empty ())
1377 fnotice (stderr, "%s:already seen blocks for '%s'\n",
1378 bbg_file_name, fn->name);
1379 else
1380 fn->blocks.resize (gcov_read_unsigned ());
1382 else if (fn && tag == GCOV_TAG_ARCS)
1384 unsigned src = gcov_read_unsigned ();
1385 fn->blocks[src].id = src;
1386 unsigned num_dests = GCOV_TAG_ARCS_NUM (length);
1387 block_t *src_blk = &fn->blocks[src];
1388 unsigned mark_catches = 0;
1389 struct arc_info *arc;
1391 if (src >= fn->blocks.size () || fn->blocks[src].succ)
1392 goto corrupt;
1394 while (num_dests--)
1396 unsigned dest = gcov_read_unsigned ();
1397 unsigned flags = gcov_read_unsigned ();
1399 if (dest >= fn->blocks.size ())
1400 goto corrupt;
1401 arc = XCNEW (arc_t);
1403 arc->dst = &fn->blocks[dest];
1404 arc->src = src_blk;
1406 arc->count = 0;
1407 arc->count_valid = 0;
1408 arc->on_tree = !!(flags & GCOV_ARC_ON_TREE);
1409 arc->fake = !!(flags & GCOV_ARC_FAKE);
1410 arc->fall_through = !!(flags & GCOV_ARC_FALLTHROUGH);
1412 arc->succ_next = src_blk->succ;
1413 src_blk->succ = arc;
1414 src_blk->num_succ++;
1416 arc->pred_next = fn->blocks[dest].pred;
1417 fn->blocks[dest].pred = arc;
1418 fn->blocks[dest].num_pred++;
1420 if (arc->fake)
1422 if (src)
1424 /* Exceptional exit from this function, the
1425 source block must be a call. */
1426 fn->blocks[src].is_call_site = 1;
1427 arc->is_call_non_return = 1;
1428 mark_catches = 1;
1430 else
1432 /* Non-local return from a callee of this
1433 function. The destination block is a setjmp. */
1434 arc->is_nonlocal_return = 1;
1435 fn->blocks[dest].is_nonlocal_return = 1;
1439 if (!arc->on_tree)
1440 fn->num_counts++;
1443 if (mark_catches)
1445 /* We have a fake exit from this block. The other
1446 non-fall through exits must be to catch handlers.
1447 Mark them as catch arcs. */
1449 for (arc = src_blk->succ; arc; arc = arc->succ_next)
1450 if (!arc->fake && !arc->fall_through)
1452 arc->is_throw = 1;
1453 fn->has_catch = 1;
1457 else if (fn && tag == GCOV_TAG_LINES)
1459 unsigned blockno = gcov_read_unsigned ();
1460 block_t *block = &fn->blocks[blockno];
1462 if (blockno >= fn->blocks.size ())
1463 goto corrupt;
1465 while (true)
1467 unsigned lineno = gcov_read_unsigned ();
1469 if (lineno)
1470 block->locations.back ().lines.push_back (lineno);
1471 else
1473 const char *file_name = gcov_read_string ();
1475 if (!file_name)
1476 break;
1477 block->locations.push_back (block_location_info
1478 (find_source (file_name)));
1482 else if (current_tag && !GCOV_TAG_IS_SUBTAG (current_tag, tag))
1484 fn = NULL;
1485 current_tag = 0;
1487 gcov_sync (base, length);
1488 if (gcov_is_error ())
1490 corrupt:;
1491 fnotice (stderr, "%s:corrupted\n", bbg_file_name);
1492 break;
1495 gcov_close ();
1497 if (!fns)
1498 fnotice (stderr, "%s:no functions found\n", bbg_file_name);
1500 return fns;
1503 /* Reads profiles from the count file and attach to each
1504 function. Return nonzero if fatal error. */
1506 static int
1507 read_count_file (function_t *fns)
1509 unsigned ix;
1510 unsigned version;
1511 unsigned tag;
1512 function_t *fn = NULL;
1513 int error = 0;
1515 if (!gcov_open (da_file_name, 1))
1517 fnotice (stderr, "%s:cannot open data file, assuming not executed\n",
1518 da_file_name);
1519 no_data_file = 1;
1520 return 0;
1522 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
1524 fnotice (stderr, "%s:not a gcov data file\n", da_file_name);
1525 cleanup:;
1526 gcov_close ();
1527 return 1;
1529 version = gcov_read_unsigned ();
1530 if (version != GCOV_VERSION)
1532 char v[4], e[4];
1534 GCOV_UNSIGNED2STRING (v, version);
1535 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
1537 fnotice (stderr, "%s:version '%.4s', prefer version '%.4s'\n",
1538 da_file_name, v, e);
1540 tag = gcov_read_unsigned ();
1541 if (tag != bbg_stamp)
1543 fnotice (stderr, "%s:stamp mismatch with notes file\n", da_file_name);
1544 goto cleanup;
1547 while ((tag = gcov_read_unsigned ()))
1549 unsigned length = gcov_read_unsigned ();
1550 unsigned long base = gcov_position ();
1552 if (tag == GCOV_TAG_PROGRAM_SUMMARY)
1554 struct gcov_summary summary;
1555 gcov_read_summary (&summary);
1556 object_runs += summary.ctrs[GCOV_COUNTER_ARCS].runs;
1557 program_count++;
1559 else if (tag == GCOV_TAG_FUNCTION && !length)
1560 ; /* placeholder */
1561 else if (tag == GCOV_TAG_FUNCTION && length == GCOV_TAG_FUNCTION_LENGTH)
1563 unsigned ident;
1564 struct function_info *fn_n;
1566 /* Try to find the function in the list. To speed up the
1567 search, first start from the last function found. */
1568 ident = gcov_read_unsigned ();
1569 fn_n = fns;
1570 for (fn = fn ? fn->next : NULL; ; fn = fn->next)
1572 if (fn)
1574 else if ((fn = fn_n))
1575 fn_n = NULL;
1576 else
1578 fnotice (stderr, "%s:unknown function '%u'\n",
1579 da_file_name, ident);
1580 break;
1582 if (fn->ident == ident)
1583 break;
1586 if (!fn)
1588 else if (gcov_read_unsigned () != fn->lineno_checksum
1589 || gcov_read_unsigned () != fn->cfg_checksum)
1591 mismatch:;
1592 fnotice (stderr, "%s:profile mismatch for '%s'\n",
1593 da_file_name, fn->name);
1594 goto cleanup;
1597 else if (tag == GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS) && fn)
1599 if (length != GCOV_TAG_COUNTER_LENGTH (fn->num_counts))
1600 goto mismatch;
1602 if (!fn->counts)
1603 fn->counts = XCNEWVEC (gcov_type, fn->num_counts);
1605 for (ix = 0; ix != fn->num_counts; ix++)
1606 fn->counts[ix] += gcov_read_counter ();
1608 gcov_sync (base, length);
1609 if ((error = gcov_is_error ()))
1611 fnotice (stderr,
1612 error < 0
1613 ? N_("%s:overflowed\n")
1614 : N_("%s:corrupted\n"),
1615 da_file_name);
1616 goto cleanup;
1620 gcov_close ();
1621 return 0;
1624 /* Solve the flow graph. Propagate counts from the instrumented arcs
1625 to the blocks and the uninstrumented arcs. */
1627 static void
1628 solve_flow_graph (function_t *fn)
1630 unsigned ix;
1631 arc_t *arc;
1632 gcov_type *count_ptr = fn->counts;
1633 block_t *blk;
1634 block_t *valid_blocks = NULL; /* valid, but unpropagated blocks. */
1635 block_t *invalid_blocks = NULL; /* invalid, but inferable blocks. */
1637 /* The arcs were built in reverse order. Fix that now. */
1638 for (ix = fn->blocks.size (); ix--;)
1640 arc_t *arc_p, *arc_n;
1642 for (arc_p = NULL, arc = fn->blocks[ix].succ; arc;
1643 arc_p = arc, arc = arc_n)
1645 arc_n = arc->succ_next;
1646 arc->succ_next = arc_p;
1648 fn->blocks[ix].succ = arc_p;
1650 for (arc_p = NULL, arc = fn->blocks[ix].pred; arc;
1651 arc_p = arc, arc = arc_n)
1653 arc_n = arc->pred_next;
1654 arc->pred_next = arc_p;
1656 fn->blocks[ix].pred = arc_p;
1659 if (fn->blocks.size () < 2)
1660 fnotice (stderr, "%s:'%s' lacks entry and/or exit blocks\n",
1661 bbg_file_name, fn->name);
1662 else
1664 if (fn->blocks[ENTRY_BLOCK].num_pred)
1665 fnotice (stderr, "%s:'%s' has arcs to entry block\n",
1666 bbg_file_name, fn->name);
1667 else
1668 /* We can't deduce the entry block counts from the lack of
1669 predecessors. */
1670 fn->blocks[ENTRY_BLOCK].num_pred = ~(unsigned)0;
1672 if (fn->blocks[EXIT_BLOCK].num_succ)
1673 fnotice (stderr, "%s:'%s' has arcs from exit block\n",
1674 bbg_file_name, fn->name);
1675 else
1676 /* Likewise, we can't deduce exit block counts from the lack
1677 of its successors. */
1678 fn->blocks[EXIT_BLOCK].num_succ = ~(unsigned)0;
1681 /* Propagate the measured counts, this must be done in the same
1682 order as the code in profile.c */
1683 for (unsigned i = 0; i < fn->blocks.size (); i++)
1685 blk = &fn->blocks[i];
1686 block_t const *prev_dst = NULL;
1687 int out_of_order = 0;
1688 int non_fake_succ = 0;
1690 for (arc = blk->succ; arc; arc = arc->succ_next)
1692 if (!arc->fake)
1693 non_fake_succ++;
1695 if (!arc->on_tree)
1697 if (count_ptr)
1698 arc->count = *count_ptr++;
1699 arc->count_valid = 1;
1700 blk->num_succ--;
1701 arc->dst->num_pred--;
1703 if (prev_dst && prev_dst > arc->dst)
1704 out_of_order = 1;
1705 prev_dst = arc->dst;
1707 if (non_fake_succ == 1)
1709 /* If there is only one non-fake exit, it is an
1710 unconditional branch. */
1711 for (arc = blk->succ; arc; arc = arc->succ_next)
1712 if (!arc->fake)
1714 arc->is_unconditional = 1;
1715 /* If this block is instrumenting a call, it might be
1716 an artificial block. It is not artificial if it has
1717 a non-fallthrough exit, or the destination of this
1718 arc has more than one entry. Mark the destination
1719 block as a return site, if none of those conditions
1720 hold. */
1721 if (blk->is_call_site && arc->fall_through
1722 && arc->dst->pred == arc && !arc->pred_next)
1723 arc->dst->is_call_return = 1;
1727 /* Sort the successor arcs into ascending dst order. profile.c
1728 normally produces arcs in the right order, but sometimes with
1729 one or two out of order. We're not using a particularly
1730 smart sort. */
1731 if (out_of_order)
1733 arc_t *start = blk->succ;
1734 unsigned changes = 1;
1736 while (changes)
1738 arc_t *arc, *arc_p, *arc_n;
1740 changes = 0;
1741 for (arc_p = NULL, arc = start; (arc_n = arc->succ_next);)
1743 if (arc->dst > arc_n->dst)
1745 changes = 1;
1746 if (arc_p)
1747 arc_p->succ_next = arc_n;
1748 else
1749 start = arc_n;
1750 arc->succ_next = arc_n->succ_next;
1751 arc_n->succ_next = arc;
1752 arc_p = arc_n;
1754 else
1756 arc_p = arc;
1757 arc = arc_n;
1761 blk->succ = start;
1764 /* Place it on the invalid chain, it will be ignored if that's
1765 wrong. */
1766 blk->invalid_chain = 1;
1767 blk->chain = invalid_blocks;
1768 invalid_blocks = blk;
1771 while (invalid_blocks || valid_blocks)
1773 while ((blk = invalid_blocks))
1775 gcov_type total = 0;
1776 const arc_t *arc;
1778 invalid_blocks = blk->chain;
1779 blk->invalid_chain = 0;
1780 if (!blk->num_succ)
1781 for (arc = blk->succ; arc; arc = arc->succ_next)
1782 total += arc->count;
1783 else if (!blk->num_pred)
1784 for (arc = blk->pred; arc; arc = arc->pred_next)
1785 total += arc->count;
1786 else
1787 continue;
1789 blk->count = total;
1790 blk->count_valid = 1;
1791 blk->chain = valid_blocks;
1792 blk->valid_chain = 1;
1793 valid_blocks = blk;
1795 while ((blk = valid_blocks))
1797 gcov_type total;
1798 arc_t *arc, *inv_arc;
1800 valid_blocks = blk->chain;
1801 blk->valid_chain = 0;
1802 if (blk->num_succ == 1)
1804 block_t *dst;
1806 total = blk->count;
1807 inv_arc = NULL;
1808 for (arc = blk->succ; arc; arc = arc->succ_next)
1810 total -= arc->count;
1811 if (!arc->count_valid)
1812 inv_arc = arc;
1814 dst = inv_arc->dst;
1815 inv_arc->count_valid = 1;
1816 inv_arc->count = total;
1817 blk->num_succ--;
1818 dst->num_pred--;
1819 if (dst->count_valid)
1821 if (dst->num_pred == 1 && !dst->valid_chain)
1823 dst->chain = valid_blocks;
1824 dst->valid_chain = 1;
1825 valid_blocks = dst;
1828 else
1830 if (!dst->num_pred && !dst->invalid_chain)
1832 dst->chain = invalid_blocks;
1833 dst->invalid_chain = 1;
1834 invalid_blocks = dst;
1838 if (blk->num_pred == 1)
1840 block_t *src;
1842 total = blk->count;
1843 inv_arc = NULL;
1844 for (arc = blk->pred; arc; arc = arc->pred_next)
1846 total -= arc->count;
1847 if (!arc->count_valid)
1848 inv_arc = arc;
1850 src = inv_arc->src;
1851 inv_arc->count_valid = 1;
1852 inv_arc->count = total;
1853 blk->num_pred--;
1854 src->num_succ--;
1855 if (src->count_valid)
1857 if (src->num_succ == 1 && !src->valid_chain)
1859 src->chain = valid_blocks;
1860 src->valid_chain = 1;
1861 valid_blocks = src;
1864 else
1866 if (!src->num_succ && !src->invalid_chain)
1868 src->chain = invalid_blocks;
1869 src->invalid_chain = 1;
1870 invalid_blocks = src;
1877 /* If the graph has been correctly solved, every block will have a
1878 valid count. */
1879 for (unsigned i = 0; ix < fn->blocks.size (); i++)
1880 if (!fn->blocks[i].count_valid)
1882 fnotice (stderr, "%s:graph is unsolvable for '%s'\n",
1883 bbg_file_name, fn->name);
1884 break;
1888 /* Mark all the blocks only reachable via an incoming catch. */
1890 static void
1891 find_exception_blocks (function_t *fn)
1893 unsigned ix;
1894 block_t **queue = XALLOCAVEC (block_t *, fn->blocks.size ());
1896 /* First mark all blocks as exceptional. */
1897 for (ix = fn->blocks.size (); ix--;)
1898 fn->blocks[ix].exceptional = 1;
1900 /* Now mark all the blocks reachable via non-fake edges */
1901 queue[0] = &fn->blocks[0];
1902 queue[0]->exceptional = 0;
1903 for (ix = 1; ix;)
1905 block_t *block = queue[--ix];
1906 const arc_t *arc;
1908 for (arc = block->succ; arc; arc = arc->succ_next)
1909 if (!arc->fake && !arc->is_throw && arc->dst->exceptional)
1911 arc->dst->exceptional = 0;
1912 queue[ix++] = arc->dst;
1918 /* Increment totals in COVERAGE according to arc ARC. */
1920 static void
1921 add_branch_counts (coverage_t *coverage, const arc_t *arc)
1923 if (arc->is_call_non_return)
1925 coverage->calls++;
1926 if (arc->src->count)
1927 coverage->calls_executed++;
1929 else if (!arc->is_unconditional)
1931 coverage->branches++;
1932 if (arc->src->count)
1933 coverage->branches_executed++;
1934 if (arc->count)
1935 coverage->branches_taken++;
1939 /* Format a GCOV_TYPE integer as either a percent ratio, or absolute
1940 count. If dp >= 0, format TOP/BOTTOM * 100 to DP decimal places.
1941 If DP is zero, no decimal point is printed. Only print 100% when
1942 TOP==BOTTOM and only print 0% when TOP=0. If dp < 0, then simply
1943 format TOP. Return pointer to a static string. */
1945 static char const *
1946 format_gcov (gcov_type top, gcov_type bottom, int dp)
1948 static char buffer[20];
1950 /* Handle invalid values that would result in a misleading value. */
1951 if (bottom != 0 && top > bottom && dp >= 0)
1953 sprintf (buffer, "NAN %%");
1954 return buffer;
1957 if (dp >= 0)
1959 float ratio = bottom ? (float)top / bottom : 0;
1960 int ix;
1961 unsigned limit = 100;
1962 unsigned percent;
1964 for (ix = dp; ix--; )
1965 limit *= 10;
1967 percent = (unsigned) (ratio * limit + (float)0.5);
1968 if (percent <= 0 && top)
1969 percent = 1;
1970 else if (percent >= limit && top != bottom)
1971 percent = limit - 1;
1972 ix = sprintf (buffer, "%.*u%%", dp + 1, percent);
1973 if (dp)
1975 dp++;
1978 buffer[ix+1] = buffer[ix];
1979 ix--;
1981 while (dp--);
1982 buffer[ix + 1] = '.';
1985 else
1986 sprintf (buffer, "%" PRId64, (int64_t)top);
1988 return buffer;
1991 /* Summary of execution */
1993 static void
1994 executed_summary (unsigned lines, unsigned executed)
1996 if (lines)
1997 fnotice (stdout, "Lines executed:%s of %d\n",
1998 format_gcov (executed, lines, 2), lines);
1999 else
2000 fnotice (stdout, "No executable lines\n");
2003 /* Output summary info for a function or file. */
2005 static void
2006 function_summary (const coverage_t *coverage, const char *title)
2008 fnotice (stdout, "%s '%s'\n", title, coverage->name);
2009 executed_summary (coverage->lines, coverage->lines_executed);
2011 if (flag_branches)
2013 if (coverage->branches)
2015 fnotice (stdout, "Branches executed:%s of %d\n",
2016 format_gcov (coverage->branches_executed,
2017 coverage->branches, 2),
2018 coverage->branches);
2019 fnotice (stdout, "Taken at least once:%s of %d\n",
2020 format_gcov (coverage->branches_taken,
2021 coverage->branches, 2),
2022 coverage->branches);
2024 else
2025 fnotice (stdout, "No branches\n");
2026 if (coverage->calls)
2027 fnotice (stdout, "Calls executed:%s of %d\n",
2028 format_gcov (coverage->calls_executed, coverage->calls, 2),
2029 coverage->calls);
2030 else
2031 fnotice (stdout, "No calls\n");
2035 /* Canonicalize the filename NAME by canonicalizing directory
2036 separators, eliding . components and resolving .. components
2037 appropriately. Always returns a unique string. */
2039 static char *
2040 canonicalize_name (const char *name)
2042 /* The canonical name cannot be longer than the incoming name. */
2043 char *result = XNEWVEC (char, strlen (name) + 1);
2044 const char *base = name, *probe;
2045 char *ptr = result;
2046 char *dd_base;
2047 int slash = 0;
2049 #if HAVE_DOS_BASED_FILE_SYSTEM
2050 if (base[0] && base[1] == ':')
2052 result[0] = base[0];
2053 result[1] = ':';
2054 base += 2;
2055 ptr += 2;
2057 #endif
2058 for (dd_base = ptr; *base; base = probe)
2060 size_t len;
2062 for (probe = base; *probe; probe++)
2063 if (IS_DIR_SEPARATOR (*probe))
2064 break;
2066 len = probe - base;
2067 if (len == 1 && base[0] == '.')
2068 /* Elide a '.' directory */
2070 else if (len == 2 && base[0] == '.' && base[1] == '.')
2072 /* '..', we can only elide it and the previous directory, if
2073 we're not a symlink. */
2074 struct stat ATTRIBUTE_UNUSED buf;
2076 *ptr = 0;
2077 if (dd_base == ptr
2078 #if defined (S_ISLNK)
2079 /* S_ISLNK is not POSIX.1-1996. */
2080 || stat (result, &buf) || S_ISLNK (buf.st_mode)
2081 #endif
2084 /* Cannot elide, or unreadable or a symlink. */
2085 dd_base = ptr + 2 + slash;
2086 goto regular;
2088 while (ptr != dd_base && *ptr != '/')
2089 ptr--;
2090 slash = ptr != result;
2092 else
2094 regular:
2095 /* Regular pathname component. */
2096 if (slash)
2097 *ptr++ = '/';
2098 memcpy (ptr, base, len);
2099 ptr += len;
2100 slash = 1;
2103 for (; IS_DIR_SEPARATOR (*probe); probe++)
2104 continue;
2106 *ptr = 0;
2108 return result;
2111 /* Print hex representation of 16 bytes from SUM and write it to BUFFER. */
2113 static void
2114 md5sum_to_hex (const char *sum, char *buffer)
2116 for (unsigned i = 0; i < 16; i++)
2117 sprintf (buffer + (2 * i), "%02x", (unsigned char)sum[i]);
2120 /* Generate an output file name. INPUT_NAME is the canonicalized main
2121 input file and SRC_NAME is the canonicalized file name.
2122 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
2123 long_output_names we prepend the processed name of the input file
2124 to each output name (except when the current source file is the
2125 input file, so you don't get a double concatenation). The two
2126 components are separated by '##'. With preserve_paths we create a
2127 filename from all path components of the source file, replacing '/'
2128 with '#', and .. with '^', without it we simply take the basename
2129 component. (Remember, the canonicalized name will already have
2130 elided '.' components and converted \\ separators.) */
2132 static char *
2133 make_gcov_file_name (const char *input_name, const char *src_name)
2135 char *ptr;
2136 char *result;
2138 if (flag_long_names && input_name && strcmp (src_name, input_name))
2140 /* Generate the input filename part. */
2141 result = XNEWVEC (char, strlen (input_name) + strlen (src_name) + 10);
2143 ptr = result;
2144 ptr = mangle_name (input_name, ptr);
2145 ptr[0] = ptr[1] = '#';
2146 ptr += 2;
2148 else
2150 result = XNEWVEC (char, strlen (src_name) + 10);
2151 ptr = result;
2154 ptr = mangle_name (src_name, ptr);
2155 strcpy (ptr, ".gcov");
2157 /* When hashing filenames, we shorten them by only using the filename
2158 component and appending a hash of the full (mangled) pathname. */
2159 if (flag_hash_filenames)
2161 md5_ctx ctx;
2162 char md5sum[16];
2163 char md5sum_hex[33];
2165 md5_init_ctx (&ctx);
2166 md5_process_bytes (src_name, strlen (src_name), &ctx);
2167 md5_finish_ctx (&ctx, md5sum);
2168 md5sum_to_hex (md5sum, md5sum_hex);
2169 free (result);
2171 result = XNEWVEC (char, strlen (src_name) + 50);
2172 ptr = result;
2173 ptr = mangle_name (src_name, ptr);
2174 ptr[0] = ptr[1] = '#';
2175 ptr += 2;
2176 memcpy (ptr, md5sum_hex, 32);
2177 ptr += 32;
2178 strcpy (ptr, ".gcov");
2181 return result;
2184 static char *
2185 mangle_name (char const *base, char *ptr)
2187 size_t len;
2189 /* Generate the source filename part. */
2190 if (!flag_preserve_paths)
2192 base = lbasename (base);
2193 len = strlen (base);
2194 memcpy (ptr, base, len);
2195 ptr += len;
2197 else
2199 /* Convert '/' to '#', convert '..' to '^',
2200 convert ':' to '~' on DOS based file system. */
2201 const char *probe;
2203 #if HAVE_DOS_BASED_FILE_SYSTEM
2204 if (base[0] && base[1] == ':')
2206 ptr[0] = base[0];
2207 ptr[1] = '~';
2208 ptr += 2;
2209 base += 2;
2211 #endif
2212 for (; *base; base = probe)
2214 size_t len;
2216 for (probe = base; *probe; probe++)
2217 if (*probe == '/')
2218 break;
2219 len = probe - base;
2220 if (len == 2 && base[0] == '.' && base[1] == '.')
2221 *ptr++ = '^';
2222 else
2224 memcpy (ptr, base, len);
2225 ptr += len;
2227 if (*probe)
2229 *ptr++ = '#';
2230 probe++;
2235 return ptr;
2238 /* Scan through the bb_data for each line in the block, increment
2239 the line number execution count indicated by the execution count of
2240 the appropriate basic block. */
2242 static void
2243 add_line_counts (coverage_t *coverage, function_t *fn)
2245 bool has_any_line = false;
2246 /* Scan each basic block. */
2247 for (unsigned ix = 0; ix != fn->blocks.size (); ix++)
2249 line_t *line = NULL;
2250 block_t *block = &fn->blocks[ix];
2251 if (block->count && ix && ix + 1 != fn->blocks.size ())
2252 fn->blocks_executed++;
2253 for (unsigned i = 0; i < block->locations.size (); i++)
2255 const source_t *src = &sources[block->locations[i].source_file_idx];
2257 vector<unsigned> &lines = block->locations[i].lines;
2258 for (unsigned j = 0; j < lines.size (); j++)
2260 line = &src->lines[lines[j]];
2261 if (coverage)
2263 if (!line->exists)
2264 coverage->lines++;
2265 if (!line->count && block->count)
2266 coverage->lines_executed++;
2268 line->exists = 1;
2269 if (!block->exceptional)
2270 line->unexceptional = 1;
2271 line->count += block->count;
2274 block->cycle.arc = NULL;
2275 block->cycle.ident = ~0U;
2276 has_any_line = true;
2278 if (!ix || ix + 1 == fn->blocks.size ())
2279 /* Entry or exit block */;
2280 else if (line != NULL)
2282 block->chain = line->blocks;
2283 line->blocks = block;
2285 if (flag_branches)
2287 arc_t *arc;
2289 for (arc = block->succ; arc; arc = arc->succ_next)
2291 arc->line_next = line->branches;
2292 line->branches = arc;
2293 if (coverage && !arc->is_unconditional)
2294 add_branch_counts (coverage, arc);
2300 if (!has_any_line)
2301 fnotice (stderr, "%s:no lines for '%s'\n", bbg_file_name, fn->name);
2304 /* Accumulate the line counts of a file. */
2306 static void
2307 accumulate_line_counts (source_t *src)
2309 line_t *line;
2310 function_t *fn, *fn_p, *fn_n;
2311 unsigned ix;
2313 /* Reverse the function order. */
2314 for (fn = src->functions, fn_p = NULL; fn; fn_p = fn, fn = fn_n)
2316 fn_n = fn->next_file_fn;
2317 fn->next_file_fn = fn_p;
2319 src->functions = fn_p;
2321 for (ix = src->num_lines, line = src->lines; ix--; line++)
2323 if (line->blocks)
2325 /* The user expects the line count to be the number of times
2326 a line has been executed. Simply summing the block count
2327 will give an artificially high number. The Right Thing
2328 is to sum the entry counts to the graph of blocks on this
2329 line, then find the elementary cycles of the local graph
2330 and add the transition counts of those cycles. */
2331 block_t *block, *block_p, *block_n;
2332 gcov_type count = 0;
2334 /* Reverse the block information. */
2335 for (block = line->blocks, block_p = NULL; block;
2336 block_p = block, block = block_n)
2338 block_n = block->chain;
2339 block->chain = block_p;
2340 block->cycle.ident = ix;
2342 line->blocks = block_p;
2344 /* Sum the entry arcs. */
2345 for (block = line->blocks; block; block = block->chain)
2347 arc_t *arc;
2349 for (arc = block->pred; arc; arc = arc->pred_next)
2350 if (flag_branches)
2351 add_branch_counts (&src->coverage, arc);
2354 /* Cycle detection. */
2355 for (block = line->blocks; block; block = block->chain)
2357 for (arc_t *arc = block->pred; arc; arc = arc->pred_next)
2358 if (!line->has_block (arc->src))
2359 count += arc->count;
2360 for (arc_t *arc = block->succ; arc; arc = arc->succ_next)
2361 arc->cs_count = arc->count;
2364 /* Now, add the count of loops entirely on this line. */
2365 count += get_cycles_count (*line);
2366 line->count = count;
2369 if (line->exists)
2371 src->coverage.lines++;
2372 if (line->count)
2373 src->coverage.lines_executed++;
2378 /* Output information about ARC number IX. Returns nonzero if
2379 anything is output. */
2381 static int
2382 output_branch_count (FILE *gcov_file, int ix, const arc_t *arc)
2384 if (arc->is_call_non_return)
2386 if (arc->src->count)
2388 fnotice (gcov_file, "call %2d returned %s\n", ix,
2389 format_gcov (arc->src->count - arc->count,
2390 arc->src->count, -flag_counts));
2392 else
2393 fnotice (gcov_file, "call %2d never executed\n", ix);
2395 else if (!arc->is_unconditional)
2397 if (arc->src->count)
2398 fnotice (gcov_file, "branch %2d taken %s%s", ix,
2399 format_gcov (arc->count, arc->src->count, -flag_counts),
2400 arc->fall_through ? " (fallthrough)"
2401 : arc->is_throw ? " (throw)" : "");
2402 else
2403 fnotice (gcov_file, "branch %2d never executed", ix);
2405 if (flag_verbose)
2406 fnotice (gcov_file, " (BB %d)", arc->dst->id);
2408 fnotice (gcov_file, "\n");
2410 else if (flag_unconditional && !arc->dst->is_call_return)
2412 if (arc->src->count)
2413 fnotice (gcov_file, "unconditional %2d taken %s\n", ix,
2414 format_gcov (arc->count, arc->src->count, -flag_counts));
2415 else
2416 fnotice (gcov_file, "unconditional %2d never executed\n", ix);
2418 else
2419 return 0;
2420 return 1;
2423 static const char *
2424 read_line (FILE *file)
2426 static char *string;
2427 static size_t string_len;
2428 size_t pos = 0;
2429 char *ptr;
2431 if (!string_len)
2433 string_len = 200;
2434 string = XNEWVEC (char, string_len);
2437 while ((ptr = fgets (string + pos, string_len - pos, file)))
2439 size_t len = strlen (string + pos);
2441 if (len && string[pos + len - 1] == '\n')
2443 string[pos + len - 1] = 0;
2444 return string;
2446 pos += len;
2447 /* If the file contains NUL characters or an incomplete
2448 last line, which can happen more than once in one run,
2449 we have to avoid doubling the STRING_LEN unnecessarily. */
2450 if (pos > string_len / 2)
2452 string_len *= 2;
2453 string = XRESIZEVEC (char, string, string_len);
2457 return pos ? string : NULL;
2460 /* Read in the source file one line at a time, and output that line to
2461 the gcov file preceded by its execution count and other
2462 information. */
2464 static void
2465 output_lines (FILE *gcov_file, const source_t *src)
2467 FILE *source_file;
2468 unsigned line_num; /* current line number. */
2469 const line_t *line; /* current line info ptr. */
2470 const char *retval = ""; /* status of source file reading. */
2471 function_t *fn = NULL;
2473 fprintf (gcov_file, "%9s:%5d:Source:%s\n", "-", 0, src->coverage.name);
2474 if (!multiple_files)
2476 fprintf (gcov_file, "%9s:%5d:Graph:%s\n", "-", 0, bbg_file_name);
2477 fprintf (gcov_file, "%9s:%5d:Data:%s\n", "-", 0,
2478 no_data_file ? "-" : da_file_name);
2479 fprintf (gcov_file, "%9s:%5d:Runs:%u\n", "-", 0, object_runs);
2481 fprintf (gcov_file, "%9s:%5d:Programs:%u\n", "-", 0, program_count);
2483 source_file = fopen (src->name, "r");
2484 if (!source_file)
2486 fnotice (stderr, "Cannot open source file %s\n", src->name);
2487 retval = NULL;
2489 else if (src->file_time == 0)
2490 fprintf (gcov_file, "%9s:%5d:Source is newer than graph\n", "-", 0);
2492 if (flag_branches)
2493 fn = src->functions;
2495 for (line_num = 1, line = &src->lines[line_num];
2496 line_num < src->num_lines; line_num++, line++)
2498 for (; fn && fn->line == line_num; fn = fn->next_file_fn)
2500 arc_t *arc = fn->blocks[EXIT_BLOCK].pred;
2501 gcov_type return_count = fn->blocks[EXIT_BLOCK].count;
2502 gcov_type called_count = fn->blocks[ENTRY_BLOCK].count;
2504 for (; arc; arc = arc->pred_next)
2505 if (arc->fake)
2506 return_count -= arc->count;
2508 fprintf (gcov_file, "function %s", flag_demangled_names ?
2509 fn->demangled_name : fn->name);
2510 fprintf (gcov_file, " called %s",
2511 format_gcov (called_count, 0, -1));
2512 fprintf (gcov_file, " returned %s",
2513 format_gcov (return_count, called_count, 0));
2514 fprintf (gcov_file, " blocks executed %s",
2515 format_gcov (fn->blocks_executed, fn->blocks.size () - 2,
2516 0));
2517 fprintf (gcov_file, "\n");
2520 if (retval)
2521 retval = read_line (source_file);
2523 /* For lines which don't exist in the .bb file, print '-' before
2524 the source line. For lines which exist but were never
2525 executed, print '#####' or '=====' before the source line.
2526 Otherwise, print the execution count before the source line.
2527 There are 16 spaces of indentation added before the source
2528 line so that tabs won't be messed up. */
2529 fprintf (gcov_file, "%9s:%5u:%s\n",
2530 !line->exists ? "-" : line->count
2531 ? format_gcov (line->count, 0, -1)
2532 : line->unexceptional ? "#####" : "=====", line_num,
2533 retval ? retval : "/*EOF*/");
2535 if (flag_all_blocks)
2537 block_t *block;
2538 arc_t *arc;
2539 int ix, jx;
2541 for (ix = jx = 0, block = line->blocks; block;
2542 block = block->chain)
2544 if (!block->is_call_return)
2546 fprintf (gcov_file, "%9s:%5u-block %2d",
2547 !line->exists ? "-" : block->count
2548 ? format_gcov (block->count, 0, -1)
2549 : block->exceptional ? "%%%%%" : "$$$$$",
2550 line_num, ix++);
2551 if (flag_verbose)
2552 fprintf (gcov_file, " (BB %u)", block->id);
2553 fprintf (gcov_file, "\n");
2555 if (flag_branches)
2556 for (arc = block->succ; arc; arc = arc->succ_next)
2557 jx += output_branch_count (gcov_file, jx, arc);
2560 else if (flag_branches)
2562 int ix;
2563 arc_t *arc;
2565 for (ix = 0, arc = line->branches; arc; arc = arc->line_next)
2566 ix += output_branch_count (gcov_file, ix, arc);
2570 /* Handle all remaining source lines. There may be lines after the
2571 last line of code. */
2572 if (retval)
2574 for (; (retval = read_line (source_file)); line_num++)
2575 fprintf (gcov_file, "%9s:%5u:%s\n", "-", line_num, retval);
2578 if (source_file)
2579 fclose (source_file);