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 get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
288 int *row_count
= _row_count
;
293 *row_count
= atoi(argv
[0]);
297 static void sql_select_return_states_pointer(const char *cols
,
298 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
302 ptr
= get_fnptr_name(call
->fn
);
306 run_sql(callback
, info
,
307 "select %s from return_states join function_ptr where "
308 "return_states.function == function_ptr.function and ptr = '%s' "
309 "and searchable = 1 "
310 "order by function_ptr.file, return_states.file, return_id, type;",
314 static int is_local_symbol(struct expression
*expr
)
316 if (expr
->type
!= EXPR_SYMBOL
)
318 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
323 void sql_select_return_states(const char *cols
, struct expression
*call
,
324 int (*callback
)(void*, int, char**, char**), void *info
)
328 if (is_fake_call(call
))
331 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
|| is_local_symbol(call
->fn
)) {
332 sql_select_return_states_pointer(cols
, call
, callback
, info
);
336 if (inlinable(call
->fn
)) {
337 mem_sql(callback
, info
,
338 "select %s from return_states where call_id = '%lu' order by return_id, type;",
339 cols
, (unsigned long)call
);
343 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
344 get_static_filter(call
->fn
->symbol
));
345 if (row_count
> 3000)
348 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
349 cols
, get_static_filter(call
->fn
->symbol
));
352 void sql_select_call_implies(const char *cols
, struct expression
*call
,
353 int (*callback
)(void*, int, char**, char**))
355 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
358 if (inlinable(call
->fn
)) {
359 mem_sql(callback
, call
,
360 "select %s from call_implies where call_id = '%lu';",
361 cols
, (unsigned long)call
);
365 run_sql(callback
, call
, "select %s from call_implies where %s;",
366 cols
, get_static_filter(call
->fn
->symbol
));
369 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
370 int (*callback
)(void*, int, char**, char**))
373 mem_sql(callback
, NULL
,
374 "select %s from caller_info where call_id = %lu;",
375 cols
, (unsigned long)__inline_fn
);
379 run_sql(callback
, NULL
,
380 "select %s from caller_info where %s order by call_id;",
381 cols
, get_static_filter(sym
));
384 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
386 struct def_callback
*def_callback
= __alloc_def_callback(0);
388 def_callback
->hook_type
= type
;
389 def_callback
->callback
= callback
;
390 add_ptr_list(&callbacks
, def_callback
);
394 * These call backs are used when the --info option is turned on to print struct
395 * member information. For example foo->bar could have a state in
396 * smatch_extra.c and also check_user.c.
398 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
400 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
402 member_callback
->owner
= owner
;
403 member_callback
->callback
= callback
;
404 add_ptr_list(&member_callbacks
, member_callback
);
407 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
409 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
411 callback
->callback
= fn
;
412 add_ptr_list(&returned_state_callbacks
, callback
);
415 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
))
417 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
419 member_callback
->owner
= owner
;
420 member_callback
->callback
= callback
;
421 add_ptr_list(&returned_member_callbacks
, member_callback
);
424 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
426 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
429 cb
->callback
= callback
;
430 add_ptr_list(&call_implies_cb_list
, cb
);
434 struct expression
*static_returns_call
;
435 struct symbol
*return_type
;
436 struct range_list
*return_range_list
;
439 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
441 struct return_info
*ret_info
= _ret_info
;
442 struct range_list
*rl
;
443 struct expression
*call_expr
= ret_info
->static_returns_call
;
447 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
448 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
452 struct range_list
*db_return_vals(struct expression
*expr
)
454 struct return_info ret_info
= {};
458 if (is_fake_call(expr
))
461 snprintf(buf
, sizeof(buf
), "return %p", expr
);
462 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
464 return clone_rl(estate_rl(sm
->state
));
465 ret_info
.static_returns_call
= expr
;
466 ret_info
.return_type
= get_type(expr
);
467 if (!ret_info
.return_type
)
470 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
473 ret_info
.return_range_list
= NULL
;
474 if (inlinable(expr
->fn
)) {
475 mem_sql(db_return_callback
, &ret_info
,
476 "select distinct return from return_states where call_id = '%lu';",
477 (unsigned long)expr
);
479 run_sql(db_return_callback
, &ret_info
,
480 "select distinct return from return_states where %s;",
481 get_static_filter(expr
->fn
->symbol
));
483 return ret_info
.return_range_list
;
486 struct range_list
*db_return_vals_from_str(const char *fn_name
)
488 struct return_info ret_info
;
490 ret_info
.static_returns_call
= NULL
;
491 ret_info
.return_type
= &llong_ctype
;
492 ret_info
.return_range_list
= NULL
;
494 run_sql(db_return_callback
, &ret_info
,
495 "select distinct return from return_states where function = '%s';",
497 return ret_info
.return_range_list
;
500 static void match_call_marker(struct expression
*expr
)
503 * we just want to record something in the database so that if we have
504 * two calls like: frob(4); frob(some_unkown); then on the receiving
505 * side we know that sometimes frob is called with unknown parameters.
508 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
511 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
512 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
518 char printed_name
[256];
521 expr
= strip_expr(expr
);
522 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
523 expr
= strip_expr(expr
->unop
);
527 name
= expr_to_var_sym(expr
, &sym
);
532 FOR_EACH_SM(stree
, sm
) {
535 if (strcmp(name
, sm
->name
) == 0) {
537 snprintf(printed_name
, sizeof(printed_name
), "*$");
538 else /* these are already handled. fixme: handle them here */
540 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
541 snprintf(printed_name
, sizeof(printed_name
), "*$");
542 } else if (strncmp(name
, sm
->name
, len
) == 0) {
543 if (isalnum(sm
->name
[len
]))
546 snprintf(printed_name
, sizeof(printed_name
), "$->%s", sm
->name
+ len
+ 1);
548 snprintf(printed_name
, sizeof(printed_name
), "$%s", sm
->name
+ len
);
552 callback(call
, param
, printed_name
, sm
);
553 } END_FOR_EACH_SM(sm
);
558 static void match_call_info(struct expression
*call
)
560 struct member_info_callback
*cb
;
561 struct expression
*arg
;
566 name
= get_fnptr_name(call
->fn
);
570 FOR_EACH_PTR(member_callbacks
, cb
) {
571 stree
= get_all_states_stree(cb
->owner
);
573 FOR_EACH_PTR(call
->args
, arg
) {
574 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
576 } END_FOR_EACH_PTR(arg
);
578 } END_FOR_EACH_PTR(cb
);
583 static int get_param(int param
, char **name
, struct symbol
**sym
)
589 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
591 * this is a temporary hack to work around a bug (I think in sparse?)
592 * 2.6.37-rc1:fs/reiserfs/journal.o
593 * If there is a function definition without parameter name found
594 * after a function implementation then it causes a crash.
598 if (arg
->ident
->name
< (char *)100)
601 *name
= arg
->ident
->name
;
606 } END_FOR_EACH_PTR(arg
);
611 static struct stree
*final_states
;
612 static int prev_func_id
= -1;
613 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
621 struct symbol
*sym
= NULL
;
622 struct def_callback
*def_callback
;
628 func_id
= atoi(argv
[0]);
630 type
= strtol(argv
[1], NULL
, 10);
631 param
= strtol(argv
[2], NULL
, 10);
638 if (prev_func_id
== -1)
639 prev_func_id
= func_id
;
640 if (func_id
!= prev_func_id
) {
641 stree
= __pop_fake_cur_stree();
642 merge_stree(&final_states
, stree
);
644 __push_fake_cur_stree();
646 prev_func_id
= func_id
;
649 if (param
>= 0 && !get_param(param
, &name
, &sym
))
652 FOR_EACH_PTR(callbacks
, def_callback
) {
653 if (def_callback
->hook_type
== type
)
654 def_callback
->callback(name
, sym
, key
, value
);
655 } END_FOR_EACH_PTR(def_callback
);
660 static void get_direct_callers(struct symbol
*sym
)
662 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
663 caller_info_callback
);
666 static struct string_list
*ptr_names_done
;
667 static struct string_list
*ptr_names
;
669 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
671 insert_string(&ptr_names
, alloc_string(argv
[0]));
675 static char *get_next_ptr_name(void)
679 FOR_EACH_PTR(ptr_names
, ptr
) {
680 if (list_has_string(ptr_names_done
, ptr
))
682 insert_string(&ptr_names_done
, ptr
);
684 } END_FOR_EACH_PTR(ptr
);
688 static void get_ptr_names(const char *file
, const char *name
)
690 char sql_filter
[1024];
694 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
697 snprintf(sql_filter
, 1024, "function = '%s';", name
);
700 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
702 run_sql(get_ptr_name
, NULL
,
703 "select distinct ptr from function_ptr where %s",
706 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
710 while ((name
= get_next_ptr_name()))
711 get_ptr_names(NULL
, name
);
714 static void match_data_from_db(struct symbol
*sym
)
719 if (!sym
|| !sym
->ident
)
722 __push_fake_cur_stree();
729 if (sym
->ctype
.modifiers
& MOD_STATIC
)
730 get_ptr_names(get_base_file(), sym
->ident
->name
);
732 get_ptr_names(NULL
, sym
->ident
->name
);
734 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
735 __free_ptr_list((struct ptr_list
**)&ptr_names
);
736 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
737 stree
= __pop_fake_cur_stree();
742 get_direct_callers(sym
);
744 FOR_EACH_PTR(ptr_names
, ptr
) {
745 run_sql(caller_info_callback
, NULL
,
746 "select call_id, type, parameter, key, value"
747 " from caller_info where function = '%s' order by call_id",
750 } END_FOR_EACH_PTR(ptr
);
752 __free_ptr_list((struct ptr_list
**)&ptr_names
);
753 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
755 get_direct_callers(sym
);
758 stree
= __pop_fake_cur_stree();
759 merge_stree(&final_states
, stree
);
762 FOR_EACH_SM(final_states
, sm
) {
764 } END_FOR_EACH_SM(sm
);
766 free_stree(&final_states
);
769 static int call_implies_callbacks(void *_call
, int argc
, char **argv
, char **azColName
)
771 struct expression
*call_expr
= _call
;
772 struct call_implies_callback
*cb
;
773 struct expression
*arg
= NULL
;
780 type
= atoi(argv
[1]);
781 param
= atoi(argv
[2]);
783 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
784 if (cb
->type
!= type
)
787 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
791 cb
->callback(arg
, argv
[3], argv
[4]);
792 } END_FOR_EACH_PTR(cb
);
797 static void match_call_implies(struct expression
*expr
)
799 sql_select_call_implies("function, type, parameter, key, value", expr
,
800 call_implies_callbacks
);
803 static void print_initializer_list(struct expression_list
*expr_list
,
804 struct symbol
*struct_type
)
806 struct expression
*expr
;
807 struct symbol
*base_type
;
808 char struct_name
[256];
810 FOR_EACH_PTR(expr_list
, expr
) {
811 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
812 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
815 if (expr
->type
!= EXPR_IDENTIFIER
)
817 if (!expr
->expr_ident
)
819 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
821 base_type
= get_type(expr
->ident_expression
);
822 if (!base_type
|| base_type
->type
!= SYM_FN
)
824 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
825 struct_type
->ident
->name
, expr
->expr_ident
->name
);
826 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
828 } END_FOR_EACH_PTR(expr
);
831 static void global_variable(struct symbol
*sym
)
833 struct symbol
*struct_type
;
837 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
839 struct_type
= get_base_type(sym
);
842 if (struct_type
->type
== SYM_ARRAY
) {
843 struct_type
= get_base_type(struct_type
);
847 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
849 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
852 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
854 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
857 static void call_return_state_hooks_conditional(struct expression
*expr
)
859 struct returned_state_callback
*cb
;
860 struct range_list
*rl
;
862 int final_pass_orig
= final_pass
;
864 __push_fake_cur_stree();
867 __split_whole_condition(expr
->conditional
);
868 final_pass
= final_pass_orig
;
870 if (get_implied_rl(expr
->cond_true
, &rl
))
871 rl
= cast_rl(cur_func_return_type(), rl
);
873 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
874 return_ranges
= show_rl(rl
);
875 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
878 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
879 cb
->callback(return_id
, return_ranges
, expr
);
880 } END_FOR_EACH_PTR(cb
);
882 __push_true_states();
883 __use_false_states();
885 if (get_implied_rl(expr
->cond_false
, &rl
))
886 rl
= cast_rl(cur_func_return_type(), rl
);
888 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
889 return_ranges
= show_rl(rl
);
890 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
893 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
894 cb
->callback(return_id
, return_ranges
, expr
);
895 } END_FOR_EACH_PTR(cb
);
897 __merge_true_states();
898 __free_fake_cur_stree();
901 static void call_return_state_hooks_compare(struct expression
*expr
)
903 struct returned_state_callback
*cb
;
905 int final_pass_orig
= final_pass
;
906 sval_t sval
= { .type
= &int_ctype
};
909 if (!get_implied_value(expr
, &ret
))
912 __push_fake_cur_stree();
915 __split_whole_condition(expr
);
916 final_pass
= final_pass_orig
;
918 if (ret
.value
!= 0) {
919 return_ranges
= alloc_sname("1");
921 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
924 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
925 cb
->callback(return_id
, return_ranges
, expr
);
926 } END_FOR_EACH_PTR(cb
);
929 __push_true_states();
930 __use_false_states();
932 if (ret
.value
!= 1) {
933 return_ranges
= alloc_sname("0");
935 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
938 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
939 cb
->callback(return_id
, return_ranges
, expr
);
940 } END_FOR_EACH_PTR(cb
);
943 __merge_true_states();
944 __free_fake_cur_stree();
947 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
949 struct sm_state
*tmp
;
951 FOR_EACH_PTR(slist
, tmp
) {
952 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
954 } END_FOR_EACH_PTR(tmp
);
959 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
961 struct returned_state_callback
*cb
;
962 struct range_list
*rl
;
964 struct sm_state
*tmp
;
966 int nr_possible
, nr_states
;
967 char *compare_str
= NULL
;
969 struct state_list
*already_handled
= NULL
;
971 if (!sm
|| !sm
->merged
)
974 if (too_many_possible(sm
))
977 /* bail if it gets too complicated */
978 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
979 nr_states
= stree_count(__get_cur_stree());
980 if (option_info
&& nr_states
* nr_possible
>= 2000)
983 FOR_EACH_PTR(sm
->possible
, tmp
) {
986 if (ptr_in_list(tmp
, already_handled
))
988 add_ptr_list(&already_handled
, tmp
);
991 __push_fake_cur_stree();
993 overwrite_states_using_pool(sm
, tmp
);
995 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
996 return_ranges
= show_rl(rl
);
997 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
998 compare_str
= expr_lte_to_param(expr
, -1);
1000 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1001 return_ranges
= alloc_sname(buf
);
1005 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1006 cb
->callback(return_id
, return_ranges
, expr
);
1007 } END_FOR_EACH_PTR(cb
);
1009 __free_fake_cur_stree();
1010 } END_FOR_EACH_PTR(tmp
);
1012 free_slist(&already_handled
);
1017 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1019 struct sm_state
*sm
;
1021 if (!expr
|| expr_equal_to_param(expr
, -1))
1024 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1025 return split_possible_helper(sm
, expr
);
1028 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1030 struct range_list
*rl
;
1031 char *return_ranges
;
1040 return alloc_sname("");
1042 if (get_implied_value(expr
, &sval
)) {
1043 *rl_p
= alloc_rl(sval
, sval
);
1044 return sval_to_str(sval
);
1047 compare_str
= expr_equal_to_param(expr
, -1);
1048 math_str
= get_value_in_terms_of_parameter_math(expr
);
1050 if (get_implied_rl(expr
, &rl
)) {
1051 rl
= cast_rl(cur_func_return_type(), rl
);
1052 return_ranges
= show_rl(rl
);
1053 } else if (get_imaginary_absolute(expr
, &rl
)){
1054 rl
= cast_rl(cur_func_return_type(), rl
);
1055 return alloc_sname(show_rl(rl
));
1057 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1058 return_ranges
= show_rl(rl
);
1063 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1064 return alloc_sname(buf
);
1068 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1069 return alloc_sname(buf
);
1072 compare_str
= expr_lte_to_param(expr
, -1);
1074 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1075 return alloc_sname(buf
);
1077 return return_ranges
;
1080 static int split_positive_from_negative(struct expression
*expr
)
1082 struct returned_state_callback
*cb
;
1083 struct range_list
*rl
;
1084 const char *return_ranges
;
1085 struct range_list
*ret_rl
;
1088 /* We're going to print the states 3 times */
1089 if (stree_count(__get_cur_stree()) > 10000 / 3)
1092 if (!get_implied_rl(expr
, &rl
) || !rl
)
1094 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1096 /* Forget about INT_MAX and larger */
1097 if (rl_max(rl
).value
<= 0)
1099 if (!sval_is_negative(rl_min(rl
)))
1102 if (!assume(compare_expression(expr
, '>', zero_expr())))
1106 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1107 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1108 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1109 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1110 } END_FOR_EACH_PTR(cb
);
1114 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1115 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1118 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1119 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1120 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1121 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1122 } END_FOR_EACH_PTR(cb
);
1128 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1131 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1132 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1133 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1134 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1135 } END_FOR_EACH_PTR(cb
);
1143 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1145 struct returned_state_callback
*cb
;
1146 struct range_list
*rl
;
1147 struct range_list
*nonnull_rl
;
1149 struct range_list
*null_rl
= NULL
;
1150 char *return_ranges
;
1151 struct smatch_state
*state
;
1153 int final_pass_orig
= final_pass
;
1155 if (!expr
|| expr_equal_to_param(expr
, -1))
1157 if (expr
->type
== EXPR_CALL
)
1159 if (!is_pointer(expr
))
1162 state
= get_state_expr(SMATCH_EXTRA
, expr
);
1163 if (!state
|| !estate_rl(state
))
1165 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1167 if (!rl_has_sval(estate_rl(state
), sval_type_val(estate_type(state
), 0)))
1170 nr_states
= stree_count(__get_cur_stree());
1171 if (option_info
&& nr_states
>= 1500)
1174 rl
= estate_rl(state
);
1176 __push_fake_cur_stree();
1179 __split_whole_condition(expr
);
1180 final_pass
= final_pass_orig
;
1182 nonnull_rl
= rl_filter(rl
, rl_zero());
1183 return_ranges
= show_rl(nonnull_rl
);
1184 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1187 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1188 cb
->callback(return_id
, return_ranges
, expr
);
1189 } END_FOR_EACH_PTR(cb
);
1191 __push_true_states();
1192 __use_false_states();
1194 return_ranges
= alloc_sname("0");
1195 null_sval
= sval_type_val(rl_type(rl
), 0);
1196 add_range(&null_rl
, null_sval
, null_sval
);
1197 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1199 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1200 cb
->callback(return_id
, return_ranges
, expr
);
1201 } END_FOR_EACH_PTR(cb
);
1203 __merge_true_states();
1204 __free_fake_cur_stree();
1209 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1211 struct range_list
*rl
;
1212 struct range_list
*nonzero_rl
;
1214 struct range_list
*zero_rl
= NULL
;
1216 struct returned_state_callback
*cb
;
1217 char *return_ranges
;
1218 int final_pass_orig
= final_pass
;
1221 if (option_project
!= PROJ_KERNEL
)
1224 nr_states
= stree_count(__get_cur_stree());
1225 if (nr_states
> 1500)
1228 if (get_value(expr
, &val
))
1230 if (!get_implied_rl(expr
, &rl
))
1232 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1234 if (rl_max(rl
).value
!= 0)
1237 __push_fake_cur_stree();
1240 __split_whole_condition(expr
);
1241 final_pass
= final_pass_orig
;
1243 nonzero_rl
= rl_filter(rl
, rl_zero());
1244 return_ranges
= show_rl(nonzero_rl
);
1245 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
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 zero_sval
= sval_type_val(rl_type(rl
), 0);
1257 add_range(&zero_rl
, zero_sval
, zero_sval
);
1258 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1260 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1261 cb
->callback(return_id
, return_ranges
, expr
);
1262 } END_FOR_EACH_PTR(cb
);
1264 __merge_true_states();
1265 __free_fake_cur_stree();
1270 static int is_boolean(struct expression
*expr
)
1272 struct range_list
*rl
;
1274 if (!get_implied_rl(expr
, &rl
))
1276 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1281 static int is_conditional(struct expression
*expr
)
1285 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1290 static int splitable_function_call(struct expression
*expr
)
1292 struct sm_state
*sm
;
1295 if (!expr
|| expr
->type
!= EXPR_CALL
)
1297 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1298 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1299 return split_possible_helper(sm
, expr
);
1302 static struct sm_state
*find_bool_param(void)
1304 struct stree
*start_states
;
1306 struct sm_state
*sm
, *tmp
;
1309 start_states
= get_start_states();
1311 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1314 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1317 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1318 rl_max(estate_rl(sm
->state
)).value
!= 1)
1321 } END_FOR_EACH_PTR_REVERSE(arg
);
1327 * Check if it's splitable. If not, then splitting it up is likely not
1328 * useful for the callers.
1330 FOR_EACH_PTR(sm
->possible
, tmp
) {
1333 if (!estate_get_single_value(tmp
->state
, &sval
))
1335 } END_FOR_EACH_PTR(tmp
);
1340 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1342 struct returned_state_callback
*cb
;
1343 struct range_list
*ret_rl
;
1344 const char *return_ranges
;
1345 struct sm_state
*tmp
;
1347 int nr_possible
, nr_states
;
1348 char *compare_str
= NULL
;
1350 struct state_list
*already_handled
= NULL
;
1352 if (!sm
|| !sm
->merged
)
1355 if (too_many_possible(sm
))
1358 /* bail if it gets too complicated */
1359 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1360 nr_states
= stree_count(__get_cur_stree());
1361 if (option_info
&& nr_states
* nr_possible
>= 2000)
1364 FOR_EACH_PTR(sm
->possible
, tmp
) {
1367 if (ptr_in_list(tmp
, already_handled
))
1369 add_ptr_list(&already_handled
, tmp
);
1372 __push_fake_cur_stree();
1374 overwrite_states_using_pool(sm
, tmp
);
1376 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1377 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1378 compare_str
= expr_lte_to_param(expr
, -1);
1380 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1381 return_ranges
= alloc_sname(buf
);
1385 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1386 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1387 } END_FOR_EACH_PTR(cb
);
1389 __free_fake_cur_stree();
1390 } END_FOR_EACH_PTR(tmp
);
1392 free_slist(&already_handled
);
1397 static int split_by_bool_param(struct expression
*expr
)
1399 struct sm_state
*start_sm
, *sm
;
1402 start_sm
= find_bool_param();
1405 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
1406 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
1408 return split_on_bool_sm(sm
, expr
);
1411 static void call_return_state_hooks(struct expression
*expr
)
1413 struct returned_state_callback
*cb
;
1414 struct range_list
*ret_rl
;
1415 const char *return_ranges
;
1419 if (__path_is_null())
1422 expr
= strip_expr(expr
);
1424 if (is_impossible_path())
1427 if (expr
&& (expr
->type
== EXPR_COMPARE
||
1428 !get_implied_value(expr
, &sval
)) &&
1429 (is_condition(expr
) || is_boolean(expr
))) {
1430 call_return_state_hooks_compare(expr
);
1432 } else if (is_conditional(expr
)) {
1433 call_return_state_hooks_conditional(expr
);
1435 } else if (call_return_state_hooks_split_possible(expr
)) {
1437 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
1439 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1441 } else if (splitable_function_call(expr
)) {
1443 } else if (split_positive_from_negative(expr
)) {
1445 } else if (split_by_bool_param(expr
)) {
1450 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1451 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1454 nr_states
= stree_count(__get_cur_stree());
1455 if (nr_states
>= 10000) {
1456 match_return_info(return_id
, (char *)return_ranges
, expr
);
1459 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1460 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1461 } END_FOR_EACH_PTR(cb
);
1464 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1466 struct returned_member_callback
*cb
;
1467 struct stree
*stree
;
1468 struct sm_state
*sm
;
1469 struct symbol
*type
;
1471 char member_name
[256];
1474 type
= get_type(expr
);
1475 if (!type
|| type
->type
!= SYM_PTR
)
1477 name
= expr_to_var(expr
);
1481 member_name
[sizeof(member_name
) - 1] = '\0';
1482 strcpy(member_name
, "$");
1485 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1486 stree
= __get_cur_stree();
1487 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1488 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1489 strcpy(member_name
, "*$");
1490 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1493 if (strncmp(sm
->name
, name
, len
) != 0)
1495 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1497 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1498 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1499 } END_FOR_EACH_SM(sm
);
1500 } END_FOR_EACH_PTR(cb
);
1505 static void reset_memdb(struct symbol
*sym
)
1507 mem_sql(NULL
, NULL
, "delete from caller_info;");
1508 mem_sql(NULL
, NULL
, "delete from return_states;");
1509 mem_sql(NULL
, NULL
, "delete from call_implies;");
1512 static void match_end_func_info(struct symbol
*sym
)
1514 if (__path_is_null())
1516 call_return_state_hooks(NULL
);
1519 static void match_after_func(struct symbol
*sym
)
1525 static void init_memdb(void)
1529 const char *schema_files
[] = {
1531 "db/caller_info.schema",
1532 "db/return_states.schema",
1533 "db/function_type_size.schema",
1534 "db/type_size.schema",
1535 "db/call_implies.schema",
1536 "db/function_ptr.schema",
1537 "db/local_values.schema",
1538 "db/function_type_value.schema",
1539 "db/type_value.schema",
1540 "db/function_type_info.schema",
1541 "db/data_info.schema",
1543 static char buf
[4096];
1548 rc
= sqlite3_open(":memory:", &mem_db
);
1549 if (rc
!= SQLITE_OK
) {
1550 printf("Error starting In-Memory database.");
1554 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1555 fd
= open_data_file(schema_files
[i
]);
1560 ret
= read(fd
, buf
, sizeof(buf
));
1561 if (ret
== sizeof(buf
)) {
1562 printf("Schema file too large: %s (limit %zd bytes)",
1563 schema_files
[i
], sizeof(buf
));
1566 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
1567 if (rc
!= SQLITE_OK
) {
1568 fprintf(stderr
, "SQL error #2: %s\n", err
);
1569 fprintf(stderr
, "%s\n", buf
);
1574 void open_smatch_db(void)
1583 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1584 if (rc
!= SQLITE_OK
) {
1591 static void register_common_funcs(void)
1593 struct token
*token
;
1597 if (option_project
== PROJ_NONE
)
1598 strcpy(filename
, "common_functions");
1600 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1602 token
= get_tokens_file(filename
);
1605 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1607 token
= token
->next
;
1608 while (token_type(token
) != TOKEN_STREAMEND
) {
1609 if (token_type(token
) != TOKEN_IDENT
)
1611 func
= alloc_string(show_ident(token
->ident
));
1612 add_ptr_list(&common_funcs
, func
);
1613 token
= token
->next
;
1615 clear_token_alloc();
1619 void register_definition_db_callbacks(int id
)
1621 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1622 add_hook(&global_variable
, BASE_HOOK
);
1623 add_hook(&global_variable
, DECLARATION_HOOK
);
1624 add_split_return_callback(match_return_info
);
1625 add_split_return_callback(print_returned_struct_members
);
1626 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1627 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1628 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
1630 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1631 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1633 register_common_funcs();
1636 void register_db_call_marker(int id
)
1638 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1641 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
1643 struct expression
*arg
;
1645 char member_name
[256];
1650 const char *star
= "";
1652 if (expr
->type
!= EXPR_ASSIGNMENT
)
1654 name
= expr_to_var_sym(expr
->left
, sym
);
1657 if (key
[0] == '*') {
1661 if (strncmp(key
, "$", 1) != 0)
1663 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1665 return alloc_string(member_name
);
1668 while (expr
->type
== EXPR_ASSIGNMENT
)
1669 expr
= strip_expr(expr
->right
);
1670 if (expr
->type
!= EXPR_CALL
)
1673 arg
= get_argument_from_call_expr(expr
->args
, param
);
1677 return get_variable_from_key(arg
, key
, sym
);
1680 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
1688 arg
= strip_expr(arg
);
1690 if (strcmp(key
, "$") == 0)
1691 return expr_to_var_sym(arg
, sym
);
1693 if (strcmp(key
, "*$") == 0) {
1694 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1695 arg
= strip_expr(arg
->unop
);
1696 return expr_to_var_sym(arg
, sym
);
1698 tmp
= expr_to_var_sym(arg
, sym
);
1701 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1703 return alloc_string(buf
);
1707 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1708 arg
= strip_expr(arg
->unop
);
1709 tmp
= expr_to_var_sym(arg
, sym
);
1712 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1713 return alloc_string(buf
);
1716 tmp
= expr_to_var_sym(arg
, sym
);
1719 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1721 return alloc_string(buf
);
1724 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
1728 if (strcmp("$", key
) == 0)
1729 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
1730 return get_variable_from_key(arg
, key
, sym
);
1733 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
1737 static char buf
[256];
1739 if (!sym
|| !sym
->ident
)
1742 param_name
= sym
->ident
->name
;
1743 name_len
= strlen(param_name
);
1745 if (strcmp(name
, param_name
) == 0) {
1747 } else if (name
[name_len
] == '-' && /* check for '-' from "->" */
1748 strncmp(name
, param_name
, name_len
) == 0) {
1749 snprintf(buf
, sizeof(buf
), "$%s", name
+ name_len
);
1751 } else if (name
[0] == '*' && strcmp(name
+ 1, param_name
) == 0) {
1757 const char *get_param_name(struct sm_state
*sm
)
1759 return get_param_name_var_sym(sm
->name
, sm
->sym
);
1762 char *get_data_info_name(struct expression
*expr
)
1769 expr
= strip_expr(expr
);
1770 name
= get_member_name(expr
);
1773 name
= expr_to_var_sym(expr
, &sym
);
1776 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1778 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1779 snprintf(buf
, sizeof(buf
), "static %s", name
);
1781 snprintf(buf
, sizeof(buf
), "global %s", name
);
1782 ret
= alloc_sname(buf
);