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_data_info(struct expression
*data
, int type
, const char *value
)
261 data_name
= get_data_info_name(data
);
264 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
265 is_static(data
) ? get_base_file() : "extern",
266 data_name
, type
, value
);
269 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
)
271 sql_insert(data_info
, "'%s', '%s', %d, '%s'",
272 (sym
->ctype
.modifiers
& MOD_STATIC
) ? get_base_file() : "extern",
276 char *get_static_filter(struct symbol
*sym
)
278 static char sql_filter
[1024];
280 /* This can only happen on buggy code. Return invalid SQL. */
282 sql_filter
[0] = '\0';
286 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
287 snprintf(sql_filter
, sizeof(sql_filter
),
288 "file = '%s' and function = '%s' and static = '1'",
289 get_base_file(), sym
->ident
->name
);
291 snprintf(sql_filter
, sizeof(sql_filter
),
292 "function = '%s' and static = '0'", sym
->ident
->name
);
298 static int get_row_count(void *_row_count
, int argc
, char **argv
, char **azColName
)
300 int *row_count
= _row_count
;
305 *row_count
= atoi(argv
[0]);
309 static void sql_select_return_states_pointer(const char *cols
,
310 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
315 ptr
= get_fnptr_name(call
->fn
);
319 run_sql(get_row_count
, &return_count
,
320 "select count(*) from return_states join function_ptr "
321 "where return_states.function == function_ptr.function and "
322 "ptr = '%s' and searchable = 1 and type = %d;", ptr
, INTERNAL
);
323 /* The magic number 100 is just from testing on the kernel. */
324 if (return_count
> 100)
327 run_sql(callback
, info
,
328 "select %s from return_states join function_ptr where "
329 "return_states.function == function_ptr.function and ptr = '%s' "
330 "and searchable = 1 "
331 "order by function_ptr.file, return_states.file, return_id, type;",
335 static int is_local_symbol(struct expression
*expr
)
337 if (expr
->type
!= EXPR_SYMBOL
)
339 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
344 void sql_select_return_states(const char *cols
, struct expression
*call
,
345 int (*callback
)(void*, int, char**, char**), void *info
)
349 if (is_fake_call(call
))
352 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
|| is_local_symbol(call
->fn
)) {
353 sql_select_return_states_pointer(cols
, call
, callback
, info
);
357 if (inlinable(call
->fn
)) {
358 mem_sql(callback
, info
,
359 "select %s from return_states where call_id = '%lu' order by return_id, type;",
360 cols
, (unsigned long)call
);
364 run_sql(get_row_count
, &row_count
, "select count(*) from return_states where %s;",
365 get_static_filter(call
->fn
->symbol
));
366 if (row_count
> 3000)
369 run_sql(callback
, info
, "select %s from return_states where %s order by file, return_id, type;",
370 cols
, get_static_filter(call
->fn
->symbol
));
373 void sql_select_call_implies(const char *cols
, struct expression
*call
,
374 int (*callback
)(void*, int, char**, char**))
376 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
379 if (inlinable(call
->fn
)) {
380 mem_sql(callback
, call
,
381 "select %s from call_implies where call_id = '%lu';",
382 cols
, (unsigned long)call
);
386 run_sql(callback
, call
, "select %s from call_implies where %s;",
387 cols
, get_static_filter(call
->fn
->symbol
));
390 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
391 int (*callback
)(void*, int, char**, char**))
394 mem_sql(callback
, NULL
,
395 "select %s from caller_info where call_id = %lu;",
396 cols
, (unsigned long)__inline_fn
);
400 run_sql(callback
, NULL
,
401 "select %s from caller_info where %s order by call_id;",
402 cols
, get_static_filter(sym
));
405 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
407 struct def_callback
*def_callback
= __alloc_def_callback(0);
409 def_callback
->hook_type
= type
;
410 def_callback
->callback
= callback
;
411 add_ptr_list(&callbacks
, def_callback
);
415 * These call backs are used when the --info option is turned on to print struct
416 * member information. For example foo->bar could have a state in
417 * smatch_extra.c and also check_user.c.
419 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
421 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
423 member_callback
->owner
= owner
;
424 member_callback
->callback
= callback
;
425 add_ptr_list(&member_callbacks
, member_callback
);
428 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
430 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
432 callback
->callback
= fn
;
433 add_ptr_list(&returned_state_callbacks
, callback
);
436 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
))
438 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
440 member_callback
->owner
= owner
;
441 member_callback
->callback
= callback
;
442 add_ptr_list(&returned_member_callbacks
, member_callback
);
445 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
447 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
450 cb
->callback
= callback
;
451 add_ptr_list(&call_implies_cb_list
, cb
);
455 struct expression
*static_returns_call
;
456 struct symbol
*return_type
;
457 struct range_list
*return_range_list
;
460 static int db_return_callback(void *_ret_info
, int argc
, char **argv
, char **azColName
)
462 struct return_info
*ret_info
= _ret_info
;
463 struct range_list
*rl
;
464 struct expression
*call_expr
= ret_info
->static_returns_call
;
468 call_results_to_rl(call_expr
, ret_info
->return_type
, argv
[0], &rl
);
469 ret_info
->return_range_list
= rl_union(ret_info
->return_range_list
, rl
);
473 struct range_list
*db_return_vals(struct expression
*expr
)
475 struct return_info ret_info
= {};
479 if (is_fake_call(expr
))
482 snprintf(buf
, sizeof(buf
), "return %p", expr
);
483 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
485 return clone_rl(estate_rl(sm
->state
));
486 ret_info
.static_returns_call
= expr
;
487 ret_info
.return_type
= get_type(expr
);
488 if (!ret_info
.return_type
)
491 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
494 ret_info
.return_range_list
= NULL
;
495 if (inlinable(expr
->fn
)) {
496 mem_sql(db_return_callback
, &ret_info
,
497 "select distinct return from return_states where call_id = '%lu';",
498 (unsigned long)expr
);
500 run_sql(db_return_callback
, &ret_info
,
501 "select distinct return from return_states where %s;",
502 get_static_filter(expr
->fn
->symbol
));
504 return ret_info
.return_range_list
;
507 struct range_list
*db_return_vals_from_str(const char *fn_name
)
509 struct return_info ret_info
;
511 ret_info
.static_returns_call
= NULL
;
512 ret_info
.return_type
= &llong_ctype
;
513 ret_info
.return_range_list
= NULL
;
515 run_sql(db_return_callback
, &ret_info
,
516 "select distinct return from return_states where function = '%s';",
518 return ret_info
.return_range_list
;
521 static void match_call_marker(struct expression
*expr
)
524 * we just want to record something in the database so that if we have
525 * two calls like: frob(4); frob(some_unkown); then on the receiving
526 * side we know that sometimes frob is called with unknown parameters.
529 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
532 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
533 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
539 char printed_name
[256];
542 expr
= strip_expr(expr
);
543 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
544 expr
= strip_expr(expr
->unop
);
548 name
= expr_to_var_sym(expr
, &sym
);
553 FOR_EACH_SM(stree
, sm
) {
556 if (strcmp(name
, sm
->name
) == 0) {
558 snprintf(printed_name
, sizeof(printed_name
), "*$");
559 else /* these are already handled. fixme: handle them here */
561 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
562 snprintf(printed_name
, sizeof(printed_name
), "*$");
563 } else if (strncmp(name
, sm
->name
, len
) == 0) {
564 if (isalnum(sm
->name
[len
]))
567 snprintf(printed_name
, sizeof(printed_name
), "$->%s", sm
->name
+ len
+ 1);
569 snprintf(printed_name
, sizeof(printed_name
), "$%s", sm
->name
+ len
);
573 callback(call
, param
, printed_name
, sm
);
574 } END_FOR_EACH_SM(sm
);
579 static void match_call_info(struct expression
*call
)
581 struct member_info_callback
*cb
;
582 struct expression
*arg
;
587 name
= get_fnptr_name(call
->fn
);
591 FOR_EACH_PTR(member_callbacks
, cb
) {
592 stree
= get_all_states_stree(cb
->owner
);
594 FOR_EACH_PTR(call
->args
, arg
) {
595 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
597 } END_FOR_EACH_PTR(arg
);
599 } END_FOR_EACH_PTR(cb
);
604 static int get_param(int param
, char **name
, struct symbol
**sym
)
610 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
612 * this is a temporary hack to work around a bug (I think in sparse?)
613 * 2.6.37-rc1:fs/reiserfs/journal.o
614 * If there is a function definition without parameter name found
615 * after a function implementation then it causes a crash.
619 if (arg
->ident
->name
< (char *)100)
622 *name
= arg
->ident
->name
;
627 } END_FOR_EACH_PTR(arg
);
632 static struct stree
*final_states
;
633 static int prev_func_id
= -1;
634 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
642 struct symbol
*sym
= NULL
;
643 struct def_callback
*def_callback
;
649 func_id
= atoi(argv
[0]);
651 type
= strtol(argv
[1], NULL
, 10);
652 param
= strtol(argv
[2], NULL
, 10);
659 if (prev_func_id
== -1)
660 prev_func_id
= func_id
;
661 if (func_id
!= prev_func_id
) {
662 stree
= __pop_fake_cur_stree();
663 merge_stree(&final_states
, stree
);
665 __push_fake_cur_stree();
667 prev_func_id
= func_id
;
670 if (param
>= 0 && !get_param(param
, &name
, &sym
))
673 FOR_EACH_PTR(callbacks
, def_callback
) {
674 if (def_callback
->hook_type
== type
)
675 def_callback
->callback(name
, sym
, key
, value
);
676 } END_FOR_EACH_PTR(def_callback
);
681 static void get_direct_callers(struct symbol
*sym
)
683 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
684 caller_info_callback
);
687 static struct string_list
*ptr_names_done
;
688 static struct string_list
*ptr_names
;
690 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
692 insert_string(&ptr_names
, alloc_string(argv
[0]));
696 static char *get_next_ptr_name(void)
700 FOR_EACH_PTR(ptr_names
, ptr
) {
701 if (list_has_string(ptr_names_done
, ptr
))
703 insert_string(&ptr_names_done
, ptr
);
705 } END_FOR_EACH_PTR(ptr
);
709 static void get_ptr_names(const char *file
, const char *name
)
711 char sql_filter
[1024];
715 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
718 snprintf(sql_filter
, 1024, "function = '%s';", name
);
721 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
723 run_sql(get_ptr_name
, NULL
,
724 "select distinct ptr from function_ptr where %s",
727 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
731 while ((name
= get_next_ptr_name()))
732 get_ptr_names(NULL
, name
);
735 static void match_data_from_db(struct symbol
*sym
)
740 if (!sym
|| !sym
->ident
)
743 __push_fake_cur_stree();
750 if (sym
->ctype
.modifiers
& MOD_STATIC
)
751 get_ptr_names(get_base_file(), sym
->ident
->name
);
753 get_ptr_names(NULL
, sym
->ident
->name
);
755 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
756 __free_ptr_list((struct ptr_list
**)&ptr_names
);
757 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
758 stree
= __pop_fake_cur_stree();
763 get_direct_callers(sym
);
765 FOR_EACH_PTR(ptr_names
, ptr
) {
766 run_sql(caller_info_callback
, NULL
,
767 "select call_id, type, parameter, key, value"
768 " from caller_info where function = '%s' order by call_id",
771 } END_FOR_EACH_PTR(ptr
);
773 __free_ptr_list((struct ptr_list
**)&ptr_names
);
774 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
776 get_direct_callers(sym
);
779 stree
= __pop_fake_cur_stree();
780 merge_stree(&final_states
, stree
);
783 FOR_EACH_SM(final_states
, sm
) {
785 } END_FOR_EACH_SM(sm
);
787 free_stree(&final_states
);
790 static int call_implies_callbacks(void *_call
, int argc
, char **argv
, char **azColName
)
792 struct expression
*call_expr
= _call
;
793 struct call_implies_callback
*cb
;
794 struct expression
*arg
= NULL
;
801 type
= atoi(argv
[1]);
802 param
= atoi(argv
[2]);
804 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
805 if (cb
->type
!= type
)
808 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
812 cb
->callback(arg
, argv
[3], argv
[4]);
813 } END_FOR_EACH_PTR(cb
);
818 static void match_call_implies(struct expression
*expr
)
820 sql_select_call_implies("function, type, parameter, key, value", expr
,
821 call_implies_callbacks
);
824 static void print_initializer_list(struct expression_list
*expr_list
,
825 struct symbol
*struct_type
)
827 struct expression
*expr
;
828 struct symbol
*base_type
;
829 char struct_name
[256];
831 FOR_EACH_PTR(expr_list
, expr
) {
832 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
833 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
836 if (expr
->type
!= EXPR_IDENTIFIER
)
838 if (!expr
->expr_ident
)
840 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
842 base_type
= get_type(expr
->ident_expression
);
843 if (!base_type
|| base_type
->type
!= SYM_FN
)
845 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
846 struct_type
->ident
->name
, expr
->expr_ident
->name
);
847 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
849 } END_FOR_EACH_PTR(expr
);
852 static void global_variable(struct symbol
*sym
)
854 struct symbol
*struct_type
;
858 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
860 struct_type
= get_base_type(sym
);
863 if (struct_type
->type
== SYM_ARRAY
) {
864 struct_type
= get_base_type(struct_type
);
868 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
870 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
873 static char *function_signature(void)
875 return type_to_str(get_real_base_type(cur_func_sym
));
878 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
880 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", function_signature());
883 static void call_return_state_hooks_conditional(struct expression
*expr
)
885 struct returned_state_callback
*cb
;
886 struct range_list
*rl
;
888 int final_pass_orig
= final_pass
;
890 __push_fake_cur_stree();
893 __split_whole_condition(expr
->conditional
);
894 final_pass
= final_pass_orig
;
896 if (get_implied_rl(expr
->cond_true
, &rl
))
897 rl
= cast_rl(cur_func_return_type(), rl
);
899 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
900 return_ranges
= show_rl(rl
);
901 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
904 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
905 cb
->callback(return_id
, return_ranges
, expr
);
906 } END_FOR_EACH_PTR(cb
);
908 __push_true_states();
909 __use_false_states();
911 if (get_implied_rl(expr
->cond_false
, &rl
))
912 rl
= cast_rl(cur_func_return_type(), rl
);
914 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
915 return_ranges
= show_rl(rl
);
916 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
919 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
920 cb
->callback(return_id
, return_ranges
, expr
);
921 } END_FOR_EACH_PTR(cb
);
923 __merge_true_states();
924 __free_fake_cur_stree();
927 static void call_return_state_hooks_compare(struct expression
*expr
)
929 struct returned_state_callback
*cb
;
931 int final_pass_orig
= final_pass
;
932 sval_t sval
= { .type
= &int_ctype
};
935 if (!get_implied_value(expr
, &ret
))
938 __push_fake_cur_stree();
941 __split_whole_condition(expr
);
942 final_pass
= final_pass_orig
;
944 if (ret
.value
!= 0) {
945 return_ranges
= alloc_sname("1");
947 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
950 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
951 cb
->callback(return_id
, return_ranges
, expr
);
952 } END_FOR_EACH_PTR(cb
);
955 __push_true_states();
956 __use_false_states();
958 if (ret
.value
!= 1) {
959 return_ranges
= alloc_sname("0");
961 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
964 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
965 cb
->callback(return_id
, return_ranges
, expr
);
966 } END_FOR_EACH_PTR(cb
);
969 __merge_true_states();
970 __free_fake_cur_stree();
973 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
975 struct sm_state
*tmp
;
977 FOR_EACH_PTR(slist
, tmp
) {
978 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
980 } END_FOR_EACH_PTR(tmp
);
985 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
987 struct returned_state_callback
*cb
;
988 struct range_list
*rl
;
990 struct sm_state
*tmp
;
992 int nr_possible
, nr_states
;
993 char *compare_str
= NULL
;
995 struct state_list
*already_handled
= NULL
;
997 if (!sm
|| !sm
->merged
)
1000 if (too_many_possible(sm
))
1003 /* bail if it gets too complicated */
1004 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1005 nr_states
= stree_count(__get_cur_stree());
1006 if (nr_states
* nr_possible
>= 2000)
1009 FOR_EACH_PTR(sm
->possible
, tmp
) {
1012 if (ptr_in_list(tmp
, already_handled
))
1014 add_ptr_list(&already_handled
, tmp
);
1017 __push_fake_cur_stree();
1019 overwrite_states_using_pool(sm
, tmp
);
1021 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
1022 return_ranges
= show_rl(rl
);
1023 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
1024 compare_str
= expr_lte_to_param(expr
, -1);
1026 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1027 return_ranges
= alloc_sname(buf
);
1031 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1032 cb
->callback(return_id
, return_ranges
, expr
);
1033 } END_FOR_EACH_PTR(cb
);
1035 __free_fake_cur_stree();
1036 } END_FOR_EACH_PTR(tmp
);
1038 free_slist(&already_handled
);
1043 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1045 struct sm_state
*sm
;
1047 if (!expr
|| expr_equal_to_param(expr
, -1))
1050 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1051 return split_possible_helper(sm
, expr
);
1054 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1056 struct range_list
*rl
;
1057 char *return_ranges
;
1066 return alloc_sname("");
1068 if (get_implied_value(expr
, &sval
)) {
1069 *rl_p
= alloc_rl(sval
, sval
);
1070 return sval_to_str(sval
);
1073 compare_str
= expr_equal_to_param(expr
, -1);
1074 math_str
= get_value_in_terms_of_parameter_math(expr
);
1076 if (get_implied_rl(expr
, &rl
)) {
1077 rl
= cast_rl(cur_func_return_type(), rl
);
1078 return_ranges
= show_rl(rl
);
1079 } else if (get_imaginary_absolute(expr
, &rl
)){
1080 rl
= cast_rl(cur_func_return_type(), rl
);
1081 return alloc_sname(show_rl(rl
));
1083 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1084 return_ranges
= show_rl(rl
);
1089 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1090 return alloc_sname(buf
);
1094 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1095 return alloc_sname(buf
);
1098 compare_str
= expr_lte_to_param(expr
, -1);
1100 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1101 return alloc_sname(buf
);
1103 return return_ranges
;
1106 static int split_positive_from_negative(struct expression
*expr
)
1108 struct returned_state_callback
*cb
;
1109 struct range_list
*rl
;
1110 const char *return_ranges
;
1111 struct range_list
*ret_rl
;
1114 /* We're going to print the states 3 times */
1115 if (stree_count(__get_cur_stree()) > 10000 / 3)
1118 if (!get_implied_rl(expr
, &rl
) || !rl
)
1120 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1122 /* Forget about INT_MAX and larger */
1123 if (rl_max(rl
).value
<= 0)
1125 if (!sval_is_negative(rl_min(rl
)))
1128 if (!assume(compare_expression(expr
, '>', zero_expr())))
1132 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1133 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1134 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1135 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1136 } END_FOR_EACH_PTR(cb
);
1140 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1141 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1144 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1145 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1146 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1147 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1148 } END_FOR_EACH_PTR(cb
);
1154 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1157 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1158 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1159 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1160 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1161 } END_FOR_EACH_PTR(cb
);
1169 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1171 struct returned_state_callback
*cb
;
1172 struct range_list
*rl
;
1173 struct range_list
*nonnull_rl
;
1175 struct range_list
*null_rl
= NULL
;
1176 char *return_ranges
;
1177 struct smatch_state
*state
;
1179 int final_pass_orig
= final_pass
;
1181 if (!expr
|| expr_equal_to_param(expr
, -1))
1183 if (expr
->type
== EXPR_CALL
)
1185 if (!is_pointer(expr
))
1188 state
= get_state_expr(SMATCH_EXTRA
, expr
);
1189 if (!state
|| !estate_rl(state
))
1191 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1193 if (!rl_has_sval(estate_rl(state
), sval_type_val(estate_type(state
), 0)))
1196 nr_states
= stree_count(__get_cur_stree());
1197 if (option_info
&& nr_states
>= 1500)
1200 rl
= estate_rl(state
);
1202 __push_fake_cur_stree();
1205 __split_whole_condition(expr
);
1206 final_pass
= final_pass_orig
;
1208 nonnull_rl
= rl_filter(rl
, rl_zero());
1209 return_ranges
= show_rl(nonnull_rl
);
1210 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1213 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1214 cb
->callback(return_id
, return_ranges
, expr
);
1215 } END_FOR_EACH_PTR(cb
);
1217 __push_true_states();
1218 __use_false_states();
1220 return_ranges
= alloc_sname("0");
1221 null_sval
= sval_type_val(rl_type(rl
), 0);
1222 add_range(&null_rl
, null_sval
, null_sval
);
1223 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1225 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1226 cb
->callback(return_id
, return_ranges
, expr
);
1227 } END_FOR_EACH_PTR(cb
);
1229 __merge_true_states();
1230 __free_fake_cur_stree();
1235 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1237 struct range_list
*rl
;
1238 struct range_list
*nonzero_rl
;
1240 struct range_list
*zero_rl
= NULL
;
1242 struct returned_state_callback
*cb
;
1243 char *return_ranges
;
1244 int final_pass_orig
= final_pass
;
1247 if (option_project
!= PROJ_KERNEL
)
1250 nr_states
= stree_count(__get_cur_stree());
1251 if (nr_states
> 1500)
1254 if (get_value(expr
, &val
))
1256 if (!get_implied_rl(expr
, &rl
))
1258 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1260 if (rl_max(rl
).value
!= 0)
1263 __push_fake_cur_stree();
1266 __split_whole_condition(expr
);
1267 final_pass
= final_pass_orig
;
1269 nonzero_rl
= rl_filter(rl
, rl_zero());
1270 return_ranges
= show_rl(nonzero_rl
);
1271 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1274 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1275 cb
->callback(return_id
, return_ranges
, expr
);
1276 } END_FOR_EACH_PTR(cb
);
1278 __push_true_states();
1279 __use_false_states();
1281 return_ranges
= alloc_sname("0");
1282 zero_sval
= sval_type_val(rl_type(rl
), 0);
1283 add_range(&zero_rl
, zero_sval
, zero_sval
);
1284 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1286 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1287 cb
->callback(return_id
, return_ranges
, expr
);
1288 } END_FOR_EACH_PTR(cb
);
1290 __merge_true_states();
1291 __free_fake_cur_stree();
1296 static int is_boolean(struct expression
*expr
)
1298 struct range_list
*rl
;
1300 if (!get_implied_rl(expr
, &rl
))
1302 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1307 static int is_conditional(struct expression
*expr
)
1311 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1316 static int splitable_function_call(struct expression
*expr
)
1318 struct sm_state
*sm
;
1321 if (!expr
|| expr
->type
!= EXPR_CALL
)
1323 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1324 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1325 return split_possible_helper(sm
, expr
);
1328 static struct sm_state
*find_bool_param(void)
1330 struct stree
*start_states
;
1332 struct sm_state
*sm
, *tmp
;
1335 start_states
= get_start_states();
1337 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1340 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1343 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1344 rl_max(estate_rl(sm
->state
)).value
!= 1)
1347 } END_FOR_EACH_PTR_REVERSE(arg
);
1353 * Check if it's splitable. If not, then splitting it up is likely not
1354 * useful for the callers.
1356 FOR_EACH_PTR(sm
->possible
, tmp
) {
1359 if (!estate_get_single_value(tmp
->state
, &sval
))
1361 } END_FOR_EACH_PTR(tmp
);
1366 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1368 struct returned_state_callback
*cb
;
1369 struct range_list
*ret_rl
;
1370 const char *return_ranges
;
1371 struct sm_state
*tmp
;
1373 int nr_possible
, nr_states
;
1374 char *compare_str
= NULL
;
1376 struct state_list
*already_handled
= NULL
;
1378 if (!sm
|| !sm
->merged
)
1381 if (too_many_possible(sm
))
1384 /* bail if it gets too complicated */
1385 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1386 nr_states
= stree_count(__get_cur_stree());
1387 if (nr_states
* nr_possible
>= 2000)
1390 FOR_EACH_PTR(sm
->possible
, tmp
) {
1393 if (ptr_in_list(tmp
, already_handled
))
1395 add_ptr_list(&already_handled
, tmp
);
1398 __push_fake_cur_stree();
1400 overwrite_states_using_pool(sm
, tmp
);
1402 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1403 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1404 compare_str
= expr_lte_to_param(expr
, -1);
1406 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1407 return_ranges
= alloc_sname(buf
);
1411 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1412 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1413 } END_FOR_EACH_PTR(cb
);
1415 __free_fake_cur_stree();
1416 } END_FOR_EACH_PTR(tmp
);
1418 free_slist(&already_handled
);
1423 static int split_by_bool_param(struct expression
*expr
)
1425 struct sm_state
*start_sm
, *sm
;
1428 start_sm
= find_bool_param();
1431 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
1432 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
1434 return split_on_bool_sm(sm
, expr
);
1437 static int split_by_null_nonnull_param(struct expression
*expr
)
1440 struct sm_state
*sm
;
1442 /* function must only take one pointer */
1443 if (ptr_list_size((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
) != 1)
1445 arg
= first_ptr_list((struct ptr_list
*)cur_func_sym
->ctype
.base_type
->arguments
);
1448 if (get_real_base_type(arg
)->type
!= SYM_PTR
)
1451 if (param_was_set_var_sym(arg
->ident
->name
, arg
))
1453 sm
= get_sm_state(SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1457 return split_on_bool_sm(sm
, expr
);
1460 static void call_return_state_hooks(struct expression
*expr
)
1462 struct returned_state_callback
*cb
;
1463 struct range_list
*ret_rl
;
1464 const char *return_ranges
;
1468 if (__path_is_null())
1471 expr
= strip_expr(expr
);
1473 if (is_impossible_path())
1476 if (expr
&& (expr
->type
== EXPR_COMPARE
||
1477 !get_implied_value(expr
, &sval
)) &&
1478 (is_condition(expr
) || is_boolean(expr
))) {
1479 call_return_state_hooks_compare(expr
);
1481 } else if (is_conditional(expr
)) {
1482 call_return_state_hooks_conditional(expr
);
1484 } else if (call_return_state_hooks_split_possible(expr
)) {
1486 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
1488 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1490 } else if (splitable_function_call(expr
)) {
1492 } else if (split_positive_from_negative(expr
)) {
1494 } else if (split_by_bool_param(expr
)) {
1495 } else if (split_by_null_nonnull_param(expr
)) {
1500 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1501 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1504 nr_states
= stree_count(__get_cur_stree());
1505 if (nr_states
>= 10000) {
1506 match_return_info(return_id
, (char *)return_ranges
, expr
);
1509 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1510 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1511 } END_FOR_EACH_PTR(cb
);
1514 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1516 struct returned_member_callback
*cb
;
1517 struct stree
*stree
;
1518 struct sm_state
*sm
;
1519 struct symbol
*type
;
1521 char member_name
[256];
1524 type
= get_type(expr
);
1525 if (!type
|| type
->type
!= SYM_PTR
)
1527 name
= expr_to_var(expr
);
1531 member_name
[sizeof(member_name
) - 1] = '\0';
1532 strcpy(member_name
, "$");
1535 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1536 stree
= __get_cur_stree();
1537 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1538 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1539 strcpy(member_name
, "*$");
1540 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1543 if (strncmp(sm
->name
, name
, len
) != 0)
1545 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1547 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1548 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1549 } END_FOR_EACH_SM(sm
);
1550 } END_FOR_EACH_PTR(cb
);
1555 static void reset_memdb(struct symbol
*sym
)
1557 mem_sql(NULL
, NULL
, "delete from caller_info;");
1558 mem_sql(NULL
, NULL
, "delete from return_states;");
1559 mem_sql(NULL
, NULL
, "delete from call_implies;");
1562 static void match_end_func_info(struct symbol
*sym
)
1564 if (__path_is_null())
1566 call_return_state_hooks(NULL
);
1569 static void match_after_func(struct symbol
*sym
)
1575 static void init_memdb(void)
1579 const char *schema_files
[] = {
1581 "db/caller_info.schema",
1582 "db/return_states.schema",
1583 "db/function_type_size.schema",
1584 "db/type_size.schema",
1585 "db/call_implies.schema",
1586 "db/function_ptr.schema",
1587 "db/local_values.schema",
1588 "db/function_type_value.schema",
1589 "db/type_value.schema",
1590 "db/function_type.schema",
1591 "db/data_info.schema",
1593 static char buf
[4096];
1598 rc
= sqlite3_open(":memory:", &mem_db
);
1599 if (rc
!= SQLITE_OK
) {
1600 printf("Error starting In-Memory database.");
1604 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1605 fd
= open_data_file(schema_files
[i
]);
1610 ret
= read(fd
, buf
, sizeof(buf
));
1611 if (ret
== sizeof(buf
)) {
1612 printf("Schema file too large: %s (limit %zd bytes)",
1613 schema_files
[i
], sizeof(buf
));
1616 rc
= sqlite3_exec(mem_db
, buf
, NULL
, NULL
, &err
);
1617 if (rc
!= SQLITE_OK
) {
1618 fprintf(stderr
, "SQL error #2: %s\n", err
);
1619 fprintf(stderr
, "%s\n", buf
);
1624 void open_smatch_db(void)
1633 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1634 if (rc
!= SQLITE_OK
) {
1641 static void register_common_funcs(void)
1643 struct token
*token
;
1647 if (option_project
== PROJ_NONE
)
1648 strcpy(filename
, "common_functions");
1650 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1652 token
= get_tokens_file(filename
);
1655 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1657 token
= token
->next
;
1658 while (token_type(token
) != TOKEN_STREAMEND
) {
1659 if (token_type(token
) != TOKEN_IDENT
)
1661 func
= alloc_string(show_ident(token
->ident
));
1662 add_ptr_list(&common_funcs
, func
);
1663 token
= token
->next
;
1665 clear_token_alloc();
1669 void register_definition_db_callbacks(int id
)
1671 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1672 add_hook(&global_variable
, BASE_HOOK
);
1673 add_hook(&global_variable
, DECLARATION_HOOK
);
1674 add_split_return_callback(match_return_info
);
1675 add_split_return_callback(print_returned_struct_members
);
1676 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1677 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1678 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
1680 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1681 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1683 register_common_funcs();
1686 void register_db_call_marker(int id
)
1688 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1691 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
1693 struct expression
*arg
;
1695 char member_name
[256];
1700 const char *star
= "";
1702 if (expr
->type
!= EXPR_ASSIGNMENT
)
1704 name
= expr_to_var_sym(expr
->left
, sym
);
1707 if (key
[0] == '*') {
1711 if (strncmp(key
, "$", 1) != 0)
1713 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1715 return alloc_string(member_name
);
1718 while (expr
->type
== EXPR_ASSIGNMENT
)
1719 expr
= strip_expr(expr
->right
);
1720 if (expr
->type
!= EXPR_CALL
)
1723 arg
= get_argument_from_call_expr(expr
->args
, param
);
1727 return get_variable_from_key(arg
, key
, sym
);
1730 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
1738 arg
= strip_expr(arg
);
1740 if (strcmp(key
, "$") == 0)
1741 return expr_to_var_sym(arg
, sym
);
1743 if (strcmp(key
, "*$") == 0) {
1744 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1745 arg
= strip_expr(arg
->unop
);
1746 return expr_to_var_sym(arg
, sym
);
1748 tmp
= expr_to_var_sym(arg
, sym
);
1751 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1753 return alloc_string(buf
);
1757 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1758 arg
= strip_expr(arg
->unop
);
1759 tmp
= expr_to_var_sym(arg
, sym
);
1762 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1763 return alloc_string(buf
);
1766 tmp
= expr_to_var_sym(arg
, sym
);
1769 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1771 return alloc_string(buf
);
1774 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
1778 if (strcmp("$", key
) == 0)
1779 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
1780 return get_variable_from_key(arg
, key
, sym
);
1783 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
1787 static char buf
[256];
1789 if (!sym
|| !sym
->ident
)
1792 param_name
= sym
->ident
->name
;
1793 name_len
= strlen(param_name
);
1795 if (strcmp(name
, param_name
) == 0) {
1797 } else if (name
[name_len
] == '-' && /* check for '-' from "->" */
1798 strncmp(name
, param_name
, name_len
) == 0) {
1799 snprintf(buf
, sizeof(buf
), "$%s", name
+ name_len
);
1801 } else if (name
[0] == '*' && strcmp(name
+ 1, param_name
) == 0) {
1807 const char *get_param_name(struct sm_state
*sm
)
1809 return get_param_name_var_sym(sm
->name
, sm
->sym
);
1812 char *get_data_info_name(struct expression
*expr
)
1819 expr
= strip_expr(expr
);
1820 name
= get_member_name(expr
);
1823 name
= expr_to_var_sym(expr
, &sym
);
1826 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1828 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1829 snprintf(buf
, sizeof(buf
), "static %s", name
);
1831 snprintf(buf
, sizeof(buf
), "global %s", name
);
1832 ret
= alloc_sname(buf
);