strlen: add a comment and rename some things
[smatch.git] / smatch_expressions.c
blob275c380d8dcc462bfa6933bffd91cb9495208d48
1 #include "smatch.h"
3 static struct position get_cur_pos(void)
5 static struct position pos;
6 static struct position none;
7 struct expression *expr;
8 struct statement *stmt;
10 expr = last_ptr_list((struct ptr_list *)big_expression_stack);
11 stmt = last_ptr_list((struct ptr_list *)big_statement_stack);
12 if (expr)
13 pos = expr->pos;
14 else if (stmt)
15 pos = stmt->pos;
16 else
17 pos = none;
18 return pos;
21 struct expression *zero_expr()
23 static struct expression *zero;
25 if (zero)
26 return zero;
28 zero = alloc_expression(get_cur_pos(), EXPR_VALUE);
29 zero->value = 0;
30 zero->ctype = &char_ctype;
31 return zero;
34 struct expression *value_expr(long long val)
36 struct expression *expr;
38 if (!val)
39 return zero_expr();
41 expr = alloc_expression(get_cur_pos(), EXPR_VALUE);
42 expr->value = val;
43 expr->ctype = &llong_ctype;
44 return expr;
47 struct expression *member_expression(struct expression *deref, int op, struct ident *member)
49 struct expression *expr;
51 expr = alloc_expression(deref->pos, EXPR_DEREF);
52 expr->op = op;
53 expr->deref = deref;
54 expr->member = member;
55 return expr;
58 struct expression *deref_expression(struct expression *expr)
60 struct expression *preop;
62 preop = alloc_expression(expr->pos, EXPR_PREOP);
63 preop->unop = expr;
64 preop->op = '*';
65 return preop;
68 struct expression *assign_expression(struct expression *left, struct expression *right)
70 struct expression *expr;
72 expr = alloc_expression(right->pos, EXPR_ASSIGNMENT);
73 expr->op = '=';
74 expr->left = left;
75 expr->right = right;
76 return expr;
79 struct expression *binop_expression(struct expression *left, int op, struct expression *right)
81 struct expression *expr;
83 expr = alloc_expression(right->pos, EXPR_BINOP);
84 expr->op = op;
85 expr->left = left;
86 expr->right = right;
87 return expr;
90 struct expression *array_element_expression(struct expression *array, struct expression *offset)
92 struct expression *expr;
94 expr = binop_expression(array, '+', offset);
95 return deref_expression(expr);
98 struct expression *symbol_expression(struct symbol *sym)
100 struct expression *expr;
102 expr = alloc_expression(sym->pos, EXPR_SYMBOL);
103 expr->symbol = sym;
104 expr->symbol_name = sym->ident;
105 return expr;
108 #define FAKE_NAME "smatch_fake"
109 struct ident unknown_value = {
110 .len = sizeof(FAKE_NAME),
111 .name = FAKE_NAME,
114 static int fake_counter;
115 static struct ident *fake_ident()
117 struct ident *ret;
118 char buf[32];
119 int len;
121 snprintf(buf, sizeof(buf), "smatch_fake_%d", fake_counter++);
122 len = strlen(buf) + 1;
123 ret = malloc(sizeof(*ret) + len);
124 memset(ret, 0, sizeof(*ret));
125 memcpy(ret->name, buf, len);
126 ret->len = len;
128 return ret;
131 struct expression *unknown_value_expression(struct expression *expr)
133 struct expression *ret;
134 struct symbol *type;
136 type = get_type(expr);
137 if (!type || type->type != SYM_BASETYPE)
138 type = &llong_ctype;
140 ret = alloc_expression(expr->pos, EXPR_SYMBOL);
141 ret->symbol = alloc_symbol(expr->pos, SYM_BASETYPE);
142 ret->symbol->ctype.base_type = type;
143 ret->symbol_name = fake_ident();
145 return ret;