debug: print the max size for __smatch_buf_size()
[smatch.git] / smatch_db.c
blob86c07ef00fc00b1b3e19cc2abd383637d3edea09
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);
861 return_id++;
862 FOR_EACH_PTR(returned_state_callbacks, cb) {
863 cb->callback(return_id, return_ranges, expr);
864 } END_FOR_EACH_PTR(cb);
866 __push_true_states();
867 __use_false_states();
869 if (get_implied_rl(expr->cond_false, &rl))
870 rl = cast_rl(cur_func_return_type(), rl);
871 else
872 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
873 return_ranges = show_rl(rl);
875 return_id++;
876 FOR_EACH_PTR(returned_state_callbacks, cb) {
877 cb->callback(return_id, return_ranges, expr);
878 } END_FOR_EACH_PTR(cb);
880 __merge_true_states();
881 __free_fake_cur_stree();
884 static void call_return_state_hooks_compare(struct expression *expr)
886 struct returned_state_callback *cb;
887 char *return_ranges;
888 int final_pass_orig = final_pass;
890 __push_fake_cur_stree();
892 final_pass = 0;
893 __split_whole_condition(expr);
894 final_pass = final_pass_orig;
896 return_ranges = alloc_sname("1");
898 return_id++;
899 FOR_EACH_PTR(returned_state_callbacks, cb) {
900 cb->callback(return_id, return_ranges, expr);
901 } END_FOR_EACH_PTR(cb);
903 __push_true_states();
904 __use_false_states();
906 return_ranges = alloc_sname("0");;
907 return_id++;
908 FOR_EACH_PTR(returned_state_callbacks, cb) {
909 cb->callback(return_id, return_ranges, expr);
910 } END_FOR_EACH_PTR(cb);
912 __merge_true_states();
913 __free_fake_cur_stree();
916 static int split_helper(struct sm_state *sm, struct expression *expr)
918 struct returned_state_callback *cb;
919 struct range_list *rl;
920 char *return_ranges;
921 struct sm_state *tmp;
922 int ret = 0;
923 int nr_possible, nr_states;
924 char *compare_str;
925 char buf[128];
926 struct stree *orig_stree;
927 struct sm_state *orig_sm;
929 if (!sm || !sm->merged)
930 return 0;
932 if (too_many_possible(sm))
933 return 0;
935 /* bail if it gets too complicated */
936 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
937 nr_states = stree_count(__get_cur_stree());
939 * the main thing option_info because we don't want to print a
940 * million lines of output. If someone else, like check_locking.c
941 * wants this data, then it doesn't cause a slow down to provide it.
943 if (option_info && nr_states * nr_possible >= 2000)
944 return 0;
946 compare_str = expr_lte_to_param(expr, -1);
948 orig_stree = clone_stree(__get_cur_stree());
949 FOR_EACH_PTR(sm->possible, tmp) {
950 if (tmp->merged)
951 continue;
953 ret = 1;
954 __push_fake_cur_stree();
956 nullify_path();
957 __unnullify_path();
958 FOR_EACH_SM(orig_stree, orig_sm) {
959 __set_sm_cur_stree(orig_sm);
960 } END_FOR_EACH_SM(orig_sm);
962 overwrite_states_using_pool(tmp);
964 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
965 return_ranges = show_rl(rl);
966 if (compare_str) {
967 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
968 return_ranges = alloc_sname(buf);
971 return_id++;
972 FOR_EACH_PTR(returned_state_callbacks, cb) {
973 cb->callback(return_id, return_ranges, expr);
974 } END_FOR_EACH_PTR(cb);
976 __free_fake_cur_stree();
977 } END_FOR_EACH_PTR(tmp);
979 free_stree(&orig_stree);
980 return ret;
983 static const char *get_return_ranges_str(struct expression *expr)
985 struct range_list *rl;
986 char *return_ranges;
987 sval_t sval;
988 char *compare_str;
989 char *math_str;
990 char buf[128];
992 if (!expr)
993 return alloc_sname("");
995 if (get_implied_value(expr, &sval))
996 return sval_to_str(sval);
998 compare_str = expr_equal_to_param(expr, -1);
999 math_str = get_value_in_terms_of_parameter_math(expr);
1001 if (get_implied_rl(expr, &rl)) {
1002 rl = cast_rl(cur_func_return_type(), rl);
1003 return_ranges = show_rl(rl);
1004 } else if (get_imaginary_absolute(expr, &rl)){
1005 rl = cast_rl(cur_func_return_type(), rl);
1006 return alloc_sname(show_rl(rl));
1007 } else {
1008 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1009 return_ranges = show_rl(rl);
1012 if (compare_str) {
1013 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1014 return alloc_sname(buf);
1017 if (math_str) {
1018 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1019 return alloc_sname(buf);
1022 compare_str = expr_lte_to_param(expr, -1);
1023 if (compare_str) {
1024 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1025 return alloc_sname(buf);
1027 return return_ranges;
1030 static int split_positive_from_negative(struct expression *expr)
1032 struct returned_state_callback *cb;
1033 struct range_list *rl;
1034 const char *return_ranges;
1035 int undo;
1037 /* We're going to print the states 3 times */
1038 if (stree_count(__get_cur_stree()) > 10000 / 3)
1039 return 0;
1041 if (!get_implied_rl(expr, &rl) || !rl)
1042 return 0;
1043 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1044 return 0;
1045 /* Forget about INT_MAX and larger */
1046 if (rl_max(rl).value <= 0)
1047 return 0;
1048 if (!sval_is_negative(rl_min(rl)))
1049 return 0;
1051 if (!assume(compare_expression(expr, '>', zero_expr())))
1052 return 0;
1054 return_id++;
1055 return_ranges = get_return_ranges_str(expr);
1056 FOR_EACH_PTR(returned_state_callbacks, cb) {
1057 cb->callback(return_id, (char *)return_ranges, expr);
1058 } END_FOR_EACH_PTR(cb);
1060 end_assume();
1062 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1063 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1065 return_id++;
1066 return_ranges = get_return_ranges_str(expr);
1067 FOR_EACH_PTR(returned_state_callbacks, cb) {
1068 cb->callback(return_id, (char *)return_ranges, expr);
1069 } END_FOR_EACH_PTR(cb);
1071 if (undo)
1072 end_assume();
1075 undo = assume(compare_expression(expr, '<', zero_expr()));
1077 return_id++;
1078 return_ranges = get_return_ranges_str(expr);
1079 FOR_EACH_PTR(returned_state_callbacks, cb) {
1080 cb->callback(return_id, (char *)return_ranges, expr);
1081 } END_FOR_EACH_PTR(cb);
1083 if (undo)
1084 end_assume();
1086 return 1;
1089 static int call_return_state_hooks_split_possible(struct expression *expr)
1091 struct returned_state_callback *cb;
1092 struct range_list *rl;
1093 char *return_ranges;
1094 struct sm_state *sm;
1095 struct sm_state *tmp;
1096 int ret = 0;
1097 int nr_possible, nr_states;
1098 char *compare_str;
1099 char buf[128];
1101 if (!expr || expr_equal_to_param(expr, -1))
1102 return 0;
1104 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1105 if (!sm || !sm->merged)
1106 return 0;
1108 if (too_many_possible(sm))
1109 return 0;
1111 /* bail if it gets too complicated */
1112 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1113 nr_states = stree_count(__get_cur_stree());
1115 * the main thing option_info because we don't want to print a
1116 * million lines of output. If someone else, like check_locking.c
1117 * wants this data, then it doesn't cause a slow down to provide it.
1119 if (option_info && nr_states * nr_possible >= 2000)
1120 return 0;
1123 FOR_EACH_PTR(sm->possible, tmp) {
1124 if (tmp->merged)
1125 continue;
1127 ret = 1;
1128 __push_fake_cur_stree();
1130 overwrite_states_using_pool(tmp);
1132 if (split_positive_from_negative(expr)) {
1133 __free_fake_cur_stree();
1134 continue;
1136 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1137 return_ranges = show_rl(rl);
1139 compare_str = expr_lte_to_param(expr, -1);
1140 if (compare_str) {
1141 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1142 return_ranges = alloc_sname(buf);
1145 return_id++;
1146 FOR_EACH_PTR(returned_state_callbacks, cb) {
1147 cb->callback(return_id, return_ranges, expr);
1148 } END_FOR_EACH_PTR(cb);
1150 __free_fake_cur_stree();
1151 } END_FOR_EACH_PTR(tmp);
1153 return ret;
1156 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1158 struct returned_state_callback *cb;
1159 struct range_list *rl;
1160 char *return_ranges;
1161 struct smatch_state *state;
1162 int nr_states;
1163 int final_pass_orig = final_pass;
1165 if (!expr || expr_equal_to_param(expr, -1))
1166 return 0;
1167 if (expr->type == EXPR_CALL)
1168 return 0;
1169 if (!is_pointer(expr))
1170 return 0;
1172 state = get_state_expr(SMATCH_EXTRA, expr);
1173 if (!state || !estate_rl(state))
1174 return 0;
1175 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1176 return 0;
1177 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1178 return 0;
1180 nr_states = stree_count(__get_cur_stree());
1181 if (option_info && nr_states >= 1500)
1182 return 0;
1184 rl = estate_rl(state);
1186 __push_fake_cur_stree();
1188 final_pass = 0;
1189 __split_whole_condition(expr);
1190 final_pass = final_pass_orig;
1192 return_ranges = show_rl(rl_filter(rl, rl_zero()));
1194 return_id++;
1195 FOR_EACH_PTR(returned_state_callbacks, cb) {
1196 cb->callback(return_id, return_ranges, expr);
1197 } END_FOR_EACH_PTR(cb);
1199 __push_true_states();
1200 __use_false_states();
1202 return_ranges = alloc_sname("0");;
1203 return_id++;
1204 FOR_EACH_PTR(returned_state_callbacks, cb) {
1205 cb->callback(return_id, return_ranges, expr);
1206 } END_FOR_EACH_PTR(cb);
1208 __merge_true_states();
1209 __free_fake_cur_stree();
1211 return 1;
1214 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1216 struct range_list *rl;
1217 int nr_states;
1218 struct returned_state_callback *cb;
1219 char *return_ranges;
1220 int final_pass_orig = final_pass;
1221 sval_t val;
1223 if (option_project != PROJ_KERNEL)
1224 return 0;
1226 nr_states = stree_count(__get_cur_stree());
1227 if (nr_states > 1500)
1228 return 0;
1230 if (get_value(expr, &val))
1231 return 0;
1232 if (!get_implied_rl(expr, &rl))
1233 return 0;
1234 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1235 return 0;
1236 if (rl_max(rl).value != 0)
1237 return 0;
1239 __push_fake_cur_stree();
1241 final_pass = 0;
1242 __split_whole_condition(expr);
1243 final_pass = final_pass_orig;
1245 return_ranges = show_rl(rl_filter(rl, rl_zero()));
1247 return_id++;
1248 FOR_EACH_PTR(returned_state_callbacks, cb) {
1249 cb->callback(return_id, return_ranges, expr);
1250 } END_FOR_EACH_PTR(cb);
1252 __push_true_states();
1253 __use_false_states();
1255 return_ranges = alloc_sname("0");;
1256 return_id++;
1257 FOR_EACH_PTR(returned_state_callbacks, cb) {
1258 cb->callback(return_id, return_ranges, expr);
1259 } END_FOR_EACH_PTR(cb);
1261 __merge_true_states();
1262 __free_fake_cur_stree();
1264 return 1;
1267 static int is_boolean(struct expression *expr)
1269 struct range_list *rl;
1271 if (!get_implied_rl(expr, &rl))
1272 return 0;
1273 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1274 return 1;
1275 return 0;
1278 static int is_conditional(struct expression *expr)
1280 if (!expr)
1281 return 0;
1282 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1283 return 1;
1284 return 0;
1287 static int splitable_function_call(struct expression *expr)
1289 struct sm_state *sm;
1290 char buf[64];
1292 if (!expr || expr->type != EXPR_CALL)
1293 return 0;
1294 snprintf(buf, sizeof(buf), "return %p", expr);
1295 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1296 return split_helper(sm, expr);
1299 static void call_return_state_hooks(struct expression *expr)
1301 struct returned_state_callback *cb;
1302 const char *return_ranges;
1303 int nr_states;
1304 sval_t sval;
1306 if (__path_is_null())
1307 return;
1309 expr = strip_expr(expr);
1311 if (is_impossible_path())
1312 goto vanilla;
1314 if (!get_implied_value(expr, &sval) &&
1315 (is_condition(expr) || is_boolean(expr))) {
1316 call_return_state_hooks_compare(expr);
1317 return;
1318 } else if (is_conditional(expr)) {
1319 call_return_state_hooks_conditional(expr);
1320 return;
1321 } else if (call_return_state_hooks_split_possible(expr)) {
1322 return;
1323 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1324 return;
1325 } else if (call_return_state_hooks_split_success_fail(expr)) {
1326 return;
1327 } else if (splitable_function_call(expr)) {
1328 return;
1329 } else if (split_positive_from_negative(expr)) {
1330 return;
1333 vanilla:
1334 return_ranges = get_return_ranges_str(expr);
1336 return_id++;
1337 nr_states = stree_count(__get_cur_stree());
1338 if (nr_states >= 10000) {
1339 match_return_info(return_id, (char *)return_ranges, expr);
1340 return;
1342 FOR_EACH_PTR(returned_state_callbacks, cb) {
1343 cb->callback(return_id, (char *)return_ranges, expr);
1344 } END_FOR_EACH_PTR(cb);
1347 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1349 struct returned_member_callback *cb;
1350 struct stree *stree;
1351 struct sm_state *sm;
1352 struct symbol *type;
1353 char *name;
1354 char member_name[256];
1355 int len;
1357 type = get_type(expr);
1358 if (!type || type->type != SYM_PTR)
1359 return;
1360 name = expr_to_var(expr);
1361 if (!name)
1362 return;
1364 member_name[sizeof(member_name) - 1] = '\0';
1365 strcpy(member_name, "$");
1367 len = strlen(name);
1368 FOR_EACH_PTR(returned_member_callbacks, cb) {
1369 stree = __get_cur_stree();
1370 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1371 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1372 strcpy(member_name, "*$");
1373 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1374 continue;
1376 if (strncmp(sm->name, name, len) != 0)
1377 continue;
1378 if (strncmp(sm->name + len, "->", 2) != 0)
1379 continue;
1380 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1381 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1382 } END_FOR_EACH_SM(sm);
1383 } END_FOR_EACH_PTR(cb);
1385 free_string(name);
1388 static void reset_memdb(void)
1390 mem_sql(NULL, NULL, "delete from caller_info;");
1391 mem_sql(NULL, NULL, "delete from return_states;");
1392 mem_sql(NULL, NULL, "delete from call_implies;");
1395 static void match_end_func_info(struct symbol *sym)
1397 if (__path_is_null())
1398 return;
1399 call_return_state_hooks(NULL);
1400 if (!__inline_fn)
1401 reset_memdb();
1404 static void init_memdb(void)
1406 char *err = NULL;
1407 int rc;
1408 const char *schema_files[] = {
1409 "db/db.schema",
1410 "db/caller_info.schema",
1411 "db/return_states.schema",
1412 "db/function_type_size.schema",
1413 "db/type_size.schema",
1414 "db/call_implies.schema",
1415 "db/function_ptr.schema",
1416 "db/local_values.schema",
1417 "db/function_type_value.schema",
1418 "db/type_value.schema",
1419 "db/function_type_info.schema",
1420 "db/data_info.schema",
1422 static char buf[4096];
1423 int fd;
1424 int ret;
1425 int i;
1427 rc = sqlite3_open(":memory:", &mem_db);
1428 if (rc != SQLITE_OK) {
1429 printf("Error starting In-Memory database.");
1430 return;
1433 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1434 fd = open_data_file(schema_files[i]);
1435 if (fd < 0) {
1436 mem_db = NULL;
1437 return;
1439 ret = read(fd, buf, sizeof(buf));
1440 if (ret == sizeof(buf)) {
1441 printf("Schema file too large: %s (limit %zd bytes)",
1442 schema_files[i], sizeof(buf));
1444 buf[ret] = '\0';
1445 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1446 if (rc != SQLITE_OK) {
1447 fprintf(stderr, "SQL error #2: %s\n", err);
1448 fprintf(stderr, "%s\n", buf);
1453 void open_smatch_db(void)
1455 int rc;
1457 if (option_no_db)
1458 return;
1460 init_memdb();
1462 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1463 if (rc != SQLITE_OK) {
1464 option_no_db = 1;
1465 return;
1467 return;
1470 static void register_common_funcs(void)
1472 struct token *token;
1473 char *func;
1474 char filename[256];
1476 if (option_project == PROJ_NONE)
1477 strcpy(filename, "common_functions");
1478 else
1479 snprintf(filename, 256, "%s.common_functions", option_project_str);
1481 token = get_tokens_file(filename);
1482 if (!token)
1483 return;
1484 if (token_type(token) != TOKEN_STREAMBEGIN)
1485 return;
1486 token = token->next;
1487 while (token_type(token) != TOKEN_STREAMEND) {
1488 if (token_type(token) != TOKEN_IDENT)
1489 return;
1490 func = alloc_string(show_ident(token->ident));
1491 add_ptr_list(&common_funcs, func);
1492 token = token->next;
1494 clear_token_alloc();
1498 void register_definition_db_callbacks(int id)
1500 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1501 add_hook(&global_variable, BASE_HOOK);
1502 add_hook(&global_variable, DECLARATION_HOOK);
1503 add_split_return_callback(match_return_info);
1504 add_split_return_callback(print_returned_struct_members);
1505 add_hook(&call_return_state_hooks, RETURN_HOOK);
1506 add_hook(&match_end_func_info, END_FUNC_HOOK);
1508 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1509 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1511 register_common_funcs();
1514 void register_db_call_marker(int id)
1516 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1519 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1521 struct expression *arg;
1522 char *name = NULL;
1523 char member_name[256];
1525 *sym = NULL;
1527 if (param == -1) {
1528 const char *star = "";
1530 if (expr->type != EXPR_ASSIGNMENT)
1531 return NULL;
1532 name = expr_to_var_sym(expr->left, sym);
1533 if (!name)
1534 return NULL;
1535 if (key[0] == '*') {
1536 star = "*";
1537 key++;
1539 if (strncmp(key, "$", 1) != 0)
1540 return name;
1541 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1542 free_string(name);
1543 return alloc_string(member_name);
1546 while (expr->type == EXPR_ASSIGNMENT)
1547 expr = strip_expr(expr->right);
1548 if (expr->type != EXPR_CALL)
1549 return NULL;
1551 arg = get_argument_from_call_expr(expr->args, param);
1552 if (!arg)
1553 return NULL;
1555 return get_variable_from_key(arg, key, sym);
1558 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1560 char buf[256];
1561 char *tmp;
1563 if (!arg)
1564 return NULL;
1566 arg = strip_expr(arg);
1568 if (strcmp(key, "$") == 0)
1569 return expr_to_var_sym(arg, sym);
1571 if (strcmp(key, "*$") == 0) {
1572 if (arg->type == EXPR_PREOP && arg->op == '&') {
1573 arg = strip_expr(arg->unop);
1574 return expr_to_var_sym(arg, sym);
1575 } else {
1576 tmp = expr_to_var_sym(arg, sym);
1577 if (!tmp)
1578 return NULL;
1579 snprintf(buf, sizeof(buf), "*%s", tmp);
1580 free_string(tmp);
1581 return alloc_string(buf);
1585 if (arg->type == EXPR_PREOP && arg->op == '&') {
1586 arg = strip_expr(arg->unop);
1587 tmp = expr_to_var_sym(arg, sym);
1588 if (!tmp)
1589 return NULL;
1590 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1591 return alloc_string(buf);
1594 tmp = expr_to_var_sym(arg, sym);
1595 if (!tmp)
1596 return NULL;
1597 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1598 free_string(tmp);
1599 return alloc_string(buf);
1602 const char *get_param_name(struct sm_state *sm)
1604 char *param_name;
1605 int name_len;
1606 static char buf[256];
1608 if (!sm->sym || !sm->sym->ident)
1609 return NULL;
1611 param_name = sm->sym->ident->name;
1612 name_len = strlen(param_name);
1614 if (strcmp(sm->name, param_name) == 0) {
1615 return "$";
1616 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1617 strncmp(sm->name, param_name, name_len) == 0) {
1618 snprintf(buf, sizeof(buf), "$%s", sm->name + name_len);
1619 return buf;
1620 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1621 return "*$";
1623 return NULL;
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;