buf_size, strlen: don't use strlen() to determine the buf size
[smatch.git] / smatch_links.c
blob76e2b3e95a8cf774152f8fb1fe412e34c791f4cc
1 /*
2 * Copyright (C) 2014 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
19 * Some helper functions for managing links.
23 #include "smatch.h"
24 #include "smatch_slist.h"
26 static struct smatch_state *alloc_link(struct var_sym_list *links)
28 struct smatch_state *state;
29 static char buf[256];
30 struct var_sym *tmp;
31 int i;
33 state = __alloc_smatch_state(0);
35 i = 0;
36 FOR_EACH_PTR(links, tmp) {
37 if (!i++) {
38 snprintf(buf, sizeof(buf), "%s", tmp->var);
39 } else {
40 append(buf, ", ", sizeof(buf));
41 append(buf, tmp->var, sizeof(buf));
43 } END_FOR_EACH_PTR(tmp);
45 state->name = alloc_sname(buf);
46 state->data = links;
47 return state;
50 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2)
52 struct var_sym_list *new_links;
54 new_links = clone_var_sym_list(s1->data);
55 merge_var_sym_list(&new_links, s2->data);
57 return alloc_link(new_links);
60 void store_link(int link_id, const char *var, struct symbol *sym, const char *link_name, struct symbol *link_sym)
63 struct smatch_state *old_state;
64 struct var_sym_list *links;
66 old_state = get_state(link_id, var, sym);
67 if (old_state)
68 links = clone_var_sym_list(old_state->data);
69 else
70 links = NULL;
72 add_var_sym(&links, link_name, link_sym);
73 set_state(link_id, var, sym, alloc_link(links));
76 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
78 struct var_sym_list *links;
79 struct var_sym *tmp;
81 links = sm->state->data;
83 FOR_EACH_PTR(links, tmp) {
84 set_state(sm->owner - 1, tmp->var, tmp->sym, &undefined);
85 } END_FOR_EACH_PTR(tmp);
86 set_state(sm->owner, sm->name, sm->sym, &undefined);
89 void set_up_link_functions(int id, int link_id)
91 if (id + 1 != link_id) {
92 printf("FATAL ERROR: links need to be registered direclty after the check");
93 exit(1);
96 add_merge_hook(link_id, &merge_link_states);
97 add_modification_hook(link_id, &match_link_modify);
98 // free link at the end of function