smdb.py: fix test for if a datatype is known
[smatch.git] / smatch_data_source.c
blob9fe3a03243de23da986207ebb5ace6312812a4e3
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 struct sm_state *orig, *cur;
30 char *ret = NULL;
31 char buf[32];
33 name = expr_to_var_sym(expr, &sym);
34 if (!name || !sym)
35 goto out;
36 param = get_param_num_from_sym(sym);
37 if (param < 0)
38 goto out;
39 cur = get_sm_state(SMATCH_EXTRA, name, sym);
40 if (!cur)
41 goto out;
42 orig = get_sm_state_stree(get_start_states(), SMATCH_EXTRA, name, sym);
43 if (!orig)
44 goto out;
45 if (orig != cur)
46 goto out;
48 snprintf(buf, sizeof(buf), "p %d", param);
49 ret = alloc_string(buf);
51 out:
52 free_string(name);
53 return ret;
56 static char *get_source_assignment(struct expression *expr)
58 struct expression *right;
59 char *name;
60 char buf[64];
61 char *ret;
63 right = get_assigned_expr(expr);
64 right = strip_expr(right);
65 if (!right)
66 return NULL;
67 if (right->type != EXPR_CALL || right->fn->type != EXPR_SYMBOL)
68 return NULL;
69 if (is_fake_call(right))
70 return NULL;
71 name = expr_to_str(right->fn);
72 if (!name)
73 return NULL;
74 snprintf(buf, sizeof(buf), "r %s", name);
75 ret = alloc_string(buf);
76 free_string(name);
77 return ret;
80 static char *get_source_str(struct expression *expr)
82 char *source;
84 source = get_source_parameter(expr);
85 if (source)
86 return source;
87 return get_source_assignment(expr);
90 static void match_caller_info(struct expression *expr)
92 struct expression *tmp;
93 char *source;
94 int i;
96 i = -1;
97 FOR_EACH_PTR(expr->args, tmp) {
98 i++;
99 source = get_source_str(tmp);
100 if (!source)
101 continue;
102 sql_insert_caller_info(expr, DATA_SOURCE, i, "$$", source);
103 free_string(source);
104 } END_FOR_EACH_PTR(tmp);
107 void register_data_source(int id)
109 if (!option_info)
110 return;
111 my_id = id;
112 add_hook(&match_caller_info, FUNCTION_CALL_HOOK);