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