Print this page
12166 resync smatch to 0.6.1-rc1-il-3
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch_helper.c
+++ new/usr/src/tools/smatch/src/smatch_helper.c
1 1 /*
2 2 * Copyright (C) 2006 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 /*
19 19 * Miscellaneous helper functions.
20 20 */
21 21
22 22 #include <stdlib.h>
23 23 #include <stdio.h>
24 24 #include "allocate.h"
25 25 #include "smatch.h"
26 26 #include "smatch_extra.h"
27 27 #include "smatch_slist.h"
28 28
29 29 #define VAR_LEN 512
30 30
31 31 char *alloc_string(const char *str)
32 32 {
33 33 char *tmp;
34 34
35 35 if (!str)
36 36 return NULL;
37 37 tmp = malloc(strlen(str) + 1);
38 38 strcpy(tmp, str);
39 39 return tmp;
40 40 }
41 41
42 42 char *alloc_string_newline(const char *str)
43 43 {
44 44 char *tmp;
45 45 int len;
46 46
47 47 if (!str)
48 48 return NULL;
49 49 len = strlen(str);
50 50 tmp = malloc(len + 2);
51 51 snprintf(tmp, len + 2, "%s\n", str);
52 52 return tmp;
53 53 }
54 54
55 55 void free_string(char *str)
56 56 {
57 57 free(str);
58 58 }
59 59
60 60 void remove_parens(char *str)
61 61 {
62 62 char *src, *dst;
63 63
64 64 dst = src = str;
65 65 while (*src != '\0') {
66 66 if (*src == '(' || *src == ')') {
67 67 src++;
68 68 continue;
69 69 }
70 70 *dst++ = *src++;
71 71 }
72 72 *dst = *src;
73 73 }
74 74
75 75 struct smatch_state *alloc_state_num(int num)
76 76 {
77 77 struct smatch_state *state;
78 78 static char buff[256];
79 79
80 80 state = __alloc_smatch_state(0);
81 81 snprintf(buff, 255, "%d", num);
82 82 buff[255] = '\0';
83 83 state->name = alloc_string(buff);
84 84 state->data = INT_PTR(num);
85 85 return state;
86 86 }
87 87
88 88 struct smatch_state *alloc_state_str(const char *name)
89 89 {
90 90 struct smatch_state *state;
91 91
92 92 state = __alloc_smatch_state(0);
93 93 state->name = alloc_string(name);
94 94 return state;
95 95 }
96 96
97 97 struct smatch_state *merge_str_state(struct smatch_state *s1, struct smatch_state *s2)
98 98 {
99 99 if (!s1->name || !s2->name)
100 100 return &merged;
101 101 if (strcmp(s1->name, s2->name) == 0)
102 102 return s1;
103 103 return &merged;
104 104 }
105 105
106 106 struct smatch_state *alloc_state_expr(struct expression *expr)
107 107 {
108 108 struct smatch_state *state;
109 109 char *name;
110 110
111 111 expr = strip_expr(expr);
112 112 name = expr_to_str(expr);
113 113 if (!name)
114 114 return NULL;
115 115
116 116 state = __alloc_smatch_state(0);
117 117 state->name = alloc_sname(name);
118 118 free_string(name);
119 119 state->data = expr;
120 120 return state;
121 121 }
122 122
123 123 void append(char *dest, const char *data, int buff_len)
124 124 {
125 125 strncat(dest, data, buff_len - strlen(dest) - 1);
126 126 }
127 127
128 128 /*
129 129 * If you have "foo(a, b, 1);" then use
130 130 * get_argument_from_call_expr(expr, 0) to return the expression for
131 131 * a. Yes, it does start counting from 0.
132 132 */
133 133 struct expression *get_argument_from_call_expr(struct expression_list *args,
134 134 int num)
135 135 {
136 136 struct expression *expr;
137 137 int i = 0;
138 138
139 139 if (!args)
140 140 return NULL;
141 141
142 142 FOR_EACH_PTR(args, expr) {
143 143 if (i == num)
144 144 return expr;
145 145 i++;
146 146 } END_FOR_EACH_PTR(expr);
147 147 return NULL;
148 148 }
149 149
150 150 static struct expression *get_array_expr(struct expression *expr)
151 151 {
152 152 struct expression *parent;
153 153 struct symbol *type;
154 154
155 155 if (expr->type != EXPR_BINOP || expr->op != '+')
156 156 return NULL;
157 157
158 158 type = get_type(expr->left);
159 159 if (!type)
160 160 return NULL;
161 161 if (type->type == SYM_ARRAY)
162 162 return expr->left;
163 163 if (type->type != SYM_PTR)
164 164 return NULL;
165 165
166 166 parent = expr_get_parent_expr(expr);
167 167 if (!parent) /* Sometimes we haven't set up the ->parent yet. FIXME!! */
168 168 return expr->left;
169 169 if (parent->type == EXPR_PREOP && parent->op == '*')
170 170 return expr->left;
171 171
172 172 return NULL;
173 173 }
174 174
175 175 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
176 176 struct expression *expr, int len,
177 177 int *complicated, int no_parens)
178 178 {
179 179
180 180
181 181 if (!expr) {
182 182 /* can't happen on valid code */
183 183 *complicated = 1;
184 184 return;
185 185 }
186 186
187 187 switch (expr->type) {
188 188 case EXPR_DEREF: {
189 189 struct expression *deref;
190 190 int op;
191 191
192 192 deref = expr->deref;
193 193 op = deref->op;
194 194 if (deref->type == EXPR_PREOP && op == '*') {
195 195 struct expression *unop = strip_expr(deref->unop);
196 196
197 197 if (unop->type == EXPR_PREOP && unop->op == '&') {
198 198 deref = unop->unop;
199 199 op = '.';
200 200 } else {
201 201 if (!is_pointer(deref) && !is_pointer(deref->unop))
202 202 op = '.';
203 203 deref = deref->unop;
204 204 }
205 205 }
206 206
207 207 __get_variable_from_expr(sym_ptr, buf, deref, len, complicated, no_parens);
208 208
209 209 if (op == '*')
210 210 append(buf, "->", len);
211 211 else
212 212 append(buf, ".", len);
213 213
214 214 if (expr->member)
215 215 append(buf, expr->member->name, len);
216 216 else
217 217 append(buf, "unknown_member", len);
218 218
219 219 return;
220 220 }
221 221 case EXPR_SYMBOL:
222 222 if (expr->symbol_name)
223 223 append(buf, expr->symbol_name->name, len);
224 224 if (sym_ptr) {
225 225 if (*sym_ptr)
226 226 *complicated = 1;
227 227 *sym_ptr = expr->symbol;
228 228 }
229 229 return;
230 230 case EXPR_PREOP: {
231 231 const char *tmp;
232 232
233 233 if (get_expression_statement(expr)) {
234 234 *complicated = 2;
235 235 return;
236 236 }
237 237
238 238 if (expr->op == '(') {
239 239 if (!no_parens && expr->unop->type != EXPR_SYMBOL)
240 240 append(buf, "(", len);
241 241 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
242 242 tmp = show_special(expr->op);
243 243 append(buf, tmp, len);
244 244 }
245 245 __get_variable_from_expr(sym_ptr, buf, expr->unop,
246 246 len, complicated, no_parens);
247 247
248 248 if (expr->op == '(' && !no_parens && expr->unop->type != EXPR_SYMBOL)
249 249 append(buf, ")", len);
250 250
251 251 if (expr->op == SPECIAL_DECREMENT ||
252 252 expr->op == SPECIAL_INCREMENT)
253 253 *complicated = 1;
254 254
255 255 return;
256 256 }
257 257 case EXPR_POSTOP: {
258 258 const char *tmp;
259 259
260 260 __get_variable_from_expr(sym_ptr, buf, expr->unop,
261 261 len, complicated, no_parens);
262 262 tmp = show_special(expr->op);
263 263 append(buf, tmp, len);
264 264
265 265 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
266 266 *complicated = 1;
267 267 return;
268 268 }
269 269 case EXPR_ASSIGNMENT:
270 270 case EXPR_COMPARE:
271 271 case EXPR_LOGICAL:
272 272 case EXPR_BINOP: {
273 273 char tmp[10];
274 274 struct expression *array_expr;
275 275
276 276 *complicated = 1;
277 277 array_expr = get_array_expr(expr);
278 278 if (array_expr) {
279 279 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
280 280 append(buf, "[", len);
281 281 } else {
282 282 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
283 283 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
284 284 append(buf, tmp, len);
285 285 }
286 286 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
287 287 if (array_expr)
288 288 append(buf, "]", len);
289 289 return;
290 290 }
291 291 case EXPR_VALUE: {
↓ open down ↓ |
291 lines elided |
↑ open up ↑ |
292 292 sval_t sval = {};
293 293 char tmp[25];
294 294
295 295 *complicated = 1;
296 296 if (!get_value(expr, &sval))
297 297 return;
298 298 snprintf(tmp, 25, "%s", sval_to_numstr(sval));
299 299 append(buf, tmp, len);
300 300 return;
301 301 }
302 + case EXPR_FVALUE: {
303 + sval_t sval = {};
304 + char tmp[25];
305 +
306 + *complicated = 1;
307 + if (!get_value(expr, &sval))
308 + return;
309 + snprintf(tmp, 25, "%s", sval_to_numstr(sval));
310 + append(buf, tmp, len);
311 + return;
312 + }
302 313 case EXPR_STRING:
303 314 append(buf, "\"", len);
304 315 if (expr->string)
305 316 append(buf, expr->string->data, len);
306 317 append(buf, "\"", len);
307 318 return;
308 319 case EXPR_CALL: {
309 320 struct expression *tmp;
310 321 int i;
311 322
312 323 *complicated = 1;
313 324 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
314 325 append(buf, "(", len);
315 326 i = 0;
316 327 FOR_EACH_PTR(expr->args, tmp) {
317 328 if (i++)
318 329 append(buf, ", ", len);
319 330 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
320 331 } END_FOR_EACH_PTR(tmp);
321 332 append(buf, ")", len);
322 333 return;
323 334 }
324 335 case EXPR_CAST:
325 336 case EXPR_FORCE_CAST:
326 337 __get_variable_from_expr(sym_ptr, buf,
327 338 expr->cast_expression, len,
328 339 complicated, no_parens);
329 340 return;
330 341 case EXPR_SIZEOF: {
331 342 sval_t sval;
332 343 int size;
333 344 char tmp[25];
334 345
335 346 if (expr->cast_type && get_base_type(expr->cast_type)) {
336 347 size = type_bytes(get_base_type(expr->cast_type));
337 348 snprintf(tmp, 25, "%d", size);
338 349 append(buf, tmp, len);
339 350 } else if (get_value(expr, &sval)) {
340 351 snprintf(tmp, 25, "%s", sval_to_str(sval));
341 352 append(buf, tmp, len);
342 353 }
343 354 return;
344 355 }
345 356 case EXPR_IDENTIFIER:
346 357 *complicated = 1;
347 358 if (expr->expr_ident)
348 359 append(buf, expr->expr_ident->name, len);
349 360 return;
350 361 default:
351 362 *complicated = 1;
352 363 //printf("unknown type = %d\n", expr->type);
353 364 return;
354 365 }
355 366 }
356 367
357 368 struct expr_str_cache_results {
358 369 struct expression *expr;
359 370 int no_parens;
360 371 char str[VAR_LEN];
361 372 struct symbol *sym;
362 373 int complicated;
363 374 };
364 375
365 376 static void get_variable_from_expr(struct symbol **sym_ptr, char *buf,
366 377 struct expression *expr, int len,
367 378 int *complicated, int no_parens)
368 379 {
369 380 static struct expr_str_cache_results cached[8];
370 381 struct symbol *tmp_sym = NULL;
371 382 static int idx;
372 383 int i;
373 384
374 385 for (i = 0; i < ARRAY_SIZE(cached); i++) {
375 386 if (expr == cached[i].expr &&
376 387 no_parens == cached[i].no_parens) {
377 388 strncpy(buf, cached[i].str, len);
378 389 if (sym_ptr)
379 390 *sym_ptr = cached[i].sym;
380 391 *complicated = cached[i].complicated;
381 392 return;
382 393 }
383 394 }
384 395
385 396 __get_variable_from_expr(&tmp_sym, buf, expr, len, complicated, no_parens);
386 397 if (sym_ptr)
387 398 *sym_ptr = tmp_sym;
388 399
389 400 cached[idx].expr = expr;
390 401 cached[idx].no_parens = no_parens;
391 402 strncpy(cached[idx].str, buf, VAR_LEN);
392 403 cached[idx].sym = tmp_sym;
393 404 cached[idx].complicated = *complicated;
394 405
395 406 idx = (idx + 1) % ARRAY_SIZE(cached);
396 407 }
397 408
398 409 /*
399 410 * This is returns a stylized "c looking" representation of the
400 411 * variable name.
401 412 *
402 413 * It uses the same buffer every time so you have to save the result
403 414 * yourself if you want to keep it.
404 415 *
405 416 */
406 417
407 418 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
408 419 {
409 420 static char var_name[VAR_LEN];
410 421 int complicated = 0;
411 422
412 423 if (sym_ptr)
413 424 *sym_ptr = NULL;
414 425 var_name[0] = '\0';
415 426
416 427 if (!expr)
417 428 return NULL;
418 429 get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
419 430 &complicated, 0);
420 431 if (complicated < 2)
421 432 return alloc_string(var_name);
422 433 else
423 434 return NULL;
424 435 }
425 436
426 437 char *expr_to_str(struct expression *expr)
427 438 {
428 439 return expr_to_str_sym(expr, NULL);
429 440 }
430 441
431 442 /*
432 443 * get_variable_from_expr_simple() only returns simple variables.
433 444 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
434 445 * then it returns NULL.
435 446 */
436 447 char *expr_to_var_sym(struct expression *expr,
437 448 struct symbol **sym_ptr)
438 449 {
439 450 static char var_name[VAR_LEN];
440 451 int complicated = 0;
441 452
442 453 if (sym_ptr)
443 454 *sym_ptr = NULL;
444 455 var_name[0] = '\0';
445 456
446 457 if (!expr)
447 458 return NULL;
448 459 expr = strip_expr(expr);
449 460 get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
450 461 &complicated, 1);
451 462
452 463 if (complicated) {
453 464 if (sym_ptr)
454 465 *sym_ptr = NULL;
455 466 return NULL;
456 467 }
457 468 return alloc_string(var_name);
458 469 }
459 470
460 471 char *expr_to_var(struct expression *expr)
461 472 {
462 473 return expr_to_var_sym(expr, NULL);
463 474 }
464 475
465 476 struct symbol *expr_to_sym(struct expression *expr)
466 477 {
467 478 struct symbol *sym;
468 479 char *name;
469 480
470 481 name = expr_to_var_sym(expr, &sym);
471 482 free_string(name);
472 483 return sym;
473 484 }
474 485
475 486 int get_complication_score(struct expression *expr)
476 487 {
477 488 expr = strip_expr(expr);
478 489
479 490 /*
480 491 * Don't forget to keep get_complication_score() and store_all_links()
481 492 * in sync.
482 493 *
483 494 */
484 495
485 496 if (!expr)
486 497 return 990;
487 498
488 499 switch (expr->type) {
489 500 case EXPR_CALL:
490 501 return 991;
491 502 case EXPR_COMPARE:
492 503 case EXPR_BINOP:
493 504 return get_complication_score(expr->left) +
494 505 get_complication_score(expr->right);
495 506 case EXPR_SYMBOL:
496 507 return 1;
497 508 case EXPR_PREOP:
498 509 if (expr->op == '*' || expr->op == '(')
499 510 return get_complication_score(expr->unop);
500 511 return 993;
501 512 case EXPR_DEREF:
502 513 return get_complication_score(expr->deref);
503 514 case EXPR_VALUE:
504 515 case EXPR_SIZEOF:
505 516 return 0;
506 517 default:
507 518 return 994;
508 519 }
509 520 }
510 521
511 522 struct expression *reorder_expr_alphabetically(struct expression *expr)
512 523 {
513 524 struct expression *ret;
514 525 char *left, *right;
515 526
516 527 if (expr->type != EXPR_BINOP)
517 528 return expr;
518 529 if (expr->op != '+' && expr->op != '*')
519 530 return expr;
520 531
521 532 left = expr_to_var(expr->left);
522 533 right = expr_to_var(expr->right);
523 534 ret = expr;
524 535 if (!left || !right)
525 536 goto free;
526 537 if (strcmp(left, right) <= 0)
527 538 goto free;
528 539
529 540 ret = binop_expression(expr->right, expr->op, expr->left);
530 541 free:
531 542 free_string(left);
532 543 free_string(right);
533 544
534 545 return ret;
535 546 }
536 547
537 548 char *expr_to_chunk_helper(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
538 549 {
539 550 struct var_sym_list *tmp_vsl;
540 551 char *name;
541 552 struct symbol *tmp;
542 553 int score;
543 554
544 555 if (vsl)
545 556 *vsl = NULL;
546 557 if (sym)
547 558 *sym = NULL;
548 559
549 560 expr = strip_parens(expr);
550 561 if (!expr)
551 562 return NULL;
552 563
553 564 name = expr_to_var_sym(expr, &tmp);
554 565 if (name && tmp) {
555 566 if (sym)
556 567 *sym = tmp;
557 568 if (vsl)
558 569 add_var_sym(vsl, name, tmp);
559 570 return name;
560 571 }
561 572 free_string(name);
562 573
563 574 score = get_complication_score(expr);
564 575 if (score <= 0 || score > 2)
565 576 return NULL;
566 577
567 578 tmp_vsl = expr_to_vsl(expr);
568 579 if (vsl) {
569 580 *vsl = tmp_vsl;
570 581 if (!*vsl)
571 582 return NULL;
572 583 }
573 584 if (sym) {
574 585 if (ptr_list_size((struct ptr_list *)tmp_vsl) == 1) {
575 586 struct var_sym *vs;
576 587
577 588 vs = first_ptr_list((struct ptr_list *)tmp_vsl);
578 589 *sym = vs->sym;
579 590 }
580 591 }
581 592
582 593 expr = reorder_expr_alphabetically(expr);
583 594
584 595 return expr_to_str(expr);
585 596 }
586 597
587 598 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym)
588 599 {
589 600 return expr_to_chunk_helper(expr, sym, NULL);
590 601 }
591 602
592 603 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
593 604 {
594 605 return expr_to_chunk_helper(expr, sym, vsl);
595 606 }
596 607
597 608 int sym_name_is(const char *name, struct expression *expr)
598 609 {
599 610 if (!expr)
600 611 return 0;
601 612 if (expr->type != EXPR_SYMBOL)
602 613 return 0;
603 614 if (!strcmp(expr->symbol_name->name, name))
604 615 return 1;
605 616 return 0;
606 617 }
607 618
608 619 int expr_is_zero(struct expression *expr)
609 620 {
610 621 sval_t sval;
611 622
612 623 if (get_value(expr, &sval) && sval.value == 0)
613 624 return 1;
614 625 return 0;
615 626 }
616 627
617 628 int is_array(struct expression *expr)
618 629 {
619 630 struct symbol *type;
620 631
621 632 expr = strip_expr(expr);
622 633 if (!expr)
623 634 return 0;
624 635
625 636 if (expr->type == EXPR_PREOP && expr->op == '*') {
626 637 expr = strip_expr(expr->unop);
627 638 if (!expr)
628 639 return 0;
629 640 if (expr->type == EXPR_BINOP && expr->op == '+')
630 641 return 1;
631 642 }
632 643
633 644 if (expr->type != EXPR_BINOP || expr->op != '+')
634 645 return 0;
635 646
636 647 type = get_type(expr->left);
637 648 if (!type || type->type != SYM_ARRAY)
638 649 return 0;
639 650
640 651 return 1;
641 652 }
642 653
643 654 struct expression *get_array_base(struct expression *expr)
644 655 {
645 656 if (!is_array(expr))
646 657 return NULL;
647 658 expr = strip_expr(expr);
648 659 if (expr->type == EXPR_PREOP && expr->op == '*')
649 660 expr = strip_expr(expr->unop);
650 661 if (expr->type != EXPR_BINOP || expr->op != '+')
651 662 return NULL;
652 663 return strip_parens(expr->left);
653 664 }
654 665
655 666 struct expression *get_array_offset(struct expression *expr)
656 667 {
657 668 if (!is_array(expr))
658 669 return NULL;
659 670 expr = strip_expr(expr);
660 671 if (expr->type == EXPR_PREOP && expr->op == '*')
661 672 expr = strip_expr(expr->unop);
662 673 if (expr->type != EXPR_BINOP || expr->op != '+')
663 674 return NULL;
664 675 return strip_parens(expr->right);
665 676 }
666 677
667 678 const char *show_state(struct smatch_state *state)
668 679 {
669 680 if (!state)
670 681 return NULL;
671 682 return state->name;
672 683 }
673 684
674 685 struct statement *get_expression_statement(struct expression *expr)
675 686 {
676 687 /* What are those things called? if (({....; ret;})) { ...*/
677 688
678 689 if (expr->type != EXPR_PREOP)
679 690 return NULL;
680 691 if (expr->op != '(')
681 692 return NULL;
682 693 if (!expr->unop)
683 694 return NULL;
684 695 if (expr->unop->type != EXPR_STATEMENT)
685 696 return NULL;
686 697 if (expr->unop->statement->type != STMT_COMPOUND)
687 698 return NULL;
688 699 return expr->unop->statement;
689 700 }
690 701
691 702 struct expression *strip_parens(struct expression *expr)
692 703 {
693 704 if (!expr)
694 705 return NULL;
695 706
696 707 if (expr->type == EXPR_PREOP) {
697 708 if (!expr->unop)
698 709 return expr; /* parsing invalid code */
699 710
700 711 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
701 712 expr->unop->statement->type == STMT_COMPOUND)
702 713 return expr;
703 714 if (expr->op == '(')
704 715 return strip_parens(expr->unop);
705 716 }
706 717 return expr;
707 718 }
708 719
709 720 static struct expression *strip_expr_helper(struct expression *expr, bool set_parent)
710 721 {
711 722 if (!expr)
712 723 return NULL;
713 724
714 725 switch (expr->type) {
715 726 case EXPR_FORCE_CAST:
716 727 case EXPR_CAST:
717 728 if (set_parent)
718 729 expr_set_parent_expr(expr->cast_expression, expr);
719 730
720 731 if (!expr->cast_expression)
721 732 return expr;
722 733 return strip_expr_helper(expr->cast_expression, set_parent);
723 734 case EXPR_PREOP: {
724 735 struct expression *unop;
725 736
726 737 if (!expr->unop) /* parsing invalid code */
727 738 return expr;
728 739 if (set_parent)
729 740 expr_set_parent_expr(expr->unop, expr);
730 741
731 742
732 743 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
733 744 expr->unop->statement->type == STMT_COMPOUND)
734 745 return expr;
735 746
736 747 unop = strip_expr_helper(expr->unop, set_parent);
737 748
738 749 if (expr->op == '*' && unop &&
739 750 unop->type == EXPR_PREOP && unop->op == '&') {
740 751 struct symbol *type = get_type(unop->unop);
741 752
742 753 if (type && type->type == SYM_ARRAY)
743 754 return expr;
744 755 return strip_expr_helper(unop->unop, set_parent);
745 756 }
746 757
747 758 if (expr->op == '(')
748 759 return unop;
749 760
750 761 return expr;
751 762 }
752 763 case EXPR_CONDITIONAL:
753 764 if (known_condition_true(expr->conditional)) {
754 765 if (expr->cond_true) {
755 766 if (set_parent)
756 767 expr_set_parent_expr(expr->cond_true, expr);
757 768 return strip_expr_helper(expr->cond_true, set_parent);
758 769 }
759 770 if (set_parent)
760 771 expr_set_parent_expr(expr->conditional, expr);
761 772 return strip_expr_helper(expr->conditional, set_parent);
762 773 }
763 774 if (known_condition_false(expr->conditional)) {
764 775 if (set_parent)
765 776 expr_set_parent_expr(expr->cond_false, expr);
766 777 return strip_expr_helper(expr->cond_false, set_parent);
767 778 }
768 779 return expr;
769 780 case EXPR_CALL:
770 781 if (sym_name_is("__builtin_expect", expr->fn) ||
771 782 sym_name_is("__builtin_bswap16", expr->fn) ||
772 783 sym_name_is("__builtin_bswap32", expr->fn) ||
773 784 sym_name_is("__builtin_bswap64", expr->fn)) {
774 785 expr = get_argument_from_call_expr(expr->args, 0);
775 786 return strip_expr_helper(expr, set_parent);
776 787 }
777 788 return expr;
778 789 }
779 790 return expr;
780 791 }
781 792
782 793 struct expression *strip_expr(struct expression *expr)
783 794 {
784 795 return strip_expr_helper(expr, false);
785 796 }
786 797
787 798 struct expression *strip_expr_set_parent(struct expression *expr)
788 799 {
789 800 return strip_expr_helper(expr, true);
790 801 }
791 802
792 803 static void delete_state_tracker(struct tracker *t)
793 804 {
794 805 delete_state(t->owner, t->name, t->sym);
795 806 __free_tracker(t);
796 807 }
797 808
798 809 void scoped_state(int my_id, const char *name, struct symbol *sym)
799 810 {
800 811 struct tracker *t;
801 812
802 813 t = alloc_tracker(my_id, name, sym);
803 814 add_scope_hook((scope_hook *)&delete_state_tracker, t);
804 815 }
805 816
806 817 int is_error_return(struct expression *expr)
807 818 {
808 819 struct symbol *cur_func = cur_func_sym;
809 820 struct range_list *rl;
810 821 sval_t sval;
811 822
812 823 if (!expr)
813 824 return 0;
814 825 if (cur_func->type != SYM_NODE)
815 826 return 0;
816 827 cur_func = get_base_type(cur_func);
817 828 if (cur_func->type != SYM_FN)
818 829 return 0;
819 830 cur_func = get_base_type(cur_func);
820 831 if (cur_func == &void_ctype)
821 832 return 0;
822 833 if (option_project == PROJ_KERNEL &&
823 834 get_implied_rl(expr, &rl) &&
824 835 rl_type(rl) == &int_ctype &&
825 836 sval_is_negative(rl_min(rl)) &&
826 837 rl_max(rl).value == -1)
827 838 return 1;
828 839 if (!get_implied_value(expr, &sval))
829 840 return 0;
830 841 if (sval.value < 0)
831 842 return 1;
832 843 if (cur_func->type == SYM_PTR && sval.value == 0)
833 844 return 1;
834 845 return 0;
835 846 }
836 847
837 848 int getting_address(struct expression *expr)
838 849 {
839 850 int deref_count = 0;
840 851
841 852 while ((expr = expr_get_parent_expr(expr))) {
842 853 if (expr->type == EXPR_PREOP && expr->op == '*') {
843 854 /* &foo->bar->baz dereferences "foo->bar" */
844 855 if (deref_count == 0)
845 856 deref_count++;
846 857 return false;
847 858 }
848 859 if (expr->type == EXPR_PREOP && expr->op == '&')
849 860 return true;
850 861 }
851 862 return false;
852 863 }
853 864
854 865 int get_struct_and_member(struct expression *expr, const char **type, const char **member)
855 866 {
856 867 struct symbol *sym;
857 868
858 869 expr = strip_expr(expr);
859 870 if (expr->type != EXPR_DEREF)
860 871 return 0;
861 872 if (!expr->member)
862 873 return 0;
863 874
864 875 sym = get_type(expr->deref);
865 876 if (!sym)
866 877 return 0;
867 878 if (sym->type == SYM_UNION)
868 879 return 0;
869 880 if (!sym->ident)
870 881 return 0;
871 882
872 883 *type = sym->ident->name;
873 884 *member = expr->member->name;
874 885 return 1;
875 886 }
876 887
877 888 char *get_member_name(struct expression *expr)
878 889 {
879 890 char buf[256];
880 891 struct symbol *sym;
881 892
882 893 expr = strip_expr(expr);
883 894 if (!expr || expr->type != EXPR_DEREF)
884 895 return NULL;
885 896 if (!expr->member)
886 897 return NULL;
887 898
888 899 sym = get_type(expr->deref);
889 900 if (!sym)
890 901 return NULL;
891 902 if (sym->type == SYM_UNION) {
892 903 snprintf(buf, sizeof(buf), "(union %s)->%s",
893 904 sym->ident ? sym->ident->name : "anonymous",
894 905 expr->member->name);
895 906 return alloc_string(buf);
896 907 }
897 908 if (!sym->ident) {
898 909 struct expression *deref;
899 910 char *full, *outer;
900 911 int len;
901 912
902 913 /*
903 914 * If we're in an anonymous struct then maybe we can find an
904 915 * outer struct name to use as a name. This code should be
905 916 * recursive and cleaner. I am not very proud of it.
906 917 *
907 918 */
908 919
909 920 deref = expr->deref;
910 921 if (deref->type != EXPR_DEREF || !deref->member)
911 922 return NULL;
912 923 sym = get_type(deref->deref);
913 924 if (!sym || sym->type != SYM_STRUCT || !sym->ident)
914 925 return NULL;
915 926
916 927 full = expr_to_str(expr);
917 928 if (!full)
918 929 return NULL;
919 930 deref = deref->deref;
920 931 if (deref->type == EXPR_PREOP && deref->op == '*')
921 932 deref = deref->unop;
922 933 outer = expr_to_str(deref);
923 934 if (!outer) {
924 935 free_string(full);
925 936 return NULL;
926 937 }
927 938 len = strlen(outer);
928 939 if (strncmp(outer, full, len) != 0) {
929 940 free_string(full);
930 941 free_string(outer);
931 942 return NULL;
932 943 }
933 944 if (full[len] == '-' && full[len + 1] == '>')
934 945 len += 2;
935 946 if (full[len] == '.')
936 947 len++;
937 948 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, full + len);
938 949 free_string(outer);
939 950 free_string(full);
940 951
941 952 return alloc_string(buf);
942 953 }
943 954 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
944 955 return alloc_string(buf);
945 956 }
946 957
947 958 int cmp_pos(struct position pos1, struct position pos2)
948 959 {
949 960 /* the stream position is ... */
950 961 if (pos1.stream > pos2.stream)
951 962 return -1;
952 963 if (pos1.stream < pos2.stream)
953 964 return 1;
954 965
955 966 if (pos1.line < pos2.line)
956 967 return -1;
957 968 if (pos1.line > pos2.line)
958 969 return 1;
959 970
960 971 if (pos1.pos < pos2.pos)
961 972 return -1;
962 973 if (pos1.pos > pos2.pos)
963 974 return 1;
964 975
965 976 return 0;
966 977 }
967 978
968 979 int positions_eq(struct position pos1, struct position pos2)
969 980 {
970 981 if (pos1.line != pos2.line)
971 982 return 0;
972 983 if (pos1.pos != pos2.pos)
973 984 return 0;
974 985 if (pos1.stream != pos2.stream)
975 986 return 0;
976 987 return 1;
977 988 }
978 989
979 990 struct statement *get_current_statement(void)
980 991 {
981 992 struct statement *prev, *tmp;
982 993
983 994 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
984 995
985 996 if (!prev || !get_macro_name(prev->pos))
986 997 return prev;
987 998
988 999 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
989 1000 if (positions_eq(tmp->pos, prev->pos))
990 1001 continue;
991 1002 if (prev->pos.line > tmp->pos.line)
992 1003 return prev;
993 1004 return tmp;
994 1005 } END_FOR_EACH_PTR_REVERSE(tmp);
995 1006 return prev;
996 1007 }
997 1008
998 1009 struct statement *get_prev_statement(void)
999 1010 {
1000 1011 struct statement *tmp;
1001 1012 int i;
1002 1013
1003 1014 i = 0;
1004 1015 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
1005 1016 if (i++ == 1)
1006 1017 return tmp;
1007 1018 } END_FOR_EACH_PTR_REVERSE(tmp);
1008 1019 return NULL;
1009 1020 }
1010 1021
1011 1022 struct expression *get_last_expr_from_expression_stmt(struct expression *expr)
1012 1023 {
1013 1024 struct statement *stmt;
1014 1025 struct statement *last_stmt;
1015 1026
1016 1027 while (expr->type == EXPR_PREOP && expr->op == '(')
1017 1028 expr = expr->unop;
1018 1029 if (expr->type != EXPR_STATEMENT)
1019 1030 return NULL;
1020 1031 stmt = expr->statement;
1021 1032 if (!stmt)
1022 1033 return NULL;
1023 1034 if (stmt->type == STMT_COMPOUND) {
1024 1035 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1025 1036 if (!last_stmt)
1026 1037 return NULL;
1027 1038 if (last_stmt->type == STMT_LABEL)
1028 1039 last_stmt = last_stmt->label_statement;
1029 1040 if (last_stmt->type != STMT_EXPRESSION)
1030 1041 return NULL;
1031 1042 return last_stmt->expression;
1032 1043 }
1033 1044 if (stmt->type == STMT_EXPRESSION)
1034 1045 return stmt->expression;
1035 1046 return NULL;
1036 1047 }
1037 1048
1038 1049 int get_param_num_from_sym(struct symbol *sym)
1039 1050 {
1040 1051 struct symbol *tmp;
1041 1052 int i;
1042 1053
1043 1054 if (!cur_func_sym)
1044 1055 return -1;
1045 1056
1046 1057 i = 0;
1047 1058 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
1048 1059 if (tmp == sym)
1049 1060 return i;
1050 1061 i++;
1051 1062 } END_FOR_EACH_PTR(tmp);
1052 1063 return -1;
1053 1064 }
1054 1065
1055 1066 int get_param_num(struct expression *expr)
1056 1067 {
1057 1068 struct symbol *sym;
1058 1069 char *name;
↓ open down ↓ |
747 lines elided |
↑ open up ↑ |
1059 1070
1060 1071 if (!cur_func_sym)
1061 1072 return -1;
1062 1073 name = expr_to_var_sym(expr, &sym);
1063 1074 free_string(name);
1064 1075 if (!sym)
1065 1076 return -1;
1066 1077 return get_param_num_from_sym(sym);
1067 1078 }
1068 1079
1080 +struct symbol *get_param_sym_from_num(int num)
1081 +{
1082 + struct symbol *sym;
1083 + int i;
1084 +
1085 + if (!cur_func_sym)
1086 + return NULL;
1087 +
1088 + i = 0;
1089 + FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, sym) {
1090 + if (i++ == num)
1091 + return sym;
1092 + } END_FOR_EACH_PTR(sym);
1093 + return NULL;
1094 +}
1095 +
1069 1096 int ms_since(struct timeval *start)
1070 1097 {
1071 1098 struct timeval end;
1072 1099 double diff;
1073 1100
1074 1101 gettimeofday(&end, NULL);
1075 1102 diff = (end.tv_sec - start->tv_sec) * 1000.0;
1076 1103 diff += (end.tv_usec - start->tv_usec) / 1000.0;
1077 1104 return (int)diff;
1078 1105 }
1079 1106
1080 1107 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
1081 1108 {
1082 1109 if (!name || !sym)
1083 1110 return 0;
1084 1111
1085 1112 if (parent_is_null_var_sym(name, sym) ||
1086 1113 parent_is_free_var_sym(name, sym))
1087 1114 return 1;
1088 1115 return 0;
1089 1116 }
1090 1117
1091 1118 int parent_is_gone(struct expression *expr)
1092 1119 {
1093 1120 struct symbol *sym;
1094 1121 char *var;
1095 1122 int ret = 0;
1096 1123
1097 1124 expr = strip_expr(expr);
1098 1125 var = expr_to_var_sym(expr, &sym);
1099 1126 if (!var || !sym)
1100 1127 goto free;
1101 1128 ret = parent_is_gone_var_sym(var, sym);
1102 1129 free:
1103 1130 free_string(var);
1104 1131 return ret;
1105 1132 }
1106 1133
1107 1134 int invert_op(int op)
1108 1135 {
1109 1136 switch (op) {
1110 1137 case '*':
1111 1138 return '/';
1112 1139 case '/':
1113 1140 return '*';
1114 1141 case '+':
1115 1142 return '-';
1116 1143 case '-':
1117 1144 return '+';
1118 1145 case SPECIAL_LEFTSHIFT:
1119 1146 return SPECIAL_RIGHTSHIFT;
1120 1147 case SPECIAL_RIGHTSHIFT:
1121 1148 return SPECIAL_LEFTSHIFT;
1122 1149 }
1123 1150 return 0;
1124 1151 }
1125 1152
1126 1153 int op_remove_assign(int op)
1127 1154 {
1128 1155 switch (op) {
1129 1156 case SPECIAL_ADD_ASSIGN:
1130 1157 return '+';
1131 1158 case SPECIAL_SUB_ASSIGN:
1132 1159 return '-';
1133 1160 case SPECIAL_MUL_ASSIGN:
1134 1161 return '*';
1135 1162 case SPECIAL_DIV_ASSIGN:
1136 1163 return '/';
1137 1164 case SPECIAL_MOD_ASSIGN:
1138 1165 return '%';
1139 1166 case SPECIAL_AND_ASSIGN:
1140 1167 return '&';
1141 1168 case SPECIAL_OR_ASSIGN:
1142 1169 return '|';
1143 1170 case SPECIAL_XOR_ASSIGN:
1144 1171 return '^';
1145 1172 case SPECIAL_SHL_ASSIGN:
1146 1173 return SPECIAL_LEFTSHIFT;
1147 1174 case SPECIAL_SHR_ASSIGN:
1148 1175 return SPECIAL_RIGHTSHIFT;
1149 1176 default:
1150 1177 return op;
1151 1178 }
1152 1179 }
1153 1180
1154 1181 int expr_equiv(struct expression *one, struct expression *two)
1155 1182 {
1156 1183 struct symbol *one_sym = NULL;
1157 1184 struct symbol *two_sym = NULL;
1158 1185 char *one_name = NULL;
1159 1186 char *two_name = NULL;
1160 1187 int ret = 0;
1161 1188
1162 1189 if (!one || !two)
1163 1190 return 0;
1164 1191 if (one->type != two->type)
1165 1192 return 0;
1166 1193 if (is_fake_call(one) || is_fake_call(two))
1167 1194 return 0;
1168 1195
1169 1196 one_name = expr_to_str_sym(one, &one_sym);
1170 1197 if (!one_name)
1171 1198 goto free;
1172 1199 two_name = expr_to_str_sym(two, &two_sym);
1173 1200 if (!two_name)
1174 1201 goto free;
1175 1202 if (one_sym != two_sym)
1176 1203 goto free;
1177 1204 /*
1178 1205 * This is a terrible hack because expr_to_str() sometimes gives up in
1179 1206 * the middle and just returns what it has. If you see a () you know
1180 1207 * the string is bogus.
1181 1208 */
1182 1209 if (strstr(one_name, "()"))
1183 1210 goto free;
1184 1211 if (strcmp(one_name, two_name) == 0)
1185 1212 ret = 1;
1186 1213 free:
1187 1214 free_string(one_name);
1188 1215 free_string(two_name);
1189 1216 return ret;
1190 1217 }
1191 1218
1192 1219 void push_int(struct int_stack **stack, int num)
1193 1220 {
1194 1221 int *munged;
1195 1222
1196 1223 /*
1197 1224 * Just put the int on directly instead of a pointer to the int.
1198 1225 * Shift it to the left because Sparse uses the last two bits.
1199 1226 * This is sort of a dirty hack, yes.
1200 1227 */
1201 1228
1202 1229 munged = INT_PTR(num << 2);
1203 1230
1204 1231 add_ptr_list(stack, munged);
1205 1232 }
1206 1233
1207 1234 int pop_int(struct int_stack **stack)
1208 1235 {
1209 1236 int *num;
1210 1237
1211 1238 num = last_ptr_list((struct ptr_list *)*stack);
1212 1239 delete_ptr_list_last((struct ptr_list **)stack);
1213 1240
1214 1241 return PTR_INT(num) >> 2;
1215 1242 }
↓ open down ↓ |
137 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX