ranges: fix parsing of ulong_max and ptr_max (not currently used)
[smatch.git] / check_nospec.c
blob5287134277c1e478e85d13a42121536cd7bbd513
1 /*
2 * Copyright (C) 2018 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 <stdlib.h>
19 #include "parse.h"
20 #include "smatch.h"
21 #include "smatch_slist.h"
22 #include "smatch_extra.h"
24 static int my_id;
26 STATE(nospec);
28 static struct smatch_state *unmatched_state(struct sm_state *sm)
30 struct range_list *rl;
32 if (__in_function_def && !get_user_rl_var_sym(sm->name, sm->sym, &rl))
33 return &nospec;
34 return &undefined;
37 bool is_nospec(struct expression *expr)
39 char *macro;
41 if (get_state_expr(my_id, expr) == &nospec)
42 return true;
43 macro = get_macro_name(expr->pos);
44 if (macro && strcmp(macro, "array_index_nospec") == 0)
45 return true;
46 return false;
49 static void nospec_assign(struct expression *expr)
51 if (is_nospec(expr->right))
52 set_state_expr(my_id, expr->left, &nospec);
55 static void set_param_nospec(const char *name, struct symbol *sym, char *key, char *value)
57 char fullname[256];
59 if (strcmp(key, "*$") == 0)
60 snprintf(fullname, sizeof(fullname), "*%s", name);
61 else if (strncmp(key, "$", 1) == 0)
62 snprintf(fullname, 256, "%s%s", name, key + 1);
63 else
64 return;
66 set_state(my_id, fullname, sym, &nospec);
69 static void match_call_info(struct expression *expr)
71 struct expression *arg;
72 int i = 0;
74 FOR_EACH_PTR(expr->args, arg) {
75 if (get_state_expr(my_id, arg) == &nospec)
76 sql_insert_caller_info(expr, NOSPEC, i, "$", "");
77 i++;
78 } END_FOR_EACH_PTR(arg);
81 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
83 sql_insert_caller_info(call, NOSPEC, param, printed_name, "");
86 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
88 struct symbol *returned_sym;
89 struct sm_state *sm;
90 const char *param_name;
91 int param;
93 returned_sym = expr_to_sym(expr);
95 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
96 param = get_param_num_from_sym(sm->sym);
97 if (param < 0) {
98 if (!returned_sym || returned_sym != sm->sym)
99 continue;
100 param = -1;
103 param_name = get_param_name(sm);
104 if (!param_name)
105 continue;
106 if (param != -1 && strcmp(param_name, "$") == 0)
107 continue;
109 sql_insert_return_states(return_id, return_ranges, NOSPEC, param, param_name, "");
110 } END_FOR_EACH_SM(sm);
113 void check_nospec(int id)
115 my_id = id;
117 add_hook(&nospec_assign, ASSIGNMENT_HOOK);
119 select_caller_info_hook(set_param_nospec, NOSPEC);
120 add_unmatched_state_hook(my_id, &unmatched_state);
122 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
123 add_member_info_callback(my_id, struct_member_callback);
124 add_split_return_callback(&returned_struct_members);