4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
15 #include "smatch_slist.h"
16 #include "smatch_extra.h"
19 static sqlite3
*mem_db
;
21 #define sql_insert(table, values...) \
25 char *err, *p = buf; \
28 p += snprintf(p, buf + sizeof(buf) - p, \
29 "insert into %s values (", #table); \
30 p += snprintf(p, buf + sizeof(buf) - p, values); \
31 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
32 sm_debug("mem-db: %s\n", buf); \
33 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
34 if (rc != SQLITE_OK) { \
35 fprintf(stderr, "SQL error #2: %s\n", err); \
36 fprintf(stderr, "SQL: '%s'\n", buf); \
42 sm_printf("SQL: insert into " #table " values (" values); \
49 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
51 ALLOCATOR(def_callback
, "definition db hook callbacks");
52 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
53 static struct callback_list
*callbacks
;
55 struct member_info_callback
{
57 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
);
59 ALLOCATOR(member_info_callback
, "caller_info callbacks");
60 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
61 static struct member_info_cb_list
*member_callbacks
;
63 struct returned_state_callback
{
64 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
, struct state_list
*slist
);
66 ALLOCATOR(returned_state_callback
, "returned state callbacks");
67 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
68 static struct returned_state_cb_list
*returned_state_callbacks
;
70 struct returned_member_callback
{
72 void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
);
74 ALLOCATOR(returned_member_callback
, "returned member callbacks");
75 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
76 static struct returned_member_cb_list
*returned_member_callbacks
;
78 struct call_implies_callback
{
80 void (*callback
)(struct expression
*arg
, char *value
);
82 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
83 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
84 static struct call_implies_cb_list
*call_implies_cb_list
;
86 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
91 if (option_no_db
|| !db
)
94 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
95 if (rc
!= SQLITE_OK
) {
96 fprintf(stderr
, "SQL error #2: %s\n", err
);
97 fprintf(stderr
, "SQL: '%s'\n", sql
);
101 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
106 rc
= sqlite3_exec(mem_db
, sql
, callback
, 0, &err
);
107 if (rc
!= SQLITE_OK
) {
108 fprintf(stderr
, "SQL error #2: %s\n", err
);
109 fprintf(stderr
, "SQL: '%s'\n", sql
);
113 void sql_insert_return_states(int return_id
, const char *return_ranges
,
114 int type
, int param
, const char *key
, const char *value
)
116 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
117 get_filename(), get_function(), (unsigned long)__inline_fn
,
118 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
121 void sql_insert_caller_info(struct expression
*call
, int type
,
122 int param
, const char *key
, const char *value
)
126 if (!option_info
&& !__inline_call
)
129 fn
= get_fnptr_name(call
->fn
);
135 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
136 get_filename(), get_function(), fn
, (unsigned long)call
,
137 is_static(call
->fn
), type
, param
, key
, value
);
143 if (strncmp(fn
, "__builtin_", 10) == 0)
146 sm_msg("SQL_caller_info: insert into caller_info values ("
147 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
148 get_filename(), get_function(), fn
, is_static(call
->fn
),
149 type
, param
, key
, value
);
154 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
156 sql_insert(function_ptr
, "'%s', '%s', '%s'", get_filename(), fn
,
160 void sql_insert_return_values(const char *return_values
)
162 sql_insert(return_values
, "'%s', '%s', %lu, %d, '%s'", get_filename(),
163 get_function(), (unsigned long)__inline_fn
, fn_static(),
167 void sql_insert_call_implies(int type
, int param
, int value
)
169 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, %d", get_filename(),
170 get_function(), (unsigned long)__inline_fn
, fn_static(),
174 void sql_insert_type_size(const char *member
, int size
)
176 sql_insert(type_size
, "'%s', '%s', %d", get_filename(), member
, size
);
179 static char *get_static_filter(struct symbol
*sym
)
181 static char sql_filter
[1024];
183 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
184 snprintf(sql_filter
, sizeof(sql_filter
),
185 "file = '%s' and function = '%s' and static = '1'",
186 get_filename(), sym
->ident
->name
);
188 snprintf(sql_filter
, sizeof(sql_filter
),
189 "function = '%s' and static = '0'", sym
->ident
->name
);
195 void sql_select_return_states(const char *cols
, struct expression
*call
,
196 int (*callback
)(void*, int, char**, char**))
198 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
201 if (inlinable(call
->fn
)) {
203 "select %s from return_states where call_id = '%lu';",
204 cols
, (unsigned long)call
);
208 run_sql(callback
, "select %s from return_states where %s;",
209 cols
, get_static_filter(call
->fn
->symbol
));
212 void sql_select_call_implies(const char *cols
, struct expression
*call
,
213 int (*callback
)(void*, int, char**, char**))
215 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
218 if (inlinable(call
->fn
)) {
220 "select %s from call_implies where call_id = '%lu';",
221 cols
, (unsigned long)call
);
225 run_sql(callback
, "select %s from call_implies where %s;",
226 cols
, get_static_filter(call
->fn
->symbol
));
229 void sql_select_return_values(const char *cols
, struct expression
*call
,
230 int (*callback
)(void*, int, char**, char**))
232 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
235 if (inlinable(call
->fn
)) {
237 "select %s from return_values where call_id = '%lu';",
238 cols
, (unsigned long)call
);
242 run_sql(callback
, "select %s from return_values where %s;",
243 cols
, get_static_filter(call
->fn
->symbol
));
246 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
247 int (*callback
)(void*, int, char**, char**))
250 mem_sql(callback
, "select %s from caller_info where call_id = %lu;",
251 cols
, (unsigned long)__inline_fn
);
256 "select %s from caller_info where %s order by call_id;",
257 cols
, get_static_filter(sym
));
260 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
262 struct def_callback
*def_callback
= __alloc_def_callback(0);
264 def_callback
->hook_type
= type
;
265 def_callback
->callback
= callback
;
266 add_ptr_list(&callbacks
, def_callback
);
270 * These call backs are used when the --info option is turned on to print struct
271 * member information. For example foo->bar could have a state in
272 * smatch_extra.c and also check_user.c.
274 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
276 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
278 member_callback
->owner
= owner
;
279 member_callback
->callback
= callback
;
280 add_ptr_list(&member_callbacks
, member_callback
);
283 void add_returned_state_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
, struct state_list
*slist
))
285 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
287 callback
->callback
= fn
;
288 add_ptr_list(&returned_state_callbacks
, callback
);
291 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
))
293 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
295 member_callback
->owner
= owner
;
296 member_callback
->callback
= callback
;
297 add_ptr_list(&returned_member_callbacks
, member_callback
);
300 void add_db_fn_call_callback(int type
, void (*callback
)(struct expression
*arg
, char *value
))
302 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
305 cb
->callback
= callback
;
306 add_ptr_list(&call_implies_cb_list
, cb
);
309 static struct symbol
*return_type
;
310 static struct range_list
*return_range_list
;
311 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
316 sm_msg("return type %d", type_positive_bits(return_type
));
317 str_to_rl(return_type
, argv
[0], &return_range_list
);
321 struct range_list
*db_return_vals(struct expression
*expr
)
323 return_type
= get_type(expr
);
326 return_range_list
= NULL
;
327 sql_select_return_values("return", expr
, db_return_callback
);
328 return return_range_list
;
331 static void match_call_marker(struct expression
*expr
)
334 * we just want to record something in the database so that if we have
335 * two calls like: frob(4); frob(some_unkown); then on the receiving
336 * side we know that sometimes frob is called with unknown parameters.
339 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
342 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct state_list
*slist
,
343 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
349 char printed_name
[256];
352 expr
= strip_expr(expr
);
353 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
354 expr
= strip_expr(expr
->unop
);
358 name
= expr_to_var_sym(expr
, &sym
);
363 FOR_EACH_PTR(slist
, sm
) {
366 if (strcmp(name
, sm
->name
) == 0) {
368 snprintf(printed_name
, sizeof(printed_name
), "*$$");
369 else /* these are already handled. fixme: handle them here */
371 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
372 snprintf(printed_name
, sizeof(printed_name
), "*$$");
373 } else if (strncmp(name
, sm
->name
, len
) == 0) {
375 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
377 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
381 callback(call
, param
, printed_name
, sm
->state
);
382 } END_FOR_EACH_PTR(sm
);
387 static void match_call_info(struct expression
*call
)
389 struct member_info_callback
*cb
;
390 struct expression
*arg
;
391 struct state_list
*slist
;
395 name
= get_fnptr_name(call
->fn
);
399 FOR_EACH_PTR(member_callbacks
, cb
) {
400 slist
= get_all_states(cb
->owner
);
402 FOR_EACH_PTR(call
->args
, arg
) {
403 print_struct_members(call
, arg
, i
, slist
, cb
->callback
);
405 } END_FOR_EACH_PTR(arg
);
407 } END_FOR_EACH_PTR(cb
);
412 static int get_param(int param
, char **name
, struct symbol
**sym
)
418 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
420 * this is a temporary hack to work around a bug (I think in sparse?)
421 * 2.6.37-rc1:fs/reiserfs/journal.o
422 * If there is a function definition without parameter name found
423 * after a function implementation then it causes a crash.
427 if (arg
->ident
->name
< (char *)100)
429 if (i
== param
&& arg
->ident
->name
) {
430 *name
= arg
->ident
->name
;
435 } END_FOR_EACH_PTR(arg
);
440 static struct state_list
*final_states
;
441 static int prev_func_id
= -1;
442 static int db_callback(void *unused
, int argc
, char **argv
, char **azColName
)
448 struct symbol
*sym
= NULL
;
449 struct def_callback
*def_callback
;
454 func_id
= atoi(argv
[0]);
456 type
= strtol(argv
[1], NULL
, 10);
457 param
= strtol(argv
[2], NULL
, 10);
461 if (prev_func_id
== -1)
462 prev_func_id
= func_id
;
463 if (func_id
!= prev_func_id
) {
464 merge_slist(&final_states
, __pop_fake_cur_slist());
465 __push_fake_cur_slist();
467 prev_func_id
= func_id
;
470 if (type
== INTERNAL
)
472 if (param
>= 0 && !get_param(param
, &name
, &sym
))
475 FOR_EACH_PTR(callbacks
, def_callback
) {
476 if (def_callback
->hook_type
== type
)
477 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
478 } END_FOR_EACH_PTR(def_callback
);
483 static void get_direct_callers(struct symbol
*sym
)
485 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
489 static char *ptr_name
;
491 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
494 free_string(ptr_name
);
499 ptr_name
= alloc_string(argv
[0]);
503 static void get_function_pointer_callers(struct symbol
*sym
)
505 char sql_filter
[1024];
507 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
508 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
509 get_filename(), sym
->ident
->name
);
511 snprintf(sql_filter
, 1024, "function = '%s';",
517 run_sql(get_ptr_name
,
518 "select distinct ptr from function_ptr where %s",
523 run_sql(db_callback
, "select call_id, type, parameter, key, value from caller_info"
524 " where function = '%s' order by call_id", ptr_name
);
526 free_string(ptr_name
);
529 static void match_data_from_db(struct symbol
*sym
)
533 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
536 __push_fake_cur_slist();
540 get_direct_callers(sym
);
542 get_function_pointer_callers(sym
);
544 merge_slist(&final_states
, __pop_fake_cur_slist());
546 FOR_EACH_PTR(final_states
, sm
) {
548 } END_FOR_EACH_PTR(sm
);
550 free_slist(&final_states
);
553 static void match_function_assign(struct expression
*expr
)
555 struct expression
*right
= expr
->right
;
560 if (right
->type
== EXPR_PREOP
&& right
->op
== '&')
561 right
= strip_expr(right
->unop
);
562 if (right
->type
!= EXPR_SYMBOL
)
564 sym
= get_type(right
);
565 if (!sym
|| sym
->type
!= SYM_FN
)
568 fn_name
= expr_to_var(right
);
569 ptr_name
= get_fnptr_name(expr
->left
);
570 if (!fn_name
|| !ptr_name
)
573 sql_insert_function_ptr(fn_name
, ptr_name
);
576 free_string(fn_name
);
577 free_string(ptr_name
);
580 static struct expression
*call_implies_call_expr
;
581 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
583 struct call_implies_callback
*cb
;
584 struct expression
*arg
= NULL
;
591 type
= atoi(argv
[1]);
592 param
= atoi(argv
[2]);
594 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
595 if (cb
->type
!= type
)
598 arg
= get_argument_from_call_expr(call_implies_call_expr
->args
, param
);
602 cb
->callback(arg
, argv
[3]);
603 } END_FOR_EACH_PTR(cb
);
608 static void match_call_implies(struct expression
*expr
)
610 call_implies_call_expr
= expr
;
611 sql_select_call_implies("function, type, parameter, value", expr
,
612 call_implies_callbacks
);
616 static void print_initializer_list(struct expression_list
*expr_list
,
617 struct symbol
*struct_type
)
619 struct expression
*expr
;
620 struct symbol
*base_type
;
621 char struct_name
[256];
623 FOR_EACH_PTR(expr_list
, expr
) {
624 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
625 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
628 if (expr
->type
!= EXPR_IDENTIFIER
)
630 if (!expr
->expr_ident
)
632 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
634 base_type
= get_type(expr
->ident_expression
);
635 if (!base_type
|| base_type
->type
!= SYM_FN
)
637 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
638 struct_type
->ident
->name
, expr
->expr_ident
->name
);
639 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
641 } END_FOR_EACH_PTR(expr
);
644 static void global_variable(struct symbol
*sym
)
646 struct symbol
*struct_type
;
650 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
652 struct_type
= get_base_type(sym
);
655 if (struct_type
->type
== SYM_ARRAY
) {
656 struct_type
= get_base_type(struct_type
);
660 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
662 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
665 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
667 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
670 static int return_id
;
671 static void match_function_def(struct symbol
*sym
)
676 static void call_return_state_hooks_compare(struct expression
*expr
)
678 struct returned_state_callback
*cb
;
679 struct state_list
*slist
;
681 int final_pass_orig
= final_pass
;
683 __push_fake_cur_slist();
686 __split_whole_condition(expr
);
687 final_pass
= final_pass_orig
;
689 return_ranges
= alloc_sname("1");
692 slist
= __get_cur_slist();
693 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
694 cb
->callback(return_id
, return_ranges
, expr
, slist
);
695 } END_FOR_EACH_PTR(cb
);
697 __push_true_states();
698 __use_false_states();
700 return_ranges
= alloc_sname("0");;
702 slist
= __get_cur_slist();
703 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
704 cb
->callback(return_id
, return_ranges
, expr
, slist
);
705 } END_FOR_EACH_PTR(cb
);
707 __merge_true_states();
708 __pop_fake_cur_slist();
711 static int call_return_state_hooks_split_possible(struct expression
*expr
)
713 struct returned_state_callback
*cb
;
714 struct state_list
*slist
;
715 struct range_list
*rl
;
718 struct sm_state
*tmp
;
720 int nr_possible
, nr_states
;
722 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
723 if (!sm
|| !sm
->merged
)
726 /* bail if it gets too complicated */
727 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
728 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
729 if (nr_possible
>= 100)
731 if (nr_states
* nr_possible
>= 1000)
734 FOR_EACH_PTR(sm
->possible
, tmp
) {
739 __push_fake_cur_slist();
741 overwrite_states_using_pool(tmp
);
743 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
744 return_ranges
= show_rl(rl
);
747 slist
= __get_cur_slist();
748 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
749 cb
->callback(return_id
, return_ranges
, expr
, slist
);
750 } END_FOR_EACH_PTR(cb
);
752 __pop_fake_cur_slist();
753 } END_FOR_EACH_PTR(tmp
);
758 static void call_return_state_hooks(struct expression
*expr
)
760 struct returned_state_callback
*cb
;
761 struct state_list
*slist
;
762 struct range_list
*rl
;
766 expr
= strip_expr(expr
);
769 return_ranges
= alloc_sname("");
770 } else if (is_condition(expr
)) {
771 call_return_state_hooks_compare(expr
);
773 } else if (call_return_state_hooks_split_possible(expr
)) {
775 } else if (get_implied_rl(expr
, &rl
)) {
776 rl
= cast_rl(cur_func_return_type(), rl
);
777 return_ranges
= show_rl(rl
);
779 rl
= alloc_whole_rl(cur_func_return_type());
780 return_ranges
= show_rl(rl
);
784 slist
= __get_cur_slist();
785 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
786 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
787 if (nr_states
< 10000)
788 cb
->callback(return_id
, return_ranges
, expr
, slist
);
790 cb
->callback(return_id
, return_ranges
, expr
, NULL
);
791 } END_FOR_EACH_PTR(cb
);
794 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
796 struct returned_member_callback
*cb
;
797 struct state_list
*my_slist
;
801 char member_name
[256];
804 type
= get_type(expr
);
805 if (!type
|| type
->type
!= SYM_PTR
)
807 type
= get_real_base_type(type
);
808 if (!type
|| type
->type
!= SYM_STRUCT
)
810 name
= expr_to_var(expr
);
814 member_name
[sizeof(member_name
) - 1] = '\0';
815 strcpy(member_name
, "$$");
818 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
819 my_slist
= get_all_states_slist(cb
->owner
, slist
);
820 FOR_EACH_PTR(my_slist
, sm
) {
821 if (strncmp(sm
->name
, name
, len
) != 0)
823 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
825 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
826 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
827 } END_FOR_EACH_PTR(sm
);
828 free_slist(&my_slist
);
829 } END_FOR_EACH_PTR(cb
);
834 static void reset_memdb(void)
836 mem_sql(NULL
, "delete from caller_info;");
837 mem_sql(NULL
, "delete from return_states;");
838 mem_sql(NULL
, "delete from call_implies;");
839 mem_sql(NULL
, "delete from return_values;");
842 static void match_end_func_info(struct symbol
*sym
)
844 if (__path_is_null())
846 call_return_state_hooks(NULL
);
851 static void init_memdb(void)
855 const char *schema_files
[] = {
857 "db/caller_info.schema",
858 "db/return_states.schema",
859 "db/type_size.schema",
860 "db/call_implies.schema",
861 "db/function_ptr.schema",
862 "db/return_values.schema",
864 static char buf
[4096];
869 rc
= sqlite3_open(":memory:", &mem_db
);
870 if (rc
!= SQLITE_OK
) {
871 printf("Error starting In-Memory database.");
875 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
876 fd
= open_data_file(schema_files
[i
]);
879 ret
= read(fd
, buf
, sizeof(buf
));
880 if (ret
== sizeof(buf
)) {
881 printf("Schema file too large: %s (limit %zd bytes)",
882 schema_files
[i
], sizeof(buf
));
885 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
886 if (rc
!= SQLITE_OK
) {
887 fprintf(stderr
, "SQL error #2: %s\n", err
);
888 fprintf(stderr
, "%s\n", buf
);
893 void open_smatch_db(void)
902 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
903 if (rc
!= SQLITE_OK
) {
910 void register_definition_db_callbacks(int id
)
912 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
914 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
915 add_hook(&match_function_assign
, ASSIGNMENT_HOOK
);
916 add_hook(&global_variable
, BASE_HOOK
);
917 add_hook(&global_variable
, DECLARATION_HOOK
);
918 add_returned_state_callback(match_return_info
);
919 add_returned_state_callback(print_returned_struct_members
);
920 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
921 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
923 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
924 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
927 void register_db_call_marker(int id
)
929 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
932 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
937 if (strcmp(key
, "$$") == 0)
938 return expr_to_var_sym(arg
, sym
);
940 if (strcmp(key
, "*$$") == 0) {
941 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
942 arg
= strip_expr(arg
->unop
);
943 return expr_to_var_sym(arg
, sym
);
945 tmp
= expr_to_var_sym(arg
, sym
);
948 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
950 return alloc_string(buf
);
954 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
955 arg
= strip_expr(arg
->unop
);
956 tmp
= expr_to_var_sym(arg
, sym
);
959 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 4);
960 return alloc_string(buf
);
963 tmp
= expr_to_var_sym(arg
, sym
);
966 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 2);
968 return alloc_string(buf
);