extra: fix a bug in how pointers are set inside called functions
[smatch.git] / check_macro_side_effects.c
blob570515bee8aed68f0c1cbb83472d4851bd6fa2a9
1 /*
2 * smatch/check_macro_side_effects.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "scope.h"
11 #include "smatch.h"
12 #include "smatch_slist.h"
13 #include "smatch_expression_stacks.h"
15 static int my_id;
17 static struct string_list *ignored_macros;
18 static struct position old_pos;
20 static struct smatch_state *alloc_my_state(struct expression *expr)
22 struct smatch_state *state;
23 char *name;
25 state = __alloc_smatch_state(0);
26 expr = strip_expr(expr);
27 name = get_variable_from_expr_complex(expr, NULL);
28 state->name = alloc_sname(name);
29 free_string(name);
30 state->data = expr;
31 return state;
34 static int defined_inside_macro(struct position macro_pos, struct expression *expr)
36 char *name;
37 struct symbol *sym;
38 int ret = 0;
40 name = get_variable_from_expr(expr, &sym);
41 if (!name || !sym)
42 goto free;
43 if (!sym->scope || !sym->scope->token)
44 goto free;
45 if (positions_eq(macro_pos, sym->scope->token->pos))
46 ret = 1;
47 free:
48 free_string(name);
49 return ret;
52 static int affected_inside_macro_before(struct expression *expr)
54 struct sm_state *sm;
55 struct sm_state *tmp;
56 struct expression *old_mod;
58 sm = get_sm_state_expr(my_id, expr);
59 if (!sm)
60 return 0;
62 FOR_EACH_PTR(sm->possible, tmp) {
63 old_mod = tmp->state->data;
64 if (!old_mod)
65 continue;
66 if (positions_eq(old_mod->pos, expr->pos))
67 return 1;
68 } END_FOR_EACH_PTR(tmp);
69 return 0;
72 static int is_ignored_macro(const char *macro)
74 char *tmp;
76 FOR_EACH_PTR(ignored_macros, tmp) {
77 if (!strcmp(tmp, macro))
78 return 1;
79 } END_FOR_EACH_PTR(tmp);
80 return 0;
83 static void match_unop(struct expression *raw_expr)
85 struct expression *expr;
86 char *macro, *name;
88 if (raw_expr->op != SPECIAL_INCREMENT && raw_expr->op != SPECIAL_DECREMENT)
89 return;
91 macro = get_macro_name(raw_expr->pos);
92 if (!macro)
93 return;
95 expr = strip_expr(raw_expr->unop);
97 if (defined_inside_macro(expr->pos, expr))
98 return;
100 if (is_ignored_macro(macro))
101 return;
103 if (!affected_inside_macro_before(expr)) {
104 set_state_expr(my_id, expr, alloc_my_state(expr));
105 old_pos = expr->pos;
106 return;
109 if (!positions_eq(old_pos, expr->pos))
110 return;
112 name = get_variable_from_expr_complex(raw_expr, NULL);
113 sm_msg("warn: side effect in macro '%s' doing '%s'",
114 macro, name);
115 free_string(name);
118 static void match_stmt(struct statement *stmt)
120 if (!positions_eq(old_pos, stmt->pos))
121 old_pos.line = 0;
124 static void register_ignored_macros(void)
126 struct token *token;
127 char *macro;
128 char name[256];
130 snprintf(name, 256, "%s.ignore_side_effects", option_project_str);
132 token = get_tokens_file(name);
133 if (!token)
134 return;
135 if (token_type(token) != TOKEN_STREAMBEGIN)
136 return;
137 token = token->next;
138 while (token_type(token) != TOKEN_STREAMEND) {
139 if (token_type(token) != TOKEN_IDENT)
140 return;
141 macro = alloc_string(show_ident(token->ident));
142 add_ptr_list(&ignored_macros, macro);
143 token = token->next;
145 clear_token_alloc();
148 void check_macro_side_effects(int id)
150 my_id = id;
152 if (!option_spammy)
153 return;
155 add_hook(&match_unop, OP_HOOK);
156 add_hook(&match_stmt, STMT_HOOK);
157 register_ignored_macros();