2008-05-28 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / scan.c
blobf697d26ec028d9c71a294153eb7670f51fbf699d
1 /* Utility functions for scan-decls and fix-header programs.
2 Copyright (C) 1993, 1994, 1998, 2002, 2003, 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
18 #include "bconfig.h"
19 #include "system.h"
20 #include "coretypes.h"
21 #include "tm.h"
22 #include "scan.h"
24 int lineno = 1;
25 int source_lineno = 1;
26 sstring source_filename;
28 void
29 make_sstring_space (sstring *str, int count)
31 int cur_pos = str->ptr - str->base;
32 int cur_size = str->limit - str->base;
33 int new_size = cur_pos + count + 100;
35 if (new_size <= cur_size)
36 return;
38 str->base = xrealloc (str->base, new_size);
39 str->ptr = str->base + cur_size;
40 str->limit = str->base + new_size;
43 void
44 sstring_append (sstring *dst, sstring *src)
46 char *d, *s;
47 int count = SSTRING_LENGTH (src);
49 MAKE_SSTRING_SPACE (dst, count + 1);
50 d = dst->ptr;
51 s = src->base;
52 while (--count >= 0) *d++ = *s++;
53 dst->ptr = d;
54 *d = 0;
57 int
58 scan_ident (FILE *fp, sstring *s, int c)
60 s->ptr = s->base;
61 if (ISIDST (c))
63 for (;;)
65 SSTRING_PUT (s, c);
66 c = getc (fp);
67 if (c == EOF || ! ISIDNUM (c))
68 break;
71 MAKE_SSTRING_SPACE (s, 1);
72 *s->ptr = 0;
73 return c;
76 int
77 scan_string (FILE *fp, sstring *s, int init)
79 int c;
81 for (;;)
83 c = getc (fp);
84 if (c == EOF || c == '\n')
85 break;
86 if (c == init)
88 c = getc (fp);
89 break;
91 if (c == '\\')
93 c = getc (fp);
94 if (c == EOF)
95 break;
96 if (c == '\n')
97 continue;
99 SSTRING_PUT (s, c);
101 MAKE_SSTRING_SPACE (s, 1);
102 *s->ptr = 0;
103 return c;
106 /* Skip horizontal white spaces (spaces, tabs, and C-style comments). */
109 skip_spaces (FILE *fp, int c)
111 for (;;)
113 if (c == ' ' || c == '\t')
114 c = getc (fp);
115 else if (c == '/')
117 c = getc (fp);
118 if (c != '*')
120 ungetc (c, fp);
121 return '/';
123 c = getc (fp);
124 for (;;)
126 if (c == EOF)
127 return EOF;
128 else if (c != '*')
130 if (c == '\n')
131 source_lineno++, lineno++;
132 c = getc (fp);
134 else if ((c = getc (fp)) == '/')
135 return getc (fp);
138 else
139 break;
141 return c;
145 read_upto (FILE *fp, sstring *str, int delim)
147 int ch;
149 for (;;)
151 ch = getc (fp);
152 if (ch == EOF || ch == delim)
153 break;
154 SSTRING_PUT (str, ch);
156 MAKE_SSTRING_SPACE (str, 1);
157 *str->ptr = 0;
158 return ch;
162 get_token (FILE *fp, sstring *s)
164 int c;
166 s->ptr = s->base;
167 retry:
168 c = ' ';
169 c = skip_spaces (fp, c);
170 if (c == '\n')
172 source_lineno++;
173 lineno++;
174 goto retry;
176 if (c == '#')
178 c = get_token (fp, s);
179 if (c == INT_TOKEN)
181 source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
182 get_token (fp, &source_filename);
184 for (;;)
186 c = getc (fp);
187 if (c == EOF)
188 return EOF;
189 if (c == '\n')
191 source_lineno++;
192 lineno++;
193 goto retry;
197 if (c == EOF)
198 return EOF;
199 if (ISDIGIT (c))
203 SSTRING_PUT (s, c);
204 c = getc (fp);
205 } while (c != EOF && ISDIGIT (c));
206 ungetc (c, fp);
207 c = INT_TOKEN;
208 goto done;
210 if (ISIDST (c))
212 c = scan_ident (fp, s, c);
213 ungetc (c, fp);
214 return IDENTIFIER_TOKEN;
216 if (c == '\'' || c == '"')
218 c = scan_string (fp, s, c);
219 ungetc (c, fp);
220 return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
222 SSTRING_PUT (s, c);
223 done:
224 MAKE_SSTRING_SPACE (s, 1);
225 *s->ptr = 0;
226 return c;
229 unsigned int
230 hashstr (const char *str, unsigned int len)
232 unsigned int n = len;
233 unsigned int r = 0;
234 const unsigned char *s = (const unsigned char *) str;
237 r = r * 67 + (*s++ - 113);
238 while (--n);
239 return r + len;