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