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 order by return_id, type;",
311 static int is_local_symbol(struct expression
*expr
)
313 if (expr
->type
!= EXPR_SYMBOL
)
315 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
320 void sql_select_return_states(const char *cols
, struct expression
*call
,
321 int (*callback
)(void*, int, char**, char**), void *info
)
323 if (is_fake_call(call
))
326 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
|| is_local_symbol(call
->fn
)) {
327 sql_select_return_states_pointer(cols
, call
, callback
, info
);
331 if (inlinable(call
->fn
)) {
332 mem_sql(callback
, info
,
333 "select %s from return_states where call_id = '%lu' order by return_id, type;",
334 cols
, (unsigned long)call
);
339 run_sql(get_row_count
, info
, "select count(*) from return_states where %s;",
340 get_static_filter(call
->fn
->symbol
));
341 if (row_count
> 3000)
344 run_sql(callback
, info
, "select %s from return_states where %s order by return_id, type;",
345 cols
, get_static_filter(call
->fn
->symbol
));
348 void sql_select_call_implies(const char *cols
, struct expression
*call
,
349 int (*callback
)(void*, int, char**, char**))
351 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
354 if (inlinable(call
->fn
)) {
355 mem_sql(callback
, call
,
356 "select %s from call_implies where call_id = '%lu';",
357 cols
, (unsigned long)call
);
361 run_sql(callback
, call
, "select %s from call_implies where %s;",
362 cols
, get_static_filter(call
->fn
->symbol
));
365 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
366 int (*callback
)(void*, int, char**, char**))
369 mem_sql(callback
, NULL
,
370 "select %s from caller_info where call_id = %lu;",
371 cols
, (unsigned long)__inline_fn
);
375 run_sql(callback
, NULL
,
376 "select %s from caller_info where %s order by call_id;",
377 cols
, get_static_filter(sym
));
380 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
382 struct def_callback
*def_callback
= __alloc_def_callback(0);
384 def_callback
->hook_type
= type
;
385 def_callback
->callback
= callback
;
386 add_ptr_list(&callbacks
, def_callback
);
390 * These call backs are used when the --info option is turned on to print struct
391 * member information. For example foo->bar could have a state in
392 * smatch_extra.c and also check_user.c.
394 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
396 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
398 member_callback
->owner
= owner
;
399 member_callback
->callback
= callback
;
400 add_ptr_list(&member_callbacks
, member_callback
);
403 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
405 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
407 callback
->callback
= fn
;
408 add_ptr_list(&returned_state_callbacks
, callback
);
411 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
))
413 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
415 member_callback
->owner
= owner
;
416 member_callback
->callback
= callback
;
417 add_ptr_list(&returned_member_callbacks
, member_callback
);
420 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
422 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
425 cb
->callback
= callback
;
426 add_ptr_list(&call_implies_cb_list
, cb
);
430 struct expression
*static_returns_call
;
431 struct symbol
*return_type
;
432 struct range_list
*return_range_list
;
435 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
437 struct return_info
*ret_info
= _ret_info
;
438 struct range_list
*rl
;
439 struct expression
*call_expr
= ret_info
->static_returns_call
;
443 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
444 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
448 struct range_list
*db_return_vals(struct expression
*expr
)
450 struct return_info ret_info
= {};
454 if (is_fake_call(expr
))
457 snprintf(buf
, sizeof(buf
), "return %p", expr
);
458 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
460 return clone_rl(estate_rl(sm
->state
));
461 ret_info
.static_returns_call
= expr
;
462 ret_info
.return_type
= get_type(expr
);
463 if (!ret_info
.return_type
)
466 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
469 ret_info
.return_range_list
= NULL
;
470 if (inlinable(expr
->fn
)) {
471 mem_sql(db_return_callback
, &ret_info
,
472 "select distinct return from return_states where call_id = '%lu';",
473 (unsigned long)expr
);
475 run_sql(db_return_callback
, &ret_info
,
476 "select distinct return from return_states where %s;",
477 get_static_filter(expr
->fn
->symbol
));
479 return ret_info
.return_range_list
;
482 struct range_list
*db_return_vals_from_str(const char *fn_name
)
484 struct return_info ret_info
;
486 ret_info
.static_returns_call
= NULL
;
487 ret_info
.return_type
= &llong_ctype
;
488 ret_info
.return_range_list
= NULL
;
490 run_sql(db_return_callback
, &ret_info
,
491 "select distinct return from return_states where function = '%s';",
493 return ret_info
.return_range_list
;
496 static void match_call_marker(struct expression
*expr
)
499 * we just want to record something in the database so that if we have
500 * two calls like: frob(4); frob(some_unkown); then on the receiving
501 * side we know that sometimes frob is called with unknown parameters.
504 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
507 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
508 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
514 char printed_name
[256];
517 expr
= strip_expr(expr
);
518 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
519 expr
= strip_expr(expr
->unop
);
523 name
= expr_to_var_sym(expr
, &sym
);
528 FOR_EACH_SM(stree
, sm
) {
531 if (strcmp(name
, sm
->name
) == 0) {
533 snprintf(printed_name
, sizeof(printed_name
), "*$");
534 else /* these are already handled. fixme: handle them here */
536 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
537 snprintf(printed_name
, sizeof(printed_name
), "*$");
538 } else if (strncmp(name
, sm
->name
, len
) == 0) {
539 if (isalnum(sm
->name
[len
]))
542 snprintf(printed_name
, sizeof(printed_name
), "$->%s", sm
->name
+ len
+ 1);
544 snprintf(printed_name
, sizeof(printed_name
), "$%s", sm
->name
+ len
);
548 callback(call
, param
, printed_name
, sm
);
549 } END_FOR_EACH_SM(sm
);
554 static void match_call_info(struct expression
*call
)
556 struct member_info_callback
*cb
;
557 struct expression
*arg
;
562 name
= get_fnptr_name(call
->fn
);
566 FOR_EACH_PTR(member_callbacks
, cb
) {
567 stree
= get_all_states_stree(cb
->owner
);
569 FOR_EACH_PTR(call
->args
, arg
) {
570 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
572 } END_FOR_EACH_PTR(arg
);
574 } END_FOR_EACH_PTR(cb
);
579 static int get_param(int param
, char **name
, struct symbol
**sym
)
585 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
587 * this is a temporary hack to work around a bug (I think in sparse?)
588 * 2.6.37-rc1:fs/reiserfs/journal.o
589 * If there is a function definition without parameter name found
590 * after a function implementation then it causes a crash.
594 if (arg
->ident
->name
< (char *)100)
597 *name
= arg
->ident
->name
;
602 } END_FOR_EACH_PTR(arg
);
607 static struct stree
*final_states
;
608 static int prev_func_id
= -1;
609 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
617 struct symbol
*sym
= NULL
;
618 struct def_callback
*def_callback
;
624 func_id
= atoi(argv
[0]);
626 type
= strtol(argv
[1], NULL
, 10);
627 param
= strtol(argv
[2], NULL
, 10);
634 if (prev_func_id
== -1)
635 prev_func_id
= func_id
;
636 if (func_id
!= prev_func_id
) {
637 stree
= __pop_fake_cur_stree();
638 merge_stree(&final_states
, stree
);
640 __push_fake_cur_stree();
642 prev_func_id
= func_id
;
645 if (param
>= 0 && !get_param(param
, &name
, &sym
))
648 FOR_EACH_PTR(callbacks
, def_callback
) {
649 if (def_callback
->hook_type
== type
)
650 def_callback
->callback(name
, sym
, key
, value
);
651 } END_FOR_EACH_PTR(def_callback
);
656 static void get_direct_callers(struct symbol
*sym
)
658 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
659 caller_info_callback
);
662 static struct string_list
*ptr_names_done
;
663 static struct string_list
*ptr_names
;
665 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
667 insert_string(&ptr_names
, alloc_string(argv
[0]));
671 static char *get_next_ptr_name(void)
675 FOR_EACH_PTR(ptr_names
, ptr
) {
676 if (list_has_string(ptr_names_done
, ptr
))
678 insert_string(&ptr_names_done
, ptr
);
680 } END_FOR_EACH_PTR(ptr
);
684 static void get_ptr_names(const char *file
, const char *name
)
686 char sql_filter
[1024];
690 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
693 snprintf(sql_filter
, 1024, "function = '%s';", name
);
696 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
698 run_sql(get_ptr_name
, NULL
,
699 "select distinct ptr from function_ptr where %s",
702 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
706 while ((name
= get_next_ptr_name()))
707 get_ptr_names(NULL
, name
);
710 static void match_data_from_db(struct symbol
*sym
)
715 if (!sym
|| !sym
->ident
)
718 __push_fake_cur_stree();
725 if (sym
->ctype
.modifiers
& MOD_STATIC
)
726 get_ptr_names(get_base_file(), sym
->ident
->name
);
728 get_ptr_names(NULL
, sym
->ident
->name
);
730 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
731 __free_ptr_list((struct ptr_list
**)&ptr_names
);
732 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
733 stree
= __pop_fake_cur_stree();
738 get_direct_callers(sym
);
740 FOR_EACH_PTR(ptr_names
, ptr
) {
741 run_sql(caller_info_callback
, NULL
,
742 "select call_id, type, parameter, key, value"
743 " from caller_info where function = '%s' order by call_id",
746 } END_FOR_EACH_PTR(ptr
);
748 __free_ptr_list((struct ptr_list
**)&ptr_names
);
749 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
751 get_direct_callers(sym
);
754 stree
= __pop_fake_cur_stree();
755 merge_stree(&final_states
, stree
);
758 FOR_EACH_SM(final_states
, sm
) {
760 } END_FOR_EACH_SM(sm
);
762 free_stree(&final_states
);
765 static int call_implies_callbacks(void *_call
, int argc
, char **argv
, char **azColName
)
767 struct expression
*call_expr
= _call
;
768 struct call_implies_callback
*cb
;
769 struct expression
*arg
= NULL
;
776 type
= atoi(argv
[1]);
777 param
= atoi(argv
[2]);
779 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
780 if (cb
->type
!= type
)
783 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
787 cb
->callback(arg
, argv
[3], argv
[4]);
788 } END_FOR_EACH_PTR(cb
);
793 static void match_call_implies(struct expression
*expr
)
795 sql_select_call_implies("function, type, parameter, key, value", expr
,
796 call_implies_callbacks
);
799 static void print_initializer_list(struct expression_list
*expr_list
,
800 struct symbol
*struct_type
)
802 struct expression
*expr
;
803 struct symbol
*base_type
;
804 char struct_name
[256];
806 FOR_EACH_PTR(expr_list
, expr
) {
807 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
808 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
811 if (expr
->type
!= EXPR_IDENTIFIER
)
813 if (!expr
->expr_ident
)
815 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
817 base_type
= get_type(expr
->ident_expression
);
818 if (!base_type
|| base_type
->type
!= SYM_FN
)
820 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
821 struct_type
->ident
->name
, expr
->expr_ident
->name
);
822 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
824 } END_FOR_EACH_PTR(expr
);
827 static void global_variable(struct symbol
*sym
)
829 struct symbol
*struct_type
;
833 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
835 struct_type
= get_base_type(sym
);
838 if (struct_type
->type
== SYM_ARRAY
) {
839 struct_type
= get_base_type(struct_type
);
843 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
845 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
848 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
850 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
853 static void call_return_state_hooks_conditional(struct expression
*expr
)
855 struct returned_state_callback
*cb
;
856 struct range_list
*rl
;
858 int final_pass_orig
= final_pass
;
860 __push_fake_cur_stree();
863 __split_whole_condition(expr
->conditional
);
864 final_pass
= final_pass_orig
;
866 if (get_implied_rl(expr
->cond_true
, &rl
))
867 rl
= cast_rl(cur_func_return_type(), rl
);
869 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
870 return_ranges
= show_rl(rl
);
871 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
874 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
875 cb
->callback(return_id
, return_ranges
, expr
);
876 } END_FOR_EACH_PTR(cb
);
878 __push_true_states();
879 __use_false_states();
881 if (get_implied_rl(expr
->cond_false
, &rl
))
882 rl
= cast_rl(cur_func_return_type(), rl
);
884 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
885 return_ranges
= show_rl(rl
);
886 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
889 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
890 cb
->callback(return_id
, return_ranges
, expr
);
891 } END_FOR_EACH_PTR(cb
);
893 __merge_true_states();
894 __free_fake_cur_stree();
897 static void call_return_state_hooks_compare(struct expression
*expr
)
899 struct returned_state_callback
*cb
;
901 int final_pass_orig
= final_pass
;
902 sval_t sval
= { .type
= &int_ctype
};
905 if (!get_implied_value(expr
, &ret
))
908 __push_fake_cur_stree();
911 __split_whole_condition(expr
);
912 final_pass
= final_pass_orig
;
914 if (ret
.value
!= 0) {
915 return_ranges
= alloc_sname("1");
917 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
920 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
921 cb
->callback(return_id
, return_ranges
, expr
);
922 } END_FOR_EACH_PTR(cb
);
925 __push_true_states();
926 __use_false_states();
928 if (ret
.value
!= 1) {
929 return_ranges
= alloc_sname("0");
931 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
934 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
935 cb
->callback(return_id
, return_ranges
, expr
);
936 } END_FOR_EACH_PTR(cb
);
939 __merge_true_states();
940 __free_fake_cur_stree();
943 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
945 struct sm_state
*tmp
;
947 FOR_EACH_PTR(slist
, tmp
) {
948 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
950 } END_FOR_EACH_PTR(tmp
);
955 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
957 struct returned_state_callback
*cb
;
958 struct range_list
*rl
;
960 struct sm_state
*tmp
;
962 int nr_possible
, nr_states
;
963 char *compare_str
= NULL
;
965 struct state_list
*already_handled
= NULL
;
967 if (!sm
|| !sm
->merged
)
970 if (too_many_possible(sm
))
973 /* bail if it gets too complicated */
974 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
975 nr_states
= stree_count(__get_cur_stree());
976 if (option_info
&& nr_states
* nr_possible
>= 2000)
979 FOR_EACH_PTR(sm
->possible
, tmp
) {
982 if (ptr_in_list(tmp
, already_handled
))
984 add_ptr_list(&already_handled
, tmp
);
987 __push_fake_cur_stree();
989 overwrite_states_using_pool(sm
, tmp
);
991 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
992 return_ranges
= show_rl(rl
);
993 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
994 compare_str
= expr_lte_to_param(expr
, -1);
996 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
997 return_ranges
= alloc_sname(buf
);
1001 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1002 cb
->callback(return_id
, return_ranges
, expr
);
1003 } END_FOR_EACH_PTR(cb
);
1005 __free_fake_cur_stree();
1006 } END_FOR_EACH_PTR(tmp
);
1008 free_slist(&already_handled
);
1013 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1015 struct sm_state
*sm
;
1017 if (!expr
|| expr_equal_to_param(expr
, -1))
1020 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1021 return split_possible_helper(sm
, expr
);
1024 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1026 struct range_list
*rl
;
1027 char *return_ranges
;
1036 return alloc_sname("");
1038 if (get_implied_value(expr
, &sval
)) {
1039 *rl_p
= alloc_rl(sval
, sval
);
1040 return sval_to_str(sval
);
1043 compare_str
= expr_equal_to_param(expr
, -1);
1044 math_str
= get_value_in_terms_of_parameter_math(expr
);
1046 if (get_implied_rl(expr
, &rl
)) {
1047 rl
= cast_rl(cur_func_return_type(), rl
);
1048 return_ranges
= show_rl(rl
);
1049 } else if (get_imaginary_absolute(expr
, &rl
)){
1050 rl
= cast_rl(cur_func_return_type(), rl
);
1051 return alloc_sname(show_rl(rl
));
1053 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1054 return_ranges
= show_rl(rl
);
1059 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1060 return alloc_sname(buf
);
1064 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1065 return alloc_sname(buf
);
1068 compare_str
= expr_lte_to_param(expr
, -1);
1070 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1071 return alloc_sname(buf
);
1073 return return_ranges
;
1076 static int split_positive_from_negative(struct expression
*expr
)
1078 struct returned_state_callback
*cb
;
1079 struct range_list
*rl
;
1080 const char *return_ranges
;
1081 struct range_list
*ret_rl
;
1084 /* We're going to print the states 3 times */
1085 if (stree_count(__get_cur_stree()) > 10000 / 3)
1088 if (!get_implied_rl(expr
, &rl
) || !rl
)
1090 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1092 /* Forget about INT_MAX and larger */
1093 if (rl_max(rl
).value
<= 0)
1095 if (!sval_is_negative(rl_min(rl
)))
1098 if (!assume(compare_expression(expr
, '>', zero_expr())))
1102 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1103 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1104 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1105 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1106 } END_FOR_EACH_PTR(cb
);
1110 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1111 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1114 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1115 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1116 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1117 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1118 } END_FOR_EACH_PTR(cb
);
1124 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1127 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1128 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1129 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1130 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1131 } END_FOR_EACH_PTR(cb
);
1139 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1141 struct returned_state_callback
*cb
;
1142 struct range_list
*rl
;
1143 struct range_list
*nonnull_rl
;
1145 struct range_list
*null_rl
= NULL
;
1146 char *return_ranges
;
1147 struct smatch_state
*state
;
1149 int final_pass_orig
= final_pass
;
1151 if (!expr
|| expr_equal_to_param(expr
, -1))
1153 if (expr
->type
== EXPR_CALL
)
1155 if (!is_pointer(expr
))
1158 state
= get_state_expr(SMATCH_EXTRA
, expr
);
1159 if (!state
|| !estate_rl(state
))
1161 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1163 if (!rl_has_sval(estate_rl(state
), sval_type_val(estate_type(state
), 0)))
1166 nr_states
= stree_count(__get_cur_stree());
1167 if (option_info
&& nr_states
>= 1500)
1170 rl
= estate_rl(state
);
1172 __push_fake_cur_stree();
1175 __split_whole_condition(expr
);
1176 final_pass
= final_pass_orig
;
1178 nonnull_rl
= rl_filter(rl
, rl_zero());
1179 return_ranges
= show_rl(nonnull_rl
);
1180 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1183 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1184 cb
->callback(return_id
, return_ranges
, expr
);
1185 } END_FOR_EACH_PTR(cb
);
1187 __push_true_states();
1188 __use_false_states();
1190 return_ranges
= alloc_sname("0");
1191 null_sval
= sval_type_val(rl_type(rl
), 0);
1192 add_range(&null_rl
, null_sval
, null_sval
);
1193 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1195 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1196 cb
->callback(return_id
, return_ranges
, expr
);
1197 } END_FOR_EACH_PTR(cb
);
1199 __merge_true_states();
1200 __free_fake_cur_stree();
1205 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1207 struct range_list
*rl
;
1208 struct range_list
*nonzero_rl
;
1210 struct range_list
*zero_rl
= NULL
;
1212 struct returned_state_callback
*cb
;
1213 char *return_ranges
;
1214 int final_pass_orig
= final_pass
;
1217 if (option_project
!= PROJ_KERNEL
)
1220 nr_states
= stree_count(__get_cur_stree());
1221 if (nr_states
> 1500)
1224 if (get_value(expr
, &val
))
1226 if (!get_implied_rl(expr
, &rl
))
1228 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1230 if (rl_max(rl
).value
!= 0)
1233 __push_fake_cur_stree();
1236 __split_whole_condition(expr
);
1237 final_pass
= final_pass_orig
;
1239 nonzero_rl
= rl_filter(rl
, rl_zero());
1240 return_ranges
= show_rl(nonzero_rl
);
1241 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1244 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1245 cb
->callback(return_id
, return_ranges
, expr
);
1246 } END_FOR_EACH_PTR(cb
);
1248 __push_true_states();
1249 __use_false_states();
1251 return_ranges
= alloc_sname("0");
1252 zero_sval
= sval_type_val(rl_type(rl
), 0);
1253 add_range(&zero_rl
, zero_sval
, zero_sval
);
1254 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1256 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1257 cb
->callback(return_id
, return_ranges
, expr
);
1258 } END_FOR_EACH_PTR(cb
);
1260 __merge_true_states();
1261 __free_fake_cur_stree();
1266 static int is_boolean(struct expression
*expr
)
1268 struct range_list
*rl
;
1270 if (!get_implied_rl(expr
, &rl
))
1272 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1277 static int is_conditional(struct expression
*expr
)
1281 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1286 static int splitable_function_call(struct expression
*expr
)
1288 struct sm_state
*sm
;
1291 if (!expr
|| expr
->type
!= EXPR_CALL
)
1293 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1294 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1295 return split_possible_helper(sm
, expr
);
1298 static struct sm_state
*find_bool_param(void)
1300 struct stree
*start_states
;
1302 struct sm_state
*sm
, *tmp
;
1305 start_states
= get_start_states();
1307 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1308 if (!arg
->ident
|| !arg
->ident
->name
)
1310 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1313 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1314 rl_max(estate_rl(sm
->state
)).value
!= 1)
1317 } END_FOR_EACH_PTR_REVERSE(arg
);
1323 * Check if it's splitable. If not, then splitting it up is likely not
1324 * useful for the callers.
1326 FOR_EACH_PTR(sm
->possible
, tmp
) {
1329 if (!estate_get_single_value(tmp
->state
, &sval
))
1331 } END_FOR_EACH_PTR(tmp
);
1336 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1338 struct returned_state_callback
*cb
;
1339 struct range_list
*ret_rl
;
1340 const char *return_ranges
;
1341 struct sm_state
*tmp
;
1343 int nr_possible
, nr_states
;
1344 char *compare_str
= NULL
;
1346 struct state_list
*already_handled
= NULL
;
1348 if (!sm
|| !sm
->merged
)
1351 if (too_many_possible(sm
))
1354 /* bail if it gets too complicated */
1355 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1356 nr_states
= stree_count(__get_cur_stree());
1357 if (option_info
&& nr_states
* nr_possible
>= 2000)
1360 FOR_EACH_PTR(sm
->possible
, tmp
) {
1363 if (ptr_in_list(tmp
, already_handled
))
1365 add_ptr_list(&already_handled
, tmp
);
1368 __push_fake_cur_stree();
1370 overwrite_states_using_pool(sm
, tmp
);
1372 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1373 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1374 compare_str
= expr_lte_to_param(expr
, -1);
1376 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1377 return_ranges
= alloc_sname(buf
);
1381 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1382 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1383 } END_FOR_EACH_PTR(cb
);
1385 __free_fake_cur_stree();
1386 } END_FOR_EACH_PTR(tmp
);
1388 free_slist(&already_handled
);
1393 static int split_by_bool_param(struct expression
*expr
)
1395 struct sm_state
*start_sm
, *sm
;
1398 start_sm
= find_bool_param();
1401 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
1402 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
1404 return split_on_bool_sm(sm
, expr
);
1407 static void call_return_state_hooks(struct expression
*expr
)
1409 struct returned_state_callback
*cb
;
1410 struct range_list
*ret_rl
;
1411 const char *return_ranges
;
1415 if (__path_is_null())
1418 expr
= strip_expr(expr
);
1420 if (is_impossible_path())
1423 if (expr
&& (expr
->type
== EXPR_COMPARE
||
1424 !get_implied_value(expr
, &sval
)) &&
1425 (is_condition(expr
) || is_boolean(expr
))) {
1426 call_return_state_hooks_compare(expr
);
1428 } else if (is_conditional(expr
)) {
1429 call_return_state_hooks_conditional(expr
);
1431 } else if (call_return_state_hooks_split_possible(expr
)) {
1433 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
1435 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1437 } else if (splitable_function_call(expr
)) {
1439 } else if (split_positive_from_negative(expr
)) {
1441 } else if (split_by_bool_param(expr
)) {
1446 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1447 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1450 nr_states
= stree_count(__get_cur_stree());
1451 if (nr_states
>= 10000) {
1452 match_return_info(return_id
, (char *)return_ranges
, expr
);
1455 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1456 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1457 } END_FOR_EACH_PTR(cb
);
1460 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1462 struct returned_member_callback
*cb
;
1463 struct stree
*stree
;
1464 struct sm_state
*sm
;
1465 struct symbol
*type
;
1467 char member_name
[256];
1470 type
= get_type(expr
);
1471 if (!type
|| type
->type
!= SYM_PTR
)
1473 name
= expr_to_var(expr
);
1477 member_name
[sizeof(member_name
) - 1] = '\0';
1478 strcpy(member_name
, "$");
1481 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1482 stree
= __get_cur_stree();
1483 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1484 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1485 strcpy(member_name
, "*$");
1486 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1489 if (strncmp(sm
->name
, name
, len
) != 0)
1491 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1493 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1494 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1495 } END_FOR_EACH_SM(sm
);
1496 } END_FOR_EACH_PTR(cb
);
1501 static void reset_memdb(struct symbol
*sym
)
1503 mem_sql(NULL
, NULL
, "delete from caller_info;");
1504 mem_sql(NULL
, NULL
, "delete from return_states;");
1505 mem_sql(NULL
, NULL
, "delete from call_implies;");
1508 static void match_end_func_info(struct symbol
*sym
)
1510 if (__path_is_null())
1512 call_return_state_hooks(NULL
);
1515 static void match_after_func(struct symbol
*sym
)
1521 static void init_memdb(void)
1525 const char *schema_files
[] = {
1527 "db/caller_info.schema",
1528 "db/return_states.schema",
1529 "db/function_type_size.schema",
1530 "db/type_size.schema",
1531 "db/call_implies.schema",
1532 "db/function_ptr.schema",
1533 "db/local_values.schema",
1534 "db/function_type_value.schema",
1535 "db/type_value.schema",
1536 "db/function_type_info.schema",
1537 "db/data_info.schema",
1539 static char buf
[4096];
1544 rc
= sqlite3_open(":memory:", &mem_db
);
1545 if (rc
!= SQLITE_OK
) {
1546 printf("Error starting In-Memory database.");
1550 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1551 fd
= open_data_file(schema_files
[i
]);
1556 ret
= read(fd
, buf
, sizeof(buf
));
1557 if (ret
== sizeof(buf
)) {
1558 printf("Schema file too large: %s (limit %zd bytes)",
1559 schema_files
[i
], sizeof(buf
));
1562 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
1563 if (rc
!= SQLITE_OK
) {
1564 fprintf(stderr
, "SQL error #2: %s\n", err
);
1565 fprintf(stderr
, "%s\n", buf
);
1570 void open_smatch_db(void)
1579 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1580 if (rc
!= SQLITE_OK
) {
1587 static void register_common_funcs(void)
1589 struct token
*token
;
1593 if (option_project
== PROJ_NONE
)
1594 strcpy(filename
, "common_functions");
1596 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1598 token
= get_tokens_file(filename
);
1601 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1603 token
= token
->next
;
1604 while (token_type(token
) != TOKEN_STREAMEND
) {
1605 if (token_type(token
) != TOKEN_IDENT
)
1607 func
= alloc_string(show_ident(token
->ident
));
1608 add_ptr_list(&common_funcs
, func
);
1609 token
= token
->next
;
1611 clear_token_alloc();
1615 void register_definition_db_callbacks(int id
)
1617 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1618 add_hook(&global_variable
, BASE_HOOK
);
1619 add_hook(&global_variable
, DECLARATION_HOOK
);
1620 add_split_return_callback(match_return_info
);
1621 add_split_return_callback(print_returned_struct_members
);
1622 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1623 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1624 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
1626 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1627 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1629 register_common_funcs();
1632 void register_db_call_marker(int id
)
1634 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1637 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
1639 struct expression
*arg
;
1641 char member_name
[256];
1646 const char *star
= "";
1648 if (expr
->type
!= EXPR_ASSIGNMENT
)
1650 name
= expr_to_var_sym(expr
->left
, sym
);
1653 if (key
[0] == '*') {
1657 if (strncmp(key
, "$", 1) != 0)
1659 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1661 return alloc_string(member_name
);
1664 while (expr
->type
== EXPR_ASSIGNMENT
)
1665 expr
= strip_expr(expr
->right
);
1666 if (expr
->type
!= EXPR_CALL
)
1669 arg
= get_argument_from_call_expr(expr
->args
, param
);
1673 return get_variable_from_key(arg
, key
, sym
);
1676 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
1684 arg
= strip_expr(arg
);
1686 if (strcmp(key
, "$") == 0)
1687 return expr_to_var_sym(arg
, sym
);
1689 if (strcmp(key
, "*$") == 0) {
1690 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1691 arg
= strip_expr(arg
->unop
);
1692 return expr_to_var_sym(arg
, sym
);
1694 tmp
= expr_to_var_sym(arg
, sym
);
1697 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1699 return alloc_string(buf
);
1703 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1704 arg
= strip_expr(arg
->unop
);
1705 tmp
= expr_to_var_sym(arg
, sym
);
1708 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1709 return alloc_string(buf
);
1712 tmp
= expr_to_var_sym(arg
, sym
);
1715 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1717 return alloc_string(buf
);
1720 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
1724 if (strcmp("$", key
) == 0)
1725 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
1726 return get_variable_from_key(arg
, key
, sym
);
1729 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
1733 static char buf
[256];
1735 if (!sym
|| !sym
->ident
)
1738 param_name
= sym
->ident
->name
;
1739 name_len
= strlen(param_name
);
1741 if (strcmp(name
, param_name
) == 0) {
1743 } else if (name
[name_len
] == '-' && /* check for '-' from "->" */
1744 strncmp(name
, param_name
, name_len
) == 0) {
1745 snprintf(buf
, sizeof(buf
), "$%s", name
+ name_len
);
1747 } else if (name
[0] == '*' && strcmp(name
+ 1, param_name
) == 0) {
1753 const char *get_param_name(struct sm_state
*sm
)
1755 return get_param_name_var_sym(sm
->name
, sm
->sym
);
1758 char *get_data_info_name(struct expression
*expr
)
1765 expr
= strip_expr(expr
);
1766 name
= get_member_name(expr
);
1769 name
= expr_to_var_sym(expr
, &sym
);
1772 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1774 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1775 snprintf(buf
, sizeof(buf
), "static %s", name
);
1777 snprintf(buf
, sizeof(buf
), "global %s", name
);
1778 ret
= alloc_sname(buf
);