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