db: sort return_states by file
[smatch.git] / smatch_data_source.c
blob9d1f3165dd029e182c0df7db744778791caebfff
1 /*
2 * Copyright (C) 2013 Oracle.
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
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
22 static int my_id;
24 static char *get_source_parameter(struct expression *expr)
26 struct symbol *sym;
27 char *name;
28 int param;
29 char *ret = NULL;
30 char buf[32];
32 expr = strip_expr(expr);
33 if (expr->type != EXPR_SYMBOL)
34 return NULL;
36 name = expr_to_var_sym(expr, &sym);
37 if (!name || !sym)
38 goto free;
39 param = get_param_num_from_sym(sym);
40 if (param < 0)
41 goto free;
42 if (param_was_set(expr))
43 goto free;
45 snprintf(buf, sizeof(buf), "p %d", param);
46 ret = alloc_string(buf);
48 free:
49 free_string(name);
50 return ret;
53 static char *get_source_assignment(struct expression *expr)
55 struct expression *right;
56 char *name;
57 char buf[64];
58 char *ret;
60 right = get_assigned_expr(expr);
61 right = strip_expr(right);
62 if (!right)
63 return NULL;
64 if (right->type != EXPR_CALL || right->fn->type != EXPR_SYMBOL)
65 return NULL;
66 if (is_fake_call(right))
67 return NULL;
68 name = expr_to_str(right->fn);
69 if (!name)
70 return NULL;
71 snprintf(buf, sizeof(buf), "r %s", name);
72 ret = alloc_string(buf);
73 free_string(name);
74 return ret;
77 static char *get_source_str(struct expression *expr)
79 char *source;
81 source = get_source_parameter(expr);
82 if (source)
83 return source;
84 return get_source_assignment(expr);
87 static void match_caller_info(struct expression *expr)
89 struct expression *tmp;
90 char *source;
91 int i;
93 i = -1;
94 FOR_EACH_PTR(expr->args, tmp) {
95 i++;
96 source = get_source_str(tmp);
97 if (!source)
98 continue;
99 sql_insert_caller_info(expr, DATA_SOURCE, i, "$", source);
100 free_string(source);
101 } END_FOR_EACH_PTR(tmp);
104 void register_data_source(int id)
106 // if (!option_info)
107 // return;
108 my_id = id;
109 add_hook(&match_caller_info, FUNCTION_CALL_HOOK);