| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <assert.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <getopt.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <string.h> |
| 27 |
#include <sys/stat.h> |
| 28 |
#include <limits.h> |
| 29 |
|
| 30 |
#include "alloca.h" |
| 31 |
#include "argv-iter.h" |
| 32 |
#include "closeout.h" |
| 33 |
#include "dirname.h" |
| 34 |
#include "error.h" |
| 35 |
#include "inttostr.h" |
| 36 |
#include "pathmax.h" |
| 37 |
#include "progname.h" |
| 38 |
#include "quote.h" |
| 39 |
#include "quotearg.h" |
| 40 |
#include "xalloc.h" |
| 41 |
|
| 42 |
#include "xnls.h" |
| 43 |
#include "idfile.h" |
| 44 |
#include "idu-hash.h" |
| 45 |
#include "scanners.h" |
| 46 |
#include "iduglobal.h" |
| 47 |
|
| 48 |
#ifndef FALLTHROUGH |
| 49 |
# if __GNUC__ < 7 |
| 50 |
# define FALLTHROUGH ((void) 0) |
| 51 |
# else |
| 52 |
# define FALLTHROUGH __attribute__ ((__fallthrough__)) |
| 53 |
# endif |
| 54 |
#endif |
| 55 |
|
| 56 |
struct summary |
| 57 |
{ |
| 58 |
struct token **sum_tokens; |
| 59 |
unsigned char const *sum_hits; |
| 60 |
struct summary *sum_parent; |
| 61 |
union { |
| 62 |
struct summary *u_kids[8]; |
| 63 |
#define sum_kids sum_u.u_kids |
| 64 |
struct member_file *u_files[8]; |
| 65 |
} sum_u; |
| 66 |
unsigned long sum_tokens_size; |
| 67 |
unsigned long sum_hits_count; |
| 68 |
int sum_free_index; |
| 69 |
int sum_level; |
| 70 |
}; |
| 71 |
|
| 72 |
void usage (void); |
| 73 |
static int ceil_log_8 (unsigned long n); |
| 74 |
static int ceil_log_2 (unsigned long n); |
| 75 |
static void assert_writeable (char const *file_name); |
| 76 |
static void scan_files (struct idhead const *idhp); |
| 77 |
static void scan_member_file (struct member_file const *member); |
| 78 |
static void scan_member_file_1 (get_token_func_t get_token, |
| 79 |
void const *args, FILE *source_FILE); |
| 80 |
static void report_statistics (void); |
| 81 |
static void write_id_file (struct idhead *idhp); |
| 82 |
static unsigned long token_hash_1 (void const *key); |
| 83 |
static unsigned long token_hash_2 (void const *key); |
| 84 |
static int token_hash_cmp (void const *x, void const *y); |
| 85 |
static int token_qsort_cmp (void const *x, void const *y); |
| 86 |
static void bump_current_hits_signature (void); |
| 87 |
static void init_hits_signature (int i); |
| 88 |
static void free_summary_tokens (void); |
| 89 |
static void summarize (void); |
| 90 |
static void init_summary (void); |
| 91 |
static struct summary *make_sibling_summary (struct summary *summary); |
| 92 |
static int count_vec_size (struct summary *summary, |
| 93 |
unsigned char const *tail_hits); |
| 94 |
static int count_buf_size (struct summary *summary, |
| 95 |
unsigned char const *tail_hits); |
| 96 |
static int check_hits (struct summary* summary) _GL_ATTRIBUTE_PURE; |
| 97 |
static void write_hits (FILE *fp, struct summary *summary, |
| 98 |
unsigned char const *tail_hits); |
| 99 |
static void sign_token (struct token *token); |
| 100 |
static void add_token_to_summary (struct summary *summary, struct token *token); |
| 101 |
|
| 102 |
static struct hash_table token_table; |
| 103 |
|
| 104 |
|
| 105 |
static unsigned long input_chars; |
| 106 |
static unsigned long name_tokens; |
| 107 |
static unsigned long number_tokens; |
| 108 |
static unsigned long string_tokens; |
| 109 |
static unsigned long literal_tokens; |
| 110 |
static unsigned long comment_tokens; |
| 111 |
static unsigned long occurrences; |
| 112 |
static unsigned long hits_length = 0; |
| 113 |
static unsigned long tokens_length = 0; |
| 114 |
static unsigned long output_length = 0; |
| 115 |
|
| 116 |
static int verbose_flag = 0; |
| 117 |
static int statistics_flag = 0; |
| 118 |
|
| 119 |
static int levels = 0; |
| 120 |
|
| 121 |
static unsigned char *current_hits_signature; |
| 122 |
#define INIT_TOKENS_SIZE(level) (1 << ((level) + 13)) |
| 123 |
static struct summary *summary_root; |
| 124 |
static struct summary *summary_leaf; |
| 125 |
|
| 126 |
static char *lang_map_file_name = 0; |
| 127 |
static int show_version = 0; |
| 128 |
static int show_help = 0; |
| 129 |
struct idhead idh; |
| 130 |
static struct file_link *cw_dlink; |
| 131 |
|
| 132 |
void usage (void) __attribute__((__noreturn__)); |
| 133 |
void |
| 134 |
usage (void) |
| 135 |
{ |
| 136 |
fprintf (stderr, _("Try `%s --help' for more information.\n"), |
| 137 |
program_name); |
| 138 |
exit (EXIT_FAILURE); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
enum |
| 144 |
{ |
| 145 |
FILES0_FROM_OPTION = CHAR_MAX +1, |
| 146 |
}; |
| 147 |
|
| 148 |
static struct option const long_options[] = |
| 149 |
{ |
| 150 |
{ "file", required_argument, 0, 'f' }, |
| 151 |
{ "output", required_argument, 0, 'o' }, |
| 152 |
{ "include", required_argument, 0, 'i' }, |
| 153 |
{ "exclude", required_argument, 0, 'x' }, |
| 154 |
{ "lang-option", required_argument, 0, 'l' }, |
| 155 |
{ "lang-map", required_argument, 0, 'm' }, |
| 156 |
{ "default-lang", required_argument, 0, 'd' }, |
| 157 |
{ "prune", required_argument, 0, 'p' }, |
| 158 |
{ "verbose", no_argument, 0, 'v' }, |
| 159 |
{ "statistics", no_argument, 0, 's' }, |
| 160 |
{ "help", no_argument, &show_help, 1 }, |
| 161 |
{ "version", no_argument, &show_version, 1 }, |
| 162 |
{ "files0-from", required_argument, NULL, FILES0_FROM_OPTION }, |
| 163 |
{NULL, 0, NULL, 0} |
| 164 |
}; |
| 165 |
|
| 166 |
static void __attribute__((__noreturn__)) |
| 167 |
help_me (void) |
| 168 |
{ |
| 169 |
printf (_("\ |
| 170 |
Usage: %s [OPTION]... [FILE]...\n\ |
| 171 |
"), program_name); |
| 172 |
|
| 173 |
printf (_("\ |
| 174 |
Build an identifier database.\n\ |
| 175 |
-o, --output=OUTFILE file name of ID database output\n\ |
| 176 |
-f, --file=OUTFILE synonym for --output\n\ |
| 177 |
-i, --include=LANGS include languages in LANGS (default: \"C C++ asm\")\n\ |
| 178 |
-x, --exclude=LANGS exclude languages in LANGS\n\ |
| 179 |
-l, --lang-option=L:OPT pass OPT as a default for language L (see below)\n\ |
| 180 |
-m, --lang-map=MAPFILE use MAPFILE to map file names onto source language\n\ |
| 181 |
-d, --default-lang=LANG make LANG the default source language\n\ |
| 182 |
-p, --prune=NAMES exclude the named files and/or directories\n\ |
| 183 |
-v, --verbose report per file statistics\n\ |
| 184 |
-s, --statistics report statistics at end of run\n\ |
| 185 |
\n\ |
| 186 |
--files0-from=F tokenize only the files specified by\n\ |
| 187 |
NUL-terminated names in file F\n\ |
| 188 |
\n\ |
| 189 |
--help display this help and exit\n\ |
| 190 |
--version output version information and exit\n\ |
| 191 |
\n\ |
| 192 |
FILE may be a file name, or a directory name to recursively search.\n\ |
| 193 |
If no FILE is given, the current directory is searched by default.\n\ |
| 194 |
Note that the `--include' and `--exclude' options are mutually-exclusive.\n\ |
| 195 |
\n\ |
| 196 |
The following arguments apply to the language-specific scanners:\n\ |
| 197 |
")); |
| 198 |
language_help_me (); |
| 199 |
printf (_("\nReport bugs to " PACKAGE_BUGREPORT "\n\n")); |
| 200 |
exit (EXIT_SUCCESS); |
| 201 |
} |
| 202 |
|
| 203 |
static void *heap_initial; |
| 204 |
static void *heap_after_walk; |
| 205 |
static void *heap_after_scan; |
| 206 |
|
| 207 |
static void *get_process_heap(void) |
| 208 |
{ |
| 209 |
#if HAVE_SBRK |
| 210 |
return sbrk(0); |
| 211 |
#else |
| 212 |
return 0; |
| 213 |
#endif |
| 214 |
} |
| 215 |
|
| 216 |
int |
| 217 |
main (int argc, char **argv) |
| 218 |
{ |
| 219 |
bool ok; |
| 220 |
int nfiles; |
| 221 |
char *files_from = NULL; |
| 222 |
|
| 223 |
set_program_name (argv[0]); |
| 224 |
heap_initial = get_process_heap(); |
| 225 |
idh.idh_file_name = DEFAULT_ID_FILE_NAME; |
| 226 |
|
| 227 |
#if ENABLE_NLS |
| 228 |
|
| 229 |
setlocale (LC_ALL, ""); |
| 230 |
|
| 231 |
|
| 232 |
bindtextdomain (PACKAGE, LOCALEDIR); |
| 233 |
textdomain (PACKAGE); |
| 234 |
#endif |
| 235 |
|
| 236 |
atexit (close_stdout); |
| 237 |
|
| 238 |
for (;;) |
| 239 |
{ |
| 240 |
int optc = getopt_long (argc, argv, "o:f:i:x:l:m:d:p:vVs", |
| 241 |
long_options, (int *) 0); |
| 242 |
if (optc < 0) |
| 243 |
break; |
| 244 |
switch (optc) |
| 245 |
{ |
| 246 |
case 0: |
| 247 |
break; |
| 248 |
|
| 249 |
case 'o': |
| 250 |
case 'f': |
| 251 |
idh.idh_file_name = optarg; |
| 252 |
break; |
| 253 |
|
| 254 |
case 'i': |
| 255 |
include_languages (optarg); |
| 256 |
break; |
| 257 |
|
| 258 |
case 'x': |
| 259 |
exclude_languages (optarg); |
| 260 |
break; |
| 261 |
|
| 262 |
case 'l': |
| 263 |
language_save_arg (optarg); |
| 264 |
break; |
| 265 |
|
| 266 |
case 'm': |
| 267 |
lang_map_file_name = optarg; |
| 268 |
break; |
| 269 |
|
| 270 |
case 'd': |
| 271 |
set_default_language (optarg); |
| 272 |
break; |
| 273 |
|
| 274 |
case 'p': |
| 275 |
if (cw_dlink == 0) |
| 276 |
cw_dlink = init_walker (&idh); |
| 277 |
prune_file_names (optarg, cw_dlink); |
| 278 |
break; |
| 279 |
|
| 280 |
case FILES0_FROM_OPTION: |
| 281 |
files_from = optarg; |
| 282 |
break; |
| 283 |
|
| 284 |
case 'V': |
| 285 |
walker_verbose_flag = 1; |
| 286 |
FALLTHROUGH; |
| 287 |
case 'v': |
| 288 |
verbose_flag = 1; |
| 289 |
FALLTHROUGH; |
| 290 |
case 's': |
| 291 |
statistics_flag = 1; |
| 292 |
break; |
| 293 |
|
| 294 |
default: |
| 295 |
usage (); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if (show_version) |
| 300 |
{ |
| 301 |
printf ("%s - %s\n", program_name, PACKAGE_VERSION); |
| 302 |
exit (EXIT_SUCCESS); |
| 303 |
} |
| 304 |
|
| 305 |
if (show_help) |
| 306 |
help_me (); |
| 307 |
|
| 308 |
nfiles = argc - optind; |
| 309 |
|
| 310 |
struct argv_iterator *ai; |
| 311 |
if (files_from) |
| 312 |
{ |
| 313 |
|
| 314 |
|
| 315 |
if (nfiles != 0) |
| 316 |
{ |
| 317 |
error (0, 0, _("extra operand %s"), quote (argv[optind])); |
| 318 |
fprintf (stderr, "%s\n", |
| 319 |
_("file operands cannot be combined with --files0-from")); |
| 320 |
usage(); |
| 321 |
} |
| 322 |
|
| 323 |
if (! (strequ (files_from, "-") || freopen (files_from, "r", stdin))) |
| 324 |
error (EXIT_FAILURE, errno, _("cannot open %s for reading"), |
| 325 |
quote (files_from)); |
| 326 |
|
| 327 |
ai = argv_iter_init_stream (stdin); |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
static char *cwd_only[2]; |
| 332 |
cwd_only[0] = bad_cast ("."); |
| 333 |
cwd_only[1] = NULL; |
| 334 |
char **files = (optind < argc ? argv + optind : cwd_only); |
| 335 |
ai = argv_iter_init_argv (files); |
| 336 |
} |
| 337 |
|
| 338 |
language_getopt (); |
| 339 |
assert_writeable (idh.idh_file_name); |
| 340 |
if (cw_dlink == 0) |
| 341 |
cw_dlink = init_walker (&idh); |
| 342 |
parse_language_map (lang_map_file_name); |
| 343 |
|
| 344 |
|
| 345 |
ok = true; |
| 346 |
while (true) |
| 347 |
{ |
| 348 |
bool skip_file = false; |
| 349 |
enum argv_iter_err ai_err; |
| 350 |
char *file_name = argv_iter (ai, &ai_err); |
| 351 |
if (ai_err == AI_ERR_EOF) |
| 352 |
break; |
| 353 |
if (!file_name) |
| 354 |
{ |
| 355 |
switch (ai_err) |
| 356 |
{ |
| 357 |
case AI_ERR_READ: |
| 358 |
error (0, errno, _("%s: read error"), quote (files_from)); |
| 359 |
continue; |
| 360 |
|
| 361 |
case AI_ERR_MEM: |
| 362 |
xalloc_die (); |
| 363 |
|
| 364 |
default: |
| 365 |
assert (!"unexpected error code from argv_iter"); |
| 366 |
} |
| 367 |
} |
| 368 |
if (files_from && STREQ (files_from, "-") && STREQ (file_name, "-")) |
| 369 |
{ |
| 370 |
|
| 371 |
|
| 372 |
error (0, 0, _("when reading file names from stdin, " |
| 373 |
"no file name of %s allowed"), |
| 374 |
quote (file_name)); |
| 375 |
skip_file = true; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
if (!file_name[0]) |
| 380 |
{ |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
if (files_from == NULL) |
| 386 |
error (0, 0, "%s", _("invalid zero-length file name")); |
| 387 |
else |
| 388 |
{ |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
unsigned long int file_number = argv_iter_n_args (ai); |
| 393 |
error (0, 0, "%s:%lu: %s", quotearg_colon (files_from), |
| 394 |
file_number, _("invalid zero-length file name")); |
| 395 |
} |
| 396 |
skip_file = true; |
| 397 |
} |
| 398 |
|
| 399 |
if (skip_file) |
| 400 |
ok = false; |
| 401 |
else |
| 402 |
{ |
| 403 |
struct file_link *flink = parse_file_name (file_name, cw_dlink); |
| 404 |
if (flink) |
| 405 |
walk_flink (flink, 0); |
| 406 |
|
| 407 |
|
| 408 |
} |
| 409 |
} |
| 410 |
argv_iter_free (ai); |
| 411 |
|
| 412 |
heap_after_walk = get_process_heap(); |
| 413 |
|
| 414 |
mark_member_file_links (&idh); |
| 415 |
log_8_member_files = ceil_log_8 (idh.idh_member_file_table.ht_fill); |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
if (log_8_member_files == 0) |
| 427 |
log_8_member_files = 1; |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
current_hits_signature = xmalloc (log_8_member_files); |
| 432 |
|
| 433 |
|
| 434 |
if (idh.idh_member_file_table.ht_fill) |
| 435 |
{ |
| 436 |
scan_files (&idh); |
| 437 |
heap_after_scan = get_process_heap(); |
| 438 |
|
| 439 |
free_summary_tokens (); |
| 440 |
free (token_table.ht_vec); |
| 441 |
chdir_to_link (cw_dlink); |
| 442 |
write_id_file (&idh); |
| 443 |
|
| 444 |
if (statistics_flag) |
| 445 |
report_statistics (); |
| 446 |
} |
| 447 |
else |
| 448 |
error (0, 0, _("nothing to do")); |
| 449 |
|
| 450 |
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
static int _GL_ATTRIBUTE_CONST |
| 456 |
ceil_log_8 (unsigned long n) |
| 457 |
{ |
| 458 |
int log_8 = 0; |
| 459 |
|
| 460 |
n--; |
| 461 |
while (n) |
| 462 |
{ |
| 463 |
log_8++; |
| 464 |
n >>= 3; |
| 465 |
} |
| 466 |
return log_8; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
static int _GL_ATTRIBUTE_CONST |
| 472 |
ceil_log_2 (unsigned long n) |
| 473 |
{ |
| 474 |
int log_2 = 0; |
| 475 |
|
| 476 |
n--; |
| 477 |
while (n) |
| 478 |
{ |
| 479 |
log_2++; |
| 480 |
n >>= 1; |
| 481 |
} |
| 482 |
return log_2; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
static void |
| 490 |
assert_writeable (char const *filename) |
| 491 |
{ |
| 492 |
if (access (filename, 06) < 0) |
| 493 |
{ |
| 494 |
if (errno == ENOENT) |
| 495 |
{ |
| 496 |
char *dirname = dir_name (filename); |
| 497 |
if (access (dirname, 06) < 0) |
| 498 |
error (EXIT_FAILURE, errno, _("can't create `%s' in `%s'"), |
| 499 |
base_name (filename), dirname); |
| 500 |
free(dirname); |
| 501 |
} |
| 502 |
else |
| 503 |
error (EXIT_FAILURE, errno, _("can't modify `%s'"), filename); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
static void |
| 511 |
scan_files (struct idhead const *idhp) |
| 512 |
{ |
| 513 |
struct member_file **members_0 |
| 514 |
= (struct member_file **) hash_dump (&idhp->idh_member_file_table, |
| 515 |
0, member_file_qsort_compare); |
| 516 |
struct member_file **end = &members_0[idhp->idh_member_file_table.ht_fill]; |
| 517 |
struct member_file **members = members_0; |
| 518 |
int n = idhp->idh_member_file_table.ht_fill; |
| 519 |
|
| 520 |
n = n * ceil_log_2 (n) * 16; |
| 521 |
if (n == 0) |
| 522 |
n = 1024; |
| 523 |
else if (n > 1024*1024) |
| 524 |
n = 1024*1024; |
| 525 |
|
| 526 |
hash_init (&token_table, n, token_hash_1, token_hash_2, token_hash_cmp); |
| 527 |
if (verbose_flag) { |
| 528 |
char offstr[INT_BUFSIZE_BOUND(off_t)]; |
| 529 |
|
| 530 |
printf ("files=%lu, largest=%s, slots=%lu\n", |
| 531 |
idhp->idh_member_file_table.ht_fill, |
| 532 |
offtostr(largest_member_file, offstr), |
| 533 |
token_table.ht_size); |
| 534 |
} |
| 535 |
init_hits_signature (0); |
| 536 |
init_summary (); |
| 537 |
obstack_init (&tokens_obstack); |
| 538 |
|
| 539 |
if (largest_member_file > MAX_LARGEST_MEMBER_FILE) |
| 540 |
largest_member_file = MAX_LARGEST_MEMBER_FILE; |
| 541 |
scanner_buffer = xmalloc (largest_member_file + 1); |
| 542 |
|
| 543 |
for (;;) |
| 544 |
{ |
| 545 |
const struct member_file *member = *members++; |
| 546 |
scan_member_file (member); |
| 547 |
if (members == end) |
| 548 |
break; |
| 549 |
if (current_hits_signature[0] & 0x80) |
| 550 |
summarize (); |
| 551 |
bump_current_hits_signature (); |
| 552 |
} |
| 553 |
|
| 554 |
free (scanner_buffer); |
| 555 |
free (members_0); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
static void |
| 561 |
scan_member_file (struct member_file const *member) |
| 562 |
{ |
| 563 |
struct lang_args const *lang_args = member->mf_lang_args; |
| 564 |
struct language const *lang = lang_args->la_language; |
| 565 |
get_token_func_t get_token = lang->lg_get_token; |
| 566 |
struct file_link *flink = member->mf_link; |
| 567 |
struct stat st; |
| 568 |
FILE *source_FILE; |
| 569 |
|
| 570 |
chdir_to_link (flink->fl_parent); |
| 571 |
source_FILE = fopen (flink->fl_name, "r"); |
| 572 |
if (source_FILE) |
| 573 |
{ |
| 574 |
char *file_name = alloca (PATH_MAX); |
| 575 |
if (statistics_flag) |
| 576 |
{ |
| 577 |
if (fstat (fileno (source_FILE), &st) < 0) |
| 578 |
{ |
| 579 |
maybe_relative_file_name (file_name, flink, cw_dlink); |
| 580 |
error (0, errno, _("can't stat `%s'"), file_name); |
| 581 |
} |
| 582 |
else |
| 583 |
input_chars += st.st_size; |
| 584 |
} |
| 585 |
if (verbose_flag) |
| 586 |
{ |
| 587 |
maybe_relative_file_name (file_name, flink, cw_dlink); |
| 588 |
printf ("%ld: %s: %s", member->mf_index, lang->lg_name, file_name); |
| 589 |
fflush (stdout); |
| 590 |
} |
| 591 |
scan_member_file_1 (get_token, lang_args->la_args_digested, source_FILE); |
| 592 |
if (verbose_flag) |
| 593 |
putchar ('\n'); |
| 594 |
fclose (source_FILE); |
| 595 |
} |
| 596 |
else |
| 597 |
error (0, errno, _("can't open `%s'"), flink->fl_name); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
static void |
| 604 |
scan_member_file_1 (get_token_func_t get_token, void const *args, FILE *source_FILE) |
| 605 |
{ |
| 606 |
struct token **slot; |
| 607 |
struct token *token; |
| 608 |
int flags; |
| 609 |
int new_tokens = 0; |
| 610 |
int distinct_tokens = 0; |
| 611 |
|
| 612 |
while ((token = (*get_token) (source_FILE, args, &flags)) != NULL) |
| 613 |
{ |
| 614 |
if (*TOKEN_NAME (token) == '\0') { |
| 615 |
obstack_free (&tokens_obstack, token); |
| 616 |
continue; |
| 617 |
} |
| 618 |
slot = (struct token **) hash_find_slot (&token_table, token); |
| 619 |
if (HASH_VACANT (*slot)) |
| 620 |
{ |
| 621 |
token->tok_flags = flags; |
| 622 |
token->tok_count = 1; |
| 623 |
memset (TOKEN_HITS (token), 0, log_8_member_files); |
| 624 |
sign_token (token); |
| 625 |
if (verbose_flag) |
| 626 |
{ |
| 627 |
distinct_tokens++; |
| 628 |
new_tokens++; |
| 629 |
} |
| 630 |
hash_insert_at (&token_table, token, slot); |
| 631 |
} |
| 632 |
else |
| 633 |
{ |
| 634 |
obstack_free (&tokens_obstack, token); |
| 635 |
token = *slot; |
| 636 |
token->tok_flags |= flags; |
| 637 |
if (token->tok_count < USHRT_MAX) |
| 638 |
token->tok_count++; |
| 639 |
if (!(TOKEN_HITS (token)[0] & current_hits_signature[0])) |
| 640 |
{ |
| 641 |
sign_token (token); |
| 642 |
if (verbose_flag) |
| 643 |
distinct_tokens++; |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
if (verbose_flag) |
| 648 |
{ |
| 649 |
printf (_(" new = %d/%d"), new_tokens, distinct_tokens); |
| 650 |
if (distinct_tokens != 0) |
| 651 |
printf (" = %.0f%%", 100.0 * (double) new_tokens / (double) distinct_tokens); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
static void |
| 656 |
report_statistics (void) |
| 657 |
{ |
| 658 |
printf (_("Name=%lu, "), name_tokens); |
| 659 |
printf (_("Number=%lu, "), number_tokens); |
| 660 |
printf (_("String=%lu, "), string_tokens); |
| 661 |
printf (_("Literal=%lu, "), literal_tokens); |
| 662 |
printf (_("Comment=%lu\n"), comment_tokens); |
| 663 |
|
| 664 |
printf (_("Files=%lu, "), idh.idh_files); |
| 665 |
printf (_("Tokens=%lu, "), occurrences); |
| 666 |
printf (_("Bytes=%lu Kb, "), input_chars / 1024); |
| 667 |
printf (_("Heap=%llu+%llu Kb, "), |
| 668 |
(unsigned long long) ((char *) heap_after_scan |
| 669 |
- (char *) heap_after_walk) / 1024, |
| 670 |
(unsigned long long) ((char *) heap_after_walk |
| 671 |
- (char *) heap_initial) / 1024); |
| 672 |
printf (_("Output=%lu (%lu tok, %lu hit)\n"), |
| 673 |
output_length, tokens_length, hits_length); |
| 674 |
|
| 675 |
hash_print_stats (&token_table, stdout); |
| 676 |
printf (_(", Freq=%lu/%lu=%.2f\n"), occurrences, token_table.ht_fill, |
| 677 |
(double) occurrences / (double) token_table.ht_fill); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
static void |
| 689 |
write_id_file (struct idhead *idhp) |
| 690 |
{ |
| 691 |
struct token **tokens; |
| 692 |
int i; |
| 693 |
int buf_size; |
| 694 |
int vec_size; |
| 695 |
int tok_size; |
| 696 |
int max_buf_size = 0; |
| 697 |
int max_vec_size = 0; |
| 698 |
|
| 699 |
if (verbose_flag) |
| 700 |
printf (_("Sorting tokens...\n")); |
| 701 |
|
| 702 |
assert (summary_root->sum_hits_count == token_table.ht_fill); |
| 703 |
tokens = xnrealloc (summary_root->sum_tokens, |
| 704 |
token_table.ht_fill, sizeof *tokens); |
| 705 |
qsort (tokens, token_table.ht_fill, sizeof (struct token *), token_qsort_cmp); |
| 706 |
|
| 707 |
if (verbose_flag) |
| 708 |
printf (_("Writing `%s'...\n"), idhp->idh_file_name); |
| 709 |
idhp->idh_FILE = fopen (idhp->idh_file_name, "w+b"); |
| 710 |
if (idhp->idh_FILE == NULL) |
| 711 |
error (EXIT_FAILURE, errno, _("can't create `%s'"), idhp->idh_file_name); |
| 712 |
|
| 713 |
idhp->idh_magic[0] = IDH_MAGIC_0; |
| 714 |
idhp->idh_magic[1] = IDH_MAGIC_1; |
| 715 |
idhp->idh_version = IDH_VERSION; |
| 716 |
idhp->idh_flags = IDH_COUNTS; |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
fseek (idhp->idh_FILE, sizeof_idhead (), 0); |
| 721 |
off_t off = ftello (idhp->idh_FILE); |
| 722 |
if (UINT32_MAX < off) |
| 723 |
error (EXIT_FAILURE, 0, _("internal limitation: offset of 2^32 or larger")); |
| 724 |
idhp->idh_flinks_offset = off; |
| 725 |
serialize_file_links (idhp); |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
putc ('\0', idhp->idh_FILE); |
| 730 |
putc ('\0', idhp->idh_FILE); |
| 731 |
off = ftello (idhp->idh_FILE); |
| 732 |
if (UINT32_MAX < off) |
| 733 |
error (EXIT_FAILURE, 0, _("internal limitation: offset of 2^32 or larger")); |
| 734 |
idhp->idh_tokens_offset = off; |
| 735 |
|
| 736 |
for (i = 0; i < token_table.ht_fill; i++, tokens++) |
| 737 |
{ |
| 738 |
struct token *token = *tokens; |
| 739 |
|
| 740 |
occurrences += token->tok_count; |
| 741 |
if (token->tok_flags & TOK_NUMBER) |
| 742 |
number_tokens++; |
| 743 |
if (token->tok_flags & TOK_NAME) |
| 744 |
name_tokens++; |
| 745 |
if (token->tok_flags & TOK_STRING) |
| 746 |
string_tokens++; |
| 747 |
if (token->tok_flags & TOK_LITERAL) |
| 748 |
literal_tokens++; |
| 749 |
if (token->tok_flags & TOK_COMMENT) |
| 750 |
comment_tokens++; |
| 751 |
|
| 752 |
fputs (TOKEN_NAME (token), idhp->idh_FILE); |
| 753 |
putc ('\0', idhp->idh_FILE); |
| 754 |
if (token->tok_count > 0xff) |
| 755 |
token->tok_flags |= TOK_SHORT_COUNT; |
| 756 |
putc (token->tok_flags, idhp->idh_FILE); |
| 757 |
putc (token->tok_count & 0xff, idhp->idh_FILE); |
| 758 |
if (token->tok_flags & TOK_SHORT_COUNT) |
| 759 |
putc (token->tok_count >> 8, idhp->idh_FILE); |
| 760 |
|
| 761 |
vec_size = count_vec_size (summary_root, TOKEN_HITS (token) + levels); |
| 762 |
buf_size = count_buf_size (summary_root, TOKEN_HITS (token) + levels); |
| 763 |
hits_length += buf_size; |
| 764 |
tok_size = strlen (TOKEN_NAME (token)) + 1; |
| 765 |
tokens_length += tok_size; |
| 766 |
buf_size += tok_size + sizeof (token->tok_flags) + sizeof (token->tok_count) + 2; |
| 767 |
if (buf_size > max_buf_size) |
| 768 |
max_buf_size = buf_size; |
| 769 |
if (vec_size > max_vec_size) |
| 770 |
max_vec_size = vec_size; |
| 771 |
|
| 772 |
write_hits (idhp->idh_FILE, summary_root, TOKEN_HITS (token) + levels); |
| 773 |
putc ('\0', idhp->idh_FILE); |
| 774 |
putc ('\0', idhp->idh_FILE); |
| 775 |
} |
| 776 |
assert (check_hits (summary_root) == 0); |
| 777 |
idhp->idh_tokens = token_table.ht_fill; |
| 778 |
off = ftello (idhp->idh_FILE); |
| 779 |
if (UINT32_MAX < off) |
| 780 |
error (EXIT_FAILURE, 0, _("internal limitation: offset of 2^32 or larger")); |
| 781 |
output_length = off; |
| 782 |
idhp->idh_end_offset = output_length - 2; |
| 783 |
idhp->idh_buf_size = max_buf_size; |
| 784 |
idhp->idh_vec_size = max_vec_size; |
| 785 |
|
| 786 |
write_idhead (&idh); |
| 787 |
if (ferror (idhp->idh_FILE) || fclose (idhp->idh_FILE) != 0) |
| 788 |
error (EXIT_FAILURE, errno, _("error closing `%s'"), idhp->idh_file_name); |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
static unsigned long _GL_ATTRIBUTE_PURE |
| 795 |
token_hash_1 (void const *key) |
| 796 |
{ |
| 797 |
return_STRING_HASH_1 (TOKEN_NAME ((struct token const *) key)); |
| 798 |
} |
| 799 |
|
| 800 |
static unsigned long _GL_ATTRIBUTE_PURE |
| 801 |
token_hash_2 (void const *key) |
| 802 |
{ |
| 803 |
return_STRING_HASH_2 (TOKEN_NAME ((struct token const *) key)); |
| 804 |
} |
| 805 |
|
| 806 |
static int _GL_ATTRIBUTE_PURE |
| 807 |
token_hash_cmp (void const *x, void const *y) |
| 808 |
{ |
| 809 |
return_STRING_COMPARE (TOKEN_NAME ((struct token const *) x), |
| 810 |
TOKEN_NAME ((struct token const *) y)); |
| 811 |
} |
| 812 |
|
| 813 |
static int _GL_ATTRIBUTE_PURE |
| 814 |
token_qsort_cmp (void const *x, void const *y) |
| 815 |
{ |
| 816 |
return_STRING_COMPARE (TOKEN_NAME (*(struct token const *const *) x), |
| 817 |
TOKEN_NAME (*(struct token const *const *) y)); |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
static void |
| 827 |
bump_current_hits_signature (void) |
| 828 |
{ |
| 829 |
unsigned char *hits = current_hits_signature; |
| 830 |
while (*hits & 0x80) |
| 831 |
*hits++ = 1; |
| 832 |
*hits <<= 1; |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
static void |
| 838 |
init_hits_signature (int i) |
| 839 |
{ |
| 840 |
unsigned char *hits = current_hits_signature; |
| 841 |
unsigned char const *end = current_hits_signature + log_8_member_files; |
| 842 |
while (hits < end) |
| 843 |
{ |
| 844 |
*hits = 1 << (i & 7); |
| 845 |
i >>= 3; |
| 846 |
hits++; |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
static void |
| 851 |
free_summary_tokens (void) |
| 852 |
{ |
| 853 |
struct summary *summary = summary_leaf; |
| 854 |
while (summary != summary_root) |
| 855 |
{ |
| 856 |
free (summary->sum_tokens); |
| 857 |
summary = summary->sum_parent; |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
static void |
| 862 |
summarize (void) |
| 863 |
{ |
| 864 |
unsigned char const *hits_sig = current_hits_signature; |
| 865 |
struct summary *summary = summary_leaf; |
| 866 |
|
| 867 |
do |
| 868 |
{ |
| 869 |
unsigned long count = summary->sum_hits_count; |
| 870 |
unsigned char *hits = xmalloc (count + 1); |
| 871 |
unsigned int level = summary->sum_level; |
| 872 |
struct token **tokens = summary->sum_tokens; |
| 873 |
unsigned long init_size = INIT_TOKENS_SIZE (summary->sum_level); |
| 874 |
|
| 875 |
if (verbose_flag) |
| 876 |
printf (_("level %d: %lu/%lu = %.0f%%\n"), |
| 877 |
summary->sum_level, count, init_size, |
| 878 |
100.0 * (double) count / (double) init_size); |
| 879 |
|
| 880 |
qsort (tokens, count, sizeof (struct token *), token_qsort_cmp); |
| 881 |
summary->sum_hits = hits; |
| 882 |
while (count--) |
| 883 |
{ |
| 884 |
unsigned char *hit = TOKEN_HITS (*tokens++) + level; |
| 885 |
*hits++ = *hit; |
| 886 |
*hit = 0; |
| 887 |
} |
| 888 |
*hits++ = 0; |
| 889 |
if (summary->sum_parent) |
| 890 |
{ |
| 891 |
free (summary->sum_tokens); |
| 892 |
summary->sum_tokens = 0; |
| 893 |
} |
| 894 |
summary = summary->sum_parent; |
| 895 |
} |
| 896 |
while (*++hits_sig & 0x80); |
| 897 |
summary_leaf = make_sibling_summary (summary_leaf); |
| 898 |
} |
| 899 |
|
| 900 |
static void |
| 901 |
init_summary (void) |
| 902 |
{ |
| 903 |
unsigned long size = INIT_TOKENS_SIZE (0); |
| 904 |
summary_root = summary_leaf = xcalloc (1, sizeof(struct summary)); |
| 905 |
summary_root->sum_tokens_size = size; |
| 906 |
summary_root->sum_tokens = xmalloc (sizeof(struct token *) * size); |
| 907 |
} |
| 908 |
|
| 909 |
static struct summary * |
| 910 |
make_sibling_summary (struct summary *summary) |
| 911 |
{ |
| 912 |
struct summary *parent = summary->sum_parent; |
| 913 |
size_t size; |
| 914 |
|
| 915 |
if (parent == NULL) |
| 916 |
{ |
| 917 |
levels++; |
| 918 |
summary_root = summary->sum_parent = parent |
| 919 |
= xcalloc (1, sizeof(struct summary)); |
| 920 |
parent->sum_level = levels; |
| 921 |
parent->sum_kids[0] = summary; |
| 922 |
parent->sum_hits_count = summary->sum_hits_count; |
| 923 |
parent->sum_free_index = 1; |
| 924 |
size = INIT_TOKENS_SIZE (levels); |
| 925 |
if (summary->sum_tokens_size >= size) |
| 926 |
{ |
| 927 |
parent->sum_tokens_size = summary->sum_tokens_size; |
| 928 |
parent->sum_tokens = summary->sum_tokens; |
| 929 |
} |
| 930 |
else |
| 931 |
{ |
| 932 |
parent->sum_tokens_size = size; |
| 933 |
parent->sum_tokens = xnrealloc (summary->sum_tokens, size, |
| 934 |
sizeof *summary->sum_tokens); |
| 935 |
} |
| 936 |
summary->sum_tokens = 0; |
| 937 |
} |
| 938 |
if (parent->sum_free_index == 8) |
| 939 |
parent = make_sibling_summary (parent); |
| 940 |
summary = xcalloc (1, sizeof(struct summary)); |
| 941 |
summary->sum_level = parent->sum_level - 1; |
| 942 |
parent->sum_kids[parent->sum_free_index++] = summary; |
| 943 |
summary->sum_parent = parent; |
| 944 |
size = INIT_TOKENS_SIZE (summary->sum_level); |
| 945 |
summary->sum_tokens_size = size; |
| 946 |
summary->sum_tokens = xmalloc (sizeof(struct token *) * size); |
| 947 |
return summary; |
| 948 |
} |
| 949 |
|
| 950 |
static int _GL_ATTRIBUTE_PURE |
| 951 |
count_vec_size (struct summary *summary, unsigned char const *tail_hits) |
| 952 |
{ |
| 953 |
struct summary **kids; |
| 954 |
unsigned int hits = (summary->sum_hits ? *summary->sum_hits : *tail_hits); |
| 955 |
|
| 956 |
kids = summary->sum_kids; |
| 957 |
if (*kids == NULL) |
| 958 |
{ |
| 959 |
static char bits_per_nybble[] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 }; |
| 960 |
return bits_per_nybble[hits & 0xf] + bits_per_nybble[hits >> 4]; |
| 961 |
} |
| 962 |
else |
| 963 |
{ |
| 964 |
int bit; |
| 965 |
int count = 0; |
| 966 |
--tail_hits; |
| 967 |
for (bit = 1; bit & 0xff; bit <<= 1, ++kids) |
| 968 |
if (bit & hits) |
| 969 |
count += count_vec_size (*kids, tail_hits); |
| 970 |
return count; |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
static int _GL_ATTRIBUTE_PURE |
| 975 |
count_buf_size (struct summary *summary, unsigned char const *tail_hits) |
| 976 |
{ |
| 977 |
struct summary **kids; |
| 978 |
unsigned int hits = (summary->sum_hits ? *summary->sum_hits : *tail_hits); |
| 979 |
|
| 980 |
kids = summary->sum_kids; |
| 981 |
if (*kids == NULL) |
| 982 |
return 1; |
| 983 |
else |
| 984 |
{ |
| 985 |
int bit; |
| 986 |
int count = 1; |
| 987 |
--tail_hits; |
| 988 |
for (bit = 1; bit & 0xff; bit <<= 1, ++kids) |
| 989 |
if (bit & hits) |
| 990 |
count += count_buf_size (*kids, tail_hits); |
| 991 |
return count; |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
static int |
| 998 |
check_hits (struct summary* summary) |
| 999 |
{ |
| 1000 |
struct summary **kids = summary->sum_kids; |
| 1001 |
struct summary **end = &kids[8]; |
| 1002 |
|
| 1003 |
if ( ! (summary->sum_hits == NULL || *summary->sum_hits == 0)) |
| 1004 |
return 1; |
| 1005 |
|
| 1006 |
if (end[-1] == 0) |
| 1007 |
while (*--end == 0) |
| 1008 |
; |
| 1009 |
while (kids < end) |
| 1010 |
if (check_hits (*kids++)) |
| 1011 |
return 1; |
| 1012 |
|
| 1013 |
return 0; |
| 1014 |
} |
| 1015 |
|
| 1016 |
static void |
| 1017 |
write_hits (FILE *fp, struct summary *summary, unsigned char const *tail_hits) |
| 1018 |
{ |
| 1019 |
struct summary **kids; |
| 1020 |
unsigned int hits = (summary->sum_hits ? *summary->sum_hits++ : *tail_hits); |
| 1021 |
|
| 1022 |
assert (hits); |
| 1023 |
putc (hits, fp); |
| 1024 |
|
| 1025 |
kids = summary->sum_kids; |
| 1026 |
if (*kids) |
| 1027 |
{ |
| 1028 |
int bit; |
| 1029 |
--tail_hits; |
| 1030 |
for (bit = 1; (bit & 0xff) && *kids; bit <<= 1, ++kids) |
| 1031 |
if (bit & hits) |
| 1032 |
write_hits (fp, *kids, tail_hits); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
static void |
| 1037 |
sign_token (struct token *token) |
| 1038 |
{ |
| 1039 |
unsigned char *tok_hits = TOKEN_HITS (token); |
| 1040 |
unsigned char *hits_sig = current_hits_signature; |
| 1041 |
unsigned char *end = current_hits_signature + log_8_member_files; |
| 1042 |
struct summary *summary = summary_leaf; |
| 1043 |
|
| 1044 |
while (summary) |
| 1045 |
{ |
| 1046 |
if (*tok_hits == 0) |
| 1047 |
add_token_to_summary (summary, token); |
| 1048 |
if (*tok_hits & *hits_sig) |
| 1049 |
break; |
| 1050 |
*tok_hits |= *hits_sig; |
| 1051 |
summary = summary->sum_parent; |
| 1052 |
tok_hits++; |
| 1053 |
hits_sig++; |
| 1054 |
} |
| 1055 |
while (hits_sig < end) |
| 1056 |
{ |
| 1057 |
if (*tok_hits & *hits_sig) |
| 1058 |
break; |
| 1059 |
*tok_hits |= *hits_sig; |
| 1060 |
tok_hits++; |
| 1061 |
hits_sig++; |
| 1062 |
} |
| 1063 |
} |
| 1064 |
|
| 1065 |
static void |
| 1066 |
add_token_to_summary (struct summary *summary, struct token *token) |
| 1067 |
{ |
| 1068 |
size_t size = summary->sum_tokens_size; |
| 1069 |
|
| 1070 |
if (summary->sum_hits_count >= size) |
| 1071 |
{ |
| 1072 |
summary->sum_tokens = x2nrealloc (summary->sum_tokens, &size, |
| 1073 |
sizeof *summary->sum_tokens); |
| 1074 |
summary->sum_tokens_size = size; |
| 1075 |
} |
| 1076 |
summary->sum_tokens[summary->sum_hits_count++] = token; |
| 1077 |
} |