unssa: update comment about the unneeded copies
[smatch.git] / char.c
blobe3f6a00698a81d66dc3980bf807eb70606e49ed1
1 #include <string.h>
2 #include "target.h"
3 #include "lib.h"
4 #include "allocate.h"
5 #include "token.h"
6 #include "expression.h"
7 #include "char.h"
9 static const char *parse_escape(const char *p, unsigned *val, const char *end, int bits, struct position pos)
11 unsigned c = *p++;
12 unsigned d;
13 if (c != '\\') {
14 *val = c;
15 return p;
18 c = *p++;
19 switch (c) {
20 case 'a': c = '\a'; break;
21 case 'b': c = '\b'; break;
22 case 't': c = '\t'; break;
23 case 'n': c = '\n'; break;
24 case 'v': c = '\v'; break;
25 case 'f': c = '\f'; break;
26 case 'r': c = '\r'; break;
27 case 'e': c = '\e'; break;
28 case 'x': {
29 unsigned mask = -(1U << (bits - 4));
30 for (c = 0; p < end; c = (c << 4) + d) {
31 d = hexval(*p);
32 if (d > 16)
33 break;
34 p++;
35 if (c & mask) {
36 warning(pos,
37 "hex escape sequence out of range");
38 mask = 0;
41 break;
43 case '0'...'7': {
44 if (p + 2 < end)
45 end = p + 2;
46 c -= '0';
47 while (p < end && (d = *p - '0') < 8) {
48 c = (c << 3) + d;
49 p++;
51 if ((c & 0400) && bits < 9)
52 warning(pos,
53 "octal escape sequence out of range");
54 break;
56 default: /* everything else is left as is */
57 break;
59 *val = c & ~((~0U << (bits - 1)) << 1);
60 return p;
63 void get_char_constant(struct token *token, unsigned long long *val)
65 const char *p = token->embedded, *end;
66 unsigned v;
67 int type = token_type(token);
68 switch (type) {
69 case TOKEN_CHAR:
70 case TOKEN_WIDE_CHAR:
71 p = token->string->data;
72 end = p + token->string->length - 1;
73 break;
74 case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
75 end = p + type - TOKEN_CHAR;
76 break;
77 default:
78 end = p + type - TOKEN_WIDE_CHAR;
80 p = parse_escape(p, &v, end,
81 type < TOKEN_WIDE_CHAR ? bits_in_char : 32, token->pos);
82 if (p != end)
83 warning(token->pos,
84 "multi-character character constant");
85 *val = v;
88 struct token *get_string_constant(struct token *token, struct expression *expr)
90 struct string *string = token->string;
91 struct token *next = token->next, *done = NULL;
92 int stringtype = token_type(token);
93 int is_wide = stringtype == TOKEN_WIDE_STRING;
94 static char buffer[MAX_STRING];
95 int len = 0;
96 int bits;
97 int esc_count = 0;
99 while (!done) {
100 switch (token_type(next)) {
101 case TOKEN_WIDE_STRING:
102 is_wide = 1;
103 case TOKEN_STRING:
104 next = next->next;
105 break;
106 default:
107 done = next;
110 bits = is_wide ? 32 : bits_in_char;
111 while (token != done) {
112 unsigned v;
113 const char *p = token->string->data;
114 const char *end = p + token->string->length - 1;
115 while (p < end) {
116 if (*p == '\\')
117 esc_count++;
118 p = parse_escape(p, &v, end, bits, token->pos);
119 if (len < MAX_STRING)
120 buffer[len] = v;
121 len++;
123 token = token->next;
125 if (len > MAX_STRING) {
126 warning(token->pos, "trying to concatenate %d-character string (%d bytes max)", len, MAX_STRING);
127 len = MAX_STRING;
130 if (esc_count || len >= string->length) {
131 if (string->immutable || len >= string->length) /* can't cannibalize */
132 string = __alloc_string(len+1);
133 string->length = len+1;
134 memcpy(string->data, buffer, len);
135 string->data[len] = '\0';
137 expr->string = string;
138 expr->wide = is_wide;
139 return token;