]>
Commit | Line | Data |
---|---|---|
7f2935c7 | 1 | /* CPP Library. |
d8bfa78c | 2 | Copyright (C) 1986, 87, 89, 92-6, 1997 Free Software Foundation, Inc. |
4c8cc616 | 3 | Contributed by Per Bothner, 1994-95. |
d8bfa78c | 4 | Based on CCCP program by Paul Rubin, June 1986 |
7f2935c7 PB |
5 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
6 | ||
7 | This program is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by the | |
9 | Free Software Foundation; either version 2, or (at your option) any | |
10 | later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
940d9d63 | 19 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
7f2935c7 PB |
20 | |
21 | In other words, you are welcome to use, share and improve this program. | |
22 | You are forbidden to forbid anyone else to use, share and improve | |
23 | what you give them. Help stamp out software-hoarding! */ | |
24 | ||
25 | #ifdef EMACS | |
26 | #define NO_SHORTNAMES | |
27 | #include "../src/config.h" | |
28 | #ifdef open | |
29 | #undef open | |
30 | #undef read | |
31 | #undef write | |
32 | #endif /* open */ | |
33 | #endif /* EMACS */ | |
34 | ||
35 | /* The macro EMACS is defined when cpp is distributed as part of Emacs, | |
36 | for the sake of machines with limited C compilers. */ | |
37 | #ifndef EMACS | |
38 | #include "config.h" | |
39 | #endif /* not EMACS */ | |
40 | ||
41 | #ifndef STANDARD_INCLUDE_DIR | |
42 | #define STANDARD_INCLUDE_DIR "/usr/include" | |
43 | #endif | |
44 | ||
7f2935c7 PB |
45 | #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */ |
46 | #ifdef __STDC__ | |
47 | #define PTR_INT_TYPE ptrdiff_t | |
48 | #else | |
49 | #define PTR_INT_TYPE long | |
50 | #endif | |
51 | #endif /* 0 */ | |
52 | ||
53 | #include "cpplib.h" | |
54 | #include "cpphash.h" | |
55 | ||
56 | #ifndef STDC_VALUE | |
57 | #define STDC_VALUE 1 | |
58 | #endif | |
59 | ||
60 | /* By default, colon separates directories in a path. */ | |
61 | #ifndef PATH_SEPARATOR | |
62 | #define PATH_SEPARATOR ':' | |
63 | #endif | |
64 | ||
7f2935c7 PB |
65 | #include <ctype.h> |
66 | #include <stdio.h> | |
67 | #include <signal.h> | |
68 | #ifdef __STDC__ | |
7f2935c7 PB |
69 | #include <stdlib.h> |
70 | #endif | |
71 | ||
72 | #ifndef VMS | |
73 | #ifndef USG | |
74 | #include <sys/time.h> /* for __DATE__ and __TIME__ */ | |
75 | #include <sys/resource.h> | |
76 | #else | |
7f2935c7 PB |
77 | #include <sys/times.h> |
78 | #include <time.h> | |
79 | #include <fcntl.h> | |
80 | #endif /* USG */ | |
81 | #endif /* not VMS */ | |
82 | ||
e9a25f70 JL |
83 | #if HAVE_LIMITS_H |
84 | # include <limits.h> | |
85 | #endif | |
86 | ||
0f41302f | 87 | /* This defines "errno" properly for VMS, and gives us EACCES. */ |
7f2935c7 PB |
88 | #include <errno.h> |
89 | ||
90 | extern char *index (); | |
91 | extern char *rindex (); | |
e9a25f70 | 92 | extern char *update_path (); |
7f2935c7 PB |
93 | |
94 | #ifndef O_RDONLY | |
95 | #define O_RDONLY 0 | |
96 | #endif | |
97 | ||
98 | #undef MIN | |
99 | #undef MAX | |
100 | #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) | |
101 | #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) | |
102 | ||
e9a25f70 JL |
103 | /* Find the largest host integer type and set its size and type. |
104 | Watch out: on some crazy hosts `long' is shorter than `int'. */ | |
105 | ||
106 | #ifndef HOST_WIDE_INT | |
107 | # if HAVE_INTTYPES_H | |
108 | # include <inttypes.h> | |
109 | # define HOST_WIDE_INT intmax_t | |
110 | # else | |
111 | # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \ | |
112 | && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT) | |
113 | # define HOST_WIDE_INT int | |
114 | # else | |
115 | # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \ | |
116 | || ! (defined LONG_LONG_MAX || defined LLONG_MAX)) | |
117 | # define HOST_WIDE_INT long | |
118 | # else | |
119 | # define HOST_WIDE_INT long long | |
120 | # endif | |
121 | # endif | |
122 | # endif | |
7f2935c7 PB |
123 | #endif |
124 | ||
125 | #ifndef S_ISREG | |
126 | #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) | |
127 | #endif | |
128 | ||
129 | #ifndef S_ISDIR | |
130 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) | |
131 | #endif | |
132 | ||
133 | /* Define a generic NULL if one hasn't already been defined. */ | |
134 | ||
135 | #ifndef NULL | |
136 | #define NULL 0 | |
137 | #endif | |
138 | ||
139 | #ifndef GENERIC_PTR | |
140 | #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__) | |
141 | #define GENERIC_PTR void * | |
142 | #else | |
143 | #define GENERIC_PTR char * | |
144 | #endif | |
145 | #endif | |
146 | ||
147 | #ifndef NULL_PTR | |
0f41302f | 148 | #define NULL_PTR ((GENERIC_PTR) 0) |
7f2935c7 PB |
149 | #endif |
150 | ||
151 | #ifndef INCLUDE_LEN_FUDGE | |
152 | #define INCLUDE_LEN_FUDGE 0 | |
153 | #endif | |
154 | ||
155 | /* Symbols to predefine. */ | |
156 | ||
157 | #ifdef CPP_PREDEFINES | |
158 | static char *predefs = CPP_PREDEFINES; | |
159 | #else | |
160 | static char *predefs = ""; | |
161 | #endif | |
162 | \f | |
163 | /* We let tm.h override the types used here, to handle trivial differences | |
164 | such as the choice of unsigned int or long unsigned int for size_t. | |
165 | When machines start needing nontrivial differences in the size type, | |
166 | it would be best to do something here to figure out automatically | |
167 | from other information what type to use. */ | |
168 | ||
169 | /* The string value for __SIZE_TYPE__. */ | |
170 | ||
171 | #ifndef SIZE_TYPE | |
172 | #define SIZE_TYPE "long unsigned int" | |
173 | #endif | |
174 | ||
175 | /* The string value for __PTRDIFF_TYPE__. */ | |
176 | ||
177 | #ifndef PTRDIFF_TYPE | |
178 | #define PTRDIFF_TYPE "long int" | |
179 | #endif | |
180 | ||
181 | /* The string value for __WCHAR_TYPE__. */ | |
182 | ||
183 | #ifndef WCHAR_TYPE | |
184 | #define WCHAR_TYPE "int" | |
185 | #endif | |
186 | #define CPP_WCHAR_TYPE(PFILE) \ | |
187 | (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE) | |
188 | ||
189 | /* The string value for __USER_LABEL_PREFIX__ */ | |
190 | ||
191 | #ifndef USER_LABEL_PREFIX | |
192 | #define USER_LABEL_PREFIX "" | |
193 | #endif | |
194 | ||
195 | /* The string value for __REGISTER_PREFIX__ */ | |
196 | ||
197 | #ifndef REGISTER_PREFIX | |
198 | #define REGISTER_PREFIX "" | |
199 | #endif | |
200 | \f | |
201 | /* In the definition of a #assert name, this structure forms | |
202 | a list of the individual values asserted. | |
203 | Each value is itself a list of "tokens". | |
204 | These are strings that are compared by name. */ | |
205 | ||
206 | struct tokenlist_list { | |
207 | struct tokenlist_list *next; | |
208 | struct arglist *tokens; | |
209 | }; | |
210 | ||
211 | struct assertion_hashnode { | |
212 | struct assertion_hashnode *next; /* double links for easy deletion */ | |
213 | struct assertion_hashnode *prev; | |
214 | /* also, a back pointer to this node's hash | |
215 | chain is kept, in case the node is the head | |
0f41302f | 216 | of the chain and gets deleted. */ |
7f2935c7 PB |
217 | struct assertion_hashnode **bucket_hdr; |
218 | int length; /* length of token, for quick comparison */ | |
219 | U_CHAR *name; /* the actual name */ | |
220 | /* List of token-sequences. */ | |
221 | struct tokenlist_list *value; | |
222 | }; | |
223 | \f | |
224 | #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0) | |
225 | #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0) | |
226 | ||
227 | #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF) | |
228 | #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N)) | |
229 | #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile)) | |
230 | #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile)) | |
231 | /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion. | |
232 | (Note that it is false while we're expanding marco *arguments*.) */ | |
233 | #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup) | |
234 | ||
235 | /* Move all backslash-newline pairs out of embarrassing places. | |
236 | Exchange all such pairs following BP | |
237 | with any potentially-embarrassing characters that follow them. | |
238 | Potentially-embarrassing characters are / and * | |
239 | (because a backslash-newline inside a comment delimiter | |
240 | would cause it not to be recognized). */ | |
241 | ||
242 | #define NEWLINE_FIX \ | |
243 | do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0) | |
244 | ||
0f41302f | 245 | /* Same, but assume we've already read the potential '\\' into C. */ |
7f2935c7 PB |
246 | #define NEWLINE_FIX1(C) do { \ |
247 | while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\ | |
248 | } while(0) | |
249 | ||
7f2935c7 PB |
250 | struct cpp_pending { |
251 | struct cpp_pending *next; | |
252 | char *cmd; | |
253 | char *arg; | |
254 | }; | |
255 | ||
256 | /* Forward declarations. */ | |
257 | ||
e9a25f70 JL |
258 | char *xmalloc (); |
259 | void cpp_fatal (); | |
260 | void cpp_file_line_for_message PARAMS ((cpp_reader *, char *, int, int)); | |
261 | void cpp_hash_cleanup PARAMS ((cpp_reader *)); | |
262 | void cpp_message (); | |
263 | void cpp_print_containing_files PARAMS ((cpp_reader *)); | |
7f2935c7 PB |
264 | |
265 | static void add_import (); | |
266 | static void append_include_chain (); | |
7f2935c7 PB |
267 | static void make_assertion (); |
268 | static void path_include (); | |
269 | static void initialize_builtins (); | |
270 | static void initialize_char_syntax (); | |
7f2935c7 | 271 | extern void delete_macro (); |
e9a25f70 | 272 | #if 0 |
7f2935c7 | 273 | static void trigraph_pcp (); |
e9a25f70 | 274 | #endif |
7f2935c7 PB |
275 | static int finclude (); |
276 | static void validate_else (); | |
277 | static int comp_def_part (); | |
e2f79f3c | 278 | #ifdef abort |
7f2935c7 | 279 | extern void fancy_abort (); |
e2f79f3c | 280 | #endif |
7f2935c7 PB |
281 | static int lookup_import (); |
282 | static int redundant_include_p (); | |
283 | static is_system_include (); | |
284 | static struct file_name_map *read_name_map (); | |
285 | static char *read_filename_string (); | |
286 | static int open_include_file (); | |
7f2935c7 PB |
287 | static int check_macro_name (); |
288 | static int compare_defs (); | |
289 | static int compare_token_lists (); | |
6be492ab | 290 | static HOST_WIDE_INT eval_if_expression (); |
7f2935c7 | 291 | static int change_newlines (); |
7f2935c7 PB |
292 | extern int hashf (); |
293 | static int file_size_and_mode (); | |
294 | static struct arglist *read_token_list (); | |
295 | static void free_token_list (); | |
296 | static int safe_read (); | |
297 | static void push_macro_expansion PARAMS ((cpp_reader *, | |
0f41302f MS |
298 | U_CHAR *, int, HASHNODE *)); |
299 | static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *)); | |
7f2935c7 | 300 | extern char *xrealloc (); |
e2f79f3c | 301 | static char *xcalloc (); |
7f2935c7 PB |
302 | static char *savestring (); |
303 | ||
304 | static void conditional_skip (); | |
305 | static void skip_if_group (); | |
306 | ||
307 | /* Last arg to output_line_command. */ | |
308 | enum file_change_code {same_file, enter_file, leave_file}; | |
309 | ||
310 | /* External declarations. */ | |
311 | ||
0f41302f | 312 | extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *)); |
6be492ab | 313 | |
7f2935c7 PB |
314 | extern char *getenv (); |
315 | extern FILE *fdopen (); | |
316 | extern char *version_string; | |
317 | extern struct tm *localtime (); | |
318 | ||
319 | /* These functions are declared to return int instead of void since they | |
320 | are going to be placed in a table and some old compilers have trouble with | |
321 | pointers to functions returning void. */ | |
322 | ||
323 | static int do_define (); | |
324 | static int do_line (); | |
325 | static int do_include (); | |
326 | static int do_undef (); | |
327 | static int do_error (); | |
328 | static int do_pragma (); | |
329 | static int do_ident (); | |
330 | static int do_if (); | |
331 | static int do_xifdef (); | |
332 | static int do_else (); | |
333 | static int do_elif (); | |
334 | static int do_endif (); | |
335 | static int do_sccs (); | |
336 | static int do_once (); | |
337 | static int do_assert (); | |
338 | static int do_unassert (); | |
339 | static int do_warning (); | |
340 | \f | |
341 | struct file_name_list | |
342 | { | |
343 | struct file_name_list *next; | |
344 | char *fname; | |
345 | /* If the following is nonzero, it is a macro name. | |
346 | Don't include the file again if that macro is defined. */ | |
347 | U_CHAR *control_macro; | |
348 | /* If the following is nonzero, it is a C-language system include | |
349 | directory. */ | |
350 | int c_system_include_path; | |
351 | /* Mapping of file names for this directory. */ | |
352 | struct file_name_map *name_map; | |
353 | /* Non-zero if name_map is valid. */ | |
354 | int got_name_map; | |
355 | }; | |
356 | ||
6bac1e64 | 357 | /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found |
0f41302f MS |
358 | via the same directory as the file that #included it. */ |
359 | #define SELF_DIR_DUMMY ((struct file_name_list *) (~0)) | |
6bac1e64 | 360 | |
0f41302f MS |
361 | /* #include "file" looks in source file dir, then stack. */ |
362 | /* #include <file> just looks in the stack. */ | |
363 | /* -I directories are added to the end, then the defaults are added. */ | |
7f2935c7 PB |
364 | /* The */ |
365 | static struct default_include { | |
366 | char *fname; /* The name of the directory. */ | |
e9a25f70 | 367 | char *component; /* The component containing the directory */ |
7f2935c7 PB |
368 | int cplusplus; /* Only look here if we're compiling C++. */ |
369 | int cxx_aware; /* Includes in this directory don't need to | |
370 | be wrapped in extern "C" when compiling | |
371 | C++. */ | |
372 | } include_defaults_array[] | |
373 | #ifdef INCLUDE_DEFAULTS | |
374 | = INCLUDE_DEFAULTS; | |
375 | #else | |
376 | = { | |
377 | /* Pick up GNU C++ specific include files. */ | |
e9a25f70 JL |
378 | { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 }, |
379 | { OLD_GPLUSPLUS_INCLUDE_DIR, 0, 1, 1 }, | |
7f2935c7 PB |
380 | #ifdef CROSS_COMPILE |
381 | /* This is the dir for fixincludes. Put it just before | |
382 | the files that we fix. */ | |
e9a25f70 | 383 | { GCC_INCLUDE_DIR, "GCC", 0, 0 }, |
7f2935c7 PB |
384 | /* For cross-compilation, this dir name is generated |
385 | automatically in Makefile.in. */ | |
e9a25f70 | 386 | { CROSS_INCLUDE_DIR, "GCC",0, 0 }, |
55a7a95e | 387 | #ifdef TOOL_INCLUDE_DIR |
7f2935c7 | 388 | /* This is another place that the target system's headers might be. */ |
e9a25f70 | 389 | { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 }, |
55a7a95e | 390 | #endif |
7f2935c7 | 391 | #else /* not CROSS_COMPILE */ |
55a7a95e | 392 | #ifdef LOCAL_INCLUDE_DIR |
7f2935c7 PB |
393 | /* This should be /usr/local/include and should come before |
394 | the fixincludes-fixed header files. */ | |
e9a25f70 | 395 | { LOCAL_INCLUDE_DIR, 0, 0, 1 }, |
55a7a95e RK |
396 | #endif |
397 | #ifdef TOOL_INCLUDE_DIR | |
7f2935c7 PB |
398 | /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here. |
399 | Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */ | |
e9a25f70 | 400 | { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 }, |
55a7a95e | 401 | #endif |
7f2935c7 PB |
402 | /* This is the dir for fixincludes. Put it just before |
403 | the files that we fix. */ | |
e9a25f70 | 404 | { GCC_INCLUDE_DIR, "GCC", 0, 0 }, |
7f2935c7 PB |
405 | /* Some systems have an extra dir of include files. */ |
406 | #ifdef SYSTEM_INCLUDE_DIR | |
e9a25f70 | 407 | { SYSTEM_INCLUDE_DIR, 0, 0, 0 }, |
7f2935c7 | 408 | #endif |
e9a25f70 JL |
409 | #ifndef STANDARD_INCLUDE_COMPONENT |
410 | #define STANDARD_INCLUDE_COMPONENT 0 | |
411 | #endif | |
412 | { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 }, | |
7f2935c7 | 413 | #endif /* not CROSS_COMPILE */ |
e9a25f70 | 414 | { 0, 0, 0, 0 } |
7f2935c7 PB |
415 | }; |
416 | #endif /* no INCLUDE_DEFAULTS */ | |
417 | ||
418 | /* `struct directive' defines one #-directive, including how to handle it. */ | |
419 | ||
420 | struct directive { | |
421 | int length; /* Length of name */ | |
422 | int (*func)(); /* Function to handle directive */ | |
423 | char *name; /* Name of directive */ | |
0f41302f MS |
424 | enum node_type type; /* Code which describes which directive. */ |
425 | char command_reads_line; /* One if rest of line is read by func. */ | |
7f2935c7 PB |
426 | }; |
427 | ||
e9a25f70 JL |
428 | #define IS_INCLUDE_DIRECTIVE_TYPE(t) (T_INCLUDE <= (t) && (t) <= T_IMPORT) |
429 | ||
7f2935c7 PB |
430 | /* Here is the actual list of #-directives, most-often-used first. |
431 | The initialize_builtins function assumes #define is the very first. */ | |
432 | ||
433 | static struct directive directive_table[] = { | |
e9a25f70 | 434 | { 6, do_define, "define", T_DEFINE}, |
7f2935c7 PB |
435 | { 5, do_xifdef, "ifdef", T_IFDEF, 1}, |
436 | { 6, do_xifdef, "ifndef", T_IFNDEF, 1}, | |
437 | { 7, do_include, "include", T_INCLUDE, 1}, | |
438 | { 12, do_include, "include_next", T_INCLUDE_NEXT, 1}, | |
439 | { 6, do_include, "import", T_IMPORT, 1}, | |
440 | { 5, do_endif, "endif", T_ENDIF, 1}, | |
441 | { 4, do_else, "else", T_ELSE, 1}, | |
442 | { 2, do_if, "if", T_IF, 1}, | |
443 | { 4, do_elif, "elif", T_ELIF, 1}, | |
444 | { 5, do_undef, "undef", T_UNDEF}, | |
445 | { 5, do_error, "error", T_ERROR}, | |
446 | { 7, do_warning, "warning", T_WARNING}, | |
e9a25f70 | 447 | { 6, do_pragma, "pragma", T_PRAGMA}, |
7f2935c7 | 448 | { 4, do_line, "line", T_LINE, 1}, |
e9a25f70 | 449 | { 5, do_ident, "ident", T_IDENT, 1}, |
7f2935c7 PB |
450 | #ifdef SCCS_DIRECTIVE |
451 | { 4, do_sccs, "sccs", T_SCCS}, | |
452 | #endif | |
453 | { 6, do_assert, "assert", T_ASSERT, 1}, | |
454 | { 8, do_unassert, "unassert", T_UNASSERT, 1}, | |
455 | { -1, 0, "", T_UNUSED}, | |
456 | }; | |
457 | \f | |
0f41302f | 458 | /* table to tell if char can be part of a C identifier. */ |
7f2935c7 | 459 | U_CHAR is_idchar[256]; |
0f41302f | 460 | /* table to tell if char can be first char of a c identifier. */ |
7f2935c7 PB |
461 | U_CHAR is_idstart[256]; |
462 | /* table to tell if c is horizontal space. */ | |
463 | U_CHAR is_hor_space[256]; | |
464 | /* table to tell if c is horizontal or vertical space. */ | |
465 | static U_CHAR is_space[256]; | |
466 | ||
467 | /* Initialize syntactic classifications of characters. */ | |
468 | ||
469 | static void | |
470 | initialize_char_syntax (opts) | |
471 | struct cpp_options *opts; | |
472 | { | |
473 | register int i; | |
474 | ||
475 | /* | |
476 | * Set up is_idchar and is_idstart tables. These should be | |
477 | * faster than saying (is_alpha (c) || c == '_'), etc. | |
478 | * Set up these things before calling any routines tthat | |
479 | * refer to them. | |
480 | */ | |
481 | for (i = 'a'; i <= 'z'; i++) { | |
482 | is_idchar[i - 'a' + 'A'] = 1; | |
483 | is_idchar[i] = 1; | |
484 | is_idstart[i - 'a' + 'A'] = 1; | |
485 | is_idstart[i] = 1; | |
486 | } | |
487 | for (i = '0'; i <= '9'; i++) | |
488 | is_idchar[i] = 1; | |
489 | is_idchar['_'] = 1; | |
490 | is_idstart['_'] = 1; | |
491 | is_idchar['$'] = opts->dollars_in_ident; | |
492 | is_idstart['$'] = opts->dollars_in_ident; | |
493 | ||
494 | /* horizontal space table */ | |
495 | is_hor_space[' '] = 1; | |
496 | is_hor_space['\t'] = 1; | |
497 | is_hor_space['\v'] = 1; | |
498 | is_hor_space['\f'] = 1; | |
499 | is_hor_space['\r'] = 1; | |
500 | ||
501 | is_space[' '] = 1; | |
502 | is_space['\t'] = 1; | |
503 | is_space['\v'] = 1; | |
504 | is_space['\f'] = 1; | |
505 | is_space['\n'] = 1; | |
506 | is_space['\r'] = 1; | |
507 | } | |
508 | ||
7f2935c7 PB |
509 | |
510 | /* Place into PFILE a quoted string representing the string SRC. | |
0f41302f MS |
511 | Caller must reserve enough space in pfile->token_buffer. */ |
512 | ||
7f2935c7 PB |
513 | static void |
514 | quote_string (pfile, src) | |
515 | cpp_reader *pfile; | |
516 | char *src; | |
517 | { | |
518 | U_CHAR c; | |
519 | ||
520 | CPP_PUTC_Q (pfile, '\"'); | |
521 | for (;;) | |
522 | switch ((c = *src++)) | |
523 | { | |
524 | default: | |
525 | if (isprint (c)) | |
526 | CPP_PUTC_Q (pfile, c); | |
527 | else | |
528 | { | |
e9a25f70 | 529 | sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c); |
7f2935c7 PB |
530 | CPP_ADJUST_WRITTEN (pfile, 4); |
531 | } | |
532 | break; | |
533 | ||
534 | case '\"': | |
535 | case '\\': | |
536 | CPP_PUTC_Q (pfile, '\\'); | |
537 | CPP_PUTC_Q (pfile, c); | |
538 | break; | |
539 | ||
540 | case '\0': | |
541 | CPP_PUTC_Q (pfile, '\"'); | |
542 | CPP_NUL_TERMINATE_Q (pfile); | |
543 | return; | |
544 | } | |
545 | } | |
546 | ||
0f41302f | 547 | /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */ |
7f2935c7 PB |
548 | |
549 | void | |
550 | cpp_grow_buffer (pfile, n) | |
551 | cpp_reader *pfile; | |
552 | long n; | |
553 | { | |
554 | long old_written = CPP_WRITTEN (pfile); | |
555 | pfile->token_buffer_size = n + 2 * pfile->token_buffer_size; | |
0f41302f | 556 | pfile->token_buffer = (U_CHAR *) |
7f2935c7 PB |
557 | xrealloc(pfile->token_buffer, pfile->token_buffer_size); |
558 | CPP_SET_WRITTEN (pfile, old_written); | |
559 | } | |
560 | ||
561 | \f | |
562 | /* | |
563 | * process a given definition string, for initialization | |
564 | * If STR is just an identifier, define it with value 1. | |
565 | * If STR has anything after the identifier, then it should | |
566 | * be identifier=definition. | |
567 | */ | |
568 | ||
b13b05f6 PB |
569 | void |
570 | cpp_define (pfile, str) | |
7f2935c7 PB |
571 | cpp_reader *pfile; |
572 | U_CHAR *str; | |
573 | { | |
574 | U_CHAR *buf, *p; | |
575 | ||
576 | buf = str; | |
577 | p = str; | |
578 | if (!is_idstart[*p]) | |
579 | { | |
580 | cpp_error (pfile, "malformed option `-D %s'", str); | |
581 | return; | |
582 | } | |
583 | while (is_idchar[*++p]) | |
584 | ; | |
585 | if (*p == 0) | |
586 | { | |
587 | buf = (U_CHAR *) alloca (p - buf + 4); | |
588 | strcpy ((char *)buf, str); | |
589 | strcat ((char *)buf, " 1"); | |
590 | } | |
591 | else if (*p != '=') | |
592 | { | |
593 | cpp_error (pfile, "malformed option `-D %s'", str); | |
594 | return; | |
595 | } | |
596 | else | |
597 | { | |
598 | U_CHAR *q; | |
599 | /* Copy the entire option so we can modify it. */ | |
600 | buf = (U_CHAR *) alloca (2 * strlen (str) + 1); | |
601 | strncpy (buf, str, p - str); | |
602 | /* Change the = to a space. */ | |
603 | buf[p - str] = ' '; | |
604 | /* Scan for any backslash-newline and remove it. */ | |
605 | p++; | |
606 | q = &buf[p - str]; | |
607 | while (*p) | |
608 | { | |
609 | if (*p == '\\' && p[1] == '\n') | |
610 | p += 2; | |
611 | else | |
612 | *q++ = *p++; | |
613 | } | |
614 | *q = 0; | |
615 | } | |
616 | ||
617 | do_define (pfile, NULL, buf, buf + strlen (buf)); | |
618 | } | |
619 | \f | |
620 | /* Process the string STR as if it appeared as the body of a #assert. | |
621 | OPTION is the option name for which STR was the argument. */ | |
622 | ||
623 | static void | |
624 | make_assertion (pfile, option, str) | |
625 | cpp_reader *pfile; | |
626 | char *option; | |
627 | U_CHAR *str; | |
628 | { | |
7f2935c7 PB |
629 | struct directive *kt; |
630 | U_CHAR *buf, *p, *q; | |
631 | ||
632 | /* Copy the entire option so we can modify it. */ | |
633 | buf = (U_CHAR *) alloca (strlen (str) + 1); | |
634 | strcpy ((char *) buf, str); | |
635 | /* Scan for any backslash-newline and remove it. */ | |
636 | p = q = buf; | |
637 | while (*p) { | |
638 | #if 0 | |
639 | if (*p == '\\' && p[1] == '\n') | |
640 | p += 2; | |
641 | else | |
642 | #endif | |
643 | *q++ = *p++; | |
644 | } | |
645 | *q = 0; | |
646 | ||
647 | p = buf; | |
648 | if (!is_idstart[*p]) { | |
649 | cpp_error (pfile, "malformed option `%s %s'", option, str); | |
650 | return; | |
651 | } | |
652 | while (is_idchar[*++p]) | |
653 | ; | |
654 | while (*p == ' ' || *p == '\t') p++; | |
655 | if (! (*p == 0 || *p == '(')) { | |
656 | cpp_error (pfile, "malformed option `%s %s'", option, str); | |
657 | return; | |
658 | } | |
659 | ||
e2f79f3c PB |
660 | if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL) |
661 | { | |
662 | do_assert (pfile, NULL, NULL, NULL); | |
663 | cpp_pop_buffer (pfile); | |
664 | } | |
7f2935c7 PB |
665 | } |
666 | \f | |
667 | /* Append a chain of `struct file_name_list's | |
668 | to the end of the main include chain. | |
669 | FIRST is the beginning of the chain to append, and LAST is the end. */ | |
670 | ||
671 | static void | |
672 | append_include_chain (pfile, first, last) | |
673 | cpp_reader *pfile; | |
674 | struct file_name_list *first, *last; | |
675 | { | |
676 | struct cpp_options *opts = CPP_OPTIONS (pfile); | |
677 | struct file_name_list *dir; | |
678 | ||
679 | if (!first || !last) | |
680 | return; | |
681 | ||
682 | if (opts->include == 0) | |
683 | opts->include = first; | |
684 | else | |
685 | opts->last_include->next = first; | |
686 | ||
687 | if (opts->first_bracket_include == 0) | |
688 | opts->first_bracket_include = first; | |
689 | ||
690 | for (dir = first; ; dir = dir->next) { | |
691 | int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE; | |
692 | if (len > pfile->max_include_len) | |
693 | pfile->max_include_len = len; | |
694 | if (dir == last) | |
695 | break; | |
696 | } | |
697 | ||
698 | last->next = NULL; | |
699 | opts->last_include = last; | |
700 | } | |
701 | \f | |
702 | /* Add output to `deps_buffer' for the -M switch. | |
703 | STRING points to the text to be output. | |
704 | SPACER is ':' for targets, ' ' for dependencies, zero for text | |
705 | to be inserted literally. */ | |
706 | ||
707 | static void | |
708 | deps_output (pfile, string, spacer) | |
709 | cpp_reader *pfile; | |
710 | char *string; | |
711 | int spacer; | |
712 | { | |
713 | int size = strlen (string); | |
714 | ||
715 | if (size == 0) | |
716 | return; | |
717 | ||
718 | #ifndef MAX_OUTPUT_COLUMNS | |
719 | #define MAX_OUTPUT_COLUMNS 72 | |
720 | #endif | |
721 | if (spacer | |
722 | && pfile->deps_column > 0 | |
723 | && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS) | |
724 | { | |
725 | deps_output (pfile, " \\\n ", 0); | |
726 | pfile->deps_column = 0; | |
727 | } | |
728 | ||
729 | if (pfile->deps_size + size + 8 > pfile->deps_allocated_size) | |
730 | { | |
731 | pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2; | |
732 | pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer, | |
733 | pfile->deps_allocated_size); | |
734 | } | |
735 | if (spacer == ' ' && pfile->deps_column > 0) | |
736 | pfile->deps_buffer[pfile->deps_size++] = ' '; | |
737 | bcopy (string, &pfile->deps_buffer[pfile->deps_size], size); | |
738 | pfile->deps_size += size; | |
739 | pfile->deps_column += size; | |
740 | if (spacer == ':') | |
741 | pfile->deps_buffer[pfile->deps_size++] = ':'; | |
742 | pfile->deps_buffer[pfile->deps_size] = 0; | |
743 | } | |
744 | \f | |
745 | /* Given a colon-separated list of file names PATH, | |
746 | add all the names to the search path for include files. */ | |
747 | ||
748 | static void | |
749 | path_include (pfile, path) | |
750 | cpp_reader *pfile; | |
751 | char *path; | |
752 | { | |
753 | char *p; | |
754 | ||
755 | p = path; | |
756 | ||
757 | if (*p) | |
758 | while (1) { | |
759 | char *q = p; | |
760 | char *name; | |
761 | struct file_name_list *dirtmp; | |
762 | ||
763 | /* Find the end of this name. */ | |
764 | while (*q != 0 && *q != PATH_SEPARATOR) q++; | |
765 | if (p == q) { | |
766 | /* An empty name in the path stands for the current directory. */ | |
767 | name = (char *) xmalloc (2); | |
768 | name[0] = '.'; | |
769 | name[1] = 0; | |
770 | } else { | |
771 | /* Otherwise use the directory that is named. */ | |
772 | name = (char *) xmalloc (q - p + 1); | |
773 | bcopy (p, name, q - p); | |
774 | name[q - p] = 0; | |
775 | } | |
776 | ||
777 | dirtmp = (struct file_name_list *) | |
778 | xmalloc (sizeof (struct file_name_list)); | |
779 | dirtmp->next = 0; /* New one goes on the end */ | |
780 | dirtmp->control_macro = 0; | |
781 | dirtmp->c_system_include_path = 0; | |
782 | dirtmp->fname = name; | |
783 | dirtmp->got_name_map = 0; | |
784 | append_include_chain (pfile, dirtmp, dirtmp); | |
785 | ||
786 | /* Advance past this name. */ | |
787 | p = q; | |
788 | if (*p == 0) | |
789 | break; | |
790 | /* Skip the colon. */ | |
791 | p++; | |
792 | } | |
793 | } | |
794 | \f | |
795 | void | |
a94c94be PB |
796 | cpp_options_init (opts) |
797 | cpp_options *opts; | |
7f2935c7 | 798 | { |
5f972d0c | 799 | bzero ((char *) opts, sizeof *opts); |
7f2935c7 PB |
800 | opts->in_fname = NULL; |
801 | opts->out_fname = NULL; | |
802 | ||
803 | /* Initialize is_idchar to allow $. */ | |
804 | opts->dollars_in_ident = 1; | |
805 | initialize_char_syntax (opts); | |
7f2935c7 PB |
806 | |
807 | opts->no_line_commands = 0; | |
808 | opts->no_trigraphs = 1; | |
809 | opts->put_out_comments = 0; | |
810 | opts->print_include_names = 0; | |
811 | opts->dump_macros = dump_none; | |
812 | opts->no_output = 0; | |
813 | opts->cplusplus = 0; | |
814 | opts->cplusplus_comments = 0; | |
815 | ||
816 | opts->verbose = 0; | |
817 | opts->objc = 0; | |
818 | opts->lang_asm = 0; | |
819 | opts->for_lint = 0; | |
820 | opts->chill = 0; | |
821 | opts->pedantic_errors = 0; | |
822 | opts->inhibit_warnings = 0; | |
823 | opts->warn_comments = 0; | |
824 | opts->warn_import = 1; | |
825 | opts->warnings_are_errors = 0; | |
826 | } | |
827 | ||
828 | enum cpp_token | |
829 | null_underflow (pfile) | |
830 | cpp_reader *pfile; | |
831 | { | |
832 | return CPP_EOF; | |
833 | } | |
834 | ||
835 | int | |
836 | null_cleanup (pbuf, pfile) | |
837 | cpp_buffer *pbuf; | |
838 | cpp_reader *pfile; | |
839 | { | |
840 | return 0; | |
841 | } | |
842 | ||
843 | int | |
844 | macro_cleanup (pbuf, pfile) | |
845 | cpp_buffer *pbuf; | |
846 | cpp_reader *pfile; | |
847 | { | |
0f41302f | 848 | HASHNODE *macro = (HASHNODE *) pbuf->data; |
7f2935c7 PB |
849 | if (macro->type == T_DISABLED) |
850 | macro->type = T_MACRO; | |
851 | if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion) | |
852 | free (pbuf->buf); | |
853 | return 0; | |
854 | } | |
855 | ||
856 | int | |
857 | file_cleanup (pbuf, pfile) | |
858 | cpp_buffer *pbuf; | |
859 | cpp_reader *pfile; | |
860 | { | |
782331f4 PB |
861 | if (pbuf->buf) |
862 | { | |
863 | free (pbuf->buf); | |
864 | pbuf->buf = 0; | |
865 | } | |
7f2935c7 PB |
866 | return 0; |
867 | } | |
868 | ||
7f2935c7 PB |
869 | /* Assuming we have read '/'. |
870 | If this is the start of a comment (followed by '*' or '/'), | |
871 | skip to the end of the comment, and return ' '. | |
872 | Return EOF if we reached the end of file before the end of the comment. | |
0f41302f | 873 | If not the start of a comment, return '/'. */ |
7f2935c7 PB |
874 | |
875 | static int | |
876 | skip_comment (pfile, linep) | |
877 | cpp_reader *pfile; | |
878 | long *linep; | |
879 | { | |
d1b680c6 | 880 | int c = 0; |
7f2935c7 PB |
881 | while (PEEKC() == '\\' && PEEKN(1) == '\n') |
882 | { | |
883 | if (linep) | |
884 | (*linep)++; | |
885 | FORWARD(2); | |
886 | } | |
887 | if (PEEKC() == '*') | |
888 | { | |
889 | FORWARD(1); | |
890 | for (;;) | |
891 | { | |
892 | int prev_c = c; | |
893 | c = GETC (); | |
894 | if (c == EOF) | |
895 | return EOF; | |
896 | while (c == '\\' && PEEKC() == '\n') | |
897 | { | |
898 | if (linep) | |
899 | (*linep)++; | |
900 | FORWARD(1), c = GETC(); | |
901 | } | |
902 | if (prev_c == '*' && c == '/') | |
903 | return ' '; | |
904 | if (c == '\n' && linep) | |
905 | (*linep)++; | |
906 | } | |
907 | } | |
908 | else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments) | |
909 | { | |
910 | FORWARD(1); | |
911 | for (;;) | |
912 | { | |
913 | c = GETC (); | |
914 | if (c == EOF) | |
0f41302f | 915 | return ' '; /* Allow // to be terminated by EOF. */ |
7f2935c7 PB |
916 | while (c == '\\' && PEEKC() == '\n') |
917 | { | |
918 | FORWARD(1); | |
919 | c = GETC(); | |
920 | if (linep) | |
921 | (*linep)++; | |
922 | } | |
923 | if (c == '\n') | |
924 | { | |
0f41302f | 925 | /* Don't consider final '\n' to be part of comment. */ |
7f2935c7 PB |
926 | FORWARD(-1); |
927 | return ' '; | |
928 | } | |
929 | } | |
930 | } | |
931 | else | |
932 | return '/'; | |
933 | } | |
934 | ||
935 | /* Skip whitespace \-newline and comments. Does not macro-expand. */ | |
0f41302f | 936 | |
7f2935c7 PB |
937 | void |
938 | cpp_skip_hspace (pfile) | |
939 | cpp_reader *pfile; | |
940 | { | |
941 | while (1) | |
942 | { | |
943 | int c = PEEKC(); | |
944 | if (c == EOF) | |
945 | return; /* FIXME */ | |
946 | if (is_hor_space[c]) | |
947 | { | |
948 | if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile)) | |
949 | cpp_pedwarn (pfile, "%s in preprocessing directive", | |
950 | c == '\f' ? "formfeed" : "vertical tab"); | |
951 | FORWARD(1); | |
952 | } | |
953 | else if (c == '/') | |
954 | { | |
955 | FORWARD (1); | |
956 | c = skip_comment (pfile, NULL); | |
957 | if (c == '/') | |
958 | FORWARD(-1); | |
959 | if (c == EOF || c == '/') | |
960 | return; | |
961 | } | |
962 | else if (c == '\\' && PEEKN(1) == '\n') { | |
963 | FORWARD(2); | |
964 | } | |
782331f4 PB |
965 | else if (c == '@' && CPP_BUFFER (pfile)->has_escapes |
966 | && is_hor_space[PEEKN(1)]) | |
967 | FORWARD(2); | |
7f2935c7 PB |
968 | else return; |
969 | } | |
970 | } | |
971 | ||
972 | /* Read the rest of the current line. | |
0f41302f | 973 | The line is appended to PFILE's output buffer. */ |
7f2935c7 | 974 | |
e2f79f3c | 975 | static void |
7f2935c7 PB |
976 | copy_rest_of_line (pfile) |
977 | cpp_reader *pfile; | |
978 | { | |
979 | struct cpp_options *opts = CPP_OPTIONS (pfile); | |
980 | for (;;) | |
981 | { | |
982 | int c = GETC(); | |
983 | int nextc; | |
984 | switch (c) | |
985 | { | |
986 | case EOF: | |
987 | goto end_directive; | |
988 | case '\\': | |
989 | if (PEEKC() == '\n') | |
990 | { | |
991 | FORWARD (1); | |
992 | continue; | |
993 | } | |
994 | case '\'': | |
995 | case '\"': | |
996 | goto scan_directive_token; | |
997 | break; | |
998 | case '/': | |
999 | nextc = PEEKC(); | |
e9a25f70 | 1000 | if (nextc == '*' || (opts->cplusplus_comments && nextc == '/')) |
7f2935c7 PB |
1001 | goto scan_directive_token; |
1002 | break; | |
1003 | case '\f': | |
1004 | case '\v': | |
1005 | if (CPP_PEDANTIC (pfile)) | |
1006 | cpp_pedwarn (pfile, "%s in preprocessing directive", | |
1007 | c == '\f' ? "formfeed" : "vertical tab"); | |
1008 | break; | |
1009 | ||
1010 | case '\n': | |
1011 | FORWARD(-1); | |
1012 | goto end_directive; | |
1013 | scan_directive_token: | |
1014 | FORWARD(-1); | |
1015 | cpp_get_token (pfile); | |
1016 | continue; | |
1017 | } | |
1018 | CPP_PUTC (pfile, c); | |
1019 | } | |
1020 | end_directive: ; | |
1021 | CPP_NUL_TERMINATE (pfile); | |
1022 | } | |
1023 | ||
1024 | void | |
1025 | skip_rest_of_line (pfile) | |
1026 | cpp_reader *pfile; | |
1027 | { | |
1028 | long old = CPP_WRITTEN (pfile); | |
1029 | copy_rest_of_line (pfile); | |
1030 | CPP_SET_WRITTEN (pfile, old); | |
1031 | } | |
1032 | ||
1033 | /* Handle a possible # directive. | |
1034 | '#' has already been read. */ | |
1035 | ||
1036 | int | |
1037 | handle_directive (pfile) | |
1038 | cpp_reader *pfile; | |
1039 | { int c; | |
1040 | register struct directive *kt; | |
1041 | int ident_length; | |
1042 | long after_ident; | |
1043 | U_CHAR *ident, *line_end; | |
1044 | long old_written = CPP_WRITTEN (pfile); | |
1045 | ||
1046 | cpp_skip_hspace (pfile); | |
1047 | ||
1048 | c = PEEKC (); | |
1049 | if (c >= '0' && c <= '9') | |
1050 | { | |
1051 | /* Handle # followed by a line number. */ | |
1052 | if (CPP_PEDANTIC (pfile)) | |
1053 | cpp_pedwarn (pfile, "`#' followed by integer"); | |
1054 | do_line (pfile, NULL); | |
1055 | goto done_a_directive; | |
1056 | } | |
1057 | ||
0f41302f | 1058 | /* Now find the directive name. */ |
7f2935c7 PB |
1059 | CPP_PUTC (pfile, '#'); |
1060 | parse_name (pfile, GETC()); | |
1061 | ident = pfile->token_buffer + old_written + 1; | |
1062 | ident_length = CPP_PWRITTEN (pfile) - ident; | |
1063 | if (ident_length == 0 && PEEKC() == '\n') | |
1064 | { | |
1065 | /* A line of just `#' becomes blank. */ | |
1066 | goto done_a_directive; | |
1067 | } | |
1068 | ||
1069 | #if 0 | |
1070 | if (ident_length == 0 || !is_idstart[*ident]) { | |
1071 | U_CHAR *p = ident; | |
1072 | while (is_idchar[*p]) { | |
1073 | if (*p < '0' || *p > '9') | |
1074 | break; | |
1075 | p++; | |
1076 | } | |
1077 | /* Avoid error for `###' and similar cases unless -pedantic. */ | |
1078 | if (p == ident) { | |
1079 | while (*p == '#' || is_hor_space[*p]) p++; | |
1080 | if (*p == '\n') { | |
1081 | if (pedantic && !lang_asm) | |
1082 | cpp_warning (pfile, "invalid preprocessor directive"); | |
1083 | return 0; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | if (!lang_asm) | |
1088 | cpp_error (pfile, "invalid preprocessor directive name"); | |
1089 | ||
1090 | return 0; | |
1091 | } | |
1092 | #endif | |
1093 | /* | |
1094 | * Decode the keyword and call the appropriate expansion | |
1095 | * routine, after moving the input pointer up to the next line. | |
1096 | */ | |
1097 | for (kt = directive_table; ; kt++) { | |
1098 | if (kt->length <= 0) | |
1099 | goto not_a_directive; | |
1100 | if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) | |
1101 | break; | |
1102 | } | |
1103 | ||
d1b680c6 PE |
1104 | if (kt->command_reads_line) |
1105 | after_ident = 0; | |
1106 | else | |
7f2935c7 PB |
1107 | { |
1108 | /* Nonzero means do not delete comments within the directive. | |
1109 | #define needs this when -traditional. */ | |
e9a25f70 | 1110 | int comments = CPP_TRADITIONAL (pfile) && kt->type == T_DEFINE; |
7f2935c7 PB |
1111 | int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments; |
1112 | CPP_OPTIONS (pfile)->put_out_comments = comments; | |
1113 | after_ident = CPP_WRITTEN (pfile); | |
1114 | copy_rest_of_line (pfile); | |
1115 | CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments; | |
1116 | } | |
1117 | ||
e9a25f70 | 1118 | /* We may want to pass through #define, #pragma, and #include. |
7f2935c7 | 1119 | Other directives may create output, but we don't want the directive |
e9a25f70 | 1120 | itself out, so we pop it now. For example conditionals may emit |
7f2935c7 PB |
1121 | #failed ... #endfailed stuff. But note that popping the buffer |
1122 | means the parameters to kt->func may point after pfile->limit | |
1123 | so these parameters are invalid as soon as something gets appended | |
1124 | to the token_buffer. */ | |
1125 | ||
1126 | line_end = CPP_PWRITTEN (pfile); | |
e9a25f70 JL |
1127 | if (! (kt->type == T_DEFINE |
1128 | || kt->type == T_PRAGMA | |
1129 | || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type) | |
1130 | && CPP_OPTIONS (pfile)->dump_includes))) | |
7f2935c7 PB |
1131 | CPP_SET_WRITTEN (pfile, old_written); |
1132 | ||
1133 | (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end); | |
e9a25f70 JL |
1134 | |
1135 | if (kt->type == T_DEFINE) | |
7f2935c7 | 1136 | { |
e9a25f70 JL |
1137 | if (CPP_OPTIONS (pfile)->dump_macros == dump_names) |
1138 | { | |
1139 | /* Skip "#define". */ | |
1140 | U_CHAR *p = pfile->token_buffer + old_written + 7; | |
1141 | ||
1142 | SKIP_WHITE_SPACE (p); | |
1143 | while (is_idchar[*p]) p++; | |
1144 | pfile->limit = p; | |
1145 | CPP_PUTC (pfile, '\n'); | |
1146 | } | |
1147 | else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions) | |
1148 | CPP_SET_WRITTEN (pfile, old_written); | |
7f2935c7 | 1149 | } |
e9a25f70 | 1150 | |
7f2935c7 PB |
1151 | done_a_directive: |
1152 | return 1; | |
1153 | ||
1154 | not_a_directive: | |
1155 | return 0; | |
1156 | } | |
1157 | ||
1158 | /* Pass a directive through to the output file. | |
1159 | BUF points to the contents of the directive, as a contiguous string. | |
1160 | LIMIT points to the first character past the end of the directive. | |
1161 | KEYWORD is the keyword-table entry for the directive. */ | |
1162 | ||
1163 | static void | |
1164 | pass_thru_directive (buf, limit, pfile, keyword) | |
1165 | U_CHAR *buf, *limit; | |
1166 | cpp_reader *pfile; | |
1167 | struct directive *keyword; | |
1168 | { | |
1169 | register unsigned keyword_length = keyword->length; | |
1170 | ||
1171 | CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf)); | |
1172 | CPP_PUTC_Q (pfile, '#'); | |
1173 | CPP_PUTS_Q (pfile, keyword->name, keyword_length); | |
1174 | if (limit != buf && buf[0] != ' ') | |
1175 | CPP_PUTC_Q (pfile, ' '); | |
1176 | CPP_PUTS_Q (pfile, buf, limit - buf); | |
1177 | #if 0 | |
1178 | CPP_PUTS_Q (pfile, '\n'); | |
1179 | /* Count the line we have just made in the output, | |
1180 | to get in sync properly. */ | |
1181 | pfile->lineno++; | |
1182 | #endif | |
1183 | } | |
1184 | \f | |
1185 | /* The arglist structure is built by do_define to tell | |
1186 | collect_definition where the argument names begin. That | |
1187 | is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist | |
1188 | would contain pointers to the strings x, y, and z. | |
1189 | Collect_definition would then build a DEFINITION node, | |
1190 | with reflist nodes pointing to the places x, y, and z had | |
1191 | appeared. So the arglist is just convenience data passed | |
1192 | between these two routines. It is not kept around after | |
1193 | the current #define has been processed and entered into the | |
0f41302f | 1194 | hash table. */ |
7f2935c7 PB |
1195 | |
1196 | struct arglist { | |
1197 | struct arglist *next; | |
1198 | U_CHAR *name; | |
1199 | int length; | |
1200 | int argno; | |
1201 | char rest_args; | |
1202 | }; | |
1203 | ||
1204 | /* Read a replacement list for a macro with parameters. | |
1205 | Build the DEFINITION structure. | |
1206 | Reads characters of text starting at BUF until END. | |
1207 | ARGLIST specifies the formal parameters to look for | |
1208 | in the text of the definition; NARGS is the number of args | |
1209 | in that list, or -1 for a macro name that wants no argument list. | |
1210 | MACRONAME is the macro name itself (so we can avoid recursive expansion) | |
1211 | and NAMELEN is its length in characters. | |
1212 | ||
782331f4 PB |
1213 | Note that comments, backslash-newlines, and leading white space |
1214 | have already been deleted from the argument. */ | |
7f2935c7 PB |
1215 | |
1216 | static DEFINITION * | |
782331f4 | 1217 | collect_expansion (pfile, buf, limit, nargs, arglist) |
7f2935c7 | 1218 | cpp_reader *pfile; |
782331f4 | 1219 | U_CHAR *buf, *limit; |
7f2935c7 PB |
1220 | int nargs; |
1221 | struct arglist *arglist; | |
1222 | { | |
1223 | DEFINITION *defn; | |
782331f4 | 1224 | register U_CHAR *p, *lastp, *exp_p; |
7f2935c7 PB |
1225 | struct reflist *endpat = NULL; |
1226 | /* Pointer to first nonspace after last ## seen. */ | |
1227 | U_CHAR *concat = 0; | |
1228 | /* Pointer to first nonspace after last single-# seen. */ | |
1229 | U_CHAR *stringify = 0; | |
1230 | int maxsize; | |
1231 | int expected_delimiter = '\0'; | |
1232 | ||
1233 | /* Scan thru the replacement list, ignoring comments and quoted | |
1234 | strings, picking up on the macro calls. It does a linear search | |
1235 | thru the arg list on every potential symbol. Profiling might say | |
0f41302f | 1236 | that something smarter should happen. */ |
7f2935c7 | 1237 | |
782331f4 | 1238 | if (limit < buf) |
7f2935c7 PB |
1239 | abort (); |
1240 | ||
1241 | /* Find the beginning of the trailing whitespace. */ | |
7f2935c7 PB |
1242 | p = buf; |
1243 | while (p < limit && is_space[limit[-1]]) limit--; | |
7f2935c7 PB |
1244 | |
1245 | /* Allocate space for the text in the macro definition. | |
1246 | Leading and trailing whitespace chars need 2 bytes each. | |
1247 | Each other input char may or may not need 1 byte, | |
782331f4 PB |
1248 | so this is an upper bound. The extra 5 are for invented |
1249 | leading and trailing newline-marker and final null. */ | |
7f2935c7 | 1250 | maxsize = (sizeof (DEFINITION) |
782331f4 | 1251 | + (limit - p) + 5); |
0f41302f | 1252 | /* Occurrences of '@' get doubled, so allocate extra space for them. */ |
782331f4 PB |
1253 | while (p < limit) |
1254 | if (*p++ == '@') | |
1255 | maxsize++; | |
7f2935c7 PB |
1256 | defn = (DEFINITION *) xcalloc (1, maxsize); |
1257 | ||
1258 | defn->nargs = nargs; | |
1259 | exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION); | |
1260 | lastp = exp_p; | |
1261 | ||
1262 | p = buf; | |
1263 | ||
782331f4 | 1264 | /* Add one initial space escape-marker to prevent accidental |
0f41302f | 1265 | token-pasting (often removed by macroexpand). */ |
782331f4 | 1266 | *exp_p++ = '@'; |
7f2935c7 PB |
1267 | *exp_p++ = ' '; |
1268 | ||
1269 | if (limit - p >= 2 && p[0] == '#' && p[1] == '#') { | |
1270 | cpp_error (pfile, "`##' at start of macro definition"); | |
1271 | p += 2; | |
1272 | } | |
1273 | ||
1274 | /* Process the main body of the definition. */ | |
1275 | while (p < limit) { | |
1276 | int skipped_arg = 0; | |
1277 | register U_CHAR c = *p++; | |
1278 | ||
1279 | *exp_p++ = c; | |
1280 | ||
1281 | if (!CPP_TRADITIONAL (pfile)) { | |
1282 | switch (c) { | |
1283 | case '\'': | |
1284 | case '\"': | |
1285 | if (expected_delimiter != '\0') { | |
1286 | if (c == expected_delimiter) | |
1287 | expected_delimiter = '\0'; | |
1288 | } else | |
1289 | expected_delimiter = c; | |
1290 | break; | |
1291 | ||
7f2935c7 | 1292 | case '\\': |
782331f4 | 1293 | if (p < limit && expected_delimiter) { |
7f2935c7 PB |
1294 | /* In a string, backslash goes through |
1295 | and makes next char ordinary. */ | |
1296 | *exp_p++ = *p++; | |
1297 | } | |
1298 | break; | |
1299 | ||
1300 | case '@': | |
a2baccf6 | 1301 | /* An '@' in a string or character constant stands for itself, |
0f41302f | 1302 | and does not need to be escaped. */ |
a2baccf6 PB |
1303 | if (!expected_delimiter) |
1304 | *exp_p++ = c; | |
7f2935c7 PB |
1305 | break; |
1306 | ||
1307 | case '#': | |
1308 | /* # is ordinary inside a string. */ | |
1309 | if (expected_delimiter) | |
1310 | break; | |
1311 | if (p < limit && *p == '#') { | |
1312 | /* ##: concatenate preceding and following tokens. */ | |
1313 | /* Take out the first #, discard preceding whitespace. */ | |
1314 | exp_p--; | |
1315 | while (exp_p > lastp && is_hor_space[exp_p[-1]]) | |
1316 | --exp_p; | |
1317 | /* Skip the second #. */ | |
1318 | p++; | |
1319 | /* Discard following whitespace. */ | |
1320 | SKIP_WHITE_SPACE (p); | |
1321 | concat = p; | |
1322 | if (p == limit) | |
1323 | cpp_error (pfile, "`##' at end of macro definition"); | |
1324 | } else if (nargs >= 0) { | |
1325 | /* Single #: stringify following argument ref. | |
1326 | Don't leave the # in the expansion. */ | |
1327 | exp_p--; | |
1328 | SKIP_WHITE_SPACE (p); | |
4c8cc616 RK |
1329 | if (p == limit || ! is_idstart[*p] |
1330 | || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"'))) | |
7f2935c7 PB |
1331 | cpp_error (pfile, |
1332 | "`#' operator is not followed by a macro argument name"); | |
1333 | else | |
1334 | stringify = p; | |
1335 | } | |
1336 | break; | |
1337 | } | |
1338 | } else { | |
1339 | /* In -traditional mode, recognize arguments inside strings and | |
1340 | and character constants, and ignore special properties of #. | |
1341 | Arguments inside strings are considered "stringified", but no | |
1342 | extra quote marks are supplied. */ | |
1343 | switch (c) { | |
1344 | case '\'': | |
1345 | case '\"': | |
1346 | if (expected_delimiter != '\0') { | |
1347 | if (c == expected_delimiter) | |
1348 | expected_delimiter = '\0'; | |
1349 | } else | |
1350 | expected_delimiter = c; | |
1351 | break; | |
1352 | ||
1353 | case '\\': | |
1354 | /* Backslash quotes delimiters and itself, but not macro args. */ | |
1355 | if (expected_delimiter != 0 && p < limit | |
1356 | && (*p == expected_delimiter || *p == '\\')) { | |
1357 | *exp_p++ = *p++; | |
1358 | continue; | |
1359 | } | |
1360 | break; | |
1361 | ||
1362 | case '/': | |
1363 | if (expected_delimiter != '\0') /* No comments inside strings. */ | |
1364 | break; | |
1365 | if (*p == '*') { | |
1366 | /* If we find a comment that wasn't removed by handle_directive, | |
1367 | this must be -traditional. So replace the comment with | |
1368 | nothing at all. */ | |
1369 | exp_p--; | |
1370 | p += 1; | |
1371 | while (p < limit && !(p[-2] == '*' && p[-1] == '/')) | |
1372 | p++; | |
1373 | #if 0 | |
1374 | /* Mark this as a concatenation-point, as if it had been ##. */ | |
1375 | concat = p; | |
1376 | #endif | |
1377 | } | |
1378 | break; | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | /* Handle the start of a symbol. */ | |
1383 | if (is_idchar[c] && nargs > 0) { | |
1384 | U_CHAR *id_beg = p - 1; | |
1385 | int id_len; | |
1386 | ||
1387 | --exp_p; | |
1388 | while (p != limit && is_idchar[*p]) p++; | |
1389 | id_len = p - id_beg; | |
1390 | ||
4c8cc616 RK |
1391 | if (is_idstart[c] |
1392 | && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) { | |
7f2935c7 PB |
1393 | register struct arglist *arg; |
1394 | ||
1395 | for (arg = arglist; arg != NULL; arg = arg->next) { | |
1396 | struct reflist *tpat; | |
1397 | ||
1398 | if (arg->name[0] == c | |
1399 | && arg->length == id_len | |
1400 | && strncmp (arg->name, id_beg, id_len) == 0) { | |
1401 | if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) { | |
1402 | if (CPP_TRADITIONAL (pfile)) { | |
1403 | cpp_warning (pfile, "macro argument `%.*s' is stringified.", | |
1404 | id_len, arg->name); | |
1405 | } else { | |
1406 | cpp_warning (pfile, | |
1407 | "macro arg `%.*s' would be stringified with -traditional.", | |
1408 | id_len, arg->name); | |
1409 | } | |
1410 | } | |
1411 | /* If ANSI, don't actually substitute inside a string. */ | |
1412 | if (!CPP_TRADITIONAL (pfile) && expected_delimiter) | |
1413 | break; | |
1414 | /* make a pat node for this arg and append it to the end of | |
1415 | the pat list */ | |
1416 | tpat = (struct reflist *) xmalloc (sizeof (struct reflist)); | |
1417 | tpat->next = NULL; | |
1418 | tpat->raw_before = concat == id_beg; | |
1419 | tpat->raw_after = 0; | |
1420 | tpat->rest_args = arg->rest_args; | |
1421 | tpat->stringify = (CPP_TRADITIONAL (pfile) | |
1422 | ? expected_delimiter != '\0' | |
1423 | : stringify == id_beg); | |
1424 | ||
1425 | if (endpat == NULL) | |
1426 | defn->pattern = tpat; | |
1427 | else | |
1428 | endpat->next = tpat; | |
1429 | endpat = tpat; | |
1430 | ||
1431 | tpat->argno = arg->argno; | |
1432 | tpat->nchars = exp_p - lastp; | |
1433 | { | |
1434 | register U_CHAR *p1 = p; | |
1435 | SKIP_WHITE_SPACE (p1); | |
1436 | if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#') | |
1437 | tpat->raw_after = 1; | |
1438 | } | |
1439 | lastp = exp_p; /* place to start copying from next time */ | |
1440 | skipped_arg = 1; | |
1441 | break; | |
1442 | } | |
1443 | } | |
1444 | } | |
1445 | ||
1446 | /* If this was not a macro arg, copy it into the expansion. */ | |
1447 | if (! skipped_arg) { | |
1448 | register U_CHAR *lim1 = p; | |
1449 | p = id_beg; | |
1450 | while (p != lim1) | |
1451 | *exp_p++ = *p++; | |
1452 | if (stringify == id_beg) | |
1453 | cpp_error (pfile, | |
1454 | "`#' operator should be followed by a macro argument name"); | |
1455 | } | |
1456 | } | |
1457 | } | |
1458 | ||
782331f4 PB |
1459 | if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0) |
1460 | { | |
1461 | /* If ANSI, put in a "@ " marker to prevent token pasting. | |
1462 | But not if "inside a string" (which in ANSI mode | |
1463 | happens only for -D option). */ | |
1464 | *exp_p++ = '@'; | |
1465 | *exp_p++ = ' '; | |
7f2935c7 | 1466 | } |
7f2935c7 PB |
1467 | |
1468 | *exp_p = '\0'; | |
1469 | ||
1470 | defn->length = exp_p - defn->expansion; | |
1471 | ||
1472 | /* Crash now if we overrun the allocated size. */ | |
1473 | if (defn->length + 1 > maxsize) | |
1474 | abort (); | |
1475 | ||
1476 | #if 0 | |
1477 | /* This isn't worth the time it takes. */ | |
1478 | /* give back excess storage */ | |
1479 | defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1); | |
1480 | #endif | |
1481 | ||
1482 | return defn; | |
1483 | } | |
1484 | ||
1485 | /* | |
1486 | * special extension string that can be added to the last macro argument to | |
1487 | * allow it to absorb the "rest" of the arguments when expanded. Ex: | |
1488 | * #define wow(a, b...) process (b, a, b) | |
1489 | * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); } | |
1490 | * { wow (one, two); } -> { process (two, one, two); } | |
1491 | * if this "rest_arg" is used with the concat token '##' and if it is not | |
1492 | * supplied then the token attached to with ## will not be outputted. Ex: | |
1493 | * #define wow (a, b...) process (b ## , a, ## b) | |
1494 | * { wow (1, 2); } -> { process (2, 1, 2); } | |
1495 | * { wow (one); } -> { process (one); { | |
1496 | */ | |
1497 | static char rest_extension[] = "..."; | |
1498 | #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1) | |
1499 | ||
1500 | /* Create a DEFINITION node from a #define directive. Arguments are | |
0f41302f MS |
1501 | as for do_define. */ |
1502 | ||
7f2935c7 PB |
1503 | static MACRODEF |
1504 | create_definition (buf, limit, pfile, predefinition) | |
1505 | U_CHAR *buf, *limit; | |
1506 | cpp_reader *pfile; | |
1507 | int predefinition; | |
1508 | { | |
1509 | U_CHAR *bp; /* temp ptr into input buffer */ | |
1510 | U_CHAR *symname; /* remember where symbol name starts */ | |
1511 | int sym_length; /* and how long it is */ | |
1512 | int rest_args = 0; | |
1513 | long line, col; | |
1514 | char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : ""; | |
1515 | DEFINITION *defn; | |
1516 | int arglengths = 0; /* Accumulate lengths of arg names | |
1517 | plus number of args. */ | |
1518 | MACRODEF mdef; | |
1519 | cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col); | |
1520 | ||
1521 | bp = buf; | |
1522 | ||
1523 | while (is_hor_space[*bp]) | |
1524 | bp++; | |
1525 | ||
1526 | symname = bp; /* remember where it starts */ | |
1527 | ||
1528 | sym_length = check_macro_name (pfile, bp, "macro"); | |
1529 | bp += sym_length; | |
1530 | ||
1531 | /* Lossage will occur if identifiers or control keywords are broken | |
1532 | across lines using backslash. This is not the right place to take | |
0f41302f | 1533 | care of that. */ |
7f2935c7 PB |
1534 | |
1535 | if (*bp == '(') { | |
1536 | struct arglist *arg_ptrs = NULL; | |
1537 | int argno = 0; | |
1538 | ||
1539 | bp++; /* skip '(' */ | |
1540 | SKIP_WHITE_SPACE (bp); | |
1541 | ||
1542 | /* Loop over macro argument names. */ | |
1543 | while (*bp != ')') { | |
1544 | struct arglist *temp; | |
1545 | ||
1546 | temp = (struct arglist *) alloca (sizeof (struct arglist)); | |
1547 | temp->name = bp; | |
1548 | temp->next = arg_ptrs; | |
1549 | temp->argno = argno++; | |
1550 | temp->rest_args = 0; | |
1551 | arg_ptrs = temp; | |
1552 | ||
1553 | if (rest_args) | |
1554 | cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension); | |
1555 | ||
1556 | if (!is_idstart[*bp]) | |
1557 | cpp_pedwarn (pfile, "invalid character in macro parameter name"); | |
1558 | ||
1559 | /* Find the end of the arg name. */ | |
1560 | while (is_idchar[*bp]) { | |
1561 | bp++; | |
1562 | /* do we have a "special" rest-args extension here? */ | |
e3da301d MS |
1563 | if (limit - bp > REST_EXTENSION_LENGTH |
1564 | && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) { | |
7f2935c7 PB |
1565 | rest_args = 1; |
1566 | temp->rest_args = 1; | |
1567 | break; | |
1568 | } | |
1569 | } | |
1570 | temp->length = bp - temp->name; | |
1571 | if (rest_args == 1) | |
1572 | bp += REST_EXTENSION_LENGTH; | |
1573 | arglengths += temp->length + 2; | |
1574 | SKIP_WHITE_SPACE (bp); | |
1575 | if (temp->length == 0 || (*bp != ',' && *bp != ')')) { | |
1576 | cpp_error (pfile, "badly punctuated parameter list in `#define'"); | |
1577 | goto nope; | |
1578 | } | |
1579 | if (*bp == ',') { | |
1580 | bp++; | |
1581 | SKIP_WHITE_SPACE (bp); | |
1582 | } | |
1583 | if (bp >= limit) { | |
1584 | cpp_error (pfile, "unterminated parameter list in `#define'"); | |
1585 | goto nope; | |
1586 | } | |
1587 | { | |
1588 | struct arglist *otemp; | |
1589 | ||
1590 | for (otemp = temp->next; otemp != NULL; otemp = otemp->next) | |
e3da301d MS |
1591 | if (temp->length == otemp->length |
1592 | && strncmp (temp->name, otemp->name, temp->length) == 0) { | |
7f2935c7 PB |
1593 | U_CHAR *name; |
1594 | ||
1595 | name = (U_CHAR *) alloca (temp->length + 1); | |
1596 | (void) strncpy (name, temp->name, temp->length); | |
1597 | name[temp->length] = '\0'; | |
1598 | cpp_error (pfile, | |
1599 | "duplicate argument name `%s' in `#define'", name); | |
1600 | goto nope; | |
1601 | } | |
1602 | } | |
1603 | } | |
1604 | ||
1605 | ++bp; /* skip paren */ | |
782331f4 | 1606 | SKIP_WHITE_SPACE (bp); |
0f41302f | 1607 | /* now everything from bp before limit is the definition. */ |
7f2935c7 PB |
1608 | defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs); |
1609 | defn->rest_args = rest_args; | |
1610 | ||
1611 | /* Now set defn->args.argnames to the result of concatenating | |
1612 | the argument names in reverse order | |
1613 | with comma-space between them. */ | |
1614 | defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1); | |
1615 | { | |
1616 | struct arglist *temp; | |
1617 | int i = 0; | |
1618 | for (temp = arg_ptrs; temp; temp = temp->next) { | |
1619 | bcopy (temp->name, &defn->args.argnames[i], temp->length); | |
1620 | i += temp->length; | |
1621 | if (temp->next != 0) { | |
1622 | defn->args.argnames[i++] = ','; | |
1623 | defn->args.argnames[i++] = ' '; | |
1624 | } | |
1625 | } | |
1626 | defn->args.argnames[i] = 0; | |
1627 | } | |
1628 | } else { | |
6be492ab PB |
1629 | /* Simple expansion or empty definition. */ |
1630 | ||
1631 | if (bp < limit) | |
1632 | { | |
1633 | if (is_hor_space[*bp]) { | |
1634 | bp++; | |
1635 | SKIP_WHITE_SPACE (bp); | |
1636 | } else { | |
1637 | switch (*bp) { | |
1638 | case '!': case '"': case '#': case '%': case '&': case '\'': | |
1639 | case ')': case '*': case '+': case ',': case '-': case '.': | |
1640 | case '/': case ':': case ';': case '<': case '=': case '>': | |
1641 | case '?': case '[': case '\\': case ']': case '^': case '{': | |
1642 | case '|': case '}': case '~': | |
1643 | cpp_warning (pfile, "missing white space after `#define %.*s'", | |
1644 | sym_length, symname); | |
1645 | break; | |
1646 | ||
1647 | default: | |
1648 | cpp_pedwarn (pfile, "missing white space after `#define %.*s'", | |
1649 | sym_length, symname); | |
1650 | break; | |
1651 | } | |
1652 | } | |
1653 | } | |
0f41302f | 1654 | /* now everything from bp before limit is the definition. */ |
7f2935c7 PB |
1655 | defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR); |
1656 | defn->args.argnames = (U_CHAR *) ""; | |
1657 | } | |
1658 | ||
1659 | defn->line = line; | |
1660 | defn->file = file; | |
1661 | ||
1662 | /* OP is null if this is a predefinition */ | |
1663 | defn->predefined = predefinition; | |
1664 | mdef.defn = defn; | |
1665 | mdef.symnam = symname; | |
1666 | mdef.symlen = sym_length; | |
1667 | ||
1668 | return mdef; | |
1669 | ||
1670 | nope: | |
1671 | mdef.defn = 0; | |
1672 | return mdef; | |
1673 | } | |
1674 | ||
1675 | /* Check a purported macro name SYMNAME, and yield its length. | |
1676 | USAGE is the kind of name this is intended for. */ | |
1677 | ||
1678 | static int | |
1679 | check_macro_name (pfile, symname, usage) | |
1680 | cpp_reader *pfile; | |
1681 | U_CHAR *symname; | |
1682 | char *usage; | |
1683 | { | |
1684 | U_CHAR *p; | |
1685 | int sym_length; | |
1686 | ||
1687 | for (p = symname; is_idchar[*p]; p++) | |
1688 | ; | |
1689 | sym_length = p - symname; | |
4c8cc616 RK |
1690 | if (sym_length == 0 |
1691 | || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"'))) | |
7f2935c7 PB |
1692 | cpp_error (pfile, "invalid %s name", usage); |
1693 | else if (!is_idstart[*symname]) { | |
0f41302f | 1694 | U_CHAR *msg; /* what pain... */ |
7f2935c7 PB |
1695 | msg = (U_CHAR *) alloca (sym_length + 1); |
1696 | bcopy (symname, msg, sym_length); | |
1697 | msg[sym_length] = 0; | |
1698 | cpp_error (pfile, "invalid %s name `%s'", usage, msg); | |
1699 | } else { | |
1700 | if (! strncmp (symname, "defined", 7) && sym_length == 7) | |
1701 | cpp_error (pfile, "invalid %s name `defined'", usage); | |
1702 | } | |
1703 | return sym_length; | |
1704 | } | |
1705 | ||
0f41302f MS |
1706 | /* Return zero if two DEFINITIONs are isomorphic. */ |
1707 | ||
7f2935c7 PB |
1708 | static int |
1709 | compare_defs (d1, d2) | |
1710 | DEFINITION *d1, *d2; | |
1711 | { | |
1712 | register struct reflist *a1, *a2; | |
1713 | register U_CHAR *p1 = d1->expansion; | |
1714 | register U_CHAR *p2 = d2->expansion; | |
1715 | int first = 1; | |
1716 | ||
1717 | if (d1->nargs != d2->nargs) | |
1718 | return 1; | |
1719 | if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames)) | |
1720 | return 1; | |
1721 | for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2; | |
1722 | a1 = a1->next, a2 = a2->next) { | |
1723 | if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars)) | |
1724 | || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0)) | |
1725 | || a1->argno != a2->argno | |
1726 | || a1->stringify != a2->stringify | |
1727 | || a1->raw_before != a2->raw_before | |
1728 | || a1->raw_after != a2->raw_after) | |
1729 | return 1; | |
1730 | first = 0; | |
1731 | p1 += a1->nchars; | |
1732 | p2 += a2->nchars; | |
1733 | } | |
1734 | if (a1 != a2) | |
1735 | return 1; | |
1736 | if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion), | |
1737 | p2, d2->length - (p2 - d2->expansion), 1)) | |
1738 | return 1; | |
1739 | return 0; | |
1740 | } | |
1741 | ||
1742 | /* Return 1 if two parts of two macro definitions are effectively different. | |
1743 | One of the parts starts at BEG1 and has LEN1 chars; | |
1744 | the other has LEN2 chars at BEG2. | |
1745 | Any sequence of whitespace matches any other sequence of whitespace. | |
1746 | FIRST means these parts are the first of a macro definition; | |
1747 | so ignore leading whitespace entirely. | |
1748 | LAST means these parts are the last of a macro definition; | |
1749 | so ignore trailing whitespace entirely. */ | |
1750 | ||
1751 | static int | |
1752 | comp_def_part (first, beg1, len1, beg2, len2, last) | |
1753 | int first; | |
1754 | U_CHAR *beg1, *beg2; | |
1755 | int len1, len2; | |
1756 | int last; | |
1757 | { | |
1758 | register U_CHAR *end1 = beg1 + len1; | |
1759 | register U_CHAR *end2 = beg2 + len2; | |
1760 | if (first) { | |
1761 | while (beg1 != end1 && is_space[*beg1]) beg1++; | |
1762 | while (beg2 != end2 && is_space[*beg2]) beg2++; | |
1763 | } | |
1764 | if (last) { | |
1765 | while (beg1 != end1 && is_space[end1[-1]]) end1--; | |
1766 | while (beg2 != end2 && is_space[end2[-1]]) end2--; | |
1767 | } | |
1768 | while (beg1 != end1 && beg2 != end2) { | |
1769 | if (is_space[*beg1] && is_space[*beg2]) { | |
1770 | while (beg1 != end1 && is_space[*beg1]) beg1++; | |
1771 | while (beg2 != end2 && is_space[*beg2]) beg2++; | |
1772 | } else if (*beg1 == *beg2) { | |
1773 | beg1++; beg2++; | |
1774 | } else break; | |
1775 | } | |
1776 | return (beg1 != end1) || (beg2 != end2); | |
1777 | } | |
1778 | ||
1779 | /* Process a #define command. | |
1780 | BUF points to the contents of the #define command, as a contiguous string. | |
1781 | LIMIT points to the first character past the end of the definition. | |
1782 | KEYWORD is the keyword-table entry for #define, | |
1783 | or NULL for a "predefined" macro. */ | |
1784 | ||
1785 | static int | |
1786 | do_define (pfile, keyword, buf, limit) | |
1787 | cpp_reader *pfile; | |
1788 | struct directive *keyword; | |
1789 | U_CHAR *buf, *limit; | |
1790 | { | |
1791 | int hashcode; | |
1792 | MACRODEF mdef; | |
1793 | HASHNODE *hp; | |
1794 | ||
1795 | #if 0 | |
1796 | /* If this is a precompiler run (with -pcp) pass thru #define commands. */ | |
1797 | if (pcp_outfile && keyword) | |
1798 | pass_thru_directive (buf, limit, pfile, keyword); | |
1799 | #endif | |
1800 | ||
1801 | mdef = create_definition (buf, limit, pfile, keyword == NULL); | |
1802 | if (mdef.defn == 0) | |
1803 | goto nope; | |
1804 | ||
1805 | hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE); | |
1806 | ||
1807 | if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL) | |
1808 | { | |
1809 | int ok = 0; | |
1810 | /* Redefining a precompiled key is ok. */ | |
1811 | if (hp->type == T_PCSTRING) | |
1812 | ok = 1; | |
1813 | /* Redefining a macro is ok if the definitions are the same. */ | |
1814 | else if (hp->type == T_MACRO) | |
1815 | ok = ! compare_defs (mdef.defn, hp->value.defn); | |
1816 | /* Redefining a constant is ok with -D. */ | |
1817 | else if (hp->type == T_CONST) | |
1818 | ok = ! CPP_OPTIONS (pfile)->done_initializing; | |
1819 | /* Print the warning if it's not ok. */ | |
1820 | if (!ok) | |
1821 | { | |
0f41302f | 1822 | U_CHAR *msg; /* what pain... */ |
7f2935c7 PB |
1823 | |
1824 | /* If we are passing through #define and #undef directives, do | |
1825 | that for this re-definition now. */ | |
1826 | if (CPP_OPTIONS (pfile)->debug_output && keyword) | |
1827 | pass_thru_directive (buf, limit, pfile, keyword); | |
1828 | ||
1829 | msg = (U_CHAR *) alloca (mdef.symlen + 22); | |
1830 | *msg = '`'; | |
1831 | bcopy (mdef.symnam, msg + 1, mdef.symlen); | |
1832 | strcpy ((char *) (msg + mdef.symlen + 1), "' redefined"); | |
1833 | cpp_pedwarn (pfile, msg); | |
1834 | if (hp->type == T_MACRO) | |
1835 | cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line, | |
1836 | "this is the location of the previous definition"); | |
1837 | } | |
1838 | /* Replace the old definition. */ | |
1839 | hp->type = T_MACRO; | |
1840 | hp->value.defn = mdef.defn; | |
1841 | } | |
1842 | else | |
1843 | { | |
1844 | /* If we are passing through #define and #undef directives, do | |
1845 | that for this new definition now. */ | |
1846 | if (CPP_OPTIONS (pfile)->debug_output && keyword) | |
1847 | pass_thru_directive (buf, limit, pfile, keyword); | |
1848 | install (mdef.symnam, mdef.symlen, T_MACRO, 0, | |
1849 | (char *) mdef.defn, hashcode); | |
1850 | } | |
1851 | ||
1852 | return 0; | |
1853 | ||
1854 | nope: | |
1855 | ||
1856 | return 1; | |
1857 | } | |
1858 | ||
1859 | /* This structure represents one parsed argument in a macro call. | |
1860 | `raw' points to the argument text as written (`raw_length' is its length). | |
1861 | `expanded' points to the argument's macro-expansion | |
1862 | (its length is `expand_length'). | |
1863 | `stringified_length' is the length the argument would have | |
1864 | if stringified. | |
1865 | `use_count' is the number of times this macro arg is substituted | |
1866 | into the macro. If the actual use count exceeds 10, | |
0f41302f | 1867 | the value stored is 10. */ |
7f2935c7 PB |
1868 | |
1869 | /* raw and expanded are relative to ARG_BASE */ | |
1870 | #define ARG_BASE ((pfile)->token_buffer) | |
1871 | ||
1872 | struct argdata { | |
1873 | /* Strings relative to pfile->token_buffer */ | |
1874 | long raw, expanded, stringified; | |
1875 | int raw_length, expand_length; | |
1876 | int stringified_length; | |
1877 | char newlines; | |
7f2935c7 PB |
1878 | char use_count; |
1879 | }; | |
1880 | ||
d013f05e PB |
1881 | /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack. |
1882 | If BUFFER != NULL, then use the LENGTH characters in BUFFER | |
1883 | as the new input buffer. | |
0f41302f | 1884 | Return the new buffer, or NULL on failure. */ |
d013f05e | 1885 | |
0f41302f | 1886 | cpp_buffer * |
7f2935c7 PB |
1887 | cpp_push_buffer (pfile, buffer, length) |
1888 | cpp_reader *pfile; | |
1889 | U_CHAR *buffer; | |
1890 | long length; | |
1891 | { | |
22bbceaf | 1892 | register cpp_buffer *buf = CPP_BUFFER (pfile); |
7f2935c7 | 1893 | if (buf == pfile->buffer_stack) |
e2f79f3c PB |
1894 | { |
1895 | cpp_fatal (pfile, "%s: macro or `#include' recursion too deep", | |
1896 | buf->fname); | |
1897 | return NULL; | |
1898 | } | |
22bbceaf | 1899 | buf--; |
7f2935c7 PB |
1900 | bzero ((char *) buf, sizeof (cpp_buffer)); |
1901 | CPP_BUFFER (pfile) = buf; | |
7f2935c7 PB |
1902 | buf->if_stack = pfile->if_stack; |
1903 | buf->cleanup = null_cleanup; | |
1904 | buf->underflow = null_underflow; | |
1905 | buf->buf = buf->cur = buffer; | |
1906 | buf->alimit = buf->rlimit = buffer + length; | |
1907 | ||
1908 | return buf; | |
1909 | } | |
1910 | ||
0f41302f | 1911 | cpp_buffer * |
7f2935c7 PB |
1912 | cpp_pop_buffer (pfile) |
1913 | cpp_reader *pfile; | |
1914 | { | |
1915 | cpp_buffer *buf = CPP_BUFFER (pfile); | |
7f2935c7 PB |
1916 | (*buf->cleanup) (buf, pfile); |
1917 | return ++CPP_BUFFER (pfile); | |
7f2935c7 PB |
1918 | } |
1919 | ||
1920 | /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer. | |
0f41302f | 1921 | Pop the buffer when done. */ |
7f2935c7 PB |
1922 | |
1923 | void | |
1924 | cpp_scan_buffer (pfile) | |
1925 | cpp_reader *pfile; | |
1926 | { | |
1927 | cpp_buffer *buffer = CPP_BUFFER (pfile); | |
1928 | for (;;) | |
1929 | { | |
1930 | enum cpp_token token = cpp_get_token (pfile); | |
0f41302f | 1931 | if (token == CPP_EOF) /* Should not happen ... */ |
7f2935c7 PB |
1932 | break; |
1933 | if (token == CPP_POP && CPP_BUFFER (pfile) == buffer) | |
1934 | { | |
1935 | cpp_pop_buffer (pfile); | |
1936 | break; | |
1937 | } | |
1938 | } | |
1939 | } | |
1940 | ||
1941 | /* | |
782331f4 | 1942 | * Rescan a string (which may have escape marks) into pfile's buffer. |
7f2935c7 PB |
1943 | * Place the result in pfile->token_buffer. |
1944 | * | |
1945 | * The input is copied before it is scanned, so it is safe to pass | |
1946 | * it something from the token_buffer that will get overwritten | |
1947 | * (because it follows CPP_WRITTEN). This is used by do_include. | |
7f2935c7 PB |
1948 | */ |
1949 | ||
782331f4 | 1950 | static void |
7f2935c7 PB |
1951 | cpp_expand_to_buffer (pfile, buf, length) |
1952 | cpp_reader *pfile; | |
1953 | U_CHAR *buf; | |
1954 | int length; | |
1955 | { | |
1956 | register cpp_buffer *ip; | |
1957 | cpp_buffer obuf; | |
1958 | U_CHAR *limit = buf + length; | |
1959 | U_CHAR *buf1; | |
1960 | #if 0 | |
1961 | int odepth = indepth; | |
1962 | #endif | |
1963 | ||
1964 | if (length < 0) | |
1965 | abort (); | |
1966 | ||
1967 | /* Set up the input on the input stack. */ | |
1968 | ||
1969 | buf1 = (U_CHAR *) alloca (length + 1); | |
1970 | { | |
1971 | register U_CHAR *p1 = buf; | |
1972 | register U_CHAR *p2 = buf1; | |
1973 | ||
1974 | while (p1 != limit) | |
1975 | *p2++ = *p1++; | |
1976 | } | |
1977 | buf1[length] = 0; | |
1978 | ||
1979 | ip = cpp_push_buffer (pfile, buf1, length); | |
e2f79f3c PB |
1980 | if (ip == NULL) |
1981 | return; | |
782331f4 | 1982 | ip->has_escapes = 1; |
7f2935c7 PB |
1983 | #if 0 |
1984 | ip->lineno = obuf.lineno = 1; | |
1985 | #endif | |
1986 | ||
1987 | /* Scan the input, create the output. */ | |
1988 | cpp_scan_buffer (pfile); | |
1989 | ||
1990 | #if 0 | |
1991 | if (indepth != odepth) | |
1992 | abort (); | |
1993 | #endif | |
1994 | ||
1995 | CPP_NUL_TERMINATE (pfile); | |
1996 | } | |
1997 | ||
1998 | \f | |
1999 | static void | |
2000 | adjust_position (buf, limit, linep, colp) | |
2001 | U_CHAR *buf; | |
2002 | U_CHAR *limit; | |
2003 | long *linep; | |
2004 | long *colp; | |
2005 | { | |
2006 | while (buf < limit) | |
2007 | { | |
2008 | U_CHAR ch = *buf++; | |
2009 | if (ch == '\n') | |
2010 | (*linep)++, (*colp) = 1; | |
2011 | else | |
2012 | (*colp)++; | |
2013 | } | |
2014 | } | |
2015 | ||
0f41302f | 2016 | /* Move line_base forward, updating lineno and colno. */ |
7f2935c7 PB |
2017 | |
2018 | static void | |
2019 | update_position (pbuf) | |
2020 | register cpp_buffer *pbuf; | |
2021 | { | |
2022 | unsigned char *old_pos = pbuf->buf + pbuf->line_base; | |
2023 | unsigned char *new_pos = pbuf->cur; | |
2024 | register struct parse_marker *mark; | |
2025 | for (mark = pbuf->marks; mark != NULL; mark = mark->next) | |
2026 | { | |
2027 | if (pbuf->buf + mark->position < new_pos) | |
2028 | new_pos = pbuf->buf + mark->position; | |
2029 | } | |
2030 | pbuf->line_base += new_pos - old_pos; | |
2031 | adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno); | |
2032 | } | |
2033 | ||
2034 | void | |
2035 | cpp_buf_line_and_col (pbuf, linep, colp) | |
2036 | register cpp_buffer *pbuf; | |
2037 | long *linep, *colp; | |
2038 | { | |
2039 | long dummy; | |
2040 | if (colp == NULL) | |
2041 | colp = &dummy; | |
2042 | if (pbuf) | |
2043 | { | |
2044 | *linep = pbuf->lineno; | |
2045 | *colp = pbuf->colno; | |
2046 | adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp); | |
2047 | } | |
2048 | else | |
2049 | { | |
2050 | *linep = 0; | |
2051 | *colp = 0; | |
2052 | } | |
2053 | } | |
2054 | ||
0f41302f | 2055 | /* Return the cpp_buffer that corresponds to a file (not a macro). */ |
7f2935c7 | 2056 | |
0f41302f | 2057 | cpp_buffer * |
7f2935c7 PB |
2058 | cpp_file_buffer (pfile) |
2059 | cpp_reader *pfile; | |
2060 | { | |
22bbceaf | 2061 | cpp_buffer *ip = CPP_BUFFER (pfile); |
7f2935c7 | 2062 | |
22bbceaf | 2063 | for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip)) |
7f2935c7 PB |
2064 | if (ip->fname != NULL) |
2065 | return ip; | |
2066 | return NULL; | |
2067 | } | |
2068 | ||
2069 | static long | |
2070 | count_newlines (buf, limit) | |
2071 | register U_CHAR *buf; | |
2072 | register U_CHAR *limit; | |
2073 | { | |
2074 | register long count = 0; | |
2075 | while (buf < limit) | |
2076 | { | |
2077 | U_CHAR ch = *buf++; | |
2078 | if (ch == '\n') | |
2079 | count++; | |
2080 | } | |
2081 | return count; | |
2082 | } | |
2083 | ||
2084 | /* | |
2085 | * write out a #line command, for instance, after an #include file. | |
2086 | * If CONDITIONAL is nonzero, we can omit the #line if it would | |
2087 | * appear to be a no-op, and we can output a few newlines instead | |
2088 | * if we want to increase the line number by a small amount. | |
2089 | * FILE_CHANGE says whether we are entering a file, leaving, or neither. | |
2090 | */ | |
2091 | ||
2092 | static void | |
2093 | output_line_command (pfile, conditional, file_change) | |
2094 | cpp_reader *pfile; | |
2095 | int conditional; | |
2096 | enum file_change_code file_change; | |
2097 | { | |
2098 | int len; | |
2099 | char *line_cmd_buf, *line_end; | |
2100 | long line, col; | |
2101 | cpp_buffer *ip = CPP_BUFFER (pfile); | |
2102 | ||
e2f79f3c | 2103 | if (ip->fname == NULL) |
7f2935c7 | 2104 | return; |
7f2935c7 PB |
2105 | |
2106 | update_position (ip); | |
e2f79f3c PB |
2107 | |
2108 | if (CPP_OPTIONS (pfile)->no_line_commands | |
2109 | || CPP_OPTIONS (pfile)->no_output) | |
2110 | return; | |
2111 | ||
7f2935c7 PB |
2112 | line = CPP_BUFFER (pfile)->lineno; |
2113 | col = CPP_BUFFER (pfile)->colno; | |
2114 | adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col); | |
2115 | ||
a8259c76 RK |
2116 | if (CPP_OPTIONS (pfile)->no_line_commands) |
2117 | return; | |
2118 | ||
7f2935c7 PB |
2119 | if (conditional) { |
2120 | if (line == pfile->lineno) | |
2121 | return; | |
2122 | ||
2123 | /* If the inherited line number is a little too small, | |
2124 | output some newlines instead of a #line command. */ | |
2125 | if (line > pfile->lineno && line < pfile->lineno + 8) { | |
2126 | CPP_RESERVE (pfile, 20); | |
2127 | while (line > pfile->lineno) { | |
2128 | CPP_PUTC_Q (pfile, '\n'); | |
2129 | pfile->lineno++; | |
2130 | } | |
2131 | return; | |
2132 | } | |
2133 | } | |
2134 | ||
2135 | #if 0 | |
2136 | /* Don't output a line number of 0 if we can help it. */ | |
2137 | if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length | |
2138 | && *ip->bufp == '\n') { | |
2139 | ip->lineno++; | |
2140 | ip->bufp++; | |
2141 | } | |
2142 | #endif | |
2143 | ||
2144 | CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50); | |
2145 | { | |
2146 | #ifdef OUTPUT_LINE_COMMANDS | |
2147 | static char sharp_line[] = "#line "; | |
2148 | #else | |
2149 | static char sharp_line[] = "# "; | |
2150 | #endif | |
2151 | CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1); | |
2152 | } | |
2153 | ||
e9a25f70 | 2154 | sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line); |
7f2935c7 PB |
2155 | CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile))); |
2156 | ||
2157 | quote_string (pfile, ip->nominal_fname); | |
2158 | if (file_change != same_file) { | |
2159 | CPP_PUTC_Q (pfile, ' '); | |
2160 | CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2'); | |
2161 | } | |
2162 | /* Tell cc1 if following text comes from a system header file. */ | |
2163 | if (ip->system_header_p) { | |
2164 | CPP_PUTC_Q (pfile, ' '); | |
2165 | CPP_PUTC_Q (pfile, '3'); | |
2166 | } | |
2167 | #ifndef NO_IMPLICIT_EXTERN_C | |
2168 | /* Tell cc1plus if following text should be treated as C. */ | |
2169 | if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) { | |
2170 | CPP_PUTC_Q (pfile, ' '); | |
2171 | CPP_PUTC_Q (pfile, '4'); | |
2172 | } | |
2173 | #endif | |
2174 | CPP_PUTC_Q (pfile, '\n'); | |
2175 | pfile->lineno = line; | |
2176 | } | |
2177 | \f | |
2178 | /* | |
782331f4 | 2179 | * Parse a macro argument and append the info on PFILE's token_buffer. |
7f2935c7 PB |
2180 | * REST_ARGS means to absorb the rest of the args. |
2181 | * Return nonzero to indicate a syntax error. | |
2182 | */ | |
2183 | ||
2184 | static enum cpp_token | |
2185 | macarg (pfile, rest_args) | |
2186 | cpp_reader *pfile; | |
2187 | int rest_args; | |
2188 | { | |
2189 | int paren = 0; | |
2190 | enum cpp_token token; | |
2191 | long arg_start = CPP_WRITTEN (pfile); | |
2192 | char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments; | |
2193 | CPP_OPTIONS (pfile)->put_out_comments = 0; | |
2194 | ||
2195 | /* Try to parse as much of the argument as exists at this | |
2196 | input stack level. */ | |
2197 | pfile->no_macro_expand++; | |
2198 | for (;;) | |
2199 | { | |
2200 | token = cpp_get_token (pfile); | |
2201 | switch (token) | |
2202 | { | |
2203 | case CPP_EOF: | |
2204 | goto done; | |
2205 | case CPP_POP: | |
2206 | /* If we've hit end of file, it's an error (reported by caller). | |
2207 | Ditto if it's the end of cpp_expand_to_buffer text. | |
2208 | If we've hit end of macro, just continue. */ | |
2209 | if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile))) | |
2210 | goto done; | |
2211 | break; | |
2212 | case CPP_LPAREN: | |
2213 | paren++; | |
2214 | break; | |
2215 | case CPP_RPAREN: | |
2216 | if (--paren < 0) | |
2217 | goto found; | |
2218 | break; | |
2219 | case CPP_COMMA: | |
2220 | /* if we've returned to lowest level and | |
2221 | we aren't absorbing all args */ | |
2222 | if (paren == 0 && rest_args == 0) | |
2223 | goto found; | |
2224 | break; | |
2225 | found: | |
0f41302f | 2226 | /* Remove ',' or ')' from argument buffer. */ |
7f2935c7 PB |
2227 | CPP_ADJUST_WRITTEN (pfile, -1); |
2228 | goto done; | |
2229 | default: ; | |
2230 | } | |
2231 | } | |
2232 | ||
2233 | done: | |
2234 | CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments; | |
2235 | pfile->no_macro_expand--; | |
2236 | ||
2237 | return token; | |
2238 | } | |
2239 | \f | |
2240 | /* Turn newlines to spaces in the string of length LENGTH at START, | |
2241 | except inside of string constants. | |
2242 | The string is copied into itself with its beginning staying fixed. */ | |
2243 | ||
2244 | static int | |
2245 | change_newlines (start, length) | |
2246 | U_CHAR *start; | |
2247 | int length; | |
2248 | { | |
2249 | register U_CHAR *ibp; | |
2250 | register U_CHAR *obp; | |
2251 | register U_CHAR *limit; | |
2252 | register int c; | |
2253 | ||
2254 | ibp = start; | |
2255 | limit = start + length; | |
2256 | obp = start; | |
2257 | ||
2258 | while (ibp < limit) { | |
2259 | *obp++ = c = *ibp++; | |
2260 | switch (c) { | |
2261 | ||
2262 | case '\'': | |
2263 | case '\"': | |
2264 | /* Notice and skip strings, so that we don't delete newlines in them. */ | |
2265 | { | |
2266 | int quotec = c; | |
2267 | while (ibp < limit) { | |
2268 | *obp++ = c = *ibp++; | |
2269 | if (c == quotec) | |
2270 | break; | |
2271 | if (c == '\n' && quotec == '\'') | |
2272 | break; | |
2273 | } | |
2274 | } | |
2275 | break; | |
2276 | } | |
2277 | } | |
2278 | ||
2279 | return obp - start; | |
2280 | } | |
2281 | ||
2282 | \f | |
2283 | static struct tm * | |
2284 | timestamp (pfile) | |
2285 | cpp_reader *pfile; | |
2286 | { | |
2287 | if (!pfile->timebuf) { | |
0f41302f | 2288 | time_t t = time ((time_t *) 0); |
7f2935c7 PB |
2289 | pfile->timebuf = localtime (&t); |
2290 | } | |
2291 | return pfile->timebuf; | |
2292 | } | |
2293 | ||
2294 | static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
2295 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", | |
2296 | }; | |
2297 | ||
2298 | /* | |
2299 | * expand things like __FILE__. Place the expansion into the output | |
2300 | * buffer *without* rescanning. | |
2301 | */ | |
2302 | ||
2303 | static void | |
2304 | special_symbol (hp, pfile) | |
2305 | HASHNODE *hp; | |
2306 | cpp_reader *pfile; | |
2307 | { | |
2308 | char *buf; | |
2309 | int i, len; | |
2310 | int true_indepth; | |
2311 | cpp_buffer *ip = NULL; | |
2312 | struct tm *timebuf; | |
2313 | ||
2314 | int paren = 0; /* For special `defined' keyword */ | |
2315 | ||
2316 | #if 0 | |
2317 | if (pcp_outfile && pcp_inside_if | |
2318 | && hp->type != T_SPEC_DEFINED && hp->type != T_CONST) | |
2319 | cpp_error (pfile, | |
2320 | "Predefined macro `%s' used inside `#if' during precompilation", | |
2321 | hp->name); | |
2322 | #endif | |
2323 | ||
2324 | for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip)) | |
2325 | { | |
d013f05e | 2326 | if (ip == CPP_NULL_BUFFER (pfile)) |
7f2935c7 PB |
2327 | { |
2328 | cpp_error (pfile, "cccp error: not in any file?!"); | |
2329 | return; /* the show must go on */ | |
2330 | } | |
2331 | if (ip->fname != NULL) | |
2332 | break; | |
2333 | } | |
2334 | ||
2335 | switch (hp->type) | |
2336 | { | |
2337 | case T_FILE: | |
2338 | case T_BASE_FILE: | |
2339 | { | |
2340 | char *string; | |
2341 | if (hp->type == T_BASE_FILE) | |
2342 | { | |
d013f05e | 2343 | while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile)) |
7f2935c7 PB |
2344 | ip = CPP_PREV_BUFFER (ip); |
2345 | } | |
2346 | string = ip->nominal_fname; | |
2347 | ||
2348 | if (!string) | |
2349 | string = ""; | |
2350 | CPP_RESERVE (pfile, 3 + 4 * strlen (string)); | |
2351 | quote_string (pfile, string); | |
2352 | return; | |
2353 | } | |
2354 | ||
2355 | case T_INCLUDE_LEVEL: | |
2356 | true_indepth = 0; | |
d013f05e PB |
2357 | ip = CPP_BUFFER (pfile); |
2358 | for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip)) | |
7f2935c7 PB |
2359 | if (ip->fname != NULL) |
2360 | true_indepth++; | |
2361 | ||
2362 | buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */ | |
2363 | sprintf (buf, "%d", true_indepth - 1); | |
2364 | break; | |
2365 | ||
2366 | case T_VERSION: | |
2367 | buf = (char *) alloca (3 + strlen (version_string)); | |
2368 | sprintf (buf, "\"%s\"", version_string); | |
2369 | break; | |
2370 | ||
2371 | #ifndef NO_BUILTIN_SIZE_TYPE | |
2372 | case T_SIZE_TYPE: | |
2373 | buf = SIZE_TYPE; | |
2374 | break; | |
2375 | #endif | |
2376 | ||
2377 | #ifndef NO_BUILTIN_PTRDIFF_TYPE | |
2378 | case T_PTRDIFF_TYPE: | |
2379 | buf = PTRDIFF_TYPE; | |
2380 | break; | |
2381 | #endif | |
2382 | ||
2383 | case T_WCHAR_TYPE: | |
2384 | buf = CPP_WCHAR_TYPE (pfile); | |
2385 | break; | |
2386 | ||
2387 | case T_USER_LABEL_PREFIX_TYPE: | |
2388 | buf = USER_LABEL_PREFIX; | |
2389 | break; | |
2390 | ||
2391 | case T_REGISTER_PREFIX_TYPE: | |
2392 | buf = REGISTER_PREFIX; | |
2393 | break; | |
2394 | ||
2395 | case T_CONST: | |
2396 | buf = (char *) alloca (4 * sizeof (int)); | |
2397 | sprintf (buf, "%d", hp->value.ival); | |
e9a25f70 JL |
2398 | #ifdef STDC_0_IN_SYSTEM_HEADERS |
2399 | if (ip->system_header_p | |
2400 | && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0 | |
2401 | && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1)) | |
2402 | strcpy (buf, "0"); | |
2403 | #endif | |
7f2935c7 PB |
2404 | #if 0 |
2405 | if (pcp_inside_if && pcp_outfile) | |
2406 | /* Output a precondition for this macro use */ | |
2407 | fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival); | |
2408 | #endif | |
2409 | break; | |
2410 | ||
2411 | case T_SPECLINE: | |
2412 | { | |
b13b05f6 PB |
2413 | long line = ip->lineno; |
2414 | long col = ip->colno; | |
7f2935c7 PB |
2415 | adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col); |
2416 | ||
2417 | buf = (char *) alloca (10); | |
e9a25f70 | 2418 | sprintf (buf, "%ld", line); |
7f2935c7 PB |
2419 | } |
2420 | break; | |
2421 | ||
2422 | case T_DATE: | |
2423 | case T_TIME: | |
2424 | buf = (char *) alloca (20); | |
2425 | timebuf = timestamp (pfile); | |
2426 | if (hp->type == T_DATE) | |
2427 | sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon], | |
2428 | timebuf->tm_mday, timebuf->tm_year + 1900); | |
2429 | else | |
2430 | sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min, | |
2431 | timebuf->tm_sec); | |
2432 | break; | |
2433 | ||
2434 | case T_SPEC_DEFINED: | |
2435 | buf = " 0 "; /* Assume symbol is not defined */ | |
2436 | ip = CPP_BUFFER (pfile); | |
2437 | SKIP_WHITE_SPACE (ip->cur); | |
2438 | if (*ip->cur == '(') | |
2439 | { | |
2440 | paren++; | |
2441 | ip->cur++; /* Skip over the paren */ | |
2442 | SKIP_WHITE_SPACE (ip->cur); | |
2443 | } | |
2444 | ||
2445 | if (!is_idstart[*ip->cur]) | |
2446 | goto oops; | |
4c8cc616 RK |
2447 | if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"')) |
2448 | goto oops; | |
7f2935c7 PB |
2449 | if (hp = cpp_lookup (pfile, ip->cur, -1, -1)) |
2450 | { | |
2451 | #if 0 | |
2452 | if (pcp_outfile && pcp_inside_if | |
2453 | && (hp->type == T_CONST | |
2454 | || (hp->type == T_MACRO && hp->value.defn->predefined))) | |
0f41302f | 2455 | /* Output a precondition for this macro use. */ |
7f2935c7 PB |
2456 | fprintf (pcp_outfile, "#define %s\n", hp->name); |
2457 | #endif | |
2458 | buf = " 1 "; | |
2459 | } | |
2460 | #if 0 | |
2461 | else | |
2462 | if (pcp_outfile && pcp_inside_if) | |
2463 | { | |
2464 | /* Output a precondition for this macro use */ | |
2465 | U_CHAR *cp = ip->bufp; | |
2466 | fprintf (pcp_outfile, "#undef "); | |
2467 | while (is_idchar[*cp]) /* Ick! */ | |
2468 | fputc (*cp++, pcp_outfile); | |
2469 | putc ('\n', pcp_outfile); | |
2470 | } | |
2471 | #endif | |
2472 | while (is_idchar[*ip->cur]) | |
2473 | ++ip->cur; | |
2474 | SKIP_WHITE_SPACE (ip->cur); | |
2475 | if (paren) | |
2476 | { | |
2477 | if (*ip->cur != ')') | |
2478 | goto oops; | |
2479 | ++ip->cur; | |
2480 | } | |
2481 | break; | |
2482 | ||
2483 | oops: | |
2484 | ||
2485 | cpp_error (pfile, "`defined' without an identifier"); | |
2486 | break; | |
2487 | ||
2488 | default: | |
2489 | cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */ | |
2490 | abort (); | |
2491 | } | |
2492 | len = strlen (buf); | |
2493 | CPP_RESERVE (pfile, len + 1); | |
2494 | CPP_PUTS_Q (pfile, buf, len); | |
2495 | CPP_NUL_TERMINATE_Q (pfile); | |
2496 | ||
2497 | return; | |
2498 | } | |
2499 | ||
d013f05e PB |
2500 | /* Write out a #define command for the special named MACRO_NAME |
2501 | to PFILE's token_buffer. */ | |
2502 | ||
2503 | static void | |
2504 | dump_special_to_buffer (pfile, macro_name) | |
2505 | cpp_reader *pfile; | |
2506 | char *macro_name; | |
2507 | { | |
2508 | static char define_directive[] = "#define "; | |
2509 | int macro_name_length = strlen (macro_name); | |
2510 | output_line_command (pfile, 0, same_file); | |
2511 | CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length); | |
2512 | CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1); | |
2513 | CPP_PUTS_Q (pfile, macro_name, macro_name_length); | |
2514 | CPP_PUTC_Q (pfile, ' '); | |
2515 | cpp_expand_to_buffer (pfile, macro_name, macro_name_length); | |
2516 | CPP_PUTC (pfile, '\n'); | |
2517 | } | |
2518 | ||
7f2935c7 PB |
2519 | /* Initialize the built-in macros. */ |
2520 | ||
2521 | static void | |
2522 | initialize_builtins (pfile) | |
2523 | cpp_reader *pfile; | |
2524 | { | |
e9a25f70 JL |
2525 | install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1); |
2526 | install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1); | |
2527 | install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1); | |
2528 | install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1); | |
2529 | install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1); | |
2530 | install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1); | |
7f2935c7 | 2531 | #ifndef NO_BUILTIN_SIZE_TYPE |
e9a25f70 | 2532 | install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1); |
7f2935c7 PB |
2533 | #endif |
2534 | #ifndef NO_BUILTIN_PTRDIFF_TYPE | |
e9a25f70 | 2535 | install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1); |
7f2935c7 | 2536 | #endif |
e9a25f70 JL |
2537 | install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1); |
2538 | install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1); | |
2539 | install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1); | |
2540 | install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1); | |
7f2935c7 | 2541 | if (!CPP_TRADITIONAL (pfile)) |
e9a25f70 | 2542 | install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1); |
7f2935c7 | 2543 | if (CPP_OPTIONS (pfile)->objc) |
e9a25f70 | 2544 | install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1); |
7f2935c7 PB |
2545 | /* This is supplied using a -D by the compiler driver |
2546 | so that it is present only when truly compiling with GNU C. */ | |
2547 | /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */ | |
2548 | ||
2549 | if (CPP_OPTIONS (pfile)->debug_output) | |
2550 | { | |
d013f05e PB |
2551 | dump_special_to_buffer (pfile, "__BASE_FILE__"); |
2552 | dump_special_to_buffer (pfile, "__VERSION__"); | |
7f2935c7 | 2553 | #ifndef NO_BUILTIN_SIZE_TYPE |
d013f05e | 2554 | dump_special_to_buffer (pfile, "__SIZE_TYPE__"); |
7f2935c7 | 2555 | #endif |
7f2935c7 | 2556 | #ifndef NO_BUILTIN_PTRDIFF_TYPE |
d013f05e | 2557 | dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__"); |
7f2935c7 | 2558 | #endif |
d013f05e PB |
2559 | dump_special_to_buffer (pfile, "__WCHAR_TYPE__"); |
2560 | dump_special_to_buffer (pfile, "__DATE__"); | |
2561 | dump_special_to_buffer (pfile, "__TIME__"); | |
7f2935c7 | 2562 | if (!CPP_TRADITIONAL (pfile)) |
d013f05e | 2563 | dump_special_to_buffer (pfile, "__STDC__"); |
7f2935c7 | 2564 | if (CPP_OPTIONS (pfile)->objc) |
d013f05e | 2565 | dump_special_to_buffer (pfile, "__OBJC__"); |
7f2935c7 PB |
2566 | } |
2567 | } | |
2568 | \f | |
2569 | /* Return 1 iff a token ending in C1 followed directly by a token C2 | |
0f41302f | 2570 | could cause mis-tokenization. */ |
7f2935c7 PB |
2571 | |
2572 | static int | |
2573 | unsafe_chars (c1, c2) | |
2574 | int c1, c2; | |
2575 | { | |
2576 | switch (c1) | |
2577 | { | |
2578 | case '+': case '-': | |
2579 | if (c2 == c1 || c2 == '=') | |
2580 | return 1; | |
2581 | goto letter; | |
2582 | case '.': | |
2583 | case '0': case '1': case '2': case '3': case '4': | |
2584 | case '5': case '6': case '7': case '8': case '9': | |
641d4443 | 2585 | case 'e': case 'E': case 'p': case 'P': |
7f2935c7 PB |
2586 | if (c2 == '-' || c2 == '+') |
2587 | return 1; /* could extend a pre-processing number */ | |
2588 | goto letter; | |
2589 | case 'L': | |
2590 | if (c2 == '\'' || c2 == '\"') | |
0f41302f | 2591 | return 1; /* Could turn into L"xxx" or L'xxx'. */ |
7f2935c7 PB |
2592 | goto letter; |
2593 | letter: | |
2594 | case '_': | |
2595 | case 'a': case 'b': case 'c': case 'd': case 'f': | |
2596 | case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': | |
641d4443 | 2597 | case 'm': case 'n': case 'o': case 'q': case 'r': |
7f2935c7 PB |
2598 | case 's': case 't': case 'u': case 'v': case 'w': case 'x': |
2599 | case 'y': case 'z': | |
2600 | case 'A': case 'B': case 'C': case 'D': case 'F': | |
2601 | case 'G': case 'H': case 'I': case 'J': case 'K': | |
641d4443 | 2602 | case 'M': case 'N': case 'O': case 'Q': case 'R': |
7f2935c7 PB |
2603 | case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': |
2604 | case 'Y': case 'Z': | |
0f41302f | 2605 | /* We're in the middle of either a name or a pre-processing number. */ |
7f2935c7 PB |
2606 | return (is_idchar[c2] || c2 == '.'); |
2607 | case '<': case '>': case '!': case '%': case '#': case ':': | |
2608 | case '^': case '&': case '|': case '*': case '/': case '=': | |
2609 | return (c2 == c1 || c2 == '='); | |
2610 | } | |
2611 | return 0; | |
2612 | } | |
2613 | ||
2614 | /* Expand a macro call. | |
2615 | HP points to the symbol that is the macro being called. | |
2616 | Put the result of expansion onto the input stack | |
2617 | so that subsequent input by our caller will use it. | |
2618 | ||
2619 | If macro wants arguments, caller has already verified that | |
2620 | an argument list follows; arguments come from the input stack. */ | |
2621 | ||
2622 | static void | |
2623 | macroexpand (pfile, hp) | |
2624 | cpp_reader *pfile; | |
2625 | HASHNODE *hp; | |
2626 | { | |
2627 | int nargs; | |
2628 | DEFINITION *defn = hp->value.defn; | |
2629 | register U_CHAR *xbuf; | |
3232050c | 2630 | long start_line, start_column; |
7f2935c7 PB |
2631 | int xbuf_len; |
2632 | struct argdata *args; | |
2633 | long old_written = CPP_WRITTEN (pfile); | |
2634 | #if 0 | |
2635 | int start_line = instack[indepth].lineno; | |
2636 | #endif | |
2637 | int rest_args, rest_zero; | |
2638 | register int i; | |
2639 | ||
2640 | #if 0 | |
2641 | CHECK_DEPTH (return;); | |
2642 | #endif | |
2643 | ||
2644 | #if 0 | |
2645 | /* This macro is being used inside a #if, which means it must be */ | |
2646 | /* recorded as a precondition. */ | |
2647 | if (pcp_inside_if && pcp_outfile && defn->predefined) | |
2648 | dump_single_macro (hp, pcp_outfile); | |
2649 | #endif | |
2650 | ||
782331f4 | 2651 | pfile->output_escapes++; |
3232050c | 2652 | cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column); |
e8037d57 | 2653 | |
7f2935c7 PB |
2654 | nargs = defn->nargs; |
2655 | ||
2656 | if (nargs >= 0) | |
2657 | { | |
7f2935c7 PB |
2658 | enum cpp_token token; |
2659 | ||
2660 | args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata)); | |
2661 | ||
2662 | for (i = 0; i < nargs; i++) | |
2663 | { | |
2664 | args[i].raw = args[i].expanded = 0; | |
2665 | args[i].raw_length = 0; | |
2666 | args[i].expand_length = args[i].stringified_length = -1; | |
2667 | args[i].use_count = 0; | |
2668 | } | |
2669 | ||
2670 | /* Parse all the macro args that are supplied. I counts them. | |
2671 | The first NARGS args are stored in ARGS. | |
2672 | The rest are discarded. If rest_args is set then we assume | |
0f41302f | 2673 | macarg absorbed the rest of the args. */ |
7f2935c7 PB |
2674 | i = 0; |
2675 | rest_args = 0; | |
2676 | rest_args = 0; | |
2677 | FORWARD(1); /* Discard the open-parenthesis before the first arg. */ | |
2678 | do | |
2679 | { | |
2680 | if (rest_args) | |
2681 | continue; | |
2682 | if (i < nargs || (nargs == 0 && i == 0)) | |
2683 | { | |
2684 | /* if we are working on last arg which absorbs rest of args... */ | |
2685 | if (i == nargs - 1 && defn->rest_args) | |
2686 | rest_args = 1; | |
2687 | args[i].raw = CPP_WRITTEN (pfile); | |
2688 | token = macarg (pfile, rest_args); | |
2689 | args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw; | |
2690 | args[i].newlines = 0; /* FIXME */ | |
2691 | } | |
2692 | else | |
2693 | token = macarg (pfile, 0); | |
2694 | if (token == CPP_EOF || token == CPP_POP) | |
2695 | { | |
3232050c | 2696 | cpp_error_with_line (pfile, start_line, start_column, |
e8037d57 PB |
2697 | "unterminated macro call"); |
2698 | return; | |
7f2935c7 PB |
2699 | } |
2700 | i++; | |
2701 | } while (token == CPP_COMMA); | |
2702 | ||
2703 | /* If we got one arg but it was just whitespace, call that 0 args. */ | |
2704 | if (i == 1) | |
2705 | { | |
2706 | register U_CHAR *bp = ARG_BASE + args[0].raw; | |
2707 | register U_CHAR *lim = bp + args[0].raw_length; | |
2708 | /* cpp.texi says for foo ( ) we provide one argument. | |
2709 | However, if foo wants just 0 arguments, treat this as 0. */ | |
2710 | if (nargs == 0) | |
2711 | while (bp != lim && is_space[*bp]) bp++; | |
2712 | if (bp == lim) | |
2713 | i = 0; | |
2714 | } | |
2715 | ||
2716 | /* Don't output an error message if we have already output one for | |
2717 | a parse error above. */ | |
2718 | rest_zero = 0; | |
2719 | if (nargs == 0 && i > 0) | |
2720 | { | |
e8037d57 | 2721 | cpp_error (pfile, "arguments given to macro `%s'", hp->name); |
7f2935c7 PB |
2722 | } |
2723 | else if (i < nargs) | |
2724 | { | |
2725 | /* traditional C allows foo() if foo wants one argument. */ | |
2726 | if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile)) | |
2727 | ; | |
2728 | /* the rest args token is allowed to absorb 0 tokens */ | |
2729 | else if (i == nargs - 1 && defn->rest_args) | |
2730 | rest_zero = 1; | |
7f2935c7 PB |
2731 | else if (i == 0) |
2732 | cpp_error (pfile, "macro `%s' used without args", hp->name); | |
2733 | else if (i == 1) | |
2734 | cpp_error (pfile, "macro `%s' used with just one arg", hp->name); | |
2735 | else | |
2736 | cpp_error (pfile, "macro `%s' used with only %d args", | |
2737 | hp->name, i); | |
2738 | } | |
2739 | else if (i > nargs) | |
2740 | { | |
e8037d57 PB |
2741 | cpp_error (pfile, |
2742 | "macro `%s' used with too many (%d) args", hp->name, i); | |
7f2935c7 PB |
2743 | } |
2744 | } | |
2745 | ||
2746 | /* If macro wants zero args, we parsed the arglist for checking only. | |
2747 | Read directly from the macro definition. */ | |
2748 | if (nargs <= 0) | |
2749 | { | |
2750 | xbuf = defn->expansion; | |
2751 | xbuf_len = defn->length; | |
2752 | } | |
2753 | else | |
2754 | { | |
2755 | register U_CHAR *exp = defn->expansion; | |
2756 | register int offset; /* offset in expansion, | |
2757 | copied a piece at a time */ | |
2758 | register int totlen; /* total amount of exp buffer filled so far */ | |
2759 | ||
2760 | register struct reflist *ap, *last_ap; | |
2761 | ||
2762 | /* Macro really takes args. Compute the expansion of this call. */ | |
2763 | ||
2764 | /* Compute length in characters of the macro's expansion. | |
2765 | Also count number of times each arg is used. */ | |
2766 | xbuf_len = defn->length; | |
2767 | for (ap = defn->pattern; ap != NULL; ap = ap->next) | |
2768 | { | |
2769 | if (ap->stringify) | |
2770 | { | |
2771 | register struct argdata *arg = &args[ap->argno]; | |
2772 | /* Stringify it it hasn't already been */ | |
2773 | if (arg->stringified_length < 0) | |
2774 | { | |
2775 | int arglen = arg->raw_length; | |
2776 | int escaped = 0; | |
2777 | int in_string = 0; | |
2778 | int c; | |
2779 | /* Initially need_space is -1. Otherwise, 1 means the | |
2780 | previous character was a space, but we suppressed it; | |
0f41302f | 2781 | 0 means the previous character was a non-space. */ |
7f2935c7 PB |
2782 | int need_space = -1; |
2783 | i = 0; | |
2784 | arg->stringified = CPP_WRITTEN (pfile); | |
2785 | if (!CPP_TRADITIONAL (pfile)) | |
2786 | CPP_PUTC (pfile, '\"'); /* insert beginning quote */ | |
2787 | for (; i < arglen; i++) | |
2788 | { | |
2789 | c = (ARG_BASE + arg->raw)[i]; | |
2790 | ||
2791 | if (! in_string) | |
2792 | { | |
2793 | /* Internal sequences of whitespace are replaced by | |
2794 | one space except within an string or char token.*/ | |
2795 | if (is_space[c]) | |
2796 | { | |
782331f4 PB |
2797 | if (CPP_WRITTEN (pfile) > arg->stringified |
2798 | && (CPP_PWRITTEN (pfile))[-1] == '@') | |
2799 | { | |
2800 | /* "@ " escape markers are removed */ | |
2801 | CPP_ADJUST_WRITTEN (pfile, -1); | |
2802 | continue; | |
2803 | } | |
7f2935c7 PB |
2804 | if (need_space == 0) |
2805 | need_space = 1; | |
2806 | continue; | |
2807 | } | |
2808 | else if (need_space > 0) | |
2809 | CPP_PUTC (pfile, ' '); | |
2810 | need_space = 0; | |
2811 | } | |
2812 | ||
2813 | if (escaped) | |
2814 | escaped = 0; | |
2815 | else | |
2816 | { | |
2817 | if (c == '\\') | |
2818 | escaped = 1; | |
2819 | if (in_string) | |
2820 | { | |
2821 | if (c == in_string) | |
2822 | in_string = 0; | |
2823 | } | |
2824 | else if (c == '\"' || c == '\'') | |
2825 | in_string = c; | |
2826 | } | |
2827 | ||
2828 | /* Escape these chars */ | |
2829 | if (c == '\"' || (in_string && c == '\\')) | |
2830 | CPP_PUTC (pfile, '\\'); | |
2831 | if (isprint (c)) | |
2832 | CPP_PUTC (pfile, c); | |
2833 | else | |
2834 | { | |
2835 | CPP_RESERVE (pfile, 4); | |
e9a25f70 | 2836 | sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", |
7f2935c7 PB |
2837 | (unsigned int) c); |
2838 | CPP_ADJUST_WRITTEN (pfile, 4); | |
2839 | } | |
2840 | } | |
2841 | if (!CPP_TRADITIONAL (pfile)) | |
2842 | CPP_PUTC (pfile, '\"'); /* insert ending quote */ | |
2843 | arg->stringified_length | |
2844 | = CPP_WRITTEN (pfile) - arg->stringified; | |
2845 | } | |
2846 | xbuf_len += args[ap->argno].stringified_length; | |
2847 | } | |
2848 | else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile)) | |
2849 | /* Add 4 for two newline-space markers to prevent | |
2850 | token concatenation. */ | |
2851 | xbuf_len += args[ap->argno].raw_length + 4; | |
2852 | else | |
2853 | { | |
2854 | /* We have an ordinary (expanded) occurrence of the arg. | |
2855 | So compute its expansion, if we have not already. */ | |
2856 | if (args[ap->argno].expand_length < 0) | |
2857 | { | |
2858 | args[ap->argno].expanded = CPP_WRITTEN (pfile); | |
7f2935c7 PB |
2859 | cpp_expand_to_buffer (pfile, |
2860 | ARG_BASE + args[ap->argno].raw, | |
2861 | args[ap->argno].raw_length); | |
2862 | ||
7f2935c7 PB |
2863 | args[ap->argno].expand_length |
2864 | = CPP_WRITTEN (pfile) - args[ap->argno].expanded; | |
2865 | } | |
2866 | ||
2867 | /* Add 4 for two newline-space markers to prevent | |
2868 | token concatenation. */ | |
2869 | xbuf_len += args[ap->argno].expand_length + 4; | |
2870 | } | |
2871 | if (args[ap->argno].use_count < 10) | |
2872 | args[ap->argno].use_count++; | |
2873 | } | |
2874 | ||
2875 | xbuf = (U_CHAR *) xmalloc (xbuf_len + 1); | |
2876 | ||
2877 | /* Generate in XBUF the complete expansion | |
2878 | with arguments substituted in. | |
2879 | TOTLEN is the total size generated so far. | |
2880 | OFFSET is the index in the definition | |
2881 | of where we are copying from. */ | |
2882 | offset = totlen = 0; | |
2883 | for (last_ap = NULL, ap = defn->pattern; ap != NULL; | |
2884 | last_ap = ap, ap = ap->next) | |
2885 | { | |
2886 | register struct argdata *arg = &args[ap->argno]; | |
2887 | int count_before = totlen; | |
2888 | ||
2889 | /* Add chars to XBUF. */ | |
2890 | for (i = 0; i < ap->nchars; i++, offset++) | |
2891 | xbuf[totlen++] = exp[offset]; | |
2892 | ||
2893 | /* If followed by an empty rest arg with concatenation, | |
2894 | delete the last run of nonwhite chars. */ | |
2895 | if (rest_zero && totlen > count_before | |
2896 | && ((ap->rest_args && ap->raw_before) | |
2897 | || (last_ap != NULL && last_ap->rest_args | |
2898 | && last_ap->raw_after))) | |
2899 | { | |
2900 | /* Delete final whitespace. */ | |
2901 | while (totlen > count_before && is_space[xbuf[totlen - 1]]) | |
2902 | totlen--; | |
2903 | ||
2904 | /* Delete the nonwhites before them. */ | |
2905 | while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) | |
2906 | totlen--; | |
2907 | } | |
2908 | ||
2909 | if (ap->stringify != 0) | |
2910 | { | |
2911 | bcopy (ARG_BASE + arg->stringified, | |
2912 | xbuf + totlen, arg->stringified_length); | |
2913 | totlen += arg->stringified_length; | |
2914 | } | |
2915 | else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile)) | |
2916 | { | |
2917 | U_CHAR *p1 = ARG_BASE + arg->raw; | |
2918 | U_CHAR *l1 = p1 + arg->raw_length; | |
2919 | if (ap->raw_before) | |
2920 | { | |
2921 | while (p1 != l1 && is_space[*p1]) p1++; | |
2922 | while (p1 != l1 && is_idchar[*p1]) | |
2923 | xbuf[totlen++] = *p1++; | |
d8bfa78c RK |
2924 | /* Delete any no-reexpansion marker that follows |
2925 | an identifier at the beginning of the argument | |
2926 | if the argument is concatenated with what precedes it. */ | |
2927 | if (p1[0] == '@' && p1[1] == '-') | |
2928 | p1 += 2; | |
7f2935c7 PB |
2929 | } |
2930 | if (ap->raw_after) | |
2931 | { | |
2932 | /* Arg is concatenated after: delete trailing whitespace, | |
2933 | whitespace markers, and no-reexpansion markers. */ | |
2934 | while (p1 != l1) | |
2935 | { | |
2936 | if (is_space[l1[-1]]) l1--; | |
2937 | else if (l1[-1] == '-') | |
2938 | { | |
2939 | U_CHAR *p2 = l1 - 1; | |
2940 | /* If a `-' is preceded by an odd number of newlines then it | |
2941 | and the last newline are a no-reexpansion marker. */ | |
2942 | while (p2 != p1 && p2[-1] == '\n') p2--; | |
2943 | if ((l1 - 1 - p2) & 1) { | |
2944 | l1 -= 2; | |
2945 | } | |
2946 | else break; | |
2947 | } | |
2948 | else break; | |
2949 | } | |
2950 | } | |
2951 | ||
2952 | bcopy (p1, xbuf + totlen, l1 - p1); | |
2953 | totlen += l1 - p1; | |
2954 | } | |
2955 | else | |
2956 | { | |
2957 | U_CHAR *expanded = ARG_BASE + arg->expanded; | |
2958 | if (!ap->raw_before && totlen > 0 && arg->expand_length | |
2959 | && !CPP_TRADITIONAL(pfile) | |
2960 | && unsafe_chars (xbuf[totlen-1], expanded[0])) | |
782331f4 PB |
2961 | { |
2962 | xbuf[totlen++] = '@'; | |
2963 | xbuf[totlen++] = ' '; | |
2964 | } | |
7f2935c7 PB |
2965 | |
2966 | bcopy (expanded, xbuf + totlen, arg->expand_length); | |
2967 | totlen += arg->expand_length; | |
2968 | ||
2969 | if (!ap->raw_after && totlen > 0 && offset < defn->length | |
2970 | && !CPP_TRADITIONAL(pfile) | |
2971 | && unsafe_chars (xbuf[totlen-1], exp[offset])) | |
782331f4 PB |
2972 | { |
2973 | xbuf[totlen++] = '@'; | |
2974 | xbuf[totlen++] = ' '; | |
2975 | } | |
7f2935c7 PB |
2976 | |
2977 | /* If a macro argument with newlines is used multiple times, | |
2978 | then only expand the newlines once. This avoids creating | |
2979 | output lines which don't correspond to any input line, | |
2980 | which confuses gdb and gcov. */ | |
2981 | if (arg->use_count > 1 && arg->newlines > 0) | |
2982 | { | |
2983 | /* Don't bother doing change_newlines for subsequent | |
2984 | uses of arg. */ | |
2985 | arg->use_count = 1; | |
2986 | arg->expand_length | |
2987 | = change_newlines (expanded, arg->expand_length); | |
2988 | } | |
2989 | } | |
2990 | ||
2991 | if (totlen > xbuf_len) | |
2992 | abort (); | |
2993 | } | |
2994 | ||
2995 | /* if there is anything left of the definition | |
0f41302f | 2996 | after handling the arg list, copy that in too. */ |
7f2935c7 PB |
2997 | |
2998 | for (i = offset; i < defn->length; i++) | |
2999 | { | |
3000 | /* if we've reached the end of the macro */ | |
3001 | if (exp[i] == ')') | |
3002 | rest_zero = 0; | |
3003 | if (! (rest_zero && last_ap != NULL && last_ap->rest_args | |
3004 | && last_ap->raw_after)) | |
3005 | xbuf[totlen++] = exp[i]; | |
3006 | } | |
3007 | ||
3008 | xbuf[totlen] = 0; | |
3009 | xbuf_len = totlen; | |
3010 | ||
3011 | } | |
3012 | ||
782331f4 PB |
3013 | pfile->output_escapes--; |
3014 | ||
7f2935c7 PB |
3015 | /* Now put the expansion on the input stack |
3016 | so our caller will commence reading from it. */ | |
3017 | push_macro_expansion (pfile, xbuf, xbuf_len, hp); | |
3018 | CPP_BUFFER (pfile)->has_escapes = 1; | |
3019 | ||
0f41302f | 3020 | /* Pop the space we've used in the token_buffer for argument expansion. */ |
7f2935c7 PB |
3021 | CPP_SET_WRITTEN (pfile, old_written); |
3022 | ||
3023 | /* Recursive macro use sometimes works traditionally. | |
3024 | #define foo(x,y) bar (x (y,0), y) | |
3025 | foo (foo, baz) */ | |
3026 | ||
3027 | if (!CPP_TRADITIONAL (pfile)) | |
3028 | hp->type = T_DISABLED; | |
3029 | } | |
3030 | ||
3031 | static void | |
3032 | push_macro_expansion (pfile, xbuf, xbuf_len, hp) | |
3033 | cpp_reader *pfile; | |
3034 | register U_CHAR *xbuf; | |
3035 | int xbuf_len; | |
3036 | HASHNODE *hp; | |
3037 | { | |
3038 | register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len); | |
e2f79f3c PB |
3039 | if (mbuf == NULL) |
3040 | return; | |
7f2935c7 PB |
3041 | mbuf->cleanup = macro_cleanup; |
3042 | mbuf->data = hp; | |
3043 | ||
782331f4 | 3044 | /* The first chars of the expansion should be a "@ " added by |
7f2935c7 PB |
3045 | collect_expansion. This is to prevent accidental token-pasting |
3046 | between the text preceding the macro invocation, and the macro | |
3047 | expansion text. | |
3048 | ||
3049 | We would like to avoid adding unneeded spaces (for the sake of | |
3050 | tools that use cpp, such as imake). In some common cases we can | |
3051 | tell that it is safe to omit the space. | |
3052 | ||
3053 | The character before the macro invocation cannot have been an | |
3054 | idchar (or else it would have been pasted with the idchars of | |
3055 | the macro name). Therefore, if the first non-space character | |
3056 | of the expansion is an idchar, we do not need the extra space | |
3057 | to prevent token pasting. | |
3058 | ||
3059 | Also, we don't need the extra space if the first char is '(', | |
3060 | or some other (less common) characters. */ | |
3061 | ||
782331f4 PB |
3062 | if (xbuf[0] == '@' && xbuf[1] == ' ' |
3063 | && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\'' | |
3064 | || xbuf[2] == '\"')) | |
3065 | mbuf->cur += 2; | |
7f2935c7 PB |
3066 | } |
3067 | \f | |
3068 | /* Like cpp_get_token, except that it does not read past end-of-line. | |
3069 | Also, horizontal space is skipped, and macros are popped. */ | |
3070 | ||
3071 | static enum cpp_token | |
3072 | get_directive_token (pfile) | |
3073 | cpp_reader *pfile; | |
3074 | { | |
3075 | for (;;) | |
3076 | { | |
3077 | long old_written = CPP_WRITTEN (pfile); | |
3078 | enum cpp_token token; | |
3079 | cpp_skip_hspace (pfile); | |
3080 | if (PEEKC () == '\n') | |
3081 | return CPP_VSPACE; | |
3082 | token = cpp_get_token (pfile); | |
3083 | switch (token) | |
3084 | { | |
3085 | case CPP_POP: | |
3086 | if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile))) | |
3087 | return token; | |
0f41302f | 3088 | /* ... else fall though ... */ |
7f2935c7 PB |
3089 | case CPP_HSPACE: case CPP_COMMENT: |
3090 | CPP_SET_WRITTEN (pfile, old_written); | |
3091 | break; | |
3092 | default: | |
3093 | return token; | |
3094 | } | |
3095 | } | |
3096 | } | |
3097 | \f | |
3098 | /* Handle #include and #import. | |
3099 | This function expects to see "fname" or <fname> on the input. | |
3100 | ||
3101 | The input is normally in part of the output_buffer following | |
ddd5a7c1 | 3102 | CPP_WRITTEN, and will get overwritten by output_line_command. |
7f2935c7 | 3103 | I.e. in input file specification has been popped by handle_directive. |
0f41302f | 3104 | This is safe. */ |
7f2935c7 PB |
3105 | |
3106 | static int | |
3107 | do_include (pfile, keyword, unused1, unused2) | |
3108 | cpp_reader *pfile; | |
3109 | struct directive *keyword; | |
3110 | U_CHAR *unused1, *unused2; | |
3111 | { | |
3112 | int importing = (keyword->type == T_IMPORT); | |
3113 | int skip_dirs = (keyword->type == T_INCLUDE_NEXT); | |
3114 | char *fname; /* Dynamically allocated fname buffer */ | |
3115 | char *pcftry; | |
3116 | char *pcfname; | |
3117 | U_CHAR *fbeg, *fend; /* Beginning and end of fname */ | |
3118 | enum cpp_token token; | |
3119 | ||
3120 | /* Chain of dirs to search */ | |
3121 | struct file_name_list *search_start = CPP_OPTIONS (pfile)->include; | |
3122 | struct file_name_list dsp[1]; /* First in chain, if #include "..." */ | |
3123 | struct file_name_list *searchptr = 0; | |
3124 | long old_written = CPP_WRITTEN (pfile); | |
3125 | ||
3126 | int flen; | |
3127 | ||
3128 | int f; /* file number */ | |
3129 | ||
3130 | int retried = 0; /* Have already tried macro | |
0f41302f | 3131 | expanding the include line */ |
7f2935c7 PB |
3132 | int angle_brackets = 0; /* 0 for "...", 1 for <...> */ |
3133 | int pcf = -1; | |
3134 | char *pcfbuf; | |
6be492ab | 3135 | char *pcfbuflimit; |
7f2935c7 PB |
3136 | int pcfnum; |
3137 | f= -1; /* JF we iz paranoid! */ | |
3138 | ||
cfb3ee16 RK |
3139 | if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p) |
3140 | { | |
3141 | if (importing) | |
3142 | cpp_pedwarn (pfile, "ANSI C does not allow `#import'"); | |
3143 | if (skip_dirs) | |
3144 | cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'"); | |
3145 | } | |
3146 | ||
7f2935c7 PB |
3147 | if (importing && CPP_OPTIONS (pfile)->warn_import |
3148 | && !CPP_OPTIONS (pfile)->inhibit_warnings | |
3149 | && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning) | |
3150 | { | |
3151 | pfile->import_warning = 1; | |
3152 | cpp_warning (pfile, "using `#import' is not recommended"); | |
3153 | fprintf (stderr, "The fact that a certain header file need not be processed more than once\n"); | |
3154 | fprintf (stderr, "should be indicated in the header file, not where it is used.\n"); | |
3155 | fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n"); | |
3156 | fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n"); | |
3157 | fprintf (stderr, " #define _FOO_H_INCLUDED\n"); | |
3158 | fprintf (stderr, " ... <real contents of file> ...\n"); | |
3159 | fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n"); | |
3160 | fprintf (stderr, "Then users can use `#include' any number of times.\n"); | |
3161 | fprintf (stderr, "GNU C automatically avoids processing the file more than once\n"); | |
3162 | fprintf (stderr, "when it is equipped with such a conditional.\n"); | |
3163 | } | |
3164 | ||
3165 | pfile->parsing_include_directive++; | |
3166 | token = get_directive_token (pfile); | |
3167 | pfile->parsing_include_directive--; | |
3168 | ||
3169 | if (token == CPP_STRING) | |
3170 | { | |
3171 | /* FIXME - check no trailing garbage */ | |
3172 | fbeg = pfile->token_buffer + old_written + 1; | |
3173 | fend = CPP_PWRITTEN (pfile) - 1; | |
3174 | if (fbeg[-1] == '<') | |
3175 | { | |
3176 | angle_brackets = 1; | |
3177 | /* If -I-, start with the first -I dir after the -I-. */ | |
3178 | if (CPP_OPTIONS (pfile)->first_bracket_include) | |
3179 | search_start = CPP_OPTIONS (pfile)->first_bracket_include; | |
3180 | } | |
0f41302f | 3181 | /* If -I- was specified, don't search current dir, only spec'd ones. */ |
7f2935c7 PB |
3182 | else if (! CPP_OPTIONS (pfile)->ignore_srcdir) |
3183 | { | |
d013f05e | 3184 | cpp_buffer *fp = CPP_BUFFER (pfile); |
7f2935c7 | 3185 | /* We have "filename". Figure out directory this source |
0f41302f | 3186 | file is coming from and put it on the front of the list. */ |
7f2935c7 | 3187 | |
d013f05e | 3188 | for ( ; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp)) |
7f2935c7 PB |
3189 | { |
3190 | int n; | |
3191 | char *ep,*nam; | |
3192 | ||
3193 | if ((nam = fp->nominal_fname) != NULL) | |
3194 | { | |
3195 | /* Found a named file. Figure out dir of the file, | |
3196 | and put it in front of the search list. */ | |
3197 | dsp[0].next = search_start; | |
3198 | search_start = dsp; | |
3199 | #ifndef VMS | |
3200 | ep = rindex (nam, '/'); | |
3201 | #else /* VMS */ | |
3202 | ep = rindex (nam, ']'); | |
3203 | if (ep == NULL) ep = rindex (nam, '>'); | |
3204 | if (ep == NULL) ep = rindex (nam, ':'); | |
3205 | if (ep != NULL) ep++; | |
3206 | #endif /* VMS */ | |
3207 | if (ep != NULL) | |
3208 | { | |
3209 | n = ep - nam; | |
3210 | dsp[0].fname = (char *) alloca (n + 1); | |
3211 | strncpy (dsp[0].fname, nam, n); | |
3212 | dsp[0].fname[n] = '\0'; | |
3213 | if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len) | |
3214 | pfile->max_include_len = n + INCLUDE_LEN_FUDGE; | |
3215 | } | |
3216 | else | |
3217 | { | |
3218 | dsp[0].fname = 0; /* Current directory */ | |
3219 | } | |
3220 | dsp[0].got_name_map = 0; | |
3221 | break; | |
3222 | } | |
3223 | } | |
3224 | } | |
3225 | } | |
3226 | #ifdef VMS | |
3227 | else if (token == CPP_NAME) | |
3228 | { | |
3229 | /* | |
3230 | * Support '#include xyz' like VAX-C to allow for easy use of all the | |
3231 | * decwindow include files. It defaults to '#include <xyz.h>' (so the | |
3232 | * code from case '<' is repeated here) and generates a warning. | |
3233 | */ | |
3234 | cpp_warning (pfile, | |
3235 | "VAX-C-style include specification found, use '#include <filename.h>' !"); | |
3236 | angle_brackets = 1; | |
3237 | /* If -I-, start with the first -I dir after the -I-. */ | |
3238 | if (CPP_OPTIONS (pfile)->first_bracket_include) | |
3239 | search_start = CPP_OPTIONS (pfile)->first_bracket_include; | |
3240 | fbeg = pfile->token_buffer + old_written; | |
3241 | fend = CPP_PWRITTEN (pfile); | |
3242 | } | |
3243 | #endif | |
3244 | else | |
3245 | { | |
3246 | cpp_error (pfile, | |
3247 | "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name); | |
3248 | CPP_SET_WRITTEN (pfile, old_written); | |
3249 | skip_rest_of_line (pfile); | |
3250 | return 0; | |
3251 | } | |
3252 | ||
3253 | *fend = 0; | |
3254 | ||
3255 | token = get_directive_token (pfile); | |
3256 | if (token != CPP_VSPACE) | |
3257 | { | |
3258 | cpp_error (pfile, "junk at end of `#include'"); | |
3259 | while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) | |
3260 | token = get_directive_token (pfile); | |
3261 | } | |
3262 | ||
3263 | /* For #include_next, skip in the search path | |
3264 | past the dir in which the containing file was found. */ | |
3265 | if (skip_dirs) | |
3266 | { | |
d013f05e PB |
3267 | cpp_buffer *fp = CPP_BUFFER (pfile); |
3268 | for (; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp)) | |
7f2935c7 PB |
3269 | if (fp->fname != NULL) |
3270 | { | |
3271 | /* fp->dir is null if the containing file was specified with | |
3272 | an absolute file name. In that case, don't skip anything. */ | |
6bac1e64 PB |
3273 | if (fp->dir == SELF_DIR_DUMMY) |
3274 | search_start = CPP_OPTIONS (pfile)->include; | |
3275 | else if (fp->dir) | |
7f2935c7 PB |
3276 | search_start = fp->dir->next; |
3277 | break; | |
3278 | } | |
3279 | } | |
3280 | ||
3281 | CPP_SET_WRITTEN (pfile, old_written); | |
3282 | ||
3283 | flen = fend - fbeg; | |
3284 | ||
3285 | if (flen == 0) | |
3286 | { | |
3287 | cpp_error (pfile, "empty file name in `#%s'", keyword->name); | |
3288 | return 0; | |
3289 | } | |
3290 | ||
3291 | /* Allocate this permanently, because it gets stored in the definitions | |
3292 | of macros. */ | |
3293 | fname = (char *) xmalloc (pfile->max_include_len + flen + 4); | |
3294 | /* + 2 above for slash and terminating null. */ | |
3295 | /* + 2 added for '.h' on VMS (to support '#include filename') */ | |
3296 | ||
3297 | /* If specified file name is absolute, just open it. */ | |
3298 | ||
3299 | if (*fbeg == '/') { | |
3300 | strncpy (fname, fbeg, flen); | |
3301 | fname[flen] = 0; | |
3302 | if (redundant_include_p (pfile, fname)) | |
3303 | return 0; | |
3304 | if (importing) | |
3305 | f = lookup_import (pfile, fname, NULL_PTR); | |
3306 | else | |
782331f4 | 3307 | f = open_include_file (pfile, fname, NULL_PTR); |
7f2935c7 PB |
3308 | if (f == -2) |
3309 | return 0; /* Already included this file */ | |
3310 | } else { | |
3311 | /* Search directory path, trying to open the file. | |
3312 | Copy each filename tried into FNAME. */ | |
3313 | ||
3314 | for (searchptr = search_start; searchptr; searchptr = searchptr->next) { | |
3315 | if (searchptr->fname) { | |
3316 | /* The empty string in a search path is ignored. | |
3317 | This makes it possible to turn off entirely | |
3318 | a standard piece of the list. */ | |
3319 | if (searchptr->fname[0] == 0) | |
3320 | continue; | |
3321 | strcpy (fname, searchptr->fname); | |
3322 | strcat (fname, "/"); | |
3323 | fname[strlen (fname) + flen] = 0; | |
3324 | } else { | |
3325 | fname[0] = 0; | |
3326 | } | |
3327 | strncat (fname, fbeg, flen); | |
3328 | #ifdef VMS | |
3329 | /* Change this 1/2 Unix 1/2 VMS file specification into a | |
3330 | full VMS file specification */ | |
3331 | if (searchptr->fname && (searchptr->fname[0] != 0)) { | |
3332 | /* Fix up the filename */ | |
3333 | hack_vms_include_specification (fname); | |
3334 | } else { | |
3335 | /* This is a normal VMS filespec, so use it unchanged. */ | |
3336 | strncpy (fname, fbeg, flen); | |
3337 | fname[flen] = 0; | |
3338 | /* if it's '#include filename', add the missing .h */ | |
3339 | if (index(fname,'.')==NULL) { | |
3340 | strcat (fname, ".h"); | |
3341 | } | |
3342 | } | |
3343 | #endif /* VMS */ | |
6be492ab PB |
3344 | /* ??? There are currently 3 separate mechanisms for avoiding processing |
3345 | of redundant include files: #import, #pragma once, and | |
3346 | redundant_include_p. It would be nice if they were unified. */ | |
3347 | if (redundant_include_p (pfile, fname)) | |
3348 | return 0; | |
7f2935c7 PB |
3349 | if (importing) |
3350 | f = lookup_import (pfile, fname, searchptr); | |
3351 | else | |
782331f4 | 3352 | f = open_include_file (pfile, fname, searchptr); |
7f2935c7 PB |
3353 | if (f == -2) |
3354 | return 0; /* Already included this file */ | |
3355 | #ifdef EACCES | |
3356 | else if (f == -1 && errno == EACCES) | |
3357 | cpp_warning (pfile, "Header file %s exists, but is not readable", | |
3358 | fname); | |
3359 | #endif | |
7f2935c7 PB |
3360 | if (f >= 0) |
3361 | break; | |
3362 | } | |
3363 | } | |
3364 | ||
3365 | if (f < 0) | |
3366 | { | |
3367 | /* A file that was not found. */ | |
3368 | strncpy (fname, fbeg, flen); | |
3369 | fname[flen] = 0; | |
3370 | /* If generating dependencies and -MG was specified, we assume missing | |
3371 | files are leaf files, living in the same directory as the source file | |
3372 | or other similar place; these missing files may be generated from | |
3373 | other files and may not exist yet (eg: y.tab.h). */ | |
3374 | ||
3375 | if (CPP_OPTIONS(pfile)->print_deps_missing_files | |
3376 | && CPP_PRINT_DEPS (pfile) | |
3377 | > (angle_brackets || (pfile->system_include_depth > 0))) | |
3378 | { | |
3379 | /* If it was requested as a system header file, | |
3380 | then assume it belongs in the first place to look for such. */ | |
3381 | if (angle_brackets) | |
3382 | { | |
3383 | for (searchptr = search_start; searchptr; | |
3384 | searchptr = searchptr->next) | |
3385 | { | |
3386 | if (searchptr->fname) | |
3387 | { | |
3388 | char *p; | |
3389 | ||
3390 | if (searchptr->fname[0] == 0) | |
3391 | continue; | |
e8037d57 PB |
3392 | p = (char *) alloca (strlen (searchptr->fname) |
3393 | + strlen (fname) + 2); | |
7f2935c7 PB |
3394 | strcpy (p, searchptr->fname); |
3395 | strcat (p, "/"); | |
3396 | strcat (p, fname); | |
3397 | deps_output (pfile, p, ' '); | |
3398 | break; | |
3399 | } | |
3400 | } | |
3401 | } | |
3402 | else | |
3403 | { | |
3404 | /* Otherwise, omit the directory, as if the file existed | |
3405 | in the directory with the source. */ | |
3406 | deps_output (pfile, fname, ' '); | |
3407 | } | |
3408 | } | |
3409 | /* If -M was specified, and this header file won't be added to the | |
3410 | dependency list, then don't count this as an error, because we can | |
3411 | still produce correct output. Otherwise, we can't produce correct | |
3412 | output, because there may be dependencies we need inside the missing | |
3413 | file, and we don't know what directory this missing file exists in.*/ | |
3414 | else if (CPP_PRINT_DEPS (pfile) | |
3415 | && (CPP_PRINT_DEPS (pfile) | |
3416 | <= (angle_brackets || (pfile->system_include_depth > 0)))) | |
3417 | cpp_warning (pfile, "No include path in which to find %s", fname); | |
3418 | else if (search_start) | |
3419 | cpp_error_from_errno (pfile, fname); | |
3420 | else | |
3421 | cpp_error (pfile, "No include path in which to find %s", fname); | |
3422 | } | |
3423 | else { | |
7f2935c7 PB |
3424 | /* Check to see if this include file is a once-only include file. |
3425 | If so, give up. */ | |
3426 | ||
0f41302f | 3427 | struct file_name_list *ptr; |
7f2935c7 PB |
3428 | |
3429 | for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) { | |
3430 | if (!strcmp (ptr->fname, fname)) { | |
3431 | close (f); | |
0f41302f | 3432 | return 0; /* This file was once'd. */ |
7f2935c7 PB |
3433 | } |
3434 | } | |
3435 | ||
3436 | for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) { | |
3437 | if (!strcmp (ptr->fname, fname)) | |
0f41302f | 3438 | break; /* This file was included before. */ |
7f2935c7 PB |
3439 | } |
3440 | ||
3441 | if (ptr == 0) { | |
3442 | /* This is the first time for this file. */ | |
3443 | /* Add it to list of files included. */ | |
3444 | ||
3445 | ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); | |
3446 | ptr->control_macro = 0; | |
3447 | ptr->c_system_include_path = 0; | |
3448 | ptr->next = pfile->all_include_files; | |
3449 | pfile->all_include_files = ptr; | |
3450 | ptr->fname = savestring (fname); | |
3451 | ptr->got_name_map = 0; | |
3452 | ||
3453 | /* For -M, add this file to the dependencies. */ | |
3454 | if (CPP_PRINT_DEPS (pfile) | |
3455 | > (angle_brackets || (pfile->system_include_depth > 0))) | |
3456 | deps_output (pfile, fname, ' '); | |
3457 | } | |
3458 | ||
3459 | /* Handle -H option. */ | |
3460 | if (CPP_OPTIONS(pfile)->print_include_names) | |
3461 | { | |
3462 | cpp_buffer *buf = CPP_BUFFER (pfile); | |
d013f05e | 3463 | while ((buf = CPP_PREV_BUFFER (buf)) != CPP_NULL_BUFFER (pfile)) |
7f2935c7 PB |
3464 | putc ('.', stderr); |
3465 | fprintf (stderr, "%s\n", fname); | |
3466 | } | |
3467 | ||
3468 | if (angle_brackets) | |
3469 | pfile->system_include_depth++; | |
3470 | ||
3471 | /* Actually process the file. */ | |
3472 | ||
0f41302f | 3473 | /* Record file on "seen" list for #import. */ |
7f2935c7 PB |
3474 | add_import (pfile, f, fname); |
3475 | ||
3476 | pcftry = (char *) alloca (strlen (fname) + 30); | |
3477 | pcfbuf = 0; | |
3478 | pcfnum = 0; | |
3479 | ||
7f2935c7 PB |
3480 | #if 0 |
3481 | if (!no_precomp) | |
6be492ab PB |
3482 | { |
3483 | struct stat stat_f; | |
3484 | ||
3485 | fstat (f, &stat_f); | |
3486 | ||
3487 | do { | |
3488 | sprintf (pcftry, "%s%d", fname, pcfnum++); | |
3489 | ||
3490 | pcf = open (pcftry, O_RDONLY, 0666); | |
3491 | if (pcf != -1) | |
3492 | { | |
3493 | struct stat s; | |
3494 | ||
3495 | fstat (pcf, &s); | |
3496 | if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino, | |
3497 | sizeof (s.st_ino)) | |
3498 | || stat_f.st_dev != s.st_dev) | |
3499 | { | |
3500 | pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit); | |
3501 | /* Don't need it any more. */ | |
3502 | close (pcf); | |
3503 | } | |
3504 | else | |
3505 | { | |
3506 | /* Don't need it at all. */ | |
3507 | close (pcf); | |
3508 | break; | |
3509 | } | |
3510 | } | |
3511 | } while (pcf != -1 && !pcfbuf); | |
3512 | } | |
7f2935c7 PB |
3513 | #endif |
3514 | ||
3515 | /* Actually process the file */ | |
e2f79f3c PB |
3516 | if (cpp_push_buffer (pfile, NULL, 0) == NULL) |
3517 | return 0; | |
7f2935c7 | 3518 | if (finclude (pfile, f, fname, is_system_include (pfile, fname), |
6bac1e64 | 3519 | searchptr != dsp ? searchptr : SELF_DIR_DUMMY)) |
7f2935c7 PB |
3520 | { |
3521 | output_line_command (pfile, 0, enter_file); | |
3522 | pfile->only_seen_white = 2; | |
3523 | } | |
3524 | ||
3525 | if (angle_brackets) | |
3526 | pfile->system_include_depth--; | |
3527 | } | |
3528 | return 0; | |
3529 | } | |
3530 | ||
3531 | /* Return nonzero if there is no need to include file NAME | |
3532 | because it has already been included and it contains a conditional | |
3533 | to make a repeated include do nothing. */ | |
3534 | ||
3535 | static int | |
3536 | redundant_include_p (pfile, name) | |
3537 | cpp_reader *pfile; | |
3538 | char *name; | |
3539 | { | |
3540 | struct file_name_list *l = pfile->all_include_files; | |
3541 | for (; l; l = l->next) | |
3542 | if (! strcmp (name, l->fname) | |
3543 | && l->control_macro | |
3544 | && cpp_lookup (pfile, l->control_macro, -1, -1)) | |
3545 | return 1; | |
3546 | return 0; | |
3547 | } | |
3548 | ||
3549 | /* Return nonzero if the given FILENAME is an absolute pathname which | |
3550 | designates a file within one of the known "system" include file | |
3551 | directories. We assume here that if the given FILENAME looks like | |
3552 | it is the name of a file which resides either directly in a "system" | |
3553 | include file directory, or within any subdirectory thereof, then the | |
3554 | given file must be a "system" include file. This function tells us | |
3555 | if we should suppress pedantic errors/warnings for the given FILENAME. | |
3556 | ||
3557 | The value is 2 if the file is a C-language system header file | |
3558 | for which C++ should (on most systems) assume `extern "C"'. */ | |
3559 | ||
3560 | static int | |
3561 | is_system_include (pfile, filename) | |
3562 | cpp_reader *pfile; | |
3563 | register char *filename; | |
3564 | { | |
3565 | struct file_name_list *searchptr; | |
3566 | ||
3567 | for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr; | |
3568 | searchptr = searchptr->next) | |
3569 | if (searchptr->fname) { | |
3570 | register char *sys_dir = searchptr->fname; | |
3571 | register unsigned length = strlen (sys_dir); | |
3572 | ||
3573 | if (! strncmp (sys_dir, filename, length) && filename[length] == '/') | |
3574 | { | |
3575 | if (searchptr->c_system_include_path) | |
3576 | return 2; | |
3577 | else | |
3578 | return 1; | |
3579 | } | |
3580 | } | |
3581 | return 0; | |
3582 | } | |
3583 | ||
3584 | \f | |
3585 | /* | |
3586 | * Install a name in the assertion hash table. | |
3587 | * | |
3588 | * If LEN is >= 0, it is the length of the name. | |
3589 | * Otherwise, compute the length by scanning the entire name. | |
3590 | * | |
3591 | * If HASH is >= 0, it is the precomputed hash code. | |
3592 | * Otherwise, compute the hash code. | |
3593 | */ | |
0f41302f | 3594 | |
7f2935c7 PB |
3595 | static ASSERTION_HASHNODE * |
3596 | assertion_install (pfile, name, len, hash) | |
3597 | cpp_reader *pfile; | |
3598 | U_CHAR *name; | |
3599 | int len; | |
3600 | int hash; | |
3601 | { | |
3602 | register ASSERTION_HASHNODE *hp; | |
3603 | register int i, bucket; | |
3604 | register U_CHAR *p, *q; | |
3605 | ||
3606 | i = sizeof (ASSERTION_HASHNODE) + len + 1; | |
3607 | hp = (ASSERTION_HASHNODE *) xmalloc (i); | |
3608 | bucket = hash; | |
3609 | hp->bucket_hdr = &pfile->assertion_hashtab[bucket]; | |
3610 | hp->next = pfile->assertion_hashtab[bucket]; | |
3611 | pfile->assertion_hashtab[bucket] = hp; | |
3612 | hp->prev = NULL; | |
3613 | if (hp->next != NULL) | |
3614 | hp->next->prev = hp; | |
3615 | hp->length = len; | |
3616 | hp->value = 0; | |
3617 | hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE); | |
3618 | p = hp->name; | |
3619 | q = name; | |
3620 | for (i = 0; i < len; i++) | |
3621 | *p++ = *q++; | |
3622 | hp->name[len] = 0; | |
3623 | return hp; | |
3624 | } | |
3625 | /* | |
3626 | * find the most recent hash node for name name (ending with first | |
3627 | * non-identifier char) installed by install | |
3628 | * | |
3629 | * If LEN is >= 0, it is the length of the name. | |
3630 | * Otherwise, compute the length by scanning the entire name. | |
3631 | * | |
3632 | * If HASH is >= 0, it is the precomputed hash code. | |
3633 | * Otherwise, compute the hash code. | |
3634 | */ | |
3635 | ||
3636 | static ASSERTION_HASHNODE * | |
3637 | assertion_lookup (pfile, name, len, hash) | |
3638 | cpp_reader *pfile; | |
3639 | U_CHAR *name; | |
3640 | int len; | |
3641 | int hash; | |
3642 | { | |
3643 | register ASSERTION_HASHNODE *bucket; | |
3644 | ||
3645 | bucket = pfile->assertion_hashtab[hash]; | |
3646 | while (bucket) { | |
3647 | if (bucket->length == len && strncmp (bucket->name, name, len) == 0) | |
3648 | return bucket; | |
3649 | bucket = bucket->next; | |
3650 | } | |
3651 | return NULL; | |
3652 | } | |
3653 | ||
3654 | static void | |
3655 | delete_assertion (hp) | |
3656 | ASSERTION_HASHNODE *hp; | |
3657 | { | |
782331f4 | 3658 | struct tokenlist_list *tail; |
7f2935c7 PB |
3659 | if (hp->prev != NULL) |
3660 | hp->prev->next = hp->next; | |
3661 | if (hp->next != NULL) | |
3662 | hp->next->prev = hp->prev; | |
3663 | ||
782331f4 PB |
3664 | for (tail = hp->value; tail; ) |
3665 | { | |
3666 | struct tokenlist_list *next = tail->next; | |
3667 | free_token_list (tail->tokens); | |
3668 | free (tail); | |
3669 | tail = next; | |
3670 | } | |
3671 | ||
0f41302f MS |
3672 | /* Make sure that the bucket chain header that |
3673 | the deleted guy was on points to the right thing afterwards. */ | |
7f2935c7 PB |
3674 | if (hp == *hp->bucket_hdr) |
3675 | *hp->bucket_hdr = hp->next; | |
3676 | ||
3677 | free (hp); | |
3678 | } | |
3679 | \f | |
3680 | /* Convert a character string literal into a nul-terminated string. | |
3681 | The input string is [IN ... LIMIT). | |
3682 | The result is placed in RESULT. RESULT can be the same as IN. | |
3683 | The value returned in the end of the string written to RESULT, | |
3684 | or NULL on error. */ | |
3685 | ||
0f41302f | 3686 | static U_CHAR * |
7f2935c7 PB |
3687 | convert_string (pfile, result, in, limit, handle_escapes) |
3688 | cpp_reader *pfile; | |
3689 | register U_CHAR *result, *in, *limit; | |
3690 | int handle_escapes; | |
3691 | { | |
3692 | U_CHAR c; | |
3693 | c = *in++; | |
3694 | if (c != '\"') | |
3695 | return NULL; | |
3696 | while (in < limit) | |
3697 | { | |
3698 | U_CHAR c = *in++; | |
3699 | switch (c) | |
3700 | { | |
3701 | case '\0': | |
3702 | return NULL; | |
3703 | case '\"': | |
3704 | limit = in; | |
3705 | break; | |
3706 | case '\\': | |
3707 | if (handle_escapes) | |
3708 | { | |
3709 | char *bpc = (char *) in; | |
3710 | int i = (U_CHAR) cpp_parse_escape (pfile, &bpc); | |
3711 | in = (U_CHAR *) bpc; | |
3712 | if (i >= 0) | |
3713 | *result++ = (U_CHAR)c; | |
3714 | break; | |
3715 | } | |
3716 | /* else fall through */ | |
3717 | default: | |
3718 | *result++ = c; | |
3719 | } | |
3720 | } | |
3721 | *result = 0; | |
3722 | return result; | |
3723 | } | |
3724 | ||
3725 | /* | |
3726 | * interpret #line command. Remembers previously seen fnames | |
3727 | * in its very own hash table. | |
3728 | */ | |
3729 | #define FNAME_HASHSIZE 37 | |
3730 | ||
3731 | static int | |
3732 | do_line (pfile, keyword) | |
3733 | cpp_reader *pfile; | |
3734 | struct directive *keyword; | |
3735 | { | |
3736 | cpp_buffer *ip = CPP_BUFFER (pfile); | |
3737 | int new_lineno; | |
3738 | long old_written = CPP_WRITTEN (pfile); | |
3739 | enum file_change_code file_change = same_file; | |
3740 | enum cpp_token token; | |
3741 | int i; | |
3742 | ||
3743 | token = get_directive_token (pfile); | |
3744 | ||
3745 | if (token != CPP_NUMBER | |
3746 | || !isdigit(pfile->token_buffer[old_written])) | |
3747 | { | |
3748 | cpp_error (pfile, "invalid format `#line' command"); | |
3749 | goto bad_line_directive; | |
3750 | } | |
3751 | ||
3752 | /* The Newline at the end of this line remains to be processed. | |
3753 | To put the next line at the specified line number, | |
3754 | we must store a line number now that is one less. */ | |
e9a25f70 | 3755 | new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1; |
7f2935c7 PB |
3756 | CPP_SET_WRITTEN (pfile, old_written); |
3757 | ||
3758 | /* NEW_LINENO is one less than the actual line number here. */ | |
3759 | if (CPP_PEDANTIC (pfile) && new_lineno < 0) | |
3760 | cpp_pedwarn (pfile, "line number out of range in `#line' command"); | |
3761 | ||
3762 | #if 0 /* #line 10"foo.c" is supposed to be allowed. */ | |
3763 | if (PEEKC() && !is_space[PEEKC()]) { | |
3764 | cpp_error (pfile, "invalid format `#line' command"); | |
3765 | goto bad_line_directive; | |
3766 | } | |
3767 | #endif | |
3768 | ||
3769 | token = get_directive_token (pfile); | |
3770 | ||
3771 | if (token == CPP_STRING) { | |
3772 | U_CHAR *fname = pfile->token_buffer + old_written; | |
3773 | U_CHAR *end_name; | |
3774 | static HASHNODE *fname_table[FNAME_HASHSIZE]; | |
3775 | HASHNODE *hp, **hash_bucket; | |
3776 | U_CHAR *p; | |
3777 | long num_start; | |
3778 | int fname_length; | |
3779 | ||
3780 | /* Turn the file name, which is a character string literal, | |
3781 | into a null-terminated string. Do this in place. */ | |
3782 | end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1); | |
3783 | if (end_name == NULL) | |
3784 | { | |
3785 | cpp_error (pfile, "invalid format `#line' command"); | |
3786 | goto bad_line_directive; | |
3787 | } | |
3788 | ||
3789 | fname_length = end_name - fname; | |
3790 | ||
3791 | num_start = CPP_WRITTEN (pfile); | |
3792 | token = get_directive_token (pfile); | |
3793 | if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) { | |
3794 | p = pfile->token_buffer + num_start; | |
3795 | if (CPP_PEDANTIC (pfile)) | |
3796 | cpp_pedwarn (pfile, "garbage at end of `#line' command"); | |
3797 | ||
3798 | if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0') | |
3799 | { | |
3800 | cpp_error (pfile, "invalid format `#line' command"); | |
3801 | goto bad_line_directive; | |
3802 | } | |
3803 | if (*p == '1') | |
3804 | file_change = enter_file; | |
3805 | else if (*p == 2) | |
3806 | file_change = leave_file; | |
3807 | else if (*p == 3) | |
3808 | ip->system_header_p = 1; | |
3809 | else /* if (*p == 4) */ | |
3810 | ip->system_header_p = 2; | |
3811 | ||
3812 | CPP_SET_WRITTEN (pfile, num_start); | |
3813 | token = get_directive_token (pfile); | |
3814 | p = pfile->token_buffer + num_start; | |
3815 | if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) { | |
3816 | ip->system_header_p = *p == 3 ? 1 : 2; | |
3817 | token = get_directive_token (pfile); | |
3818 | } | |
3819 | if (token != CPP_VSPACE) { | |
3820 | cpp_error (pfile, "invalid format `#line' command"); | |
3821 | goto bad_line_directive; | |
3822 | } | |
3823 | } | |
3824 | ||
e3da301d | 3825 | hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)]; |
7f2935c7 | 3826 | for (hp = *hash_bucket; hp != NULL; hp = hp->next) |
e3da301d MS |
3827 | if (hp->length == fname_length |
3828 | && strncmp (hp->value.cpval, fname, fname_length) == 0) { | |
7f2935c7 PB |
3829 | ip->nominal_fname = hp->value.cpval; |
3830 | break; | |
3831 | } | |
3832 | if (hp == 0) { | |
3833 | /* Didn't find it; cons up a new one. */ | |
3834 | hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1); | |
3835 | hp->next = *hash_bucket; | |
3836 | *hash_bucket = hp; | |
3837 | ||
3838 | hp->length = fname_length; | |
3839 | ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE); | |
3840 | bcopy (fname, hp->value.cpval, fname_length); | |
3841 | } | |
3842 | } | |
3843 | else if (token != CPP_VSPACE && token != CPP_EOF) { | |
3844 | cpp_error (pfile, "invalid format `#line' command"); | |
3845 | goto bad_line_directive; | |
3846 | } | |
3847 | ||
3848 | ip->lineno = new_lineno; | |
3849 | bad_line_directive: | |
3850 | skip_rest_of_line (pfile); | |
3851 | CPP_SET_WRITTEN (pfile, old_written); | |
3852 | output_line_command (pfile, 0, file_change); | |
3853 | return 0; | |
3854 | } | |
3855 | ||
3856 | /* | |
3857 | * remove the definition of a symbol from the symbol table. | |
3858 | * according to un*x /lib/cpp, it is not an error to undef | |
3859 | * something that has no definitions, so it isn't one here either. | |
3860 | */ | |
3861 | ||
3862 | static int | |
3863 | do_undef (pfile, keyword, buf, limit) | |
3864 | cpp_reader *pfile; | |
3865 | struct directive *keyword; | |
3866 | U_CHAR *buf, *limit; | |
3867 | { | |
3868 | int sym_length; | |
3869 | HASHNODE *hp; | |
3870 | U_CHAR *orig_buf = buf; | |
3871 | ||
3872 | #if 0 | |
3873 | /* If this is a precompiler run (with -pcp) pass thru #undef commands. */ | |
3874 | if (pcp_outfile && keyword) | |
3875 | pass_thru_directive (buf, limit, pfile, keyword); | |
3876 | #endif | |
3877 | ||
3878 | SKIP_WHITE_SPACE (buf); | |
3879 | sym_length = check_macro_name (pfile, buf, "macro"); | |
3880 | ||
3881 | while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL) | |
3882 | { | |
3883 | /* If we are generating additional info for debugging (with -g) we | |
3884 | need to pass through all effective #undef commands. */ | |
3885 | if (CPP_OPTIONS (pfile)->debug_output && keyword) | |
3886 | pass_thru_directive (orig_buf, limit, pfile, keyword); | |
3887 | if (hp->type != T_MACRO) | |
3888 | cpp_warning (pfile, "undefining `%s'", hp->name); | |
3889 | delete_macro (hp); | |
3890 | } | |
3891 | ||
3892 | if (CPP_PEDANTIC (pfile)) { | |
3893 | buf += sym_length; | |
3894 | SKIP_WHITE_SPACE (buf); | |
3895 | if (buf != limit) | |
3896 | cpp_pedwarn (pfile, "garbage after `#undef' directive"); | |
3897 | } | |
3898 | return 0; | |
3899 | } | |
3900 | \f | |
3901 | /* | |
3902 | * Report an error detected by the program we are processing. | |
3903 | * Use the text of the line in the error message. | |
3904 | * (We use error because it prints the filename & line#.) | |
3905 | */ | |
3906 | ||
3907 | static int | |
3908 | do_error (pfile, keyword, buf, limit) | |
3909 | cpp_reader *pfile; | |
3910 | struct directive *keyword; | |
3911 | U_CHAR *buf, *limit; | |
3912 | { | |
3913 | int length = limit - buf; | |
52320a47 | 3914 | U_CHAR *copy = (U_CHAR *) alloca (length + 1); |
7f2935c7 PB |
3915 | bcopy (buf, copy, length); |
3916 | copy[length] = 0; | |
3917 | SKIP_WHITE_SPACE (copy); | |
3918 | cpp_error (pfile, "#error %s", copy); | |
3919 | return 0; | |
3920 | } | |
3921 | ||
3922 | /* | |
3923 | * Report a warning detected by the program we are processing. | |
3924 | * Use the text of the line in the warning message, then continue. | |
3925 | * (We use error because it prints the filename & line#.) | |
3926 | */ | |
3927 | ||
3928 | static int | |
3929 | do_warning (pfile, keyword, buf, limit) | |
3930 | cpp_reader *pfile; | |
3931 | struct directive *keyword; | |
3932 | U_CHAR *buf, *limit; | |
3933 | { | |
3934 | int length = limit - buf; | |
52320a47 | 3935 | U_CHAR *copy = (U_CHAR *) alloca (length + 1); |
7f2935c7 PB |
3936 | bcopy (buf, copy, length); |
3937 | copy[length] = 0; | |
3938 | SKIP_WHITE_SPACE (copy); | |
cfb3ee16 RK |
3939 | /* Use `pedwarn' not `warning', because #warning isn't in the C Standard; |
3940 | if -pedantic-errors is given, #warning should cause an error. */ | |
3941 | cpp_pedwarn (pfile, "#warning %s", copy); | |
7f2935c7 PB |
3942 | return 0; |
3943 | } | |
3944 | ||
3945 | /* Remember the name of the current file being read from so that we can | |
3946 | avoid ever including it again. */ | |
3947 | ||
3948 | static int | |
3949 | do_once (pfile) | |
3950 | cpp_reader *pfile; | |
3951 | { | |
3952 | cpp_buffer *ip = NULL; | |
3953 | struct file_name_list *new; | |
3954 | ||
3955 | for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip)) | |
3956 | { | |
d013f05e | 3957 | if (ip == CPP_NULL_BUFFER (pfile)) |
7f2935c7 PB |
3958 | return 0; |
3959 | if (ip->fname != NULL) | |
3960 | break; | |
3961 | } | |
3962 | ||
3963 | ||
3964 | new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); | |
3965 | new->next = pfile->dont_repeat_files; | |
3966 | pfile->dont_repeat_files = new; | |
3967 | new->fname = savestring (ip->fname); | |
3968 | new->control_macro = 0; | |
3969 | new->got_name_map = 0; | |
3970 | new->c_system_include_path = 0; | |
3971 | ||
3972 | return 0; | |
3973 | } | |
3974 | ||
e9a25f70 | 3975 | /* Report program identification. */ |
7f2935c7 PB |
3976 | |
3977 | static int | |
3978 | do_ident (pfile, keyword, buf, limit) | |
3979 | cpp_reader *pfile; | |
3980 | struct directive *keyword; | |
3981 | U_CHAR *buf, *limit; | |
3982 | { | |
3983 | /* long old_written = CPP_WRITTEN (pfile);*/ | |
3984 | int len; | |
3985 | ||
3986 | /* Allow #ident in system headers, since that's not user's fault. */ | |
3987 | if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p) | |
3988 | cpp_pedwarn (pfile, "ANSI C does not allow `#ident'"); | |
3989 | ||
0f41302f | 3990 | /* Leave rest of line to be read by later calls to cpp_get_token. */ |
7f2935c7 PB |
3991 | |
3992 | return 0; | |
3993 | } | |
3994 | ||
3995 | /* #pragma and its argument line have already been copied to the output file. | |
3996 | Just check for some recognized pragmas that need validation here. */ | |
3997 | ||
3998 | static int | |
3999 | do_pragma (pfile, keyword, buf, limit) | |
4000 | cpp_reader *pfile; | |
4001 | struct directive *keyword; | |
4002 | U_CHAR *buf, *limit; | |
4003 | { | |
4004 | while (*buf == ' ' || *buf == '\t') | |
4005 | buf++; | |
4006 | if (!strncmp (buf, "once", 4)) { | |
4007 | /* Allow #pragma once in system headers, since that's not the user's | |
4008 | fault. */ | |
4009 | if (!CPP_BUFFER (pfile)->system_header_p) | |
4010 | cpp_warning (pfile, "`#pragma once' is obsolete"); | |
4011 | do_once (pfile); | |
4012 | } | |
4013 | ||
4014 | if (!strncmp (buf, "implementation", 14)) { | |
4015 | /* Be quiet about `#pragma implementation' for a file only if it hasn't | |
4016 | been included yet. */ | |
4017 | struct file_name_list *ptr; | |
4018 | U_CHAR *p = buf + 14, *fname, *inc_fname; | |
4019 | int fname_len; | |
4020 | SKIP_WHITE_SPACE (p); | |
4021 | if (*p == '\n' || *p != '\"') | |
4022 | return 0; | |
4023 | ||
4024 | fname = p + 1; | |
4025 | p = (U_CHAR *) index (fname, '\"'); | |
4026 | fname_len = p != NULL ? p - fname : strlen (fname); | |
4027 | ||
4028 | for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) { | |
4029 | inc_fname = (U_CHAR *) rindex (ptr->fname, '/'); | |
4030 | inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname; | |
4031 | if (inc_fname && !strncmp (inc_fname, fname, fname_len)) | |
4032 | cpp_warning (pfile, | |
4033 | "`#pragma implementation' for `%s' appears after file is included", | |
4034 | fname); | |
4035 | } | |
4036 | } | |
4037 | ||
4038 | return 0; | |
4039 | } | |
4040 | ||
4041 | #if 0 | |
4042 | /* This was a fun hack, but #pragma seems to start to be useful. | |
4043 | By failing to recognize it, we pass it through unchanged to cc1. */ | |
4044 | ||
4045 | /* | |
4046 | * the behavior of the #pragma directive is implementation defined. | |
4047 | * this implementation defines it as follows. | |
4048 | */ | |
4049 | ||
4050 | static int | |
4051 | do_pragma () | |
4052 | { | |
4053 | close (0); | |
4054 | if (open ("/dev/tty", O_RDONLY, 0666) != 0) | |
4055 | goto nope; | |
4056 | close (1); | |
4057 | if (open ("/dev/tty", O_WRONLY, 0666) != 1) | |
4058 | goto nope; | |
4059 | execl ("/usr/games/hack", "#pragma", 0); | |
4060 | execl ("/usr/games/rogue", "#pragma", 0); | |
4061 | execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0); | |
4062 | execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0); | |
4063 | nope: | |
4064 | fatal ("You are in a maze of twisty compiler features, all different"); | |
4065 | } | |
4066 | #endif | |
4067 | ||
4068 | /* Just ignore #sccs, on systems where we define it at all. */ | |
4069 | ||
4070 | static int | |
4071 | do_sccs (pfile, keyword, buf, limit) | |
4072 | cpp_reader *pfile; | |
4073 | struct directive *keyword; | |
4074 | U_CHAR *buf, *limit; | |
4075 | { | |
4076 | if (CPP_PEDANTIC (pfile)) | |
4077 | cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'"); | |
4078 | return 0; | |
4079 | } | |
4080 | \f | |
4081 | /* | |
4082 | * handle #if command by | |
4083 | * 1) inserting special `defined' keyword into the hash table | |
4084 | * that gets turned into 0 or 1 by special_symbol (thus, | |
4085 | * if the luser has a symbol called `defined' already, it won't | |
4086 | * work inside the #if command) | |
4087 | * 2) rescan the input into a temporary output buffer | |
4088 | * 3) pass the output buffer to the yacc parser and collect a value | |
4089 | * 4) clean up the mess left from steps 1 and 2. | |
4090 | * 5) call conditional_skip to skip til the next #endif (etc.), | |
4091 | * or not, depending on the value from step 3. | |
4092 | */ | |
4093 | ||
4094 | static int | |
4095 | do_if (pfile, keyword, buf, limit) | |
4096 | cpp_reader *pfile; | |
4097 | struct directive *keyword; | |
4098 | U_CHAR *buf, *limit; | |
4099 | { | |
6be492ab | 4100 | HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf); |
7f2935c7 PB |
4101 | conditional_skip (pfile, value == 0, T_IF, NULL_PTR); |
4102 | return 0; | |
4103 | } | |
4104 | ||
4105 | /* | |
4106 | * handle a #elif directive by not changing if_stack either. | |
4107 | * see the comment above do_else. | |
4108 | */ | |
4109 | ||
4110 | static int | |
4111 | do_elif (pfile, keyword, buf, limit) | |
4112 | cpp_reader *pfile; | |
4113 | struct directive *keyword; | |
4114 | U_CHAR *buf, *limit; | |
4115 | { | |
7f2935c7 PB |
4116 | if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) { |
4117 | cpp_error (pfile, "`#elif' not within a conditional"); | |
4118 | return 0; | |
4119 | } else { | |
4120 | if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) { | |
4121 | cpp_error (pfile, "`#elif' after `#else'"); | |
4122 | #if 0 | |
4123 | fprintf (stderr, " (matches line %d", pfile->if_stack->lineno); | |
4124 | #endif | |
4125 | if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL | |
4126 | && strcmp (pfile->if_stack->fname, | |
4127 | CPP_BUFFER (pfile)->nominal_fname) != 0) | |
4128 | fprintf (stderr, ", file %s", pfile->if_stack->fname); | |
4129 | fprintf (stderr, ")\n"); | |
4130 | } | |
4131 | pfile->if_stack->type = T_ELIF; | |
4132 | } | |
4133 | ||
4134 | if (pfile->if_stack->if_succeeded) | |
4135 | skip_if_group (pfile, 0); | |
4136 | else { | |
6be492ab | 4137 | HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf); |
7f2935c7 PB |
4138 | if (value == 0) |
4139 | skip_if_group (pfile, 0); | |
4140 | else { | |
4141 | ++pfile->if_stack->if_succeeded; /* continue processing input */ | |
4142 | output_line_command (pfile, 1, same_file); | |
4143 | } | |
4144 | } | |
4145 | return 0; | |
4146 | } | |
4147 | ||
4148 | /* | |
4149 | * evaluate a #if expression in BUF, of length LENGTH, | |
4150 | * then parse the result as a C expression and return the value as an int. | |
4151 | */ | |
0f41302f | 4152 | |
6be492ab | 4153 | static HOST_WIDE_INT |
7f2935c7 PB |
4154 | eval_if_expression (pfile, buf, length) |
4155 | cpp_reader *pfile; | |
4156 | U_CHAR *buf; | |
4157 | int length; | |
4158 | { | |
4159 | HASHNODE *save_defined; | |
6be492ab | 4160 | HOST_WIDE_INT value; |
7f2935c7 PB |
4161 | long old_written = CPP_WRITTEN (pfile); |
4162 | ||
e9a25f70 | 4163 | save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1); |
7f2935c7 PB |
4164 | pfile->pcp_inside_if = 1; |
4165 | ||
4166 | value = cpp_parse_expr (pfile); | |
4167 | pfile->pcp_inside_if = 0; | |
4168 | delete_macro (save_defined); /* clean up special symbol */ | |
4169 | ||
4170 | CPP_SET_WRITTEN (pfile, old_written); /* Pop */ | |
4171 | ||
4172 | return value; | |
4173 | } | |
4174 | ||
4175 | /* | |
4176 | * routine to handle ifdef/ifndef. Try to look up the symbol, | |
4177 | * then do or don't skip to the #endif/#else/#elif depending | |
4178 | * on what directive is actually being processed. | |
4179 | */ | |
4180 | ||
4181 | static int | |
4182 | do_xifdef (pfile, keyword, unused1, unused2) | |
4183 | cpp_reader *pfile; | |
4184 | struct directive *keyword; | |
4185 | U_CHAR *unused1, *unused2; | |
4186 | { | |
4187 | int skip; | |
4188 | cpp_buffer *ip = CPP_BUFFER (pfile); | |
0f41302f | 4189 | U_CHAR *ident; |
7f2935c7 PB |
4190 | int ident_length; |
4191 | enum cpp_token token; | |
4192 | int start_of_file = 0; | |
4193 | U_CHAR *control_macro = 0; | |
4194 | int old_written = CPP_WRITTEN (pfile); | |
4195 | ||
4196 | /* Detect a #ifndef at start of file (not counting comments). */ | |
4197 | if (ip->fname != 0 && keyword->type == T_IFNDEF) | |
4198 | start_of_file = pfile->only_seen_white == 2; | |
4199 | ||
4200 | pfile->no_macro_expand++; | |
4201 | token = get_directive_token (pfile); | |
4202 | pfile->no_macro_expand--; | |
4203 | ||
4204 | ident = pfile->token_buffer + old_written; | |
4205 | ident_length = CPP_WRITTEN (pfile) - old_written; | |
4206 | CPP_SET_WRITTEN (pfile, old_written); /* Pop */ | |
4207 | ||
4208 | if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF) | |
4209 | { | |
4210 | skip = (keyword->type == T_IFDEF); | |
4211 | if (! CPP_TRADITIONAL (pfile)) | |
4212 | cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name); | |
4213 | } | |
4214 | else if (token == CPP_NAME) | |
4215 | { | |
4216 | HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1); | |
4217 | skip = (hp == NULL) ^ (keyword->type == T_IFNDEF); | |
4218 | if (start_of_file && !skip) | |
4219 | { | |
4220 | control_macro = (U_CHAR *) xmalloc (ident_length + 1); | |
4221 | bcopy (ident, control_macro, ident_length + 1); | |
4222 | } | |
4223 | } | |
4224 | else | |
4225 | { | |
4226 | skip = (keyword->type == T_IFDEF); | |
4227 | if (! CPP_TRADITIONAL (pfile)) | |
4228 | cpp_error (pfile, "`#%s' with invalid argument", keyword->name); | |
4229 | } | |
4230 | ||
4231 | if (!CPP_TRADITIONAL (pfile)) | |
4232 | { int c; | |
4233 | cpp_skip_hspace (pfile); | |
4234 | c = PEEKC (); | |
4235 | if (c != EOF && c != '\n') | |
4236 | cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name); | |
4237 | } | |
4238 | skip_rest_of_line (pfile); | |
4239 | ||
4240 | #if 0 | |
4241 | if (pcp_outfile) { | |
4242 | /* Output a precondition for this macro. */ | |
4243 | if (hp && hp->value.defn->predefined) | |
4244 | fprintf (pcp_outfile, "#define %s\n", hp->name); | |
4245 | else { | |
4246 | U_CHAR *cp = buf; | |
4247 | fprintf (pcp_outfile, "#undef "); | |
4248 | while (is_idchar[*cp]) /* Ick! */ | |
4249 | fputc (*cp++, pcp_outfile); | |
4250 | putc ('\n', pcp_outfile); | |
4251 | } | |
4252 | #endif | |
4253 | ||
4254 | conditional_skip (pfile, skip, T_IF, control_macro); | |
4255 | return 0; | |
4256 | } | |
4257 | ||
4258 | /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead. | |
4259 | If this is a #ifndef starting at the beginning of a file, | |
4260 | CONTROL_MACRO is the macro name tested by the #ifndef. | |
4261 | Otherwise, CONTROL_MACRO is 0. */ | |
4262 | ||
4263 | static void | |
4264 | conditional_skip (pfile, skip, type, control_macro) | |
4265 | cpp_reader *pfile; | |
4266 | int skip; | |
4267 | enum node_type type; | |
4268 | U_CHAR *control_macro; | |
4269 | { | |
4270 | IF_STACK_FRAME *temp; | |
4271 | ||
4272 | temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME)); | |
4273 | temp->fname = CPP_BUFFER (pfile)->nominal_fname; | |
4274 | #if 0 | |
4275 | temp->lineno = CPP_BUFFER (pfile)->lineno; | |
4276 | #endif | |
4277 | temp->next = pfile->if_stack; | |
4278 | temp->control_macro = control_macro; | |
4279 | pfile->if_stack = temp; | |
4280 | ||
4281 | pfile->if_stack->type = type; | |
4282 | ||
4283 | if (skip != 0) { | |
4284 | skip_if_group (pfile, 0); | |
4285 | return; | |
4286 | } else { | |
4287 | ++pfile->if_stack->if_succeeded; | |
4288 | output_line_command (pfile, 1, same_file); | |
4289 | } | |
4290 | } | |
4291 | ||
4292 | /* | |
4293 | * skip to #endif, #else, or #elif. adjust line numbers, etc. | |
4294 | * leaves input ptr at the sharp sign found. | |
4295 | * If ANY is nonzero, return at next directive of any sort. | |
4296 | */ | |
0f41302f | 4297 | |
7f2935c7 PB |
4298 | static void |
4299 | skip_if_group (pfile, any) | |
4300 | cpp_reader *pfile; | |
4301 | int any; | |
4302 | { | |
4303 | int c; | |
4304 | int at_beg_of_line = 1; | |
4305 | struct directive *kt; | |
4306 | IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */ | |
4307 | #if 0 | |
4308 | U_CHAR *beg_of_line = bp; | |
4309 | #endif | |
4310 | register int ident_length; | |
4311 | U_CHAR *ident, *after_ident; | |
4312 | struct parse_marker line_start_mark; | |
4313 | ||
4314 | parse_set_mark (&line_start_mark, pfile); | |
4315 | ||
4316 | if (CPP_OPTIONS (pfile)->output_conditionals) { | |
4317 | static char failed[] = "#failed\n"; | |
4318 | CPP_PUTS (pfile, failed, sizeof(failed)-1); | |
4319 | pfile->lineno++; | |
4320 | output_line_command (pfile, 1, same_file); | |
4321 | } | |
4322 | ||
4323 | beg_of_line: | |
4324 | if (CPP_OPTIONS (pfile)->output_conditionals) | |
4325 | { | |
4326 | cpp_buffer *pbuf = CPP_BUFFER (pfile); | |
4327 | U_CHAR *start_line = pbuf->buf + line_start_mark.position; | |
4328 | CPP_PUTS (pfile, start_line, pbuf->cur - start_line); | |
4329 | } | |
4330 | parse_move_mark (&line_start_mark, pfile); | |
4331 | if (!CPP_TRADITIONAL (pfile)) | |
4332 | cpp_skip_hspace (pfile); | |
4333 | c = GETC(); | |
4334 | if (c == '#') | |
4335 | { | |
4336 | int old_written = CPP_WRITTEN (pfile); | |
4337 | cpp_skip_hspace (pfile); | |
4338 | ||
4339 | parse_name (pfile, GETC()); | |
4340 | ident_length = CPP_WRITTEN (pfile) - old_written; | |
4341 | ident = pfile->token_buffer + old_written; | |
4342 | pfile->limit = ident; | |
4343 | #if 0 | |
4344 | if (ident_length == 0) | |
4345 | goto not_a_directive; | |
4346 | ||
4347 | /* Handle # followed by a line number. */ | |
4348 | ||
4349 | /* Avoid error for `###' and similar cases unless -pedantic. */ | |
4350 | #endif | |
4351 | ||
4352 | for (kt = directive_table; kt->length >= 0; kt++) | |
4353 | { | |
4354 | IF_STACK_FRAME *temp; | |
4355 | if (ident_length == kt->length | |
4356 | && strncmp (ident, kt->name, kt->length) == 0) | |
4357 | { | |
4358 | /* If we are asked to return on next directive, do so now. */ | |
4359 | if (any) | |
4360 | goto done; | |
4361 | ||
4362 | switch (kt->type) | |
4363 | { | |
4364 | case T_IF: | |
4365 | case T_IFDEF: | |
4366 | case T_IFNDEF: | |
4367 | temp | |
4368 | = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME)); | |
4369 | temp->next = pfile->if_stack; | |
4370 | pfile->if_stack = temp; | |
4371 | #if 0 | |
4372 | temp->lineno = CPP_BUFFER(pfile)->lineno; | |
4373 | #endif | |
4374 | temp->fname = CPP_BUFFER(pfile)->nominal_fname; | |
4375 | temp->type = kt->type; | |
4376 | break; | |
4377 | case T_ELSE: | |
4378 | case T_ENDIF: | |
4379 | if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack) | |
4380 | validate_else (pfile, | |
4381 | kt->type == T_ELSE ? "#else" : "#endif"); | |
4382 | case T_ELIF: | |
4383 | if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) | |
4384 | { | |
4385 | cpp_error (pfile, | |
4386 | "`#%s' not within a conditional", kt->name); | |
4387 | break; | |
4388 | } | |
4389 | else if (pfile->if_stack == save_if_stack) | |
4390 | goto done; /* found what we came for */ | |
4391 | ||
4392 | if (kt->type != T_ENDIF) | |
4393 | { | |
4394 | if (pfile->if_stack->type == T_ELSE) | |
4395 | cpp_error (pfile, "`#else' or `#elif' after `#else'"); | |
4396 | pfile->if_stack->type = kt->type; | |
4397 | break; | |
4398 | } | |
4399 | ||
4400 | temp = pfile->if_stack; | |
4401 | pfile->if_stack = temp->next; | |
4402 | free (temp); | |
4403 | break; | |
4404 | default: ; | |
4405 | } | |
4406 | break; | |
4407 | } | |
4408 | /* Don't let erroneous code go by. */ | |
4409 | if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm | |
4410 | && CPP_PEDANTIC (pfile)) | |
4411 | cpp_pedwarn (pfile, "invalid preprocessor directive name"); | |
4412 | } | |
4413 | c = GETC (); | |
4414 | } | |
0f41302f | 4415 | /* We're in the middle of a line. Skip the rest of it. */ |
7f2935c7 PB |
4416 | for (;;) { |
4417 | switch (c) | |
4418 | { | |
3232050c | 4419 | long old; |
7f2935c7 PB |
4420 | case EOF: |
4421 | goto done; | |
4422 | case '/': /* possible comment */ | |
4423 | c = skip_comment (pfile, NULL); | |
4424 | if (c == EOF) | |
4425 | goto done; | |
4426 | break; | |
4427 | case '\"': | |
4428 | case '\'': | |
3232050c PB |
4429 | FORWARD(-1); |
4430 | old = CPP_WRITTEN (pfile); | |
4431 | cpp_get_token (pfile); | |
4432 | CPP_SET_WRITTEN (pfile, old); | |
7f2935c7 PB |
4433 | break; |
4434 | case '\\': | |
4435 | /* Char after backslash loses its special meaning. */ | |
4436 | if (PEEKC() == '\n') | |
4437 | FORWARD (1); | |
4438 | break; | |
4439 | case '\n': | |
4440 | goto beg_of_line; | |
4441 | break; | |
4442 | } | |
4443 | c = GETC (); | |
4444 | } | |
4445 | done: | |
4446 | if (CPP_OPTIONS (pfile)->output_conditionals) { | |
4447 | static char end_failed[] = "#endfailed\n"; | |
4448 | CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1); | |
4449 | pfile->lineno++; | |
4450 | } | |
4451 | pfile->only_seen_white = 1; | |
4452 | parse_goto_mark (&line_start_mark, pfile); | |
4453 | parse_clear_mark (&line_start_mark); | |
4454 | } | |
4455 | ||
4456 | /* | |
4457 | * handle a #else directive. Do this by just continuing processing | |
4458 | * without changing if_stack ; this is so that the error message | |
4459 | * for missing #endif's etc. will point to the original #if. It | |
4460 | * is possible that something different would be better. | |
4461 | */ | |
4462 | ||
4463 | static int | |
4464 | do_else (pfile, keyword, buf, limit) | |
4465 | cpp_reader *pfile; | |
4466 | struct directive *keyword; | |
4467 | U_CHAR *buf, *limit; | |
4468 | { | |
4469 | cpp_buffer *ip = CPP_BUFFER (pfile); | |
4470 | ||
4471 | if (CPP_PEDANTIC (pfile)) | |
4472 | validate_else (pfile, "#else"); | |
4473 | skip_rest_of_line (pfile); | |
4474 | ||
4475 | if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) { | |
4476 | cpp_error (pfile, "`#else' not within a conditional"); | |
4477 | return 0; | |
4478 | } else { | |
4479 | /* #ifndef can't have its special treatment for containing the whole file | |
4480 | if it has a #else clause. */ | |
4481 | pfile->if_stack->control_macro = 0; | |
4482 | ||
4483 | if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) { | |
4484 | cpp_error (pfile, "`#else' after `#else'"); | |
4485 | fprintf (stderr, " (matches line %d", pfile->if_stack->lineno); | |
4486 | if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0) | |
4487 | fprintf (stderr, ", file %s", pfile->if_stack->fname); | |
4488 | fprintf (stderr, ")\n"); | |
4489 | } | |
4490 | pfile->if_stack->type = T_ELSE; | |
4491 | } | |
4492 | ||
4493 | if (pfile->if_stack->if_succeeded) | |
4494 | skip_if_group (pfile, 0); | |
4495 | else { | |
4496 | ++pfile->if_stack->if_succeeded; /* continue processing input */ | |
4497 | output_line_command (pfile, 1, same_file); | |
4498 | } | |
4499 | return 0; | |
4500 | } | |
4501 | ||
4502 | /* | |
4503 | * unstack after #endif command | |
4504 | */ | |
4505 | ||
4506 | static int | |
4507 | do_endif (pfile, keyword, buf, limit) | |
4508 | cpp_reader *pfile; | |
4509 | struct directive *keyword; | |
4510 | U_CHAR *buf, *limit; | |
4511 | { | |
4512 | if (CPP_PEDANTIC (pfile)) | |
4513 | validate_else (pfile, "#endif"); | |
4514 | skip_rest_of_line (pfile); | |
4515 | ||
4516 | if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) | |
4517 | cpp_error (pfile, "unbalanced `#endif'"); | |
4518 | else | |
4519 | { | |
4520 | IF_STACK_FRAME *temp = pfile->if_stack; | |
4521 | pfile->if_stack = temp->next; | |
4522 | if (temp->control_macro != 0) | |
4523 | { | |
4524 | /* This #endif matched a #ifndef at the start of the file. | |
4525 | See if it is at the end of the file. */ | |
4526 | struct parse_marker start_mark; | |
4527 | int c; | |
4528 | ||
4529 | parse_set_mark (&start_mark, pfile); | |
4530 | ||
4531 | for (;;) | |
4532 | { | |
4533 | cpp_skip_hspace (pfile); | |
4534 | c = GETC (); | |
4535 | if (c != '\n') | |
4536 | break; | |
4537 | } | |
4538 | parse_goto_mark (&start_mark, pfile); | |
4539 | parse_clear_mark (&start_mark); | |
4540 | ||
4541 | if (c == EOF) | |
4542 | { | |
4543 | /* If we get here, this #endif ends a #ifndef | |
4544 | that contains all of the file (aside from whitespace). | |
4545 | Arrange not to include the file again | |
4546 | if the macro that was tested is defined. | |
4547 | ||
4548 | Do not do this for the top-level file in a -include or any | |
4549 | file in a -imacros. */ | |
4550 | #if 0 | |
4551 | FIXME! | |
4552 | if (indepth != 0 | |
4553 | && ! (indepth == 1 && pfile->no_record_file) | |
4554 | && ! (pfile->no_record_file && no_output)) | |
4555 | #endif | |
4556 | { | |
4557 | struct file_name_list *ifile = pfile->all_include_files; | |
4558 | ||
4559 | for ( ; ifile != NULL; ifile = ifile->next) | |
4560 | { | |
4561 | if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname)) | |
4562 | { | |
4563 | ifile->control_macro = temp->control_macro; | |
4564 | break; | |
4565 | } | |
4566 | } | |
4567 | } | |
4568 | } | |
4569 | } | |
4570 | free (temp); | |
4571 | output_line_command (pfile, 1, same_file); | |
4572 | } | |
4573 | return 0; | |
4574 | } | |
4575 | ||
4576 | /* When an #else or #endif is found while skipping failed conditional, | |
4577 | if -pedantic was specified, this is called to warn about text after | |
4578 | the command name. P points to the first char after the command name. */ | |
4579 | ||
4580 | static void | |
4581 | validate_else (pfile, directive) | |
4582 | cpp_reader *pfile; | |
4583 | char *directive; | |
4584 | { | |
4585 | int c; | |
4586 | cpp_skip_hspace (pfile); | |
4587 | c = PEEKC (); | |
4588 | if (c != EOF && c != '\n') | |
4589 | cpp_pedwarn (pfile, | |
4590 | "text following `%s' violates ANSI standard", directive); | |
4591 | } | |
4592 | ||
22bbceaf | 4593 | /* Get the next token, and add it to the text in pfile->token_buffer. |
0f41302f | 4594 | Return the kind of token we got. */ |
7f2935c7 | 4595 | |
7f2935c7 PB |
4596 | enum cpp_token |
4597 | cpp_get_token (pfile) | |
4598 | cpp_reader *pfile; | |
4599 | { | |
4600 | register int c, c2, c3; | |
4601 | long old_written; | |
3232050c | 4602 | long start_line, start_column; |
7f2935c7 PB |
4603 | enum cpp_token token; |
4604 | struct cpp_options *opts = CPP_OPTIONS (pfile); | |
4605 | CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur; | |
4606 | get_next: | |
4607 | c = GETC(); | |
4608 | if (c == EOF) | |
4609 | { | |
4610 | handle_eof: | |
4611 | if (CPP_BUFFER (pfile)->seen_eof) | |
4612 | { | |
4613 | if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile)) | |
4614 | goto get_next; | |
4615 | else | |
4616 | return CPP_EOF; | |
4617 | } | |
4618 | else | |
4619 | { | |
4620 | cpp_buffer *next_buf | |
4621 | = CPP_PREV_BUFFER (CPP_BUFFER (pfile)); | |
4622 | CPP_BUFFER (pfile)->seen_eof = 1; | |
d013f05e PB |
4623 | if (CPP_BUFFER (pfile)->nominal_fname |
4624 | && next_buf != CPP_NULL_BUFFER (pfile)) | |
7f2935c7 PB |
4625 | { |
4626 | /* We're about to return from an #include file. | |
ddd5a7c1 | 4627 | Emit #line information now (as part of the CPP_POP) result. |
0f41302f | 4628 | But the #line refers to the file we will pop to. */ |
7f2935c7 PB |
4629 | cpp_buffer *cur_buffer = CPP_BUFFER (pfile); |
4630 | CPP_BUFFER (pfile) = next_buf; | |
4631 | pfile->input_stack_listing_current = 0; | |
4632 | output_line_command (pfile, 0, leave_file); | |
4633 | CPP_BUFFER (pfile) = cur_buffer; | |
4634 | } | |
4635 | return CPP_POP; | |
4636 | } | |
4637 | } | |
4638 | else | |
4639 | { | |
4640 | switch (c) | |
4641 | { | |
4642 | long newlines; | |
4643 | struct parse_marker start_mark; | |
4644 | case '/': | |
4645 | if (PEEKC () == '=') | |
4646 | goto op2; | |
4647 | if (opts->put_out_comments) | |
4648 | parse_set_mark (&start_mark, pfile); | |
4649 | newlines = 0; | |
3232050c PB |
4650 | cpp_buf_line_and_col (cpp_file_buffer (pfile), |
4651 | &start_line, &start_column); | |
7f2935c7 PB |
4652 | c = skip_comment (pfile, &newlines); |
4653 | if (opts->put_out_comments && (c == '/' || c == EOF)) | |
4654 | parse_clear_mark (&start_mark); | |
4655 | if (c == '/') | |
4656 | goto randomchar; | |
4657 | if (c == EOF) | |
4658 | { | |
3232050c | 4659 | cpp_error_with_line (pfile, start_line, start_column, |
7f2935c7 PB |
4660 | "unterminated comment"); |
4661 | goto handle_eof; | |
4662 | } | |
0f41302f | 4663 | c = '/'; /* Initial letter of comment. */ |
7f2935c7 PB |
4664 | return_comment: |
4665 | /* Comments are equivalent to spaces. | |
4666 | For -traditional, a comment is equivalent to nothing. */ | |
4667 | if (opts->put_out_comments) | |
4668 | { | |
4669 | cpp_buffer *pbuf = CPP_BUFFER (pfile); | |
4670 | long dummy; | |
4671 | U_CHAR *start = pbuf->buf + start_mark.position; | |
4672 | int len = pbuf->cur - start; | |
4673 | CPP_RESERVE(pfile, 1 + len); | |
4674 | CPP_PUTC_Q (pfile, c); | |
4675 | CPP_PUTS_Q (pfile, start, len); | |
4676 | pfile->lineno += newlines; | |
4677 | parse_clear_mark (&start_mark); | |
4678 | return CPP_COMMENT; | |
4679 | } | |
4680 | else if (CPP_TRADITIONAL (pfile)) | |
355142da PB |
4681 | { |
4682 | return CPP_COMMENT; | |
4683 | } | |
7f2935c7 PB |
4684 | else |
4685 | { | |
4686 | #if 0 | |
4687 | /* This may not work if cpp_get_token is called recursively, | |
0f41302f | 4688 | since many places look for horizontal space. */ |
7f2935c7 PB |
4689 | if (newlines) |
4690 | { | |
4691 | /* Copy the newlines into the output buffer, in order to | |
4692 | avoid the pain of a #line every time a multiline comment | |
4693 | is seen. */ | |
4694 | CPP_RESERVE(pfile, newlines); | |
4695 | while (--newlines >= 0) | |
4696 | { | |
4697 | CPP_PUTC_Q (pfile, '\n'); | |
4698 | pfile->lineno++; | |
4699 | } | |
4700 | return CPP_VSPACE; | |
4701 | } | |
4702 | #endif | |
4703 | CPP_RESERVE(pfile, 1); | |
4704 | CPP_PUTC_Q (pfile, ' '); | |
4705 | return CPP_HSPACE; | |
4706 | } | |
7f2935c7 | 4707 | #if 0 |
782331f4 | 4708 | if (opts->for_lint) { |
7f2935c7 PB |
4709 | U_CHAR *argbp; |
4710 | int cmdlen, arglen; | |
4711 | char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen); | |
4712 | ||
4713 | if (lintcmd != NULL) { | |
4714 | /* I believe it is always safe to emit this newline: */ | |
4715 | obp[-1] = '\n'; | |
4716 | bcopy ("#pragma lint ", (char *) obp, 13); | |
4717 | obp += 13; | |
4718 | bcopy (lintcmd, (char *) obp, cmdlen); | |
4719 | obp += cmdlen; | |
4720 | ||
4721 | if (arglen != 0) { | |
4722 | *(obp++) = ' '; | |
4723 | bcopy (argbp, (char *) obp, arglen); | |
4724 | obp += arglen; | |
4725 | } | |
4726 | ||
4727 | /* OK, now bring us back to the state we were in before we entered | |
4728 | this branch. We need #line b/c the newline for the pragma | |
0f41302f | 4729 | could fuck things up. */ |
7f2935c7 PB |
4730 | output_line_command (pfile, 0, same_file); |
4731 | *(obp++) = ' '; /* just in case, if comments are copied thru */ | |
4732 | *(obp++) = '/'; | |
4733 | } | |
7f2935c7 | 4734 | } |
782331f4 | 4735 | #endif |
7f2935c7 PB |
4736 | |
4737 | case '#': | |
4738 | #if 0 | |
4739 | /* If this is expanding a macro definition, don't recognize | |
4740 | preprocessor directives. */ | |
4741 | if (ip->macro != 0) | |
4742 | goto randomchar; | |
4743 | /* If this is expand_into_temp_buffer, recognize them | |
4744 | only after an actual newline at this level, | |
4745 | not at the beginning of the input level. */ | |
4746 | if (ip->fname == 0 && beg_of_line == ip->buf) | |
4747 | goto randomchar; | |
4748 | if (ident_length) | |
4749 | goto specialchar; | |
4750 | #endif | |
4751 | ||
4752 | if (!pfile->only_seen_white) | |
4753 | goto randomchar; | |
4754 | if (handle_directive (pfile)) | |
4755 | return CPP_DIRECTIVE; | |
4756 | pfile->only_seen_white = 0; | |
4757 | return CPP_OTHER; | |
4758 | ||
4759 | case '\"': | |
4760 | case '\'': | |
4761 | /* A single quoted string is treated like a double -- some | |
4762 | programs (e.g., troff) are perverse this way */ | |
3232050c PB |
4763 | cpp_buf_line_and_col (cpp_file_buffer (pfile), |
4764 | &start_line, &start_column); | |
7f2935c7 PB |
4765 | old_written = CPP_WRITTEN (pfile); |
4766 | string: | |
4767 | CPP_PUTC (pfile, c); | |
4768 | while (1) | |
4769 | { | |
4770 | int cc = GETC(); | |
4771 | if (cc == EOF) | |
4772 | { | |
4773 | if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile))) | |
4774 | { | |
4775 | /* try harder: this string crosses a macro expansion | |
4776 | boundary. This can happen naturally if -traditional. | |
4777 | Otherwise, only -D can make a macro with an unmatched | |
4778 | quote. */ | |
4779 | cpp_buffer *next_buf | |
4780 | = CPP_PREV_BUFFER (CPP_BUFFER (pfile)); | |
4781 | (*CPP_BUFFER (pfile)->cleanup) | |
4782 | (CPP_BUFFER (pfile), pfile); | |
4783 | CPP_BUFFER (pfile) = next_buf; | |
4784 | continue; | |
4785 | } | |
7f2935c7 PB |
4786 | if (!CPP_TRADITIONAL (pfile)) |
4787 | { | |
3232050c | 4788 | cpp_error_with_line (pfile, start_line, start_column, |
7f2935c7 | 4789 | "unterminated string or character constant"); |
3232050c PB |
4790 | if (pfile->multiline_string_line != start_line |
4791 | && pfile->multiline_string_line != 0) | |
4792 | cpp_error_with_line (pfile, | |
4793 | pfile->multiline_string_line, -1, | |
4794 | "possible real start of unterminated constant"); | |
4795 | pfile->multiline_string_line = 0; | |
7f2935c7 | 4796 | } |
7f2935c7 PB |
4797 | break; |
4798 | } | |
4799 | CPP_PUTC (pfile, cc); | |
4800 | switch (cc) | |
4801 | { | |
4802 | case '\n': | |
4803 | /* Traditionally, end of line ends a string constant with | |
4804 | no error. So exit the loop and record the new line. */ | |
4805 | if (CPP_TRADITIONAL (pfile)) | |
4806 | goto while2end; | |
7f2935c7 PB |
4807 | if (c == '\'') |
4808 | { | |
3232050c | 4809 | cpp_error_with_line (pfile, start_line, start_column, |
e8037d57 | 4810 | "unterminated character constant"); |
7f2935c7 PB |
4811 | goto while2end; |
4812 | } | |
3232050c PB |
4813 | if (CPP_PEDANTIC (pfile) |
4814 | && pfile->multiline_string_line == 0) | |
7f2935c7 | 4815 | { |
3232050c | 4816 | cpp_pedwarn_with_line (pfile, start_line, start_column, |
7f2935c7 PB |
4817 | "string constant runs past end of line"); |
4818 | } | |
3232050c PB |
4819 | if (pfile->multiline_string_line == 0) |
4820 | pfile->multiline_string_line = start_line; | |
7f2935c7 PB |
4821 | break; |
4822 | ||
4823 | case '\\': | |
4824 | cc = GETC(); | |
4825 | if (cc == '\n') | |
4826 | { | |
0f41302f | 4827 | /* Backslash newline is replaced by nothing at all. */ |
7f2935c7 PB |
4828 | CPP_ADJUST_WRITTEN (pfile, -1); |
4829 | pfile->lineno++; | |
4830 | } | |
4831 | else | |
4832 | { | |
4833 | /* ANSI stupidly requires that in \\ the second \ | |
4834 | is *not* prevented from combining with a newline. */ | |
4835 | NEWLINE_FIX1(cc); | |
4836 | if (cc != EOF) | |
4837 | CPP_PUTC (pfile, cc); | |
4838 | } | |
4839 | break; | |
4840 | ||
4841 | case '\"': | |
4842 | case '\'': | |
4843 | if (cc == c) | |
4844 | goto while2end; | |
4845 | break; | |
4846 | } | |
4847 | } | |
4848 | while2end: | |
4849 | pfile->lineno += count_newlines (pfile->token_buffer + old_written, | |
4850 | CPP_PWRITTEN (pfile)); | |
4851 | pfile->only_seen_white = 0; | |
4852 | return c == '\'' ? CPP_CHAR : CPP_STRING; | |
4853 | ||
4854 | case '$': | |
4855 | if (!opts->dollars_in_ident) | |
4856 | goto randomchar; | |
4857 | goto letter; | |
4858 | ||
4859 | case ':': | |
4860 | if (opts->cplusplus && PEEKC () == ':') | |
4861 | goto op2; | |
4862 | goto randomchar; | |
4863 | ||
4864 | case '&': | |
4865 | case '+': | |
4866 | case '|': | |
4867 | NEWLINE_FIX; | |
4868 | c2 = PEEKC (); | |
4869 | if (c2 == c || c2 == '=') | |
4870 | goto op2; | |
4871 | goto randomchar; | |
4872 | ||
4873 | case '*': | |
4874 | case '!': | |
4875 | case '%': | |
4876 | case '=': | |
4877 | case '^': | |
4878 | NEWLINE_FIX; | |
4879 | if (PEEKC () == '=') | |
4880 | goto op2; | |
4881 | goto randomchar; | |
4882 | ||
4883 | case '-': | |
4884 | NEWLINE_FIX; | |
4885 | c2 = PEEKC (); | |
4886 | if (c2 == '-' && opts->chill) | |
4887 | { | |
4888 | /* Chill style comment */ | |
4889 | if (opts->put_out_comments) | |
4890 | parse_set_mark (&start_mark, pfile); | |
0f41302f | 4891 | FORWARD(1); /* Skip second '-'. */ |
7f2935c7 PB |
4892 | for (;;) |
4893 | { | |
4894 | c = GETC (); | |
4895 | if (c == EOF) | |
4896 | break; | |
4897 | if (c == '\n') | |
4898 | { | |
0f41302f | 4899 | /* Don't consider final '\n' to be part of comment. */ |
7f2935c7 PB |
4900 | FORWARD(-1); |
4901 | break; | |
4902 | } | |
4903 | } | |
4904 | c = '-'; | |
4905 | goto return_comment; | |
4906 | } | |
4907 | if (c2 == '-' || c2 == '=' || c2 == '>') | |
4908 | goto op2; | |
4909 | goto randomchar; | |
4910 | ||
4911 | case '<': | |
4912 | if (pfile->parsing_include_directive) | |
4913 | { | |
4914 | for (;;) | |
4915 | { | |
4916 | CPP_PUTC (pfile, c); | |
4917 | if (c == '>') | |
4918 | break; | |
4919 | c = GETC (); | |
4920 | NEWLINE_FIX1 (c); | |
4921 | if (c == '\n' || c == EOF) | |
4922 | { | |
4923 | cpp_error (pfile, | |
4924 | "missing '>' in `#include <FILENAME>'"); | |
4925 | break; | |
4926 | } | |
4927 | } | |
4928 | return CPP_STRING; | |
4929 | } | |
4930 | /* else fall through */ | |
4931 | case '>': | |
4932 | NEWLINE_FIX; | |
4933 | c2 = PEEKC (); | |
4934 | if (c2 == '=') | |
4935 | goto op2; | |
4936 | if (c2 != c) | |
4937 | goto randomchar; | |
4938 | FORWARD(1); | |
4939 | CPP_RESERVE (pfile, 4); | |
4940 | CPP_PUTC (pfile, c); | |
4941 | CPP_PUTC (pfile, c2); | |
4942 | NEWLINE_FIX; | |
4943 | c3 = PEEKC (); | |
4944 | if (c3 == '=') | |
4945 | CPP_PUTC_Q (pfile, GETC ()); | |
4946 | CPP_NUL_TERMINATE_Q (pfile); | |
4947 | pfile->only_seen_white = 0; | |
4948 | return CPP_OTHER; | |
4949 | ||
4950 | case '@': | |
4951 | if (CPP_BUFFER (pfile)->has_escapes) | |
4952 | { | |
4953 | c = GETC (); | |
4954 | if (c == '-') | |
4955 | { | |
4956 | if (pfile->output_escapes) | |
4957 | CPP_PUTS (pfile, "@-", 2); | |
4958 | parse_name (pfile, GETC ()); | |
4959 | return CPP_NAME; | |
4960 | } | |
782331f4 PB |
4961 | else if (is_space [c]) |
4962 | { | |
4963 | CPP_RESERVE (pfile, 2); | |
4964 | if (pfile->output_escapes) | |
4965 | CPP_PUTC_Q (pfile, '@'); | |
4966 | CPP_PUTC_Q (pfile, c); | |
4967 | return CPP_HSPACE; | |
4968 | } | |
7f2935c7 PB |
4969 | } |
4970 | if (pfile->output_escapes) | |
4971 | { | |
4972 | CPP_PUTS (pfile, "@@", 2); | |
4973 | return CPP_OTHER; | |
4974 | } | |
4975 | goto randomchar; | |
4976 | ||
4977 | case '.': | |
4978 | NEWLINE_FIX; | |
4979 | c2 = PEEKC (); | |
4980 | if (isdigit(c2)) | |
4981 | { | |
4982 | CPP_RESERVE(pfile, 2); | |
4983 | CPP_PUTC_Q (pfile, '.'); | |
4984 | c = GETC (); | |
4985 | goto number; | |
4986 | } | |
4987 | /* FIXME - misses the case "..\\\n." */ | |
4988 | if (c2 == '.' && PEEKN(1) == '.') | |
4989 | { | |
4990 | CPP_RESERVE(pfile, 4); | |
4991 | CPP_PUTC_Q (pfile, '.'); | |
4992 | CPP_PUTC_Q (pfile, '.'); | |
4993 | CPP_PUTC_Q (pfile, '.'); | |
4994 | FORWARD (2); | |
4995 | CPP_NUL_TERMINATE_Q (pfile); | |
4996 | pfile->only_seen_white = 0; | |
4997 | return CPP_3DOTS; | |
4998 | } | |
4999 | goto randomchar; | |
5000 | ||
5001 | op2: | |
5002 | token = CPP_OTHER; | |
5003 | pfile->only_seen_white = 0; | |
5004 | op2any: | |
5005 | CPP_RESERVE(pfile, 3); | |
5006 | CPP_PUTC_Q (pfile, c); | |
5007 | CPP_PUTC_Q (pfile, GETC ()); | |
5008 | CPP_NUL_TERMINATE_Q (pfile); | |
5009 | return token; | |
5010 | ||
5011 | case 'L': | |
5012 | NEWLINE_FIX; | |
5013 | c2 = PEEKC (); | |
5014 | if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile)) | |
5015 | { | |
5016 | CPP_PUTC (pfile, c); | |
5017 | c = GETC (); | |
5018 | goto string; | |
5019 | } | |
5020 | goto letter; | |
5021 | ||
5022 | case '0': case '1': case '2': case '3': case '4': | |
5023 | case '5': case '6': case '7': case '8': case '9': | |
5024 | number: | |
5025 | c2 = '.'; | |
5026 | for (;;) | |
5027 | { | |
5028 | CPP_RESERVE (pfile, 2); | |
5029 | CPP_PUTC_Q (pfile, c); | |
5030 | NEWLINE_FIX; | |
5031 | c = PEEKC (); | |
5032 | if (c == EOF) | |
5033 | break; | |
5034 | if (!is_idchar[c] && c != '.' | |
641d4443 RK |
5035 | && ((c2 != 'e' && c2 != 'E' |
5036 | && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile))) | |
5037 | || (c != '+' && c != '-'))) | |
7f2935c7 PB |
5038 | break; |
5039 | FORWARD(1); | |
5040 | c2= c; | |
5041 | } | |
5042 | CPP_NUL_TERMINATE_Q (pfile); | |
5043 | pfile->only_seen_white = 0; | |
5044 | return CPP_NUMBER; | |
5045 | case 'b': case 'c': case 'd': case 'h': case 'o': | |
5046 | case 'B': case 'C': case 'D': case 'H': case 'O': | |
5047 | if (opts->chill && PEEKC () == '\'') | |
5048 | { | |
5049 | pfile->only_seen_white = 0; | |
5050 | CPP_RESERVE (pfile, 2); | |
5051 | CPP_PUTC_Q (pfile, c); | |
5052 | CPP_PUTC_Q (pfile, '\''); | |
5053 | FORWARD(1); | |
5054 | for (;;) | |
5055 | { | |
5056 | c = GETC(); | |
5057 | if (c == EOF) | |
5058 | goto chill_number_eof; | |
5059 | if (!is_idchar[c]) | |
5060 | { | |
5061 | if (c == '\\' && PEEKC() == '\n') | |
5062 | { | |
5063 | FORWARD(2); | |
5064 | continue; | |
5065 | } | |
5066 | break; | |
5067 | } | |
5068 | CPP_PUTC (pfile, c); | |
5069 | } | |
5070 | if (c == '\'') | |
5071 | { | |
5072 | CPP_RESERVE (pfile, 2); | |
5073 | CPP_PUTC_Q (pfile, c); | |
5074 | CPP_NUL_TERMINATE_Q (pfile); | |
5075 | return CPP_STRING; | |
5076 | } | |
5077 | else | |
5078 | { | |
5079 | FORWARD(-1); | |
5080 | chill_number_eof: | |
5081 | CPP_NUL_TERMINATE (pfile); | |
5082 | return CPP_NUMBER; | |
5083 | } | |
5084 | } | |
5085 | else | |
5086 | goto letter; | |
5087 | case '_': | |
5088 | case 'a': case 'e': case 'f': case 'g': case 'i': case 'j': | |
5089 | case 'k': case 'l': case 'm': case 'n': case 'p': case 'q': | |
5090 | case 'r': case 's': case 't': case 'u': case 'v': case 'w': | |
5091 | case 'x': case 'y': case 'z': | |
5092 | case 'A': case 'E': case 'F': case 'G': case 'I': case 'J': | |
5093 | case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R': | |
5094 | case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': | |
5095 | case 'Y': case 'Z': | |
5096 | letter: | |
5097 | { | |
5098 | HASHNODE *hp; | |
5099 | unsigned char *ident; | |
5100 | int before_name_written = CPP_WRITTEN (pfile); | |
5101 | int ident_len; | |
5102 | parse_name (pfile, c); | |
5103 | pfile->only_seen_white = 0; | |
5104 | if (pfile->no_macro_expand) | |
5105 | return CPP_NAME; | |
5106 | ident = pfile->token_buffer + before_name_written; | |
5107 | ident_len = CPP_PWRITTEN (pfile) - ident; | |
5108 | hp = cpp_lookup (pfile, ident, ident_len, -1); | |
5109 | if (!hp) | |
5110 | return CPP_NAME; | |
5111 | if (hp->type == T_DISABLED) | |
5112 | { | |
5113 | if (pfile->output_escapes) | |
0f41302f | 5114 | { /* Return "@-IDENT", followed by '\0'. */ |
7f2935c7 PB |
5115 | int i; |
5116 | CPP_RESERVE (pfile, 3); | |
5117 | ident = pfile->token_buffer + before_name_written; | |
5118 | CPP_ADJUST_WRITTEN (pfile, 2); | |
5119 | for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i]; | |
5120 | ident[0] = '@'; | |
5121 | ident[1] = '-'; | |
5122 | } | |
5123 | return CPP_NAME; | |
5124 | } | |
5125 | ||
5126 | /* If macro wants an arglist, verify that a '(' follows. | |
5127 | first skip all whitespace, copying it to the output | |
5128 | after the macro name. Then, if there is no '(', | |
5129 | decide this is not a macro call and leave things that way. */ | |
5130 | if (hp->type == T_MACRO && hp->value.defn->nargs >= 0) | |
5131 | { | |
5132 | struct parse_marker macro_mark; | |
5133 | int is_macro_call; | |
5134 | while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile))) | |
5135 | { | |
5136 | cpp_buffer *next_buf; | |
5137 | cpp_skip_hspace (pfile); | |
5138 | if (PEEKC () != EOF) | |
5139 | break; | |
5140 | next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile)); | |
5141 | (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile); | |
5142 | CPP_BUFFER (pfile) = next_buf; | |
5143 | } | |
5144 | parse_set_mark (¯o_mark, pfile); | |
5145 | for (;;) | |
5146 | { | |
5147 | cpp_skip_hspace (pfile); | |
5148 | c = PEEKC (); | |
5149 | is_macro_call = c == '('; | |
5150 | if (c != '\n') | |
5151 | break; | |
5152 | FORWARD (1); | |
5153 | } | |
5154 | if (!is_macro_call) | |
5155 | parse_goto_mark (¯o_mark, pfile); | |
5156 | parse_clear_mark (¯o_mark); | |
5157 | if (!is_macro_call) | |
5158 | return CPP_NAME; | |
5159 | } | |
0f41302f | 5160 | /* This is now known to be a macro call. */ |
7f2935c7 PB |
5161 | |
5162 | /* it might not actually be a macro. */ | |
5163 | if (hp->type != T_MACRO) { | |
5164 | int xbuf_len; U_CHAR *xbuf; | |
5165 | CPP_SET_WRITTEN (pfile, before_name_written); | |
5166 | special_symbol (hp, pfile); | |
5167 | xbuf_len = CPP_WRITTEN (pfile) - before_name_written; | |
5168 | xbuf = (U_CHAR *) xmalloc (xbuf_len + 1); | |
5169 | CPP_SET_WRITTEN (pfile, before_name_written); | |
5170 | bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1); | |
5171 | push_macro_expansion (pfile, xbuf, xbuf_len, hp); | |
5172 | } | |
5173 | else | |
5174 | { | |
5175 | /* Expand the macro, reading arguments as needed, | |
5176 | and push the expansion on the input stack. */ | |
5177 | macroexpand (pfile, hp); | |
5178 | CPP_SET_WRITTEN (pfile, before_name_written); | |
5179 | } | |
5180 | ||
782331f4 | 5181 | /* An extra "@ " is added to the end of a macro expansion |
7f2935c7 PB |
5182 | to prevent accidental token pasting. We prefer to avoid |
5183 | unneeded extra spaces (for the sake of cpp-using tools like | |
0f41302f | 5184 | imake). Here we remove the space if it is safe to do so. */ |
782331f4 PB |
5185 | if (pfile->buffer->rlimit - pfile->buffer->cur >= 3 |
5186 | && pfile->buffer->rlimit[-2] == '@' | |
b13b05f6 | 5187 | && pfile->buffer->rlimit[-1] == ' ') |
7f2935c7 | 5188 | { |
782331f4 | 5189 | int c1 = pfile->buffer->rlimit[-3]; |
7f2935c7 PB |
5190 | int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile))); |
5191 | if (c2 == EOF || ! unsafe_chars (c1, c2)) | |
782331f4 | 5192 | pfile->buffer->rlimit -= 2; |
7f2935c7 | 5193 | } |
7f2935c7 | 5194 | } |
782331f4 | 5195 | goto get_next; |
7f2935c7 PB |
5196 | |
5197 | case ' ': case '\t': case '\v': case '\r': | |
5198 | for (;;) | |
5199 | { | |
5200 | CPP_PUTC (pfile, c); | |
5201 | c = PEEKC (); | |
5202 | if (c == EOF || !is_hor_space[c]) | |
5203 | break; | |
5204 | FORWARD(1); | |
5205 | } | |
5206 | return CPP_HSPACE; | |
5207 | ||
5208 | case '\\': | |
5209 | c2 = PEEKC (); | |
5210 | if (c2 != '\n') | |
5211 | goto randomchar; | |
5212 | token = CPP_HSPACE; | |
5213 | goto op2any; | |
5214 | ||
5215 | case '\n': | |
5216 | CPP_PUTC (pfile, c); | |
5217 | if (pfile->only_seen_white == 0) | |
5218 | pfile->only_seen_white = 1; | |
5219 | pfile->lineno++; | |
5220 | output_line_command (pfile, 1, same_file); | |
5221 | return CPP_VSPACE; | |
5222 | ||
5223 | case '(': token = CPP_LPAREN; goto char1; | |
5224 | case ')': token = CPP_RPAREN; goto char1; | |
5225 | case '{': token = CPP_LBRACE; goto char1; | |
5226 | case '}': token = CPP_RBRACE; goto char1; | |
5227 | case ',': token = CPP_COMMA; goto char1; | |
5228 | case ';': token = CPP_SEMICOLON; goto char1; | |
5229 | ||
5230 | randomchar: | |
5231 | default: | |
5232 | token = CPP_OTHER; | |
5233 | char1: | |
5234 | pfile->only_seen_white = 0; | |
5235 | CPP_PUTC (pfile, c); | |
5236 | return token; | |
5237 | } | |
5238 | } | |
5239 | } | |
5240 | ||
0f41302f MS |
5241 | /* Like cpp_get_token, but skip spaces and comments. */ |
5242 | ||
7f2935c7 PB |
5243 | enum cpp_token |
5244 | cpp_get_non_space_token (pfile) | |
5245 | cpp_reader *pfile; | |
5246 | { | |
5247 | int old_written = CPP_WRITTEN (pfile); | |
5248 | for (;;) | |
5249 | { | |
5250 | enum cpp_token token = cpp_get_token (pfile); | |
5251 | if (token != CPP_COMMENT && token != CPP_POP | |
5252 | && token != CPP_HSPACE && token != CPP_VSPACE) | |
5253 | return token; | |
5254 | CPP_SET_WRITTEN (pfile, old_written); | |
5255 | } | |
5256 | } | |
5257 | ||
0f41302f | 5258 | /* Parse an identifier starting with C. */ |
7f2935c7 PB |
5259 | |
5260 | int | |
5261 | parse_name (pfile, c) | |
5262 | cpp_reader *pfile; int c; | |
5263 | { | |
5264 | for (;;) | |
5265 | { | |
5266 | if (! is_idchar[c]) | |
5267 | { | |
5268 | if (c == '\\' && PEEKC() == '\n') | |
5269 | { | |
5270 | FORWARD(2); | |
5271 | continue; | |
5272 | } | |
5273 | FORWARD (-1); | |
5274 | break; | |
5275 | } | |
5276 | ||
9e979f8f RK |
5277 | if (c == '$' && CPP_PEDANTIC (pfile)) |
5278 | cpp_pedwarn ("`$' in identifier"); | |
5279 | ||
0f41302f | 5280 | CPP_RESERVE(pfile, 2); /* One more for final NUL. */ |
7f2935c7 PB |
5281 | CPP_PUTC_Q (pfile, c); |
5282 | c = GETC(); | |
5283 | if (c == EOF) | |
5284 | break; | |
5285 | } | |
5286 | CPP_NUL_TERMINATE_Q (pfile); | |
5287 | return 1; | |
5288 | } | |
5289 | ||
5290 | \f | |
5291 | /* Maintain and search list of included files, for #import. */ | |
5292 | ||
5293 | /* Hash a file name for import_hash_table. */ | |
5294 | ||
5295 | static int | |
5296 | import_hash (f) | |
5297 | char *f; | |
5298 | { | |
5299 | int val = 0; | |
5300 | ||
5301 | while (*f) val += *f++; | |
5302 | return (val%IMPORT_HASH_SIZE); | |
5303 | } | |
5304 | ||
5305 | /* Search for file FILENAME in import_hash_table. | |
5306 | Return -2 if found, either a matching name or a matching inode. | |
5307 | Otherwise, open the file and return a file descriptor if successful | |
5308 | or -1 if unsuccessful. */ | |
5309 | ||
5310 | static int | |
5311 | lookup_import (pfile, filename, searchptr) | |
782331f4 | 5312 | cpp_reader *pfile; |
7f2935c7 PB |
5313 | char *filename; |
5314 | struct file_name_list *searchptr; | |
5315 | { | |
5316 | struct import_file *i; | |
5317 | int h; | |
5318 | int hashval; | |
5319 | struct stat sb; | |
5320 | int fd; | |
5321 | ||
5322 | hashval = import_hash (filename); | |
5323 | ||
5324 | /* Attempt to find file in list of already included files */ | |
5325 | i = pfile->import_hash_table[hashval]; | |
5326 | ||
5327 | while (i) { | |
5328 | if (!strcmp (filename, i->name)) | |
5329 | return -2; /* return found */ | |
5330 | i = i->next; | |
5331 | } | |
5332 | /* Open it and try a match on inode/dev */ | |
782331f4 | 5333 | fd = open_include_file (pfile, filename, searchptr); |
7f2935c7 PB |
5334 | if (fd < 0) |
5335 | return fd; | |
5336 | fstat (fd, &sb); | |
5337 | for (h = 0; h < IMPORT_HASH_SIZE; h++) { | |
5338 | i = pfile->import_hash_table[h]; | |
5339 | while (i) { | |
5340 | /* Compare the inode and the device. | |
5341 | Supposedly on some systems the inode is not a scalar. */ | |
5f972d0c | 5342 | if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino)) |
7f2935c7 PB |
5343 | && i->dev == sb.st_dev) { |
5344 | close (fd); | |
5345 | return -2; /* return found */ | |
5346 | } | |
5347 | i = i->next; | |
5348 | } | |
5349 | } | |
5350 | return fd; /* Not found, return open file */ | |
5351 | } | |
5352 | ||
5353 | /* Add the file FNAME, open on descriptor FD, to import_hash_table. */ | |
5354 | ||
5355 | static void | |
5356 | add_import (pfile, fd, fname) | |
5357 | cpp_reader *pfile; | |
5358 | int fd; | |
5359 | char *fname; | |
5360 | { | |
5361 | struct import_file *i; | |
5362 | int hashval; | |
5363 | struct stat sb; | |
5364 | ||
5365 | hashval = import_hash (fname); | |
5366 | fstat (fd, &sb); | |
5367 | i = (struct import_file *)xmalloc (sizeof (struct import_file)); | |
5368 | i->name = (char *)xmalloc (strlen (fname)+1); | |
5369 | strcpy (i->name, fname); | |
5f972d0c | 5370 | bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino)); |
7f2935c7 PB |
5371 | i->dev = sb.st_dev; |
5372 | i->next = pfile->import_hash_table[hashval]; | |
5373 | pfile->import_hash_table[hashval] = i; | |
5374 | } | |
5375 | \f | |
5376 | /* The file_name_map structure holds a mapping of file names for a | |
5377 | particular directory. This mapping is read from the file named | |
5378 | FILE_NAME_MAP_FILE in that directory. Such a file can be used to | |
5379 | map filenames on a file system with severe filename restrictions, | |
5380 | such as DOS. The format of the file name map file is just a series | |
5381 | of lines with two tokens on each line. The first token is the name | |
5382 | to map, and the second token is the actual name to use. */ | |
5383 | ||
5384 | struct file_name_map | |
5385 | { | |
5386 | struct file_name_map *map_next; | |
5387 | char *map_from; | |
5388 | char *map_to; | |
5389 | }; | |
5390 | ||
5391 | #define FILE_NAME_MAP_FILE "header.gcc" | |
5392 | ||
5393 | /* Read a space delimited string of unlimited length from a stdio | |
5394 | file. */ | |
5395 | ||
5396 | static char * | |
5397 | read_filename_string (ch, f) | |
5398 | int ch; | |
5399 | FILE *f; | |
5400 | { | |
5401 | char *alloc, *set; | |
5402 | int len; | |
5403 | ||
5404 | len = 20; | |
5405 | set = alloc = xmalloc (len + 1); | |
5406 | if (! is_space[ch]) | |
5407 | { | |
5408 | *set++ = ch; | |
5409 | while ((ch = getc (f)) != EOF && ! is_space[ch]) | |
5410 | { | |
5411 | if (set - alloc == len) | |
5412 | { | |
5413 | len *= 2; | |
5414 | alloc = xrealloc (alloc, len + 1); | |
5415 | set = alloc + len / 2; | |
5416 | } | |
5417 | *set++ = ch; | |
5418 | } | |
5419 | } | |
5420 | *set = '\0'; | |
5421 | ungetc (ch, f); | |
5422 | return alloc; | |
5423 | } | |
5424 | ||
0f41302f MS |
5425 | /* This structure holds a linked list of file name maps, one per directory. */ |
5426 | ||
782331f4 PB |
5427 | struct file_name_map_list |
5428 | { | |
5429 | struct file_name_map_list *map_list_next; | |
5430 | char *map_list_name; | |
5431 | struct file_name_map *map_list_map; | |
5432 | }; | |
5433 | ||
7f2935c7 PB |
5434 | /* Read the file name map file for DIRNAME. */ |
5435 | ||
5436 | static struct file_name_map * | |
782331f4 PB |
5437 | read_name_map (pfile, dirname) |
5438 | cpp_reader *pfile; | |
7f2935c7 PB |
5439 | char *dirname; |
5440 | { | |
7f2935c7 PB |
5441 | register struct file_name_map_list *map_list_ptr; |
5442 | char *name; | |
5443 | FILE *f; | |
5444 | ||
782331f4 | 5445 | for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr; |
7f2935c7 PB |
5446 | map_list_ptr = map_list_ptr->map_list_next) |
5447 | if (! strcmp (map_list_ptr->map_list_name, dirname)) | |
5448 | return map_list_ptr->map_list_map; | |
5449 | ||
5450 | map_list_ptr = ((struct file_name_map_list *) | |
5451 | xmalloc (sizeof (struct file_name_map_list))); | |
5452 | map_list_ptr->map_list_name = savestring (dirname); | |
5453 | map_list_ptr->map_list_map = NULL; | |
5454 | ||
5455 | name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2); | |
5456 | strcpy (name, dirname); | |
5457 | if (*dirname) | |
5458 | strcat (name, "/"); | |
5459 | strcat (name, FILE_NAME_MAP_FILE); | |
5460 | f = fopen (name, "r"); | |
5461 | if (!f) | |
5462 | map_list_ptr->map_list_map = NULL; | |
5463 | else | |
5464 | { | |
5465 | int ch; | |
5466 | int dirlen = strlen (dirname); | |
5467 | ||
5468 | while ((ch = getc (f)) != EOF) | |
5469 | { | |
5470 | char *from, *to; | |
5471 | struct file_name_map *ptr; | |
5472 | ||
5473 | if (is_space[ch]) | |
5474 | continue; | |
5475 | from = read_filename_string (ch, f); | |
5476 | while ((ch = getc (f)) != EOF && is_hor_space[ch]) | |
5477 | ; | |
5478 | to = read_filename_string (ch, f); | |
5479 | ||
5480 | ptr = ((struct file_name_map *) | |
5481 | xmalloc (sizeof (struct file_name_map))); | |
5482 | ptr->map_from = from; | |
5483 | ||
5484 | /* Make the real filename absolute. */ | |
5485 | if (*to == '/') | |
5486 | ptr->map_to = to; | |
5487 | else | |
5488 | { | |
5489 | ptr->map_to = xmalloc (dirlen + strlen (to) + 2); | |
5490 | strcpy (ptr->map_to, dirname); | |
5491 | ptr->map_to[dirlen] = '/'; | |
5492 | strcpy (ptr->map_to + dirlen + 1, to); | |
5493 | free (to); | |
5494 | } | |
5495 | ||
5496 | ptr->map_next = map_list_ptr->map_list_map; | |
5497 | map_list_ptr->map_list_map = ptr; | |
5498 | ||
5499 | while ((ch = getc (f)) != '\n') | |
5500 | if (ch == EOF) | |
5501 | break; | |
5502 | } | |
5503 | fclose (f); | |
5504 | } | |
5505 | ||
782331f4 PB |
5506 | map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list; |
5507 | CPP_OPTIONS (pfile)->map_list = map_list_ptr; | |
7f2935c7 PB |
5508 | |
5509 | return map_list_ptr->map_list_map; | |
5510 | } | |
5511 | ||
5512 | /* Try to open include file FILENAME. SEARCHPTR is the directory | |
5513 | being tried from the include file search path. This function maps | |
5514 | filenames on file systems based on information read by | |
5515 | read_name_map. */ | |
5516 | ||
5517 | static int | |
782331f4 PB |
5518 | open_include_file (pfile, filename, searchptr) |
5519 | cpp_reader *pfile; | |
7f2935c7 PB |
5520 | char *filename; |
5521 | struct file_name_list *searchptr; | |
5522 | { | |
5523 | register struct file_name_map *map; | |
5524 | register char *from; | |
5525 | char *p, *dir; | |
5526 | ||
5527 | if (searchptr && ! searchptr->got_name_map) | |
5528 | { | |
782331f4 PB |
5529 | searchptr->name_map = read_name_map (pfile, |
5530 | searchptr->fname | |
7f2935c7 PB |
5531 | ? searchptr->fname : "."); |
5532 | searchptr->got_name_map = 1; | |
5533 | } | |
5534 | ||
5535 | /* First check the mapping for the directory we are using. */ | |
5536 | if (searchptr && searchptr->name_map) | |
5537 | { | |
5538 | from = filename; | |
5539 | if (searchptr->fname) | |
5540 | from += strlen (searchptr->fname) + 1; | |
5541 | for (map = searchptr->name_map; map; map = map->map_next) | |
5542 | { | |
5543 | if (! strcmp (map->map_from, from)) | |
5544 | { | |
5545 | /* Found a match. */ | |
5546 | return open (map->map_to, O_RDONLY, 0666); | |
5547 | } | |
5548 | } | |
5549 | } | |
5550 | ||
5551 | /* Try to find a mapping file for the particular directory we are | |
5552 | looking in. Thus #include <sys/types.h> will look up sys/types.h | |
5553 | in /usr/include/header.gcc and look up types.h in | |
5554 | /usr/include/sys/header.gcc. */ | |
5555 | p = rindex (filename, '/'); | |
5556 | if (! p) | |
5557 | p = filename; | |
5558 | if (searchptr | |
5559 | && searchptr->fname | |
5560 | && strlen (searchptr->fname) == p - filename | |
5561 | && ! strncmp (searchptr->fname, filename, p - filename)) | |
5562 | { | |
5563 | /* FILENAME is in SEARCHPTR, which we've already checked. */ | |
5564 | return open (filename, O_RDONLY, 0666); | |
5565 | } | |
5566 | ||
5567 | if (p == filename) | |
5568 | { | |
5569 | dir = "."; | |
5570 | from = filename; | |
5571 | } | |
5572 | else | |
5573 | { | |
5574 | dir = (char *) alloca (p - filename + 1); | |
5575 | bcopy (filename, dir, p - filename); | |
5576 | dir[p - filename] = '\0'; | |
5577 | from = p + 1; | |
5578 | } | |
782331f4 | 5579 | for (map = read_name_map (pfile, dir); map; map = map->map_next) |
7f2935c7 PB |
5580 | if (! strcmp (map->map_from, from)) |
5581 | return open (map->map_to, O_RDONLY, 0666); | |
5582 | ||
5583 | return open (filename, O_RDONLY, 0666); | |
5584 | } | |
5585 | ||
5586 | /* Process the contents of include file FNAME, already open on descriptor F, | |
5587 | with output to OP. | |
5588 | SYSTEM_HEADER_P is 1 if this file resides in any one of the known | |
5589 | "system" include directories (as decided by the `is_system_include' | |
5590 | function above). | |
5591 | DIRPTR is the link in the dir path through which this file was found, | |
6bac1e64 | 5592 | or 0 if the file name was absolute or via the current directory. |
22bbceaf PB |
5593 | Return 1 on success, 0 on failure. |
5594 | ||
5595 | The caller is responsible for the cpp_push_buffer. */ | |
7f2935c7 PB |
5596 | |
5597 | static int | |
5598 | finclude (pfile, f, fname, system_header_p, dirptr) | |
5599 | cpp_reader *pfile; | |
5600 | int f; | |
5601 | char *fname; | |
5602 | int system_header_p; | |
5603 | struct file_name_list *dirptr; | |
5604 | { | |
5605 | int st_mode; | |
5606 | long st_size; | |
5607 | long i; | |
5608 | int length; | |
5609 | cpp_buffer *fp; /* For input stack frame */ | |
5610 | int missing_newline = 0; | |
5611 | ||
7f2935c7 PB |
5612 | if (file_size_and_mode (f, &st_mode, &st_size) < 0) |
5613 | { | |
5614 | cpp_perror_with_name (pfile, fname); | |
5615 | close (f); | |
22bbceaf | 5616 | cpp_pop_buffer (pfile); |
7f2935c7 PB |
5617 | return 0; |
5618 | } | |
5619 | ||
22bbceaf | 5620 | fp = CPP_BUFFER (pfile); |
7f2935c7 PB |
5621 | fp->nominal_fname = fp->fname = fname; |
5622 | #if 0 | |
5623 | fp->length = 0; | |
5624 | #endif | |
5625 | fp->dir = dirptr; | |
5626 | fp->system_header_p = system_header_p; | |
5627 | fp->lineno = 1; | |
5628 | fp->colno = 1; | |
5629 | fp->cleanup = file_cleanup; | |
5630 | ||
5631 | if (S_ISREG (st_mode)) { | |
5632 | fp->buf = (U_CHAR *) xmalloc (st_size + 2); | |
5633 | fp->alimit = fp->buf + st_size + 2; | |
5634 | fp->cur = fp->buf; | |
5635 | ||
5636 | /* Read the file contents, knowing that st_size is an upper bound | |
5637 | on the number of bytes we can read. */ | |
5638 | length = safe_read (f, fp->buf, st_size); | |
5639 | fp->rlimit = fp->buf + length; | |
5640 | if (length < 0) goto nope; | |
5641 | } | |
5642 | else if (S_ISDIR (st_mode)) { | |
5643 | cpp_error (pfile, "directory `%s' specified in #include", fname); | |
5644 | close (f); | |
5645 | return 0; | |
5646 | } else { | |
5647 | /* Cannot count its file size before reading. | |
5648 | First read the entire file into heap and | |
0f41302f | 5649 | copy them into buffer on stack. */ |
7f2935c7 PB |
5650 | |
5651 | int bsize = 2000; | |
5652 | ||
5653 | st_size = 0; | |
5654 | fp->buf = (U_CHAR *) xmalloc (bsize + 2); | |
5655 | ||
5656 | for (;;) { | |
5657 | i = safe_read (f, fp->buf + st_size, bsize - st_size); | |
5658 | if (i < 0) | |
5659 | goto nope; /* error! */ | |
5660 | st_size += i; | |
5661 | if (st_size != bsize) | |
5662 | break; /* End of file */ | |
5663 | bsize *= 2; | |
5664 | fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2); | |
5665 | } | |
a5827481 | 5666 | fp->cur = fp->buf; |
7f2935c7 PB |
5667 | length = st_size; |
5668 | } | |
5669 | ||
5670 | if ((length > 0 && fp->buf[length - 1] != '\n') | |
5671 | /* Backslash-newline at end is not good enough. */ | |
5672 | || (length > 1 && fp->buf[length - 2] == '\\')) { | |
5673 | fp->buf[length++] = '\n'; | |
5674 | #if 0 | |
5675 | missing_newline = 1; | |
5676 | #endif | |
5677 | } | |
5678 | fp->buf[length] = '\0'; | |
5679 | fp->rlimit = fp->buf + length; | |
5680 | ||
5681 | /* Close descriptor now, so nesting does not use lots of descriptors. */ | |
5682 | close (f); | |
5683 | ||
5684 | /* Must do this before calling trigraph_pcp, so that the correct file name | |
5685 | will be printed in warning messages. */ | |
5686 | ||
5687 | pfile->input_stack_listing_current = 0; | |
5688 | ||
5689 | #if 0 | |
5690 | if (!no_trigraphs) | |
5691 | trigraph_pcp (fp); | |
5692 | #endif | |
5693 | ||
5694 | #if 0 | |
5695 | rescan (op, 0); | |
5696 | ||
5697 | if (missing_newline) | |
5698 | fp->lineno--; | |
5699 | ||
5700 | if (CPP_PEDANTIC (pfile) && missing_newline) | |
5701 | pedwarn ("file does not end in newline"); | |
5702 | ||
5703 | indepth--; | |
5704 | input_file_stack_tick++; | |
5705 | free (fp->buf); | |
5706 | #endif | |
5707 | return 1; | |
5708 | ||
5709 | nope: | |
5710 | ||
5711 | cpp_perror_with_name (pfile, fname); | |
5712 | close (f); | |
5713 | free (fp->buf); | |
5714 | return 1; | |
5715 | } | |
5716 | ||
a94c94be PB |
5717 | /* This is called after options have been processed. |
5718 | * Check options for consistency, and setup for processing input | |
5719 | * from the file named FNAME. (Use standard input if FNAME==NULL.) | |
5720 | * Return 1 on succes, 0 on failure. | |
5721 | */ | |
5722 | ||
7f2935c7 | 5723 | int |
a94c94be | 5724 | cpp_start_read (pfile, fname) |
7f2935c7 PB |
5725 | cpp_reader *pfile; |
5726 | char *fname; | |
5727 | { | |
5728 | struct cpp_options *opts = CPP_OPTIONS (pfile); | |
5729 | struct cpp_pending *pend; | |
5730 | char *p; | |
5731 | int f; | |
22bbceaf | 5732 | cpp_buffer *fp; |
7f2935c7 PB |
5733 | |
5734 | /* The code looks at the defaults through this pointer, rather than through | |
5735 | the constant structure above. This pointer gets changed if an environment | |
5736 | variable specifies other defaults. */ | |
5737 | struct default_include *include_defaults = include_defaults_array; | |
5738 | ||
5739 | /* Add dirs from CPATH after dirs from -I. */ | |
5740 | /* There seems to be confusion about what CPATH should do, | |
5741 | so for the moment it is not documented. */ | |
5742 | /* Some people say that CPATH should replace the standard include dirs, | |
5743 | but that seems pointless: it comes before them, so it overrides them | |
5744 | anyway. */ | |
5745 | p = (char *) getenv ("CPATH"); | |
5746 | if (p != 0 && ! opts->no_standard_includes) | |
5747 | path_include (pfile, p); | |
5748 | ||
5749 | /* Now that dollars_in_ident is known, initialize is_idchar. */ | |
5750 | initialize_char_syntax (opts); | |
5751 | ||
7f2935c7 PB |
5752 | /* Do partial setup of input buffer for the sake of generating |
5753 | early #line directives (when -g is in effect). */ | |
22bbceaf | 5754 | fp = cpp_push_buffer (pfile, NULL, 0); |
e2f79f3c PB |
5755 | if (!fp) |
5756 | return 0; | |
22bbceaf PB |
5757 | if (opts->in_fname == NULL) |
5758 | opts->in_fname = ""; | |
5759 | fp->nominal_fname = fp->fname = opts->in_fname; | |
7f2935c7 | 5760 | fp->lineno = 0; |
7f2935c7 PB |
5761 | |
5762 | /* Install __LINE__, etc. Must follow initialize_char_syntax | |
5763 | and option processing. */ | |
5764 | initialize_builtins (pfile); | |
5765 | ||
5766 | /* Do standard #defines and assertions | |
5767 | that identify system and machine type. */ | |
5768 | ||
5769 | if (!opts->inhibit_predefs) { | |
5770 | char *p = (char *) alloca (strlen (predefs) + 1); | |
5771 | strcpy (p, predefs); | |
5772 | while (*p) { | |
5773 | char *q; | |
5774 | while (*p == ' ' || *p == '\t') | |
5775 | p++; | |
5776 | /* Handle -D options. */ | |
5777 | if (p[0] == '-' && p[1] == 'D') { | |
5778 | q = &p[2]; | |
5779 | while (*p && *p != ' ' && *p != '\t') | |
5780 | p++; | |
5781 | if (*p != 0) | |
5782 | *p++= 0; | |
5783 | if (opts->debug_output) | |
5784 | output_line_command (pfile, 0, same_file); | |
b13b05f6 | 5785 | cpp_define (pfile, q); |
7f2935c7 PB |
5786 | while (*p == ' ' || *p == '\t') |
5787 | p++; | |
5788 | } else if (p[0] == '-' && p[1] == 'A') { | |
5789 | /* Handle -A options (assertions). */ | |
5790 | char *assertion; | |
5791 | char *past_name; | |
5792 | char *value; | |
5793 | char *past_value; | |
5794 | char *termination; | |
5795 | int save_char; | |
5796 | ||
5797 | assertion = &p[2]; | |
5798 | past_name = assertion; | |
5799 | /* Locate end of name. */ | |
5800 | while (*past_name && *past_name != ' ' | |
5801 | && *past_name != '\t' && *past_name != '(') | |
5802 | past_name++; | |
5803 | /* Locate `(' at start of value. */ | |
5804 | value = past_name; | |
5805 | while (*value && (*value == ' ' || *value == '\t')) | |
5806 | value++; | |
5807 | if (*value++ != '(') | |
5808 | abort (); | |
5809 | while (*value && (*value == ' ' || *value == '\t')) | |
5810 | value++; | |
5811 | past_value = value; | |
5812 | /* Locate end of value. */ | |
5813 | while (*past_value && *past_value != ' ' | |
5814 | && *past_value != '\t' && *past_value != ')') | |
5815 | past_value++; | |
5816 | termination = past_value; | |
5817 | while (*termination && (*termination == ' ' || *termination == '\t')) | |
5818 | termination++; | |
5819 | if (*termination++ != ')') | |
5820 | abort (); | |
5821 | if (*termination && *termination != ' ' && *termination != '\t') | |
5822 | abort (); | |
5823 | /* Temporarily null-terminate the value. */ | |
5824 | save_char = *termination; | |
5825 | *termination = '\0'; | |
5826 | /* Install the assertion. */ | |
5827 | make_assertion (pfile, "-A", assertion); | |
5828 | *termination = (char) save_char; | |
5829 | p = termination; | |
5830 | while (*p == ' ' || *p == '\t') | |
5831 | p++; | |
5832 | } else { | |
5833 | abort (); | |
5834 | } | |
5835 | } | |
5836 | } | |
5837 | ||
5838 | /* Now handle the command line options. */ | |
5839 | ||
5840 | /* Do -U's, -D's and -A's in the order they were seen. */ | |
0f41302f | 5841 | /* First reverse the list. */ |
22bbceaf | 5842 | opts->pending = nreverse_pending (opts->pending); |
7f2935c7 | 5843 | |
22bbceaf PB |
5844 | for (pend = opts->pending; pend; pend = pend->next) |
5845 | { | |
5846 | if (pend->cmd != NULL && pend->cmd[0] == '-') | |
5847 | { | |
5848 | switch (pend->cmd[1]) | |
5849 | { | |
5850 | case 'U': | |
5851 | if (opts->debug_output) | |
5852 | output_line_command (pfile, 0, same_file); | |
5853 | do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg)); | |
5854 | break; | |
5855 | case 'D': | |
5856 | if (opts->debug_output) | |
5857 | output_line_command (pfile, 0, same_file); | |
b13b05f6 | 5858 | cpp_define (pfile, pend->arg); |
22bbceaf PB |
5859 | break; |
5860 | case 'A': | |
5861 | make_assertion (pfile, "-A", pend->arg); | |
5862 | break; | |
5863 | } | |
5864 | } | |
5865 | } | |
7f2935c7 PB |
5866 | |
5867 | opts->done_initializing = 1; | |
5868 | ||
0f41302f MS |
5869 | { /* Read the appropriate environment variable and if it exists |
5870 | replace include_defaults with the listed path. */ | |
7f2935c7 PB |
5871 | char *epath = 0; |
5872 | switch ((opts->objc << 1) + opts->cplusplus) | |
5873 | { | |
5874 | case 0: | |
5875 | epath = getenv ("C_INCLUDE_PATH"); | |
5876 | break; | |
5877 | case 1: | |
5878 | epath = getenv ("CPLUS_INCLUDE_PATH"); | |
5879 | break; | |
5880 | case 2: | |
5881 | epath = getenv ("OBJC_INCLUDE_PATH"); | |
5882 | break; | |
5883 | case 3: | |
5884 | epath = getenv ("OBJCPLUS_INCLUDE_PATH"); | |
5885 | break; | |
5886 | } | |
5887 | /* If the environment var for this language is set, | |
5888 | add to the default list of include directories. */ | |
5889 | if (epath) { | |
5890 | char *nstore = (char *) alloca (strlen (epath) + 2); | |
5891 | int num_dirs; | |
5892 | char *startp, *endp; | |
5893 | ||
5894 | for (num_dirs = 1, startp = epath; *startp; startp++) | |
5895 | if (*startp == PATH_SEPARATOR) | |
5896 | num_dirs++; | |
5897 | include_defaults | |
5898 | = (struct default_include *) xmalloc ((num_dirs | |
5899 | * sizeof (struct default_include)) | |
5900 | + sizeof (include_defaults_array)); | |
5901 | startp = endp = epath; | |
5902 | num_dirs = 0; | |
5903 | while (1) { | |
5904 | /* Handle cases like c:/usr/lib:d:/gcc/lib */ | |
5905 | if ((*endp == PATH_SEPARATOR) | |
5906 | || *endp == 0) { | |
5907 | strncpy (nstore, startp, endp-startp); | |
5908 | if (endp == startp) | |
5909 | strcpy (nstore, "."); | |
5910 | else | |
5911 | nstore[endp-startp] = '\0'; | |
5912 | ||
5913 | include_defaults[num_dirs].fname = savestring (nstore); | |
e9a25f70 | 5914 | include_defaults[num_dirs].component = 0; |
7f2935c7 PB |
5915 | include_defaults[num_dirs].cplusplus = opts->cplusplus; |
5916 | include_defaults[num_dirs].cxx_aware = 1; | |
5917 | num_dirs++; | |
5918 | if (*endp == '\0') | |
5919 | break; | |
5920 | endp = startp = endp + 1; | |
5921 | } else | |
5922 | endp++; | |
5923 | } | |
5924 | /* Put the usual defaults back in at the end. */ | |
5f972d0c RK |
5925 | bcopy ((char *) include_defaults_array, |
5926 | (char *) &include_defaults[num_dirs], | |
7f2935c7 PB |
5927 | sizeof (include_defaults_array)); |
5928 | } | |
5929 | } | |
5930 | ||
5931 | append_include_chain (pfile, opts->before_system, opts->last_before_system); | |
5932 | opts->first_system_include = opts->before_system; | |
5933 | ||
5934 | /* Unless -fnostdinc, | |
5935 | tack on the standard include file dirs to the specified list */ | |
5936 | if (!opts->no_standard_includes) { | |
5937 | struct default_include *p = include_defaults; | |
5938 | char *specd_prefix = opts->include_prefix; | |
5939 | char *default_prefix = savestring (GCC_INCLUDE_DIR); | |
5940 | int default_len = 0; | |
5941 | /* Remove the `include' from /usr/local/lib/gcc.../include. */ | |
5942 | if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) { | |
5943 | default_len = strlen (default_prefix) - 7; | |
5944 | default_prefix[default_len] = 0; | |
5945 | } | |
5946 | /* Search "translated" versions of GNU directories. | |
5947 | These have /usr/local/lib/gcc... replaced by specd_prefix. */ | |
5948 | if (specd_prefix != 0 && default_len != 0) | |
5949 | for (p = include_defaults; p->fname; p++) { | |
5950 | /* Some standard dirs are only for C++. */ | |
5951 | if (!p->cplusplus | |
5952 | || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) { | |
5953 | /* Does this dir start with the prefix? */ | |
5954 | if (!strncmp (p->fname, default_prefix, default_len)) { | |
5955 | /* Yes; change prefix and add to search list. */ | |
5956 | struct file_name_list *new | |
5957 | = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); | |
5958 | int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len; | |
5959 | char *str = (char *) xmalloc (this_len + 1); | |
5960 | strcpy (str, specd_prefix); | |
5961 | strcat (str, p->fname + default_len); | |
5962 | new->fname = str; | |
5963 | new->control_macro = 0; | |
5964 | new->c_system_include_path = !p->cxx_aware; | |
5965 | new->got_name_map = 0; | |
5966 | append_include_chain (pfile, new, new); | |
5967 | if (opts->first_system_include == 0) | |
5968 | opts->first_system_include = new; | |
5969 | } | |
5970 | } | |
5971 | } | |
5972 | /* Search ordinary names for GNU include directories. */ | |
5973 | for (p = include_defaults; p->fname; p++) { | |
5974 | /* Some standard dirs are only for C++. */ | |
5975 | if (!p->cplusplus | |
5976 | || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) { | |
5977 | struct file_name_list *new | |
5978 | = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); | |
5979 | new->control_macro = 0; | |
5980 | new->c_system_include_path = !p->cxx_aware; | |
e9a25f70 | 5981 | new->fname = update_path (p->fname, p->component); |
7f2935c7 PB |
5982 | new->got_name_map = 0; |
5983 | append_include_chain (pfile, new, new); | |
5984 | if (opts->first_system_include == 0) | |
5985 | opts->first_system_include = new; | |
5986 | } | |
5987 | } | |
5988 | } | |
5989 | ||
5990 | /* Tack the after_include chain at the end of the include chain. */ | |
5991 | append_include_chain (pfile, opts->after_include, opts->last_after_include); | |
5992 | if (opts->first_system_include == 0) | |
5993 | opts->first_system_include = opts->after_include; | |
5994 | ||
5995 | /* With -v, print the list of dirs to search. */ | |
5996 | if (opts->verbose) { | |
5997 | struct file_name_list *p; | |
5998 | fprintf (stderr, "#include \"...\" search starts here:\n"); | |
5999 | for (p = opts->include; p; p = p->next) { | |
6000 | if (p == opts->first_bracket_include) | |
6001 | fprintf (stderr, "#include <...> search starts here:\n"); | |
6002 | fprintf (stderr, " %s\n", p->fname); | |
6003 | } | |
6004 | fprintf (stderr, "End of search list.\n"); | |
6005 | } | |
6006 | ||
6007 | /* Scan the -imacros files before the main input. | |
6008 | Much like #including them, but with no_output set | |
6009 | so that only their macro definitions matter. */ | |
6010 | ||
6011 | opts->no_output++; pfile->no_record_file++; | |
6012 | for (pend = opts->pending; pend; pend = pend->next) | |
6013 | { | |
6014 | if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0) | |
6015 | { | |
6016 | int fd = open (pend->arg, O_RDONLY, 0666); | |
6017 | if (fd < 0) | |
6018 | { | |
6019 | cpp_perror_with_name (pfile, pend->arg); | |
a94c94be | 6020 | return 0; |
7f2935c7 | 6021 | } |
e2f79f3c PB |
6022 | if (!cpp_push_buffer (pfile, NULL, 0)) |
6023 | return 0; | |
7f2935c7 PB |
6024 | finclude (pfile, fd, pend->arg, 0, NULL_PTR); |
6025 | cpp_scan_buffer (pfile); | |
6026 | } | |
6027 | } | |
6028 | opts->no_output--; pfile->no_record_file--; | |
6029 | ||
6030 | /* Copy the entire contents of the main input file into | |
6031 | the stacked input buffer previously allocated for it. */ | |
6032 | if (fname == NULL || *fname == 0) { | |
6033 | fname = ""; | |
6034 | f = 0; | |
6035 | } else if ((f = open (fname, O_RDONLY, 0666)) < 0) | |
6036 | cpp_pfatal_with_name (pfile, fname); | |
6037 | ||
6038 | /* -MG doesn't select the form of output and must be specified with one of | |
6039 | -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't | |
6040 | inhibit compilation. */ | |
6041 | if (opts->print_deps_missing_files | |
6042 | && (opts->print_deps == 0 || !opts->no_output)) | |
a94c94be PB |
6043 | { |
6044 | cpp_fatal (pfile, "-MG must be specified with one of -M or -MM"); | |
6045 | return 0; | |
6046 | } | |
7f2935c7 PB |
6047 | |
6048 | /* Either of two environment variables can specify output of deps. | |
6049 | Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET", | |
6050 | where OUTPUT_FILE is the file to write deps info to | |
6051 | and DEPS_TARGET is the target to mention in the deps. */ | |
6052 | ||
6053 | if (opts->print_deps == 0 | |
6054 | && (getenv ("SUNPRO_DEPENDENCIES") != 0 | |
6055 | || getenv ("DEPENDENCIES_OUTPUT") != 0)) { | |
6056 | char *spec = getenv ("DEPENDENCIES_OUTPUT"); | |
6057 | char *s; | |
6058 | char *output_file; | |
6059 | ||
6060 | if (spec == 0) | |
6061 | { | |
6062 | spec = getenv ("SUNPRO_DEPENDENCIES"); | |
6063 | opts->print_deps = 2; | |
6064 | } | |
6065 | else | |
6066 | opts->print_deps = 1; | |
6067 | ||
6068 | s = spec; | |
6069 | /* Find the space before the DEPS_TARGET, if there is one. */ | |
6070 | /* This should use index. (mrs) */ | |
6071 | while (*s != 0 && *s != ' ') s++; | |
6072 | if (*s != 0) | |
6073 | { | |
6074 | opts->deps_target = s + 1; | |
6075 | output_file = (char *) xmalloc (s - spec + 1); | |
6076 | bcopy (spec, output_file, s - spec); | |
6077 | output_file[s - spec] = 0; | |
6078 | } | |
6079 | else | |
6080 | { | |
6081 | opts->deps_target = 0; | |
6082 | output_file = spec; | |
6083 | } | |
6084 | ||
6085 | opts->deps_file = output_file; | |
6086 | opts->print_deps_append = 1; | |
6087 | } | |
6088 | ||
6089 | /* For -M, print the expected object file name | |
6090 | as the target of this Make-rule. */ | |
6091 | if (opts->print_deps) | |
6092 | { | |
6093 | pfile->deps_allocated_size = 200; | |
6094 | pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size); | |
6095 | pfile->deps_buffer[0] = 0; | |
6096 | pfile->deps_size = 0; | |
6097 | pfile->deps_column = 0; | |
6098 | ||
6099 | if (opts->deps_target) | |
6100 | deps_output (pfile, opts->deps_target, ':'); | |
6101 | else if (*opts->in_fname == 0) | |
6102 | deps_output (pfile, "-", ':'); | |
6103 | else | |
6104 | { | |
e9a25f70 JL |
6105 | char *p, *q, *r; |
6106 | int len, x; | |
6107 | static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m", | |
6108 | ".cc", ".cxx", ".cpp", ".cp", | |
6109 | ".c++", 0 | |
6110 | }; | |
7f2935c7 PB |
6111 | |
6112 | /* Discard all directory prefixes from filename. */ | |
6113 | if ((q = rindex (opts->in_fname, '/')) != NULL | |
6114 | #ifdef DIR_SEPARATOR | |
6115 | && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL | |
6116 | #endif | |
6117 | ) | |
6118 | ++q; | |
6119 | else | |
6120 | q = opts->in_fname; | |
6121 | ||
6122 | /* Copy remainder to mungable area. */ | |
6123 | p = (char *) alloca (strlen(q) + 8); | |
6124 | strcpy (p, q); | |
6125 | ||
6126 | /* Output P, but remove known suffixes. */ | |
6127 | len = strlen (p); | |
6128 | q = p + len; | |
e9a25f70 JL |
6129 | /* Point to the filename suffix. */ |
6130 | r = rindex (p, '.'); | |
6131 | /* Compare against the known suffixes. */ | |
6132 | x = 0; | |
6133 | while (known_suffixes[x] != 0) | |
6134 | { | |
6135 | if (strncmp (known_suffixes[x], r, q - r) == 0) | |
6136 | { | |
6137 | /* Make q point to the bit we're going to overwrite | |
6138 | with an object suffix. */ | |
6139 | q = r; | |
6140 | break; | |
6141 | } | |
6142 | x++; | |
6143 | } | |
7f2935c7 PB |
6144 | |
6145 | /* Supply our own suffix. */ | |
6146 | #ifndef VMS | |
6147 | strcpy (q, ".o"); | |
6148 | #else | |
6149 | strcpy (q, ".obj"); | |
6150 | #endif | |
6151 | ||
6152 | deps_output (pfile, p, ':'); | |
6153 | deps_output (pfile, opts->in_fname, ' '); | |
6154 | } | |
6155 | } | |
6156 | ||
6157 | #if 0 | |
6158 | /* Make sure data ends with a newline. And put a null after it. */ | |
6159 | ||
6160 | if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n') | |
6161 | /* Backslash-newline at end is not good enough. */ | |
6162 | || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) { | |
6163 | fp->buf[fp->length++] = '\n'; | |
6164 | missing_newline = 1; | |
6165 | } | |
6166 | fp->buf[fp->length] = '\0'; | |
6167 | ||
6168 | /* Unless inhibited, convert trigraphs in the input. */ | |
6169 | ||
6170 | if (!no_trigraphs) | |
6171 | trigraph_pcp (fp); | |
6172 | #endif | |
6173 | ||
22bbceaf PB |
6174 | /* Scan the -include files before the main input. |
6175 | We push these in reverse order, so that the first one is handled first. */ | |
7f2935c7 PB |
6176 | |
6177 | pfile->no_record_file++; | |
22bbceaf | 6178 | opts->pending = nreverse_pending (opts->pending); |
7f2935c7 PB |
6179 | for (pend = opts->pending; pend; pend = pend->next) |
6180 | { | |
6181 | if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0) | |
6182 | { | |
6183 | int fd = open (pend->arg, O_RDONLY, 0666); | |
6184 | if (fd < 0) | |
6185 | { | |
6186 | cpp_perror_with_name (pfile, pend->arg); | |
a94c94be | 6187 | return 0; |
7f2935c7 | 6188 | } |
e2f79f3c PB |
6189 | if (!cpp_push_buffer (pfile, NULL, 0)) |
6190 | return 0; | |
7f2935c7 | 6191 | finclude (pfile, fd, pend->arg, 0, NULL_PTR); |
7f2935c7 PB |
6192 | } |
6193 | } | |
6194 | pfile->no_record_file--; | |
6195 | ||
0f41302f | 6196 | /* Free the pending list. */ |
7f2935c7 PB |
6197 | for (pend = opts->pending; pend; ) |
6198 | { | |
6199 | struct cpp_pending *next = pend->next; | |
6200 | free (pend); | |
6201 | pend = next; | |
6202 | } | |
6203 | opts->pending = NULL; | |
6204 | ||
6205 | #if 0 | |
6206 | /* Scan the input, processing macros and directives. */ | |
6207 | ||
6208 | rescan (&outbuf, 0); | |
6209 | ||
6210 | if (missing_newline) | |
6211 | fp->lineno--; | |
6212 | ||
6213 | if (CPP_PEDANTIC (pfile) && missing_newline) | |
6214 | pedwarn ("file does not end in newline"); | |
6215 | ||
6216 | #endif | |
6bac1e64 | 6217 | if (finclude (pfile, f, fname, 0, NULL_PTR)) |
7f2935c7 | 6218 | output_line_command (pfile, 0, same_file); |
a94c94be | 6219 | return 1; |
7f2935c7 PB |
6220 | } |
6221 | ||
6222 | void | |
a94c94be | 6223 | cpp_reader_init (pfile) |
7f2935c7 PB |
6224 | cpp_reader *pfile; |
6225 | { | |
5f972d0c | 6226 | bzero ((char *) pfile, sizeof (cpp_reader)); |
7f2935c7 PB |
6227 | pfile->get_token = cpp_get_token; |
6228 | ||
6229 | pfile->token_buffer_size = 200; | |
0f41302f | 6230 | pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size); |
7f2935c7 PB |
6231 | CPP_SET_WRITTEN (pfile, 0); |
6232 | ||
6233 | pfile->system_include_depth = 0; | |
6234 | pfile->dont_repeat_files = 0; | |
6235 | pfile->all_include_files = 0; | |
6236 | pfile->max_include_len = 0; | |
6237 | pfile->timebuf = NULL; | |
6238 | pfile->only_seen_white = 1; | |
6239 | pfile->buffer = CPP_NULL_BUFFER(pfile); | |
6240 | } | |
6241 | ||
22bbceaf PB |
6242 | static struct cpp_pending * |
6243 | nreverse_pending (list) | |
6244 | struct cpp_pending *list; | |
6245 | ||
6246 | { | |
6247 | register struct cpp_pending *prev = 0, *next, *pend; | |
6248 | for (pend = list; pend; pend = next) | |
6249 | { | |
6250 | next = pend->next; | |
6251 | pend->next = prev; | |
6252 | prev = pend; | |
6253 | } | |
6254 | return prev; | |
6255 | } | |
6256 | ||
7f2935c7 PB |
6257 | static void |
6258 | push_pending (pfile, cmd, arg) | |
6259 | cpp_reader *pfile; | |
6260 | char *cmd; | |
6261 | char *arg; | |
6262 | { | |
6263 | struct cpp_pending *pend | |
0f41302f | 6264 | = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending)); |
7f2935c7 PB |
6265 | pend->cmd = cmd; |
6266 | pend->arg = arg; | |
6267 | pend->next = CPP_OPTIONS (pfile)->pending; | |
6268 | CPP_OPTIONS (pfile)->pending = pend; | |
6269 | } | |
6270 | ||
6271 | /* Handle command-line options in (argc, argv). | |
6272 | Can be called multiple times, to handle multiple sets of options. | |
6273 | Returns if an unrecognized option is seen. | |
6274 | Returns number of handled arguments. */ | |
6275 | ||
6276 | int | |
6277 | cpp_handle_options (pfile, argc, argv) | |
6278 | cpp_reader *pfile; | |
6279 | int argc; | |
6280 | char **argv; | |
6281 | { | |
6282 | int i; | |
6283 | struct cpp_options *opts = CPP_OPTIONS (pfile); | |
6284 | for (i = 0; i < argc; i++) { | |
6285 | if (argv[i][0] != '-') { | |
6286 | if (opts->out_fname != NULL) | |
a94c94be PB |
6287 | { |
6288 | cpp_fatal (pfile, "Usage: %s [switches] input output", argv[0]); | |
6289 | return argc; | |
6290 | } | |
7f2935c7 PB |
6291 | else if (opts->in_fname != NULL) |
6292 | opts->out_fname = argv[i]; | |
6293 | else | |
6294 | opts->in_fname = argv[i]; | |
6295 | } else { | |
6296 | switch (argv[i][1]) { | |
6297 | ||
a94c94be PB |
6298 | missing_filename: |
6299 | cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]); | |
6300 | return argc; | |
6301 | missing_dirname: | |
6302 | cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]); | |
6303 | return argc; | |
6304 | ||
7f2935c7 PB |
6305 | case 'i': |
6306 | if (!strcmp (argv[i], "-include") | |
6307 | || !strcmp (argv[i], "-imacros")) { | |
6308 | if (i + 1 == argc) | |
a94c94be | 6309 | goto missing_filename; |
7f2935c7 PB |
6310 | else |
6311 | push_pending (pfile, argv[i], argv[i+1]), i++; | |
6312 | } | |
6313 | if (!strcmp (argv[i], "-iprefix")) { | |
6314 | if (i + 1 == argc) | |
a94c94be | 6315 | goto missing_filename; |
7f2935c7 PB |
6316 | else |
6317 | opts->include_prefix = argv[++i]; | |
6318 | } | |
6319 | if (!strcmp (argv[i], "-ifoutput")) { | |
6320 | opts->output_conditionals = 1; | |
6321 | } | |
6322 | if (!strcmp (argv[i], "-isystem")) { | |
6323 | struct file_name_list *dirtmp; | |
6324 | ||
6325 | if (i + 1 == argc) | |
a94c94be | 6326 | goto missing_filename; |
7f2935c7 PB |
6327 | |
6328 | dirtmp = (struct file_name_list *) | |
6329 | xmalloc (sizeof (struct file_name_list)); | |
6330 | dirtmp->next = 0; | |
6331 | dirtmp->control_macro = 0; | |
6332 | dirtmp->c_system_include_path = 1; | |
6333 | dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1); | |
6334 | strcpy (dirtmp->fname, argv[++i]); | |
6335 | dirtmp->got_name_map = 0; | |
6336 | ||
6337 | if (opts->before_system == 0) | |
6338 | opts->before_system = dirtmp; | |
6339 | else | |
6340 | opts->last_before_system->next = dirtmp; | |
6341 | opts->last_before_system = dirtmp; /* Tail follows the last one */ | |
6342 | } | |
6343 | /* Add directory to end of path for includes, | |
6344 | with the default prefix at the front of its name. */ | |
6345 | if (!strcmp (argv[i], "-iwithprefix")) { | |
6346 | struct file_name_list *dirtmp; | |
6347 | char *prefix; | |
6348 | ||
6349 | if (opts->include_prefix != 0) | |
6350 | prefix = opts->include_prefix; | |
6351 | else { | |
6352 | prefix = savestring (GCC_INCLUDE_DIR); | |
6353 | /* Remove the `include' from /usr/local/lib/gcc.../include. */ | |
6354 | if (!strcmp (prefix + strlen (prefix) - 8, "/include")) | |
6355 | prefix[strlen (prefix) - 7] = 0; | |
6356 | } | |
6357 | ||
6358 | dirtmp = (struct file_name_list *) | |
6359 | xmalloc (sizeof (struct file_name_list)); | |
6360 | dirtmp->next = 0; /* New one goes on the end */ | |
6361 | dirtmp->control_macro = 0; | |
6362 | dirtmp->c_system_include_path = 0; | |
6363 | if (i + 1 == argc) | |
a94c94be | 6364 | goto missing_dirname; |
7f2935c7 PB |
6365 | |
6366 | dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) | |
6367 | + strlen (prefix) + 1); | |
6368 | strcpy (dirtmp->fname, prefix); | |
6369 | strcat (dirtmp->fname, argv[++i]); | |
6370 | dirtmp->got_name_map = 0; | |
6371 | ||
6372 | if (opts->after_include == 0) | |
6373 | opts->after_include = dirtmp; | |
6374 | else | |
6375 | opts->last_after_include->next = dirtmp; | |
6376 | opts->last_after_include = dirtmp; /* Tail follows the last one */ | |
6377 | } | |
6378 | /* Add directory to main path for includes, | |
6379 | with the default prefix at the front of its name. */ | |
6380 | if (!strcmp (argv[i], "-iwithprefixbefore")) { | |
6381 | struct file_name_list *dirtmp; | |
6382 | char *prefix; | |
6383 | ||
6384 | if (opts->include_prefix != 0) | |
6385 | prefix = opts->include_prefix; | |
6386 | else { | |
6387 | prefix = savestring (GCC_INCLUDE_DIR); | |
6388 | /* Remove the `include' from /usr/local/lib/gcc.../include. */ | |
6389 | if (!strcmp (prefix + strlen (prefix) - 8, "/include")) | |
6390 | prefix[strlen (prefix) - 7] = 0; | |
6391 | } | |
6392 | ||
6393 | dirtmp = (struct file_name_list *) | |
6394 | xmalloc (sizeof (struct file_name_list)); | |
6395 | dirtmp->next = 0; /* New one goes on the end */ | |
6396 | dirtmp->control_macro = 0; | |
6397 | dirtmp->c_system_include_path = 0; | |
6398 | if (i + 1 == argc) | |
a94c94be | 6399 | goto missing_dirname; |
7f2935c7 PB |
6400 | |
6401 | dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) | |
6402 | + strlen (prefix) + 1); | |
6403 | strcpy (dirtmp->fname, prefix); | |
6404 | strcat (dirtmp->fname, argv[++i]); | |
6405 | dirtmp->got_name_map = 0; | |
6406 | ||
6407 | append_include_chain (pfile, dirtmp, dirtmp); | |
6408 | } | |
6409 | /* Add directory to end of path for includes. */ | |
6410 | if (!strcmp (argv[i], "-idirafter")) { | |
6411 | struct file_name_list *dirtmp; | |
6412 | ||
6413 | dirtmp = (struct file_name_list *) | |
6414 | xmalloc (sizeof (struct file_name_list)); | |
6415 | dirtmp->next = 0; /* New one goes on the end */ | |
6416 | dirtmp->control_macro = 0; | |
6417 | dirtmp->c_system_include_path = 0; | |
6418 | if (i + 1 == argc) | |
a94c94be | 6419 | goto missing_dirname; |
7f2935c7 PB |
6420 | else |
6421 | dirtmp->fname = argv[++i]; | |
6422 | dirtmp->got_name_map = 0; | |
6423 | ||
6424 | if (opts->after_include == 0) | |
6425 | opts->after_include = dirtmp; | |
6426 | else | |
6427 | opts->last_after_include->next = dirtmp; | |
6428 | opts->last_after_include = dirtmp; /* Tail follows the last one */ | |
6429 | } | |
6430 | break; | |
6431 | ||
6432 | case 'o': | |
6433 | if (opts->out_fname != NULL) | |
a94c94be PB |
6434 | { |
6435 | cpp_fatal (pfile, "Output filename specified twice"); | |
6436 | return argc; | |
6437 | } | |
7f2935c7 | 6438 | if (i + 1 == argc) |
a94c94be | 6439 | goto missing_filename; |
7f2935c7 PB |
6440 | opts->out_fname = argv[++i]; |
6441 | if (!strcmp (opts->out_fname, "-")) | |
6442 | opts->out_fname = ""; | |
6443 | break; | |
6444 | ||
6445 | case 'p': | |
6446 | if (!strcmp (argv[i], "-pedantic")) | |
6447 | CPP_PEDANTIC (pfile) = 1; | |
6448 | else if (!strcmp (argv[i], "-pedantic-errors")) { | |
6449 | CPP_PEDANTIC (pfile) = 1; | |
6450 | opts->pedantic_errors = 1; | |
6451 | } | |
6452 | #if 0 | |
6453 | else if (!strcmp (argv[i], "-pcp")) { | |
6454 | char *pcp_fname = argv[++i]; | |
e3da301d MS |
6455 | pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0') |
6456 | ? fopen (pcp_fname, "w") | |
6457 | : fdopen (dup (fileno (stdout)), "w")); | |
7f2935c7 PB |
6458 | if (pcp_outfile == 0) |
6459 | cpp_pfatal_with_name (pfile, pcp_fname); | |
6460 | no_precomp = 1; | |
6461 | } | |
6462 | #endif | |
6463 | break; | |
6464 | ||
6465 | case 't': | |
6466 | if (!strcmp (argv[i], "-traditional")) { | |
6467 | opts->traditional = 1; | |
7f2935c7 PB |
6468 | } else if (!strcmp (argv[i], "-trigraphs")) { |
6469 | if (!opts->chill) | |
6470 | opts->no_trigraphs = 0; | |
6471 | } | |
6472 | break; | |
6473 | ||
6474 | case 'l': | |
6475 | if (! strcmp (argv[i], "-lang-c")) | |
641d4443 RK |
6476 | opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0, |
6477 | opts->objc = 0; | |
6478 | if (! strcmp (argv[i], "-lang-c89")) | |
6479 | opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1, | |
6480 | opts->objc = 0; | |
7f2935c7 | 6481 | if (! strcmp (argv[i], "-lang-c++")) |
641d4443 RK |
6482 | opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0, |
6483 | opts->objc = 0; | |
7f2935c7 | 6484 | if (! strcmp (argv[i], "-lang-objc")) |
641d4443 RK |
6485 | opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0, |
6486 | opts->objc = 1; | |
7f2935c7 | 6487 | if (! strcmp (argv[i], "-lang-objc++")) |
641d4443 RK |
6488 | opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0, |
6489 | opts->objc = 1; | |
7f2935c7 PB |
6490 | if (! strcmp (argv[i], "-lang-asm")) |
6491 | opts->lang_asm = 1; | |
6492 | if (! strcmp (argv[i], "-lint")) | |
6493 | opts->for_lint = 1; | |
6494 | if (! strcmp (argv[i], "-lang-chill")) | |
6495 | opts->objc = 0, opts->cplusplus = 0, opts->chill = 1, | |
6496 | opts->traditional = 1, opts->no_trigraphs = 1; | |
6497 | break; | |
6498 | ||
6499 | case '+': | |
6500 | opts->cplusplus = 1, opts->cplusplus_comments = 1; | |
6501 | break; | |
6502 | ||
6503 | case 'w': | |
6504 | opts->inhibit_warnings = 1; | |
6505 | break; | |
6506 | ||
6507 | case 'W': | |
6508 | if (!strcmp (argv[i], "-Wtrigraphs")) | |
6509 | opts->warn_trigraphs = 1; | |
6510 | else if (!strcmp (argv[i], "-Wno-trigraphs")) | |
6511 | opts->warn_trigraphs = 0; | |
6512 | else if (!strcmp (argv[i], "-Wcomment")) | |
6513 | opts->warn_comments = 1; | |
6514 | else if (!strcmp (argv[i], "-Wno-comment")) | |
6515 | opts->warn_comments = 0; | |
6516 | else if (!strcmp (argv[i], "-Wcomments")) | |
6517 | opts->warn_comments = 1; | |
6518 | else if (!strcmp (argv[i], "-Wno-comments")) | |
6519 | opts->warn_comments = 0; | |
6520 | else if (!strcmp (argv[i], "-Wtraditional")) | |
6521 | opts->warn_stringify = 1; | |
6522 | else if (!strcmp (argv[i], "-Wno-traditional")) | |
6523 | opts->warn_stringify = 0; | |
dfb45725 RK |
6524 | else if (!strcmp (argv[i], "-Wundef")) |
6525 | opts->warn_undef = 1; | |
6526 | else if (!strcmp (argv[i], "-Wno-undef")) | |
6527 | opts->warn_undef = 0; | |
7f2935c7 PB |
6528 | else if (!strcmp (argv[i], "-Wimport")) |
6529 | opts->warn_import = 1; | |
6530 | else if (!strcmp (argv[i], "-Wno-import")) | |
6531 | opts->warn_import = 0; | |
6532 | else if (!strcmp (argv[i], "-Werror")) | |
6533 | opts->warnings_are_errors = 1; | |
6534 | else if (!strcmp (argv[i], "-Wno-error")) | |
6535 | opts->warnings_are_errors = 0; | |
6536 | else if (!strcmp (argv[i], "-Wall")) | |
6537 | { | |
6538 | opts->warn_trigraphs = 1; | |
6539 | opts->warn_comments = 1; | |
6540 | } | |
6541 | break; | |
6542 | ||
6543 | case 'M': | |
6544 | /* The style of the choices here is a bit mixed. | |
6545 | The chosen scheme is a hybrid of keeping all options in one string | |
6546 | and specifying each option in a separate argument: | |
6547 | -M|-MM|-MD file|-MMD file [-MG]. An alternative is: | |
6548 | -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely: | |
6549 | -M[M][G][D file]. This is awkward to handle in specs, and is not | |
6550 | as extensible. */ | |
6551 | /* ??? -MG must be specified in addition to one of -M or -MM. | |
6552 | This can be relaxed in the future without breaking anything. | |
6553 | The converse isn't true. */ | |
6554 | ||
6555 | /* -MG isn't valid with -MD or -MMD. This is checked for later. */ | |
6556 | if (!strcmp (argv[i], "-MG")) | |
6557 | { | |
6558 | opts->print_deps_missing_files = 1; | |
6559 | break; | |
6560 | } | |
6561 | if (!strcmp (argv[i], "-M")) | |
6562 | opts->print_deps = 2; | |
6563 | else if (!strcmp (argv[i], "-MM")) | |
6564 | opts->print_deps = 1; | |
6565 | else if (!strcmp (argv[i], "-MD")) | |
6566 | opts->print_deps = 2; | |
6567 | else if (!strcmp (argv[i], "-MMD")) | |
6568 | opts->print_deps = 1; | |
6569 | /* For -MD and -MMD options, write deps on file named by next arg. */ | |
6570 | if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD")) | |
6571 | { | |
6572 | if (i+1 == argc) | |
a94c94be | 6573 | goto missing_filename; |
7f2935c7 PB |
6574 | opts->deps_file = argv[++i]; |
6575 | } | |
6576 | else | |
6577 | { | |
6578 | /* For -M and -MM, write deps on standard output | |
6579 | and suppress the usual output. */ | |
6580 | opts->no_output = 1; | |
6581 | } | |
6582 | break; | |
6583 | ||
6584 | case 'd': | |
6585 | { | |
6586 | char *p = argv[i] + 2; | |
6587 | char c; | |
6588 | while ((c = *p++) != 0) { | |
6589 | /* Arg to -d specifies what parts of macros to dump */ | |
6590 | switch (c) { | |
6591 | case 'M': | |
6592 | opts->dump_macros = dump_only; | |
6593 | opts->no_output = 1; | |
6594 | break; | |
6595 | case 'N': | |
6596 | opts->dump_macros = dump_names; | |
6597 | break; | |
6598 | case 'D': | |
6599 | opts->dump_macros = dump_definitions; | |
6600 | break; | |
e9a25f70 JL |
6601 | case 'I': |
6602 | opts->dump_includes = 1; | |
6603 | break; | |
7f2935c7 PB |
6604 | } |
6605 | } | |
6606 | } | |
6607 | break; | |
6608 | ||
6609 | case 'g': | |
6610 | if (argv[i][2] == '3') | |
6611 | opts->debug_output = 1; | |
6612 | break; | |
6613 | ||
6614 | case 'v': | |
6615 | fprintf (stderr, "GNU CPP version %s", version_string); | |
6616 | #ifdef TARGET_VERSION | |
6617 | TARGET_VERSION; | |
6618 | #endif | |
6619 | fprintf (stderr, "\n"); | |
6620 | opts->verbose = 1; | |
6621 | break; | |
6622 | ||
6623 | case 'H': | |
6624 | opts->print_include_names = 1; | |
6625 | break; | |
6626 | ||
6627 | case 'D': | |
6628 | if (argv[i][2] != 0) | |
6629 | push_pending (pfile, "-D", argv[i] + 2); | |
6630 | else if (i + 1 == argc) | |
a94c94be PB |
6631 | { |
6632 | cpp_fatal (pfile, "Macro name missing after -D option"); | |
6633 | return argc; | |
6634 | } | |
7f2935c7 PB |
6635 | else |
6636 | i++, push_pending (pfile, "-D", argv[i]); | |
6637 | break; | |
6638 | ||
6639 | case 'A': | |
6640 | { | |
6641 | char *p; | |
6642 | ||
6643 | if (argv[i][2] != 0) | |
6644 | p = argv[i] + 2; | |
6645 | else if (i + 1 == argc) | |
a94c94be PB |
6646 | { |
6647 | cpp_fatal (pfile, "Assertion missing after -A option"); | |
6648 | return argc; | |
6649 | } | |
7f2935c7 PB |
6650 | else |
6651 | p = argv[++i]; | |
6652 | ||
6653 | if (!strcmp (p, "-")) { | |
6654 | struct cpp_pending **ptr; | |
6655 | /* -A- eliminates all predefined macros and assertions. | |
6656 | Let's include also any that were specified earlier | |
6657 | on the command line. That way we can get rid of any | |
6658 | that were passed automatically in from GCC. */ | |
6659 | int j; | |
6660 | opts->inhibit_predefs = 1; | |
6661 | for (ptr = &opts->pending; *ptr != NULL; ) | |
6662 | { | |
6663 | struct cpp_pending *pend = *ptr; | |
6664 | if (pend->cmd && pend->cmd[0] == '-' | |
6665 | && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A')) | |
6666 | { | |
6667 | *ptr = pend->next; | |
6668 | free (pend); | |
6669 | } | |
6670 | else | |
6671 | ptr = &pend->next; | |
6672 | } | |
6673 | } else { | |
6674 | push_pending (pfile, "-A", p); | |
6675 | } | |
6676 | } | |
6677 | break; | |
6678 | ||
6679 | case 'U': /* JF #undef something */ | |
6680 | if (argv[i][2] != 0) | |
6681 | push_pending (pfile, "-U", argv[i] + 2); | |
6682 | else if (i + 1 == argc) | |
a94c94be PB |
6683 | { |
6684 | cpp_fatal (pfile, "Macro name missing after -U option", NULL); | |
6685 | return argc; | |
6686 | } | |
7f2935c7 PB |
6687 | else |
6688 | push_pending (pfile, "-U", argv[i+1]), i++; | |
6689 | break; | |
6690 | ||
6691 | case 'C': | |
6692 | opts->put_out_comments = 1; | |
6693 | break; | |
6694 | ||
6695 | case 'E': /* -E comes from cc -E; ignore it. */ | |
6696 | break; | |
6697 | ||
6698 | case 'P': | |
6699 | opts->no_line_commands = 1; | |
6700 | break; | |
6701 | ||
6702 | case '$': /* Don't include $ in identifiers. */ | |
6703 | opts->dollars_in_ident = 0; | |
6704 | break; | |
6705 | ||
6706 | case 'I': /* Add directory to path for includes. */ | |
6707 | { | |
6708 | struct file_name_list *dirtmp; | |
6709 | ||
6710 | if (! CPP_OPTIONS(pfile)->ignore_srcdir | |
6711 | && !strcmp (argv[i] + 2, "-")) { | |
6712 | CPP_OPTIONS (pfile)->ignore_srcdir = 1; | |
6713 | /* Don't use any preceding -I directories for #include <...>. */ | |
6714 | CPP_OPTIONS (pfile)->first_bracket_include = 0; | |
6715 | } | |
6716 | else { | |
6717 | dirtmp = (struct file_name_list *) | |
6718 | xmalloc (sizeof (struct file_name_list)); | |
6719 | dirtmp->next = 0; /* New one goes on the end */ | |
6720 | dirtmp->control_macro = 0; | |
6721 | dirtmp->c_system_include_path = 0; | |
6722 | if (argv[i][2] != 0) | |
6723 | dirtmp->fname = argv[i] + 2; | |
6724 | else if (i + 1 == argc) | |
a94c94be | 6725 | goto missing_dirname; |
7f2935c7 PB |
6726 | else |
6727 | dirtmp->fname = argv[++i]; | |
6728 | dirtmp->got_name_map = 0; | |
6729 | append_include_chain (pfile, dirtmp, dirtmp); | |
6730 | } | |
6731 | } | |
6732 | break; | |
6733 | ||
6734 | case 'n': | |
6735 | if (!strcmp (argv[i], "-nostdinc")) | |
6736 | /* -nostdinc causes no default include directories. | |
6737 | You must specify all include-file directories with -I. */ | |
6738 | opts->no_standard_includes = 1; | |
6739 | else if (!strcmp (argv[i], "-nostdinc++")) | |
6740 | /* -nostdinc++ causes no default C++-specific include directories. */ | |
6741 | opts->no_standard_cplusplus_includes = 1; | |
6742 | #if 0 | |
6743 | else if (!strcmp (argv[i], "-noprecomp")) | |
6744 | no_precomp = 1; | |
6745 | #endif | |
6746 | break; | |
6747 | ||
6748 | case 'u': | |
6749 | /* Sun compiler passes undocumented switch "-undef". | |
6750 | Let's assume it means to inhibit the predefined symbols. */ | |
6751 | opts->inhibit_predefs = 1; | |
6752 | break; | |
6753 | ||
6754 | case '\0': /* JF handle '-' as file name meaning stdin or stdout */ | |
6755 | if (opts->in_fname == NULL) { | |
6756 | opts->in_fname = ""; | |
6757 | break; | |
6758 | } else if (opts->out_fname == NULL) { | |
6759 | opts->out_fname = ""; | |
6760 | break; | |
6761 | } /* else fall through into error */ | |
6762 | ||
6763 | default: | |
6764 | return i; | |
6765 | } | |
6766 | } | |
6767 | } | |
6768 | return i; | |
6769 | } | |
6770 | \f | |
6771 | void | |
6772 | cpp_finish (pfile) | |
6773 | cpp_reader *pfile; | |
6774 | { | |
6775 | struct cpp_options *opts = CPP_OPTIONS (pfile); | |
6776 | ||
6777 | if (opts->print_deps) | |
6778 | { | |
6779 | /* Stream on which to print the dependency information. */ | |
6780 | FILE *deps_stream; | |
6781 | ||
6782 | /* Don't actually write the deps file if compilation has failed. */ | |
6783 | if (pfile->errors == 0) | |
6784 | { | |
6785 | char *deps_mode = opts->print_deps_append ? "a" : "w"; | |
6786 | if (opts->deps_file == 0) | |
6787 | deps_stream = stdout; | |
6788 | else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0) | |
6789 | cpp_pfatal_with_name (pfile, opts->deps_file); | |
6790 | fputs (pfile->deps_buffer, deps_stream); | |
6791 | putc ('\n', deps_stream); | |
6792 | if (opts->deps_file) | |
6793 | { | |
6794 | if (ferror (deps_stream) || fclose (deps_stream) != 0) | |
a94c94be | 6795 | cpp_fatal (pfile, "I/O error on output"); |
7f2935c7 PB |
6796 | } |
6797 | } | |
6798 | } | |
6799 | } | |
782331f4 | 6800 | |
d013f05e | 6801 | /* Free resources used by PFILE. |
0f41302f | 6802 | This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */ |
782331f4 PB |
6803 | |
6804 | void | |
6805 | cpp_cleanup (pfile) | |
6806 | cpp_reader *pfile; | |
6807 | { | |
6808 | int i; | |
6809 | while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile)) | |
6810 | cpp_pop_buffer (pfile); | |
6811 | ||
6812 | if (pfile->token_buffer) | |
6813 | { | |
6814 | free (pfile->token_buffer); | |
6815 | pfile->token_buffer = NULL; | |
6816 | } | |
6817 | ||
6818 | if (pfile->deps_buffer) | |
6819 | { | |
6820 | free (pfile->deps_buffer); | |
6821 | pfile->deps_buffer = NULL; | |
6822 | pfile->deps_allocated_size = 0; | |
6823 | } | |
6824 | ||
6825 | while (pfile->if_stack) | |
6826 | { | |
6827 | IF_STACK_FRAME *temp = pfile->if_stack; | |
6828 | pfile->if_stack = temp->next; | |
6829 | free (temp); | |
6830 | } | |
6831 | ||
6832 | while (pfile->dont_repeat_files) | |
6833 | { | |
6834 | struct file_name_list *temp = pfile->dont_repeat_files; | |
6835 | pfile->dont_repeat_files = temp->next; | |
6836 | free (temp->fname); | |
6837 | free (temp); | |
6838 | } | |
6839 | ||
6840 | while (pfile->all_include_files) | |
6841 | { | |
6842 | struct file_name_list *temp = pfile->all_include_files; | |
6843 | pfile->all_include_files = temp->next; | |
6844 | free (temp->fname); | |
6845 | free (temp); | |
6846 | } | |
6847 | ||
6848 | for (i = IMPORT_HASH_SIZE; --i >= 0; ) | |
6849 | { | |
6850 | register struct import_file *imp = pfile->import_hash_table[i]; | |
6851 | while (imp) | |
6852 | { | |
6853 | struct import_file *next = imp->next; | |
6854 | free (imp->name); | |
6855 | free (imp); | |
6856 | imp = next; | |
6857 | } | |
6858 | pfile->import_hash_table[i] = 0; | |
6859 | } | |
6860 | ||
6861 | for (i = ASSERTION_HASHSIZE; --i >= 0; ) | |
6862 | { | |
6863 | while (pfile->assertion_hashtab[i]) | |
6864 | delete_assertion (pfile->assertion_hashtab[i]); | |
6865 | } | |
6866 | ||
6867 | cpp_hash_cleanup (pfile); | |
6868 | } | |
7f2935c7 PB |
6869 | \f |
6870 | static int | |
6871 | do_assert (pfile, keyword, buf, limit) | |
6872 | cpp_reader *pfile; | |
6873 | struct directive *keyword; | |
6874 | U_CHAR *buf, *limit; | |
6875 | { | |
6876 | long symstart; /* remember where symbol name starts */ | |
6877 | int c; | |
6878 | int sym_length; /* and how long it is */ | |
6879 | struct arglist *tokens = NULL; | |
6880 | ||
6881 | if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing | |
6882 | && !CPP_BUFFER (pfile)->system_header_p) | |
6883 | cpp_pedwarn (pfile, "ANSI C does not allow `#assert'"); | |
6884 | ||
6885 | cpp_skip_hspace (pfile); | |
6886 | symstart = CPP_WRITTEN (pfile); /* remember where it starts */ | |
6887 | parse_name (pfile, GETC()); | |
6888 | sym_length = check_macro_name (pfile, pfile->token_buffer + symstart, | |
6889 | "assertion"); | |
6890 | ||
6891 | cpp_skip_hspace (pfile); | |
6892 | if (PEEKC() != '(') { | |
6893 | cpp_error (pfile, "missing token-sequence in `#assert'"); | |
6894 | goto error; | |
6895 | } | |
6896 | ||
6897 | { | |
6898 | int error_flag = 0; | |
6899 | tokens = read_token_list (pfile, &error_flag); | |
6900 | if (error_flag) | |
6901 | goto error; | |
6902 | if (tokens == 0) { | |
6903 | cpp_error (pfile, "empty token-sequence in `#assert'"); | |
6904 | goto error; | |
6905 | } | |
6906 | cpp_skip_hspace (pfile); | |
6907 | c = PEEKC (); | |
6908 | if (c != EOF && c != '\n') | |
6909 | cpp_pedwarn (pfile, "junk at end of `#assert'"); | |
6910 | skip_rest_of_line (pfile); | |
6911 | } | |
6912 | ||
6913 | /* If this name isn't already an assertion name, make it one. | |
6914 | Error if it was already in use in some other way. */ | |
6915 | ||
6916 | { | |
6917 | ASSERTION_HASHNODE *hp; | |
6918 | U_CHAR *symname = pfile->token_buffer + symstart; | |
6919 | int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE); | |
6920 | struct tokenlist_list *value | |
6921 | = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list)); | |
6922 | ||
6923 | hp = assertion_lookup (pfile, symname, sym_length, hashcode); | |
6924 | if (hp == NULL) { | |
6925 | if (sym_length == 7 && ! strncmp (symname, "defined", sym_length)) | |
6926 | cpp_error (pfile, "`defined' redefined as assertion"); | |
6927 | hp = assertion_install (pfile, symname, sym_length, hashcode); | |
6928 | } | |
6929 | ||
6930 | /* Add the spec'd token-sequence to the list of such. */ | |
6931 | value->tokens = tokens; | |
6932 | value->next = hp->value; | |
6933 | hp->value = value; | |
6934 | } | |
6935 | CPP_SET_WRITTEN (pfile, symstart); /* Pop */ | |
6936 | return 0; | |
6937 | error: | |
6938 | CPP_SET_WRITTEN (pfile, symstart); /* Pop */ | |
6939 | skip_rest_of_line (pfile); | |
6940 | return 1; | |
6941 | } | |
6942 | \f | |
6943 | static int | |
6944 | do_unassert (pfile, keyword, buf, limit) | |
6945 | cpp_reader *pfile; | |
6946 | struct directive *keyword; | |
6947 | U_CHAR *buf, *limit; | |
6948 | { | |
6949 | long symstart; /* remember where symbol name starts */ | |
6950 | int sym_length; /* and how long it is */ | |
6951 | int c; | |
6952 | ||
6953 | struct arglist *tokens = NULL; | |
6954 | int tokens_specified = 0; | |
6955 | ||
6956 | if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing | |
6957 | && !CPP_BUFFER (pfile)->system_header_p) | |
6958 | cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'"); | |
6959 | ||
6960 | cpp_skip_hspace (pfile); | |
6961 | ||
6962 | symstart = CPP_WRITTEN (pfile); /* remember where it starts */ | |
6963 | parse_name (pfile, GETC()); | |
6964 | sym_length = check_macro_name (pfile, pfile->token_buffer + symstart, | |
6965 | "assertion"); | |
6966 | ||
6967 | cpp_skip_hspace (pfile); | |
6968 | if (PEEKC() == '(') { | |
6969 | int error_flag = 0; | |
6970 | ||
6971 | tokens = read_token_list (pfile, &error_flag); | |
6972 | if (error_flag) | |
6973 | goto error; | |
6974 | if (tokens == 0) { | |
6975 | cpp_error (pfile, "empty token list in `#unassert'"); | |
6976 | goto error; | |
6977 | } | |
6978 | ||
6979 | tokens_specified = 1; | |
6980 | } | |
6981 | ||
6982 | cpp_skip_hspace (pfile); | |
6983 | c = PEEKC (); | |
6984 | if (c != EOF && c != '\n') | |
6985 | cpp_error (pfile, "junk at end of `#unassert'"); | |
6986 | skip_rest_of_line (pfile); | |
6987 | ||
6988 | { | |
6989 | ASSERTION_HASHNODE *hp; | |
6990 | U_CHAR *symname = pfile->token_buffer + symstart; | |
6991 | int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE); | |
6992 | struct tokenlist_list *tail, *prev; | |
6993 | ||
6994 | hp = assertion_lookup (pfile, symname, sym_length, hashcode); | |
6995 | if (hp == NULL) | |
6996 | return 1; | |
6997 | ||
6998 | /* If no token list was specified, then eliminate this assertion | |
6999 | entirely. */ | |
782331f4 | 7000 | if (! tokens_specified) |
7f2935c7 | 7001 | delete_assertion (hp); |
782331f4 | 7002 | else { |
7f2935c7 PB |
7003 | /* If a list of tokens was given, then delete any matching list. */ |
7004 | ||
7005 | tail = hp->value; | |
7006 | prev = 0; | |
7007 | while (tail) { | |
7008 | struct tokenlist_list *next = tail->next; | |
7009 | if (compare_token_lists (tail->tokens, tokens)) { | |
7010 | if (prev) | |
7011 | prev->next = next; | |
7012 | else | |
7013 | hp->value = tail->next; | |
7014 | free_token_list (tail->tokens); | |
7015 | free (tail); | |
7016 | } else { | |
7017 | prev = tail; | |
7018 | } | |
7019 | tail = next; | |
7020 | } | |
7021 | } | |
7022 | } | |
7023 | ||
7024 | CPP_SET_WRITTEN (pfile, symstart); /* Pop */ | |
7025 | return 0; | |
7026 | error: | |
7027 | CPP_SET_WRITTEN (pfile, symstart); /* Pop */ | |
7028 | skip_rest_of_line (pfile); | |
7029 | return 1; | |
7030 | } | |
7031 | \f | |
7032 | /* Test whether there is an assertion named NAME | |
7033 | and optionally whether it has an asserted token list TOKENS. | |
7034 | NAME is not null terminated; its length is SYM_LENGTH. | |
7035 | If TOKENS_SPECIFIED is 0, then don't check for any token list. */ | |
7036 | ||
7037 | int | |
7038 | check_assertion (pfile, name, sym_length, tokens_specified, tokens) | |
7039 | cpp_reader *pfile; | |
7040 | U_CHAR *name; | |
7041 | int sym_length; | |
7042 | int tokens_specified; | |
7043 | struct arglist *tokens; | |
7044 | { | |
7045 | ASSERTION_HASHNODE *hp; | |
7046 | int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE); | |
7047 | ||
7048 | if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p) | |
7049 | cpp_pedwarn (pfile, "ANSI C does not allow testing assertions"); | |
7050 | ||
7051 | hp = assertion_lookup (pfile, name, sym_length, hashcode); | |
7052 | if (hp == NULL) | |
7053 | /* It is not an assertion; just return false. */ | |
7054 | return 0; | |
7055 | ||
7056 | /* If no token list was specified, then value is 1. */ | |
7057 | if (! tokens_specified) | |
7058 | return 1; | |
7059 | ||
7060 | { | |
7061 | struct tokenlist_list *tail; | |
7062 | ||
7063 | tail = hp->value; | |
7064 | ||
7065 | /* If a list of tokens was given, | |
7066 | then succeed if the assertion records a matching list. */ | |
7067 | ||
7068 | while (tail) { | |
7069 | if (compare_token_lists (tail->tokens, tokens)) | |
7070 | return 1; | |
7071 | tail = tail->next; | |
7072 | } | |
7073 | ||
7074 | /* Fail if the assertion has no matching list. */ | |
7075 | return 0; | |
7076 | } | |
7077 | } | |
7078 | ||
7079 | /* Compare two lists of tokens for equality including order of tokens. */ | |
7080 | ||
7081 | static int | |
7082 | compare_token_lists (l1, l2) | |
7083 | struct arglist *l1, *l2; | |
7084 | { | |
7085 | while (l1 && l2) { | |
7086 | if (l1->length != l2->length) | |
7087 | return 0; | |
7088 | if (strncmp (l1->name, l2->name, l1->length)) | |
7089 | return 0; | |
7090 | l1 = l1->next; | |
7091 | l2 = l2->next; | |
7092 | } | |
7093 | ||
7094 | /* Succeed if both lists end at the same time. */ | |
7095 | return l1 == l2; | |
7096 | } | |
7097 | \f | |
7098 | struct arglist * | |
7099 | reverse_token_list (tokens) | |
7100 | struct arglist *tokens; | |
7101 | { | |
7102 | register struct arglist *prev = 0, *this, *next; | |
7103 | for (this = tokens; this; this = next) | |
7104 | { | |
7105 | next = this->next; | |
7106 | this->next = prev; | |
7107 | prev = this; | |
7108 | } | |
7109 | return prev; | |
7110 | } | |
7111 | ||
7112 | /* Read a space-separated list of tokens ending in a close parenthesis. | |
7113 | Return a list of strings, in the order they were written. | |
7114 | (In case of error, return 0 and store -1 in *ERROR_FLAG.) */ | |
7115 | ||
7116 | static struct arglist * | |
7117 | read_token_list (pfile, error_flag) | |
7118 | cpp_reader *pfile; | |
7119 | int *error_flag; | |
7120 | { | |
7121 | struct arglist *token_ptrs = 0; | |
7122 | int depth = 1; | |
7123 | int length; | |
7124 | ||
7125 | *error_flag = 0; | |
7126 | FORWARD (1); /* Skip '(' */ | |
7127 | ||
7128 | /* Loop over the assertion value tokens. */ | |
7129 | while (depth > 0) | |
7130 | { | |
7131 | struct arglist *temp; | |
7132 | long name_written = CPP_WRITTEN (pfile); | |
7133 | int eofp = 0; int c; | |
7134 | ||
7135 | cpp_skip_hspace (pfile); | |
7136 | ||
7137 | c = GETC (); | |
7138 | ||
7139 | /* Find the end of the token. */ | |
7140 | if (c == '(') | |
7141 | { | |
7142 | CPP_PUTC (pfile, c); | |
7143 | depth++; | |
7144 | } | |
7145 | else if (c == ')') | |
7146 | { | |
7147 | depth--; | |
7148 | if (depth == 0) | |
7149 | break; | |
7150 | CPP_PUTC (pfile, c); | |
7151 | } | |
7152 | else if (c == '"' || c == '\'') | |
7153 | { | |
7154 | FORWARD(-1); | |
7155 | cpp_get_token (pfile); | |
7156 | } | |
7157 | else if (c == '\n') | |
7158 | break; | |
7159 | else | |
7160 | { | |
7161 | while (c != EOF && ! is_space[c] && c != '(' && c != ')' | |
7162 | && c != '"' && c != '\'') | |
7163 | { | |
7164 | CPP_PUTC (pfile, c); | |
7165 | c = GETC(); | |
7166 | } | |
7167 | if (c != EOF) FORWARD(-1); | |
7168 | } | |
7169 | ||
7170 | length = CPP_WRITTEN (pfile) - name_written; | |
7171 | temp = (struct arglist *) | |
7172 | xmalloc (sizeof (struct arglist) + length + 1); | |
7173 | temp->name = (U_CHAR *) (temp + 1); | |
7174 | bcopy ((char *) (pfile->token_buffer + name_written), | |
7175 | (char *) temp->name, length); | |
7176 | temp->name[length] = 0; | |
7177 | temp->next = token_ptrs; | |
7178 | token_ptrs = temp; | |
7179 | temp->length = length; | |
7180 | ||
7181 | CPP_ADJUST_WRITTEN (pfile, -length); /* pop */ | |
7182 | ||
7183 | if (c == EOF || c == '\n') | |
7184 | { /* FIXME */ | |
7185 | cpp_error (pfile, | |
7186 | "unterminated token sequence following `#' operator"); | |
7187 | return 0; | |
7188 | } | |
7189 | } | |
7190 | ||
7191 | /* We accumulated the names in reverse order. | |
7192 | Now reverse them to get the proper order. */ | |
7193 | return reverse_token_list (token_ptrs); | |
7194 | } | |
7195 | ||
7196 | static void | |
7197 | free_token_list (tokens) | |
7198 | struct arglist *tokens; | |
7199 | { | |
7200 | while (tokens) { | |
7201 | struct arglist *next = tokens->next; | |
7202 | free (tokens->name); | |
7203 | free (tokens); | |
7204 | tokens = next; | |
7205 | } | |
7206 | } | |
7207 | \f | |
7208 | /* Get the file-mode and data size of the file open on FD | |
7209 | and store them in *MODE_POINTER and *SIZE_POINTER. */ | |
7210 | ||
7211 | static int | |
7212 | file_size_and_mode (fd, mode_pointer, size_pointer) | |
7213 | int fd; | |
7214 | int *mode_pointer; | |
7215 | long int *size_pointer; | |
7216 | { | |
7217 | struct stat sbuf; | |
7218 | ||
7219 | if (fstat (fd, &sbuf) < 0) return (-1); | |
7220 | if (mode_pointer) *mode_pointer = sbuf.st_mode; | |
7221 | if (size_pointer) *size_pointer = sbuf.st_size; | |
7222 | return 0; | |
7223 | } | |
7224 | ||
7225 | /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME, | |
e83dc357 RK |
7226 | retrying if necessary. If MAX_READ_LEN is defined, read at most |
7227 | that bytes at a time. Return a negative value if an error occurs, | |
7f2935c7 PB |
7228 | otherwise return the actual number of bytes read, |
7229 | which must be LEN unless end-of-file was reached. */ | |
7230 | ||
7231 | static int | |
7232 | safe_read (desc, ptr, len) | |
7233 | int desc; | |
7234 | char *ptr; | |
7235 | int len; | |
7236 | { | |
e83dc357 RK |
7237 | int left, rcount, nchars; |
7238 | ||
7239 | left = len; | |
7f2935c7 | 7240 | while (left > 0) { |
e83dc357 RK |
7241 | rcount = left; |
7242 | #ifdef MAX_READ_LEN | |
7243 | if (rcount > MAX_READ_LEN) | |
7244 | rcount = MAX_READ_LEN; | |
7245 | #endif | |
7246 | nchars = read (desc, ptr, rcount); | |
7f2935c7 PB |
7247 | if (nchars < 0) |
7248 | { | |
7249 | #ifdef EINTR | |
7250 | if (errno == EINTR) | |
7251 | continue; | |
7252 | #endif | |
7253 | return nchars; | |
7254 | } | |
7255 | if (nchars == 0) | |
7256 | break; | |
7257 | ptr += nchars; | |
7258 | left -= nchars; | |
7259 | } | |
7260 | return len - left; | |
7261 | } | |
7262 | ||
e2f79f3c PB |
7263 | static char * |
7264 | xcalloc (number, size) | |
7265 | unsigned number, size; | |
7266 | { | |
7267 | register unsigned total = number * size; | |
7268 | register char *ptr = (char *) xmalloc (total); | |
7269 | bzero (ptr, total); | |
7270 | return ptr; | |
7271 | } | |
7272 | ||
7f2935c7 PB |
7273 | static char * |
7274 | savestring (input) | |
7275 | char *input; | |
7276 | { | |
7277 | unsigned size = strlen (input); | |
7278 | char *output = xmalloc (size + 1); | |
7279 | strcpy (output, input); | |
7280 | return output; | |
7281 | } | |
7282 | \f | |
0f41302f MS |
7283 | /* Initialize PMARK to remember the current position of PFILE. */ |
7284 | ||
7f2935c7 PB |
7285 | void |
7286 | parse_set_mark (pmark, pfile) | |
7287 | struct parse_marker *pmark; | |
7288 | cpp_reader *pfile; | |
7289 | { | |
7290 | cpp_buffer *pbuf = CPP_BUFFER (pfile); | |
7291 | pmark->next = pbuf->marks; | |
7292 | pbuf->marks = pmark; | |
7293 | pmark->buf = pbuf; | |
7294 | pmark->position = pbuf->cur - pbuf->buf; | |
7295 | } | |
7296 | ||
0f41302f MS |
7297 | /* Cleanup PMARK - we no longer need it. */ |
7298 | ||
7f2935c7 PB |
7299 | void |
7300 | parse_clear_mark (pmark) | |
7301 | struct parse_marker *pmark; | |
7302 | { | |
7303 | struct parse_marker **pp = &pmark->buf->marks; | |
7304 | for (; ; pp = &(*pp)->next) { | |
e2f79f3c | 7305 | if (*pp == NULL) abort (); |
7f2935c7 PB |
7306 | if (*pp == pmark) break; |
7307 | } | |
7308 | *pp = pmark->next; | |
7309 | } | |
7310 | ||
0f41302f | 7311 | /* Backup the current position of PFILE to that saved in PMARK. */ |
7f2935c7 PB |
7312 | |
7313 | void | |
7314 | parse_goto_mark (pmark, pfile) | |
7315 | struct parse_marker *pmark; | |
7316 | cpp_reader *pfile; | |
7317 | { | |
7318 | cpp_buffer *pbuf = CPP_BUFFER (pfile); | |
7319 | if (pbuf != pmark->buf) | |
a94c94be | 7320 | cpp_fatal (pfile, "internal error %s", "parse_goto_mark"); |
7f2935c7 PB |
7321 | pbuf->cur = pbuf->buf + pmark->position; |
7322 | } | |
7323 | ||
7324 | /* Reset PMARK to point to the current position of PFILE. (Same | |
0f41302f | 7325 | as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */ |
7f2935c7 PB |
7326 | |
7327 | void | |
7328 | parse_move_mark (pmark, pfile) | |
7329 | struct parse_marker *pmark; | |
7330 | cpp_reader *pfile; | |
7331 | { | |
7332 | cpp_buffer *pbuf = CPP_BUFFER (pfile); | |
7333 | if (pbuf != pmark->buf) | |
a94c94be | 7334 | cpp_fatal (pfile, "internal error %s", "parse_move_mark"); |
7f2935c7 PB |
7335 | pmark->position = pbuf->cur - pbuf->buf; |
7336 | } | |
7337 | ||
7338 | int | |
7339 | cpp_read_check_assertion (pfile) | |
7340 | cpp_reader *pfile; | |
7341 | { | |
7342 | int name_start = CPP_WRITTEN (pfile); | |
7343 | int name_length, name_written; | |
7344 | int result; | |
7345 | FORWARD (1); /* Skip '#' */ | |
7346 | cpp_skip_hspace (pfile); | |
7347 | parse_name (pfile, GETC ()); | |
7348 | name_written = CPP_WRITTEN (pfile); | |
7349 | name_length = name_written - name_start; | |
7350 | cpp_skip_hspace (pfile); | |
7351 | if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(') | |
7352 | { | |
7353 | int error_flag; | |
7354 | struct arglist *token_ptrs = read_token_list (pfile, &error_flag); | |
7355 | result = check_assertion (pfile, | |
7356 | pfile->token_buffer + name_start, name_length, | |
7357 | 1, token_ptrs); | |
7358 | } | |
7359 | else | |
7360 | result = check_assertion (pfile, | |
7361 | pfile->token_buffer + name_start, name_length, | |
7362 | 0, NULL_PTR); | |
7363 | CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */ | |
7364 | return result; | |
7365 | } | |
355142da PB |
7366 | \f |
7367 | void | |
7368 | cpp_print_file_and_line (pfile) | |
7369 | cpp_reader *pfile; | |
7370 | { | |
7371 | cpp_buffer *ip = cpp_file_buffer (pfile); | |
7372 | ||
7373 | if (ip != NULL) | |
7374 | { | |
7375 | long line, col; | |
7376 | cpp_buf_line_and_col (ip, &line, &col); | |
7377 | cpp_file_line_for_message (pfile, ip->nominal_fname, | |
7378 | line, pfile->show_column ? col : -1); | |
7379 | } | |
7380 | } | |
7381 | ||
7382 | void | |
7383 | cpp_error (pfile, msg, arg1, arg2, arg3) | |
7384 | cpp_reader *pfile; | |
7385 | char *msg; | |
7386 | char *arg1, *arg2, *arg3; | |
7387 | { | |
7388 | cpp_print_containing_files (pfile); | |
7389 | cpp_print_file_and_line (pfile); | |
7390 | cpp_message (pfile, 1, msg, arg1, arg2, arg3); | |
7391 | } | |
7392 | ||
7393 | /* Print error message but don't count it. */ | |
7394 | ||
7395 | void | |
7396 | cpp_warning (pfile, msg, arg1, arg2, arg3) | |
7397 | cpp_reader *pfile; | |
7398 | char *msg; | |
7399 | char *arg1, *arg2, *arg3; | |
7400 | { | |
7401 | if (CPP_OPTIONS (pfile)->inhibit_warnings) | |
7402 | return; | |
7403 | ||
7404 | if (CPP_OPTIONS (pfile)->warnings_are_errors) | |
7405 | pfile->errors++; | |
7406 | ||
7407 | cpp_print_containing_files (pfile); | |
7408 | cpp_print_file_and_line (pfile); | |
7409 | cpp_message (pfile, 0, msg, arg1, arg2, arg3); | |
7410 | } | |
7411 | ||
7412 | /* Print an error message and maybe count it. */ | |
7413 | ||
7414 | void | |
7415 | cpp_pedwarn (pfile, msg, arg1, arg2, arg3) | |
7416 | cpp_reader *pfile; | |
7417 | char *msg; | |
7418 | char *arg1, *arg2, *arg3; | |
7419 | { | |
7420 | if (CPP_OPTIONS (pfile)->pedantic_errors) | |
7421 | cpp_error (pfile, msg, arg1, arg2, arg3); | |
7422 | else | |
7423 | cpp_warning (pfile, msg, arg1, arg2, arg3); | |
7424 | } | |
7425 | ||
7426 | void | |
3232050c | 7427 | cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3) |
355142da | 7428 | cpp_reader *pfile; |
3232050c | 7429 | int line, column; |
355142da PB |
7430 | char *msg; |
7431 | char *arg1, *arg2, *arg3; | |
7432 | { | |
7433 | int i; | |
7434 | cpp_buffer *ip = cpp_file_buffer (pfile); | |
7435 | ||
7436 | cpp_print_containing_files (pfile); | |
7437 | ||
7438 | if (ip != NULL) | |
3232050c | 7439 | cpp_file_line_for_message (pfile, ip->nominal_fname, line, column); |
355142da PB |
7440 | |
7441 | cpp_message (pfile, 1, msg, arg1, arg2, arg3); | |
7442 | } | |
7443 | ||
3232050c PB |
7444 | static void |
7445 | cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3) | |
355142da | 7446 | cpp_reader *pfile; |
3232050c | 7447 | int line, column; |
355142da PB |
7448 | char *msg; |
7449 | char *arg1, *arg2, *arg3; | |
7450 | { | |
7451 | int i; | |
7452 | cpp_buffer *ip; | |
7453 | ||
7454 | if (CPP_OPTIONS (pfile)->inhibit_warnings) | |
7455 | return; | |
7456 | ||
7457 | if (CPP_OPTIONS (pfile)->warnings_are_errors) | |
7458 | pfile->errors++; | |
7459 | ||
7460 | cpp_print_containing_files (pfile); | |
7461 | ||
7462 | ip = cpp_file_buffer (pfile); | |
7463 | ||
7464 | if (ip != NULL) | |
3232050c | 7465 | cpp_file_line_for_message (pfile, ip->nominal_fname, line, column); |
355142da PB |
7466 | |
7467 | cpp_message (pfile, 0, msg, arg1, arg2, arg3); | |
7468 | } | |
7469 | ||
7470 | void | |
3232050c | 7471 | cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3) |
355142da PB |
7472 | cpp_reader *pfile; |
7473 | int line; | |
7474 | char *msg; | |
7475 | char *arg1, *arg2, *arg3; | |
7476 | { | |
7477 | if (CPP_OPTIONS (pfile)->pedantic_errors) | |
3232050c | 7478 | cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3); |
355142da | 7479 | else |
3232050c | 7480 | cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3); |
355142da PB |
7481 | } |
7482 | ||
7483 | /* Report a warning (or an error if pedantic_errors) | |
7484 | giving specified file name and line number, not current. */ | |
7485 | ||
7486 | void | |
7487 | cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3) | |
7488 | cpp_reader *pfile; | |
7489 | char *file; | |
7490 | int line; | |
7491 | char *msg; | |
7492 | char *arg1, *arg2, *arg3; | |
7493 | { | |
7494 | if (!CPP_OPTIONS (pfile)->pedantic_errors | |
7495 | && CPP_OPTIONS (pfile)->inhibit_warnings) | |
7496 | return; | |
7497 | if (file != NULL) | |
7498 | cpp_file_line_for_message (pfile, file, line, -1); | |
7499 | cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, | |
7500 | msg, arg1, arg2, arg3); | |
7501 | } | |
7502 | ||
0f41302f | 7503 | /* This defines "errno" properly for VMS, and gives us EACCES. */ |
355142da PB |
7504 | #include <errno.h> |
7505 | #ifndef errno | |
7506 | extern int errno; | |
7507 | #endif | |
7508 | ||
7509 | #ifndef VMS | |
7510 | #ifndef HAVE_STRERROR | |
7511 | extern int sys_nerr; | |
355142da | 7512 | extern char *sys_errlist[]; |
ddd5a7c1 | 7513 | #else /* HAVE_STRERROR */ |
355142da PB |
7514 | char *strerror (); |
7515 | #endif | |
7516 | #else /* VMS */ | |
7517 | char *strerror (int,...); | |
7518 | #endif | |
7519 | ||
0f41302f MS |
7520 | /* my_strerror - return the descriptive text associated with an |
7521 | `errno' code. */ | |
355142da PB |
7522 | |
7523 | char * | |
7524 | my_strerror (errnum) | |
7525 | int errnum; | |
7526 | { | |
7527 | char *result; | |
7528 | ||
7529 | #ifndef VMS | |
7530 | #ifndef HAVE_STRERROR | |
7531 | result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0); | |
7532 | #else | |
7533 | result = strerror (errnum); | |
7534 | #endif | |
7535 | #else /* VMS */ | |
7536 | /* VAXCRTL's strerror() takes an optional second argument, which only | |
7537 | matters when the first argument is EVMSERR. However, it's simplest | |
7538 | just to pass it unconditionally. `vaxc$errno' is declared in | |
7539 | <errno.h>, and maintained by the library in parallel with `errno'. | |
7540 | We assume that caller's `errnum' either matches the last setting of | |
7541 | `errno' by the library or else does not have the value `EVMSERR'. */ | |
7542 | ||
7543 | result = strerror (errnum, vaxc$errno); | |
7544 | #endif | |
7545 | ||
7546 | if (!result) | |
7547 | result = "undocumented I/O error"; | |
7548 | ||
7549 | return result; | |
7550 | } | |
7551 | ||
7552 | /* Error including a message from `errno'. */ | |
7553 | ||
7554 | void | |
7555 | cpp_error_from_errno (pfile, name) | |
7556 | cpp_reader *pfile; | |
7557 | char *name; | |
7558 | { | |
7559 | int i; | |
7560 | cpp_buffer *ip = cpp_file_buffer (pfile); | |
7561 | ||
7562 | cpp_print_containing_files (pfile); | |
7563 | ||
7564 | if (ip != NULL) | |
7565 | cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1); | |
7566 | ||
22bbceaf | 7567 | cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno)); |
355142da PB |
7568 | } |
7569 | ||
7570 | void | |
7571 | cpp_perror_with_name (pfile, name) | |
7572 | cpp_reader *pfile; | |
7573 | char *name; | |
7574 | { | |
22bbceaf | 7575 | cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno)); |
355142da | 7576 | } |
7f2935c7 PB |
7577 | |
7578 | /* TODO: | |
7579 | * No pre-compiled header file support. | |
7580 | * | |
7581 | * Possibly different enum token codes for each C/C++ token. | |
7582 | * | |
7f2935c7 PB |
7583 | * Should clean up remaining directives to that do_XXX functions |
7584 | * only take two arguments and all have command_reads_line. | |
7585 | * | |
7586 | * Find and cleanup remaining uses of static variables, | |
7587 | * | |
7588 | * Support for trigraphs. | |
7589 | * | |
7590 | * Support -dM flag (dump_all_macros). | |
782331f4 PB |
7591 | * |
7592 | * Support for_lint flag. | |
7f2935c7 | 7593 | */ |