Print this page
11506 smatch resync
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch_flow.c
+++ new/usr/src/tools/smatch/src/smatch_flow.c
1 1 /*
2 2 * Copyright (C) 2006,2008 Dan Carpenter.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 #define _GNU_SOURCE 1
19 19 #include <unistd.h>
20 20 #include <stdio.h>
21 21 #include "token.h"
22 22 #include "scope.h"
23 23 #include "smatch.h"
24 24 #include "smatch_expression_stacks.h"
25 25 #include "smatch_extra.h"
26 26 #include "smatch_slist.h"
27 27
28 28 int __in_fake_assign;
29 29 int __in_fake_struct_assign;
30 30 int in_fake_env;
31 31 int final_pass;
32 32 int __inline_call;
33 33 struct expression *__inline_fn;
34 34
35 35 static int __smatch_lineno = 0;
36 36
37 37 static char *base_file;
38 38 static const char *filename;
39 39 static char *pathname;
40 40 static char *full_filename;
41 41 static char *full_base_file;
42 42 static char *cur_func;
43 43 static unsigned int loop_count;
44 44 static int last_goto_statement_handled;
45 45 int __expr_stmt_count;
46 46 int __in_function_def;
47 47 static struct expression_list *switch_expr_stack = NULL;
48 48 static struct expression_list *post_op_stack = NULL;
49 49
50 50 static struct ptr_list *backup;
51 51
52 52 struct expression_list *big_expression_stack;
53 53 struct statement_list *big_statement_stack;
54 54 struct statement *__prev_stmt;
55 55 struct statement *__cur_stmt;
56 56 struct statement *__next_stmt;
57 57 int __in_pre_condition = 0;
58 58 int __bail_on_rest_of_function = 0;
59 59 static struct timeval fn_start_time;
60 60 static struct timeval outer_fn_start_time;
61 61 char *get_function(void) { return cur_func; }
62 62 int get_lineno(void) { return __smatch_lineno; }
63 63 int inside_loop(void) { return !!loop_count; }
64 64 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
65 65 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
66 66 int in_expression_statement(void) { return !!__expr_stmt_count; }
67 67
68 68 static void split_symlist(struct symbol_list *sym_list);
↓ open down ↓ |
68 lines elided |
↑ open up ↑ |
69 69 static void split_declaration(struct symbol_list *sym_list);
70 70 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
71 71 static void add_inline_function(struct symbol *sym);
72 72 static void parse_inline(struct expression *expr);
73 73
74 74 int option_assume_loops = 0;
75 75 int option_two_passes = 0;
76 76 struct symbol *cur_func_sym = NULL;
77 77 struct stree *global_states;
78 78
79 -long long valid_ptr_min = 4096;
80 -long long valid_ptr_max = 2117777777;
81 -sval_t valid_ptr_min_sval = {
79 +const unsigned long valid_ptr_min = 4096;
80 +unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
81 +const sval_t valid_ptr_min_sval = {
82 82 .type = &ptr_ctype,
83 83 {.value = 4096},
84 84 };
85 85 sval_t valid_ptr_max_sval = {
86 86 .type = &ptr_ctype,
87 - {.value = LONG_MAX - 100000},
87 + {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
88 88 };
89 89 struct range_list *valid_ptr_rl;
90 90
91 -static void set_valid_ptr_max(void)
91 +void alloc_valid_ptr_rl(void)
92 92 {
93 - if (type_bits(&ptr_ctype) == 32)
94 - valid_ptr_max = 2117777777;
95 - else if (type_bits(&ptr_ctype) == 64)
96 - valid_ptr_max = 2117777777777777777LL;
97 -
93 + valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
98 94 valid_ptr_max_sval.value = valid_ptr_max;
99 -}
100 95
101 -static void alloc_valid_ptr_rl(void)
102 -{
103 96 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
104 97 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
105 98 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
106 99 }
107 100
108 101 int outside_of_function(void)
109 102 {
110 103 return cur_func_sym == NULL;
111 104 }
112 105
113 106 const char *get_filename(void)
114 107 {
115 108 if (option_info && option_full_path)
116 109 return full_base_file;
117 110 if (option_info)
118 111 return base_file;
119 112 if (option_full_path)
120 113 return full_filename;
121 114 return filename;
122 115 }
123 116
124 117 const char *get_base_file(void)
125 118 {
126 119 if (option_full_path)
127 120 return full_base_file;
128 121 return base_file;
129 122 }
130 123
131 124 static void set_position(struct position pos)
132 125 {
133 126 int len;
134 127 static int prev_stream = -1;
135 128
136 129 if (in_fake_env)
137 130 return;
138 131
139 132 if (pos.stream == 0 && pos.line == 0)
140 133 return;
141 134
142 135 __smatch_lineno = pos.line;
143 136
144 137 if (pos.stream == prev_stream)
145 138 return;
146 139
147 140 filename = stream_name(pos.stream);
148 141
149 142 free(full_filename);
150 143 pathname = getcwd(NULL, 0);
151 144 if (pathname) {
152 145 len = strlen(pathname) + 1 + strlen(filename) + 1;
153 146 full_filename = malloc(len);
154 147 snprintf(full_filename, len, "%s/%s", pathname, filename);
155 148 } else {
156 149 full_filename = alloc_string(filename);
157 150 }
158 151 free(pathname);
159 152 }
160 153
161 154 int is_assigned_call(struct expression *expr)
162 155 {
163 156 struct expression *parent = expr_get_parent_expr(expr);
164 157
165 158 if (parent &&
166 159 parent->type == EXPR_ASSIGNMENT &&
167 160 parent->op == '=' &&
168 161 strip_expr(parent->right) == expr)
169 162 return 1;
170 163
171 164 return 0;
172 165 }
173 166
174 167 static int is_inline_func(struct expression *expr)
175 168 {
176 169 if (expr->type != EXPR_SYMBOL || !expr->symbol)
177 170 return 0;
178 171 if (expr->symbol->ctype.modifiers & MOD_INLINE)
179 172 return 1;
180 173 return 0;
181 174 }
182 175
183 176 static int is_noreturn_func(struct expression *expr)
184 177 {
185 178 if (expr->type != EXPR_SYMBOL || !expr->symbol)
186 179 return 0;
187 180 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
188 181 return 1;
189 182 return 0;
190 183 }
191 184
192 185 static int inline_budget = 20;
193 186
194 187 int inlinable(struct expression *expr)
195 188 {
196 189 struct symbol *sym;
197 190 struct statement *last_stmt = NULL;
198 191
199 192 if (__inline_fn) /* don't nest */
200 193 return 0;
201 194
202 195 if (expr->type != EXPR_SYMBOL || !expr->symbol)
203 196 return 0;
204 197 if (is_no_inline_function(expr->symbol->ident->name))
205 198 return 0;
206 199 sym = get_base_type(expr->symbol);
207 200 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
208 201 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
209 202 return 0;
210 203 if (sym->stmt->type != STMT_COMPOUND)
211 204 return 0;
212 205 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
213 206 }
214 207 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
215 208 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
216 209 return 0;
217 210 if (sym->inline_stmt->type != STMT_COMPOUND)
218 211 return 0;
219 212 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
220 213 }
221 214
222 215 if (!last_stmt)
223 216 return 0;
224 217
225 218 /* the magic numbers in this function are pulled out of my bum. */
226 219 if (last_stmt->pos.line > sym->pos.line + inline_budget)
227 220 return 0;
228 221
229 222 return 1;
230 223 }
231 224
232 225 void __process_post_op_stack(void)
233 226 {
234 227 struct expression *expr;
235 228
236 229 FOR_EACH_PTR(post_op_stack, expr) {
237 230 __pass_to_client(expr, OP_HOOK);
238 231 } END_FOR_EACH_PTR(expr);
239 232
240 233 __free_ptr_list((struct ptr_list **)&post_op_stack);
241 234 }
242 235
243 236 static int handle_comma_assigns(struct expression *expr)
244 237 {
245 238 struct expression *right;
246 239 struct expression *assign;
247 240
248 241 right = strip_expr(expr->right);
249 242 if (right->type != EXPR_COMMA)
250 243 return 0;
251 244
252 245 __split_expr(right->left);
253 246 __process_post_op_stack();
254 247
255 248 assign = assign_expression(expr->left, '=', right->right);
256 249 __split_expr(assign);
257 250
258 251 return 1;
259 252 }
260 253
261 254 /* This is to handle *p++ = foo; assignments */
262 255 static int handle_postop_assigns(struct expression *expr)
263 256 {
264 257 struct expression *left, *fake_left;
265 258 struct expression *assign;
266 259
267 260 left = strip_expr(expr->left);
268 261 if (left->type != EXPR_PREOP || left->op != '*')
269 262 return 0;
270 263 left = strip_expr(left->unop);
271 264 if (left->type != EXPR_POSTOP)
272 265 return 0;
273 266
274 267 fake_left = deref_expression(strip_expr(left->unop));
275 268 assign = assign_expression(fake_left, '=', expr->right);
276 269
277 270 __split_expr(assign);
278 271 __split_expr(expr->left);
279 272
280 273 return 1;
281 274 }
282 275
283 276 static int prev_expression_is_getting_address(struct expression *expr)
284 277 {
285 278 struct expression *parent;
286 279
287 280 do {
288 281 parent = expr_get_parent_expr(expr);
289 282
290 283 if (!parent)
291 284 return 0;
292 285 if (parent->type == EXPR_PREOP && parent->op == '&')
293 286 return 1;
294 287 if (parent->type == EXPR_PREOP && parent->op == '(')
295 288 goto next;
296 289 if (parent->type == EXPR_DEREF && parent->op == '.')
297 290 goto next;
298 291
299 292 return 0;
300 293 next:
301 294 expr = parent;
302 295 } while (1);
303 296 }
304 297
305 298 static void handle_builtin_overflow_func(struct expression *expr)
306 299 {
307 300 struct expression *a, *b, *res, *assign;
308 301 int op;
309 302
310 303 if (sym_name_is("__builtin_add_overflow", expr->fn))
311 304 op = '+';
312 305 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
313 306 op = '-';
314 307 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
315 308 op = '*';
316 309 else
317 310 return;
318 311
319 312 a = get_argument_from_call_expr(expr->args, 0);
320 313 b = get_argument_from_call_expr(expr->args, 1);
321 314 res = get_argument_from_call_expr(expr->args, 2);
322 315
323 316 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
324 317 __split_expr(assign);
325 318 }
326 319
327 320 static int handle__builtin_choose_expr(struct expression *expr)
328 321 {
329 322 struct expression *const_expr, *expr1, *expr2;
330 323 sval_t sval;
331 324
332 325 if (!sym_name_is("__builtin_choose_expr", expr->fn))
333 326 return 0;
334 327
335 328 const_expr = get_argument_from_call_expr(expr->args, 0);
336 329 expr1 = get_argument_from_call_expr(expr->args, 1);
337 330 expr2 = get_argument_from_call_expr(expr->args, 2);
338 331
339 332 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
340 333 return 0;
341 334 if (sval.value)
342 335 __split_expr(expr1);
343 336 else
344 337 __split_expr(expr2);
345 338 return 1;
346 339 }
347 340
348 341 static int handle__builtin_choose_expr_assigns(struct expression *expr)
349 342 {
350 343 struct expression *const_expr, *right, *expr1, *expr2, *fake;
351 344 sval_t sval;
352 345
353 346 right = strip_expr(expr->right);
354 347 if (right->type != EXPR_CALL)
355 348 return 0;
356 349 if (!sym_name_is("__builtin_choose_expr", right->fn))
357 350 return 0;
358 351
359 352 const_expr = get_argument_from_call_expr(right->args, 0);
360 353 expr1 = get_argument_from_call_expr(right->args, 1);
361 354 expr2 = get_argument_from_call_expr(right->args, 2);
362 355
363 356 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
364 357 return 0;
365 358
366 359 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
367 360 __split_expr(fake);
368 361 return 1;
369 362 }
370 363
371 364 void __split_expr(struct expression *expr)
372 365 {
373 366 if (!expr)
374 367 return;
375 368
376 369 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
377 370
378 371 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
379 372 return;
380 373 if (__in_fake_assign >= 4) /* don't allow too much nesting */
381 374 return;
382 375
383 376 push_expression(&big_expression_stack, expr);
384 377 set_position(expr->pos);
385 378 __pass_to_client(expr, EXPR_HOOK);
386 379
387 380 switch (expr->type) {
388 381 case EXPR_PREOP:
389 382 expr_set_parent_expr(expr->unop, expr);
390 383
391 384 if (expr->op == '*' &&
392 385 !prev_expression_is_getting_address(expr))
393 386 __pass_to_client(expr, DEREF_HOOK);
394 387 __split_expr(expr->unop);
395 388 __pass_to_client(expr, OP_HOOK);
396 389 break;
397 390 case EXPR_POSTOP:
398 391 expr_set_parent_expr(expr->unop, expr);
399 392
400 393 __split_expr(expr->unop);
401 394 push_expression(&post_op_stack, expr);
402 395 break;
403 396 case EXPR_STATEMENT:
404 397 __expr_stmt_count++;
405 398 if (expr->statement && !expr->statement) {
406 399 stmt_set_parent_stmt(expr->statement,
407 400 last_ptr_list((struct ptr_list *)big_statement_stack));
408 401 }
409 402 __split_stmt(expr->statement);
410 403 __expr_stmt_count--;
411 404 break;
412 405 case EXPR_LOGICAL:
413 406 case EXPR_COMPARE:
414 407 expr_set_parent_expr(expr->left, expr);
415 408 expr_set_parent_expr(expr->right, expr);
416 409
417 410 __pass_to_client(expr, LOGIC_HOOK);
418 411 __handle_logic(expr);
419 412 break;
420 413 case EXPR_BINOP:
421 414 expr_set_parent_expr(expr->left, expr);
422 415 expr_set_parent_expr(expr->right, expr);
423 416
424 417 __pass_to_client(expr, BINOP_HOOK);
425 418 case EXPR_COMMA:
426 419 expr_set_parent_expr(expr->left, expr);
427 420 expr_set_parent_expr(expr->right, expr);
428 421
429 422 __split_expr(expr->left);
430 423 __process_post_op_stack();
431 424 __split_expr(expr->right);
432 425 break;
433 426 case EXPR_ASSIGNMENT: {
434 427 struct expression *right;
435 428
436 429 expr_set_parent_expr(expr->left, expr);
437 430 expr_set_parent_expr(expr->right, expr);
438 431
439 432 right = strip_expr(expr->right);
440 433 if (!right)
441 434 break;
442 435
443 436 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
444 437
445 438 /* foo = !bar() */
446 439 if (__handle_condition_assigns(expr))
447 440 break;
448 441 /* foo = (x < 5 ? foo : 5); */
449 442 if (__handle_select_assigns(expr))
450 443 break;
451 444 /* foo = ({frob(); frob(); frob(); 1;}) */
452 445 if (__handle_expr_statement_assigns(expr))
453 446 break;
454 447 /* foo = (3, 4); */
455 448 if (handle_comma_assigns(expr))
456 449 break;
457 450 if (handle_postop_assigns(expr))
458 451 break;
459 452 if (handle__builtin_choose_expr_assigns(expr))
460 453 break;
461 454
462 455 __split_expr(expr->right);
463 456 if (outside_of_function())
464 457 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
465 458 else
466 459 __pass_to_client(expr, ASSIGNMENT_HOOK);
467 460
468 461 __fake_struct_member_assignments(expr);
469 462
470 463 if (expr->op == '=' && right->type == EXPR_CALL)
471 464 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
472 465
473 466 if (get_macro_name(right->pos) &&
474 467 get_macro_name(expr->pos) != get_macro_name(right->pos))
475 468 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
476 469
477 470 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
478 471
479 472 __split_expr(expr->left);
480 473 break;
481 474 }
482 475 case EXPR_DEREF:
483 476 expr_set_parent_expr(expr->deref, expr);
484 477
485 478 __pass_to_client(expr, DEREF_HOOK);
486 479 __split_expr(expr->deref);
487 480 break;
488 481 case EXPR_SLICE:
489 482 expr_set_parent_expr(expr->base, expr);
490 483
491 484 __split_expr(expr->base);
492 485 break;
493 486 case EXPR_CAST:
494 487 case EXPR_FORCE_CAST:
495 488 expr_set_parent_expr(expr->cast_expression, expr);
496 489
↓ open down ↓ |
384 lines elided |
↑ open up ↑ |
497 490 __pass_to_client(expr, CAST_HOOK);
498 491 __split_expr(expr->cast_expression);
499 492 break;
500 493 case EXPR_SIZEOF:
501 494 if (expr->cast_expression)
502 495 __pass_to_client(strip_parens(expr->cast_expression),
503 496 SIZEOF_HOOK);
504 497 break;
505 498 case EXPR_OFFSETOF:
506 499 case EXPR_ALIGNOF:
507 - evaluate_expression(expr);
508 500 break;
509 501 case EXPR_CONDITIONAL:
510 502 case EXPR_SELECT:
511 503 expr_set_parent_expr(expr->conditional, expr);
512 504 expr_set_parent_expr(expr->cond_true, expr);
513 505 expr_set_parent_expr(expr->cond_false, expr);
514 506
515 507 if (known_condition_true(expr->conditional)) {
516 508 __split_expr(expr->cond_true);
517 509 break;
518 510 }
519 511 if (known_condition_false(expr->conditional)) {
520 512 __split_expr(expr->cond_false);
521 513 break;
522 514 }
523 515 __pass_to_client(expr, SELECT_HOOK);
524 516 __split_whole_condition(expr->conditional);
525 517 __split_expr(expr->cond_true);
526 518 __push_true_states();
527 519 __use_false_states();
528 520 __split_expr(expr->cond_false);
529 521 __merge_true_states();
530 522 break;
531 523 case EXPR_CALL:
532 524 expr_set_parent_expr(expr->fn, expr);
533 525
534 526 if (sym_name_is("__builtin_constant_p", expr->fn))
535 527 break;
536 528 if (handle__builtin_choose_expr(expr))
537 529 break;
538 530 split_expr_list(expr->args, expr);
539 531 __split_expr(expr->fn);
540 532 if (is_inline_func(expr->fn))
541 533 add_inline_function(expr->fn->symbol);
542 534 if (inlinable(expr->fn))
543 535 __inline_call = 1;
544 536 __process_post_op_stack();
545 537 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
546 538 __pass_to_client(expr, FUNCTION_CALL_HOOK);
547 539 __inline_call = 0;
548 540 if (inlinable(expr->fn)) {
549 541 parse_inline(expr);
550 542 }
551 543 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
552 544 if (is_noreturn_func(expr->fn))
553 545 nullify_path();
554 546 handle_builtin_overflow_func(expr);
555 547 break;
556 548 case EXPR_INITIALIZER:
557 549 split_expr_list(expr->expr_list, expr);
558 550 break;
559 551 case EXPR_IDENTIFIER:
560 552 expr_set_parent_expr(expr->ident_expression, expr);
561 553 __split_expr(expr->ident_expression);
562 554 break;
563 555 case EXPR_INDEX:
564 556 expr_set_parent_expr(expr->idx_expression, expr);
565 557 __split_expr(expr->idx_expression);
566 558 break;
567 559 case EXPR_POS:
568 560 expr_set_parent_expr(expr->init_expr, expr);
569 561 __split_expr(expr->init_expr);
570 562 break;
571 563 case EXPR_SYMBOL:
572 564 __pass_to_client(expr, SYM_HOOK);
573 565 break;
574 566 case EXPR_STRING:
575 567 __pass_to_client(expr, STRING_HOOK);
576 568 break;
577 569 default:
578 570 break;
579 571 };
580 572 pop_expression(&big_expression_stack);
581 573 }
582 574
583 575 static int is_forever_loop(struct statement *stmt)
584 576 {
585 577 struct expression *expr;
586 578 sval_t sval;
587 579
588 580 expr = strip_expr(stmt->iterator_pre_condition);
589 581 if (!expr)
590 582 expr = stmt->iterator_post_condition;
591 583 if (!expr) {
592 584 /* this is a for(;;) loop... */
593 585 return 1;
594 586 }
595 587
596 588 if (get_value(expr, &sval) && sval.value != 0)
597 589 return 1;
598 590
599 591 return 0;
600 592 }
601 593
602 594 static int loop_num;
603 595 static char *get_loop_name(int num)
604 596 {
605 597 char buf[256];
606 598
607 599 snprintf(buf, 255, "-loop%d", num);
608 600 buf[255] = '\0';
609 601 return alloc_sname(buf);
610 602 }
611 603
612 604 /*
613 605 * Pre Loops are while and for loops.
614 606 */
615 607 static void handle_pre_loop(struct statement *stmt)
616 608 {
617 609 int once_through; /* we go through the loop at least once */
618 610 struct sm_state *extra_sm = NULL;
619 611 int unchanged = 0;
620 612 char *loop_name;
621 613 struct stree *stree = NULL;
622 614 struct sm_state *sm = NULL;
623 615
624 616 loop_name = get_loop_name(loop_num);
625 617 loop_num++;
626 618
627 619 __split_stmt(stmt->iterator_pre_statement);
628 620 __prev_stmt = stmt->iterator_pre_statement;
629 621
630 622 once_through = implied_condition_true(stmt->iterator_pre_condition);
631 623
632 624 loop_count++;
633 625 __push_continues();
634 626 __push_breaks();
635 627
636 628 __merge_gotos(loop_name, NULL);
637 629
638 630 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
639 631 __in_pre_condition++;
640 632 __pass_to_client(stmt, PRELOOP_HOOK);
641 633 __split_whole_condition(stmt->iterator_pre_condition);
642 634 __in_pre_condition--;
643 635 FOR_EACH_SM(stree, sm) {
644 636 set_state(sm->owner, sm->name, sm->sym, sm->state);
645 637 } END_FOR_EACH_SM(sm);
646 638 free_stree(&stree);
647 639 if (extra_sm)
648 640 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
649 641
650 642 if (option_assume_loops)
651 643 once_through = 1;
652 644
653 645 __split_stmt(stmt->iterator_statement);
654 646 if (is_forever_loop(stmt)) {
655 647 __merge_continues();
656 648 __save_gotos(loop_name, NULL);
657 649
658 650 __push_fake_cur_stree();
659 651 __split_stmt(stmt->iterator_post_statement);
660 652 stree = __pop_fake_cur_stree();
661 653
662 654 __discard_false_states();
663 655 __use_breaks();
664 656
665 657 if (!__path_is_null())
666 658 __merge_stree_into_cur(stree);
667 659 free_stree(&stree);
668 660 } else {
669 661 __merge_continues();
670 662 unchanged = __iterator_unchanged(extra_sm);
671 663 __split_stmt(stmt->iterator_post_statement);
672 664 __prev_stmt = stmt->iterator_post_statement;
673 665 __cur_stmt = stmt;
674 666
675 667 __save_gotos(loop_name, NULL);
676 668 __in_pre_condition++;
677 669 __split_whole_condition(stmt->iterator_pre_condition);
678 670 __in_pre_condition--;
679 671 nullify_path();
680 672 __merge_false_states();
681 673 if (once_through)
682 674 __discard_false_states();
683 675 else
684 676 __merge_false_states();
685 677
686 678 if (extra_sm && unchanged)
687 679 __extra_pre_loop_hook_after(extra_sm,
688 680 stmt->iterator_post_statement,
689 681 stmt->iterator_pre_condition);
690 682 __merge_breaks();
691 683 }
692 684 loop_count--;
693 685 }
694 686
695 687 /*
696 688 * Post loops are do {} while();
697 689 */
698 690 static void handle_post_loop(struct statement *stmt)
699 691 {
700 692 char *loop_name;
701 693
702 694 loop_name = get_loop_name(loop_num);
703 695 loop_num++;
704 696 loop_count++;
705 697
706 698 __push_continues();
707 699 __push_breaks();
708 700 __merge_gotos(loop_name, NULL);
709 701 __split_stmt(stmt->iterator_statement);
710 702 __merge_continues();
711 703 if (!is_zero(stmt->iterator_post_condition))
712 704 __save_gotos(loop_name, NULL);
713 705
714 706 if (is_forever_loop(stmt)) {
715 707 __use_breaks();
716 708 } else {
717 709 __split_whole_condition(stmt->iterator_post_condition);
718 710 __use_false_states();
719 711 __merge_breaks();
720 712 }
721 713 loop_count--;
722 714 }
723 715
724 716 static int empty_statement(struct statement *stmt)
725 717 {
726 718 if (!stmt)
727 719 return 0;
728 720 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
729 721 return 1;
730 722 return 0;
731 723 }
732 724
733 725 static int last_stmt_on_same_line(void)
734 726 {
735 727 struct statement *stmt;
736 728 int i = 0;
737 729
738 730 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
739 731 if (!i++)
740 732 continue;
741 733 if (stmt->pos.line == get_lineno())
742 734 return 1;
743 735 return 0;
744 736 } END_FOR_EACH_PTR_REVERSE(stmt);
745 737 return 0;
746 738 }
747 739
748 740 static void split_asm_constraints(struct expression_list *expr_list)
749 741 {
750 742 struct expression *expr;
751 743 int state = 0;
752 744
753 745 FOR_EACH_PTR(expr_list, expr) {
754 746 switch (state) {
755 747 case 0: /* identifier */
756 748 case 1: /* constraint */
757 749 state++;
758 750 continue;
759 751 case 2: /* expression */
760 752 state = 0;
761 753 __split_expr(expr);
762 754 continue;
763 755 }
764 756 } END_FOR_EACH_PTR(expr);
765 757 }
766 758
767 759 static int is_case_val(struct statement *stmt, sval_t sval)
768 760 {
769 761 sval_t case_sval;
770 762
771 763 if (stmt->type != STMT_CASE)
772 764 return 0;
773 765 if (!stmt->case_expression) {
774 766 __set_default();
775 767 return 1;
776 768 }
777 769 if (!get_value(stmt->case_expression, &case_sval))
778 770 return 0;
779 771 if (case_sval.value == sval.value)
780 772 return 1;
781 773 return 0;
782 774 }
783 775
784 776 static struct range_list *get_case_rl(struct expression *switch_expr,
785 777 struct expression *case_expr,
786 778 struct expression *case_to)
787 779 {
788 780 sval_t start, end;
789 781 struct range_list *rl = NULL;
790 782 struct symbol *switch_type;
791 783
792 784 switch_type = get_type(switch_expr);
793 785 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
794 786 start = sval_cast(switch_type, start);
795 787 end = sval_cast(switch_type, end);
796 788 add_range(&rl, start, end);
797 789 } else if (get_value(case_expr, &start)) {
798 790 start = sval_cast(switch_type, start);
799 791 add_range(&rl, start, start);
800 792 }
801 793
802 794 return rl;
803 795 }
804 796
805 797 static void split_known_switch(struct statement *stmt, sval_t sval)
806 798 {
807 799 struct statement *tmp;
808 800 struct range_list *rl;
809 801
810 802 __split_expr(stmt->switch_expression);
811 803 sval = sval_cast(get_type(stmt->switch_expression), sval);
812 804
813 805 push_expression(&switch_expr_stack, stmt->switch_expression);
814 806 __save_switch_states(top_expression(switch_expr_stack));
815 807 nullify_path();
816 808 __push_default();
817 809 __push_breaks();
818 810
819 811 stmt = stmt->switch_statement;
820 812
821 813 __push_scope_hooks();
822 814 FOR_EACH_PTR(stmt->stmts, tmp) {
823 815 __smatch_lineno = tmp->pos.line;
824 816 if (is_case_val(tmp, sval)) {
825 817 rl = alloc_rl(sval, sval);
826 818 __merge_switches(top_expression(switch_expr_stack), rl);
827 819 __pass_case_to_client(top_expression(switch_expr_stack), rl);
828 820 }
829 821 if (__path_is_null())
830 822 continue;
831 823 __split_stmt(tmp);
832 824 if (__path_is_null()) {
833 825 __set_default();
834 826 goto out;
835 827 }
836 828 } END_FOR_EACH_PTR(tmp);
837 829 out:
838 830 __call_scope_hooks();
839 831 if (!__pop_default())
840 832 __merge_switches(top_expression(switch_expr_stack), NULL);
841 833 __discard_switches();
842 834 __merge_breaks();
843 835 pop_expression(&switch_expr_stack);
844 836 }
845 837
846 838 static void split_case(struct statement *stmt)
847 839 {
848 840 struct range_list *rl = NULL;
849 841
850 842 expr_set_parent_stmt(stmt->case_expression, stmt);
851 843 expr_set_parent_stmt(stmt->case_to, stmt);
852 844
853 845 rl = get_case_rl(top_expression(switch_expr_stack),
854 846 stmt->case_expression, stmt->case_to);
855 847 while (stmt->case_statement->type == STMT_CASE) {
856 848 struct range_list *tmp;
857 849
858 850 tmp = get_case_rl(top_expression(switch_expr_stack),
859 851 stmt->case_statement->case_expression,
860 852 stmt->case_statement->case_to);
861 853 if (!tmp)
862 854 break;
863 855 rl = rl_union(rl, tmp);
864 856 if (!stmt->case_expression)
865 857 __set_default();
866 858 stmt = stmt->case_statement;
867 859 }
868 860
869 861 __merge_switches(top_expression(switch_expr_stack), rl);
870 862
↓ open down ↓ |
353 lines elided |
↑ open up ↑ |
871 863 if (!stmt->case_expression)
872 864 __set_default();
873 865 __split_stmt(stmt->case_statement);
874 866 }
875 867
876 868 int time_parsing_function(void)
877 869 {
878 870 return ms_since(&fn_start_time) / 1000;
879 871 }
880 872
881 -static int taking_too_long(void)
873 +/*
874 + * This defaults to 60 * 5 == 5 minutes, so we'll just multiply
875 + * whatever we're given by 5.
876 + */
877 +bool taking_too_long(void)
882 878 {
883 - if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
879 + if (option_timeout &&
880 + (ms_since(&outer_fn_start_time) / 1000) > option_timeout * 5)
884 881 return 1;
885 882 return 0;
886 883 }
887 884
888 885 static int is_last_stmt(struct statement *cur_stmt)
889 886 {
890 887 struct symbol *fn;
891 888 struct statement *stmt;
892 889
893 890 if (!cur_func_sym)
894 891 return 0;
895 892 fn = get_base_type(cur_func_sym);
896 893 if (!fn)
897 894 return 0;
898 895 stmt = fn->stmt;
899 896 if (!stmt)
900 897 stmt = fn->inline_stmt;
901 898 if (!stmt || stmt->type != STMT_COMPOUND)
902 899 return 0;
903 900 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
904 901 if (stmt && stmt->type == STMT_LABEL)
905 902 stmt = stmt->label_statement;
906 903 if (stmt == cur_stmt)
907 904 return 1;
908 905 return 0;
909 906 }
910 907
911 908 static void handle_backward_goto(struct statement *goto_stmt)
912 909 {
913 910 const char *goto_name, *label_name;
914 911 struct statement *func_stmt;
915 912 struct symbol *base_type = get_base_type(cur_func_sym);
916 913 struct statement *tmp;
917 914 int found = 0;
918 915
919 916 if (!option_info)
920 917 return;
921 918 if (last_goto_statement_handled)
922 919 return;
923 920 last_goto_statement_handled = 1;
924 921
925 922 if (!goto_stmt->goto_label ||
926 923 goto_stmt->goto_label->type != SYM_LABEL ||
927 924 !goto_stmt->goto_label->ident)
928 925 return;
929 926 goto_name = goto_stmt->goto_label->ident->name;
930 927
931 928 func_stmt = base_type->stmt;
932 929 if (!func_stmt)
933 930 func_stmt = base_type->inline_stmt;
934 931 if (!func_stmt)
935 932 return;
936 933 if (func_stmt->type != STMT_COMPOUND)
937 934 return;
938 935
939 936 FOR_EACH_PTR(func_stmt->stmts, tmp) {
940 937 if (!found) {
941 938 if (tmp->type != STMT_LABEL)
942 939 continue;
943 940 if (!tmp->label_identifier ||
944 941 tmp->label_identifier->type != SYM_LABEL ||
945 942 !tmp->label_identifier->ident)
946 943 continue;
947 944 label_name = tmp->label_identifier->ident->name;
948 945 if (strcmp(goto_name, label_name) != 0)
949 946 continue;
950 947 found = 1;
951 948 }
952 949 __split_stmt(tmp);
953 950 } END_FOR_EACH_PTR(tmp);
954 951 }
955 952
956 953 static void fake_a_return(void)
957 954 {
958 955 struct symbol *return_type;
959 956
960 957 nullify_path();
961 958 __unnullify_path();
962 959
963 960 return_type = get_real_base_type(cur_func_sym);
964 961 return_type = get_real_base_type(return_type);
965 962 if (return_type != &void_ctype) {
966 963 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
967 964 nullify_path();
968 965 }
969 966 }
970 967
971 968 static void fake_an_empty_default(struct position pos)
972 969 {
973 970 static struct statement none = {};
974 971
975 972 none.pos = pos;
976 973 none.type = STMT_NONE;
977 974 __merge_switches(top_expression(switch_expr_stack), NULL);
978 975 __split_stmt(&none);
979 976 }
980 977
981 978 static void split_compound(struct statement *stmt)
982 979 {
983 980 struct statement *prev = NULL;
984 981 struct statement *cur = NULL;
985 982 struct statement *next;
986 983
987 984 __push_scope_hooks();
988 985
989 986 FOR_EACH_PTR(stmt->stmts, next) {
990 987 /* just set them all ahead of time */
991 988 stmt_set_parent_stmt(next, stmt);
992 989
993 990 if (cur) {
994 991 __prev_stmt = prev;
995 992 __next_stmt = next;
996 993 __cur_stmt = cur;
997 994 __split_stmt(cur);
998 995 }
999 996 prev = cur;
1000 997 cur = next;
1001 998 } END_FOR_EACH_PTR(next);
1002 999 if (cur) {
1003 1000 __prev_stmt = prev;
1004 1001 __cur_stmt = cur;
1005 1002 __next_stmt = NULL;
1006 1003 __split_stmt(cur);
1007 1004 }
1008 1005
1009 1006 /*
1010 1007 * For function scope, then delay calling the scope hooks until the
1011 1008 * end of function hooks can run. I'm not positive this is the right
1012 1009 * thing...
1013 1010 */
1014 1011 if (!is_last_stmt(cur))
1015 1012 __call_scope_hooks();
1016 1013 }
1017 1014
1018 1015 /*
1019 1016 * This is a hack, work around for detecting empty functions.
1020 1017 */
1021 1018 static int need_delayed_scope_hooks(void)
1022 1019 {
1023 1020 struct symbol *fn = get_base_type(cur_func_sym);
1024 1021 struct statement *stmt;
1025 1022
1026 1023 if (!fn)
1027 1024 return 0;
1028 1025 stmt = fn->stmt;
1029 1026 if (!stmt)
1030 1027 stmt = fn->inline_stmt;
1031 1028 if (stmt && stmt->type == STMT_COMPOUND)
1032 1029 return 1;
1033 1030 return 0;
1034 1031 }
1035 1032
1036 1033 void __split_label_stmt(struct statement *stmt)
1037 1034 {
1038 1035 if (stmt->label_identifier &&
1039 1036 stmt->label_identifier->type == SYM_LABEL &&
1040 1037 stmt->label_identifier->ident) {
1041 1038 loop_count |= 0x0800000;
1042 1039 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1043 1040 }
1044 1041 }
1045 1042
1046 1043 static void find_asm_gotos(struct statement *stmt)
1047 1044 {
1048 1045 struct symbol *sym;
1049 1046
1050 1047 FOR_EACH_PTR(stmt->asm_labels, sym) {
1051 1048 __save_gotos(sym->ident->name, sym);
1052 1049 } END_FOR_EACH_PTR(sym);
1053 1050 }
1054 1051
1055 1052 void __split_stmt(struct statement *stmt)
1056 1053 {
1057 1054 sval_t sval;
1058 1055
1059 1056 if (!stmt)
1060 1057 goto out;
1061 1058
1062 1059 if (!__in_fake_assign)
1063 1060 __silence_warnings_for_stmt = false;
1064 1061
1065 1062 if (__bail_on_rest_of_function || is_skipped_function())
1066 1063 return;
1067 1064
1068 1065 if (out_of_memory() || taking_too_long()) {
1069 1066 struct timeval stop;
1070 1067
1071 1068 gettimeofday(&stop, NULL);
1072 1069
1073 1070 __bail_on_rest_of_function = 1;
1074 1071 final_pass = 1;
1075 1072 sm_perror("Function too hairy. Giving up. %lu seconds",
1076 1073 stop.tv_sec - fn_start_time.tv_sec);
1077 1074 fake_a_return();
1078 1075 final_pass = 0; /* turn off sm_msg() from here */
1079 1076 return;
1080 1077 }
1081 1078
1082 1079 add_ptr_list(&big_statement_stack, stmt);
1083 1080 free_expression_stack(&big_expression_stack);
1084 1081 set_position(stmt->pos);
1085 1082 __pass_to_client(stmt, STMT_HOOK);
1086 1083
1087 1084 switch (stmt->type) {
1088 1085 case STMT_DECLARATION:
1089 1086 split_declaration(stmt->declaration);
1090 1087 break;
1091 1088 case STMT_RETURN:
1092 1089 expr_set_parent_stmt(stmt->ret_value, stmt);
1093 1090
1094 1091 __split_expr(stmt->ret_value);
1095 1092 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1096 1093 __process_post_op_stack();
1097 1094 nullify_path();
1098 1095 break;
1099 1096 case STMT_EXPRESSION:
1100 1097 expr_set_parent_stmt(stmt->expression, stmt);
1101 1098 expr_set_parent_stmt(stmt->context, stmt);
1102 1099
1103 1100 __split_expr(stmt->expression);
1104 1101 break;
1105 1102 case STMT_COMPOUND:
1106 1103 split_compound(stmt);
1107 1104 break;
1108 1105 case STMT_IF:
1109 1106 stmt_set_parent_stmt(stmt->if_true, stmt);
1110 1107 stmt_set_parent_stmt(stmt->if_false, stmt);
1111 1108 expr_set_parent_stmt(stmt->if_conditional, stmt);
1112 1109
1113 1110 if (known_condition_true(stmt->if_conditional)) {
1114 1111 __split_stmt(stmt->if_true);
1115 1112 break;
1116 1113 }
1117 1114 if (known_condition_false(stmt->if_conditional)) {
1118 1115 __split_stmt(stmt->if_false);
1119 1116 break;
1120 1117 }
1121 1118 __split_whole_condition(stmt->if_conditional);
1122 1119 __split_stmt(stmt->if_true);
1123 1120 if (empty_statement(stmt->if_true) &&
1124 1121 last_stmt_on_same_line() &&
1125 1122 !get_macro_name(stmt->if_true->pos))
1126 1123 sm_warning("if();");
1127 1124 __push_true_states();
1128 1125 __use_false_states();
1129 1126 __split_stmt(stmt->if_false);
1130 1127 __merge_true_states();
1131 1128 break;
1132 1129 case STMT_ITERATOR:
1133 1130 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1134 1131 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1135 1132 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1136 1133 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1137 1134 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1138 1135
1139 1136 if (stmt->iterator_pre_condition)
1140 1137 handle_pre_loop(stmt);
1141 1138 else if (stmt->iterator_post_condition)
1142 1139 handle_post_loop(stmt);
1143 1140 else {
1144 1141 // these are for(;;) type loops.
1145 1142 handle_pre_loop(stmt);
1146 1143 }
1147 1144 break;
1148 1145 case STMT_SWITCH:
1149 1146 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1150 1147 expr_set_parent_stmt(stmt->switch_expression, stmt);
1151 1148
1152 1149 if (get_value(stmt->switch_expression, &sval)) {
1153 1150 split_known_switch(stmt, sval);
1154 1151 break;
1155 1152 }
1156 1153 __split_expr(stmt->switch_expression);
1157 1154 push_expression(&switch_expr_stack, stmt->switch_expression);
1158 1155 __save_switch_states(top_expression(switch_expr_stack));
1159 1156 nullify_path();
1160 1157 __push_default();
1161 1158 __push_breaks();
1162 1159 __split_stmt(stmt->switch_statement);
1163 1160 if (!__pop_default() && have_remaining_cases())
1164 1161 fake_an_empty_default(stmt->pos);
1165 1162 __discard_switches();
1166 1163 __merge_breaks();
1167 1164 pop_expression(&switch_expr_stack);
1168 1165 break;
1169 1166 case STMT_CASE:
1170 1167 split_case(stmt);
1171 1168 break;
1172 1169 case STMT_LABEL:
1173 1170 __split_label_stmt(stmt);
1174 1171 __split_stmt(stmt->label_statement);
1175 1172 break;
1176 1173 case STMT_GOTO:
1177 1174 expr_set_parent_stmt(stmt->goto_expression, stmt);
1178 1175
1179 1176 __split_expr(stmt->goto_expression);
1180 1177 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1181 1178 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1182 1179 __process_breaks();
1183 1180 } else if (!strcmp(stmt->goto_label->ident->name,
1184 1181 "continue")) {
1185 1182 __process_continues();
1186 1183 }
1187 1184 } else if (stmt->goto_label &&
1188 1185 stmt->goto_label->type == SYM_LABEL &&
1189 1186 stmt->goto_label->ident) {
1190 1187 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1191 1188 }
1192 1189 nullify_path();
1193 1190 if (is_last_stmt(stmt))
1194 1191 handle_backward_goto(stmt);
1195 1192 break;
1196 1193 case STMT_NONE:
1197 1194 break;
1198 1195 case STMT_ASM:
1199 1196 expr_set_parent_stmt(stmt->asm_string, stmt);
1200 1197
1201 1198 find_asm_gotos(stmt);
1202 1199 __pass_to_client(stmt, ASM_HOOK);
1203 1200 __split_expr(stmt->asm_string);
1204 1201 split_asm_constraints(stmt->asm_outputs);
1205 1202 split_asm_constraints(stmt->asm_inputs);
1206 1203 split_asm_constraints(stmt->asm_clobbers);
1207 1204 break;
1208 1205 case STMT_CONTEXT:
1209 1206 break;
1210 1207 case STMT_RANGE:
1211 1208 __split_expr(stmt->range_expression);
1212 1209 __split_expr(stmt->range_low);
1213 1210 __split_expr(stmt->range_high);
1214 1211 break;
1215 1212 }
1216 1213 __pass_to_client(stmt, STMT_HOOK_AFTER);
1217 1214 out:
1218 1215 __process_post_op_stack();
1219 1216 }
1220 1217
1221 1218 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1222 1219 {
1223 1220 struct expression *expr;
1224 1221
1225 1222 FOR_EACH_PTR(expr_list, expr) {
1226 1223 expr_set_parent_expr(expr, parent);
1227 1224 __split_expr(expr);
1228 1225 __process_post_op_stack();
1229 1226 } END_FOR_EACH_PTR(expr);
1230 1227 }
1231 1228
1232 1229 static void split_sym(struct symbol *sym)
1233 1230 {
1234 1231 if (!sym)
1235 1232 return;
1236 1233 if (!(sym->namespace & NS_SYMBOL))
1237 1234 return;
1238 1235
1239 1236 __split_stmt(sym->stmt);
1240 1237 __split_expr(sym->array_size);
1241 1238 split_symlist(sym->arguments);
1242 1239 split_symlist(sym->symbol_list);
1243 1240 __split_stmt(sym->inline_stmt);
1244 1241 split_symlist(sym->inline_symbol_list);
1245 1242 }
1246 1243
1247 1244 static void split_symlist(struct symbol_list *sym_list)
1248 1245 {
1249 1246 struct symbol *sym;
1250 1247
1251 1248 FOR_EACH_PTR(sym_list, sym) {
1252 1249 split_sym(sym);
1253 1250 } END_FOR_EACH_PTR(sym);
1254 1251 }
1255 1252
1256 1253 typedef void (fake_cb)(struct expression *expr);
1257 1254
1258 1255 static int member_to_number(struct expression *expr, struct ident *member)
1259 1256 {
1260 1257 struct symbol *type, *tmp;
1261 1258 char *name;
1262 1259 int i;
1263 1260
1264 1261 if (!member)
1265 1262 return -1;
1266 1263 name = member->name;
1267 1264
1268 1265 type = get_type(expr);
1269 1266 if (!type || type->type != SYM_STRUCT)
1270 1267 return -1;
1271 1268
1272 1269 i = -1;
1273 1270 FOR_EACH_PTR(type->symbol_list, tmp) {
1274 1271 i++;
1275 1272 if (!tmp->ident)
1276 1273 continue;
1277 1274 if (strcmp(name, tmp->ident->name) == 0)
1278 1275 return i;
1279 1276 } END_FOR_EACH_PTR(tmp);
1280 1277 return -1;
1281 1278 }
1282 1279
1283 1280 static struct ident *number_to_member(struct expression *expr, int num)
1284 1281 {
1285 1282 struct symbol *type, *member;
1286 1283 int i = 0;
1287 1284
1288 1285 type = get_type(expr);
1289 1286 if (!type || type->type != SYM_STRUCT)
1290 1287 return NULL;
1291 1288
1292 1289 FOR_EACH_PTR(type->symbol_list, member) {
1293 1290 if (i == num)
1294 1291 return member->ident;
1295 1292 i++;
1296 1293 } END_FOR_EACH_PTR(member);
1297 1294 return NULL;
1298 1295 }
1299 1296
1300 1297 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1301 1298
1302 1299 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1303 1300 {
1304 1301 struct expression *edge_member, *assign;
1305 1302 struct symbol *base = get_real_base_type(member);
1306 1303 struct symbol *tmp;
1307 1304
1308 1305 if (member->ident)
1309 1306 expr = member_expression(expr, '.', member->ident);
1310 1307
1311 1308 FOR_EACH_PTR(base->symbol_list, tmp) {
1312 1309 struct symbol *type;
1313 1310
1314 1311 type = get_real_base_type(tmp);
1315 1312 if (!type)
1316 1313 continue;
1317 1314
1318 1315 edge_member = member_expression(expr, '.', tmp->ident);
1319 1316 if (get_extra_state(edge_member))
1320 1317 continue;
1321 1318
1322 1319 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1323 1320 set_inner_struct_members(expr, tmp);
1324 1321 continue;
1325 1322 }
1326 1323
1327 1324 if (!tmp->ident)
1328 1325 continue;
1329 1326
1330 1327 assign = assign_expression(edge_member, '=', zero_expr());
1331 1328 __split_expr(assign);
1332 1329 } END_FOR_EACH_PTR(tmp);
1333 1330
1334 1331
1335 1332 }
1336 1333
1337 1334 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1338 1335 {
1339 1336 struct symbol *tmp;
1340 1337 struct expression *member = NULL;
1341 1338 struct expression *assign;
1342 1339 int op = '*';
1343 1340
1344 1341 if (expr->type == EXPR_PREOP && expr->op == '&') {
1345 1342 expr = strip_expr(expr->unop);
1346 1343 op = '.';
1347 1344 }
1348 1345
1349 1346 FOR_EACH_PTR(type->symbol_list, tmp) {
1350 1347 type = get_real_base_type(tmp);
1351 1348 if (!type)
1352 1349 continue;
1353 1350
1354 1351 if (tmp->ident) {
1355 1352 member = member_expression(expr, op, tmp->ident);
1356 1353 if (get_extra_state(member))
1357 1354 continue;
1358 1355 }
1359 1356
1360 1357 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1361 1358 set_inner_struct_members(expr, tmp);
1362 1359 continue;
1363 1360 }
1364 1361 if (type->type == SYM_ARRAY)
1365 1362 continue;
1366 1363 if (!tmp->ident)
1367 1364 continue;
1368 1365
1369 1366 assign = assign_expression(member, '=', zero_expr());
1370 1367 __split_expr(assign);
1371 1368 } END_FOR_EACH_PTR(tmp);
1372 1369 }
1373 1370
1374 1371 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1375 1372 {
1376 1373 struct expression *deref, *assign, *tmp, *right;
1377 1374 struct symbol *struct_type, *type;
1378 1375 struct ident *member;
1379 1376 int member_idx;
1380 1377
1381 1378 struct_type = get_type(symbol);
1382 1379 if (!struct_type ||
1383 1380 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1384 1381 return;
1385 1382
1386 1383 /*
1387 1384 * We're parsing an initializer that could look something like this:
1388 1385 * struct foo foo = {
1389 1386 * 42,
1390 1387 * .whatever.xxx = 11,
1391 1388 * .zzz = 12,
1392 1389 * };
1393 1390 *
1394 1391 * So what we have here is a list with 42, .whatever, and .zzz. We need
1395 1392 * to break it up into left and right sides of the assignments.
1396 1393 *
1397 1394 */
1398 1395 member_idx = 0;
1399 1396 FOR_EACH_PTR(members, tmp) {
1400 1397 deref = NULL;
1401 1398 if (tmp->type == EXPR_IDENTIFIER) {
1402 1399 member_idx = member_to_number(symbol, tmp->expr_ident);
1403 1400 while (tmp->type == EXPR_IDENTIFIER) {
1404 1401 member = tmp->expr_ident;
1405 1402 tmp = tmp->ident_expression;
1406 1403 if (deref)
1407 1404 deref = member_expression(deref, '.', member);
1408 1405 else
1409 1406 deref = member_expression(symbol, '.', member);
1410 1407 }
1411 1408 } else {
1412 1409 member = number_to_member(symbol, member_idx);
1413 1410 deref = member_expression(symbol, '.', member);
1414 1411 }
1415 1412 right = tmp;
1416 1413 member_idx++;
1417 1414 if (right->type == EXPR_INITIALIZER) {
1418 1415 type = get_type(deref);
1419 1416 if (type && type->type == SYM_ARRAY)
1420 1417 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1421 1418 else
1422 1419 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1423 1420 } else {
1424 1421 assign = assign_expression(deref, '=', right);
1425 1422 fake_cb(assign);
1426 1423 }
1427 1424 } END_FOR_EACH_PTR(tmp);
1428 1425
1429 1426 set_unset_to_zero(struct_type, symbol);
1430 1427 }
1431 1428
1432 1429 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1433 1430 {
1434 1431 fake_member_assigns_helper(symbol_expression(sym),
1435 1432 sym->initializer->expr_list, fake_cb);
1436 1433 }
1437 1434
1438 1435 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1439 1436 {
1440 1437 struct expression *offset, *binop, *assign, *tmp;
1441 1438 struct symbol *type;
1442 1439 int idx;
1443 1440
1444 1441 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1445 1442 return;
1446 1443
1447 1444 idx = 0;
1448 1445 FOR_EACH_PTR(expr_list, tmp) {
1449 1446 if (tmp->type == EXPR_INDEX) {
1450 1447 if (tmp->idx_from != tmp->idx_to)
1451 1448 return;
1452 1449 idx = tmp->idx_from;
1453 1450 if (!tmp->idx_expression)
1454 1451 goto next;
1455 1452 tmp = tmp->idx_expression;
1456 1453 }
1457 1454 offset = value_expr(idx);
1458 1455 binop = array_element_expression(array, offset);
1459 1456 if (tmp->type == EXPR_INITIALIZER) {
1460 1457 type = get_type(binop);
1461 1458 if (type && type->type == SYM_ARRAY)
1462 1459 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1463 1460 else
1464 1461 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1465 1462 } else {
1466 1463 assign = assign_expression(binop, '=', tmp);
1467 1464 fake_cb(assign);
1468 1465 }
1469 1466 next:
1470 1467 idx++;
1471 1468 } END_FOR_EACH_PTR(tmp);
1472 1469 }
1473 1470
1474 1471 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1475 1472 {
1476 1473 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1477 1474 }
1478 1475
1479 1476 static void fake_assign_expr(struct symbol *sym)
1480 1477 {
1481 1478 struct expression *assign, *symbol;
1482 1479
1483 1480 symbol = symbol_expression(sym);
1484 1481 assign = assign_expression(symbol, '=', sym->initializer);
1485 1482 __split_expr(assign);
1486 1483 }
1487 1484
1488 1485 static void do_initializer_stuff(struct symbol *sym)
1489 1486 {
1490 1487 if (!sym->initializer)
1491 1488 return;
1492 1489
1493 1490 if (sym->initializer->type == EXPR_INITIALIZER) {
1494 1491 if (get_real_base_type(sym)->type == SYM_ARRAY)
1495 1492 fake_element_assigns(sym, __split_expr);
1496 1493 else
1497 1494 fake_member_assigns(sym, __split_expr);
1498 1495 } else {
1499 1496 fake_assign_expr(sym);
1500 1497 }
1501 1498 }
1502 1499
1503 1500 static void split_declaration(struct symbol_list *sym_list)
1504 1501 {
1505 1502 struct symbol *sym;
1506 1503
1507 1504 FOR_EACH_PTR(sym_list, sym) {
1508 1505 __pass_to_client(sym, DECLARATION_HOOK);
1509 1506 do_initializer_stuff(sym);
1510 1507 split_sym(sym);
1511 1508 } END_FOR_EACH_PTR(sym);
1512 1509 }
1513 1510
1514 1511 static void call_global_assign_hooks(struct expression *assign)
1515 1512 {
1516 1513 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1517 1514 }
1518 1515
1519 1516 static void fake_global_assign(struct symbol *sym)
1520 1517 {
1521 1518 struct expression *assign, *symbol;
1522 1519
1523 1520 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1524 1521 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1525 1522 fake_element_assigns(sym, call_global_assign_hooks);
1526 1523 } else if (sym->initializer) {
1527 1524 symbol = symbol_expression(sym);
1528 1525 assign = assign_expression(symbol, '=', sym->initializer);
1529 1526 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1530 1527 } else {
1531 1528 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1532 1529 }
1533 1530 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1534 1531 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1535 1532 fake_member_assigns(sym, call_global_assign_hooks);
1536 1533 } else if (sym->initializer) {
1537 1534 symbol = symbol_expression(sym);
1538 1535 assign = assign_expression(symbol, '=', sym->initializer);
1539 1536 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1540 1537 } else {
1541 1538 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1542 1539 }
1543 1540 } else {
1544 1541 symbol = symbol_expression(sym);
1545 1542 if (sym->initializer) {
1546 1543 assign = assign_expression(symbol, '=', sym->initializer);
1547 1544 __split_expr(assign);
1548 1545 } else {
1549 1546 assign = assign_expression(symbol, '=', zero_expr());
1550 1547 }
1551 1548 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1552 1549 }
1553 1550 }
1554 1551
1555 1552 static void start_function_definition(struct symbol *sym)
1556 1553 {
1557 1554 __in_function_def = 1;
1558 1555 __pass_to_client(sym, FUNC_DEF_HOOK);
1559 1556 __in_function_def = 0;
1560 1557 __pass_to_client(sym, AFTER_DEF_HOOK);
1561 1558
1562 1559 }
1563 1560
1564 1561 static void split_function(struct symbol *sym)
1565 1562 {
1566 1563 struct symbol *base_type = get_base_type(sym);
1567 1564 struct timeval stop;
1568 1565
1569 1566 if (!base_type->stmt && !base_type->inline_stmt)
1570 1567 return;
1571 1568
1572 1569 gettimeofday(&outer_fn_start_time, NULL);
1573 1570 gettimeofday(&fn_start_time, NULL);
1574 1571 cur_func_sym = sym;
1575 1572 if (sym->ident)
1576 1573 cur_func = sym->ident->name;
1577 1574 set_position(sym->pos);
1578 1575 loop_count = 0;
1579 1576 last_goto_statement_handled = 0;
1580 1577 sm_debug("new function: %s\n", cur_func);
1581 1578 __stree_id = 0;
1582 1579 if (option_two_passes) {
1583 1580 __unnullify_path();
1584 1581 loop_num = 0;
1585 1582 final_pass = 0;
1586 1583 start_function_definition(sym);
1587 1584 __split_stmt(base_type->stmt);
1588 1585 __split_stmt(base_type->inline_stmt);
1589 1586 nullify_path();
1590 1587 }
1591 1588 __unnullify_path();
1592 1589 loop_num = 0;
1593 1590 final_pass = 1;
1594 1591 start_function_definition(sym);
1595 1592 __split_stmt(base_type->stmt);
1596 1593 __split_stmt(base_type->inline_stmt);
1597 1594 __pass_to_client(sym, END_FUNC_HOOK);
1598 1595 if (need_delayed_scope_hooks())
1599 1596 __call_scope_hooks();
1600 1597 __pass_to_client(sym, AFTER_FUNC_HOOK);
1601 1598
1602 1599 clear_all_states();
1603 1600
1604 1601 gettimeofday(&stop, NULL);
1605 1602 if (option_time && stop.tv_sec - fn_start_time.tv_sec > 2) {
1606 1603 final_pass++;
1607 1604 sm_msg("func_time: %lu", stop.tv_sec - fn_start_time.tv_sec);
1608 1605 final_pass--;
1609 1606 }
1610 1607 cur_func_sym = NULL;
1611 1608 cur_func = NULL;
1612 1609 free_data_info_allocs();
1613 1610 free_expression_stack(&switch_expr_stack);
1614 1611 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1615 1612 __bail_on_rest_of_function = 0;
1616 1613 }
1617 1614
1618 1615 static void save_flow_state(void)
1619 1616 {
1620 1617 __add_ptr_list(&backup, INT_PTR(loop_num << 2), 0);
1621 1618 __add_ptr_list(&backup, INT_PTR(loop_count << 2), 0);
1622 1619 __add_ptr_list(&backup, INT_PTR(final_pass << 2), 0);
1623 1620
1624 1621 __add_ptr_list(&backup, big_statement_stack, 0);
1625 1622 __add_ptr_list(&backup, big_expression_stack, 0);
1626 1623 __add_ptr_list(&backup, big_condition_stack, 0);
1627 1624 __add_ptr_list(&backup, switch_expr_stack, 0);
1628 1625
1629 1626 __add_ptr_list(&backup, cur_func_sym, 0);
1630 1627
1631 1628 __add_ptr_list(&backup, __prev_stmt, 0);
1632 1629 __add_ptr_list(&backup, __cur_stmt, 0);
1633 1630 __add_ptr_list(&backup, __next_stmt, 0);
1634 1631
1635 1632 }
1636 1633
1637 1634 static void *pop_backup(void)
1638 1635 {
1639 1636 void *ret;
1640 1637
1641 1638 ret = last_ptr_list(backup);
1642 1639 delete_ptr_list_last(&backup);
1643 1640 return ret;
1644 1641 }
1645 1642
1646 1643 static void restore_flow_state(void)
1647 1644 {
1648 1645 __next_stmt = pop_backup();
1649 1646 __cur_stmt = pop_backup();
1650 1647 __prev_stmt = pop_backup();
1651 1648
1652 1649 cur_func_sym = pop_backup();
1653 1650 switch_expr_stack = pop_backup();
1654 1651 big_condition_stack = pop_backup();
1655 1652 big_expression_stack = pop_backup();
1656 1653 big_statement_stack = pop_backup();
1657 1654 final_pass = PTR_INT(pop_backup()) >> 2;
1658 1655 loop_count = PTR_INT(pop_backup()) >> 2;
1659 1656 loop_num = PTR_INT(pop_backup()) >> 2;
1660 1657 }
1661 1658
1662 1659 static void parse_inline(struct expression *call)
1663 1660 {
1664 1661 struct symbol *base_type;
1665 1662 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1666 1663 struct timeval time_backup = fn_start_time;
1667 1664 struct expression *orig_inline = __inline_fn;
1668 1665 int orig_budget;
1669 1666
1670 1667 if (out_of_memory() || taking_too_long())
1671 1668 return;
1672 1669
1673 1670 save_flow_state();
1674 1671
1675 1672 __pass_to_client(call, INLINE_FN_START);
1676 1673 final_pass = 0; /* don't print anything */
1677 1674 __inline_fn = call;
1678 1675 orig_budget = inline_budget;
1679 1676 inline_budget = inline_budget - 5;
1680 1677
1681 1678 base_type = get_base_type(call->fn->symbol);
1682 1679 cur_func_sym = call->fn->symbol;
1683 1680 if (call->fn->symbol->ident)
1684 1681 cur_func = call->fn->symbol->ident->name;
1685 1682 else
1686 1683 cur_func = NULL;
1687 1684 set_position(call->fn->symbol->pos);
1688 1685
1689 1686 save_all_states();
1690 1687 big_statement_stack = NULL;
1691 1688 big_expression_stack = NULL;
1692 1689 big_condition_stack = NULL;
1693 1690 switch_expr_stack = NULL;
1694 1691
1695 1692 sm_debug("inline function: %s\n", cur_func);
1696 1693 __unnullify_path();
1697 1694 loop_num = 0;
1698 1695 loop_count = 0;
1699 1696 start_function_definition(call->fn->symbol);
1700 1697 __split_stmt(base_type->stmt);
1701 1698 __split_stmt(base_type->inline_stmt);
1702 1699 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1703 1700 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1704 1701
1705 1702 free_expression_stack(&switch_expr_stack);
1706 1703 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1707 1704 nullify_path();
1708 1705 free_goto_stack();
1709 1706
1710 1707 restore_flow_state();
1711 1708 fn_start_time = time_backup;
1712 1709 cur_func = cur_func_bak;
1713 1710
1714 1711 restore_all_states();
1715 1712 set_position(call->pos);
1716 1713 __inline_fn = orig_inline;
1717 1714 inline_budget = orig_budget;
1718 1715 __pass_to_client(call, INLINE_FN_END);
1719 1716 }
1720 1717
1721 1718 static struct symbol_list *inlines_called;
1722 1719 static void add_inline_function(struct symbol *sym)
1723 1720 {
1724 1721 static struct symbol_list *already_added;
1725 1722 struct symbol *tmp;
1726 1723
1727 1724 FOR_EACH_PTR(already_added, tmp) {
1728 1725 if (tmp == sym)
1729 1726 return;
1730 1727 } END_FOR_EACH_PTR(tmp);
1731 1728
1732 1729 add_ptr_list(&already_added, sym);
1733 1730 add_ptr_list(&inlines_called, sym);
1734 1731 }
1735 1732
1736 1733 static void process_inlines(void)
1737 1734 {
1738 1735 struct symbol *tmp;
1739 1736
1740 1737 FOR_EACH_PTR(inlines_called, tmp) {
1741 1738 split_function(tmp);
1742 1739 } END_FOR_EACH_PTR(tmp);
1743 1740 free_ptr_list(&inlines_called);
1744 1741 }
1745 1742
1746 1743 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1747 1744 {
1748 1745 struct symbol *sym;
1749 1746
1750 1747 FOR_EACH_PTR_REVERSE(big_list, sym) {
1751 1748 if (!sym->scope)
1752 1749 continue;
1753 1750 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1754 1751 return sym;
1755 1752 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1756 1753 return sym;
1757 1754 } END_FOR_EACH_PTR_REVERSE(sym);
1758 1755
1759 1756 return NULL;
1760 1757 }
1761 1758
1762 1759 static bool interesting_function(struct symbol *sym)
1763 1760 {
1764 1761 static int prev_stream = -1;
1765 1762 static bool prev_answer;
1766 1763 const char *filename;
1767 1764 int len;
1768 1765
1769 1766 if (!(sym->ctype.modifiers & MOD_INLINE))
1770 1767 return true;
1771 1768
1772 1769 if (sym->pos.stream == prev_stream)
1773 1770 return prev_answer;
1774 1771
1775 1772 prev_stream = sym->pos.stream;
1776 1773 prev_answer = false;
1777 1774
1778 1775 filename = stream_name(sym->pos.stream);
1779 1776 len = strlen(filename);
1780 1777 if (len > 0 && filename[len - 1] == 'c')
1781 1778 prev_answer = true;
1782 1779 return prev_answer;
1783 1780 }
1784 1781
1785 1782 static void split_inlines_in_scope(struct symbol *sym)
1786 1783 {
1787 1784 struct symbol *base;
1788 1785 struct symbol_list *scope_list;
1789 1786 int stream;
1790 1787
1791 1788 scope_list = sym->scope->symbols;
1792 1789 stream = sym->pos.stream;
1793 1790
1794 1791 /* find the last static symbol in the file */
1795 1792 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1796 1793 if (sym->pos.stream != stream)
1797 1794 continue;
1798 1795 if (sym->type != SYM_NODE)
1799 1796 continue;
1800 1797 base = get_base_type(sym);
1801 1798 if (!base)
1802 1799 continue;
1803 1800 if (base->type != SYM_FN)
1804 1801 continue;
1805 1802 if (!base->inline_stmt)
1806 1803 continue;
1807 1804 if (!interesting_function(sym))
1808 1805 continue;
1809 1806 add_inline_function(sym);
1810 1807 } END_FOR_EACH_PTR_REVERSE(sym);
1811 1808
1812 1809 process_inlines();
1813 1810 }
1814 1811
1815 1812 static void split_inlines(struct symbol_list *sym_list)
1816 1813 {
1817 1814 struct symbol *sym;
1818 1815
1819 1816 sym = get_last_scoped_symbol(sym_list, 0);
1820 1817 if (sym)
1821 1818 split_inlines_in_scope(sym);
1822 1819 sym = get_last_scoped_symbol(sym_list, 1);
1823 1820 if (sym)
1824 1821 split_inlines_in_scope(sym);
1825 1822 }
1826 1823
1827 1824 static struct stree *clone_estates_perm(struct stree *orig)
1828 1825 {
1829 1826 struct stree *ret = NULL;
1830 1827 struct sm_state *tmp;
1831 1828
1832 1829 FOR_EACH_SM(orig, tmp) {
1833 1830 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1834 1831 } END_FOR_EACH_SM(tmp);
1835 1832
1836 1833 return ret;
1837 1834 }
1838 1835
1839 1836 struct position last_pos;
1840 1837 static void split_c_file_functions(struct symbol_list *sym_list)
1841 1838 {
1842 1839 struct symbol *sym;
1843 1840
1844 1841 __unnullify_path();
1845 1842 FOR_EACH_PTR(sym_list, sym) {
1846 1843 set_position(sym->pos);
1847 1844 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1848 1845 __pass_to_client(sym, BASE_HOOK);
1849 1846 fake_global_assign(sym);
1850 1847 }
1851 1848 } END_FOR_EACH_PTR(sym);
1852 1849 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1853 1850 nullify_path();
1854 1851
1855 1852 FOR_EACH_PTR(sym_list, sym) {
1856 1853 set_position(sym->pos);
1857 1854 last_pos = sym->pos;
1858 1855 if (!interesting_function(sym))
1859 1856 continue;
1860 1857 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1861 1858 split_function(sym);
1862 1859 process_inlines();
1863 1860 }
1864 1861 last_pos = sym->pos;
1865 1862 } END_FOR_EACH_PTR(sym);
1866 1863 split_inlines(sym_list);
1867 1864 __pass_to_client(sym_list, END_FILE_HOOK);
1868 1865 }
1869 1866
1870 1867 static int final_before_fake;
1871 1868 void init_fake_env(void)
1872 1869 {
1873 1870 if (!in_fake_env)
1874 1871 final_before_fake = final_pass;
1875 1872 in_fake_env++;
1876 1873 __push_fake_cur_stree();
1877 1874 final_pass = 0;
1878 1875 }
1879 1876
1880 1877 void end_fake_env(void)
1881 1878 {
1882 1879 __pop_fake_cur_stree();
1883 1880 in_fake_env--;
1884 1881 if (!in_fake_env)
1885 1882 final_pass = final_before_fake;
1886 1883 }
1887 1884
1888 1885 static void open_output_files(char *base_file)
1889 1886 {
1890 1887 char buf[256];
1891 1888
1892 1889 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1893 1890 sm_outfd = fopen(buf, "w");
1894 1891 if (!sm_outfd)
1895 1892 sm_fatal("Cannot open %s", buf);
1896 1893
1897 1894 if (!option_info)
1898 1895 return;
1899 1896
1900 1897 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
↓ open down ↓ |
1007 lines elided |
↑ open up ↑ |
1901 1898 sql_outfd = fopen(buf, "w");
1902 1899 if (!sql_outfd)
1903 1900 sm_fatal("Error: Cannot open %s", buf);
1904 1901
1905 1902 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1906 1903 caller_info_fd = fopen(buf, "w");
1907 1904 if (!caller_info_fd)
1908 1905 sm_fatal("Error: Cannot open %s", buf);
1909 1906 }
1910 1907
1911 -void smatch(int argc, char **argv)
1908 +void smatch(struct string_list *filelist)
1912 1909 {
1913 - struct string_list *filelist = NULL;
1914 1910 struct symbol_list *sym_list;
1915 1911 struct timeval stop, start;
1916 1912 char *path;
1917 1913 int len;
1918 1914
1919 1915 gettimeofday(&start, NULL);
1920 1916
1921 - sparse_initialize(argc, argv, &filelist);
1922 - set_valid_ptr_max();
1923 - alloc_valid_ptr_rl();
1924 1917 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1925 1918 path = getcwd(NULL, 0);
1926 1919 free(full_base_file);
1927 1920 if (path) {
1928 1921 len = strlen(path) + 1 + strlen(base_file) + 1;
1929 1922 full_base_file = malloc(len);
1930 1923 snprintf(full_base_file, len, "%s/%s", path, base_file);
1931 1924 } else {
1932 1925 full_base_file = alloc_string(base_file);
1933 1926 }
1934 1927 if (option_file_output)
1935 1928 open_output_files(base_file);
1936 1929 sym_list = sparse_keep_tokens(base_file);
1937 1930 split_c_file_functions(sym_list);
1938 1931 } END_FOR_EACH_PTR_NOTAG(base_file);
1939 1932
1940 1933 gettimeofday(&stop, NULL);
1941 1934
1942 1935 set_position(last_pos);
1936 + final_pass = 1;
1943 1937 if (option_time)
1944 1938 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1945 1939 if (option_mem)
1946 1940 sm_msg("mem: %luKb", get_max_memory());
1947 1941 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX