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
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
28 static sqlite3
*mem_db
;
32 #define sql_insert(table, values...) \
38 char *err, *p = buf; \
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, NULL, &err); \
47 if (rc != SQLITE_OK) { \
48 fprintf(stderr, "SQL error #2: %s\n", err); \
49 fprintf(stderr, "SQL: '%s'\n", buf); \
55 sm_printf("SQL: insert into " #table " values (" values); \
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
{
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
{
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
{
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
)
103 for (i
= 0; i
< argc
; i
++) {
106 sm_printf("%s", argv
[i
]);
112 void debug_sql(const char *sql
)
117 sql_exec(print_sql_output
, NULL
, sql
);
120 void debug_mem_sql(const char *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
)
133 if (option_no_db
|| !db
)
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
)
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)
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
)
173 if (strncmp(fn
, "__builtin_", 10) == 0)
176 FOR_EACH_PTR(common_funcs
, tmp
) {
177 if (strcmp(tmp
, fn
) == 0)
179 } END_FOR_EACH_PTR(tmp
);
184 void sql_insert_caller_info(struct expression
*call
, int type
,
185 int param
, const char *key
, const char *value
)
189 if (!option_info
&& !__inline_call
)
192 if (key
&& strlen(key
) >= 80)
195 fn
= get_fnptr_name(call
->fn
);
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
);
209 if (is_common_function(fn
))
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
);
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
,
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
)
258 data_name
= get_data_info_name(data
);
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 /* This can only happen on buggy code. Return invalid SQL. */
270 sql_filter
[0] = '\0';
274 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
275 snprintf(sql_filter
, sizeof(sql_filter
),
276 "file = '%s' and function = '%s' and static = '1'",
277 get_base_file(), sym
->ident
->name
);
279 snprintf(sql_filter
, sizeof(sql_filter
),
280 "function = '%s' and static = '0'", sym
->ident
->name
);
286 static int row_count
;
287 static int get_row_count(void *unused
, int argc
, char **argv
, char **azColName
)
291 row_count
= atoi(argv
[0]);
295 static void sql_select_return_states_pointer(const char *cols
,
296 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
300 ptr
= get_fnptr_name(call
->fn
);
304 run_sql(callback
, info
,
305 "select %s from return_states join function_ptr where "
306 "return_states.function == function_ptr.function and ptr = '%s' "
307 "and searchable = 1 "
308 "order by function_ptr.file, return_states.file, return_id, type;",
312 static int is_local_symbol(struct expression
*expr
)
314 if (expr
->type
!= EXPR_SYMBOL
)
316 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
321 void sql_select_return_states(const char *cols
, struct expression
*call
,
322 int (*callback
)(void*, int, char**, char**), void *info
)
324 if (is_fake_call(call
))
327 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
|| is_local_symbol(call
->fn
)) {
328 sql_select_return_states_pointer(cols
, call
, callback
, info
);
332 if (inlinable(call
->fn
)) {
333 mem_sql(callback
, info
,
334 "select %s from return_states where call_id = '%lu' order by return_id, type;",
335 cols
, (unsigned long)call
);
340 run_sql(get_row_count
, info
, "select count(*) from return_states where %s;",
341 get_static_filter(call
->fn
->symbol
));
342 if (row_count
> 3000)
345 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
346 cols
, get_static_filter(call
->fn
->symbol
));
349 void sql_select_call_implies(const char *cols
, struct expression
*call
,
350 int (*callback
)(void*, int, char**, char**))
352 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
355 if (inlinable(call
->fn
)) {
356 mem_sql(callback
, call
,
357 "select %s from call_implies where call_id = '%lu';",
358 cols
, (unsigned long)call
);
362 run_sql(callback
, call
, "select %s from call_implies where %s;",
363 cols
, get_static_filter(call
->fn
->symbol
));
366 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
367 int (*callback
)(void*, int, char**, char**))
370 mem_sql(callback
, NULL
,
371 "select %s from caller_info where call_id = %lu;",
372 cols
, (unsigned long)__inline_fn
);
376 run_sql(callback
, NULL
,
377 "select %s from caller_info where %s order by call_id;",
378 cols
, get_static_filter(sym
));
381 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
383 struct def_callback
*def_callback
= __alloc_def_callback(0);
385 def_callback
->hook_type
= type
;
386 def_callback
->callback
= callback
;
387 add_ptr_list(&callbacks
, def_callback
);
391 * These call backs are used when the --info option is turned on to print struct
392 * member information. For example foo->bar could have a state in
393 * smatch_extra.c and also check_user.c.
395 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
397 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
399 member_callback
->owner
= owner
;
400 member_callback
->callback
= callback
;
401 add_ptr_list(&member_callbacks
, member_callback
);
404 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
406 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
408 callback
->callback
= fn
;
409 add_ptr_list(&returned_state_callbacks
, callback
);
412 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
))
414 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
416 member_callback
->owner
= owner
;
417 member_callback
->callback
= callback
;
418 add_ptr_list(&returned_member_callbacks
, member_callback
);
421 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
423 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
426 cb
->callback
= callback
;
427 add_ptr_list(&call_implies_cb_list
, cb
);
431 struct expression
*static_returns_call
;
432 struct symbol
*return_type
;
433 struct range_list
*return_range_list
;
436 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
438 struct return_info
*ret_info
= _ret_info
;
439 struct range_list
*rl
;
440 struct expression
*call_expr
= ret_info
->static_returns_call
;
444 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
445 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
449 struct range_list
*db_return_vals(struct expression
*expr
)
451 struct return_info ret_info
= {};
455 if (is_fake_call(expr
))
458 snprintf(buf
, sizeof(buf
), "return %p", expr
);
459 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
461 return clone_rl(estate_rl(sm
->state
));
462 ret_info
.static_returns_call
= expr
;
463 ret_info
.return_type
= get_type(expr
);
464 if (!ret_info
.return_type
)
467 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
470 ret_info
.return_range_list
= NULL
;
471 if (inlinable(expr
->fn
)) {
472 mem_sql(db_return_callback
, &ret_info
,
473 "select distinct return from return_states where call_id = '%lu';",
474 (unsigned long)expr
);
476 run_sql(db_return_callback
, &ret_info
,
477 "select distinct return from return_states where %s;",
478 get_static_filter(expr
->fn
->symbol
));
480 return ret_info
.return_range_list
;
483 struct range_list
*db_return_vals_from_str(const char *fn_name
)
485 struct return_info ret_info
;
487 ret_info
.static_returns_call
= NULL
;
488 ret_info
.return_type
= &llong_ctype
;
489 ret_info
.return_range_list
= NULL
;
491 run_sql(db_return_callback
, &ret_info
,
492 "select distinct return from return_states where function = '%s';",
494 return ret_info
.return_range_list
;
497 static void match_call_marker(struct expression
*expr
)
500 * we just want to record something in the database so that if we have
501 * two calls like: frob(4); frob(some_unkown); then on the receiving
502 * side we know that sometimes frob is called with unknown parameters.
505 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
508 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
509 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
515 char printed_name
[256];
518 expr
= strip_expr(expr
);
519 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
520 expr
= strip_expr(expr
->unop
);
524 name
= expr_to_var_sym(expr
, &sym
);
529 FOR_EACH_SM(stree
, sm
) {
532 if (strcmp(name
, sm
->name
) == 0) {
534 snprintf(printed_name
, sizeof(printed_name
), "*$");
535 else /* these are already handled. fixme: handle them here */
537 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
538 snprintf(printed_name
, sizeof(printed_name
), "*$");
539 } else if (strncmp(name
, sm
->name
, len
) == 0) {
540 if (isalnum(sm
->name
[len
]))
543 snprintf(printed_name
, sizeof(printed_name
), "$->%s", sm
->name
+ len
+ 1);
545 snprintf(printed_name
, sizeof(printed_name
), "$%s", sm
->name
+ len
);
549 callback(call
, param
, printed_name
, sm
);
550 } END_FOR_EACH_SM(sm
);
555 static void match_call_info(struct expression
*call
)
557 struct member_info_callback
*cb
;
558 struct expression
*arg
;
563 name
= get_fnptr_name(call
->fn
);
567 FOR_EACH_PTR(member_callbacks
, cb
) {
568 stree
= get_all_states_stree(cb
->owner
);
570 FOR_EACH_PTR(call
->args
, arg
) {
571 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
573 } END_FOR_EACH_PTR(arg
);
575 } END_FOR_EACH_PTR(cb
);
580 static int get_param(int param
, char **name
, struct symbol
**sym
)
586 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
588 * this is a temporary hack to work around a bug (I think in sparse?)
589 * 2.6.37-rc1:fs/reiserfs/journal.o
590 * If there is a function definition without parameter name found
591 * after a function implementation then it causes a crash.
595 if (arg
->ident
->name
< (char *)100)
598 *name
= arg
->ident
->name
;
603 } END_FOR_EACH_PTR(arg
);
608 static struct stree
*final_states
;
609 static int prev_func_id
= -1;
610 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
618 struct symbol
*sym
= NULL
;
619 struct def_callback
*def_callback
;
625 func_id
= atoi(argv
[0]);
627 type
= strtol(argv
[1], NULL
, 10);
628 param
= strtol(argv
[2], NULL
, 10);
635 if (prev_func_id
== -1)
636 prev_func_id
= func_id
;
637 if (func_id
!= prev_func_id
) {
638 stree
= __pop_fake_cur_stree();
639 merge_stree(&final_states
, stree
);
641 __push_fake_cur_stree();
643 prev_func_id
= func_id
;
646 if (param
>= 0 && !get_param(param
, &name
, &sym
))
649 FOR_EACH_PTR(callbacks
, def_callback
) {
650 if (def_callback
->hook_type
== type
)
651 def_callback
->callback(name
, sym
, key
, value
);
652 } END_FOR_EACH_PTR(def_callback
);
657 static void get_direct_callers(struct symbol
*sym
)
659 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
660 caller_info_callback
);
663 static struct string_list
*ptr_names_done
;
664 static struct string_list
*ptr_names
;
666 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
668 insert_string(&ptr_names
, alloc_string(argv
[0]));
672 static char *get_next_ptr_name(void)
676 FOR_EACH_PTR(ptr_names
, ptr
) {
677 if (list_has_string(ptr_names_done
, ptr
))
679 insert_string(&ptr_names_done
, ptr
);
681 } END_FOR_EACH_PTR(ptr
);
685 static void get_ptr_names(const char *file
, const char *name
)
687 char sql_filter
[1024];
691 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
694 snprintf(sql_filter
, 1024, "function = '%s';", name
);
697 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
699 run_sql(get_ptr_name
, NULL
,
700 "select distinct ptr from function_ptr where %s",
703 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
707 while ((name
= get_next_ptr_name()))
708 get_ptr_names(NULL
, name
);
711 static void match_data_from_db(struct symbol
*sym
)
716 if (!sym
|| !sym
->ident
)
719 __push_fake_cur_stree();
726 if (sym
->ctype
.modifiers
& MOD_STATIC
)
727 get_ptr_names(get_base_file(), sym
->ident
->name
);
729 get_ptr_names(NULL
, sym
->ident
->name
);
731 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
732 __free_ptr_list((struct ptr_list
**)&ptr_names
);
733 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
734 stree
= __pop_fake_cur_stree();
739 get_direct_callers(sym
);
741 FOR_EACH_PTR(ptr_names
, ptr
) {
742 run_sql(caller_info_callback
, NULL
,
743 "select call_id, type, parameter, key, value"
744 " from caller_info where function = '%s' order by call_id",
747 } END_FOR_EACH_PTR(ptr
);
749 __free_ptr_list((struct ptr_list
**)&ptr_names
);
750 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
752 get_direct_callers(sym
);
755 stree
= __pop_fake_cur_stree();
756 merge_stree(&final_states
, stree
);
759 FOR_EACH_SM(final_states
, sm
) {
761 } END_FOR_EACH_SM(sm
);
763 free_stree(&final_states
);
766 static int call_implies_callbacks(void *_call
, int argc
, char **argv
, char **azColName
)
768 struct expression
*call_expr
= _call
;
769 struct call_implies_callback
*cb
;
770 struct expression
*arg
= NULL
;
777 type
= atoi(argv
[1]);
778 param
= atoi(argv
[2]);
780 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
781 if (cb
->type
!= type
)
784 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
788 cb
->callback(arg
, argv
[3], argv
[4]);
789 } END_FOR_EACH_PTR(cb
);
794 static void match_call_implies(struct expression
*expr
)
796 sql_select_call_implies("function, type, parameter, key, value", expr
,
797 call_implies_callbacks
);
800 static void print_initializer_list(struct expression_list
*expr_list
,
801 struct symbol
*struct_type
)
803 struct expression
*expr
;
804 struct symbol
*base_type
;
805 char struct_name
[256];
807 FOR_EACH_PTR(expr_list
, expr
) {
808 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
809 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
812 if (expr
->type
!= EXPR_IDENTIFIER
)
814 if (!expr
->expr_ident
)
816 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
818 base_type
= get_type(expr
->ident_expression
);
819 if (!base_type
|| base_type
->type
!= SYM_FN
)
821 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
822 struct_type
->ident
->name
, expr
->expr_ident
->name
);
823 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
825 } END_FOR_EACH_PTR(expr
);
828 static void global_variable(struct symbol
*sym
)
830 struct symbol
*struct_type
;
834 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
836 struct_type
= get_base_type(sym
);
839 if (struct_type
->type
== SYM_ARRAY
) {
840 struct_type
= get_base_type(struct_type
);
844 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
846 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
849 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
851 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
854 static void call_return_state_hooks_conditional(struct expression
*expr
)
856 struct returned_state_callback
*cb
;
857 struct range_list
*rl
;
859 int final_pass_orig
= final_pass
;
861 __push_fake_cur_stree();
864 __split_whole_condition(expr
->conditional
);
865 final_pass
= final_pass_orig
;
867 if (get_implied_rl(expr
->cond_true
, &rl
))
868 rl
= cast_rl(cur_func_return_type(), rl
);
870 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
871 return_ranges
= show_rl(rl
);
872 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
875 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
876 cb
->callback(return_id
, return_ranges
, expr
);
877 } END_FOR_EACH_PTR(cb
);
879 __push_true_states();
880 __use_false_states();
882 if (get_implied_rl(expr
->cond_false
, &rl
))
883 rl
= cast_rl(cur_func_return_type(), rl
);
885 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
886 return_ranges
= show_rl(rl
);
887 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
890 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
891 cb
->callback(return_id
, return_ranges
, expr
);
892 } END_FOR_EACH_PTR(cb
);
894 __merge_true_states();
895 __free_fake_cur_stree();
898 static void call_return_state_hooks_compare(struct expression
*expr
)
900 struct returned_state_callback
*cb
;
902 int final_pass_orig
= final_pass
;
903 sval_t sval
= { .type
= &int_ctype
};
906 if (!get_implied_value(expr
, &ret
))
909 __push_fake_cur_stree();
912 __split_whole_condition(expr
);
913 final_pass
= final_pass_orig
;
915 if (ret
.value
!= 0) {
916 return_ranges
= alloc_sname("1");
918 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
921 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
922 cb
->callback(return_id
, return_ranges
, expr
);
923 } END_FOR_EACH_PTR(cb
);
926 __push_true_states();
927 __use_false_states();
929 if (ret
.value
!= 1) {
930 return_ranges
= alloc_sname("0");
932 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
935 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
936 cb
->callback(return_id
, return_ranges
, expr
);
937 } END_FOR_EACH_PTR(cb
);
940 __merge_true_states();
941 __free_fake_cur_stree();
944 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
946 struct sm_state
*tmp
;
948 FOR_EACH_PTR(slist
, tmp
) {
949 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
951 } END_FOR_EACH_PTR(tmp
);
956 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
958 struct returned_state_callback
*cb
;
959 struct range_list
*rl
;
961 struct sm_state
*tmp
;
963 int nr_possible
, nr_states
;
964 char *compare_str
= NULL
;
966 struct state_list
*already_handled
= NULL
;
968 if (!sm
|| !sm
->merged
)
971 if (too_many_possible(sm
))
974 /* bail if it gets too complicated */
975 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
976 nr_states
= stree_count(__get_cur_stree());
977 if (option_info
&& nr_states
* nr_possible
>= 2000)
980 FOR_EACH_PTR(sm
->possible
, tmp
) {
983 if (ptr_in_list(tmp
, already_handled
))
985 add_ptr_list(&already_handled
, tmp
);
988 __push_fake_cur_stree();
990 overwrite_states_using_pool(sm
, tmp
);
992 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
993 return_ranges
= show_rl(rl
);
994 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
995 compare_str
= expr_lte_to_param(expr
, -1);
997 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
998 return_ranges
= alloc_sname(buf
);
1002 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1003 cb
->callback(return_id
, return_ranges
, expr
);
1004 } END_FOR_EACH_PTR(cb
);
1006 __free_fake_cur_stree();
1007 } END_FOR_EACH_PTR(tmp
);
1009 free_slist(&already_handled
);
1014 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1016 struct sm_state
*sm
;
1018 if (!expr
|| expr_equal_to_param(expr
, -1))
1021 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1022 return split_possible_helper(sm
, expr
);
1025 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1027 struct range_list
*rl
;
1028 char *return_ranges
;
1037 return alloc_sname("");
1039 if (get_implied_value(expr
, &sval
)) {
1040 *rl_p
= alloc_rl(sval
, sval
);
1041 return sval_to_str(sval
);
1044 compare_str
= expr_equal_to_param(expr
, -1);
1045 math_str
= get_value_in_terms_of_parameter_math(expr
);
1047 if (get_implied_rl(expr
, &rl
)) {
1048 rl
= cast_rl(cur_func_return_type(), rl
);
1049 return_ranges
= show_rl(rl
);
1050 } else if (get_imaginary_absolute(expr
, &rl
)){
1051 rl
= cast_rl(cur_func_return_type(), rl
);
1052 return alloc_sname(show_rl(rl
));
1054 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1055 return_ranges
= show_rl(rl
);
1060 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1061 return alloc_sname(buf
);
1065 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1066 return alloc_sname(buf
);
1069 compare_str
= expr_lte_to_param(expr
, -1);
1071 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1072 return alloc_sname(buf
);
1074 return return_ranges
;
1077 static int split_positive_from_negative(struct expression
*expr
)
1079 struct returned_state_callback
*cb
;
1080 struct range_list
*rl
;
1081 const char *return_ranges
;
1082 struct range_list
*ret_rl
;
1085 /* We're going to print the states 3 times */
1086 if (stree_count(__get_cur_stree()) > 10000 / 3)
1089 if (!get_implied_rl(expr
, &rl
) || !rl
)
1091 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1093 /* Forget about INT_MAX and larger */
1094 if (rl_max(rl
).value
<= 0)
1096 if (!sval_is_negative(rl_min(rl
)))
1099 if (!assume(compare_expression(expr
, '>', zero_expr())))
1103 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1104 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1105 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1106 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1107 } END_FOR_EACH_PTR(cb
);
1111 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1112 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1115 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1116 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1117 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1118 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1119 } END_FOR_EACH_PTR(cb
);
1125 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1128 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1129 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1130 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1131 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1132 } END_FOR_EACH_PTR(cb
);
1140 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1142 struct returned_state_callback
*cb
;
1143 struct range_list
*rl
;
1144 struct range_list
*nonnull_rl
;
1146 struct range_list
*null_rl
= NULL
;
1147 char *return_ranges
;
1148 struct smatch_state
*state
;
1150 int final_pass_orig
= final_pass
;
1152 if (!expr
|| expr_equal_to_param(expr
, -1))
1154 if (expr
->type
== EXPR_CALL
)
1156 if (!is_pointer(expr
))
1159 state
= get_state_expr(SMATCH_EXTRA
, expr
);
1160 if (!state
|| !estate_rl(state
))
1162 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1164 if (!rl_has_sval(estate_rl(state
), sval_type_val(estate_type(state
), 0)))
1167 nr_states
= stree_count(__get_cur_stree());
1168 if (option_info
&& nr_states
>= 1500)
1171 rl
= estate_rl(state
);
1173 __push_fake_cur_stree();
1176 __split_whole_condition(expr
);
1177 final_pass
= final_pass_orig
;
1179 nonnull_rl
= rl_filter(rl
, rl_zero());
1180 return_ranges
= show_rl(nonnull_rl
);
1181 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1184 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1185 cb
->callback(return_id
, return_ranges
, expr
);
1186 } END_FOR_EACH_PTR(cb
);
1188 __push_true_states();
1189 __use_false_states();
1191 return_ranges
= alloc_sname("0");
1192 null_sval
= sval_type_val(rl_type(rl
), 0);
1193 add_range(&null_rl
, null_sval
, null_sval
);
1194 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1196 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1197 cb
->callback(return_id
, return_ranges
, expr
);
1198 } END_FOR_EACH_PTR(cb
);
1200 __merge_true_states();
1201 __free_fake_cur_stree();
1206 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1208 struct range_list
*rl
;
1209 struct range_list
*nonzero_rl
;
1211 struct range_list
*zero_rl
= NULL
;
1213 struct returned_state_callback
*cb
;
1214 char *return_ranges
;
1215 int final_pass_orig
= final_pass
;
1218 if (option_project
!= PROJ_KERNEL
)
1221 nr_states
= stree_count(__get_cur_stree());
1222 if (nr_states
> 1500)
1225 if (get_value(expr
, &val
))
1227 if (!get_implied_rl(expr
, &rl
))
1229 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1231 if (rl_max(rl
).value
!= 0)
1234 __push_fake_cur_stree();
1237 __split_whole_condition(expr
);
1238 final_pass
= final_pass_orig
;
1240 nonzero_rl
= rl_filter(rl
, rl_zero());
1241 return_ranges
= show_rl(nonzero_rl
);
1242 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1245 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1246 cb
->callback(return_id
, return_ranges
, expr
);
1247 } END_FOR_EACH_PTR(cb
);
1249 __push_true_states();
1250 __use_false_states();
1252 return_ranges
= alloc_sname("0");
1253 zero_sval
= sval_type_val(rl_type(rl
), 0);
1254 add_range(&zero_rl
, zero_sval
, zero_sval
);
1255 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
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();
1267 static int is_boolean(struct expression
*expr
)
1269 struct range_list
*rl
;
1271 if (!get_implied_rl(expr
, &rl
))
1273 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1278 static int is_conditional(struct expression
*expr
)
1282 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1287 static int splitable_function_call(struct expression
*expr
)
1289 struct sm_state
*sm
;
1292 if (!expr
|| expr
->type
!= EXPR_CALL
)
1294 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1295 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1296 return split_possible_helper(sm
, expr
);
1299 static struct sm_state
*find_bool_param(void)
1301 struct stree
*start_states
;
1303 struct sm_state
*sm
, *tmp
;
1306 start_states
= get_start_states();
1308 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1311 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1314 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1315 rl_max(estate_rl(sm
->state
)).value
!= 1)
1318 } END_FOR_EACH_PTR_REVERSE(arg
);
1324 * Check if it's splitable. If not, then splitting it up is likely not
1325 * useful for the callers.
1327 FOR_EACH_PTR(sm
->possible
, tmp
) {
1330 if (!estate_get_single_value(tmp
->state
, &sval
))
1332 } END_FOR_EACH_PTR(tmp
);
1337 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1339 struct returned_state_callback
*cb
;
1340 struct range_list
*ret_rl
;
1341 const char *return_ranges
;
1342 struct sm_state
*tmp
;
1344 int nr_possible
, nr_states
;
1345 char *compare_str
= NULL
;
1347 struct state_list
*already_handled
= NULL
;
1349 if (!sm
|| !sm
->merged
)
1352 if (too_many_possible(sm
))
1355 /* bail if it gets too complicated */
1356 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1357 nr_states
= stree_count(__get_cur_stree());
1358 if (option_info
&& nr_states
* nr_possible
>= 2000)
1361 FOR_EACH_PTR(sm
->possible
, tmp
) {
1364 if (ptr_in_list(tmp
, already_handled
))
1366 add_ptr_list(&already_handled
, tmp
);
1369 __push_fake_cur_stree();
1371 overwrite_states_using_pool(sm
, tmp
);
1373 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1374 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1375 compare_str
= expr_lte_to_param(expr
, -1);
1377 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1378 return_ranges
= alloc_sname(buf
);
1382 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1383 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1384 } END_FOR_EACH_PTR(cb
);
1386 __free_fake_cur_stree();
1387 } END_FOR_EACH_PTR(tmp
);
1389 free_slist(&already_handled
);
1394 static int split_by_bool_param(struct expression
*expr
)
1396 struct sm_state
*start_sm
, *sm
;
1399 start_sm
= find_bool_param();
1402 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
1403 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
1405 return split_on_bool_sm(sm
, expr
);
1408 static void call_return_state_hooks(struct expression
*expr
)
1410 struct returned_state_callback
*cb
;
1411 struct range_list
*ret_rl
;
1412 const char *return_ranges
;
1416 if (__path_is_null())
1419 expr
= strip_expr(expr
);
1421 if (is_impossible_path())
1424 if (expr
&& (expr
->type
== EXPR_COMPARE
||
1425 !get_implied_value(expr
, &sval
)) &&
1426 (is_condition(expr
) || is_boolean(expr
))) {
1427 call_return_state_hooks_compare(expr
);
1429 } else if (is_conditional(expr
)) {
1430 call_return_state_hooks_conditional(expr
);
1432 } else if (call_return_state_hooks_split_possible(expr
)) {
1434 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
1436 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1438 } else if (splitable_function_call(expr
)) {
1440 } else if (split_positive_from_negative(expr
)) {
1442 } else if (split_by_bool_param(expr
)) {
1447 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1448 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1451 nr_states
= stree_count(__get_cur_stree());
1452 if (nr_states
>= 10000) {
1453 match_return_info(return_id
, (char *)return_ranges
, expr
);
1456 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1457 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1458 } END_FOR_EACH_PTR(cb
);
1461 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1463 struct returned_member_callback
*cb
;
1464 struct stree
*stree
;
1465 struct sm_state
*sm
;
1466 struct symbol
*type
;
1468 char member_name
[256];
1471 type
= get_type(expr
);
1472 if (!type
|| type
->type
!= SYM_PTR
)
1474 name
= expr_to_var(expr
);
1478 member_name
[sizeof(member_name
) - 1] = '\0';
1479 strcpy(member_name
, "$");
1482 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1483 stree
= __get_cur_stree();
1484 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1485 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1486 strcpy(member_name
, "*$");
1487 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1490 if (strncmp(sm
->name
, name
, len
) != 0)
1492 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1494 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1495 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1496 } END_FOR_EACH_SM(sm
);
1497 } END_FOR_EACH_PTR(cb
);
1502 static void reset_memdb(struct symbol
*sym
)
1504 mem_sql(NULL
, NULL
, "delete from caller_info;");
1505 mem_sql(NULL
, NULL
, "delete from return_states;");
1506 mem_sql(NULL
, NULL
, "delete from call_implies;");
1509 static void match_end_func_info(struct symbol
*sym
)
1511 if (__path_is_null())
1513 call_return_state_hooks(NULL
);
1516 static void match_after_func(struct symbol
*sym
)
1522 static void init_memdb(void)
1526 const char *schema_files
[] = {
1528 "db/caller_info.schema",
1529 "db/return_states.schema",
1530 "db/function_type_size.schema",
1531 "db/type_size.schema",
1532 "db/call_implies.schema",
1533 "db/function_ptr.schema",
1534 "db/local_values.schema",
1535 "db/function_type_value.schema",
1536 "db/type_value.schema",
1537 "db/function_type_info.schema",
1538 "db/data_info.schema",
1540 static char buf
[4096];
1545 rc
= sqlite3_open(":memory:", &mem_db
);
1546 if (rc
!= SQLITE_OK
) {
1547 printf("Error starting In-Memory database.");
1551 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1552 fd
= open_data_file(schema_files
[i
]);
1557 ret
= read(fd
, buf
, sizeof(buf
));
1558 if (ret
== sizeof(buf
)) {
1559 printf("Schema file too large: %s (limit %zd bytes)",
1560 schema_files
[i
], sizeof(buf
));
1563 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
1564 if (rc
!= SQLITE_OK
) {
1565 fprintf(stderr
, "SQL error #2: %s\n", err
);
1566 fprintf(stderr
, "%s\n", buf
);
1571 void open_smatch_db(void)
1580 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1581 if (rc
!= SQLITE_OK
) {
1588 static void register_common_funcs(void)
1590 struct token
*token
;
1594 if (option_project
== PROJ_NONE
)
1595 strcpy(filename
, "common_functions");
1597 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1599 token
= get_tokens_file(filename
);
1602 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1604 token
= token
->next
;
1605 while (token_type(token
) != TOKEN_STREAMEND
) {
1606 if (token_type(token
) != TOKEN_IDENT
)
1608 func
= alloc_string(show_ident(token
->ident
));
1609 add_ptr_list(&common_funcs
, func
);
1610 token
= token
->next
;
1612 clear_token_alloc();
1616 void register_definition_db_callbacks(int id
)
1618 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1619 add_hook(&global_variable
, BASE_HOOK
);
1620 add_hook(&global_variable
, DECLARATION_HOOK
);
1621 add_split_return_callback(match_return_info
);
1622 add_split_return_callback(print_returned_struct_members
);
1623 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1624 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1625 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
1627 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1628 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1630 register_common_funcs();
1633 void register_db_call_marker(int id
)
1635 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1638 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
1640 struct expression
*arg
;
1642 char member_name
[256];
1647 const char *star
= "";
1649 if (expr
->type
!= EXPR_ASSIGNMENT
)
1651 name
= expr_to_var_sym(expr
->left
, sym
);
1654 if (key
[0] == '*') {
1658 if (strncmp(key
, "$", 1) != 0)
1660 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1662 return alloc_string(member_name
);
1665 while (expr
->type
== EXPR_ASSIGNMENT
)
1666 expr
= strip_expr(expr
->right
);
1667 if (expr
->type
!= EXPR_CALL
)
1670 arg
= get_argument_from_call_expr(expr
->args
, param
);
1674 return get_variable_from_key(arg
, key
, sym
);
1677 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
1685 arg
= strip_expr(arg
);
1687 if (strcmp(key
, "$") == 0)
1688 return expr_to_var_sym(arg
, sym
);
1690 if (strcmp(key
, "*$") == 0) {
1691 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1692 arg
= strip_expr(arg
->unop
);
1693 return expr_to_var_sym(arg
, sym
);
1695 tmp
= expr_to_var_sym(arg
, sym
);
1698 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1700 return alloc_string(buf
);
1704 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1705 arg
= strip_expr(arg
->unop
);
1706 tmp
= expr_to_var_sym(arg
, sym
);
1709 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1710 return alloc_string(buf
);
1713 tmp
= expr_to_var_sym(arg
, sym
);
1716 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1718 return alloc_string(buf
);
1721 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
1725 if (strcmp("$", key
) == 0)
1726 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
1727 return get_variable_from_key(arg
, key
, sym
);
1730 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
1734 static char buf
[256];
1736 if (!sym
|| !sym
->ident
)
1739 param_name
= sym
->ident
->name
;
1740 name_len
= strlen(param_name
);
1742 if (strcmp(name
, param_name
) == 0) {
1744 } else if (name
[name_len
] == '-' && /* check for '-' from "->" */
1745 strncmp(name
, param_name
, name_len
) == 0) {
1746 snprintf(buf
, sizeof(buf
), "$%s", name
+ name_len
);
1748 } else if (name
[0] == '*' && strcmp(name
+ 1, param_name
) == 0) {
1754 const char *get_param_name(struct sm_state
*sm
)
1756 return get_param_name_var_sym(sm
->name
, sm
->sym
);
1759 char *get_data_info_name(struct expression
*expr
)
1766 expr
= strip_expr(expr
);
1767 name
= get_member_name(expr
);
1770 name
= expr_to_var_sym(expr
, &sym
);
1773 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1775 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1776 snprintf(buf
, sizeof(buf
), "static %s", name
);
1778 snprintf(buf
, sizeof(buf
), "global %s", name
);
1779 ret
= alloc_sname(buf
);