struct_assignment: ignore certain types of assignments
[smatch.git] / smatch_db.c
blobd46866cf9eb75f530a4b5ebdf5d76ec5a823cf93
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include <string.h>
19 #include <errno.h>
20 #include <sqlite3.h>
21 #include <unistd.h>
22 #include <ctype.h>
23 #include "smatch.h"
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
27 static sqlite3 *db;
28 static sqlite3 *mem_db;
30 static int return_id;
32 #define sql_insert(table, values...) \
33 do { \
34 if (!mem_db) \
35 break; \
36 if (__inline_fn) { \
37 char buf[1024]; \
38 char *err, *p = buf; \
39 int rc; \
41 p += snprintf(p, buf + sizeof(buf) - p, \
42 "insert into %s values (", #table); \
43 p += snprintf(p, buf + sizeof(buf) - p, values); \
44 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
45 sm_debug("in-mem: %s\n", buf); \
46 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
47 if (rc != SQLITE_OK) { \
48 fprintf(stderr, "SQL error #2: %s\n", err); \
49 fprintf(stderr, "SQL: '%s'\n", buf); \
50 } \
51 break; \
52 } \
53 if (option_info) { \
54 sm_prefix(); \
55 sm_printf("SQL: insert into " #table " values (" values); \
56 sm_printf(");\n"); \
57 } \
58 } while (0)
60 struct def_callback {
61 int hook_type;
62 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
64 ALLOCATOR(def_callback, "definition db hook callbacks");
65 DECLARE_PTR_LIST(callback_list, struct def_callback);
66 static struct callback_list *callbacks;
68 struct member_info_callback {
69 int owner;
70 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
72 ALLOCATOR(member_info_callback, "caller_info callbacks");
73 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
74 static struct member_info_cb_list *member_callbacks;
76 struct returned_state_callback {
77 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
79 ALLOCATOR(returned_state_callback, "returned state callbacks");
80 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
81 static struct returned_state_cb_list *returned_state_callbacks;
83 struct returned_member_callback {
84 int owner;
85 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
87 ALLOCATOR(returned_member_callback, "returned member callbacks");
88 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
89 static struct returned_member_cb_list *returned_member_callbacks;
91 struct call_implies_callback {
92 int type;
93 void (*callback)(struct expression *arg, char *key, char *value);
95 ALLOCATOR(call_implies_callback, "call_implies callbacks");
96 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
97 static struct call_implies_cb_list *call_implies_cb_list;
99 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
101 int i;
103 for (i = 0; i < argc; i++) {
104 if (i != 0)
105 printf(", ");
106 sm_printf("%s", argv[i]);
108 sm_printf("\n");
109 return 0;
112 void debug_sql(const char *sql)
114 if (!option_debug)
115 return;
116 sm_msg("%s", sql);
117 sql_exec(print_sql_output, NULL, sql);
120 void debug_mem_sql(const char *sql)
122 if (!option_debug)
123 return;
124 sm_msg("%s", sql);
125 sql_mem_exec(print_sql_output, NULL, sql);
128 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
130 char *err = NULL;
131 int rc;
133 if (option_no_db || !db)
134 return;
136 rc = sqlite3_exec(db, sql, callback, data, &err);
137 if (rc != SQLITE_OK) {
138 fprintf(stderr, "SQL error #2: %s\n", err);
139 fprintf(stderr, "SQL: '%s'\n", sql);
143 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
145 char *err = NULL;
146 int rc;
148 if (!mem_db)
149 return;
151 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
152 if (rc != SQLITE_OK) {
153 fprintf(stderr, "SQL error #2: %s\n", err);
154 fprintf(stderr, "SQL: '%s'\n", sql);
158 void sql_insert_return_states(int return_id, const char *return_ranges,
159 int type, int param, const char *key, const char *value)
161 if (key && strlen(key) >= 80)
162 return;
163 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
164 get_base_file(), get_function(), (unsigned long)__inline_fn,
165 return_id, return_ranges, fn_static(), type, param, key, value);
168 static struct string_list *common_funcs;
169 static int is_common_function(const char *fn)
171 char *tmp;
173 if (strncmp(fn, "__builtin_", 10) == 0)
174 return 1;
176 FOR_EACH_PTR(common_funcs, tmp) {
177 if (strcmp(tmp, fn) == 0)
178 return 1;
179 } END_FOR_EACH_PTR(tmp);
181 return 0;
184 void sql_insert_caller_info(struct expression *call, int type,
185 int param, const char *key, const char *value)
187 char *fn;
189 if (!option_info && !__inline_call)
190 return;
192 if (key && strlen(key) >= 80)
193 return;
195 fn = get_fnptr_name(call->fn);
196 if (!fn)
197 return;
199 if (__inline_call) {
200 mem_sql(NULL, NULL,
201 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
202 get_base_file(), get_function(), fn, (unsigned long)call,
203 is_static(call->fn), type, param, key, value);
206 if (!option_info)
207 return;
209 if (is_common_function(fn))
210 return;
212 sm_msg("SQL_caller_info: insert into caller_info values ("
213 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
214 get_base_file(), get_function(), fn, is_static(call->fn),
215 type, param, key, value);
217 free_string(fn);
220 void sql_insert_function_ptr(const char *fn, const char *struct_name)
222 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
223 struct_name);
226 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
228 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
229 get_function(), (unsigned long)__inline_fn, fn_static(),
230 type, param, key, value);
233 void sql_insert_function_type_size(const char *member, const char *ranges)
235 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
238 void sql_insert_local_values(const char *name, const char *value)
240 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
243 void sql_insert_function_type_value(const char *type, const char *value)
245 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
248 void sql_insert_function_type_info(int param, const char *value)
250 sql_insert(function_type_info, "'%s', '%s', %d, %d, '%s'",
251 get_base_file(), get_function(), fn_static(), param, value);
254 void sql_insert_data_info(struct expression *data, int type, const char *value)
256 char *data_name;
258 data_name = get_data_info_name(data);
259 if (!data_name)
260 return;
261 sql_insert(data_info, "'%s', '%s', %d, '%s'", get_base_file(), data_name, type, value);
264 char *get_static_filter(struct symbol *sym)
266 static char sql_filter[1024];
268 if (sym->ctype.modifiers & MOD_STATIC) {
269 snprintf(sql_filter, sizeof(sql_filter),
270 "file = '%s' and function = '%s' and static = '1'",
271 get_base_file(), sym->ident->name);
272 } else {
273 snprintf(sql_filter, sizeof(sql_filter),
274 "function = '%s' and static = '0'", sym->ident->name);
277 return sql_filter;
280 static int row_count;
281 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
283 if (argc != 1)
284 return 0;
285 row_count = atoi(argv[0]);
286 return 0;
289 static void sql_select_return_states_pointer(const char *cols,
290 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
292 char *ptr;
294 ptr = get_fnptr_name(call->fn);
295 if (!ptr)
296 return;
298 run_sql(callback, info,
299 "select %s from return_states join function_ptr where "
300 "return_states.function == function_ptr.function and ptr = '%s'"
301 "and searchable = 1 order by return_id, type;",
302 cols, ptr);
305 static int is_local_symbol(struct expression *expr)
307 if (expr->type != EXPR_SYMBOL)
308 return 0;
309 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
310 return 0;
311 return 1;
314 void sql_select_return_states(const char *cols, struct expression *call,
315 int (*callback)(void*, int, char**, char**), void *info)
317 if (is_fake_call(call))
318 return;
320 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
321 sql_select_return_states_pointer(cols, call, callback, info);
322 return;
325 if (inlinable(call->fn)) {
326 mem_sql(callback, info,
327 "select %s from return_states where call_id = '%lu' order by return_id, type;",
328 cols, (unsigned long)call);
329 return;
332 row_count = 0;
333 run_sql(get_row_count, info, "select count(*) from return_states where %s;",
334 get_static_filter(call->fn->symbol));
335 if (row_count > 3000)
336 return;
338 run_sql(callback, info, "select %s from return_states where %s order by return_id, type;",
339 cols, get_static_filter(call->fn->symbol));
342 void sql_select_call_implies(const char *cols, struct expression *call,
343 int (*callback)(void*, int, char**, char**))
345 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
346 return;
348 if (inlinable(call->fn)) {
349 mem_sql(callback, NULL,
350 "select %s from call_implies where call_id = '%lu';",
351 cols, (unsigned long)call);
352 return;
355 run_sql(callback, NULL, "select %s from call_implies where %s;",
356 cols, get_static_filter(call->fn->symbol));
359 void sql_select_caller_info(const char *cols, struct symbol *sym,
360 int (*callback)(void*, int, char**, char**))
362 if (__inline_fn) {
363 mem_sql(callback, NULL,
364 "select %s from caller_info where call_id = %lu;",
365 cols, (unsigned long)__inline_fn);
366 return;
369 run_sql(callback, NULL,
370 "select %s from caller_info where %s order by call_id;",
371 cols, get_static_filter(sym));
374 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
376 struct def_callback *def_callback = __alloc_def_callback(0);
378 def_callback->hook_type = type;
379 def_callback->callback = callback;
380 add_ptr_list(&callbacks, def_callback);
384 * These call backs are used when the --info option is turned on to print struct
385 * member information. For example foo->bar could have a state in
386 * smatch_extra.c and also check_user.c.
388 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
390 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
392 member_callback->owner = owner;
393 member_callback->callback = callback;
394 add_ptr_list(&member_callbacks, member_callback);
397 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
399 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
401 callback->callback = fn;
402 add_ptr_list(&returned_state_callbacks, callback);
405 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state))
407 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
409 member_callback->owner = owner;
410 member_callback->callback = callback;
411 add_ptr_list(&returned_member_callbacks, member_callback);
414 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
416 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
418 cb->type = type;
419 cb->callback = callback;
420 add_ptr_list(&call_implies_cb_list, cb);
423 static struct expression *static_call_expr;
424 static struct expression *static_returns_call;
425 static struct symbol *return_type;
426 static struct range_list *return_range_list;
427 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
429 struct range_list *rl;
430 struct expression *call_expr = static_returns_call;
432 if (argc != 1)
433 return 0;
434 call_results_to_rl(call_expr, return_type, argv[0], &rl);
435 return_range_list = rl_union(return_range_list, rl);
436 return 0;
439 struct range_list *db_return_vals(struct expression *expr)
441 char buf[64];
442 struct sm_state *sm;
444 if (is_fake_call(expr))
445 return NULL;
447 snprintf(buf, sizeof(buf), "return %p", expr);
448 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
449 if (sm)
450 return clone_rl(estate_rl(sm->state));
451 static_returns_call = expr;
452 return_type = get_type(expr);
453 if (!return_type)
454 return NULL;
456 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
457 return NULL;
459 return_range_list = NULL;
460 if (inlinable(expr->fn)) {
461 mem_sql(db_return_callback, NULL,
462 "select distinct return from return_states where call_id = '%lu';",
463 (unsigned long)expr);
464 } else {
465 run_sql(db_return_callback, NULL,
466 "select distinct return from return_states where %s;",
467 get_static_filter(expr->fn->symbol));
469 return return_range_list;
472 struct range_list *db_return_vals_from_str(const char *fn_name)
474 static_returns_call = NULL;
475 return_type = &llong_ctype;
477 return_range_list = NULL;
478 run_sql(db_return_callback, NULL,
479 "select distinct return from return_states where function = '%s';",
480 fn_name);
481 return return_range_list;
484 static void match_call_marker(struct expression *expr)
487 * we just want to record something in the database so that if we have
488 * two calls like: frob(4); frob(some_unkown); then on the receiving
489 * side we know that sometimes frob is called with unknown parameters.
492 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
495 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
496 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
498 struct sm_state *sm;
499 char *name;
500 struct symbol *sym;
501 int len;
502 char printed_name[256];
503 int is_address = 0;
505 expr = strip_expr(expr);
506 if (expr->type == EXPR_PREOP && expr->op == '&') {
507 expr = strip_expr(expr->unop);
508 is_address = 1;
511 name = expr_to_var_sym(expr, &sym);
512 if (!name || !sym)
513 goto free;
515 len = strlen(name);
516 FOR_EACH_SM(stree, sm) {
517 if (sm->sym != sym)
518 continue;
519 if (strcmp(name, sm->name) == 0) {
520 if (is_address)
521 snprintf(printed_name, sizeof(printed_name), "*$");
522 else /* these are already handled. fixme: handle them here */
523 continue;
524 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
525 snprintf(printed_name, sizeof(printed_name), "*$");
526 } else if (strncmp(name, sm->name, len) == 0) {
527 if (isalnum(sm->name[len]))
528 continue;
529 if (is_address)
530 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
531 else
532 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
533 } else {
534 continue;
536 callback(call, param, printed_name, sm);
537 } END_FOR_EACH_SM(sm);
538 free:
539 free_string(name);
542 static void match_call_info(struct expression *call)
544 struct member_info_callback *cb;
545 struct expression *arg;
546 struct stree *stree;
547 char *name;
548 int i;
550 name = get_fnptr_name(call->fn);
551 if (!name)
552 return;
554 FOR_EACH_PTR(member_callbacks, cb) {
555 stree = get_all_states_stree(cb->owner);
556 i = 0;
557 FOR_EACH_PTR(call->args, arg) {
558 print_struct_members(call, arg, i, stree, cb->callback);
559 i++;
560 } END_FOR_EACH_PTR(arg);
561 free_stree(&stree);
562 } END_FOR_EACH_PTR(cb);
564 free_string(name);
567 static int get_param(int param, char **name, struct symbol **sym)
569 struct symbol *arg;
570 int i;
572 i = 0;
573 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
575 * this is a temporary hack to work around a bug (I think in sparse?)
576 * 2.6.37-rc1:fs/reiserfs/journal.o
577 * If there is a function definition without parameter name found
578 * after a function implementation then it causes a crash.
579 * int foo() {}
580 * int bar(char *);
582 if (arg->ident->name < (char *)100)
583 continue;
584 if (i == param) {
585 *name = arg->ident->name;
586 *sym = arg;
587 return TRUE;
589 i++;
590 } END_FOR_EACH_PTR(arg);
592 return FALSE;
595 static struct stree *final_states;
596 static int prev_func_id = -1;
597 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
599 int func_id;
600 long type;
601 long param;
602 char *key;
603 char *value;
604 char *name = NULL;
605 struct symbol *sym = NULL;
606 struct def_callback *def_callback;
607 struct stree *stree;
609 if (argc != 5)
610 return 0;
612 func_id = atoi(argv[0]);
613 errno = 0;
614 type = strtol(argv[1], NULL, 10);
615 param = strtol(argv[2], NULL, 10);
616 if (errno)
617 return 0;
618 key = argv[3];
619 value = argv[4];
622 if (prev_func_id == -1)
623 prev_func_id = func_id;
624 if (func_id != prev_func_id) {
625 stree = __pop_fake_cur_stree();
626 merge_stree(&final_states, stree);
627 free_stree(&stree);
628 __push_fake_cur_stree();
629 __unnullify_path();
630 prev_func_id = func_id;
633 if (param >= 0 && !get_param(param, &name, &sym))
634 return 0;
636 FOR_EACH_PTR(callbacks, def_callback) {
637 if (def_callback->hook_type == type)
638 def_callback->callback(name, sym, key, value);
639 } END_FOR_EACH_PTR(def_callback);
641 return 0;
644 static void get_direct_callers(struct symbol *sym)
646 sql_select_caller_info("call_id, type, parameter, key, value", sym,
647 caller_info_callback);
650 static struct string_list *ptr_names_done;
651 static struct string_list *ptr_names;
653 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
655 insert_string(&ptr_names, alloc_string(argv[0]));
656 return 0;
659 static char *get_next_ptr_name(void)
661 char *ptr;
663 FOR_EACH_PTR(ptr_names, ptr) {
664 if (list_has_string(ptr_names_done, ptr))
665 continue;
666 insert_string(&ptr_names_done, ptr);
667 return ptr;
668 } END_FOR_EACH_PTR(ptr);
669 return NULL;
672 static void get_ptr_names(const char *file, const char *name)
674 char sql_filter[1024];
675 int before, after;
677 if (file) {
678 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
679 file, name);
680 } else {
681 snprintf(sql_filter, 1024, "function = '%s';", name);
684 before = ptr_list_size((struct ptr_list *)ptr_names);
686 run_sql(get_ptr_name, NULL,
687 "select distinct ptr from function_ptr where %s",
688 sql_filter);
690 after = ptr_list_size((struct ptr_list *)ptr_names);
691 if (before == after)
692 return;
694 while ((name = get_next_ptr_name()))
695 get_ptr_names(NULL, name);
698 static void match_data_from_db(struct symbol *sym)
700 struct sm_state *sm;
701 struct stree *stree;
703 if (!sym || !sym->ident)
704 return;
706 __push_fake_cur_stree();
707 __unnullify_path();
708 prev_func_id = -1;
710 if (!__inline_fn) {
711 char *ptr;
713 if (sym->ctype.modifiers & MOD_STATIC)
714 get_ptr_names(get_base_file(), sym->ident->name);
715 else
716 get_ptr_names(NULL, sym->ident->name);
718 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
719 __free_ptr_list((struct ptr_list **)&ptr_names);
720 __free_ptr_list((struct ptr_list **)&ptr_names_done);
721 stree = __pop_fake_cur_stree();
722 free_stree(&stree);
723 return;
726 get_direct_callers(sym);
728 FOR_EACH_PTR(ptr_names, ptr) {
729 run_sql(caller_info_callback, NULL,
730 "select call_id, type, parameter, key, value"
731 " from caller_info where function = '%s' order by call_id",
732 ptr);
733 free_string(ptr);
734 } END_FOR_EACH_PTR(ptr);
736 __free_ptr_list((struct ptr_list **)&ptr_names);
737 __free_ptr_list((struct ptr_list **)&ptr_names_done);
738 } else {
739 get_direct_callers(sym);
742 stree = __pop_fake_cur_stree();
743 merge_stree(&final_states, stree);
744 free_stree(&stree);
746 FOR_EACH_SM(final_states, sm) {
747 __set_sm(sm);
748 } END_FOR_EACH_SM(sm);
750 free_stree(&final_states);
753 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
755 struct expression *call_expr = static_call_expr;
756 struct call_implies_callback *cb;
757 struct expression *arg = NULL;
758 int type;
759 int param;
761 if (argc != 5)
762 return 0;
764 type = atoi(argv[1]);
765 param = atoi(argv[2]);
767 FOR_EACH_PTR(call_implies_cb_list, cb) {
768 if (cb->type != type)
769 continue;
770 if (param != -1) {
771 arg = get_argument_from_call_expr(call_expr->args, param);
772 if (!arg)
773 continue;
775 cb->callback(arg, argv[3], argv[4]);
776 } END_FOR_EACH_PTR(cb);
778 return 0;
781 static void match_call_implies(struct expression *expr)
783 static_call_expr = expr;
784 sql_select_call_implies("function, type, parameter, key, value", expr,
785 call_implies_callbacks);
788 static void print_initializer_list(struct expression_list *expr_list,
789 struct symbol *struct_type)
791 struct expression *expr;
792 struct symbol *base_type;
793 char struct_name[256];
795 FOR_EACH_PTR(expr_list, expr) {
796 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
797 print_initializer_list(expr->idx_expression->expr_list, struct_type);
798 continue;
800 if (expr->type != EXPR_IDENTIFIER)
801 continue;
802 if (!expr->expr_ident)
803 continue;
804 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
805 continue;
806 base_type = get_type(expr->ident_expression);
807 if (!base_type || base_type->type != SYM_FN)
808 continue;
809 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
810 struct_type->ident->name, expr->expr_ident->name);
811 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
812 struct_name);
813 } END_FOR_EACH_PTR(expr);
816 static void global_variable(struct symbol *sym)
818 struct symbol *struct_type;
820 if (!sym->ident)
821 return;
822 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
823 return;
824 struct_type = get_base_type(sym);
825 if (!struct_type)
826 return;
827 if (struct_type->type == SYM_ARRAY) {
828 struct_type = get_base_type(struct_type);
829 if (!struct_type)
830 return;
832 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
833 return;
834 print_initializer_list(sym->initializer->expr_list, struct_type);
837 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
839 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
842 static void call_return_state_hooks_conditional(struct expression *expr)
844 struct returned_state_callback *cb;
845 struct range_list *rl;
846 char *return_ranges;
847 int final_pass_orig = final_pass;
849 __push_fake_cur_stree();
851 final_pass = 0;
852 __split_whole_condition(expr->conditional);
853 final_pass = final_pass_orig;
855 if (get_implied_rl(expr->cond_true, &rl))
856 rl = cast_rl(cur_func_return_type(), rl);
857 else
858 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
859 return_ranges = show_rl(rl);
860 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
862 return_id++;
863 FOR_EACH_PTR(returned_state_callbacks, cb) {
864 cb->callback(return_id, return_ranges, expr);
865 } END_FOR_EACH_PTR(cb);
867 __push_true_states();
868 __use_false_states();
870 if (get_implied_rl(expr->cond_false, &rl))
871 rl = cast_rl(cur_func_return_type(), rl);
872 else
873 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
874 return_ranges = show_rl(rl);
875 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
877 return_id++;
878 FOR_EACH_PTR(returned_state_callbacks, cb) {
879 cb->callback(return_id, return_ranges, expr);
880 } END_FOR_EACH_PTR(cb);
882 __merge_true_states();
883 __free_fake_cur_stree();
886 static void call_return_state_hooks_compare(struct expression *expr)
888 struct returned_state_callback *cb;
889 char *return_ranges;
890 int final_pass_orig = final_pass;
891 sval_t sval = { .type = &int_ctype };
893 __push_fake_cur_stree();
895 final_pass = 0;
896 __split_whole_condition(expr);
897 final_pass = final_pass_orig;
899 return_ranges = alloc_sname("1");
900 sval.value = 1;
901 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
903 return_id++;
904 FOR_EACH_PTR(returned_state_callbacks, cb) {
905 cb->callback(return_id, return_ranges, expr);
906 } END_FOR_EACH_PTR(cb);
908 __push_true_states();
909 __use_false_states();
911 return_ranges = alloc_sname("0");
912 sval.value = 0;
913 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
914 return_id++;
915 FOR_EACH_PTR(returned_state_callbacks, cb) {
916 cb->callback(return_id, return_ranges, expr);
917 } END_FOR_EACH_PTR(cb);
919 __merge_true_states();
920 __free_fake_cur_stree();
923 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
925 struct sm_state *tmp;
927 FOR_EACH_PTR(slist, tmp) {
928 if (strcmp(tmp->state->name, sm->state->name) == 0)
929 return 1;
930 } END_FOR_EACH_PTR(tmp);
932 return 0;
935 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
937 struct returned_state_callback *cb;
938 struct range_list *rl;
939 char *return_ranges;
940 struct sm_state *tmp;
941 int ret = 0;
942 int nr_possible, nr_states;
943 char *compare_str;
944 char buf[128];
945 struct state_list *already_handled = NULL;
947 if (!sm || !sm->merged)
948 return 0;
950 if (too_many_possible(sm))
951 return 0;
953 /* bail if it gets too complicated */
954 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
955 nr_states = stree_count(__get_cur_stree());
956 if (option_info && nr_states * nr_possible >= 2000)
957 return 0;
959 FOR_EACH_PTR(sm->possible, tmp) {
960 if (tmp->merged)
961 continue;
962 if (ptr_in_list(tmp, already_handled))
963 continue;
964 add_ptr_list(&already_handled, tmp);
966 ret = 1;
967 __push_fake_cur_stree();
969 overwrite_states_using_pool(sm, tmp);
971 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
972 return_ranges = show_rl(rl);
973 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
974 compare_str = expr_lte_to_param(expr, -1);
975 if (compare_str) {
976 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
977 return_ranges = alloc_sname(buf);
980 return_id++;
981 FOR_EACH_PTR(returned_state_callbacks, cb) {
982 cb->callback(return_id, return_ranges, expr);
983 } END_FOR_EACH_PTR(cb);
985 __free_fake_cur_stree();
986 } END_FOR_EACH_PTR(tmp);
988 free_slist(&already_handled);
990 return ret;
993 static int call_return_state_hooks_split_possible(struct expression *expr)
995 struct sm_state *sm;
997 if (!expr || expr_equal_to_param(expr, -1))
998 return 0;
1000 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1001 return split_possible_helper(sm, expr);
1004 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1006 struct range_list *rl;
1007 char *return_ranges;
1008 sval_t sval;
1009 char *compare_str;
1010 char *math_str;
1011 char buf[128];
1013 *rl_p = NULL;
1015 if (!expr)
1016 return alloc_sname("");
1018 if (get_implied_value(expr, &sval)) {
1019 *rl_p = alloc_rl(sval, sval);
1020 return sval_to_str(sval);
1023 compare_str = expr_equal_to_param(expr, -1);
1024 math_str = get_value_in_terms_of_parameter_math(expr);
1026 if (get_implied_rl(expr, &rl)) {
1027 rl = cast_rl(cur_func_return_type(), rl);
1028 return_ranges = show_rl(rl);
1029 } else if (get_imaginary_absolute(expr, &rl)){
1030 rl = cast_rl(cur_func_return_type(), rl);
1031 return alloc_sname(show_rl(rl));
1032 } else {
1033 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1034 return_ranges = show_rl(rl);
1036 *rl_p = rl;
1038 if (compare_str) {
1039 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1040 return alloc_sname(buf);
1043 if (math_str) {
1044 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1045 return alloc_sname(buf);
1048 compare_str = expr_lte_to_param(expr, -1);
1049 if (compare_str) {
1050 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1051 return alloc_sname(buf);
1053 return return_ranges;
1056 static int split_positive_from_negative(struct expression *expr)
1058 struct returned_state_callback *cb;
1059 struct range_list *rl;
1060 const char *return_ranges;
1061 struct range_list *ret_rl;
1062 int undo;
1064 /* We're going to print the states 3 times */
1065 if (stree_count(__get_cur_stree()) > 10000 / 3)
1066 return 0;
1068 if (!get_implied_rl(expr, &rl) || !rl)
1069 return 0;
1070 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1071 return 0;
1072 /* Forget about INT_MAX and larger */
1073 if (rl_max(rl).value <= 0)
1074 return 0;
1075 if (!sval_is_negative(rl_min(rl)))
1076 return 0;
1078 if (!assume(compare_expression(expr, '>', zero_expr())))
1079 return 0;
1081 return_id++;
1082 return_ranges = get_return_ranges_str(expr, &ret_rl);
1083 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1084 FOR_EACH_PTR(returned_state_callbacks, cb) {
1085 cb->callback(return_id, (char *)return_ranges, expr);
1086 } END_FOR_EACH_PTR(cb);
1088 end_assume();
1090 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1091 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1093 return_id++;
1094 return_ranges = get_return_ranges_str(expr, &ret_rl);
1095 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1096 FOR_EACH_PTR(returned_state_callbacks, cb) {
1097 cb->callback(return_id, (char *)return_ranges, expr);
1098 } END_FOR_EACH_PTR(cb);
1100 if (undo)
1101 end_assume();
1104 undo = assume(compare_expression(expr, '<', zero_expr()));
1106 return_id++;
1107 return_ranges = get_return_ranges_str(expr, &ret_rl);
1108 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1109 FOR_EACH_PTR(returned_state_callbacks, cb) {
1110 cb->callback(return_id, (char *)return_ranges, expr);
1111 } END_FOR_EACH_PTR(cb);
1113 if (undo)
1114 end_assume();
1116 return 1;
1119 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1121 struct returned_state_callback *cb;
1122 struct range_list *rl;
1123 struct range_list *nonnull_rl;
1124 sval_t null_sval;
1125 struct range_list *null_rl = NULL;
1126 char *return_ranges;
1127 struct smatch_state *state;
1128 int nr_states;
1129 int final_pass_orig = final_pass;
1131 if (!expr || expr_equal_to_param(expr, -1))
1132 return 0;
1133 if (expr->type == EXPR_CALL)
1134 return 0;
1135 if (!is_pointer(expr))
1136 return 0;
1138 state = get_state_expr(SMATCH_EXTRA, expr);
1139 if (!state || !estate_rl(state))
1140 return 0;
1141 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1142 return 0;
1143 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1144 return 0;
1146 nr_states = stree_count(__get_cur_stree());
1147 if (option_info && nr_states >= 1500)
1148 return 0;
1150 rl = estate_rl(state);
1152 __push_fake_cur_stree();
1154 final_pass = 0;
1155 __split_whole_condition(expr);
1156 final_pass = final_pass_orig;
1158 nonnull_rl = rl_filter(rl, rl_zero());
1159 return_ranges = show_rl(nonnull_rl);
1160 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1162 return_id++;
1163 FOR_EACH_PTR(returned_state_callbacks, cb) {
1164 cb->callback(return_id, return_ranges, expr);
1165 } END_FOR_EACH_PTR(cb);
1167 __push_true_states();
1168 __use_false_states();
1170 return_ranges = alloc_sname("0");
1171 null_sval = sval_type_val(rl_type(rl), 0);
1172 add_range(&null_rl, null_sval, null_sval);
1173 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1174 return_id++;
1175 FOR_EACH_PTR(returned_state_callbacks, cb) {
1176 cb->callback(return_id, return_ranges, expr);
1177 } END_FOR_EACH_PTR(cb);
1179 __merge_true_states();
1180 __free_fake_cur_stree();
1182 return 1;
1185 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1187 struct range_list *rl;
1188 struct range_list *nonzero_rl;
1189 sval_t zero_sval;
1190 struct range_list *zero_rl = NULL;
1191 int nr_states;
1192 struct returned_state_callback *cb;
1193 char *return_ranges;
1194 int final_pass_orig = final_pass;
1195 sval_t val;
1197 if (option_project != PROJ_KERNEL)
1198 return 0;
1200 nr_states = stree_count(__get_cur_stree());
1201 if (nr_states > 1500)
1202 return 0;
1204 if (get_value(expr, &val))
1205 return 0;
1206 if (!get_implied_rl(expr, &rl))
1207 return 0;
1208 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1209 return 0;
1210 if (rl_max(rl).value != 0)
1211 return 0;
1213 __push_fake_cur_stree();
1215 final_pass = 0;
1216 __split_whole_condition(expr);
1217 final_pass = final_pass_orig;
1219 nonzero_rl = rl_filter(rl, rl_zero());
1220 return_ranges = show_rl(nonzero_rl);
1221 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1223 return_id++;
1224 FOR_EACH_PTR(returned_state_callbacks, cb) {
1225 cb->callback(return_id, return_ranges, expr);
1226 } END_FOR_EACH_PTR(cb);
1228 __push_true_states();
1229 __use_false_states();
1231 return_ranges = alloc_sname("0");
1232 zero_sval = sval_type_val(rl_type(rl), 0);
1233 add_range(&zero_rl, zero_sval, zero_sval);
1234 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1235 return_id++;
1236 FOR_EACH_PTR(returned_state_callbacks, cb) {
1237 cb->callback(return_id, return_ranges, expr);
1238 } END_FOR_EACH_PTR(cb);
1240 __merge_true_states();
1241 __free_fake_cur_stree();
1243 return 1;
1246 static int is_boolean(struct expression *expr)
1248 struct range_list *rl;
1250 if (!get_implied_rl(expr, &rl))
1251 return 0;
1252 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1253 return 1;
1254 return 0;
1257 static int is_conditional(struct expression *expr)
1259 if (!expr)
1260 return 0;
1261 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1262 return 1;
1263 return 0;
1266 static int splitable_function_call(struct expression *expr)
1268 struct sm_state *sm;
1269 char buf[64];
1271 if (!expr || expr->type != EXPR_CALL)
1272 return 0;
1273 snprintf(buf, sizeof(buf), "return %p", expr);
1274 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1275 return split_possible_helper(sm, expr);
1278 static void call_return_state_hooks(struct expression *expr)
1280 struct returned_state_callback *cb;
1281 struct range_list *ret_rl;
1282 const char *return_ranges;
1283 int nr_states;
1284 sval_t sval;
1286 if (__path_is_null())
1287 return;
1289 expr = strip_expr(expr);
1291 if (is_impossible_path())
1292 goto vanilla;
1294 if (!get_implied_value(expr, &sval) &&
1295 (is_condition(expr) || is_boolean(expr))) {
1296 call_return_state_hooks_compare(expr);
1297 return;
1298 } else if (is_conditional(expr)) {
1299 call_return_state_hooks_conditional(expr);
1300 return;
1301 } else if (call_return_state_hooks_split_possible(expr)) {
1302 return;
1303 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1304 return;
1305 } else if (call_return_state_hooks_split_success_fail(expr)) {
1306 return;
1307 } else if (splitable_function_call(expr)) {
1308 return;
1309 } else if (split_positive_from_negative(expr)) {
1310 return;
1313 vanilla:
1314 return_ranges = get_return_ranges_str(expr, &ret_rl);
1315 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1317 return_id++;
1318 nr_states = stree_count(__get_cur_stree());
1319 if (nr_states >= 10000) {
1320 match_return_info(return_id, (char *)return_ranges, expr);
1321 return;
1323 FOR_EACH_PTR(returned_state_callbacks, cb) {
1324 cb->callback(return_id, (char *)return_ranges, expr);
1325 } END_FOR_EACH_PTR(cb);
1328 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1330 struct returned_member_callback *cb;
1331 struct stree *stree;
1332 struct sm_state *sm;
1333 struct symbol *type;
1334 char *name;
1335 char member_name[256];
1336 int len;
1338 type = get_type(expr);
1339 if (!type || type->type != SYM_PTR)
1340 return;
1341 name = expr_to_var(expr);
1342 if (!name)
1343 return;
1345 member_name[sizeof(member_name) - 1] = '\0';
1346 strcpy(member_name, "$");
1348 len = strlen(name);
1349 FOR_EACH_PTR(returned_member_callbacks, cb) {
1350 stree = __get_cur_stree();
1351 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1352 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1353 strcpy(member_name, "*$");
1354 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1355 continue;
1357 if (strncmp(sm->name, name, len) != 0)
1358 continue;
1359 if (strncmp(sm->name + len, "->", 2) != 0)
1360 continue;
1361 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1362 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1363 } END_FOR_EACH_SM(sm);
1364 } END_FOR_EACH_PTR(cb);
1366 free_string(name);
1369 static void reset_memdb(struct symbol *sym)
1371 mem_sql(NULL, NULL, "delete from caller_info;");
1372 mem_sql(NULL, NULL, "delete from return_states;");
1373 mem_sql(NULL, NULL, "delete from call_implies;");
1376 static void match_end_func_info(struct symbol *sym)
1378 if (__path_is_null())
1379 return;
1380 call_return_state_hooks(NULL);
1383 static void match_after_func(struct symbol *sym)
1385 if (!__inline_fn)
1386 reset_memdb(sym);
1389 static void init_memdb(void)
1391 char *err = NULL;
1392 int rc;
1393 const char *schema_files[] = {
1394 "db/db.schema",
1395 "db/caller_info.schema",
1396 "db/return_states.schema",
1397 "db/function_type_size.schema",
1398 "db/type_size.schema",
1399 "db/call_implies.schema",
1400 "db/function_ptr.schema",
1401 "db/local_values.schema",
1402 "db/function_type_value.schema",
1403 "db/type_value.schema",
1404 "db/function_type_info.schema",
1405 "db/data_info.schema",
1407 static char buf[4096];
1408 int fd;
1409 int ret;
1410 int i;
1412 rc = sqlite3_open(":memory:", &mem_db);
1413 if (rc != SQLITE_OK) {
1414 printf("Error starting In-Memory database.");
1415 return;
1418 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1419 fd = open_data_file(schema_files[i]);
1420 if (fd < 0) {
1421 mem_db = NULL;
1422 return;
1424 ret = read(fd, buf, sizeof(buf));
1425 if (ret == sizeof(buf)) {
1426 printf("Schema file too large: %s (limit %zd bytes)",
1427 schema_files[i], sizeof(buf));
1429 buf[ret] = '\0';
1430 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1431 if (rc != SQLITE_OK) {
1432 fprintf(stderr, "SQL error #2: %s\n", err);
1433 fprintf(stderr, "%s\n", buf);
1438 void open_smatch_db(void)
1440 int rc;
1442 if (option_no_db)
1443 return;
1445 init_memdb();
1447 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1448 if (rc != SQLITE_OK) {
1449 option_no_db = 1;
1450 return;
1452 return;
1455 static void register_common_funcs(void)
1457 struct token *token;
1458 char *func;
1459 char filename[256];
1461 if (option_project == PROJ_NONE)
1462 strcpy(filename, "common_functions");
1463 else
1464 snprintf(filename, 256, "%s.common_functions", option_project_str);
1466 token = get_tokens_file(filename);
1467 if (!token)
1468 return;
1469 if (token_type(token) != TOKEN_STREAMBEGIN)
1470 return;
1471 token = token->next;
1472 while (token_type(token) != TOKEN_STREAMEND) {
1473 if (token_type(token) != TOKEN_IDENT)
1474 return;
1475 func = alloc_string(show_ident(token->ident));
1476 add_ptr_list(&common_funcs, func);
1477 token = token->next;
1479 clear_token_alloc();
1483 void register_definition_db_callbacks(int id)
1485 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1486 add_hook(&global_variable, BASE_HOOK);
1487 add_hook(&global_variable, DECLARATION_HOOK);
1488 add_split_return_callback(match_return_info);
1489 add_split_return_callback(print_returned_struct_members);
1490 add_hook(&call_return_state_hooks, RETURN_HOOK);
1491 add_hook(&match_end_func_info, END_FUNC_HOOK);
1492 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1494 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1495 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1497 register_common_funcs();
1500 void register_db_call_marker(int id)
1502 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1505 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
1507 struct expression *arg;
1508 char *name = NULL;
1509 char member_name[256];
1511 *sym = NULL;
1513 if (param == -1) {
1514 const char *star = "";
1516 if (expr->type != EXPR_ASSIGNMENT)
1517 return NULL;
1518 name = expr_to_var_sym(expr->left, sym);
1519 if (!name)
1520 return NULL;
1521 if (key[0] == '*') {
1522 star = "*";
1523 key++;
1525 if (strncmp(key, "$", 1) != 0)
1526 return name;
1527 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1528 free_string(name);
1529 return alloc_string(member_name);
1532 while (expr->type == EXPR_ASSIGNMENT)
1533 expr = strip_expr(expr->right);
1534 if (expr->type != EXPR_CALL)
1535 return NULL;
1537 arg = get_argument_from_call_expr(expr->args, param);
1538 if (!arg)
1539 return NULL;
1541 return get_variable_from_key(arg, key, sym);
1544 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
1546 char buf[256];
1547 char *tmp;
1549 if (!arg)
1550 return NULL;
1552 arg = strip_expr(arg);
1554 if (strcmp(key, "$") == 0)
1555 return expr_to_var_sym(arg, sym);
1557 if (strcmp(key, "*$") == 0) {
1558 if (arg->type == EXPR_PREOP && arg->op == '&') {
1559 arg = strip_expr(arg->unop);
1560 return expr_to_var_sym(arg, sym);
1561 } else {
1562 tmp = expr_to_var_sym(arg, sym);
1563 if (!tmp)
1564 return NULL;
1565 snprintf(buf, sizeof(buf), "*%s", tmp);
1566 free_string(tmp);
1567 return alloc_string(buf);
1571 if (arg->type == EXPR_PREOP && arg->op == '&') {
1572 arg = strip_expr(arg->unop);
1573 tmp = expr_to_var_sym(arg, sym);
1574 if (!tmp)
1575 return NULL;
1576 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1577 return alloc_string(buf);
1580 tmp = expr_to_var_sym(arg, sym);
1581 if (!tmp)
1582 return NULL;
1583 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1584 free_string(tmp);
1585 return alloc_string(buf);
1588 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
1590 *vsl = NULL;
1592 if (strcmp("$", key) == 0)
1593 return expr_to_chunk_sym_vsl(arg, sym, vsl);
1594 return get_variable_from_key(arg, key, sym);
1597 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
1599 char *param_name;
1600 int name_len;
1601 static char buf[256];
1603 if (!sym || !sym->ident)
1604 return NULL;
1606 param_name = sym->ident->name;
1607 name_len = strlen(param_name);
1609 if (strcmp(name, param_name) == 0) {
1610 return "$";
1611 } else if (name[name_len] == '-' && /* check for '-' from "->" */
1612 strncmp(name, param_name, name_len) == 0) {
1613 snprintf(buf, sizeof(buf), "$%s", name + name_len);
1614 return buf;
1615 } else if (name[0] == '*' && strcmp(name + 1, param_name) == 0) {
1616 return "*$";
1618 return NULL;
1621 const char *get_param_name(struct sm_state *sm)
1623 return get_param_name_var_sym(sm->name, sm->sym);
1626 char *get_data_info_name(struct expression *expr)
1628 struct symbol *sym;
1629 char *name;
1630 char buf[256];
1631 char *ret = NULL;
1633 expr = strip_expr(expr);
1634 name = get_member_name(expr);
1635 if (name)
1636 return name;
1637 name = expr_to_var_sym(expr, &sym);
1638 if (!name || !sym)
1639 goto free;
1640 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1641 goto free;
1642 if (sym->ctype.modifiers & MOD_STATIC)
1643 snprintf(buf, sizeof(buf), "static %s", name);
1644 else
1645 snprintf(buf, sizeof(buf), "global %s", name);
1646 ret = alloc_sname(buf);
1647 free:
1648 free_string(name);
1649 return ret;