Add a new check_locking script that checks spinlocks.
[smatch.git] / smatch_implied.c
blob819d6a754962f36f3eb44f807ad208c38f782fe3
1 /*
2 * sparse/smatch_implied.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * Imagine we have this code:
12 * foo = 0;
13 * if (bar)
14 * foo = 1;
15 * // <-- point #1
16 * else
17 * frob();
18 * // <-- point #2
19 * if (foo == 1) // <-- point #3
20 * bar->baz; // <-- point #4
22 * Currently (Oct 2008) in smatch when we merge bar states
23 * null and nonnull, at point #2, the state becomes undefined.
24 * As a result we get an error at point #3.
26 * The idea behind "implied state pools" is to fix that.
28 * The implied pools get created in merge_slist(). Whatever
29 * is unique to one slist being merged gets put into a pool.
31 * If we set a state that removes it from all pools.
33 * When we come to an if statement where "foo" has some pools
34 * associated we take all the pools where "foo == 1" and keep
35 * all the states that are consistent across those pools.
37 * The point of doing this is to turn an undefined state into
38 * a defined state. This hopefully gets rid of some false positives.
39 * What it doesn't do is find new errors that were
40 * missed before.
42 * There are quite a few implementation details I haven't figured
43 * out. How do you create implied state pools inside a
44 * complex condition? How do you determine what is implied
45 * from a complex condition? The initial patch is extremely rudimentary...
48 #include "smatch.h"
49 #include "smatch_slist.h"
52 * This condition hook is very connected to smatch_extra.c.
53 * It's registered there.
56 void __implied_states_hook(struct expression *expr)
58 struct symbol *sym;
59 char *name;
60 struct sm_state *state;
62 //printf("type = %d\n", expr->type);
64 if (expr->type != EXPR_COMPARE || expr->op != SPECIAL_EQUAL)
65 return;
67 name = get_variable_from_expr(expr->left, &sym);
68 state = __get_sm_state(name, SMATCH_EXTRA, sym);
69 free_string(name);