buf_size: add a NULL check
[smatch.git] / check_assigned_expr.c
blobac6f747f860358e882b36055c513c9dff8cb3645
1 /*
2 * sparse/check_assigned_expr.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is not a check. It just saves an struct expression pointer
12 * whenever something is assigned. This can be used later on by other scripts.
15 #include "smatch.h"
16 #include "smatch_slist.h"
18 int check_assigned_expr_id;
19 static int my_id;
21 struct expression *get_assigned_expr(struct expression *expr)
23 struct smatch_state *state;
25 state = get_state_expr(my_id, expr);
26 if (!state)
27 return NULL;
28 return (struct expression *)state->data;
31 static struct smatch_state *alloc_my_state(struct expression *expr)
33 struct smatch_state *state;
34 char *name;
36 state = __alloc_smatch_state(0);
37 expr = strip_expr(expr);
38 name = expr_to_str(expr);
39 state->name = alloc_sname(name);
40 free_string(name);
41 state->data = expr;
42 return state;
45 static void match_assignment(struct expression *expr)
47 if (expr->op != '=')
48 return;
49 set_state_expr(my_id, expr->left, alloc_my_state(expr->right));
52 void check_assigned_expr(int id)
54 my_id = check_assigned_expr_id = id;
55 add_hook(&match_assignment, ASSIGNMENT_HOOK);