ranges: fix "trouble parsing empty" messages
[smatch.git] / smatch_db.c
blobf2533c3b60e2c6fcb7409d96049db27b2582e693
1 /*
2 * smatch/smatch_db.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <string.h>
11 #include <errno.h>
12 #include <sqlite3.h>
13 #include <unistd.h>
14 #include "smatch.h"
15 #include "smatch_slist.h"
16 #include "smatch_extra.h"
18 static sqlite3 *db;
19 static sqlite3 *mem_db;
21 #define sql_insert(table, values...) \
22 do { \
23 if (__inline_fn) { \
24 char buf[1024]; \
25 char *err, *p = buf; \
26 int rc; \
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 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
33 if (rc != SQLITE_OK) { \
34 fprintf(stderr, "SQL error #2: %s\n", err); \
35 fprintf(stderr, "SQL: '%s'\n", buf); \
36 } \
37 return; \
38 } \
39 if (option_info) { \
40 sm_prefix(); \
41 sm_printf("SQL: insert into " #table " values (" values); \
42 sm_printf(");\n"); \
43 } \
44 } while (0)
46 struct def_callback {
47 int hook_type;
48 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
50 ALLOCATOR(def_callback, "definition db hook callbacks");
51 DECLARE_PTR_LIST(callback_list, struct def_callback);
52 static struct callback_list *callbacks;
54 struct member_info_callback {
55 int owner;
56 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state);
58 ALLOCATOR(member_info_callback, "caller_info callbacks");
59 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
60 static struct member_info_cb_list *member_callbacks;
62 struct returned_state_callback {
63 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr, struct state_list *slist);
65 ALLOCATOR(returned_state_callback, "returned state callbacks");
66 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
67 static struct returned_state_cb_list *returned_state_callbacks;
69 struct returned_member_callback {
70 int owner;
71 void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state);
73 ALLOCATOR(returned_member_callback, "returned member callbacks");
74 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
75 static struct returned_member_cb_list *returned_member_callbacks;
77 struct call_implies_callback {
78 int type;
79 void (*callback)(struct expression *arg, char *value);
81 ALLOCATOR(call_implies_callback, "call_implies callbacks");
82 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
83 static struct call_implies_cb_list *call_implies_cb_list;
85 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
87 char *err = NULL;
88 int rc;
90 if (option_no_db || !db)
91 return;
93 rc = sqlite3_exec(db, sql, callback, 0, &err);
94 if (rc != SQLITE_OK) {
95 fprintf(stderr, "SQL error #2: %s\n", err);
96 fprintf(stderr, "SQL: '%s'\n", sql);
100 void sql_insert_return_states(int return_id, const char *return_ranges,
101 int type, int param, const char *key, const char *value)
103 sql_insert(return_states, "'%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s'",
104 get_filename(), get_function(), return_id, return_ranges,
105 fn_static(), type, param, key, value);
108 void sql_insert_caller_info(struct expression *call, int type,
109 int param, const char *key, const char *value)
111 char *fn;
113 if (!option_info && !__inline_call)
114 return;
116 fn = get_fnptr_name(call->fn);
117 if (!fn)
118 return;
120 if (__inline_call) {
121 char buf[1024];
122 char *err;
123 int rc;
125 snprintf(buf, sizeof(buf), "insert into caller_info values ("
126 "'%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
127 get_filename(), get_function(), fn,
128 (unsigned long)call, is_static(call->fn), type, param,
129 key, value);
130 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
131 if (rc != SQLITE_OK) {
132 fprintf(stderr, "SQL error #2: %s\n", err);
133 fprintf(stderr, "SQL: '%s'\n", buf);
135 return;
138 sm_msg("SQL_caller_info: insert into caller_info values ("
139 "'%s', '%s', '%s', %%FUNC_ID%%, %d, %d, %d, '%s', '%s');",
140 get_filename(), get_function(), fn, is_static(call->fn),
141 type, param, key, value);
143 free_string(fn);
146 void sql_insert_function_ptr(const char *fn, const char *struct_name)
148 sql_insert(function_ptr, "'%s', '%s', '%s'", get_filename(), fn,
149 struct_name);
152 void sql_insert_return_values(const char *return_values)
154 sql_insert(return_values, "'%s', '%s', %d, '%s'", get_filename(),
155 get_function(), fn_static(), return_values);
158 void sql_insert_call_implies(int type, int param, int value)
160 sql_insert(call_implies, "'%s', '%s', %d, %d, %d, %d", get_filename(),
161 get_function(), fn_static(), type, param, value);
164 void sql_insert_type_size(const char *member, int size)
166 sql_insert(type_size, "'%s', '%s', %d", get_filename(), member, size);
169 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
171 struct def_callback *def_callback = __alloc_def_callback(0);
173 def_callback->hook_type = type;
174 def_callback->callback = callback;
175 add_ptr_list(&callbacks, def_callback);
179 * These call backs are used when the --info option is turned on to print struct
180 * member information. For example foo->bar could have a state in
181 * smatch_extra.c and also check_user.c.
183 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
185 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
187 member_callback->owner = owner;
188 member_callback->callback = callback;
189 add_ptr_list(&member_callbacks, member_callback);
192 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
194 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
196 callback->callback = fn;
197 add_ptr_list(&returned_state_callbacks, callback);
200 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
202 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
204 member_callback->owner = owner;
205 member_callback->callback = callback;
206 add_ptr_list(&returned_member_callbacks, member_callback);
209 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
211 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
213 cb->type = type;
214 cb->callback = callback;
215 add_ptr_list(&call_implies_cb_list, cb);
218 static struct symbol *return_type;
219 static struct range_list *return_range_list;
220 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
222 if (argc != 1)
223 return 0;
224 if (option_debug)
225 sm_msg("return type %d", type_positive_bits(return_type));
226 str_to_rl(return_type, argv[0], &return_range_list);
227 return 0;
230 struct range_list *db_return_vals(struct expression *expr)
232 struct symbol *sym;
233 static char sql_filter[1024];
235 if (expr->type != EXPR_CALL)
236 return NULL;
237 if (expr->fn->type != EXPR_SYMBOL)
238 return NULL;
239 return_type = get_type(expr);
240 if (!return_type)
241 return NULL;
242 sym = expr->fn->symbol;
243 if (!sym)
244 return NULL;
246 if (sym->ctype.modifiers & MOD_STATIC) {
247 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
248 get_filename(), sym->ident->name);
249 } else {
250 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
251 sym->ident->name);
254 return_range_list = NULL;
255 run_sql(db_return_callback, "select return from return_values where %s",
256 sql_filter);
257 return return_range_list;
260 static void match_call_marker(struct expression *expr)
263 * we just want to record something in the database so that if we have
264 * two calls like: frob(4); frob(some_unkown); then on the receiving
265 * side we know that sometimes frob is called with unknown parameters.
268 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
271 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
272 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
274 struct sm_state *sm;
275 char *name;
276 struct symbol *sym;
277 int len;
278 char printed_name[256];
279 int is_address = 0;
281 expr = strip_expr(expr);
282 if (expr->type == EXPR_PREOP && expr->op == '&') {
283 expr = strip_expr(expr->unop);
284 is_address = 1;
287 name = expr_to_var_sym(expr, &sym);
288 if (!name || !sym)
289 goto free;
291 len = strlen(name);
292 FOR_EACH_PTR(slist, sm) {
293 if (sm->sym != sym)
294 continue;
295 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
296 continue;
297 if (is_address)
298 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
299 else
300 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
301 callback(call, param, printed_name, sm->state);
302 } END_FOR_EACH_PTR(sm);
303 free:
304 free_string(name);
307 static void match_call_info(struct expression *call)
309 struct member_info_callback *cb;
310 struct expression *arg;
311 struct state_list *slist;
312 char *name;
313 int i;
315 name = get_fnptr_name(call->fn);
316 if (!name)
317 return;
319 FOR_EACH_PTR(member_callbacks, cb) {
320 slist = get_all_states(cb->owner);
321 i = 0;
322 FOR_EACH_PTR(call->args, arg) {
323 print_struct_members(call, arg, i, slist, cb->callback);
324 i++;
325 } END_FOR_EACH_PTR(arg);
326 free_slist(&slist);
327 } END_FOR_EACH_PTR(cb);
329 free_string(name);
332 static int get_param(int param, char **name, struct symbol **sym)
334 struct symbol *arg;
335 int i;
337 i = 0;
338 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
340 * this is a temporary hack to work around a bug (I think in sparse?)
341 * 2.6.37-rc1:fs/reiserfs/journal.o
342 * If there is a function definition without parameter name found
343 * after a function implementation then it causes a crash.
344 * int foo() {}
345 * int bar(char *);
347 if (arg->ident->name < (char *)100)
348 continue;
349 if (i == param && arg->ident->name) {
350 *name = arg->ident->name;
351 *sym = arg;
352 return TRUE;
354 i++;
355 } END_FOR_EACH_PTR(arg);
357 return FALSE;
360 static struct state_list *final_states;
361 static int prev_func_id = -1;
362 static int db_callback(void *unused, int argc, char **argv, char **azColName)
364 int func_id;
365 long type;
366 long param;
367 char *name = NULL;
368 struct symbol *sym = NULL;
369 struct def_callback *def_callback;
371 if (argc != 5)
372 return 0;
374 func_id = atoi(argv[0]);
375 errno = 0;
376 type = strtol(argv[1], NULL, 10);
377 param = strtol(argv[2], NULL, 10);
378 if (errno)
379 return 0;
381 if (prev_func_id == -1)
382 prev_func_id = func_id;
383 if (func_id != prev_func_id) {
384 merge_slist(&final_states, __pop_fake_cur_slist());
385 __push_fake_cur_slist();
386 __unnullify_path();
387 prev_func_id = func_id;
390 if (type == INTERNAL)
391 return 0;
392 if (param >= 0 && !get_param(param, &name, &sym))
393 return 0;
395 FOR_EACH_PTR(callbacks, def_callback) {
396 if (def_callback->hook_type == type)
397 def_callback->callback(name, sym, argv[3], argv[4]);
398 } END_FOR_EACH_PTR(def_callback);
400 return 0;
403 static void get_direct_callers(struct symbol *sym)
405 char sql_filter[1024];
407 if (sym->ctype.modifiers & MOD_STATIC) {
408 snprintf(sql_filter, 1024,
409 "file = '%s' and function = '%s' order by function_id;",
410 get_filename(), sym->ident->name);
411 } else {
412 snprintf(sql_filter, 1024,
413 "function = '%s' and static = 0 order by function_id;",
414 sym->ident->name);
417 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
418 " where %s", sql_filter);
421 static char *ptr_name;
422 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
424 if (!ptr_name)
425 ptr_name = alloc_string(argv[0]);
426 return 0;
429 static void get_function_pointer_callers(struct symbol *sym)
431 char sql_filter[1024];
433 if (sym->ctype.modifiers & MOD_STATIC) {
434 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
435 get_filename(), sym->ident->name);
436 } else {
437 snprintf(sql_filter, 1024, "function = '%s';",
438 sym->ident->name);
441 ptr_name = NULL;
442 run_sql(get_ptr_name, "select ptr from function_ptr where %s", sql_filter);
443 if (!ptr_name)
444 return;
446 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
447 " where function = '%s' order by function_id", ptr_name);
449 free_string(ptr_name);
452 static void match_data_from_db(struct symbol *sym)
454 struct sm_state *sm;
456 if (!sym || !sym->ident || !sym->ident->name)
457 return;
459 __push_fake_cur_slist();
460 __unnullify_path();
461 prev_func_id = -1;
463 get_direct_callers(sym);
464 get_function_pointer_callers(sym);
466 merge_slist(&final_states, __pop_fake_cur_slist());
468 FOR_EACH_PTR(final_states, sm) {
469 __set_sm(sm);
470 } END_FOR_EACH_PTR(sm);
472 free_slist(&final_states);
475 static void match_function_assign(struct expression *expr)
477 struct expression *right = expr->right;
478 struct symbol *sym;
479 char *fn_name;
480 char *ptr_name;
482 if (right->type == EXPR_PREOP && right->op == '&')
483 right = strip_expr(right->unop);
484 if (right->type != EXPR_SYMBOL)
485 return;
486 sym = get_type(right);
487 if (!sym || sym->type != SYM_FN)
488 return;
490 fn_name = expr_to_var(right);
491 ptr_name = get_fnptr_name(expr->left);
492 if (!fn_name || !ptr_name)
493 goto free;
495 sql_insert_function_ptr(fn_name, ptr_name);
497 free:
498 free_string(fn_name);
499 free_string(ptr_name);
502 static struct expression *call_implies_call_expr;
503 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
505 struct call_implies_callback *cb;
506 struct expression *arg = NULL;
507 int type;
508 int param;
510 if (argc != 4)
511 return 0;
513 type = atoi(argv[1]);
514 param = atoi(argv[2]);
516 FOR_EACH_PTR(call_implies_cb_list, cb) {
517 if (cb->type != type)
518 continue;
519 if (param != -1) {
520 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
521 if (!arg)
522 continue;
524 cb->callback(arg, argv[3]);
525 } END_FOR_EACH_PTR(cb);
527 return 0;
530 static void match_call_implies(struct expression *expr)
532 struct symbol *sym;
533 static char sql_filter[1024];
535 if (expr->fn->type != EXPR_SYMBOL)
536 return;
537 sym = expr->fn->symbol;
538 if (!sym)
539 return;
541 if (sym->ctype.modifiers & MOD_STATIC) {
542 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
543 get_filename(), sym->ident->name);
544 } else {
545 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
546 sym->ident->name);
549 call_implies_call_expr = expr;
550 run_sql(call_implies_callbacks,
551 "select function, type, parameter, value from call_implies where %s",
552 sql_filter);
553 return;
556 static void print_initializer_list(struct expression_list *expr_list,
557 struct symbol *struct_type)
559 struct expression *expr;
560 struct symbol *base_type;
561 char struct_name[256];
563 FOR_EACH_PTR(expr_list, expr) {
564 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
565 print_initializer_list(expr->idx_expression->expr_list, struct_type);
566 continue;
568 if (expr->type != EXPR_IDENTIFIER)
569 continue;
570 if (!expr->expr_ident)
571 continue;
572 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
573 continue;
574 base_type = get_type(expr->ident_expression);
575 if (!base_type || base_type->type != SYM_FN)
576 continue;
577 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
578 struct_type->ident->name, expr->expr_ident->name);
579 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
580 struct_name);
581 } END_FOR_EACH_PTR(expr);
584 static void global_variable(struct symbol *sym)
586 struct symbol *struct_type;
588 if (!sym->ident)
589 return;
590 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
591 return;
592 struct_type = get_base_type(sym);
593 if (!struct_type)
594 return;
595 if (struct_type->type == SYM_ARRAY) {
596 struct_type = get_base_type(struct_type);
597 if (!struct_type)
598 return;
600 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
601 return;
602 print_initializer_list(sym->initializer->expr_list, struct_type);
605 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
607 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
610 static int return_id;
611 static void match_function_def(struct symbol *sym)
613 return_id = 0;
616 static void call_return_state_hooks_compare(struct expression *expr)
618 struct returned_state_callback *cb;
619 struct state_list *slist;
620 char *return_ranges;
621 int final_pass_orig = final_pass;
623 __push_fake_cur_slist();
625 final_pass = 0;
626 __split_whole_condition(expr);
627 final_pass = final_pass_orig;
629 return_ranges = alloc_sname("1");
631 return_id++;
632 slist = __get_cur_slist();
633 FOR_EACH_PTR(returned_state_callbacks, cb) {
634 cb->callback(return_id, return_ranges, expr, slist);
635 } END_FOR_EACH_PTR(cb);
637 __push_true_states();
638 __use_false_states();
640 return_ranges = alloc_sname("0");;
641 return_id++;
642 slist = __get_cur_slist();
643 FOR_EACH_PTR(returned_state_callbacks, cb) {
644 cb->callback(return_id, return_ranges, expr, slist);
645 } END_FOR_EACH_PTR(cb);
647 __merge_true_states();
648 __pop_fake_cur_slist();
651 static int call_return_state_hooks_split_possible(struct expression *expr)
653 struct returned_state_callback *cb;
654 struct state_list *slist;
655 struct range_list *rl;
656 char *return_ranges;
657 struct sm_state *sm;
658 struct sm_state *tmp;
659 int ret = 0;
660 int nr_possible, nr_states;
662 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
663 if (!sm || !sm->merged)
664 return 0;
666 /* bail if it gets too complicated */
667 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
668 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
669 if (nr_possible >= 100)
670 return 0;
671 if (nr_states * nr_possible >= 1000)
672 return 0;
674 FOR_EACH_PTR(sm->possible, tmp) {
675 if (tmp->merged)
676 continue;
678 ret = 1;
679 __push_fake_cur_slist();
681 overwrite_states_using_pool(tmp);
683 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
684 return_ranges = show_rl(rl);
686 return_id++;
687 slist = __get_cur_slist();
688 FOR_EACH_PTR(returned_state_callbacks, cb) {
689 cb->callback(return_id, return_ranges, expr, slist);
690 } END_FOR_EACH_PTR(cb);
692 __pop_fake_cur_slist();
693 } END_FOR_EACH_PTR(tmp);
695 return ret;
698 static void call_return_state_hooks(struct expression *expr)
700 struct returned_state_callback *cb;
701 struct state_list *slist;
702 struct range_list *rl;
703 char *return_ranges;
704 int nr_states;
706 expr = strip_expr(expr);
708 if (!expr) {
709 return_ranges = alloc_sname("");
710 } else if (is_condition(expr)) {
711 call_return_state_hooks_compare(expr);
712 return;
713 } else if (call_return_state_hooks_split_possible(expr)) {
714 return;
715 } else if (get_implied_rl(expr, &rl)) {
716 rl = cast_rl(cur_func_return_type(), rl);
717 return_ranges = show_rl(rl);
718 } else {
719 rl = alloc_whole_rl(cur_func_return_type());
720 return_ranges = show_rl(rl);
723 return_id++;
724 slist = __get_cur_slist();
725 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
726 FOR_EACH_PTR(returned_state_callbacks, cb) {
727 if (nr_states < 10000)
728 cb->callback(return_id, return_ranges, expr, slist);
729 else
730 cb->callback(return_id, return_ranges, expr, NULL);
731 } END_FOR_EACH_PTR(cb);
734 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
736 struct returned_member_callback *cb;
737 struct state_list *my_slist;
738 struct sm_state *sm;
739 struct symbol *type;
740 char *name;
741 char member_name[256];
742 int len;
744 type = get_type(expr);
745 if (!type || type->type != SYM_PTR)
746 return;
747 type = get_real_base_type(type);
748 if (!type || type->type != SYM_STRUCT)
749 return;
750 name = expr_to_var(expr);
751 if (!name)
752 return;
754 member_name[sizeof(member_name) - 1] = '\0';
755 strcpy(member_name, "$$");
757 len = strlen(name);
758 FOR_EACH_PTR(returned_member_callbacks, cb) {
759 my_slist = get_all_states_slist(cb->owner, slist);
760 FOR_EACH_PTR(my_slist, sm) {
761 if (strncmp(sm->name, name, len) != 0)
762 continue;
763 if (strncmp(sm->name + len, "->", 2) != 0)
764 continue;
765 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
766 cb->callback(return_id, return_ranges, member_name, sm->state);
767 } END_FOR_EACH_PTR(sm);
768 free_slist(&my_slist);
769 } END_FOR_EACH_PTR(cb);
771 free_string(name);
774 static void match_end_func_info(struct symbol *sym)
776 if (__path_is_null())
777 return;
778 call_return_state_hooks(NULL);
781 static void init_memdb(void)
783 char *err = NULL;
784 int rc;
785 const char *schema_files[] = {
786 "db/db.schema",
787 "db/caller_info.schema",
788 "db/return_states.schema",
789 "db/type_size.schema",
790 "db/call_implies.schema",
791 "db/function_ptr.schema",
792 "db/return_values.schema",
794 static char buf[4096];
795 int fd;
796 int ret;
797 int i;
799 rc = sqlite3_open(":memory:", &mem_db);
800 if (rc != SQLITE_OK) {
801 printf("Error starting In-Memory database.");
802 return;
805 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
806 fd = open_data_file(schema_files[i]);
807 if (fd < 0)
808 continue;
809 ret = read(fd, buf, sizeof(buf));
810 if (ret == sizeof(buf)) {
811 printf("Schema file too large: %s (limit %ld bytes)",
812 schema_files[i], sizeof(buf));
814 buf[ret] = '\0';
815 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
816 if (rc != SQLITE_OK) {
817 fprintf(stderr, "SQL error #2: %s\n", err);
818 fprintf(stderr, "%s\n", buf);
823 void open_smatch_db(void)
825 int rc;
827 if (option_no_db)
828 return;
830 init_memdb();
832 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
833 if (rc != SQLITE_OK) {
834 option_no_db = 1;
835 return;
837 return;
840 void register_definition_db_callbacks(int id)
842 add_hook(&match_function_def, FUNC_DEF_HOOK);
844 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
845 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
846 add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
847 add_hook(&global_variable, BASE_HOOK);
848 add_hook(&global_variable, DECLARATION_HOOK);
849 add_returned_state_callback(match_return_info);
850 add_returned_state_callback(print_returned_struct_members);
851 add_hook(&call_return_state_hooks, RETURN_HOOK);
852 add_hook(&match_end_func_info, END_FUNC_HOOK);
854 if (option_no_db)
855 return;
857 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
858 add_hook(&match_call_implies, FUNCTION_CALL_HOOK);
861 void register_db_call_marker(int id)
863 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
866 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
868 char buf[256];
869 char *tmp;
871 if (strcmp(key, "$$") == 0)
872 return expr_to_var_sym(arg, sym);
874 if (strcmp(key, "*$$") == 0) {
875 if (arg->type == EXPR_PREOP && arg->op == '&') {
876 arg = strip_expr(arg->unop);
877 return expr_to_var_sym(arg, sym);
878 } else {
879 tmp = expr_to_var_sym(arg, sym);
880 if (!tmp)
881 return NULL;
882 snprintf(buf, sizeof(buf), "*%s", tmp);
883 free_string(tmp);
884 return alloc_string(buf);
888 if (arg->type == EXPR_PREOP && arg->op == '&') {
889 arg = strip_expr(arg->unop);
890 tmp = expr_to_var_sym(arg, sym);
891 if (!tmp)
892 return NULL;
893 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
894 return alloc_string(buf);
897 tmp = expr_to_var_sym(arg, sym);
898 if (!tmp)
899 return NULL;
900 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
901 free_string(tmp);
902 return alloc_string(buf);