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