11f5207b7SJohn Levon /*
21f5207b7SJohn Levon * Copyright (C) 2013 Oracle.
31f5207b7SJohn Levon *
41f5207b7SJohn Levon * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon * of the License, or (at your option) any later version.
81f5207b7SJohn Levon *
91f5207b7SJohn Levon * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121f5207b7SJohn Levon * GNU General Public License for more details.
131f5207b7SJohn Levon *
141f5207b7SJohn Levon * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon */
171f5207b7SJohn Levon
181f5207b7SJohn Levon #include "smatch.h"
191f5207b7SJohn Levon #include "smatch_slist.h"
201f5207b7SJohn Levon #include "smatch_extra.h"
211f5207b7SJohn Levon
221f5207b7SJohn Levon static int my_id;
231f5207b7SJohn Levon
get_source_parameter(struct expression * expr)241f5207b7SJohn Levon static char *get_source_parameter(struct expression *expr)
251f5207b7SJohn Levon {
261f5207b7SJohn Levon struct expression *tmp;
27*efe51d0cSJohn Levon const char *param_name;
281f5207b7SJohn Levon struct symbol *sym;
291f5207b7SJohn Levon char *name;
301f5207b7SJohn Levon int param;
311f5207b7SJohn Levon char *ret = NULL;
321f5207b7SJohn Levon char buf[32];
331f5207b7SJohn Levon int cnt = 0;
34*efe51d0cSJohn Levon bool modified = false;
351f5207b7SJohn Levon
361f5207b7SJohn Levon tmp = expr;
371f5207b7SJohn Levon while ((tmp = get_assigned_expr(tmp))) {
381f5207b7SJohn Levon expr = tmp;
391f5207b7SJohn Levon if (cnt++ > 3)
401f5207b7SJohn Levon break;
411f5207b7SJohn Levon }
421f5207b7SJohn Levon
431f5207b7SJohn Levon expr = strip_expr(expr);
441f5207b7SJohn Levon if (expr->type != EXPR_SYMBOL)
451f5207b7SJohn Levon return NULL;
461f5207b7SJohn Levon
471f5207b7SJohn Levon name = expr_to_var_sym(expr, &sym);
481f5207b7SJohn Levon if (!name || !sym)
491f5207b7SJohn Levon goto free;
501f5207b7SJohn Levon param = get_param_num_from_sym(sym);
511f5207b7SJohn Levon if (param < 0)
521f5207b7SJohn Levon goto free;
53*efe51d0cSJohn Levon param_name = get_param_name_var_sym(name, sym);
54*efe51d0cSJohn Levon if (!param_name)
551f5207b7SJohn Levon goto free;
56*efe51d0cSJohn Levon if (param_was_set_var_sym(name, sym))
57*efe51d0cSJohn Levon modified = true;
581f5207b7SJohn Levon
59*efe51d0cSJohn Levon snprintf(buf, sizeof(buf), "$%d%s%s", param, param_name + 1,
60*efe51d0cSJohn Levon modified ? " [m]" : "");
611f5207b7SJohn Levon ret = alloc_string(buf);
621f5207b7SJohn Levon
631f5207b7SJohn Levon free:
641f5207b7SJohn Levon free_string(name);
651f5207b7SJohn Levon return ret;
661f5207b7SJohn Levon }
671f5207b7SJohn Levon
get_source_assignment(struct expression * expr)681f5207b7SJohn Levon static char *get_source_assignment(struct expression *expr)
691f5207b7SJohn Levon {
701f5207b7SJohn Levon struct expression *right;
711f5207b7SJohn Levon char *name;
721f5207b7SJohn Levon char buf[64];
731f5207b7SJohn Levon char *ret;
741f5207b7SJohn Levon
751f5207b7SJohn Levon right = get_assigned_expr(expr);
761f5207b7SJohn Levon right = strip_expr(right);
771f5207b7SJohn Levon if (!right)
781f5207b7SJohn Levon return NULL;
791f5207b7SJohn Levon if (right->type != EXPR_CALL || right->fn->type != EXPR_SYMBOL)
801f5207b7SJohn Levon return NULL;
811f5207b7SJohn Levon if (is_fake_call(right))
821f5207b7SJohn Levon return NULL;
831f5207b7SJohn Levon name = expr_to_str(right->fn);
841f5207b7SJohn Levon if (!name)
851f5207b7SJohn Levon return NULL;
861f5207b7SJohn Levon snprintf(buf, sizeof(buf), "r %s", name);
871f5207b7SJohn Levon ret = alloc_string(buf);
881f5207b7SJohn Levon free_string(name);
891f5207b7SJohn Levon return ret;
901f5207b7SJohn Levon }
911f5207b7SJohn Levon
get_source_str(struct expression * arg)921f5207b7SJohn Levon static char *get_source_str(struct expression *arg)
931f5207b7SJohn Levon {
941f5207b7SJohn Levon char *source;
951f5207b7SJohn Levon
961f5207b7SJohn Levon source = get_source_parameter(arg);
971f5207b7SJohn Levon if (source)
981f5207b7SJohn Levon return source;
991f5207b7SJohn Levon return get_source_assignment(arg);
1001f5207b7SJohn Levon }
1011f5207b7SJohn Levon
match_caller_info(struct expression * expr)1021f5207b7SJohn Levon static void match_caller_info(struct expression *expr)
1031f5207b7SJohn Levon {
1041f5207b7SJohn Levon struct expression *arg;
1051f5207b7SJohn Levon char *source;
1061f5207b7SJohn Levon int i;
1071f5207b7SJohn Levon
1081f5207b7SJohn Levon i = -1;
1091f5207b7SJohn Levon FOR_EACH_PTR(expr->args, arg) {
1101f5207b7SJohn Levon i++;
1111f5207b7SJohn Levon source = get_source_str(arg);
1121f5207b7SJohn Levon if (!source)
1131f5207b7SJohn Levon continue;
1141f5207b7SJohn Levon sql_insert_caller_info(expr, DATA_SOURCE, i, "$", source);
1151f5207b7SJohn Levon free_string(source);
1161f5207b7SJohn Levon } END_FOR_EACH_PTR(arg);
1171f5207b7SJohn Levon }
1181f5207b7SJohn Levon
register_data_source(int id)1191f5207b7SJohn Levon void register_data_source(int id)
1201f5207b7SJohn Levon {
1211f5207b7SJohn Levon // if (!option_info)
1221f5207b7SJohn Levon // return;
1231f5207b7SJohn Levon my_id = id;
1241f5207b7SJohn Levon add_hook(&match_caller_info, FUNCTION_CALL_HOOK);
1251f5207b7SJohn Levon }
126