]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/coverage.c
Daily bump.
[thirdparty/gcc.git] / gcc / coverage.c
CommitLineData
ca29da43 1/* Read and write coverage files, and associated functionality.
d1e082c2 2 Copyright (C) 1990-2013 Free Software Foundation, Inc.
ca29da43
NS
3 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
4 based on some ideas from Dain Samples of UC Berkeley.
5 Further mangling by Bob Manson, Cygnus Support.
6 Further mangled by Nathan Sidwell, CodeSourcery
7
8This file is part of GCC.
9
10GCC is free software; you can redistribute it and/or modify it under
11the terms of the GNU General Public License as published by the Free
9dcd6f09 12Software Foundation; either version 3, or (at your option) any later
ca29da43
NS
13version.
14
15GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16WARRANTY; without even the implied warranty of MERCHANTABILITY or
17FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18for more details.
19
20You should have received a copy of the GNU General Public License
9dcd6f09
NC
21along with GCC; see the file COPYING3. If not see
22<http://www.gnu.org/licenses/>. */
ca29da43
NS
23
24
25#define GCOV_LINKAGE
26
27#include "config.h"
28#include "system.h"
29#include "coretypes.h"
30#include "tm.h"
31#include "rtl.h"
32#include "tree.h"
33#include "flags.h"
34#include "output.h"
35#include "regs.h"
36#include "expr.h"
37#include "function.h"
2d1a4cc1 38#include "basic-block.h"
ca29da43 39#include "toplev.h"
5773a50f 40#include "tm_p.h"
ca29da43 41#include "ggc.h"
ca29da43 42#include "coverage.h"
ca29da43 43#include "langhooks.h"
0823efed 44#include "hash-table.h"
c9b9aa64
RH
45#include "tree-iterator.h"
46#include "cgraph.h"
7ee2468b 47#include "dumpfile.h"
1da2ed5f 48#include "diagnostic-core.h"
650cfcab 49#include "intl.h"
ba78087b 50#include "filenames.h"
9e66e106 51#include "target.h"
ca29da43 52
10adac51 53#include "gcov-io.h"
ca29da43
NS
54#include "gcov-io.c"
55
b724567e 56struct GTY((chain_next ("%h.next"))) coverage_data
ca29da43 57{
b724567e 58 struct coverage_data *next; /* next function */
159b3be1 59 unsigned ident; /* function ident */
10adac51
XDL
60 unsigned lineno_checksum; /* function lineno checksum */
61 unsigned cfg_checksum; /* function cfg checksum */
5366b186
NS
62 tree fn_decl; /* the function decl */
63 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
ca29da43
NS
64};
65
66/* Counts information for a function. */
67typedef struct counts_entry
68{
69 /* We hash by */
796621e8 70 unsigned ident;
cdb23767 71 unsigned ctr;
159b3be1 72
ca29da43 73 /* Store */
10adac51
XDL
74 unsigned lineno_checksum;
75 unsigned cfg_checksum;
ca29da43 76 gcov_type *counts;
cdb23767 77 struct gcov_ctr_summary summary;
5deac340
RG
78
79 /* hash_table support. */
5831a5f0
LC
80 typedef counts_entry value_type;
81 typedef counts_entry compare_type;
82 static inline hashval_t hash (const value_type *);
83 static int equal (const value_type *, const compare_type *);
84 static void remove (value_type *);
ca29da43
NS
85} counts_entry_t;
86
b724567e
NS
87static GTY(()) struct coverage_data *functions_head = 0;
88static struct coverage_data **functions_tail = &functions_head;
6d70e6be 89static unsigned no_coverage = 0;
ca29da43 90
cdb23767
NS
91/* Cumulative counter information for whole program. */
92static unsigned prg_ctr_mask; /* Mask of counter types generated. */
ca29da43 93
cdb23767 94/* Counter information for current function. */
71c0e7fc 95static unsigned fn_ctr_mask; /* Mask of counters used. */
5366b186 96static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
6d70e6be
NS
97static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
98static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
ca29da43 99
b724567e
NS
100/* Coverage info VAR_DECL and function info type nodes. */
101static GTY(()) tree gcov_info_var;
102static GTY(()) tree gcov_fn_info_type;
103static GTY(()) tree gcov_fn_info_ptr_type;
104
efbb59b2
SB
105/* Name of the notes (gcno) output file. The "bbg" prefix is for
106 historical reasons, when the notes file contained only the
107 basic block graph notes.
108 If this is NULL we're not writing to the notes file. */
ca29da43 109static char *bbg_file_name;
ca29da43 110
efbb59b2 111/* File stamp for notes file. */
cb686b99
NS
112static unsigned bbg_file_stamp;
113
efbb59b2 114/* Name of the count data (gcda) file. */
ca29da43
NS
115static char *da_file_name;
116
09780dfb 117/* The names of merge functions for counters. */
9b514d25
NS
118static const char *const ctr_merge_functions[GCOV_COUNTERS] = GCOV_MERGE_FUNCTIONS;
119static const char *const ctr_names[GCOV_COUNTERS] = GCOV_COUNTER_NAMES;
09780dfb 120
ca29da43 121/* Forward declarations. */
159b3be1 122static void read_counts_file (void);
5366b186
NS
123static tree build_var (tree, tree, int);
124static void build_fn_info_type (tree, unsigned, tree);
b724567e
NS
125static void build_info_type (tree, tree);
126static tree build_fn_info (const struct coverage_data *, tree, tree);
127static tree build_info (tree, tree);
128static bool coverage_obj_init (void);
9771b263
DN
129static vec<constructor_elt, va_gc> *coverage_obj_fn
130(vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
131static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
251e2ff2
RS
132\f
133/* Return the type node for gcov_type. */
134
135tree
136get_gcov_type (void)
137{
0f250839
RG
138 enum machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
139 return lang_hooks.types.type_for_mode (mode, false);
251e2ff2
RS
140}
141
142/* Return the type node for gcov_unsigned_t. */
ca29da43 143
251e2ff2
RS
144static tree
145get_gcov_unsigned_t (void)
146{
0f250839
RG
147 enum machine_mode mode = smallest_mode_for_size (32, MODE_INT);
148 return lang_hooks.types.type_for_mode (mode, true);
251e2ff2 149}
ca29da43 150\f
0823efed 151inline hashval_t
5831a5f0 152counts_entry::hash (const value_type *entry)
ca29da43 153{
796621e8 154 return entry->ident * GCOV_COUNTERS + entry->ctr;
ca29da43
NS
155}
156
0823efed 157inline int
5831a5f0
LC
158counts_entry::equal (const value_type *entry1,
159 const compare_type *entry2)
ca29da43 160{
796621e8 161 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
ca29da43
NS
162}
163
0823efed 164inline void
5831a5f0 165counts_entry::remove (value_type *entry)
ca29da43 166{
ca29da43
NS
167 free (entry->counts);
168 free (entry);
169}
170
0823efed 171/* Hash table of count data. */
5deac340 172static hash_table <counts_entry> counts_hash;
0823efed 173
ca29da43
NS
174/* Read in the counts file, if available. */
175
176static void
159b3be1 177read_counts_file (void)
ca29da43 178{
9b514d25 179 gcov_unsigned_t fn_ident = 0;
5366b186
NS
180 struct gcov_summary summary;
181 unsigned new_summary = 1;
7d63a2fa 182 gcov_unsigned_t tag;
24a4a033 183 int is_error = 0;
10adac51
XDL
184 unsigned lineno_checksum = 0;
185 unsigned cfg_checksum = 0;
7d63a2fa 186
ca29da43
NS
187 if (!gcov_open (da_file_name, 1))
188 return;
159b3be1 189
160e2e4f 190 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
ca29da43 191 {
d4ee4d25 192 warning (0, "%qs is not a gcov data file", da_file_name);
ca29da43
NS
193 gcov_close ();
194 return;
195 }
160e2e4f 196 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
ca29da43 197 {
330d2e2a
NS
198 char v[4], e[4];
199
200 GCOV_UNSIGNED2STRING (v, tag);
201 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
159b3be1 202
d4ee4d25 203 warning (0, "%qs is version %q.*s, expected version %q.*s",
cb83302c 204 da_file_name, 4, v, 4, e);
ca29da43
NS
205 gcov_close ();
206 return;
207 }
159b3be1 208
cb686b99
NS
209 /* Read the stamp, used for creating a generation count. */
210 tag = gcov_read_unsigned ();
211 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
b8698a0f 212
0823efed 213 counts_hash.create (10);
7d63a2fa 214 while ((tag = gcov_read_unsigned ()))
ca29da43 215 {
7d63a2fa 216 gcov_unsigned_t length;
9b514d25 217 gcov_position_t offset;
159b3be1 218
ca29da43
NS
219 length = gcov_read_unsigned ();
220 offset = gcov_position ();
221 if (tag == GCOV_TAG_FUNCTION)
222 {
5366b186 223 if (length)
ca29da43 224 {
5366b186
NS
225 fn_ident = gcov_read_unsigned ();
226 lineno_checksum = gcov_read_unsigned ();
227 cfg_checksum = gcov_read_unsigned ();
ca29da43 228 }
5366b186
NS
229 else
230 fn_ident = lineno_checksum = cfg_checksum = 0;
231 new_summary = 1;
ca29da43
NS
232 }
233 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
234 {
5366b186
NS
235 struct gcov_summary sum;
236 unsigned ix;
159b3be1 237
5366b186
NS
238 if (new_summary)
239 memset (&summary, 0, sizeof (summary));
159b3be1 240
5366b186
NS
241 gcov_read_summary (&sum);
242 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
243 {
244 summary.ctrs[ix].runs += sum.ctrs[ix].runs;
245 summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
246 if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
247 summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
248 summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
ca29da43 249 }
9f71de84
TJ
250 if (new_summary)
251 memcpy (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
252 sum.ctrs[GCOV_COUNTER_ARCS].histogram,
253 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
254 else
255 gcov_histogram_merge (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
256 sum.ctrs[GCOV_COUNTER_ARCS].histogram);
5366b186 257 new_summary = 0;
ca29da43 258 }
796621e8 259 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
ca29da43
NS
260 {
261 counts_entry_t **slot, *entry, elt;
330d2e2a 262 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
ca29da43
NS
263 unsigned ix;
264
796621e8 265 elt.ident = fn_ident;
cdb23767 266 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
ca29da43 267
0823efed 268 slot = counts_hash.find_slot (&elt, INSERT);
ca29da43
NS
269 entry = *slot;
270 if (!entry)
271 {
5ed6ace5 272 *slot = entry = XCNEW (counts_entry_t);
10adac51 273 entry->ident = fn_ident;
cdb23767 274 entry->ctr = elt.ctr;
10adac51
XDL
275 entry->lineno_checksum = lineno_checksum;
276 entry->cfg_checksum = cfg_checksum;
9f71de84
TJ
277 if (elt.ctr < GCOV_COUNTERS_SUMMABLE)
278 entry->summary = summary.ctrs[elt.ctr];
279 entry->summary.num = n_counts;
5ed6ace5 280 entry->counts = XCNEWVEC (gcov_type, n_counts);
ca29da43 281 }
10adac51
XDL
282 else if (entry->lineno_checksum != lineno_checksum
283 || entry->cfg_checksum != cfg_checksum)
ca29da43 284 {
10adac51
XDL
285 error ("Profile data for function %u is corrupted", fn_ident);
286 error ("checksum is (%x,%x) instead of (%x,%x)",
287 entry->lineno_checksum, entry->cfg_checksum,
288 lineno_checksum, cfg_checksum);
0823efed 289 counts_hash.dispose ();
24a4a033
JH
290 break;
291 }
292 else if (entry->summary.num != n_counts)
293 {
10adac51 294 error ("Profile data for function %u is corrupted", fn_ident);
24a4a033 295 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
0823efed 296 counts_hash.dispose ();
ca29da43
NS
297 break;
298 }
9b514d25
NS
299 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
300 {
24a4a033
JH
301 error ("cannot merge separate %s counters for function %u",
302 ctr_names[elt.ctr], fn_ident);
9b514d25
NS
303 goto skip_merge;
304 }
5366b186 305 else
ca29da43 306 {
5366b186
NS
307 entry->summary.runs += summary.ctrs[elt.ctr].runs;
308 entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
309 if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
310 entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
311 entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
ca29da43
NS
312 }
313 for (ix = 0; ix != n_counts; ix++)
314 entry->counts[ix] += gcov_read_counter ();
9b514d25 315 skip_merge:;
ca29da43 316 }
474f141e 317 gcov_sync (offset, length);
24a4a033 318 if ((is_error = gcov_is_error ()))
00cf2913 319 {
971801ff 320 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
00cf2913 321 da_file_name);
0823efed 322 counts_hash.dispose ();
00cf2913
NS
323 break;
324 }
7d63a2fa 325 }
159b3be1 326
ca29da43
NS
327 gcov_close ();
328}
329
330/* Returns the counters for a particular tag. */
331
332gcov_type *
cdb23767 333get_coverage_counts (unsigned counter, unsigned expected,
10adac51 334 unsigned cfg_checksum, unsigned lineno_checksum,
cdb23767 335 const struct gcov_ctr_summary **summary)
ca29da43
NS
336{
337 counts_entry_t *entry, elt;
338
71c0e7fc 339 /* No hash table, no counts. */
0823efed 340 if (!counts_hash.is_created ())
ca29da43
NS
341 {
342 static int warned = 0;
343
344 if (!warned++)
1f5b3869 345 inform (input_location, (flag_guess_branch_prob
02307675
R
346 ? "file %s not found, execution counts estimated"
347 : "file %s not found, execution counts assumed to be zero"),
bbfff586 348 da_file_name);
ca29da43
NS
349 return NULL;
350 }
351
6d70e6be 352 elt.ident = current_function_funcdef_no + 1;
cdb23767 353 elt.ctr = counter;
0823efed 354 entry = counts_hash.find (&elt);
5366b186
NS
355 if (!entry || !entry->summary.num)
356 /* The function was not emitted, or is weak and not chosen in the
357 final executable. Silently fail, because there's nothing we
358 can do about it. */
359 return NULL;
360
10adac51 361 if (entry->cfg_checksum != cfg_checksum
16c1c158 362 || entry->summary.num != expected)
24a4a033 363 {
16c1c158 364 static int warned = 0;
650cfcab 365 bool warning_printed = false;
4f1e4960 366 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
16c1c158 367
10adac51
XDL
368 warning_printed =
369 warning_at (input_location, OPT_Wcoverage_mismatch,
5366b186 370 "the control flow of function %qE does not match "
10adac51 371 "its profile data (counter %qs)", id, ctr_names[counter]);
650cfcab 372 if (warning_printed)
16c1c158 373 {
5366b186 374 inform (input_location, "use -Wno-error=coverage-mismatch to tolerate "
10adac51 375 "the mismatch but performance may drop if the function is hot");
650cfcab 376
1da2ed5f 377 if (!seen_error ()
650cfcab
NV
378 && !warned++)
379 {
380 inform (input_location, "coverage mismatch ignored");
381 inform (input_location, flag_guess_branch_prob
382 ? G_("execution counts estimated")
383 : G_("execution counts assumed to be zero"));
384 if (!flag_guess_branch_prob)
385 inform (input_location,
386 "this can result in poorly optimized code");
387 }
16c1c158
RG
388 }
389
390 return NULL;
ca29da43 391 }
5366b186
NS
392 else if (entry->lineno_checksum != lineno_checksum)
393 {
b724567e 394 warning (0, "source locations for function %qE have changed,"
5366b186
NS
395 " the profile data may be out of date",
396 DECL_ASSEMBLER_NAME (current_function_decl));
397 }
159b3be1 398
cdb23767
NS
399 if (summary)
400 *summary = &entry->summary;
ca29da43
NS
401
402 return entry->counts;
403}
cdb23767 404
6356f892 405/* Allocate NUM counters of type COUNTER. Returns nonzero if the
6d70e6be 406 allocation succeeded. */
cdb23767 407
6d70e6be
NS
408int
409coverage_counter_alloc (unsigned counter, unsigned num)
cdb23767 410{
6d70e6be
NS
411 if (no_coverage)
412 return 0;
159b3be1 413
6d70e6be
NS
414 if (!num)
415 return 1;
159b3be1 416
5366b186 417 if (!fn_v_ctrs[counter])
cdb23767 418 {
5366b186
NS
419 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
420
421 fn_v_ctrs[counter]
422 = build_var (current_function_decl, array_type, counter);
cdb23767 423 }
5366b186 424
6d70e6be
NS
425 fn_b_ctrs[counter] = fn_n_ctrs[counter];
426 fn_n_ctrs[counter] += num;
5366b186 427
6d70e6be
NS
428 fn_ctr_mask |= 1 << counter;
429 return 1;
430}
cdb23767 431
6de9cd9a
DN
432/* Generate a tree to access COUNTER NO. */
433
434tree
435tree_coverage_counter_ref (unsigned counter, unsigned no)
436{
070e3969 437 tree gcov_type_node = get_gcov_type ();
6de9cd9a 438
341c100f 439 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
6de9cd9a 440
5366b186
NS
441 no += fn_b_ctrs[counter];
442
6de9cd9a 443 /* "no" here is an array index, scaled to bytes later. */
5366b186 444 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
f81b1a3d 445 build_int_cst (integer_type_node, no), NULL, NULL);
6de9cd9a 446}
fc9161c1
RG
447
448/* Generate a tree to access the address of COUNTER NO. */
449
450tree
451tree_coverage_counter_addr (unsigned counter, unsigned no)
452{
453 tree gcov_type_node = get_gcov_type ();
454
455 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
5366b186 456 no += fn_b_ctrs[counter];
628c189e 457
fc9161c1
RG
458 /* "no" here is an array index, scaled to bytes later. */
459 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
5366b186 460 fn_v_ctrs[counter],
f81b1a3d 461 build_int_cst (integer_type_node, no),
fc9161c1
RG
462 NULL, NULL));
463}
ca29da43 464\f
10adac51 465
ca29da43 466/* Generate a checksum for a string. CHKSUM is the current
71c0e7fc 467 checksum. */
ca29da43
NS
468
469static unsigned
20c361f3 470coverage_checksum_string (unsigned chksum, const char *string)
ca29da43 471{
20c361f3
JH
472 int i;
473 char *dup = NULL;
474
475 /* Look for everything that looks if it were produced by
5880f14f 476 get_file_function_name and zero out the second part
20c361f3
JH
477 that may result from flag_random_seed. This is not critical
478 as the checksums are used only for sanity checking. */
479 for (i = 0; string[i]; i++)
ca29da43 480 {
2b557972
JH
481 int offset = 0;
482 if (!strncmp (string + i, "_GLOBAL__N_", 11))
483 offset = 11;
20c361f3 484 if (!strncmp (string + i, "_GLOBAL__", 9))
2b557972
JH
485 offset = 9;
486
487 /* C++ namespaces do have scheme:
488 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
489 since filename might contain extra underscores there seems
490 to be no better chance then walk all possible offsets looking
fa10beec 491 for magicnumber. */
2b557972 492 if (offset)
1f651229
JH
493 {
494 for (i = i + offset; string[i]; i++)
495 if (string[i]=='_')
496 {
497 int y;
498
499 for (y = 1; y < 9; y++)
500 if (!(string[i + y] >= '0' && string[i + y] <= '9')
501 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
502 break;
503 if (y != 9 || string[i + 9] != '_')
504 continue;
505 for (y = 10; y < 18; y++)
506 if (!(string[i + y] >= '0' && string[i + y] <= '9')
507 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
508 break;
509 if (y != 18)
510 continue;
511 if (!dup)
512 string = dup = xstrdup (string);
513 for (y = 10; y < 18; y++)
514 dup[i + y] = '0';
515 }
516 break;
517 }
ca29da43 518 }
20c361f3
JH
519
520 chksum = crc32_string (chksum, string);
04695783 521 free (dup);
159b3be1 522
ca29da43
NS
523 return chksum;
524}
525
526/* Compute checksum for the current function. We generate a CRC32. */
527
10adac51
XDL
528unsigned
529coverage_compute_lineno_checksum (void)
ca29da43 530{
a281759f
PB
531 expanded_location xloc
532 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
533 unsigned chksum = xloc.line;
ca29da43 534
a281759f 535 chksum = coverage_checksum_string (chksum, xloc.file);
20c361f3 536 chksum = coverage_checksum_string
ca29da43
NS
537 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
538
539 return chksum;
540}
10adac51
XDL
541
542/* Compute cfg checksum for the current function.
543 The checksum is calculated carefully so that
544 source code changes that doesn't affect the control flow graph
545 won't change the checksum.
546 This is to make the profile data useable across source code change.
547 The downside of this is that the compiler may use potentially
548 wrong profile data - that the source code change has non-trivial impact
549 on the validity of profile data (e.g. the reversed condition)
550 but the compiler won't detect the change and use the wrong profile data. */
551
552unsigned
553coverage_compute_cfg_checksum (void)
554{
555 basic_block bb;
556 unsigned chksum = n_basic_blocks;
557
558 FOR_EACH_BB (bb)
559 {
560 edge e;
561 edge_iterator ei;
562 chksum = crc32_byte (chksum, bb->index);
563 FOR_EACH_EDGE (e, ei, bb->succs)
564 {
565 chksum = crc32_byte (chksum, e->dest->index);
566 }
567 }
568
569 return chksum;
570}
ca29da43 571\f
efbb59b2 572/* Begin output to the notes file for the current function.
b724567e 573 Writes the function header. Returns nonzero if data should be output. */
ca29da43
NS
574
575int
b724567e 576coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
ca29da43 577{
b724567e
NS
578 expanded_location xloc;
579 unsigned long offset;
580
2f908293
SP
581 /* We don't need to output .gcno file unless we're under -ftest-coverage
582 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
b724567e 583 if (no_coverage || !bbg_file_name)
6d70e6be 584 return 0;
159b3be1 585
b724567e 586 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
10adac51 587
b724567e
NS
588 /* Announce function */
589 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
590 gcov_write_unsigned (current_function_funcdef_no + 1);
591 gcov_write_unsigned (lineno_checksum);
592 gcov_write_unsigned (cfg_checksum);
593 gcov_write_string (IDENTIFIER_POINTER
594 (DECL_ASSEMBLER_NAME (current_function_decl)));
595 gcov_write_string (xloc.file);
596 gcov_write_unsigned (xloc.line);
597 gcov_write_length (offset);
ca29da43 598
ca29da43
NS
599 return !gcov_is_error ();
600}
601
602/* Finish coverage data for the current function. Verify no output
603 error has occurred. Save function coverage counts. */
604
605void
10adac51 606coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
ca29da43
NS
607{
608 unsigned i;
159b3be1 609
b724567e 610 if (bbg_file_name && gcov_is_error ())
159b3be1 611 {
d4ee4d25 612 warning (0, "error writing %qs", bbg_file_name);
b724567e
NS
613 unlink (bbg_file_name);
614 bbg_file_name = NULL;
ca29da43 615 }
cdb23767 616
2ac69a0c 617 if (fn_ctr_mask)
cdb23767 618 {
2ac69a0c
NS
619 struct coverage_data *item = 0;
620
621 /* If the function is extern (i.e. extern inline), then we won't
622 be outputting it, so don't chain it onto the function
623 list. */
624 if (!DECL_EXTERNAL (current_function_decl))
625 {
626 item = ggc_alloc_coverage_data ();
627
628 item->ident = current_function_funcdef_no + 1;
629 item->lineno_checksum = lineno_checksum;
630 item->cfg_checksum = cfg_checksum;
631
632 item->fn_decl = current_function_decl;
633 item->next = 0;
634 *functions_tail = item;
635 functions_tail = &item->next;
636 }
10adac51 637
cdb23767
NS
638 for (i = 0; i != GCOV_COUNTERS; i++)
639 {
5366b186 640 tree var = fn_v_ctrs[i];
2ac69a0c
NS
641
642 if (item)
643 item->ctr_vars[i] = var;
5366b186
NS
644 if (var)
645 {
646 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
647 array_type = build_array_type (get_gcov_type (), array_type);
648 TREE_TYPE (var) = array_type;
649 DECL_SIZE (var) = TYPE_SIZE (array_type);
650 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
651 varpool_finalize_decl (var);
652 }
2ac69a0c 653
5366b186
NS
654 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
655 fn_v_ctrs[i] = NULL_TREE;
cdb23767
NS
656 }
657 prg_ctr_mask |= fn_ctr_mask;
658 fn_ctr_mask = 0;
659 }
ca29da43
NS
660}
661
5366b186 662/* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
0e485da0 663 >= 0 it is a counter array, otherwise it is the function structure. */
ca29da43 664
ca29da43 665static tree
5366b186
NS
666build_var (tree fn_decl, tree type, int counter)
667{
668 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
9e66e106
JJ
669 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
670 char *buf;
671 size_t fn_name_len, len;
672
673 fn_name = targetm.strip_name_encoding (fn_name);
674 fn_name_len = strlen (fn_name);
675 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
5366b186 676
5366b186 677 if (counter < 0)
9e66e106 678 strcpy (buf, "__gcov__");
5366b186 679 else
9e66e106
JJ
680 sprintf (buf, "__gcov%u_", counter);
681 len = strlen (buf);
682#ifndef NO_DOT_IN_LABEL
683 buf[len - 1] = '.';
684#elif !defined NO_DOLLAR_IN_LABEL
685 buf[len - 1] = '$';
686#endif
687 memcpy (buf + len, fn_name, fn_name_len + 1);
5366b186 688 DECL_NAME (var) = get_identifier (buf);
89b0c303
NS
689 TREE_STATIC (var) = 1;
690 TREE_ADDRESSABLE (var) = 1;
691 DECL_ALIGN (var) = TYPE_ALIGN (type);
5366b186
NS
692
693 return var;
694}
695
696/* Creates the gcov_fn_info RECORD_TYPE. */
697
698static void
699build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
ca29da43 700{
5366b186 701 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
ca29da43 702 tree field, fields;
cdb23767 703 tree array_type;
159b3be1 704
5366b186
NS
705 gcc_assert (counters);
706
707 /* ctr_info::num */
708 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
709 get_gcov_unsigned_t ());
710 fields = field;
711
712 /* ctr_info::values */
713 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
714 build_pointer_type (get_gcov_type ()));
715 DECL_CHAIN (field) = fields;
716 fields = field;
717
718 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
719
720 /* key */
721 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
722 build_pointer_type (build_qualified_type
723 (gcov_info_type, TYPE_QUAL_CONST)));
724 fields = field;
725
796621e8 726 /* ident */
5366b186
NS
727 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
728 get_gcov_unsigned_t ());
729 DECL_CHAIN (field) = fields;
730 fields = field;
731
10adac51 732 /* lineno_checksum */
5366b186
NS
733 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
734 get_gcov_unsigned_t ());
910ad8de 735 DECL_CHAIN (field) = fields;
ca29da43
NS
736 fields = field;
737
10adac51 738 /* cfg checksum */
5366b186
NS
739 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
740 get_gcov_unsigned_t ());
10adac51
XDL
741 DECL_CHAIN (field) = fields;
742 fields = field;
743
f81b1a3d 744 array_type = build_index_type (size_int (counters - 1));
5366b186 745 array_type = build_array_type (ctr_info, array_type);
159b3be1 746
ca29da43 747 /* counters */
5366b186 748 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
910ad8de 749 DECL_CHAIN (field) = fields;
ca29da43
NS
750 fields = field;
751
cdb23767 752 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
ca29da43
NS
753}
754
b724567e
NS
755/* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
756 the coverage data for the function and TYPE is the gcov_fn_info
757 RECORD_TYPE. KEY is the object file key. */
cdb23767 758
ca29da43 759static tree
b724567e 760build_fn_info (const struct coverage_data *data, tree type, tree key)
ca29da43 761{
cdb23767 762 tree fields = TYPE_FIELDS (type);
5366b186 763 tree ctr_type;
cdb23767 764 unsigned ix;
9771b263
DN
765 vec<constructor_elt, va_gc> *v1 = NULL;
766 vec<constructor_elt, va_gc> *v2 = NULL;
159b3be1 767
5366b186
NS
768 /* key */
769 CONSTRUCTOR_APPEND_ELT (v1, fields,
770 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
771 fields = DECL_CHAIN (fields);
772
796621e8 773 /* ident */
f9b36bb3
KH
774 CONSTRUCTOR_APPEND_ELT (v1, fields,
775 build_int_cstu (get_gcov_unsigned_t (),
b724567e 776 data->ident));
910ad8de 777 fields = DECL_CHAIN (fields);
159b3be1 778
10adac51
XDL
779 /* lineno_checksum */
780 CONSTRUCTOR_APPEND_ELT (v1, fields,
781 build_int_cstu (get_gcov_unsigned_t (),
b724567e 782 data->lineno_checksum));
10adac51
XDL
783 fields = DECL_CHAIN (fields);
784
785 /* cfg_checksum */
f9b36bb3
KH
786 CONSTRUCTOR_APPEND_ELT (v1, fields,
787 build_int_cstu (get_gcov_unsigned_t (),
b724567e 788 data->cfg_checksum));
910ad8de 789 fields = DECL_CHAIN (fields);
159b3be1 790
ca29da43 791 /* counters */
5366b186 792 ctr_type = TREE_TYPE (TREE_TYPE (fields));
cdb23767
NS
793 for (ix = 0; ix != GCOV_COUNTERS; ix++)
794 if (prg_ctr_mask & (1 << ix))
5366b186 795 {
9771b263 796 vec<constructor_elt, va_gc> *ctr = NULL;
b724567e 797 tree var = data->ctr_vars[ix];
5366b186
NS
798 unsigned count = 0;
799
800 if (var)
801 count
802 = tree_low_cst (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))), 0)
803 + 1;
804
805 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
806 build_int_cstu (get_gcov_unsigned_t (),
807 count));
808
809 if (var)
810 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
811 build_fold_addr_expr (var));
812
813 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
814 }
815
f9b36bb3
KH
816 CONSTRUCTOR_APPEND_ELT (v1, fields,
817 build_constructor (TREE_TYPE (fields), v2));
159b3be1 818
f9b36bb3 819 return build_constructor (type, v1);
ca29da43
NS
820}
821
b724567e
NS
822/* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
823 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
cdb23767 824
5366b186 825static void
b724567e 826build_info_type (tree type, tree fn_info_ptr_type)
ca29da43 827{
cdb23767 828 tree field, fields = NULL_TREE;
b724567e 829 tree merge_fn_type;
9b514d25 830
5366b186
NS
831 /* Version ident */
832 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
833 get_gcov_unsigned_t ());
910ad8de 834 DECL_CHAIN (field) = fields;
ca29da43
NS
835 fields = field;
836
5366b186
NS
837 /* next pointer */
838 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
839 build_pointer_type (build_qualified_type
840 (type, TYPE_QUAL_CONST)));
910ad8de 841 DECL_CHAIN (field) = fields;
ca29da43
NS
842 fields = field;
843
5366b186
NS
844 /* stamp */
845 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
846 get_gcov_unsigned_t ());
910ad8de 847 DECL_CHAIN (field) = fields;
09780dfb
ZD
848 fields = field;
849
5366b186
NS
850 /* Filename */
851 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
852 build_pointer_type (build_qualified_type
853 (char_type_node, TYPE_QUAL_CONST)));
854 DECL_CHAIN (field) = fields;
855 fields = field;
ca29da43 856
5366b186
NS
857 /* merge fn array */
858 merge_fn_type
859 = build_function_type_list (void_type_node,
860 build_pointer_type (get_gcov_type ()),
861 get_gcov_unsigned_t (), NULL_TREE);
862 merge_fn_type
863 = build_array_type (build_pointer_type (merge_fn_type),
864 build_index_type (size_int (GCOV_COUNTERS - 1)));
865 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
866 merge_fn_type);
867 DECL_CHAIN (field) = fields;
868 fields = field;
869
870 /* n_functions */
871 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
872 get_gcov_unsigned_t ());
873 DECL_CHAIN (field) = fields;
874 fields = field;
875
b724567e
NS
876 /* function_info pointer pointer */
877 fn_info_ptr_type = build_pointer_type
878 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
5366b186 879 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
b724567e 880 fn_info_ptr_type);
5366b186
NS
881 DECL_CHAIN (field) = fields;
882 fields = field;
09780dfb 883
5366b186 884 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
ca29da43
NS
885}
886
b724567e
NS
887/* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
888 gcov_info structure type, FN_ARY is the array of pointers to
889 function info objects. */
cdb23767 890
ca29da43 891static tree
b724567e 892build_info (tree info_type, tree fn_ary)
ca29da43 893{
5366b186 894 tree info_fields = TYPE_FIELDS (info_type);
b724567e 895 tree merge_fn_type, n_funcs;
5366b186 896 unsigned ix;
cdb23767 897 tree filename_string;
2f908293 898 int da_file_name_len;
9771b263
DN
899 vec<constructor_elt, va_gc> *v1 = NULL;
900 vec<constructor_elt, va_gc> *v2 = NULL;
159b3be1 901
cdb23767 902 /* Version ident */
5366b186
NS
903 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
904 build_int_cstu (TREE_TYPE (info_fields),
905 GCOV_VERSION));
906 info_fields = DECL_CHAIN (info_fields);
159b3be1 907
ca29da43 908 /* next -- NULL */
5366b186
NS
909 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
910 info_fields = DECL_CHAIN (info_fields);
911
dd486eb2 912 /* stamp */
5366b186
NS
913 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
914 build_int_cstu (TREE_TYPE (info_fields),
cb686b99 915 bbg_file_stamp));
5366b186 916 info_fields = DECL_CHAIN (info_fields);
dd486eb2 917
ca29da43 918 /* Filename */
2f908293
SP
919 da_file_name_len = strlen (da_file_name);
920 filename_string = build_string (da_file_name_len + 1, da_file_name);
4a90aeeb 921 TREE_TYPE (filename_string) = build_array_type
f81b1a3d 922 (char_type_node, build_index_type (size_int (da_file_name_len)));
5366b186
NS
923 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
924 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
925 filename_string));
926 info_fields = DECL_CHAIN (info_fields);
159b3be1 927
5366b186
NS
928 /* merge fn array -- NULL slots indicate unmeasured counters */
929 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
930 for (ix = 0; ix != GCOV_COUNTERS; ix++)
ca29da43 931 {
5366b186 932 tree ptr = null_pointer_node;
159b3be1 933
5366b186
NS
934 if ((1u << ix) & prg_ctr_mask)
935 {
936 tree merge_fn = build_decl (BUILTINS_LOCATION,
937 FUNCTION_DECL,
938 get_identifier (ctr_merge_functions[ix]),
939 TREE_TYPE (merge_fn_type));
940 DECL_EXTERNAL (merge_fn) = 1;
941 TREE_PUBLIC (merge_fn) = 1;
942 DECL_ARTIFICIAL (merge_fn) = 1;
943 TREE_NOTHROW (merge_fn) = 1;
944 /* Initialize assembler name so we can stream out. */
945 DECL_ASSEMBLER_NAME (merge_fn);
946 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
947 }
948 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
ca29da43 949 }
5366b186
NS
950 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
951 build_constructor (TREE_TYPE (info_fields), v2));
952 info_fields = DECL_CHAIN (info_fields);
953
954 /* n_functions */
b724567e
NS
955 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
956 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
957 n_funcs, size_one_node);
958 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
5366b186 959 info_fields = DECL_CHAIN (info_fields);
5366b186 960
b724567e 961 /* functions */
5366b186 962 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
b724567e
NS
963 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
964 info_fields = DECL_CHAIN (info_fields);
965
966 gcc_assert (!info_fields);
5366b186 967 return build_constructor (info_type, v1);
ca29da43
NS
968}
969
b724567e
NS
970/* Create the gcov_info types and object. Generate the constructor
971 function to call __gcov_init. Does not generate the initializer
972 for the object. Returns TRUE if coverage data is being emitted. */
ca29da43 973
b724567e
NS
974static bool
975coverage_obj_init (void)
ca29da43 976{
b724567e
NS
977 tree gcov_info_type, ctor, stmt, init_fn;
978 unsigned n_counters = 0;
5366b186 979 unsigned ix;
b724567e
NS
980 struct coverage_data *fn;
981 struct coverage_data **fn_prev;
c9b9aa64 982 char name_buf[32];
ca29da43 983
6d70e6be 984 no_coverage = 1; /* Disable any further coverage. */
159b3be1 985
cdb23767 986 if (!prg_ctr_mask)
b724567e 987 return false;
159b3be1 988
5366b186
NS
989 if (cgraph_dump_file)
990 fprintf (cgraph_dump_file, "Using data file %s\n", da_file_name);
ca29da43 991
b724567e 992 /* Prune functions. */
5366b186
NS
993 for (fn_prev = &functions_head; (fn = *fn_prev);)
994 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
b724567e 995 fn_prev = &fn->next;
5366b186
NS
996 else
997 /* The function is not being emitted, remove from list. */
998 *fn_prev = fn->next;
b724567e 999
0f9fb22f
JJ
1000 if (functions_head == NULL)
1001 return false;
1002
b724567e
NS
1003 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1004 if ((1u << ix) & prg_ctr_mask)
1005 n_counters++;
5366b186
NS
1006
1007 /* Build the info and fn_info types. These are mutually recursive. */
1008 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
b724567e
NS
1009 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1010 gcov_fn_info_ptr_type = build_pointer_type
1011 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
1012 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
1013 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
5366b186
NS
1014
1015 /* Build the gcov info var, this is referred to in its own
1016 initializer. */
b724567e
NS
1017 gcov_info_var = build_decl (BUILTINS_LOCATION,
1018 VAR_DECL, NULL_TREE, gcov_info_type);
1019 TREE_STATIC (gcov_info_var) = 1;
c9b9aa64 1020 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
b724567e 1021 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
ca29da43 1022
c9b9aa64 1023 /* Build a decl for __gcov_init. */
b724567e
NS
1024 init_fn = build_pointer_type (gcov_info_type);
1025 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
1026 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1027 get_identifier ("__gcov_init"), init_fn);
1028 TREE_PUBLIC (init_fn) = 1;
1029 DECL_EXTERNAL (init_fn) = 1;
1030 DECL_ASSEMBLER_NAME (init_fn);
c9b9aa64
RH
1031
1032 /* Generate a call to __gcov_init(&gcov_info). */
b724567e
NS
1033 ctor = NULL;
1034 stmt = build_fold_addr_expr (gcov_info_var);
1035 stmt = build_call_expr (init_fn, 1, stmt);
1036 append_to_statement_list (stmt, &ctor);
c9b9aa64
RH
1037
1038 /* Generate a constructor to run it. */
b724567e
NS
1039 cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
1040
1041 return true;
ca29da43 1042}
b724567e
NS
1043
1044/* Generate the coverage function info for FN and DATA. Append a
1045 pointer to that object to CTOR and return the appended CTOR. */
1046
9771b263
DN
1047static vec<constructor_elt, va_gc> *
1048coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
b724567e
NS
1049 struct coverage_data const *data)
1050{
1051 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
1052 tree var = build_var (fn, gcov_fn_info_type, -1);
1053
1054 DECL_INITIAL (var) = init;
1055 varpool_finalize_decl (var);
1056
1057 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
1058 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
1059 return ctor;
1060}
1061
1062/* Finalize the coverage data. Generates the array of pointers to
1063 function objects from CTOR. Generate the gcov_info initializer. */
1064
1065static void
9771b263 1066coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
b724567e 1067{
9771b263 1068 unsigned n_functions = vec_safe_length (ctor);
b724567e
NS
1069 tree fn_info_ary_type = build_array_type
1070 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
1071 build_index_type (size_int (n_functions - 1)));
1072 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
1073 fn_info_ary_type);
1074 char name_buf[32];
1075
1076 TREE_STATIC (fn_info_ary) = 1;
1077 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
1078 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
1079 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
1080 varpool_finalize_decl (fn_info_ary);
1081
1082 DECL_INITIAL (gcov_info_var)
1083 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
1084 varpool_finalize_decl (gcov_info_var);
1085}
1086
ca29da43 1087/* Perform file-level initialization. Read in data file, generate name
efbb59b2 1088 of notes file. */
ca29da43
NS
1089
1090void
159b3be1 1091coverage_init (const char *filename)
ca29da43
NS
1092{
1093 int len = strlen (filename);
b724567e 1094 int prefix_len = 0;
b8698a0f 1095
b724567e 1096 if (!profile_data_prefix && !IS_ABSOLUTE_PATH (filename))
2f908293
SP
1097 profile_data_prefix = getpwd ();
1098
b724567e
NS
1099 if (profile_data_prefix)
1100 prefix_len = strlen (profile_data_prefix);
ca29da43 1101
796621e8 1102 /* Name of da file. */
b8698a0f 1103 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
b724567e 1104 + prefix_len + 2);
2f908293
SP
1105
1106 if (profile_data_prefix)
1107 {
b724567e
NS
1108 memcpy (da_file_name, profile_data_prefix, prefix_len);
1109 da_file_name[prefix_len++] = '/';
2f908293 1110 }
b724567e
NS
1111 memcpy (da_file_name + prefix_len, filename, len);
1112 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
159b3be1 1113
cb686b99
NS
1114 bbg_file_stamp = local_tick;
1115
1116 if (flag_branch_probabilities)
1117 read_counts_file ();
1118
796621e8 1119 /* Name of bbg file. */
b724567e
NS
1120 if (flag_test_coverage && !flag_compare_debug)
1121 {
1122 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
1123 memcpy (bbg_file_name, filename, len);
1124 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
1125
1126 if (!gcov_open (bbg_file_name, -1))
1127 {
1128 error ("cannot open %s", bbg_file_name);
1129 bbg_file_name = NULL;
1130 }
1131 else
1132 {
1133 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1134 gcov_write_unsigned (GCOV_VERSION);
cb686b99 1135 gcov_write_unsigned (bbg_file_stamp);
b724567e
NS
1136 }
1137 }
ca29da43
NS
1138}
1139
efbb59b2 1140/* Performs file-level cleanup. Close notes file, generate coverage
ca29da43
NS
1141 variables and constructor. */
1142
1143void
159b3be1 1144coverage_finish (void)
ca29da43 1145{
b724567e
NS
1146 if (bbg_file_name && gcov_close ())
1147 unlink (bbg_file_name);
cb686b99
NS
1148
1149 if (!flag_branch_probabilities && flag_test_coverage
1150 && (!local_tick || local_tick == (unsigned)-1))
1151 /* Only remove the da file, if we're emitting coverage code and
1152 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
b724567e
NS
1153 unlink (da_file_name);
1154
1155 if (coverage_obj_init ())
ca29da43 1156 {
9771b263 1157 vec<constructor_elt, va_gc> *fn_ctor = NULL;
b724567e
NS
1158 struct coverage_data *fn;
1159
1160 for (fn = functions_head; fn; fn = fn->next)
1161 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
1162 coverage_obj_finish (fn_ctor);
ca29da43
NS
1163 }
1164}
1165
ca29da43 1166#include "gt-coverage.h"