xref: /illumos-gate/usr/src/tools/smatch/src/smatch_statement_count.c (revision 1f5207b7604fb44407eb4342aff613f7c4508508)
1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2018 Oracle.
3*1f5207b7SJohn Levon  *
4*1f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon  * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*1f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*1f5207b7SJohn Levon  * GNU General Public License for more details.
13*1f5207b7SJohn Levon  *
14*1f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon  */
17*1f5207b7SJohn Levon 
18*1f5207b7SJohn Levon #include <stdlib.h>
19*1f5207b7SJohn Levon #include "parse.h"
20*1f5207b7SJohn Levon #include "smatch.h"
21*1f5207b7SJohn Levon #include "smatch_slist.h"
22*1f5207b7SJohn Levon #include "smatch_extra.h"
23*1f5207b7SJohn Levon 
24*1f5207b7SJohn Levon static int my_id;
25*1f5207b7SJohn Levon 
26*1f5207b7SJohn Levon static struct smatch_state *merge_states(struct smatch_state *s1, struct smatch_state *s2)
27*1f5207b7SJohn Levon {
28*1f5207b7SJohn Levon 	int left, right, min;
29*1f5207b7SJohn Levon 
30*1f5207b7SJohn Levon 	left = PTR_INT(s1->data);
31*1f5207b7SJohn Levon 	right = PTR_INT(s2->data);
32*1f5207b7SJohn Levon 
33*1f5207b7SJohn Levon 	min = left;
34*1f5207b7SJohn Levon 	if (right < min)
35*1f5207b7SJohn Levon 		min = right;
36*1f5207b7SJohn Levon 	return alloc_state_num(min);
37*1f5207b7SJohn Levon }
38*1f5207b7SJohn Levon 
39*1f5207b7SJohn Levon long get_stmt_cnt(void)
40*1f5207b7SJohn Levon {
41*1f5207b7SJohn Levon 	struct smatch_state *state;
42*1f5207b7SJohn Levon 
43*1f5207b7SJohn Levon 	state = get_state(my_id, "stmts", NULL);
44*1f5207b7SJohn Levon 	if (!state)
45*1f5207b7SJohn Levon 		return 0;
46*1f5207b7SJohn Levon 	return (long)state->data;
47*1f5207b7SJohn Levon }
48*1f5207b7SJohn Levon 
49*1f5207b7SJohn Levon static void match_statement(struct statement *stmt)
50*1f5207b7SJohn Levon {
51*1f5207b7SJohn Levon 	int cnt;
52*1f5207b7SJohn Levon 
53*1f5207b7SJohn Levon 	cnt = get_stmt_cnt();
54*1f5207b7SJohn Levon 	cnt++;
55*1f5207b7SJohn Levon 	set_state(my_id, "stmts", NULL, alloc_state_num(cnt));
56*1f5207b7SJohn Levon }
57*1f5207b7SJohn Levon 
58*1f5207b7SJohn Levon static void insert_return_info(int return_id, char *return_ranges, struct expression *expr)
59*1f5207b7SJohn Levon {
60*1f5207b7SJohn Levon 	char buf[32];
61*1f5207b7SJohn Levon 	int cnt;
62*1f5207b7SJohn Levon 
63*1f5207b7SJohn Levon 	cnt = get_stmt_cnt();
64*1f5207b7SJohn Levon 	snprintf(buf, sizeof(buf), "%d", cnt);
65*1f5207b7SJohn Levon 	sql_insert_return_states(return_id, return_ranges, STMT_CNT, -1, "", buf);
66*1f5207b7SJohn Levon }
67*1f5207b7SJohn Levon 
68*1f5207b7SJohn Levon static void select_return_info(struct expression *expr, int param, char *key, char *value)
69*1f5207b7SJohn Levon {
70*1f5207b7SJohn Levon 	int cnt, add;
71*1f5207b7SJohn Levon 
72*1f5207b7SJohn Levon 	cnt = get_stmt_cnt();
73*1f5207b7SJohn Levon 	add = atoi(value);
74*1f5207b7SJohn Levon 
75*1f5207b7SJohn Levon 	set_state(my_id, "stmts", NULL, alloc_state_num(cnt + add));
76*1f5207b7SJohn Levon }
77*1f5207b7SJohn Levon 
78*1f5207b7SJohn Levon void register_statement_count(int id)
79*1f5207b7SJohn Levon {
80*1f5207b7SJohn Levon 	my_id = id;
81*1f5207b7SJohn Levon 
82*1f5207b7SJohn Levon 	add_hook(match_statement, STMT_HOOK);
83*1f5207b7SJohn Levon 	add_merge_hook(my_id, &merge_states);
84*1f5207b7SJohn Levon 
85*1f5207b7SJohn Levon 	add_split_return_callback(&insert_return_info);
86*1f5207b7SJohn Levon 	select_return_states_hook(STMT_CNT, &select_return_info);
87*1f5207b7SJohn Levon }
88*1f5207b7SJohn Levon 
89