comparison: add support for COMPARE_LIMIT
[smatch.git] / check_negative_error_code_type_promoted.c
blobb75c46d416687602dee689f29db2ca612505352a
1 /*
2 * Copyright 2023 Linaro Ltd.
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 "smatch.h"
19 #include "smatch_extra.h"
21 static int my_id;
23 static bool flip_order(struct expression *expr, struct expression **left, int *op, struct expression **right)
25 /* flip everything to < */
27 expr = strip_expr(expr);
29 switch (expr->op) {
30 case '>':
31 case SPECIAL_GTE:
32 case SPECIAL_UNSIGNED_GT:
33 case SPECIAL_UNSIGNED_GTE:
34 *left = strip_parens(expr->right);
35 *op = flip_comparison(expr->op);
36 *right = strip_parens(expr->left);
37 return true;
38 case '<':
39 case SPECIAL_LTE:
40 case SPECIAL_UNSIGNED_LT:
41 case SPECIAL_UNSIGNED_LTE:
42 *left = strip_parens(expr->left);
43 *op = expr->op;
44 *right = strip_parens(expr->right);
45 return true;
48 return false;
51 static void match_condition(struct expression *expr)
53 struct expression *left, *right;
54 char *name;
55 int op;
57 if (expr->type != EXPR_COMPARE)
58 return;
60 if (!flip_order(expr, &left, &op, &right))
61 return;
63 if (op != SPECIAL_UNSIGNED_LT &&
64 op != SPECIAL_UNSIGNED_LTE)
65 return;
67 if (!holds_kernel_error_codes(left))
68 return;
70 name = expr_to_str(left);
71 sm_warning("error code type promoted to positive: '%s'", name);
72 free_string(name);
75 void check_negative_error_code_type_promoted(int id)
77 my_id = id;
79 if (option_project != PROJ_KERNEL)
80 return;
82 add_hook(&match_condition, CONDITION_HOOK);