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