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("in-mem: %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_base_file(), get_function(), (unsigned long)__inline_fn
,
118 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
121 static struct string_list
*common_funcs
;
122 static int is_common_function(const char *fn
)
126 if (strncmp(fn
, "__builtin_", 10) == 0)
129 FOR_EACH_PTR(common_funcs
, tmp
) {
130 if (strcmp(tmp
, fn
) == 0)
132 } END_FOR_EACH_PTR(tmp
);
137 void sql_insert_caller_info(struct expression
*call
, int type
,
138 int param
, const char *key
, const char *value
)
142 if (!option_info
&& !__inline_call
)
145 fn
= get_fnptr_name(call
->fn
);
151 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
152 get_base_file(), get_function(), fn
, (unsigned long)call
,
153 is_static(call
->fn
), type
, param
, key
, value
);
159 if (is_common_function(fn
))
162 sm_msg("SQL_caller_info: insert into caller_info values ("
163 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
164 get_base_file(), get_function(), fn
, is_static(call
->fn
),
165 type
, param
, key
, value
);
170 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
172 sql_insert(function_ptr
, "'%s', '%s', '%s'", get_base_file(), fn
,
176 void sql_insert_return_values(const char *return_values
)
178 sql_insert(return_values
, "'%s', '%s', %lu, %d, '%s'", get_base_file(),
179 get_function(), (unsigned long)__inline_fn
, fn_static(),
183 void sql_insert_call_implies(int type
, int param
, int value
)
185 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
186 get_function(), (unsigned long)__inline_fn
, fn_static(),
190 void sql_insert_type_size(const char *member
, int size
)
192 sql_insert(type_size
, "'%s', '%s', %d", get_base_file(), member
, size
);
195 void sql_insert_local_values(const char *name
, const char *value
)
197 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
200 static char *get_static_filter(struct symbol
*sym
)
202 static char sql_filter
[1024];
204 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
205 snprintf(sql_filter
, sizeof(sql_filter
),
206 "file = '%s' and function = '%s' and static = '1'",
207 get_base_file(), sym
->ident
->name
);
209 snprintf(sql_filter
, sizeof(sql_filter
),
210 "function = '%s' and static = '0'", sym
->ident
->name
);
216 void sql_select_return_states(const char *cols
, struct expression
*call
,
217 int (*callback
)(void*, int, char**, char**))
219 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
222 if (inlinable(call
->fn
)) {
224 "select %s from return_states where call_id = '%lu';",
225 cols
, (unsigned long)call
);
229 run_sql(callback
, "select %s from return_states where %s;",
230 cols
, get_static_filter(call
->fn
->symbol
));
233 void sql_select_call_implies(const char *cols
, struct expression
*call
,
234 int (*callback
)(void*, int, char**, char**))
236 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
239 if (inlinable(call
->fn
)) {
241 "select %s from call_implies where call_id = '%lu';",
242 cols
, (unsigned long)call
);
246 run_sql(callback
, "select %s from call_implies where %s;",
247 cols
, get_static_filter(call
->fn
->symbol
));
250 void sql_select_return_values(const char *cols
, struct expression
*call
,
251 int (*callback
)(void*, int, char**, char**))
253 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
256 if (inlinable(call
->fn
)) {
258 "select %s from return_values where call_id = '%lu';",
259 cols
, (unsigned long)call
);
263 run_sql(callback
, "select %s from return_values where %s;",
264 cols
, get_static_filter(call
->fn
->symbol
));
267 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
268 int (*callback
)(void*, int, char**, char**))
271 mem_sql(callback
, "select %s from caller_info where call_id = %lu;",
272 cols
, (unsigned long)__inline_fn
);
277 "select %s from caller_info where %s order by call_id;",
278 cols
, get_static_filter(sym
));
281 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
283 struct def_callback
*def_callback
= __alloc_def_callback(0);
285 def_callback
->hook_type
= type
;
286 def_callback
->callback
= callback
;
287 add_ptr_list(&callbacks
, def_callback
);
291 * These call backs are used when the --info option is turned on to print struct
292 * member information. For example foo->bar could have a state in
293 * smatch_extra.c and also check_user.c.
295 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
297 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
299 member_callback
->owner
= owner
;
300 member_callback
->callback
= callback
;
301 add_ptr_list(&member_callbacks
, member_callback
);
304 void add_returned_state_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
, struct state_list
*slist
))
306 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
308 callback
->callback
= fn
;
309 add_ptr_list(&returned_state_callbacks
, callback
);
312 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
))
314 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
316 member_callback
->owner
= owner
;
317 member_callback
->callback
= callback
;
318 add_ptr_list(&returned_member_callbacks
, member_callback
);
321 void add_db_fn_call_callback(int type
, void (*callback
)(struct expression
*arg
, char *value
))
323 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
326 cb
->callback
= callback
;
327 add_ptr_list(&call_implies_cb_list
, cb
);
330 static struct symbol
*return_type
;
331 static struct range_list
*return_range_list
;
332 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
337 sm_msg("return type %d", type_positive_bits(return_type
));
338 str_to_rl(return_type
, argv
[0], &return_range_list
);
342 struct range_list
*db_return_vals(struct expression
*expr
)
344 return_type
= get_type(expr
);
347 return_range_list
= NULL
;
348 sql_select_return_values("return", expr
, db_return_callback
);
349 return return_range_list
;
352 static void match_call_marker(struct expression
*expr
)
355 * we just want to record something in the database so that if we have
356 * two calls like: frob(4); frob(some_unkown); then on the receiving
357 * side we know that sometimes frob is called with unknown parameters.
360 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
363 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct state_list
*slist
,
364 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
370 char printed_name
[256];
373 expr
= strip_expr(expr
);
374 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
375 expr
= strip_expr(expr
->unop
);
379 name
= expr_to_var_sym(expr
, &sym
);
384 FOR_EACH_PTR(slist
, sm
) {
387 if (strcmp(name
, sm
->name
) == 0) {
389 snprintf(printed_name
, sizeof(printed_name
), "*$$");
390 else /* these are already handled. fixme: handle them here */
392 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
393 snprintf(printed_name
, sizeof(printed_name
), "*$$");
394 } else if (strncmp(name
, sm
->name
, len
) == 0) {
396 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
398 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
402 callback(call
, param
, printed_name
, sm
->state
);
403 } END_FOR_EACH_PTR(sm
);
408 static void match_call_info(struct expression
*call
)
410 struct member_info_callback
*cb
;
411 struct expression
*arg
;
412 struct state_list
*slist
;
416 name
= get_fnptr_name(call
->fn
);
420 FOR_EACH_PTR(member_callbacks
, cb
) {
421 slist
= get_all_states(cb
->owner
);
423 FOR_EACH_PTR(call
->args
, arg
) {
424 print_struct_members(call
, arg
, i
, slist
, cb
->callback
);
426 } END_FOR_EACH_PTR(arg
);
428 } END_FOR_EACH_PTR(cb
);
433 static int get_param(int param
, char **name
, struct symbol
**sym
)
439 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
441 * this is a temporary hack to work around a bug (I think in sparse?)
442 * 2.6.37-rc1:fs/reiserfs/journal.o
443 * If there is a function definition without parameter name found
444 * after a function implementation then it causes a crash.
448 if (arg
->ident
->name
< (char *)100)
450 if (i
== param
&& arg
->ident
->name
) {
451 *name
= arg
->ident
->name
;
456 } END_FOR_EACH_PTR(arg
);
461 static struct state_list
*final_states
;
462 static int prev_func_id
= -1;
463 static int db_callback(void *unused
, int argc
, char **argv
, char **azColName
)
469 struct symbol
*sym
= NULL
;
470 struct def_callback
*def_callback
;
475 func_id
= atoi(argv
[0]);
477 type
= strtol(argv
[1], NULL
, 10);
478 param
= strtol(argv
[2], NULL
, 10);
482 if (prev_func_id
== -1)
483 prev_func_id
= func_id
;
484 if (func_id
!= prev_func_id
) {
485 merge_slist(&final_states
, __pop_fake_cur_slist());
486 __push_fake_cur_slist();
488 prev_func_id
= func_id
;
491 if (type
== INTERNAL
)
493 if (param
>= 0 && !get_param(param
, &name
, &sym
))
496 FOR_EACH_PTR(callbacks
, def_callback
) {
497 if (def_callback
->hook_type
== type
)
498 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
499 } END_FOR_EACH_PTR(def_callback
);
504 static void get_direct_callers(struct symbol
*sym
)
506 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
510 static char *ptr_name
;
512 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
515 free_string(ptr_name
);
520 ptr_name
= alloc_string(argv
[0]);
524 static void get_function_pointer_callers(struct symbol
*sym
)
526 char sql_filter
[1024];
528 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
529 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
530 get_base_file(), sym
->ident
->name
);
532 snprintf(sql_filter
, 1024, "function = '%s';",
538 run_sql(get_ptr_name
,
539 "select distinct ptr from function_ptr where %s",
544 run_sql(db_callback
, "select call_id, type, parameter, key, value from caller_info"
545 " where function = '%s' order by call_id", ptr_name
);
547 free_string(ptr_name
);
550 static void match_data_from_db(struct symbol
*sym
)
554 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
557 __push_fake_cur_slist();
561 get_direct_callers(sym
);
563 get_function_pointer_callers(sym
);
565 merge_slist(&final_states
, __pop_fake_cur_slist());
567 FOR_EACH_PTR(final_states
, sm
) {
569 } END_FOR_EACH_PTR(sm
);
571 free_slist(&final_states
);
574 static struct expression
*call_implies_call_expr
;
575 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
577 struct call_implies_callback
*cb
;
578 struct expression
*arg
= NULL
;
585 type
= atoi(argv
[1]);
586 param
= atoi(argv
[2]);
588 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
589 if (cb
->type
!= type
)
592 arg
= get_argument_from_call_expr(call_implies_call_expr
->args
, param
);
596 cb
->callback(arg
, argv
[3]);
597 } END_FOR_EACH_PTR(cb
);
602 static void match_call_implies(struct expression
*expr
)
604 call_implies_call_expr
= expr
;
605 sql_select_call_implies("function, type, parameter, value", expr
,
606 call_implies_callbacks
);
610 static void print_initializer_list(struct expression_list
*expr_list
,
611 struct symbol
*struct_type
)
613 struct expression
*expr
;
614 struct symbol
*base_type
;
615 char struct_name
[256];
617 FOR_EACH_PTR(expr_list
, expr
) {
618 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
619 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
622 if (expr
->type
!= EXPR_IDENTIFIER
)
624 if (!expr
->expr_ident
)
626 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
628 base_type
= get_type(expr
->ident_expression
);
629 if (!base_type
|| base_type
->type
!= SYM_FN
)
631 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
632 struct_type
->ident
->name
, expr
->expr_ident
->name
);
633 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
635 } END_FOR_EACH_PTR(expr
);
638 static void global_variable(struct symbol
*sym
)
640 struct symbol
*struct_type
;
644 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
646 struct_type
= get_base_type(sym
);
649 if (struct_type
->type
== SYM_ARRAY
) {
650 struct_type
= get_base_type(struct_type
);
654 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
656 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
659 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
661 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
664 static int return_id
;
665 static void match_function_def(struct symbol
*sym
)
670 static void call_return_state_hooks_compare(struct expression
*expr
)
672 struct returned_state_callback
*cb
;
673 struct state_list
*slist
;
675 int final_pass_orig
= final_pass
;
677 __push_fake_cur_slist();
680 __split_whole_condition(expr
);
681 final_pass
= final_pass_orig
;
683 return_ranges
= alloc_sname("1");
686 slist
= __get_cur_slist();
687 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
688 cb
->callback(return_id
, return_ranges
, expr
, slist
);
689 } END_FOR_EACH_PTR(cb
);
691 __push_true_states();
692 __use_false_states();
694 return_ranges
= alloc_sname("0");;
696 slist
= __get_cur_slist();
697 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
698 cb
->callback(return_id
, return_ranges
, expr
, slist
);
699 } END_FOR_EACH_PTR(cb
);
701 __merge_true_states();
702 __pop_fake_cur_slist();
705 static int call_return_state_hooks_split_possible(struct expression
*expr
)
707 struct returned_state_callback
*cb
;
708 struct state_list
*slist
;
709 struct range_list
*rl
;
712 struct sm_state
*tmp
;
714 int nr_possible
, nr_states
;
716 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
717 if (!sm
|| !sm
->merged
)
720 /* bail if it gets too complicated */
721 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
722 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
723 if (nr_possible
>= 100)
725 if (nr_states
* nr_possible
>= 1000)
728 FOR_EACH_PTR(sm
->possible
, tmp
) {
733 __push_fake_cur_slist();
735 overwrite_states_using_pool(tmp
);
737 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
738 return_ranges
= show_rl(rl
);
741 slist
= __get_cur_slist();
742 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
743 cb
->callback(return_id
, return_ranges
, expr
, slist
);
744 } END_FOR_EACH_PTR(cb
);
746 __pop_fake_cur_slist();
747 } END_FOR_EACH_PTR(tmp
);
752 static void call_return_state_hooks(struct expression
*expr
)
754 struct returned_state_callback
*cb
;
755 struct state_list
*slist
;
756 struct range_list
*rl
;
760 expr
= strip_expr(expr
);
763 return_ranges
= alloc_sname("");
764 } else if (is_condition(expr
)) {
765 call_return_state_hooks_compare(expr
);
767 } else if (call_return_state_hooks_split_possible(expr
)) {
769 } else if (get_implied_rl(expr
, &rl
)) {
770 rl
= cast_rl(cur_func_return_type(), rl
);
771 return_ranges
= show_rl(rl
);
773 rl
= alloc_whole_rl(cur_func_return_type());
774 return_ranges
= show_rl(rl
);
778 slist
= __get_cur_slist();
779 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
780 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
781 if (nr_states
< 10000)
782 cb
->callback(return_id
, return_ranges
, expr
, slist
);
784 cb
->callback(return_id
, return_ranges
, expr
, NULL
);
785 } END_FOR_EACH_PTR(cb
);
788 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
790 struct returned_member_callback
*cb
;
791 struct state_list
*my_slist
;
795 char member_name
[256];
798 type
= get_type(expr
);
799 if (!type
|| type
->type
!= SYM_PTR
)
801 type
= get_real_base_type(type
);
802 if (!type
|| type
->type
!= SYM_STRUCT
)
804 name
= expr_to_var(expr
);
808 member_name
[sizeof(member_name
) - 1] = '\0';
809 strcpy(member_name
, "$$");
812 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
813 my_slist
= get_all_states_slist(cb
->owner
, slist
);
814 FOR_EACH_PTR(my_slist
, sm
) {
815 if (strncmp(sm
->name
, name
, len
) != 0)
817 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
819 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
820 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
821 } END_FOR_EACH_PTR(sm
);
822 free_slist(&my_slist
);
823 } END_FOR_EACH_PTR(cb
);
828 static void reset_memdb(void)
830 mem_sql(NULL
, "delete from caller_info;");
831 mem_sql(NULL
, "delete from return_states;");
832 mem_sql(NULL
, "delete from call_implies;");
833 mem_sql(NULL
, "delete from return_values;");
836 static void match_end_func_info(struct symbol
*sym
)
838 if (__path_is_null())
840 call_return_state_hooks(NULL
);
845 static void init_memdb(void)
849 const char *schema_files
[] = {
851 "db/caller_info.schema",
852 "db/return_states.schema",
853 "db/type_size.schema",
854 "db/call_implies.schema",
855 "db/function_ptr.schema",
856 "db/return_values.schema",
857 "db/local_values.schema",
859 static char buf
[4096];
864 rc
= sqlite3_open(":memory:", &mem_db
);
865 if (rc
!= SQLITE_OK
) {
866 printf("Error starting In-Memory database.");
870 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
871 fd
= open_data_file(schema_files
[i
]);
874 ret
= read(fd
, buf
, sizeof(buf
));
875 if (ret
== sizeof(buf
)) {
876 printf("Schema file too large: %s (limit %zd bytes)",
877 schema_files
[i
], sizeof(buf
));
880 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
881 if (rc
!= SQLITE_OK
) {
882 fprintf(stderr
, "SQL error #2: %s\n", err
);
883 fprintf(stderr
, "%s\n", buf
);
888 void open_smatch_db(void)
897 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
898 if (rc
!= SQLITE_OK
) {
905 static void register_common_funcs(void)
911 if (option_project
== PROJ_NONE
)
912 strcpy(filename
, "common_functions");
914 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
916 token
= get_tokens_file(filename
);
919 if (token_type(token
) != TOKEN_STREAMBEGIN
)
922 while (token_type(token
) != TOKEN_STREAMEND
) {
923 if (token_type(token
) != TOKEN_IDENT
)
925 func
= alloc_string(show_ident(token
->ident
));
926 add_ptr_list(&common_funcs
, func
);
933 void register_definition_db_callbacks(int id
)
935 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
937 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
938 add_hook(&global_variable
, BASE_HOOK
);
939 add_hook(&global_variable
, DECLARATION_HOOK
);
940 add_returned_state_callback(match_return_info
);
941 add_returned_state_callback(print_returned_struct_members
);
942 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
943 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
945 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
946 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
948 register_common_funcs();
951 void register_db_call_marker(int id
)
953 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
956 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
961 if (strcmp(key
, "$$") == 0)
962 return expr_to_var_sym(arg
, sym
);
964 if (strcmp(key
, "*$$") == 0) {
965 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
966 arg
= strip_expr(arg
->unop
);
967 return expr_to_var_sym(arg
, sym
);
969 tmp
= expr_to_var_sym(arg
, sym
);
972 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
974 return alloc_string(buf
);
978 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
979 arg
= strip_expr(arg
->unop
);
980 tmp
= expr_to_var_sym(arg
, sym
);
983 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 4);
984 return alloc_string(buf
);
987 tmp
= expr_to_var_sym(arg
, sym
);
990 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 2);
992 return alloc_string(buf
);