helper: cache get_member_name()
[smatch.git] / check_missing_error_code2.c
blobd76d2ef3f9b13ab1b28d03625b77b6ee61058f2f
1 /*
2 * Copyright (C) 2022 Dan Carpenter.
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 #include "smatch.h"
20 #include "smatch_extra.h"
21 #include "smatch_slist.h"
23 static int my_id;
25 static struct statement *get_if_statement(struct expression *expr)
27 struct statement *stmt;
29 stmt = get_parent_stmt(expr);
30 if (!stmt || stmt->type != STMT_RETURN)
31 return NULL;
32 stmt = stmt_get_parent_stmt(stmt);
33 if (stmt && stmt->type == STMT_COMPOUND)
34 stmt = stmt_get_parent_stmt(stmt);
35 if (!stmt)
36 return NULL;
38 if (stmt->type != STMT_IF)
39 return NULL;
41 return stmt;
44 static bool condition_matches(struct expression *cond, struct expression *expr)
46 expr = strip_expr(expr);
47 cond = strip_expr(cond);
49 while (cond) {
50 if (cond->type == EXPR_PREOP &&
51 (cond->op == '(' || cond->op == '!')) {
52 cond = strip_expr(cond->unop);
53 continue;
55 if (cond->type == EXPR_COMPARE &&
56 (cond->op == SPECIAL_EQUAL || cond->op == SPECIAL_NOTEQUAL) &&
57 expr_is_zero(cond->right)) {
58 cond = strip_expr(cond->left);
59 continue;
61 break;
64 if (expr_equiv(cond, expr))
65 return true;
67 return false;
70 static struct expression *get_orig_call(struct expression *expr)
72 struct expression *orig;
74 orig = get_assigned_expr(expr);
75 if (!orig || orig->type != EXPR_CALL)
76 return NULL;
78 return orig;
81 static void match_return(struct expression *expr)
83 struct expression *call;
84 struct statement *stmt;
85 sval_t sval;
86 char *name;
88 if (!expr || expr->type != EXPR_SYMBOL)
89 return;
91 if (get_type(expr) != &int_ctype)
92 return;
94 if (!get_implied_value(expr, &sval) || sval.value != 0)
95 return;
97 stmt = get_if_statement(expr);
98 if (!stmt)
99 return;
100 if (condition_matches(stmt->if_conditional, expr))
101 return;
103 call = get_orig_call(expr);
104 if (!call)
105 return;
107 if (call->pos.line >= stmt->pos.line)
108 return;
110 if (call->pos.line + 5 >= expr->pos.line)
111 return;
113 name = expr_to_str(expr);
114 sm_warning("missing error code? '%s'", name);
115 free_string(name);
118 void check_missing_error_code2(int id)
120 my_id = id;
122 add_hook(&match_return, RETURN_HOOK);