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
, "++")))
139 prepend(buf
, ")", len
);
140 __get_variable_from_expr(NULL
, buf
, expr
->right
, len
,
142 tmp
= show_special(expr
->op
);
143 prepend(buf
, tmp
, len
);
144 __get_variable_from_expr(sym_ptr
, buf
, expr
->left
,
146 prepend(buf
, "(", len
);
152 snprintf(tmp
, 25, "%lld", expr
->value
);
153 prepend(buf
, tmp
, len
);
157 prepend(buf
, expr
->string
->data
, len
);
160 struct expression
*tmp
;
164 prepend(buf
, ")", len
);
165 FOR_EACH_PTR_REVERSE(expr
->args
, tmp
) {
167 prepend(buf
, ", ", len
);
168 __get_variable_from_expr(NULL
, buf
, tmp
, len
,
170 } END_FOR_EACH_PTR_REVERSE(tmp
);
171 prepend(buf
, "(", len
);
172 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
,
177 __get_variable_from_expr(sym_ptr
, buf
,
178 expr
->cast_expression
, len
,
185 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
186 size
= (get_base_type(expr
->cast_type
))->bit_size
;
187 snprintf(tmp
, 25, "%d", size
);
188 prepend(buf
, tmp
, len
);
193 //printf("unknown type = %d\n", expr->type);
200 * This is returns a stylized "c looking" representation of the
203 * It uses the same buffer every time so you have to save the result
204 * yourself if you want to keep it.
208 char *get_variable_from_expr(struct expression
*expr
, struct symbol
**sym_ptr
)
210 static char var_name
[VAR_LEN
];
220 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
227 * get_variable_from_expr_simple() only returns simple variables.
228 * If it's a complicated variable like a->foo instead of just 'a'
229 * then it returns NULL.
232 char *get_variable_from_expr_simple(struct expression
*expr
,
233 struct symbol
**sym_ptr
)
235 static char var_name
[VAR_LEN
];
245 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),