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