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