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, 0, &err); \
47 if (rc != SQLITE_OK) { \
48 fprintf(stderr, "SQL error #2: %s\n", err); \
49 fprintf(stderr, "SQL: '%s'\n", buf); \
55 sm_printf("SQL: insert into " #table " values (" values); \
62 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
64 ALLOCATOR(def_callback
, "definition db hook callbacks");
65 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
66 static struct callback_list
*callbacks
;
68 struct member_info_callback
{
70 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
);
72 ALLOCATOR(member_info_callback
, "caller_info callbacks");
73 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
74 static struct member_info_cb_list
*member_callbacks
;
76 struct returned_state_callback
{
77 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
79 ALLOCATOR(returned_state_callback
, "returned state callbacks");
80 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
81 static struct returned_state_cb_list
*returned_state_callbacks
;
83 struct returned_member_callback
{
85 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
);
87 ALLOCATOR(returned_member_callback
, "returned member callbacks");
88 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
89 static struct returned_member_cb_list
*returned_member_callbacks
;
91 struct call_implies_callback
{
93 void (*callback
)(struct expression
*arg
, char *key
, char *value
);
95 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
96 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
97 static struct call_implies_cb_list
*call_implies_cb_list
;
99 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
103 for (i
= 0; i
< argc
; i
++) {
106 sm_printf("%s", argv
[i
]);
112 void debug_sql(const char *sql
)
117 sql_exec(print_sql_output
, NULL
, sql
);
120 void debug_mem_sql(const char *sql
)
125 sql_mem_exec(print_sql_output
, NULL
, sql
);
128 void sql_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
133 if (option_no_db
|| !db
)
136 rc
= sqlite3_exec(db
, sql
, callback
, data
, &err
);
137 if (rc
!= SQLITE_OK
) {
138 fprintf(stderr
, "SQL error #2: %s\n", err
);
139 fprintf(stderr
, "SQL: '%s'\n", sql
);
143 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
)
151 rc
= sqlite3_exec(mem_db
, sql
, callback
, data
, &err
);
152 if (rc
!= SQLITE_OK
) {
153 fprintf(stderr
, "SQL error #2: %s\n", err
);
154 fprintf(stderr
, "SQL: '%s'\n", sql
);
158 void sql_insert_return_states(int return_id
, const char *return_ranges
,
159 int type
, int param
, const char *key
, const char *value
)
161 if (key
&& strlen(key
) >= 80)
163 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
164 get_base_file(), get_function(), (unsigned long)__inline_fn
,
165 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
168 static struct string_list
*common_funcs
;
169 static int is_common_function(const char *fn
)
173 if (strncmp(fn
, "__builtin_", 10) == 0)
176 FOR_EACH_PTR(common_funcs
, tmp
) {
177 if (strcmp(tmp
, fn
) == 0)
179 } END_FOR_EACH_PTR(tmp
);
184 void sql_insert_caller_info(struct expression
*call
, int type
,
185 int param
, const char *key
, const char *value
)
189 if (!option_info
&& !__inline_call
)
192 if (key
&& strlen(key
) >= 80)
195 fn
= get_fnptr_name(call
->fn
);
201 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
202 get_base_file(), get_function(), fn
, (unsigned long)call
,
203 is_static(call
->fn
), type
, param
, key
, value
);
209 if (is_common_function(fn
))
212 sm_msg("SQL_caller_info: insert into caller_info values ("
213 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
214 get_base_file(), get_function(), fn
, is_static(call
->fn
),
215 type
, param
, key
, value
);
220 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
222 sql_insert(function_ptr
, "'%s', '%s', '%s', 0", get_base_file(), fn
,
226 void sql_insert_call_implies(int type
, int param
, const char *key
, const char *value
)
228 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
229 get_function(), (unsigned long)__inline_fn
, fn_static(),
230 type
, param
, key
, value
);
233 void sql_insert_function_type_size(const char *member
, const char *ranges
)
235 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
238 void sql_insert_local_values(const char *name
, const char *value
)
240 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
243 void sql_insert_function_type_value(const char *type
, const char *value
)
245 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
248 void sql_insert_function_type_info(int param
, const char *value
)
250 sql_insert(function_type_info
, "'%s', '%s', %d, %d, '%s'",
251 get_base_file(), get_function(), fn_static(), param
, value
);
254 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
)
258 data_name
= get_data_info_name(data
);
261 sql_insert(data_info
, "'%s', '%s', %d, '%s'", get_base_file(), data_name
, type
, value
);
264 char *get_static_filter(struct symbol
*sym
)
266 static char sql_filter
[1024];
268 /* This can only happen on buggy code. Return invalid SQL. */
270 sql_filter
[0] = '\0';
274 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
275 snprintf(sql_filter
, sizeof(sql_filter
),
276 "file = '%s' and function = '%s' and static = '1'",
277 get_base_file(), sym
->ident
->name
);
279 snprintf(sql_filter
, sizeof(sql_filter
),
280 "function = '%s' and static = '0'", sym
->ident
->name
);
286 static int row_count
;
287 static int get_row_count(void *unused
, int argc
, char **argv
, char **azColName
)
291 row_count
= atoi(argv
[0]);
295 static void sql_select_return_states_pointer(const char *cols
,
296 struct expression
*call
, int (*callback
)(void*, int, char**, char**), void *info
)
300 ptr
= get_fnptr_name(call
->fn
);
304 run_sql(callback
, info
,
305 "select %s from return_states join function_ptr where "
306 "return_states.function == function_ptr.function and ptr = '%s'"
307 "and searchable = 1 order by return_id, type;",
311 static int is_local_symbol(struct expression
*expr
)
313 if (expr
->type
!= EXPR_SYMBOL
)
315 if (expr
->symbol
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
))
320 void sql_select_return_states(const char *cols
, struct expression
*call
,
321 int (*callback
)(void*, int, char**, char**), void *info
)
323 if (is_fake_call(call
))
326 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
|| is_local_symbol(call
->fn
)) {
327 sql_select_return_states_pointer(cols
, call
, callback
, info
);
331 if (inlinable(call
->fn
)) {
332 mem_sql(callback
, info
,
333 "select %s from return_states where call_id = '%lu' order by return_id, type;",
334 cols
, (unsigned long)call
);
339 run_sql(get_row_count
, info
, "select count(*) from return_states where %s;",
340 get_static_filter(call
->fn
->symbol
));
341 if (row_count
> 3000)
344 run_sql(callback
, info
, "select %s from return_states where %s order by return_id, type;",
345 cols
, get_static_filter(call
->fn
->symbol
));
348 void sql_select_call_implies(const char *cols
, struct expression
*call
,
349 int (*callback
)(void*, int, char**, char**))
351 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
354 if (inlinable(call
->fn
)) {
355 mem_sql(callback
, NULL
,
356 "select %s from call_implies where call_id = '%lu';",
357 cols
, (unsigned long)call
);
361 run_sql(callback
, NULL
, "select %s from call_implies where %s;",
362 cols
, get_static_filter(call
->fn
->symbol
));
365 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
366 int (*callback
)(void*, int, char**, char**))
369 mem_sql(callback
, NULL
,
370 "select %s from caller_info where call_id = %lu;",
371 cols
, (unsigned long)__inline_fn
);
375 run_sql(callback
, NULL
,
376 "select %s from caller_info where %s order by call_id;",
377 cols
, get_static_filter(sym
));
380 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
382 struct def_callback
*def_callback
= __alloc_def_callback(0);
384 def_callback
->hook_type
= type
;
385 def_callback
->callback
= callback
;
386 add_ptr_list(&callbacks
, def_callback
);
390 * These call backs are used when the --info option is turned on to print struct
391 * member information. For example foo->bar could have a state in
392 * smatch_extra.c and also check_user.c.
394 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
396 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
398 member_callback
->owner
= owner
;
399 member_callback
->callback
= callback
;
400 add_ptr_list(&member_callbacks
, member_callback
);
403 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
405 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
407 callback
->callback
= fn
;
408 add_ptr_list(&returned_state_callbacks
, callback
);
411 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, struct expression
*expr
, char *printed_name
, struct smatch_state
*state
))
413 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
415 member_callback
->owner
= owner
;
416 member_callback
->callback
= callback
;
417 add_ptr_list(&returned_member_callbacks
, member_callback
);
420 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *key
, char *value
))
422 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
425 cb
->callback
= callback
;
426 add_ptr_list(&call_implies_cb_list
, cb
);
429 static struct expression
*static_call_expr
;
430 static struct expression
*static_returns_call
;
431 static struct symbol
*return_type
;
432 static struct range_list
*return_range_list
;
433 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
435 struct range_list
*rl
;
436 struct expression
*call_expr
= static_returns_call
;
440 call_results_to_rl(call_expr
, return_type
, argv
[0], &rl
);
441 return_range_list
= rl_union(return_range_list
, rl
);
445 struct range_list
*db_return_vals(struct expression
*expr
)
450 if (is_fake_call(expr
))
453 snprintf(buf
, sizeof(buf
), "return %p", expr
);
454 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
456 return clone_rl(estate_rl(sm
->state
));
457 static_returns_call
= expr
;
458 return_type
= get_type(expr
);
462 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
465 return_range_list
= NULL
;
466 if (inlinable(expr
->fn
)) {
467 mem_sql(db_return_callback
, NULL
,
468 "select distinct return from return_states where call_id = '%lu';",
469 (unsigned long)expr
);
471 run_sql(db_return_callback
, NULL
,
472 "select distinct return from return_states where %s;",
473 get_static_filter(expr
->fn
->symbol
));
475 return return_range_list
;
478 struct range_list
*db_return_vals_from_str(const char *fn_name
)
480 static_returns_call
= NULL
;
481 return_type
= &llong_ctype
;
483 return_range_list
= NULL
;
484 run_sql(db_return_callback
, NULL
,
485 "select distinct return from return_states where function = '%s';",
487 return return_range_list
;
490 static void match_call_marker(struct expression
*expr
)
493 * we just want to record something in the database so that if we have
494 * two calls like: frob(4); frob(some_unkown); then on the receiving
495 * side we know that sometimes frob is called with unknown parameters.
498 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
501 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
502 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
))
508 char printed_name
[256];
511 expr
= strip_expr(expr
);
512 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
513 expr
= strip_expr(expr
->unop
);
517 name
= expr_to_var_sym(expr
, &sym
);
522 FOR_EACH_SM(stree
, sm
) {
525 if (strcmp(name
, sm
->name
) == 0) {
527 snprintf(printed_name
, sizeof(printed_name
), "*$");
528 else /* these are already handled. fixme: handle them here */
530 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
531 snprintf(printed_name
, sizeof(printed_name
), "*$");
532 } else if (strncmp(name
, sm
->name
, len
) == 0) {
533 if (isalnum(sm
->name
[len
]))
536 snprintf(printed_name
, sizeof(printed_name
), "$->%s", sm
->name
+ len
+ 1);
538 snprintf(printed_name
, sizeof(printed_name
), "$%s", sm
->name
+ len
);
542 callback(call
, param
, printed_name
, sm
);
543 } END_FOR_EACH_SM(sm
);
548 static void match_call_info(struct expression
*call
)
550 struct member_info_callback
*cb
;
551 struct expression
*arg
;
556 name
= get_fnptr_name(call
->fn
);
560 FOR_EACH_PTR(member_callbacks
, cb
) {
561 stree
= get_all_states_stree(cb
->owner
);
563 FOR_EACH_PTR(call
->args
, arg
) {
564 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
566 } END_FOR_EACH_PTR(arg
);
568 } END_FOR_EACH_PTR(cb
);
573 static int get_param(int param
, char **name
, struct symbol
**sym
)
579 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
581 * this is a temporary hack to work around a bug (I think in sparse?)
582 * 2.6.37-rc1:fs/reiserfs/journal.o
583 * If there is a function definition without parameter name found
584 * after a function implementation then it causes a crash.
588 if (arg
->ident
->name
< (char *)100)
591 *name
= arg
->ident
->name
;
596 } END_FOR_EACH_PTR(arg
);
601 static struct stree
*final_states
;
602 static int prev_func_id
= -1;
603 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
611 struct symbol
*sym
= NULL
;
612 struct def_callback
*def_callback
;
618 func_id
= atoi(argv
[0]);
620 type
= strtol(argv
[1], NULL
, 10);
621 param
= strtol(argv
[2], NULL
, 10);
628 if (prev_func_id
== -1)
629 prev_func_id
= func_id
;
630 if (func_id
!= prev_func_id
) {
631 stree
= __pop_fake_cur_stree();
632 merge_stree(&final_states
, stree
);
634 __push_fake_cur_stree();
636 prev_func_id
= func_id
;
639 if (param
>= 0 && !get_param(param
, &name
, &sym
))
642 FOR_EACH_PTR(callbacks
, def_callback
) {
643 if (def_callback
->hook_type
== type
)
644 def_callback
->callback(name
, sym
, key
, value
);
645 } END_FOR_EACH_PTR(def_callback
);
650 static void get_direct_callers(struct symbol
*sym
)
652 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
653 caller_info_callback
);
656 static struct string_list
*ptr_names_done
;
657 static struct string_list
*ptr_names
;
659 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
661 insert_string(&ptr_names
, alloc_string(argv
[0]));
665 static char *get_next_ptr_name(void)
669 FOR_EACH_PTR(ptr_names
, ptr
) {
670 if (list_has_string(ptr_names_done
, ptr
))
672 insert_string(&ptr_names_done
, ptr
);
674 } END_FOR_EACH_PTR(ptr
);
678 static void get_ptr_names(const char *file
, const char *name
)
680 char sql_filter
[1024];
684 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
687 snprintf(sql_filter
, 1024, "function = '%s';", name
);
690 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
692 run_sql(get_ptr_name
, NULL
,
693 "select distinct ptr from function_ptr where %s",
696 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
700 while ((name
= get_next_ptr_name()))
701 get_ptr_names(NULL
, name
);
704 static void match_data_from_db(struct symbol
*sym
)
709 if (!sym
|| !sym
->ident
)
712 __push_fake_cur_stree();
719 if (sym
->ctype
.modifiers
& MOD_STATIC
)
720 get_ptr_names(get_base_file(), sym
->ident
->name
);
722 get_ptr_names(NULL
, sym
->ident
->name
);
724 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
725 __free_ptr_list((struct ptr_list
**)&ptr_names
);
726 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
727 stree
= __pop_fake_cur_stree();
732 get_direct_callers(sym
);
734 FOR_EACH_PTR(ptr_names
, ptr
) {
735 run_sql(caller_info_callback
, NULL
,
736 "select call_id, type, parameter, key, value"
737 " from caller_info where function = '%s' order by call_id",
740 } END_FOR_EACH_PTR(ptr
);
742 __free_ptr_list((struct ptr_list
**)&ptr_names
);
743 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
745 get_direct_callers(sym
);
748 stree
= __pop_fake_cur_stree();
749 merge_stree(&final_states
, stree
);
752 FOR_EACH_SM(final_states
, sm
) {
754 } END_FOR_EACH_SM(sm
);
756 free_stree(&final_states
);
759 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
761 struct expression
*call_expr
= static_call_expr
;
762 struct call_implies_callback
*cb
;
763 struct expression
*arg
= NULL
;
770 type
= atoi(argv
[1]);
771 param
= atoi(argv
[2]);
773 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
774 if (cb
->type
!= type
)
777 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
781 cb
->callback(arg
, argv
[3], argv
[4]);
782 } END_FOR_EACH_PTR(cb
);
787 static void match_call_implies(struct expression
*expr
)
789 static_call_expr
= expr
;
790 sql_select_call_implies("function, type, parameter, key, value", expr
,
791 call_implies_callbacks
);
794 static void print_initializer_list(struct expression_list
*expr_list
,
795 struct symbol
*struct_type
)
797 struct expression
*expr
;
798 struct symbol
*base_type
;
799 char struct_name
[256];
801 FOR_EACH_PTR(expr_list
, expr
) {
802 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
803 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
806 if (expr
->type
!= EXPR_IDENTIFIER
)
808 if (!expr
->expr_ident
)
810 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
812 base_type
= get_type(expr
->ident_expression
);
813 if (!base_type
|| base_type
->type
!= SYM_FN
)
815 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
816 struct_type
->ident
->name
, expr
->expr_ident
->name
);
817 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
819 } END_FOR_EACH_PTR(expr
);
822 static void global_variable(struct symbol
*sym
)
824 struct symbol
*struct_type
;
828 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
830 struct_type
= get_base_type(sym
);
833 if (struct_type
->type
== SYM_ARRAY
) {
834 struct_type
= get_base_type(struct_type
);
838 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
840 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
843 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
845 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
848 static void call_return_state_hooks_conditional(struct expression
*expr
)
850 struct returned_state_callback
*cb
;
851 struct range_list
*rl
;
853 int final_pass_orig
= final_pass
;
855 __push_fake_cur_stree();
858 __split_whole_condition(expr
->conditional
);
859 final_pass
= final_pass_orig
;
861 if (get_implied_rl(expr
->cond_true
, &rl
))
862 rl
= cast_rl(cur_func_return_type(), rl
);
864 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
865 return_ranges
= show_rl(rl
);
866 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
869 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
870 cb
->callback(return_id
, return_ranges
, expr
);
871 } END_FOR_EACH_PTR(cb
);
873 __push_true_states();
874 __use_false_states();
876 if (get_implied_rl(expr
->cond_false
, &rl
))
877 rl
= cast_rl(cur_func_return_type(), rl
);
879 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
880 return_ranges
= show_rl(rl
);
881 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(rl
));
884 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
885 cb
->callback(return_id
, return_ranges
, expr
);
886 } END_FOR_EACH_PTR(cb
);
888 __merge_true_states();
889 __free_fake_cur_stree();
892 static void call_return_state_hooks_compare(struct expression
*expr
)
894 struct returned_state_callback
*cb
;
896 int final_pass_orig
= final_pass
;
897 sval_t sval
= { .type
= &int_ctype
};
899 __push_fake_cur_stree();
902 __split_whole_condition(expr
);
903 final_pass
= final_pass_orig
;
905 return_ranges
= alloc_sname("1");
907 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
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 return_ranges
= alloc_sname("0");
919 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_sval(sval
));
921 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
922 cb
->callback(return_id
, return_ranges
, expr
);
923 } END_FOR_EACH_PTR(cb
);
925 __merge_true_states();
926 __free_fake_cur_stree();
929 static int ptr_in_list(struct sm_state
*sm
, struct state_list
*slist
)
931 struct sm_state
*tmp
;
933 FOR_EACH_PTR(slist
, tmp
) {
934 if (strcmp(tmp
->state
->name
, sm
->state
->name
) == 0)
936 } END_FOR_EACH_PTR(tmp
);
941 static int split_possible_helper(struct sm_state
*sm
, struct expression
*expr
)
943 struct returned_state_callback
*cb
;
944 struct range_list
*rl
;
946 struct sm_state
*tmp
;
948 int nr_possible
, nr_states
;
949 char *compare_str
= NULL
;
951 struct state_list
*already_handled
= NULL
;
953 if (!sm
|| !sm
->merged
)
956 if (too_many_possible(sm
))
959 /* bail if it gets too complicated */
960 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
961 nr_states
= stree_count(__get_cur_stree());
962 if (option_info
&& nr_states
* nr_possible
>= 2000)
965 FOR_EACH_PTR(sm
->possible
, tmp
) {
968 if (ptr_in_list(tmp
, already_handled
))
970 add_ptr_list(&already_handled
, tmp
);
973 __push_fake_cur_stree();
975 overwrite_states_using_pool(sm
, tmp
);
977 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
978 return_ranges
= show_rl(rl
);
979 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(clone_rl(rl
)));
980 compare_str
= expr_lte_to_param(expr
, -1);
982 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
983 return_ranges
= alloc_sname(buf
);
987 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
988 cb
->callback(return_id
, return_ranges
, expr
);
989 } END_FOR_EACH_PTR(cb
);
991 __free_fake_cur_stree();
992 } END_FOR_EACH_PTR(tmp
);
994 free_slist(&already_handled
);
999 static int call_return_state_hooks_split_possible(struct expression
*expr
)
1001 struct sm_state
*sm
;
1003 if (!expr
|| expr_equal_to_param(expr
, -1))
1006 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
1007 return split_possible_helper(sm
, expr
);
1010 static const char *get_return_ranges_str(struct expression
*expr
, struct range_list
**rl_p
)
1012 struct range_list
*rl
;
1013 char *return_ranges
;
1022 return alloc_sname("");
1024 if (get_implied_value(expr
, &sval
)) {
1025 *rl_p
= alloc_rl(sval
, sval
);
1026 return sval_to_str(sval
);
1029 compare_str
= expr_equal_to_param(expr
, -1);
1030 math_str
= get_value_in_terms_of_parameter_math(expr
);
1032 if (get_implied_rl(expr
, &rl
)) {
1033 rl
= cast_rl(cur_func_return_type(), rl
);
1034 return_ranges
= show_rl(rl
);
1035 } else if (get_imaginary_absolute(expr
, &rl
)){
1036 rl
= cast_rl(cur_func_return_type(), rl
);
1037 return alloc_sname(show_rl(rl
));
1039 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
1040 return_ranges
= show_rl(rl
);
1045 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1046 return alloc_sname(buf
);
1050 snprintf(buf
, sizeof(buf
), "%s[%s]", return_ranges
, math_str
);
1051 return alloc_sname(buf
);
1054 compare_str
= expr_lte_to_param(expr
, -1);
1056 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1057 return alloc_sname(buf
);
1059 return return_ranges
;
1062 static int split_positive_from_negative(struct expression
*expr
)
1064 struct returned_state_callback
*cb
;
1065 struct range_list
*rl
;
1066 const char *return_ranges
;
1067 struct range_list
*ret_rl
;
1070 /* We're going to print the states 3 times */
1071 if (stree_count(__get_cur_stree()) > 10000 / 3)
1074 if (!get_implied_rl(expr
, &rl
) || !rl
)
1076 if (is_whole_rl(rl
) || is_whole_rl_non_zero(rl
))
1078 /* Forget about INT_MAX and larger */
1079 if (rl_max(rl
).value
<= 0)
1081 if (!sval_is_negative(rl_min(rl
)))
1084 if (!assume(compare_expression(expr
, '>', zero_expr())))
1088 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1089 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1090 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1091 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1092 } END_FOR_EACH_PTR(cb
);
1096 if (rl_has_sval(rl
, sval_type_val(rl_type(rl
), 0))) {
1097 undo
= assume(compare_expression(expr
, SPECIAL_EQUAL
, zero_expr()));
1100 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1101 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1102 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1103 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1104 } END_FOR_EACH_PTR(cb
);
1110 undo
= assume(compare_expression(expr
, '<', zero_expr()));
1113 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1114 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1115 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1116 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1117 } END_FOR_EACH_PTR(cb
);
1125 static int call_return_state_hooks_split_null_non_null(struct expression
*expr
)
1127 struct returned_state_callback
*cb
;
1128 struct range_list
*rl
;
1129 struct range_list
*nonnull_rl
;
1131 struct range_list
*null_rl
= NULL
;
1132 char *return_ranges
;
1133 struct smatch_state
*state
;
1135 int final_pass_orig
= final_pass
;
1137 if (!expr
|| expr_equal_to_param(expr
, -1))
1139 if (expr
->type
== EXPR_CALL
)
1141 if (!is_pointer(expr
))
1144 state
= get_state_expr(SMATCH_EXTRA
, expr
);
1145 if (!state
|| !estate_rl(state
))
1147 if (estate_min(state
).value
== 0 && estate_max(state
).value
== 0)
1149 if (!rl_has_sval(estate_rl(state
), sval_type_val(estate_type(state
), 0)))
1152 nr_states
= stree_count(__get_cur_stree());
1153 if (option_info
&& nr_states
>= 1500)
1156 rl
= estate_rl(state
);
1158 __push_fake_cur_stree();
1161 __split_whole_condition(expr
);
1162 final_pass
= final_pass_orig
;
1164 nonnull_rl
= rl_filter(rl
, rl_zero());
1165 return_ranges
= show_rl(nonnull_rl
);
1166 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonnull_rl
));
1169 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1170 cb
->callback(return_id
, return_ranges
, expr
);
1171 } END_FOR_EACH_PTR(cb
);
1173 __push_true_states();
1174 __use_false_states();
1176 return_ranges
= alloc_sname("0");
1177 null_sval
= sval_type_val(rl_type(rl
), 0);
1178 add_range(&null_rl
, null_sval
, null_sval
);
1179 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(null_rl
));
1181 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1182 cb
->callback(return_id
, return_ranges
, expr
);
1183 } END_FOR_EACH_PTR(cb
);
1185 __merge_true_states();
1186 __free_fake_cur_stree();
1191 static int call_return_state_hooks_split_success_fail(struct expression
*expr
)
1193 struct range_list
*rl
;
1194 struct range_list
*nonzero_rl
;
1196 struct range_list
*zero_rl
= NULL
;
1198 struct returned_state_callback
*cb
;
1199 char *return_ranges
;
1200 int final_pass_orig
= final_pass
;
1203 if (option_project
!= PROJ_KERNEL
)
1206 nr_states
= stree_count(__get_cur_stree());
1207 if (nr_states
> 1500)
1210 if (get_value(expr
, &val
))
1212 if (!get_implied_rl(expr
, &rl
))
1214 if (rl_min(rl
).value
< -4095 || rl_min(rl
).value
>= 0)
1216 if (rl_max(rl
).value
!= 0)
1219 __push_fake_cur_stree();
1222 __split_whole_condition(expr
);
1223 final_pass
= final_pass_orig
;
1225 nonzero_rl
= rl_filter(rl
, rl_zero());
1226 return_ranges
= show_rl(nonzero_rl
);
1227 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(nonzero_rl
));
1230 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1231 cb
->callback(return_id
, return_ranges
, expr
);
1232 } END_FOR_EACH_PTR(cb
);
1234 __push_true_states();
1235 __use_false_states();
1237 return_ranges
= alloc_sname("0");
1238 zero_sval
= sval_type_val(rl_type(rl
), 0);
1239 add_range(&zero_rl
, zero_sval
, zero_sval
);
1240 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(zero_rl
));
1242 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1243 cb
->callback(return_id
, return_ranges
, expr
);
1244 } END_FOR_EACH_PTR(cb
);
1246 __merge_true_states();
1247 __free_fake_cur_stree();
1252 static int is_boolean(struct expression
*expr
)
1254 struct range_list
*rl
;
1256 if (!get_implied_rl(expr
, &rl
))
1258 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
1263 static int is_conditional(struct expression
*expr
)
1267 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
1272 static int splitable_function_call(struct expression
*expr
)
1274 struct sm_state
*sm
;
1277 if (!expr
|| expr
->type
!= EXPR_CALL
)
1279 snprintf(buf
, sizeof(buf
), "return %p", expr
);
1280 sm
= get_sm_state(SMATCH_EXTRA
, buf
, NULL
);
1281 return split_possible_helper(sm
, expr
);
1284 static struct sm_state
*find_bool_param(void)
1286 struct stree
*start_states
;
1288 struct sm_state
*sm
, *tmp
;
1291 start_states
= get_start_states();
1293 FOR_EACH_PTR_REVERSE(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
1294 if (!arg
->ident
|| !arg
->ident
->name
)
1296 sm
= get_sm_state_stree(start_states
, SMATCH_EXTRA
, arg
->ident
->name
, arg
);
1299 if (rl_min(estate_rl(sm
->state
)).value
!= 0 ||
1300 rl_max(estate_rl(sm
->state
)).value
!= 1)
1303 } END_FOR_EACH_PTR_REVERSE(arg
);
1309 * Check if it's splitable. If not, then splitting it up is likely not
1310 * useful for the callers.
1312 FOR_EACH_PTR(sm
->possible
, tmp
) {
1315 if (!estate_get_single_value(tmp
->state
, &sval
))
1317 } END_FOR_EACH_PTR(tmp
);
1322 static int split_on_bool_sm(struct sm_state
*sm
, struct expression
*expr
)
1324 struct returned_state_callback
*cb
;
1325 struct range_list
*ret_rl
;
1326 const char *return_ranges
;
1327 struct sm_state
*tmp
;
1329 int nr_possible
, nr_states
;
1330 char *compare_str
= NULL
;
1332 struct state_list
*already_handled
= NULL
;
1334 if (!sm
|| !sm
->merged
)
1337 if (too_many_possible(sm
))
1340 /* bail if it gets too complicated */
1341 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
1342 nr_states
= stree_count(__get_cur_stree());
1343 if (option_info
&& nr_states
* nr_possible
>= 2000)
1346 FOR_EACH_PTR(sm
->possible
, tmp
) {
1349 if (ptr_in_list(tmp
, already_handled
))
1351 add_ptr_list(&already_handled
, tmp
);
1354 __push_fake_cur_stree();
1356 overwrite_states_using_pool(sm
, tmp
);
1358 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1359 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1360 compare_str
= expr_lte_to_param(expr
, -1);
1362 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
1363 return_ranges
= alloc_sname(buf
);
1367 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1368 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1369 } END_FOR_EACH_PTR(cb
);
1371 __free_fake_cur_stree();
1372 } END_FOR_EACH_PTR(tmp
);
1374 free_slist(&already_handled
);
1379 static int split_by_bool_param(struct expression
*expr
)
1381 struct sm_state
*start_sm
, *sm
;
1384 start_sm
= find_bool_param();
1387 sm
= get_sm_state(SMATCH_EXTRA
, start_sm
->name
, start_sm
->sym
);
1388 if (!sm
|| estate_get_single_value(sm
->state
, &sval
))
1390 return split_on_bool_sm(sm
, expr
);
1393 static void call_return_state_hooks(struct expression
*expr
)
1395 struct returned_state_callback
*cb
;
1396 struct range_list
*ret_rl
;
1397 const char *return_ranges
;
1401 if (__path_is_null())
1404 expr
= strip_expr(expr
);
1406 if (is_impossible_path())
1409 if (!get_implied_value(expr
, &sval
) &&
1410 (is_condition(expr
) || is_boolean(expr
))) {
1411 call_return_state_hooks_compare(expr
);
1413 } else if (is_conditional(expr
)) {
1414 call_return_state_hooks_conditional(expr
);
1416 } else if (call_return_state_hooks_split_possible(expr
)) {
1418 } else if (call_return_state_hooks_split_null_non_null(expr
)) {
1420 } else if (call_return_state_hooks_split_success_fail(expr
)) {
1422 } else if (splitable_function_call(expr
)) {
1424 } else if (split_positive_from_negative(expr
)) {
1426 } else if (split_by_bool_param(expr
)) {
1431 return_ranges
= get_return_ranges_str(expr
, &ret_rl
);
1432 set_state(RETURN_ID
, "return_ranges", NULL
, alloc_estate_rl(ret_rl
));
1435 nr_states
= stree_count(__get_cur_stree());
1436 if (nr_states
>= 10000) {
1437 match_return_info(return_id
, (char *)return_ranges
, expr
);
1440 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
1441 cb
->callback(return_id
, (char *)return_ranges
, expr
);
1442 } END_FOR_EACH_PTR(cb
);
1445 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
1447 struct returned_member_callback
*cb
;
1448 struct stree
*stree
;
1449 struct sm_state
*sm
;
1450 struct symbol
*type
;
1452 char member_name
[256];
1455 type
= get_type(expr
);
1456 if (!type
|| type
->type
!= SYM_PTR
)
1458 name
= expr_to_var(expr
);
1462 member_name
[sizeof(member_name
) - 1] = '\0';
1463 strcpy(member_name
, "$");
1466 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1467 stree
= __get_cur_stree();
1468 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1469 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1470 strcpy(member_name
, "*$");
1471 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1474 if (strncmp(sm
->name
, name
, len
) != 0)
1476 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1478 snprintf(member_name
, sizeof(member_name
), "$%s", sm
->name
+ len
);
1479 cb
->callback(return_id
, return_ranges
, expr
, member_name
, sm
->state
);
1480 } END_FOR_EACH_SM(sm
);
1481 } END_FOR_EACH_PTR(cb
);
1486 static void reset_memdb(struct symbol
*sym
)
1488 mem_sql(NULL
, NULL
, "delete from caller_info;");
1489 mem_sql(NULL
, NULL
, "delete from return_states;");
1490 mem_sql(NULL
, NULL
, "delete from call_implies;");
1493 static void match_end_func_info(struct symbol
*sym
)
1495 if (__path_is_null())
1497 call_return_state_hooks(NULL
);
1500 static void match_after_func(struct symbol
*sym
)
1506 static void init_memdb(void)
1510 const char *schema_files
[] = {
1512 "db/caller_info.schema",
1513 "db/return_states.schema",
1514 "db/function_type_size.schema",
1515 "db/type_size.schema",
1516 "db/call_implies.schema",
1517 "db/function_ptr.schema",
1518 "db/local_values.schema",
1519 "db/function_type_value.schema",
1520 "db/type_value.schema",
1521 "db/function_type_info.schema",
1522 "db/data_info.schema",
1524 static char buf
[4096];
1529 rc
= sqlite3_open(":memory:", &mem_db
);
1530 if (rc
!= SQLITE_OK
) {
1531 printf("Error starting In-Memory database.");
1535 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1536 fd
= open_data_file(schema_files
[i
]);
1541 ret
= read(fd
, buf
, sizeof(buf
));
1542 if (ret
== sizeof(buf
)) {
1543 printf("Schema file too large: %s (limit %zd bytes)",
1544 schema_files
[i
], sizeof(buf
));
1547 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
1548 if (rc
!= SQLITE_OK
) {
1549 fprintf(stderr
, "SQL error #2: %s\n", err
);
1550 fprintf(stderr
, "%s\n", buf
);
1555 void open_smatch_db(void)
1564 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1565 if (rc
!= SQLITE_OK
) {
1572 static void register_common_funcs(void)
1574 struct token
*token
;
1578 if (option_project
== PROJ_NONE
)
1579 strcpy(filename
, "common_functions");
1581 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1583 token
= get_tokens_file(filename
);
1586 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1588 token
= token
->next
;
1589 while (token_type(token
) != TOKEN_STREAMEND
) {
1590 if (token_type(token
) != TOKEN_IDENT
)
1592 func
= alloc_string(show_ident(token
->ident
));
1593 add_ptr_list(&common_funcs
, func
);
1594 token
= token
->next
;
1596 clear_token_alloc();
1600 void register_definition_db_callbacks(int id
)
1602 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1603 add_hook(&global_variable
, BASE_HOOK
);
1604 add_hook(&global_variable
, DECLARATION_HOOK
);
1605 add_split_return_callback(match_return_info
);
1606 add_split_return_callback(print_returned_struct_members
);
1607 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1608 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1609 add_hook(&match_after_func
, AFTER_FUNC_HOOK
);
1611 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1612 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1614 register_common_funcs();
1617 void register_db_call_marker(int id
)
1619 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1622 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
1624 struct expression
*arg
;
1626 char member_name
[256];
1631 const char *star
= "";
1633 if (expr
->type
!= EXPR_ASSIGNMENT
)
1635 name
= expr_to_var_sym(expr
->left
, sym
);
1638 if (key
[0] == '*') {
1642 if (strncmp(key
, "$", 1) != 0)
1644 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 1);
1646 return alloc_string(member_name
);
1649 while (expr
->type
== EXPR_ASSIGNMENT
)
1650 expr
= strip_expr(expr
->right
);
1651 if (expr
->type
!= EXPR_CALL
)
1654 arg
= get_argument_from_call_expr(expr
->args
, param
);
1658 return get_variable_from_key(arg
, key
, sym
);
1661 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
1669 arg
= strip_expr(arg
);
1671 if (strcmp(key
, "$") == 0)
1672 return expr_to_var_sym(arg
, sym
);
1674 if (strcmp(key
, "*$") == 0) {
1675 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1676 arg
= strip_expr(arg
->unop
);
1677 return expr_to_var_sym(arg
, sym
);
1679 tmp
= expr_to_var_sym(arg
, sym
);
1682 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1684 return alloc_string(buf
);
1688 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1689 arg
= strip_expr(arg
->unop
);
1690 tmp
= expr_to_var_sym(arg
, sym
);
1693 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 3);
1694 return alloc_string(buf
);
1697 tmp
= expr_to_var_sym(arg
, sym
);
1700 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 1);
1702 return alloc_string(buf
);
1705 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
1709 if (strcmp("$", key
) == 0)
1710 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
1711 return get_variable_from_key(arg
, key
, sym
);
1714 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
1718 static char buf
[256];
1720 if (!sym
|| !sym
->ident
)
1723 param_name
= sym
->ident
->name
;
1724 name_len
= strlen(param_name
);
1726 if (strcmp(name
, param_name
) == 0) {
1728 } else if (name
[name_len
] == '-' && /* check for '-' from "->" */
1729 strncmp(name
, param_name
, name_len
) == 0) {
1730 snprintf(buf
, sizeof(buf
), "$%s", name
+ name_len
);
1732 } else if (name
[0] == '*' && strcmp(name
+ 1, param_name
) == 0) {
1738 const char *get_param_name(struct sm_state
*sm
)
1740 return get_param_name_var_sym(sm
->name
, sm
->sym
);
1743 char *get_data_info_name(struct expression
*expr
)
1750 expr
= strip_expr(expr
);
1751 name
= get_member_name(expr
);
1754 name
= expr_to_var_sym(expr
, &sym
);
1757 if (!(sym
->ctype
.modifiers
& MOD_TOPLEVEL
))
1759 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1760 snprintf(buf
, sizeof(buf
), "static %s", name
);
1762 snprintf(buf
, sizeof(buf
), "global %s", name
);
1763 ret
= alloc_sname(buf
);