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