flow: handle struct initialization better
[smatch.git] / check_return_negative_var.c
blob8fcc149d62e27139a05c659a6760f96cad4cc9cb
1 /*
2 * smatch/check_return.c
4 * Copyright (C) 2011 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
11 #include "smatch_slist.h"
13 static int my_id;
14 extern int check_assigned_expr_id;
16 static struct expression *get_assigned_expr(struct expression *expr)
18 struct smatch_state *state;
20 state = get_state_expr(check_assigned_expr_id, expr);
21 if (!state)
22 return NULL;
23 /* fixme: handle merged states as well */
24 return (struct expression *)state->data;
27 static void match_return(struct expression *ret_value)
29 struct expression *expr;
30 char *macro;
32 if (!ret_value)
33 return;
34 expr = ret_value;
35 if (ret_value->type != EXPR_PREOP || ret_value->op != '-')
36 return;
38 macro = get_macro_name(expr->unop->pos);
39 if (macro && !strcmp(macro, "PTR_ERR")) {
40 sm_msg("warn: returning -%s()", macro);
41 return;
44 if (!option_spammy)
45 return;
47 expr = get_assigned_expr(ret_value->unop);
48 if (!expr)
49 return;
50 if (expr->type != EXPR_CALL)
51 return;
53 sm_msg("warn: should this return really be negated?");
56 void check_return_negative_var(int id)
58 if (option_project != PROJ_KERNEL)
59 return;
61 my_id = id;
62 add_hook(&match_return, RETURN_HOOK);