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); \
56 sm_printf("SQL: insert into " #table " values (" values); \
63 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
65 ALLOCATOR(def_callback
, "definition db hook callbacks");
66 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
67 static struct callback_list
*callbacks
;
69 struct member_info_callback
{
71 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
73 ALLOCATOR(member_info_callback
, "caller_info callbacks");
74 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
75 static struct member_info_cb_list
*member_callbacks
;
77 struct returned_state_callback
{
78 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
80 ALLOCATOR(returned_state_callback
, "returned state callbacks");
81 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
82 static struct returned_state_cb_list
*returned_state_callbacks
;
84 struct returned_member_callback
{
86 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
88 ALLOCATOR(returned_member_callback
, "returned member callbacks");
89 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
90 static struct returned_member_cb_list
*returned_member_callbacks
;
92 struct call_implies_callback
{
94 void (*callback
)(struct expression
*arg
, char *key
, char *value
);
96 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
97 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
98 static struct call_implies_cb_list
*call_implies_cb_list
;
100 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
104 for (i
= 0; i
< argc
; i
++) {
107 sm_printf("%s", argv
[i
]);
113 void debug_sql(const char *sql
)
118 sql_exec(print_sql_output
, NULL
, sql
);
121 void debug_mem_sql(const char *sql
)
126 sql_mem_exec(print_sql_output
, NULL
, sql
);
129 void sql_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
134 if (option_no_db
|| !db
)
137 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
138 if (rc
!= SQLITE_OK
&& !parse_error
) {
139 fprintf(stderr
, "SQL error #2: %s\n", err
);
140 fprintf(stderr
, "SQL: '%s'\n", sql
);
145 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
153 rc
= sqlite3_exec(mem_db
, sql
, callback
, data
, &err
);
154 if (rc
!= SQLITE_OK
) {
155 fprintf(stderr
, "SQL error #2: %s\n", err
);
156 fprintf(stderr
, "SQL: '%s'\n", sql
);
161 void sql_insert_return_states(int return_id
, const char *return_ranges
,
162 int type
, int param
, const char *key
, const char *value
)
164 if (key
&& strlen(key
) >= 80)
166 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
167 get_base_file(), get_function(), (unsigned long)__inline_fn
,
168 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
171 static struct string_list
*common_funcs
;
172 static int is_common_function(const char *fn
)
176 if (strncmp(fn
, "__builtin_", 10) == 0)
179 FOR_EACH_PTR(common_funcs
, tmp
) {
180 if (strcmp(tmp
, fn
) == 0)
182 } END_FOR_EACH_PTR(tmp
);
187 void sql_insert_caller_info(struct expression
*call
, int type
,
188 int param
, const char *key
, const char *value
)
192 if (!option_info
&& !__inline_call
)
195 if (key
&& strlen(key
) >= 80)
198 fn
= get_fnptr_name(call
->fn
);
204 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
205 get_base_file(), get_function(), fn
, (unsigned long)call
,
206 is_static(call
->fn
), type
, param
, key
, value
);
212 if (is_common_function(fn
))
215 sm_msg("SQL_caller_info: insert into caller_info values ("
216 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
217 get_base_file(), get_function(), fn
, is_static(call
->fn
),
218 type
, param
, key
, value
);
223 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
225 sql_insert(function_ptr
, "'%s', '%s', '%s', 0", get_base_file(), fn
,
229 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
231 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
232 get_function(), (unsigned long)__inline_fn
, fn_static(),
233 type
, param
, key
, value
);
236 void sql_insert_function_type_size(const char *member
, const char *ranges
)
238 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
241 void sql_insert_local_values(const char *name
, const char *value
)
243 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
246 void sql_insert_function_type_value(const char *type
, const char *value
)
248 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
251 void sql_insert_function_type(int param
, const char *value
)
253 sql_insert(function_type
, "'%s', '%s', %d, %d, '%s'",
254 get_base_file(), get_function(), fn_static(), param
, value
);
257 void sql_insert_parameter_name(int param
, const char *value
)
259 sql_insert(parameter_name
, "'%s', '%s', %d, %d, '%s'",
260 get_base_file(), get_function(), fn_static(), param
, value
);
263 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
267 data_name
= get_data_info_name(data
);
270 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
271 is_static(data
) ? get_base_file() : "extern",
272 data_name
, type
, value
);
275 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
277 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
278 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
282 char *get_static_filter(struct symbol
*sym
)
284 static char sql_filter
[1024];
286 /* This can only happen on buggy code. Return invalid SQL. */
288 sql_filter
[0] = '\0';
292 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
293 snprintf(sql_filter
, sizeof(sql_filter
),
294 "file = '%s' and function = '%s' and static = '1'",
295 get_base_file(), sym
->ident
->name
);
297 snprintf(sql_filter
, sizeof(sql_filter
),
298 "function = '%s' and static = '0'", sym
->ident
->name
);
304 static int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
306 int *row_count
= _row_count
;
311 *row_count
= atoi(argv
[0]);
315 static void sql_select_return_states_pointer(const char *cols
,
316 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
321 ptr
= get_fnptr_name(call
->fn
);
325 run_sql(get_row_count
, &return_count
,
326 "select count(*) from return_states join function_ptr "
327 "where return_states.function == function_ptr.function and "
328 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
329 /* The magic number 100 is just from testing on the kernel. */
330 if (return_count
> 100)
333 run_sql(callback
, info
,
334 "select %s from return_states join function_ptr where "
335 "return_states.function == function_ptr.function and ptr = '%s' "
336 "and searchable = 1 "
337 "order by function_ptr.file, return_states.file, return_id, type;",
341 static int is_local_symbol(struct expression
*expr
)
343 if (expr
->type
!= EXPR_SYMBOL
)
345 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
350 void sql_select_return_states(const char *cols
, struct expression
*call
,
351 int (*callback
)(void*, int, char**, char**), void *info
)
355 if (is_fake_call(call
))
358 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
|| is_local_symbol(call
->fn
)) {
359 sql_select_return_states_pointer(cols
, call
, callback
, info
);
363 if (inlinable(call
->fn
)) {
364 mem_sql(callback
, info
,
365 "select %s from return_states where call_id = '%lu' order by return_id, type;",
366 cols
, (unsigned long)call
);
370 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
371 get_static_filter(call
->fn
->symbol
));
372 if (row_count
> 3000)
375 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
376 cols
, get_static_filter(call
->fn
->symbol
));
379 void sql_select_call_implies(const char *cols
, struct expression
*call
,
380 int (*callback
)(void*, int, char**, char**))
382 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
385 if (inlinable(call
->fn
)) {
386 mem_sql(callback
, call
,
387 "select %s from call_implies where call_id = '%lu';",
388 cols
, (unsigned long)call
);
392 run_sql(callback
, call
, "select %s from call_implies where %s;",
393 cols
, get_static_filter(call
->fn
->symbol
));
396 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
397 int (*callback
)(void*, int, char**, char**))
400 mem_sql(callback
, NULL
,
401 "select %s from caller_info where call_id = %lu;",
402 cols
, (unsigned long)__inline_fn
);
406 run_sql(callback
, NULL
,
407 "select %s from caller_info where %s order by call_id;",
408 cols
, get_static_filter(sym
));
411 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
413 struct def_callback
*def_callback
= __alloc_def_callback(0);
415 def_callback
->hook_type
= type
;
416 def_callback
->callback
= callback
;
417 add_ptr_list(&callbacks
, def_callback
);
421 * These call backs are used when the --info option is turned on to print struct
422 * member information. For example foo->bar could have a state in
423 * smatch_extra.c and also check_user.c.
425 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
427 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
429 member_callback
->owner
= owner
;
430 member_callback
->callback
= callback
;
431 add_ptr_list(&member_callbacks
, member_callback
);
434 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
436 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
438 callback
->callback
= fn
;
439 add_ptr_list(&returned_state_callbacks
, callback
);
442 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
))
444 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
446 member_callback
->owner
= owner
;
447 member_callback
->callback
= callback
;
448 add_ptr_list(&returned_member_callbacks
, member_callback
);
451 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
453 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
456 cb
->callback
= callback
;
457 add_ptr_list(&call_implies_cb_list
, cb
);
461 struct expression
*static_returns_call
;
462 struct symbol
*return_type
;
463 struct range_list
*return_range_list
;
466 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
468 struct return_info
*ret_info
= _ret_info
;
469 struct range_list
*rl
;
470 struct expression
*call_expr
= ret_info
->static_returns_call
;
474 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
475 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
479 struct range_list
*db_return_vals(struct expression
*expr
)
481 struct return_info ret_info
= {};
485 if (is_fake_call(expr
))
488 snprintf(buf
, sizeof(buf
), "return %p", expr
);
489 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
491 return clone_rl(estate_rl(sm
->state
));
492 ret_info
.static_returns_call
= expr
;
493 ret_info
.return_type
= get_type(expr
);
494 if (!ret_info
.return_type
)
497 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
500 ret_info
.return_range_list
= NULL
;
501 if (inlinable(expr
->fn
)) {
502 mem_sql(db_return_callback
, &ret_info
,
503 "select distinct return from return_states where call_id = '%lu';",
504 (unsigned long)expr
);
506 run_sql(db_return_callback
, &ret_info
,
507 "select distinct return from return_states where %s;",
508 get_static_filter(expr
->fn
->symbol
));
510 return ret_info
.return_range_list
;
513 struct range_list
*db_return_vals_from_str(const char *fn_name
)
515 struct return_info ret_info
;
517 ret_info
.static_returns_call
= NULL
;
518 ret_info
.return_type
= &llong_ctype
;
519 ret_info
.return_range_list
= NULL
;
521 run_sql(db_return_callback
, &ret_info
,
522 "select distinct return from return_states where function = '%s';",
524 return ret_info
.return_range_list
;
527 static void match_call_marker(struct expression
*expr
)
530 * we just want to record something in the database so that if we have
531 * two calls like: frob(4); frob(some_unkown); then on the receiving
532 * side we know that sometimes frob is called with unknown parameters.
535 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
538 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
539 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
545 char printed_name
[256];
548 expr
= strip_expr(expr
);
549 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
550 expr
= strip_expr(expr
->unop
);
554 name
= expr_to_var_sym(expr
, &sym
);
559 FOR_EACH_SM(stree
, sm
) {
562 if (strcmp(name
, sm
->name
) == 0) {
564 snprintf(printed_name
, sizeof(printed_name
), "*$");
565 else /* these are already handled. fixme: handle them here */
567 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
568 snprintf(printed_name
, sizeof(printed_name
), "*$");
569 } else if (strncmp(name
, sm
->name
, len
) == 0) {
570 if (isalnum(sm
->name
[len
]))
573 snprintf(printed_name
, sizeof(printed_name
), "$->%s", sm
->name
+ len
+ 1);
575 snprintf(printed_name
, sizeof(printed_name
), "$%s", sm
->name
+ len
);
579 callback(call
, param
, printed_name
, sm
);
580 } END_FOR_EACH_SM(sm
);
585 static void match_call_info(struct expression
*call
)
587 struct member_info_callback
*cb
;
588 struct expression
*arg
;
593 name
= get_fnptr_name(call
->fn
);
597 FOR_EACH_PTR(member_callbacks
, cb
) {
598 stree
= get_all_states_stree(cb
->owner
);
600 FOR_EACH_PTR(call
->args
, arg
) {
601 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
603 } END_FOR_EACH_PTR(arg
);
605 } END_FOR_EACH_PTR(cb
);
610 static int get_param(int param
, char **name
, struct symbol
**sym
)
616 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
618 * this is a temporary hack to work around a bug (I think in sparse?)
619 * 2.6.37-rc1:fs/reiserfs/journal.o
620 * If there is a function definition without parameter name found
621 * after a function implementation then it causes a crash.
625 if (arg
->ident
->name
< (char *)100)
628 *name
= arg
->ident
->name
;
633 } END_FOR_EACH_PTR(arg
);
638 static struct stree
*final_states
;
639 static int prev_func_id
= -1;
640 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
648 struct symbol
*sym
= NULL
;
649 struct def_callback
*def_callback
;
655 func_id
= atoi(argv
[0]);
657 type
= strtol(argv
[1], NULL
, 10);
658 param
= strtol(argv
[2], NULL
, 10);
665 if (prev_func_id
== -1)
666 prev_func_id
= func_id
;
667 if (func_id
!= prev_func_id
) {
668 stree
= __pop_fake_cur_stree();
669 merge_stree(&final_states
, stree
);
671 __push_fake_cur_stree();
673 prev_func_id
= func_id
;
676 if (param
>= 0 && !get_param(param
, &name
, &sym
))
679 FOR_EACH_PTR(callbacks
, def_callback
) {
680 if (def_callback
->hook_type
== type
)
681 def_callback
->callback(name
, sym
, key
, value
);
682 } END_FOR_EACH_PTR(def_callback
);
687 static void get_direct_callers(struct symbol
*sym
)
689 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
690 caller_info_callback
);
693 static struct string_list
*ptr_names_done
;
694 static struct string_list
*ptr_names
;
696 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
698 insert_string(&ptr_names
, alloc_string(argv
[0]));
702 static char *get_next_ptr_name(void)
706 FOR_EACH_PTR(ptr_names
, ptr
) {
707 if (list_has_string(ptr_names_done
, ptr
))
709 insert_string(&ptr_names_done
, ptr
);
711 } END_FOR_EACH_PTR(ptr
);
715 static void get_ptr_names(const char *file
, const char *name
)
717 char sql_filter
[1024];
721 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
724 snprintf(sql_filter
, 1024, "function = '%s';", name
);
727 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
729 run_sql(get_ptr_name
, NULL
,
730 "select distinct ptr from function_ptr where %s",
733 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
737 while ((name
= get_next_ptr_name()))
738 get_ptr_names(NULL
, name
);
741 static void match_data_from_db(struct symbol
*sym
)
746 if (!sym
|| !sym
->ident
)
749 __push_fake_cur_stree();
756 if (sym
->ctype
.modifiers
& MOD_STATIC
)
757 get_ptr_names(get_base_file(), sym
->ident
->name
);
759 get_ptr_names(NULL
, sym
->ident
->name
);
761 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
762 __free_ptr_list((struct ptr_list
**)&ptr_names
);
763 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
764 stree
= __pop_fake_cur_stree();
769 get_direct_callers(sym
);
771 FOR_EACH_PTR(ptr_names
, ptr
) {
772 run_sql(caller_info_callback
, NULL
,
773 "select call_id, type, parameter, key, value"
774 " from caller_info where function = '%s' order by call_id",
777 } END_FOR_EACH_PTR(ptr
);
779 __free_ptr_list((struct ptr_list
**)&ptr_names
);
780 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
782 get_direct_callers(sym
);
785 stree
= __pop_fake_cur_stree();
786 merge_stree(&final_states
, stree
);
789 FOR_EACH_SM(final_states
, sm
) {
791 } END_FOR_EACH_SM(sm
);
793 free_stree(&final_states
);
796 static int call_implies_callbacks(void *_call
, int argc
, char **argv
, char **azColName
)
798 struct expression
*call_expr
= _call
;
799 struct call_implies_callback
*cb
;
800 struct expression
*arg
= NULL
;
807 type
= atoi(argv
[1]);
808 param
= atoi(argv
[2]);
810 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
811 if (cb
->type
!= type
)
814 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
818 cb
->callback(arg
, argv
[3], argv
[4]);
819 } END_FOR_EACH_PTR(cb
);
824 static void match_call_implies(struct expression
*expr
)
826 sql_select_call_implies("function, type, parameter, key, value", expr
,
827 call_implies_callbacks
);
830 static void print_initializer_list(struct expression_list
*expr_list
,
831 struct symbol
*struct_type
)
833 struct expression
*expr
;
834 struct symbol
*base_type
;
835 char struct_name
[256];
837 FOR_EACH_PTR(expr_list
, expr
) {
838 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
839 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
842 if (expr
->type
!= EXPR_IDENTIFIER
)
844 if (!expr
->expr_ident
)
846 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
848 base_type
= get_type(expr
->ident_expression
);
849 if (!base_type
|| base_type
->type
!= SYM_FN
)
851 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
852 struct_type
->ident
->name
, expr
->expr_ident
->name
);
853 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
855 } END_FOR_EACH_PTR(expr
);
858 static void global_variable(struct symbol
*sym
)
860 struct symbol
*struct_type
;
864 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
866 struct_type
= get_base_type(sym
);
869 if (struct_type
->type
== SYM_ARRAY
) {
870 struct_type
= get_base_type(struct_type
);
874 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
876 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
879 static char *function_signature(void)
881 return type_to_str(get_real_base_type(cur_func_sym
));
884 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
886 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
889 static void call_return_state_hooks_conditional(struct expression
*expr
)
891 struct returned_state_callback
*cb
;
892 struct range_list
*rl
;
894 int final_pass_orig
= final_pass
;
896 __push_fake_cur_stree();
899 __split_whole_condition(expr
->conditional
);
900 final_pass
= final_pass_orig
;
902 if (get_implied_rl(expr
->cond_true
, &rl
))
903 rl
= cast_rl(cur_func_return_type(), rl
);
905 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
906 return_ranges
= show_rl(rl
);
907 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
910 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
911 cb
->callback(return_id
, return_ranges
, expr
);
912 } END_FOR_EACH_PTR(cb
);
914 __push_true_states();
915 __use_false_states();
917 if (get_implied_rl(expr
->cond_false
, &rl
))
918 rl
= cast_rl(cur_func_return_type(), rl
);
920 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
921 return_ranges
= show_rl(rl
);
922 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
925 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
926 cb
->callback(return_id
, return_ranges
, expr
);
927 } END_FOR_EACH_PTR(cb
);
929 __merge_true_states();
930 __free_fake_cur_stree();
933 static void call_return_state_hooks_compare(struct expression
*expr
)
935 struct returned_state_callback
*cb
;
937 int final_pass_orig
= final_pass
;
938 sval_t sval
= { .type
= &int_ctype
};
941 if (!get_implied_value(expr
, &ret
))
944 __push_fake_cur_stree();
947 __split_whole_condition(expr
);
948 final_pass
= final_pass_orig
;
950 if (ret
.value
!= 0) {
951 return_ranges
= alloc_sname("1");
953 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
956 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
957 cb
->callback(return_id
, return_ranges
, expr
);
958 } END_FOR_EACH_PTR(cb
);
961 __push_true_states();
962 __use_false_states();
964 if (ret
.value
!= 1) {
965 return_ranges
= alloc_sname("0");
967 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
970 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
971 cb
->callback(return_id
, return_ranges
, expr
);
972 } END_FOR_EACH_PTR(cb
);
975 __merge_true_states();
976 __free_fake_cur_stree();
979 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
981 struct sm_state
*tmp
;
983 FOR_EACH_PTR(slist
, tmp
) {
984 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
986 } END_FOR_EACH_PTR(tmp
);
991 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
993 struct returned_state_callback
*cb
;
994 struct range_list
*rl
;
996 struct sm_state
*tmp
;
998 int nr_possible
, nr_states
;
999 char *compare_str
= NULL
;
1001 struct state_list
*already_handled
= NULL
;
1003 if (!sm
|| !sm
->merged
)
1006 if (too_many_possible(sm
))
1009 /* bail if it gets too complicated */
1010 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1011 nr_states
= stree_count(__get_cur_stree());
1012 if (nr_states
* nr_possible
>= 2000)
1015 FOR_EACH_PTR(sm
->possible
, tmp
) {
1018 if (ptr_in_list(tmp
, already_handled
))
1020 add_ptr_list(&already_handled
, tmp
);
1023 __push_fake_cur_stree();
1025 overwrite_states_using_pool(sm
, tmp
);
1027 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1028 return_ranges
= show_rl(rl
);
1029 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1030 compare_str
= expr_lte_to_param(expr
, -1);
1032 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1033 return_ranges
= alloc_sname(buf
);
1037 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1038 cb
->callback(return_id
, return_ranges
, expr
);
1039 } END_FOR_EACH_PTR(cb
);
1041 __free_fake_cur_stree();
1042 } END_FOR_EACH_PTR(tmp
);
1044 free_slist(&already_handled
);
1049 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1051 struct sm_state
*sm
;
1053 if (!expr
|| expr_equal_to_param(expr
, -1))
1056 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1057 return split_possible_helper(sm
, expr
);
1060 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1062 struct range_list
*rl
;
1063 char *return_ranges
;
1072 return alloc_sname("");
1074 if (get_implied_value(expr
, &sval
)) {
1075 *rl_p
= alloc_rl(sval
, sval
);
1076 return sval_to_str(sval
);
1079 compare_str
= expr_equal_to_param(expr
, -1);
1080 math_str
= get_value_in_terms_of_parameter_math(expr
);
1082 if (get_implied_rl(expr
, &rl
)) {
1083 rl
= cast_rl(cur_func_return_type(), rl
);
1084 return_ranges
= show_rl(rl
);
1085 } else if (get_imaginary_absolute(expr
, &rl
)){
1086 rl
= cast_rl(cur_func_return_type(), rl
);
1087 return alloc_sname(show_rl(rl
));
1089 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1090 return_ranges
= show_rl(rl
);
1095 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1096 return alloc_sname(buf
);
1100 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1101 return alloc_sname(buf
);
1104 compare_str
= expr_lte_to_param(expr
, -1);
1106 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1107 return alloc_sname(buf
);
1109 return return_ranges
;
1112 static int split_positive_from_negative(struct expression
*expr
)
1114 struct returned_state_callback
*cb
;
1115 struct range_list
*rl
;
1116 const char *return_ranges
;
1117 struct range_list
*ret_rl
;
1120 /* We're going to print the states 3 times */
1121 if (stree_count(__get_cur_stree()) > 10000 / 3)
1124 if (!get_implied_rl(expr
, &rl
) || !rl
)
1126 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1128 /* Forget about INT_MAX and larger */
1129 if (rl_max(rl
).value
<= 0)
1131 if (!sval_is_negative(rl_min(rl
)))
1134 if (!assume(compare_expression(expr
, '>', zero_expr())))
1138 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1139 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1140 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1141 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1142 } END_FOR_EACH_PTR(cb
);
1146 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1147 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1150 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1151 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1152 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1153 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1154 } END_FOR_EACH_PTR(cb
);
1160 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1163 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1164 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1165 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1166 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1167 } END_FOR_EACH_PTR(cb
);
1175 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1177 struct returned_state_callback
*cb
;
1178 struct range_list
*rl
;
1179 struct range_list
*nonnull_rl
;
1181 struct range_list
*null_rl
= NULL
;
1182 char *return_ranges
;
1183 struct smatch_state
*state
;
1185 int final_pass_orig
= final_pass
;
1187 if (!expr
|| expr_equal_to_param(expr
, -1))
1189 if (expr
->type
== EXPR_CALL
)
1191 if (!is_pointer(expr
))
1194 state
= get_state_expr(SMATCH_EXTRA
, expr
);
1195 if (!state
|| !estate_rl(state
))
1197 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1199 if (!rl_has_sval(estate_rl(state
), sval_type_val(estate_type(state
), 0)))
1202 nr_states
= stree_count(__get_cur_stree());
1203 if (option_info
&& nr_states
>= 1500)
1206 rl
= estate_rl(state
);
1208 __push_fake_cur_stree();
1211 __split_whole_condition(expr
);
1212 final_pass
= final_pass_orig
;
1214 nonnull_rl
= rl_filter(rl
, rl_zero());
1215 return_ranges
= show_rl(nonnull_rl
);
1216 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1219 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1220 cb
->callback(return_id
, return_ranges
, expr
);
1221 } END_FOR_EACH_PTR(cb
);
1223 __push_true_states();
1224 __use_false_states();
1226 return_ranges
= alloc_sname("0");
1227 null_sval
= sval_type_val(rl_type(rl
), 0);
1228 add_range(&null_rl
, null_sval
, null_sval
);
1229 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1231 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1232 cb
->callback(return_id
, return_ranges
, expr
);
1233 } END_FOR_EACH_PTR(cb
);
1235 __merge_true_states();
1236 __free_fake_cur_stree();
1241 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1243 struct range_list
*rl
;
1244 struct range_list
*nonzero_rl
;
1246 struct range_list
*zero_rl
= NULL
;
1248 struct returned_state_callback
*cb
;
1249 char *return_ranges
;
1250 int final_pass_orig
= final_pass
;
1253 if (option_project
!= PROJ_KERNEL
)
1256 nr_states
= stree_count(__get_cur_stree());
1257 if (nr_states
> 1500)
1260 if (get_value(expr
, &val
))
1262 if (!get_implied_rl(expr
, &rl
))
1264 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1266 if (rl_max(rl
).value
!= 0)
1269 __push_fake_cur_stree();
1272 __split_whole_condition(expr
);
1273 final_pass
= final_pass_orig
;
1275 nonzero_rl
= rl_filter(rl
, rl_zero());
1276 return_ranges
= show_rl(nonzero_rl
);
1277 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1280 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1281 cb
->callback(return_id
, return_ranges
, expr
);
1282 } END_FOR_EACH_PTR(cb
);
1284 __push_true_states();
1285 __use_false_states();
1287 return_ranges
= alloc_sname("0");
1288 zero_sval
= sval_type_val(rl_type(rl
), 0);
1289 add_range(&zero_rl
, zero_sval
, zero_sval
);
1290 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1292 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1293 cb
->callback(return_id
, return_ranges
, expr
);
1294 } END_FOR_EACH_PTR(cb
);
1296 __merge_true_states();
1297 __free_fake_cur_stree();
1302 static int is_boolean(struct expression
*expr
)
1304 struct range_list
*rl
;
1306 if (!get_implied_rl(expr
, &rl
))
1308 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1313 static int is_conditional(struct expression
*expr
)
1317 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1322 static int splitable_function_call(struct expression
*expr
)
1324 struct sm_state
*sm
;
1327 if (!expr
|| expr
->type
!= EXPR_CALL
)
1329 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1330 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1331 return split_possible_helper(sm
, expr
);
1334 static struct sm_state
*find_bool_param(void)
1336 struct stree
*start_states
;
1338 struct sm_state
*sm
, *tmp
;
1341 start_states
= get_start_states();
1343 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1346 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1349 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1350 rl_max(estate_rl(sm
->state
)).value
!= 1)
1353 } END_FOR_EACH_PTR_REVERSE(arg
);
1359 * Check if it's splitable. If not, then splitting it up is likely not
1360 * useful for the callers.
1362 FOR_EACH_PTR(sm
->possible
, tmp
) {
1365 if (!estate_get_single_value(tmp
->state
, &sval
))
1367 } END_FOR_EACH_PTR(tmp
);
1372 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1374 struct returned_state_callback
*cb
;
1375 struct range_list
*ret_rl
;
1376 const char *return_ranges
;
1377 struct sm_state
*tmp
;
1379 int nr_possible
, nr_states
;
1380 char *compare_str
= NULL
;
1382 struct state_list
*already_handled
= NULL
;
1384 if (!sm
|| !sm
->merged
)
1387 if (too_many_possible(sm
))
1390 /* bail if it gets too complicated */
1391 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1392 nr_states
= stree_count(__get_cur_stree());
1393 if (nr_states
* nr_possible
>= 2000)
1396 FOR_EACH_PTR(sm
->possible
, tmp
) {
1399 if (ptr_in_list(tmp
, already_handled
))
1401 add_ptr_list(&already_handled
, tmp
);
1404 __push_fake_cur_stree();
1406 overwrite_states_using_pool(sm
, tmp
);
1408 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1409 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1410 compare_str
= expr_lte_to_param(expr
, -1);
1412 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1413 return_ranges
= alloc_sname(buf
);
1417 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1418 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1419 } END_FOR_EACH_PTR(cb
);
1421 __free_fake_cur_stree();
1422 } END_FOR_EACH_PTR(tmp
);
1424 free_slist(&already_handled
);
1429 static int split_by_bool_param(struct expression
*expr
)
1431 struct sm_state
*start_sm
, *sm
;
1434 start_sm
= find_bool_param();
1437 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
1438 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
1440 return split_on_bool_sm(sm
, expr
);
1443 static int split_by_null_nonnull_param(struct expression
*expr
)
1446 struct sm_state
*sm
;
1448 /* function must only take one pointer */
1449 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
1451 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
1454 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
1457 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
1459 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1463 return split_on_bool_sm(sm
, expr
);
1466 static void call_return_state_hooks(struct expression
*expr
)
1468 struct returned_state_callback
*cb
;
1469 struct range_list
*ret_rl
;
1470 const char *return_ranges
;
1474 if (__path_is_null())
1477 expr
= strip_expr(expr
);
1479 if (is_impossible_path())
1482 if (expr
&& (expr
->type
== EXPR_COMPARE
||
1483 !get_implied_value(expr
, &sval
)) &&
1484 (is_condition(expr
) || is_boolean(expr
))) {
1485 call_return_state_hooks_compare(expr
);
1487 } else if (is_conditional(expr
)) {
1488 call_return_state_hooks_conditional(expr
);
1490 } else if (call_return_state_hooks_split_possible(expr
)) {
1492 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
1494 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1496 } else if (splitable_function_call(expr
)) {
1498 } else if (split_positive_from_negative(expr
)) {
1500 } else if (split_by_bool_param(expr
)) {
1501 } else if (split_by_null_nonnull_param(expr
)) {
1506 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1507 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1510 nr_states
= stree_count(__get_cur_stree());
1511 if (nr_states
>= 10000) {
1512 match_return_info(return_id
, (char *)return_ranges
, expr
);
1515 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1516 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1517 } END_FOR_EACH_PTR(cb
);
1520 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1522 struct returned_member_callback
*cb
;
1523 struct stree
*stree
;
1524 struct sm_state
*sm
;
1525 struct symbol
*type
;
1527 char member_name
[256];
1530 type
= get_type(expr
);
1531 if (!type
|| type
->type
!= SYM_PTR
)
1533 name
= expr_to_var(expr
);
1537 member_name
[sizeof(member_name
) - 1] = '\0';
1538 strcpy(member_name
, "$");
1541 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1542 stree
= __get_cur_stree();
1543 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1544 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1545 strcpy(member_name
, "*$");
1546 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1549 if (strncmp(sm
->name
, name
, len
) != 0)
1551 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1553 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1554 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1555 } END_FOR_EACH_SM(sm
);
1556 } END_FOR_EACH_PTR(cb
);
1561 static void reset_memdb(struct symbol
*sym
)
1563 mem_sql(NULL
, NULL
, "delete from caller_info;");
1564 mem_sql(NULL
, NULL
, "delete from return_states;");
1565 mem_sql(NULL
, NULL
, "delete from call_implies;");
1568 static void match_end_func_info(struct symbol
*sym
)
1570 if (__path_is_null())
1572 call_return_state_hooks(NULL
);
1575 static void match_after_func(struct symbol
*sym
)
1581 static void init_memdb(void)
1585 const char *schema_files
[] = {
1587 "db/caller_info.schema",
1588 "db/return_states.schema",
1589 "db/function_type_size.schema",
1590 "db/type_size.schema",
1591 "db/call_implies.schema",
1592 "db/function_ptr.schema",
1593 "db/local_values.schema",
1594 "db/function_type_value.schema",
1595 "db/type_value.schema",
1596 "db/function_type.schema",
1597 "db/data_info.schema",
1598 "db/parameter_name.schema",
1600 static char buf
[4096];
1605 rc
= sqlite3_open(":memory:", &mem_db
);
1606 if (rc
!= SQLITE_OK
) {
1607 printf("Error starting In-Memory database.");
1611 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1612 fd
= open_data_file(schema_files
[i
]);
1617 ret
= read(fd
, buf
, sizeof(buf
));
1618 if (ret
== sizeof(buf
)) {
1619 printf("Schema file too large: %s (limit %zd bytes)",
1620 schema_files
[i
], sizeof(buf
));
1623 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
1624 if (rc
!= SQLITE_OK
) {
1625 fprintf(stderr
, "SQL error #2: %s\n", err
);
1626 fprintf(stderr
, "%s\n", buf
);
1631 void open_smatch_db(void)
1640 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1641 if (rc
!= SQLITE_OK
) {
1648 static void register_common_funcs(void)
1650 struct token
*token
;
1654 if (option_project
== PROJ_NONE
)
1655 strcpy(filename
, "common_functions");
1657 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1659 token
= get_tokens_file(filename
);
1662 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1664 token
= token
->next
;
1665 while (token_type(token
) != TOKEN_STREAMEND
) {
1666 if (token_type(token
) != TOKEN_IDENT
)
1668 func
= alloc_string(show_ident(token
->ident
));
1669 add_ptr_list(&common_funcs
, func
);
1670 token
= token
->next
;
1672 clear_token_alloc();
1676 void register_definition_db_callbacks(int id
)
1678 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1679 add_hook(&global_variable
, BASE_HOOK
);
1680 add_hook(&global_variable
, DECLARATION_HOOK
);
1681 add_split_return_callback(match_return_info
);
1682 add_split_return_callback(print_returned_struct_members
);
1683 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1684 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1685 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
1687 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1688 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1690 register_common_funcs();
1693 void register_db_call_marker(int id
)
1695 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1698 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
1700 struct expression
*arg
;
1702 char member_name
[256];
1707 const char *star
= "";
1709 if (expr
->type
!= EXPR_ASSIGNMENT
)
1711 name
= expr_to_var_sym(expr
->left
, sym
);
1714 if (key
[0] == '*') {
1718 if (strncmp(key
, "$", 1) != 0)
1720 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1722 return alloc_string(member_name
);
1725 while (expr
->type
== EXPR_ASSIGNMENT
)
1726 expr
= strip_expr(expr
->right
);
1727 if (expr
->type
!= EXPR_CALL
)
1730 arg
= get_argument_from_call_expr(expr
->args
, param
);
1734 return get_variable_from_key(arg
, key
, sym
);
1737 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
1745 arg
= strip_expr(arg
);
1747 if (strcmp(key
, "$") == 0)
1748 return expr_to_var_sym(arg
, sym
);
1750 if (strcmp(key
, "*$") == 0) {
1751 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1752 arg
= strip_expr(arg
->unop
);
1753 return expr_to_var_sym(arg
, sym
);
1755 tmp
= expr_to_var_sym(arg
, sym
);
1758 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1760 return alloc_string(buf
);
1764 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1765 arg
= strip_expr(arg
->unop
);
1766 tmp
= expr_to_var_sym(arg
, sym
);
1769 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1770 return alloc_string(buf
);
1773 tmp
= expr_to_var_sym(arg
, sym
);
1776 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1778 return alloc_string(buf
);
1781 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
1785 if (strcmp("$", key
) == 0)
1786 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
1787 return get_variable_from_key(arg
, key
, sym
);
1790 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
1794 static char buf
[256];
1796 if (!sym
|| !sym
->ident
)
1799 param_name
= sym
->ident
->name
;
1800 name_len
= strlen(param_name
);
1802 if (strcmp(name
, param_name
) == 0) {
1804 } else if (name
[name_len
] == '-' && /* check for '-' from "->" */
1805 strncmp(name
, param_name
, name_len
) == 0) {
1806 snprintf(buf
, sizeof(buf
), "$%s", name
+ name_len
);
1808 } else if (name
[0] == '*' && strcmp(name
+ 1, param_name
) == 0) {
1814 const char *get_param_name(struct sm_state
*sm
)
1816 return get_param_name_var_sym(sm
->name
, sm
->sym
);
1819 char *get_data_info_name(struct expression
*expr
)
1826 expr
= strip_expr(expr
);
1827 name
= get_member_name(expr
);
1830 name
= expr_to_var_sym(expr
, &sym
);
1833 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1835 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1836 snprintf(buf
, sizeof(buf
), "static %s", name
);
1838 snprintf(buf
, sizeof(buf
), "global %s", name
);
1839 ret
= alloc_sname(buf
);