2 * sparse/smatch_helper.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
15 static int star_count
;
19 char *alloc_string(char *str
)
25 tmp
= malloc(strlen(str
) + 1);
30 void free_string(char *str
)
35 static void prepend(char *dest
, const char *data
, int buff_len
)
40 space_needed
= strlen(data
);
41 for (i
= buff_len
- space_needed
- 1; i
>= 0 ; i
--)
42 dest
[i
+ space_needed
] = dest
[i
];
43 for (i
= 0; i
< space_needed
% buff_len
; i
++)
45 dest
[buff_len
- 1] = '\0';
49 * If you have "foo(a, b, 1);" then use
50 * get_argument_from_call_expr(expr, 0) to return the expression for
51 * a. Yes, it does start counting from 0.
53 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
56 struct expression
*expr
;
62 FOR_EACH_PTR(args
, expr
) {
66 } END_FOR_EACH_PTR(expr
);
70 static void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
71 struct expression
*expr
, int len
,
76 prepend(buf
, expr
->member
->name
, len
);
77 if (!strcmp(show_special(expr
->deref
->op
), "*"))
78 prepend(buf
, "->", len
);
80 prepend(buf
, ".", len
);
82 //printf("debug: %d\n", expr->deref
84 __get_variable_from_expr(sym_ptr
, buf
, expr
->deref
,
88 if (expr
->symbol_name
)
89 prepend(buf
, expr
->symbol_name
->name
, len
);
93 *sym_ptr
= expr
->symbol
;
102 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
104 tmp
= show_special(expr
->op
);
106 if (star_count
-- >= 0) {
107 prepend(buf
, tmp
, len
);
110 prepend(buf
, tmp
, len
);
114 strncat(buf
, ")", len
);
118 if ((!strcmp(tmp
, "--")) || (!strcmp(tmp
, "++")))
126 tmp
= show_special(expr
->op
);
127 prepend(buf
, tmp
, len
);
128 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
131 if ((!strcmp(tmp
, "--")) || (!strcmp(tmp
, "++")))
140 prepend(buf
, ")", len
);
141 __get_variable_from_expr(NULL
, buf
, expr
->right
, len
,
143 tmp
= show_special(expr
->op
);
144 prepend(buf
, tmp
, len
);
145 __get_variable_from_expr(sym_ptr
, buf
, expr
->left
,
147 prepend(buf
, "(", len
);
153 snprintf(tmp
, 25, "%lld", expr
->value
);
154 prepend(buf
, tmp
, len
);
158 prepend(buf
, expr
->string
->data
, len
);
161 struct expression
*tmp
;
165 prepend(buf
, ")", len
);
166 FOR_EACH_PTR_REVERSE(expr
->args
, tmp
) {
168 prepend(buf
, ", ", len
);
169 __get_variable_from_expr(NULL
, buf
, tmp
, len
,
171 } END_FOR_EACH_PTR_REVERSE(tmp
);
172 prepend(buf
, "(", len
);
173 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
,
178 __get_variable_from_expr(sym_ptr
, buf
,
179 expr
->cast_expression
, len
,
186 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
187 size
= (get_base_type(expr
->cast_type
))->bit_size
;
188 snprintf(tmp
, 25, "%d", size
);
189 prepend(buf
, tmp
, len
);
195 //printf("unknown type = %d\n", expr->type);
202 * This is returns a stylized "c looking" representation of the
205 * It uses the same buffer every time so you have to save the result
206 * yourself if you want to keep it.
210 char *get_variable_from_expr_complex(struct expression
*expr
, struct symbol
**sym_ptr
)
212 static char var_name
[VAR_LEN
];
222 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
229 * get_variable_from_expr_simple() only returns simple variables.
230 * If it's a complicated variable like a->foo instead of just 'a'
231 * then it returns NULL.
234 char *get_variable_from_expr(struct expression
*expr
,
235 struct symbol
**sym_ptr
)
237 static char var_name
[VAR_LEN
];
247 expr
= strip_expr(expr
);
248 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
256 return alloc_string(var_name
);
259 int sym_name_is(const char *name
, struct expression
*expr
)
263 if (expr
->type
!= EXPR_SYMBOL
)
265 if (!strcmp(expr
->symbol_name
->name
, name
))
270 static int _get_value(struct expression
*expr
, int *discard
)
287 if (!strcmp("-", show_special(expr
->op
)))
288 ret
= - _get_value(expr
->unop
, discard
);
291 if (show_special(expr
->op
) &&
292 !strcmp("*", show_special(expr
->op
)))
293 ret
= _get_value(expr
->left
, discard
)
294 * _get_value(expr
->right
, discard
);
297 if (expr
->cast_type
&& get_base_type(expr
->cast_type
))
298 ret
= (get_base_type(expr
->cast_type
))->bit_size
/ 8;
299 if (expr
->cast_expression
)
300 ;//printf("debugs %d\n", expr->cast_expression->type);
303 //printf("ouchies-->%d\n", expr->type);
311 int get_value(struct expression
*expr
)
313 return _get_value(expr
, NULL
);
316 int is_zero(struct expression
*expr
)
318 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 0)
321 return is_zero(expr
->unop
);
322 if (expr
->type
== EXPR_CAST
)
323 return is_zero(expr
->cast_expression
);
327 const char *show_state(struct smatch_state
*state
)
334 struct expression
*strip_expr(struct expression
*expr
)
339 return strip_expr(expr
->cast_expression
);
342 return strip_expr(expr
->unop
);