ranges: cleanup: move true_comparison_range_lr() into the function
[smatch.git] / smatch_project.c
blob27ed61d8330fd623f059025b5f1816bb4819c677
1 /*
2 * sparse/smatch_project.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This file is only for very generic stuff, that is reusable
12 * between projects. If you need something special create a
13 * check_your_project.c.
17 #include "smatch.h"
18 #include "smatch_extra.h"
20 static void register_no_return_funcs(void)
22 struct token *token;
23 const char *func;
24 char name[256];
26 if (option_project == PROJ_NONE)
27 strcpy(name, "no_return_funcs");
28 else
29 snprintf(name, 256, "%s.no_return_funcs", option_project_str);
31 token = get_tokens_file(name);
32 if (!token)
33 return;
34 if (token_type(token) != TOKEN_STREAMBEGIN)
35 return;
36 token = token->next;
37 while (token_type(token) != TOKEN_STREAMEND) {
38 if (token_type(token) != TOKEN_IDENT)
39 return;
40 func = show_ident(token->ident);
41 add_function_hook(func, &__match_nullify_path_hook, NULL);
42 token = token->next;
44 clear_token_alloc();
47 static void register_ignored_macros(void)
49 struct token *token;
50 char *macro;
51 char name[256];
53 if (option_project == PROJ_NONE)
54 strcpy(name, "ignored_macros");
55 else
56 snprintf(name, 256, "%s.ignored_macros", option_project_str);
58 token = get_tokens_file(name);
59 if (!token)
60 return;
61 if (token_type(token) != TOKEN_STREAMBEGIN)
62 return;
63 token = token->next;
64 while (token_type(token) != TOKEN_STREAMEND) {
65 if (token_type(token) != TOKEN_IDENT)
66 return;
67 macro = alloc_string(show_ident(token->ident));
68 add_ptr_list(&__ignored_macros, macro);
69 token = token->next;
71 clear_token_alloc();
74 struct param_implication {
75 int param;
76 struct range_list *rl;
79 static void match_param_implication(const char *fn, struct expression *call_expr,
80 struct expression *assign_expr, void *_imp)
82 struct param_implication *imp = _imp;
83 struct expression *arg;
85 arg = get_argument_from_call_expr(call_expr->args, imp->param);
86 set_extra_expr_nomod(arg, alloc_estate_range_list(clone_range_list(imp->rl)));
89 static void add_param_implication(const char *func, int param, char *range, char *ret_range)
91 struct range_list *ret_rl = NULL;
92 struct range_list *rl = NULL;
93 struct param_implication *imp;
95 get_value_ranges(ret_range, &ret_rl);
97 get_value_ranges(range, &rl);
98 rl = clone_permanent(rl);
100 imp = malloc(sizeof(*imp));
101 imp->param = param;
102 imp->rl = rl;
104 printf("%s returning %lld-%lld implies %s\n", func, rl_min(ret_rl), rl_max(ret_rl), show_ranges(rl));
106 return_implies_state(func, rl_min(ret_rl), rl_max(ret_rl), &match_param_implication, imp);
109 static void register_parameter_implications(void)
111 char name[256];
112 struct token *token;
113 const char *func;
114 int param;
115 char *range;
116 char *ret_range;
118 snprintf(name, 256, "%s.parameter_implications", option_project_str);
119 name[255] = '\0';
120 token = get_tokens_file(name);
121 if (!token)
122 return;
123 if (token_type(token) != TOKEN_STREAMBEGIN)
124 return;
125 token = token->next;
126 while (token_type(token) != TOKEN_STREAMEND) {
127 if (token_type(token) != TOKEN_IDENT)
128 return;
129 func = show_ident(token->ident);
131 token = token->next;
132 if (token_type(token) != TOKEN_NUMBER)
133 return;
134 param = atoi(token->number);
136 token = token->next;
137 if (token_type(token) != TOKEN_STRING)
138 return;
139 range = token->string->data;
141 token = token->next;
142 if (token_type(token) != TOKEN_STRING)
143 return;
144 ret_range = token->string->data;
147 add_param_implication(func, param, range, ret_range);
149 token = token->next;
151 clear_token_alloc();
154 void register_project(int id)
156 register_no_return_funcs();
157 register_ignored_macros();
158 register_parameter_implications();